comment change. This is a mips file, not 88k.
[deliverable/binutils-gdb.git] / bfd / aoutx.h
CommitLineData
1f29e30b 1/* BFD semi-generic back-end for a.out binaries.
a99c3d70 2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
88dfcd68 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 122
0f213cc2 123#include <assert.h>
a99c3d70 124#include <string.h> /* For strchr and friends */
67c060c3 125#include "bfd.h"
7ed4093a
SC
126#include <sysdep.h>
127#include <ansidecl.h>
128
9e2dad8e 129struct external_exec;
6f715d66 130#include "libaout.h"
7ed4093a 131#include "libbfd.h"
c3eb25fc
SC
132#include "aout/aout64.h"
133#include "aout/stab_gnu.h"
134#include "aout/ar.h"
7ed4093a 135
ce07dd7c 136extern void (*bfd_error_trap)();
7ed4093a 137
4e41b5aa
SC
138/*
139SUBSECTION
140 relocations
141
142DESCRIPTION
143 The file @code{aoutx.h} caters for both the @emph{standard}
144 and @emph{extended} forms of a.out relocation records.
145
146 The standard records are characterised by containing only an
147 address, a symbol index and a type field. The extended records
148 (used on 29ks and sparcs) also have a full integer for an
149 addend.
7ed4093a 150
6f715d66 151*/
7ed4093a 152#define CTOR_TABLE_RELOC_IDX 2
67c060c3 153
ce07dd7c
KR
154#define howto_table_ext NAME(aout,ext_howto_table)
155#define howto_table_std NAME(aout,std_howto_table)
67c060c3 156
ce07dd7c 157reloc_howto_type howto_table_ext[] =
7ed4093a
SC
158{
159 HOWTO(RELOC_8, 0, 0, 8, false, 0, true, true,0,"8", false, 0,0x000000ff, false),
160 HOWTO(RELOC_16, 0, 1, 16, false, 0, true, true,0,"16", false, 0,0x0000ffff, false),
161 HOWTO(RELOC_32, 0, 2, 32, false, 0, true, true,0,"32", false, 0,0xffffffff, false),
162 HOWTO(RELOC_DISP8, 0, 0, 8, true, 0, false, true,0,"DISP8", false, 0,0x000000ff, false),
163 HOWTO(RELOC_DISP16, 0, 1, 16, true, 0, false, true,0,"DISP16", false, 0,0x0000ffff, false),
164 HOWTO(RELOC_DISP32, 0, 2, 32, true, 0, false, true,0,"DISP32", false, 0,0xffffffff, false),
165 HOWTO(RELOC_WDISP30,2, 2, 30, true, 0, false, true,0,"WDISP30", false, 0,0x3fffffff, false),
166 HOWTO(RELOC_WDISP22,2, 2, 22, true, 0, false, true,0,"WDISP22", false, 0,0x003fffff, false),
167 HOWTO(RELOC_HI22, 10, 2, 22, false, 0, false, true,0,"HI22", false, 0,0x003fffff, false),
168 HOWTO(RELOC_22, 0, 2, 22, false, 0, false, true,0,"22", false, 0,0x003fffff, false),
169 HOWTO(RELOC_13, 0, 2, 13, false, 0, false, true,0,"13", false, 0,0x00001fff, false),
170 HOWTO(RELOC_LO10, 0, 2, 10, false, 0, false, true,0,"LO10", false, 0,0x000003ff, false),
171 HOWTO(RELOC_SFA_BASE,0, 2, 32, false, 0, false, true,0,"SFA_BASE", false, 0,0xffffffff, false),
172 HOWTO(RELOC_SFA_OFF13,0,2, 32, false, 0, false, true,0,"SFA_OFF13",false, 0,0xffffffff, false),
173 HOWTO(RELOC_BASE10, 0, 2, 16, false, 0, false, true,0,"BASE10", false, 0,0x0000ffff, false),
174 HOWTO(RELOC_BASE13, 0, 2, 13, false, 0, false, true,0,"BASE13", false, 0,0x00001fff, false),
175 HOWTO(RELOC_BASE22, 0, 2, 0, false, 0, false, true,0,"BASE22", false, 0,0x00000000, false),
176 HOWTO(RELOC_PC10, 0, 2, 10, false, 0, false, true,0,"PC10", false, 0,0x000003ff, false),
177 HOWTO(RELOC_PC22, 0, 2, 22, false, 0, false, true,0,"PC22", false, 0,0x003fffff, false),
178 HOWTO(RELOC_JMP_TBL,0, 2, 32, false, 0, false, true,0,"JMP_TBL", false, 0,0xffffffff, false),
179 HOWTO(RELOC_SEGOFF16,0, 2, 0, false, 0, false, true,0,"SEGOFF16", false, 0,0x00000000, false),
180 HOWTO(RELOC_GLOB_DAT,0, 2, 0, false, 0, false, true,0,"GLOB_DAT", false, 0,0x00000000, false),
181 HOWTO(RELOC_JMP_SLOT,0, 2, 0, false, 0, false, true,0,"JMP_SLOT", false, 0,0x00000000, false),
67c060c3 182 HOWTO(RELOC_RELATIVE,0, 2, 0, false, 0, false, true,0,"RELATIVE", false, 0,0x00000000, false),
7ed4093a
SC
183};
184
185/* Convert standard reloc records to "arelent" format (incl byte swap). */
186
ce07dd7c 187reloc_howto_type howto_table_std[] = {
7ed4093a
SC
188 /* type rs size bsz pcrel bitpos abs ovrf sf name part_inpl readmask setmask pcdone */
189HOWTO( 0, 0, 0, 8, false, 0, true, true,0,"8", true, 0x000000ff,0x000000ff, false),
190HOWTO( 1, 0, 1, 16, false, 0, true, true,0,"16", true, 0x0000ffff,0x0000ffff, false),
191HOWTO( 2, 0, 2, 32, false, 0, true, true,0,"32", true, 0xffffffff,0xffffffff, false),
192HOWTO( 3, 0, 3, 64, false, 0, true, true,0,"64", true, 0xdeaddead,0xdeaddead, false),
193HOWTO( 4, 0, 0, 8, true, 0, false, true,0,"DISP8", true, 0x000000ff,0x000000ff, false),
194HOWTO( 5, 0, 1, 16, true, 0, false, true,0,"DISP16", true, 0x0000ffff,0x0000ffff, false),
195HOWTO( 6, 0, 2, 32, true, 0, false, true,0,"DISP32", true, 0xffffffff,0xffffffff, false),
196HOWTO( 7, 0, 3, 64, true, 0, false, true,0,"DISP64", true, 0xfeedface,0xfeedface, false),
197};
198
214f8f23
KR
199CONST struct reloc_howto_struct *
200DEFUN(NAME(aout,reloc_type_lookup),(abfd,code),
201 bfd *abfd AND
202 bfd_reloc_code_real_type code)
203{
204#define EXT(i,j) case i: return &howto_table_ext[j]
205#define STD(i,j) case i: return &howto_table_std[j]
206 int ext = obj_reloc_entry_size (abfd) == RELOC_EXT_SIZE;
207 if (code == BFD_RELOC_CTOR)
208 switch (bfd_get_arch_info (abfd)->bits_per_address)
209 {
210 case 32:
211 code = BFD_RELOC_32;
212 break;
213 }
214 if (ext)
215 switch (code)
216 {
217 EXT (BFD_RELOC_32, 2);
218 EXT (BFD_RELOC_HI22, 8);
219 EXT (BFD_RELOC_LO10, 11);
220 EXT (BFD_RELOC_32_PCREL_S2, 6);
a99c3d70 221 default: return (CONST struct reloc_howto_struct *) 0;
214f8f23
KR
222 }
223 else
224 /* std relocs */
225 switch (code)
226 {
227 STD (BFD_RELOC_16, 1);
228 STD (BFD_RELOC_32, 2);
229 STD (BFD_RELOC_8_PCREL, 4);
230 STD (BFD_RELOC_16_PCREL, 5);
231 STD (BFD_RELOC_32_PCREL, 6);
a99c3d70 232 default: return (CONST struct reloc_howto_struct *) 0;
214f8f23 233 }
214f8f23 234}
7ed4093a 235
ce07dd7c 236extern bfd_error_vector_type bfd_error_vector;
6f715d66 237
4e41b5aa
SC
238/*
239SUBSECTION
240 Internal Entry Points
241
242DESCRIPTION
243 @code{aoutx.h} exports several routines for accessing the
244 contents of an a.out file, which are gathered and exported in
245 turn by various format specific files (eg sunos.c).
246
6f715d66
SC
247*/
248
4e41b5aa
SC
249/*
250FUNCTION
251 aout_<size>_swap_exec_header_in
252
253DESCRIPTION
254 Swaps the information in an executable header taken from a raw
255 byte stream memory image, into the internal exec_header
256 structure.
257
fa2b89f1 258SYNOPSIS
4e41b5aa
SC
259 void aout_<size>_swap_exec_header_in,
260 (bfd *abfd,
261 struct external_exec *raw_bytes,
262 struct internal_exec *execp);
6f715d66
SC
263*/
264
34dd8ba3 265#ifndef NAME_swap_exec_header_in
7ed4093a
SC
266void
267DEFUN(NAME(aout,swap_exec_header_in),(abfd, raw_bytes, execp),
268 bfd *abfd AND
269 struct external_exec *raw_bytes AND
270 struct internal_exec *execp)
271{
272 struct external_exec *bytes = (struct external_exec *)raw_bytes;
273
55c0061e
FF
274 /* The internal_exec structure has some fields that are unused in this
275 configuration (IE for i960), so ensure that all such uninitialized
276 fields are zero'd out. There are places where two of these structs
277 are memcmp'd, and thus the contents do matter. */
278 memset (execp, 0, sizeof (struct internal_exec));
7ed4093a
SC
279 /* Now fill in fields in the execp, from the bytes in the raw data. */
280 execp->a_info = bfd_h_get_32 (abfd, bytes->e_info);
281 execp->a_text = GET_WORD (abfd, bytes->e_text);
282 execp->a_data = GET_WORD (abfd, bytes->e_data);
283 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
284 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
285 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
286 execp->a_trsize = GET_WORD (abfd, bytes->e_trsize);
287 execp->a_drsize = GET_WORD (abfd, bytes->e_drsize);
288}
34dd8ba3
JG
289#define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in)
290#endif
7ed4093a 291
4e41b5aa
SC
292/*
293FUNCTION
294 aout_<size>_swap_exec_header_out
295
296DESCRIPTION
297 Swaps the information in an internal exec header structure
298 into the supplied buffer ready for writing to disk.
299
fa2b89f1 300SYNOPSIS
4e41b5aa 301 void aout_<size>_swap_exec_header_out
6f715d66
SC
302 (bfd *abfd,
303 struct internal_exec *execp,
4e41b5aa 304 struct external_exec *raw_bytes);
6f715d66 305*/
7ed4093a
SC
306void
307DEFUN(NAME(aout,swap_exec_header_out),(abfd, execp, raw_bytes),
308 bfd *abfd AND
309 struct internal_exec *execp AND
310 struct external_exec *raw_bytes)
311{
312 struct external_exec *bytes = (struct external_exec *)raw_bytes;
313
314 /* Now fill in fields in the raw data, from the fields in the exec struct. */
315 bfd_h_put_32 (abfd, execp->a_info , bytes->e_info);
316 PUT_WORD (abfd, execp->a_text , bytes->e_text);
317 PUT_WORD (abfd, execp->a_data , bytes->e_data);
318 PUT_WORD (abfd, execp->a_bss , bytes->e_bss);
319 PUT_WORD (abfd, execp->a_syms , bytes->e_syms);
320 PUT_WORD (abfd, execp->a_entry , bytes->e_entry);
321 PUT_WORD (abfd, execp->a_trsize, bytes->e_trsize);
322 PUT_WORD (abfd, execp->a_drsize, bytes->e_drsize);
323}
324
7ed4093a 325
6f715d66 326
4e41b5aa
SC
327/*
328FUNCTION
329 aout_<size>_some_aout_object_p
6f715d66 330
4e41b5aa
SC
331DESCRIPTION
332 Some A.OUT variant thinks that the file whose format we're
333 checking is an a.out file. Do some more checking, and set up
334 for access if it really is. Call back to the calling
335 environments "finish up" function just before returning, to
336 handle any last-minute setup.
6f715d66 337
fa2b89f1 338SYNOPSIS
4e41b5aa 339 bfd_target *aout_<size>_some_aout_object_p
6f715d66 340 (bfd *abfd,
4e41b5aa 341 bfd_target *(*callback_to_real_object_p)());
6f715d66 342*/
7ed4093a
SC
343
344bfd_target *
7b02b4ed 345DEFUN(NAME(aout,some_aout_object_p),(abfd, execp, callback_to_real_object_p),
7ed4093a 346 bfd *abfd AND
7b02b4ed 347 struct internal_exec *execp AND
b86f998b 348 bfd_target *(*callback_to_real_object_p) PARAMS ((bfd *)))
7ed4093a 349{
214f8f23 350 struct aout_data_struct *rawptr, *oldrawptr;
e6e265ce 351 bfd_target *result;
7ed4093a 352
6db82ea7 353 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, sizeof (struct aout_data_struct ));
7ed4093a
SC
354 if (rawptr == NULL) {
355 bfd_error = no_memory;
356 return 0;
357 }
358
214f8f23 359 oldrawptr = abfd->tdata.aout_data;
6db82ea7
SC
360 abfd->tdata.aout_data = rawptr;
361 abfd->tdata.aout_data->a.hdr = &rawptr->e;
362 *(abfd->tdata.aout_data->a.hdr) = *execp; /* Copy in the internal_exec struct */
363 execp = abfd->tdata.aout_data->a.hdr;
7ed4093a
SC
364
365 /* Set the file flags */
366 abfd->flags = NO_FLAGS;
367 if (execp->a_drsize || execp->a_trsize)
368 abfd->flags |= HAS_RELOC;
e6e265ce 369 /* Setting of EXEC_P has been deferred to the bottom of this function */
7ed4093a
SC
370 if (execp->a_syms)
371 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
372
ce07dd7c
KR
373 if (N_MAGIC (*execp) == ZMAGIC)
374 {
375 abfd->flags |= D_PAGED|WP_TEXT;
376 adata(abfd).magic = z_magic;
377 }
378 else if (N_MAGIC (*execp) == NMAGIC)
379 {
380 abfd->flags |= WP_TEXT;
381 adata(abfd).magic = n_magic;
382 }
383 else
384 adata(abfd).magic = o_magic;
7ed4093a
SC
385
386 bfd_get_start_address (abfd) = execp->a_entry;
387
388 obj_aout_symbols (abfd) = (aout_symbol_type *)NULL;
389 bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
390
7ed4093a
SC
391 /* The default relocation entry size is that of traditional V7 Unix. */
392 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
393
7b02b4ed
JG
394 /* The default symbol entry size is that of traditional Unix. */
395 obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
396
7ed4093a
SC
397 /* create the sections. This is raunchy, but bfd_close wants to reclaim
398 them */
6db82ea7 399
214f8f23
KR
400 obj_textsec (abfd) = bfd_make_section_old_way (abfd, ".text");
401 obj_datasec (abfd) = bfd_make_section_old_way (abfd, ".data");
402 obj_bsssec (abfd) = bfd_make_section_old_way (abfd, ".bss");
403
404#if 0
405 (void)bfd_make_section (abfd, ".text");
406 (void)bfd_make_section (abfd, ".data");
407 (void)bfd_make_section (abfd, ".bss");
408#endif
7ed4093a 409
6db82ea7
SC
410 obj_datasec (abfd)->_raw_size = execp->a_data;
411 obj_bsssec (abfd)->_raw_size = execp->a_bss;
7ed4093a 412
7ed4093a 413 obj_textsec (abfd)->flags = (execp->a_trsize != 0 ?
d047d16a
JG
414 (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC) :
415 (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
7ed4093a 416 obj_datasec (abfd)->flags = (execp->a_drsize != 0 ?
d047d16a
JG
417 (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC) :
418 (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
7ed4093a
SC
419 obj_bsssec (abfd)->flags = SEC_ALLOC;
420
421#ifdef THIS_IS_ONLY_DOCUMENTATION
98d43107
JG
422 /* The common code can't fill in these things because they depend
423 on either the start address of the text segment, the rounding
424 up of virtual addersses between segments, or the starting file
425 position of the text segment -- all of which varies among different
426 versions of a.out. */
427
7ed4093a
SC
428 /* Call back to the format-dependent code to fill in the rest of the
429 fields and do any further cleanup. Things that should be filled
430 in by the callback: */
431
432 struct exec *execp = exec_hdr (abfd);
433
98d43107 434 obj_textsec (abfd)->size = N_TXTSIZE(*execp);
6db82ea7 435 obj_textsec (abfd)->raw_size = N_TXTSIZE(*execp);
98d43107
JG
436 /* data and bss are already filled in since they're so standard */
437
7ed4093a 438 /* The virtual memory addresses of the sections */
7ed4093a 439 obj_textsec (abfd)->vma = N_TXTADDR(*execp);
98d43107
JG
440 obj_datasec (abfd)->vma = N_DATADDR(*execp);
441 obj_bsssec (abfd)->vma = N_BSSADDR(*execp);
7ed4093a
SC
442
443 /* The file offsets of the sections */
444 obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
445 obj_datasec (abfd)->filepos = N_DATOFF(*execp);
446
447 /* The file offsets of the relocation info */
448 obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);
449 obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);
450
451 /* The file offsets of the string table and symbol table. */
452 obj_str_filepos (abfd) = N_STROFF (*execp);
453 obj_sym_filepos (abfd) = N_SYMOFF (*execp);
454
7ed4093a
SC
455 /* Determine the architecture and machine type of the object file. */
456 switch (N_MACHTYPE (*exec_hdr (abfd))) {
457 default:
458 abfd->obj_arch = bfd_arch_obscure;
459 break;
460 }
461
7b02b4ed
JG
462 adata(abfd)->page_size = PAGE_SIZE;
463 adata(abfd)->segment_size = SEGMENT_SIZE;
464 adata(abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
465
7ed4093a
SC
466 return abfd->xvec;
467
468 /* The architecture is encoded in various ways in various a.out variants,
469 or is not encoded at all in some of them. The relocation size depends
470 on the architecture and the a.out variant. Finally, the return value
471 is the bfd_target vector in use. If an error occurs, return zero and
472 set bfd_error to the appropriate error code.
473
474 Formats such as b.out, which have additional fields in the a.out
475 header, should cope with them in this callback as well. */
476#endif /* DOCUMENTATION */
477
e6e265ce
JG
478 result = (*callback_to_real_object_p)(abfd);
479
480 /* Now that the segment addresses have been worked out, take a better
481 guess at whether the file is executable. If the entry point
482 is within the text segment, assume it is. (This makes files
483 executable even if their entry point address is 0, as long as
484 their text starts at zero.)
485
486 At some point we should probably break down and stat the file and
487 declare it executable if (one of) its 'x' bits are on... */
488 if ((execp->a_entry >= obj_textsec(abfd)->vma) &&
6db82ea7 489 (execp->a_entry < obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size))
e6e265ce 490 abfd->flags |= EXEC_P;
214f8f23
KR
491 if (result)
492 {
1f29e30b 493#if 0 /* These should be set correctly anyways. */
214f8f23
KR
494 abfd->sections = obj_textsec (abfd);
495 obj_textsec (abfd)->next = obj_datasec (abfd);
496 obj_datasec (abfd)->next = obj_bsssec (abfd);
1f29e30b 497#endif
214f8f23
KR
498 }
499 else
500 {
501 free (rawptr);
502 abfd->tdata.aout_data = oldrawptr;
503 }
e6e265ce 504 return result;
7ed4093a
SC
505}
506
4e41b5aa
SC
507/*
508FUNCTION
509 aout_<size>_mkobject
6f715d66 510
4e41b5aa
SC
511DESCRIPTION
512 This routine initializes a BFD for use with a.out files.
6f715d66 513
fa2b89f1 514SYNOPSIS
4e41b5aa 515 boolean aout_<size>_mkobject, (bfd *);
6f715d66 516*/
7ed4093a
SC
517
518boolean
519DEFUN(NAME(aout,mkobject),(abfd),
520 bfd *abfd)
521{
6db82ea7 522 struct aout_data_struct *rawptr;
7ed4093a
SC
523
524 bfd_error = system_call_error;
525
526 /* Use an intermediate variable for clarity */
6db82ea7 527 rawptr = (struct aout_data_struct *)bfd_zalloc (abfd, sizeof (struct aout_data_struct ));
7ed4093a
SC
528
529 if (rawptr == NULL) {
530 bfd_error = no_memory;
531 return false;
532 }
533
6db82ea7 534 abfd->tdata.aout_data = rawptr;
7ed4093a
SC
535 exec_hdr (abfd) = &(rawptr->e);
536
537 /* For simplicity's sake we just make all the sections right here. */
538
539 obj_textsec (abfd) = (asection *)NULL;
540 obj_datasec (abfd) = (asection *)NULL;
541 obj_bsssec (abfd) = (asection *)NULL;
542 bfd_make_section (abfd, ".text");
543 bfd_make_section (abfd, ".data");
544 bfd_make_section (abfd, ".bss");
6db82ea7
SC
545 bfd_make_section (abfd, BFD_ABS_SECTION_NAME);
546 bfd_make_section (abfd, BFD_UND_SECTION_NAME);
547 bfd_make_section (abfd, BFD_COM_SECTION_NAME);
7ed4093a
SC
548
549 return true;
550}
551
6f715d66 552
4e41b5aa
SC
553/*
554FUNCTION
555 aout_<size>_machine_type
6f715d66 556
4e41b5aa
SC
557DESCRIPTION
558 Keep track of machine architecture and machine type for
559 a.out's. Return the machine_type for a particular
560 arch&machine, or M_UNKNOWN if that exact arch&machine can't be
561 represented in a.out format.
7ed4093a 562
4e41b5aa
SC
563 If the architecture is understood, machine type 0 (default)
564 should always be understood.
6f715d66 565
fa2b89f1 566SYNOPSIS
4e41b5aa 567 enum machine_type aout_<size>_machine_type
6f715d66
SC
568 (enum bfd_architecture arch,
569 unsigned long machine));
570*/
7ed4093a
SC
571
572enum machine_type
573DEFUN(NAME(aout,machine_type),(arch, machine),
574 enum bfd_architecture arch AND
575 unsigned long machine)
576{
577 enum machine_type arch_flags;
578
579 arch_flags = M_UNKNOWN;
580
581 switch (arch) {
582 case bfd_arch_sparc:
583 if (machine == 0) arch_flags = M_SPARC;
584 break;
585
586 case bfd_arch_m68k:
587 switch (machine) {
588 case 0: arch_flags = M_68010; break;
589 case 68000: arch_flags = M_UNKNOWN; break;
590 case 68010: arch_flags = M_68010; break;
591 case 68020: arch_flags = M_68020; break;
592 default: arch_flags = M_UNKNOWN; break;
593 }
594 break;
595
596 case bfd_arch_i386:
597 if (machine == 0) arch_flags = M_386;
598 break;
599
600 case bfd_arch_a29k:
601 if (machine == 0) arch_flags = M_29K;
602 break;
603
5cd3dcff
KR
604 case bfd_arch_mips:
605 switch (machine) {
606 case 0:
607 case 2000:
608 case 3000: arch_flags = M_MIPS1; break;
609 case 4000:
610 case 4400:
611 case 6000: arch_flags = M_MIPS2; break;
612 default: arch_flags = M_UNKNOWN; break;
613 }
614 break;
615
7ed4093a
SC
616 default:
617 arch_flags = M_UNKNOWN;
7ed4093a
SC
618 }
619 return arch_flags;
620}
621
9e2dad8e 622
4e41b5aa
SC
623/*
624FUNCTION
625 aout_<size>_set_arch_mach
6f715d66 626
4e41b5aa
SC
627DESCRIPTION
628 Sets the architecture and the machine of the BFD to those
629 values supplied. Verifies that the format can support the
630 architecture required.
6f715d66 631
fa2b89f1 632SYNOPSIS
4e41b5aa 633 boolean aout_<size>_set_arch_mach,
6f715d66
SC
634 (bfd *,
635 enum bfd_architecture,
636 unsigned long machine));
637*/
638
7ed4093a
SC
639boolean
640DEFUN(NAME(aout,set_arch_mach),(abfd, arch, machine),
641 bfd *abfd AND
642 enum bfd_architecture arch AND
643 unsigned long machine)
644{
9e2dad8e 645 bfd_default_set_arch_mach(abfd, arch, machine);
7ed4093a
SC
646 if (arch != bfd_arch_unknown &&
647 NAME(aout,machine_type) (arch, machine) == M_UNKNOWN)
648 return false; /* We can't represent this type */
ce07dd7c 649
214f8f23
KR
650 /* Determine the size of a relocation entry */
651 switch (arch) {
652 case bfd_arch_sparc:
653 case bfd_arch_a29k:
5cd3dcff 654 case bfd_arch_mips:
214f8f23
KR
655 obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
656 break;
657 default:
658 obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
659 break;
660 }
661
2768b3f7 662 return (*aout_backend_info(abfd)->set_sizes) (abfd);
7ed4093a 663}
7ed4093a 664
ce07dd7c
KR
665boolean
666DEFUN (NAME (aout,adjust_sizes_and_vmas), (abfd, text_size, text_end),
667 bfd *abfd AND bfd_size_type *text_size AND file_ptr *text_end)
668{
669 struct internal_exec *execp = exec_hdr (abfd);
670 if ((obj_textsec (abfd) == NULL) || (obj_datasec (abfd) == NULL))
671 {
672 bfd_error = invalid_operation;
673 return false;
674 }
675 if (adata(abfd).magic != undecided_magic) return true;
676 obj_textsec(abfd)->_raw_size =
677 align_power(obj_textsec(abfd)->_raw_size,
678 obj_textsec(abfd)->alignment_power);
679
680 *text_size = obj_textsec (abfd)->_raw_size;
681 /* Rule (heuristic) for when to pad to a new page. Note that there
682 * are (at least) two ways demand-paged (ZMAGIC) files have been
683 * handled. Most Berkeley-based systems start the text segment at
684 * (PAGE_SIZE). However, newer versions of SUNOS start the text
685 * segment right after the exec header; the latter is counted in the
686 * text segment size, and is paged in by the kernel with the rest of
687 * the text. */
688
689 /* This perhaps isn't the right way to do this, but made it simpler for me
690 to understand enough to implement it. Better would probably be to go
691 right from BFD flags to alignment/positioning characteristics. But the
692 old code was sloppy enough about handling the flags, and had enough
693 other magic, that it was a little hard for me to understand. I think
694 I understand it better now, but I haven't time to do the cleanup this
695 minute. */
696 if (adata(abfd).magic == undecided_magic)
697 {
698 if (abfd->flags & D_PAGED)
699 /* whether or not WP_TEXT is set */
700 adata(abfd).magic = z_magic;
701 else if (abfd->flags & WP_TEXT)
702 adata(abfd).magic = n_magic;
703 else
704 adata(abfd).magic = o_magic;
705 }
706
707#ifdef BFD_AOUT_DEBUG /* requires gcc2 */
708#if __GNUC__ >= 2
709 fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
710 ({ char *str;
711 switch (adata(abfd).magic) {
712 case n_magic: str = "NMAGIC"; break;
713 case o_magic: str = "OMAGIC"; break;
714 case z_magic: str = "ZMAGIC"; break;
715 default: abort ();
716 }
717 str;
718 }),
719 obj_textsec(abfd)->vma, obj_textsec(abfd)->_raw_size, obj_textsec(abfd)->alignment_power,
720 obj_datasec(abfd)->vma, obj_datasec(abfd)->_raw_size, obj_datasec(abfd)->alignment_power,
721 obj_bsssec(abfd)->vma, obj_bsssec(abfd)->_raw_size, obj_bsssec(abfd)->alignment_power);
722#endif
723#endif
724
725 switch (adata(abfd).magic)
726 {
727 case o_magic:
728 {
729 file_ptr pos = adata (abfd).exec_bytes_size;
730 bfd_vma vma = 0;
214f8f23 731 int pad = 0;
ce07dd7c
KR
732
733 obj_textsec(abfd)->filepos = pos;
734 pos += obj_textsec(abfd)->_raw_size;
735 vma += obj_textsec(abfd)->_raw_size;
736 if (!obj_datasec(abfd)->user_set_vma)
737 {
214f8f23 738#if 0 /* ?? Does alignment in the file image really matter? */
ce07dd7c 739 pad = align_power (vma, obj_datasec(abfd)->alignment_power) - vma;
214f8f23 740#endif
ce07dd7c
KR
741 obj_textsec(abfd)->_raw_size += pad;
742 pos += pad;
743 vma += pad;
744 obj_datasec(abfd)->vma = vma;
745 }
746 obj_datasec(abfd)->filepos = pos;
747 pos += obj_datasec(abfd)->_raw_size;
748 vma += obj_datasec(abfd)->_raw_size;
749 if (!obj_bsssec(abfd)->user_set_vma)
750 {
214f8f23 751#if 0
ce07dd7c 752 pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma;
214f8f23 753#endif
ce07dd7c
KR
754 obj_datasec(abfd)->_raw_size += pad;
755 pos += pad;
756 vma += pad;
757 obj_bsssec(abfd)->vma = vma;
758 }
759 obj_bsssec(abfd)->filepos = pos;
760 execp->a_text = obj_textsec(abfd)->_raw_size;
761 execp->a_data = obj_datasec(abfd)->_raw_size;
762 execp->a_bss = obj_bsssec(abfd)->_raw_size;
763 N_SET_MAGIC (*execp, OMAGIC);
764 }
765 break;
766 case z_magic:
767 {
768 bfd_size_type data_pad, text_pad;
769 file_ptr text_end;
770 CONST struct aout_backend_data *abdp;
771 int ztih;
772 bfd_vma data_vma;
773
774 abdp = aout_backend_info (abfd);
775 ztih = abdp && abdp->text_includes_header;
776 obj_textsec(abfd)->filepos = (ztih
777 ? adata(abfd).exec_bytes_size
778 : adata(abfd).page_size);
779 if (! obj_textsec(abfd)->user_set_vma)
780 /* ?? Do we really need to check for relocs here? */
781 obj_textsec(abfd)->vma = ((abfd->flags & HAS_RELOC)
782 ? 0
783 : (ztih
784 ? (abdp->default_text_vma
785 + adata(abfd).exec_bytes_size)
786 : abdp->default_text_vma));
787 /* Could take strange alignment of text section into account here? */
788
789 /* Find start of data. */
790 text_end = obj_textsec(abfd)->filepos + obj_textsec(abfd)->_raw_size;
791 text_pad = BFD_ALIGN (text_end, adata(abfd).page_size) - text_end;
792 obj_textsec(abfd)->_raw_size += text_pad;
793 text_end += text_pad;
794
795 if (!obj_datasec(abfd)->user_set_vma)
796 {
797 bfd_vma vma;
798 vma = obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size;
799 obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
800 }
801 data_vma = obj_datasec(abfd)->vma;
802 if (abdp && abdp->zmagic_mapped_contiguous)
803 {
804 text_pad = (obj_datasec(abfd)->vma
805 - obj_textsec(abfd)->vma
806 - obj_textsec(abfd)->_raw_size);
807 obj_textsec(abfd)->_raw_size += text_pad;
808 }
809 obj_datasec(abfd)->filepos = (obj_textsec(abfd)->filepos
810 + obj_textsec(abfd)->_raw_size);
811
812 /* Fix up exec header while we're at it. */
813 execp->a_text = obj_textsec(abfd)->_raw_size;
7f90aa8b 814 if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
ce07dd7c
KR
815 execp->a_text += adata(abfd).exec_bytes_size;
816 N_SET_MAGIC (*execp, ZMAGIC);
817 /* Spec says data section should be rounded up to page boundary. */
818 /* If extra space in page is left after data section, fudge data
819 in the header so that the bss section looks smaller by that
820 amount. We'll start the bss section there, and lie to the OS. */
821 obj_datasec(abfd)->_raw_size
822 = align_power (obj_datasec(abfd)->_raw_size,
823 obj_bsssec(abfd)->alignment_power);
824 execp->a_data = BFD_ALIGN (obj_datasec(abfd)->_raw_size,
825 adata(abfd).page_size);
826 data_pad = execp->a_data - obj_datasec(abfd)->_raw_size;
a99c3d70 827
ce07dd7c
KR
828 if (!obj_bsssec(abfd)->user_set_vma)
829 obj_bsssec(abfd)->vma = (obj_datasec(abfd)->vma
830 + obj_datasec(abfd)->_raw_size);
831 if (data_pad > obj_bsssec(abfd)->_raw_size)
832 execp->a_bss = 0;
833 else
834 execp->a_bss = obj_bsssec(abfd)->_raw_size - data_pad;
835 }
836 break;
837 case n_magic:
838 {
ce07dd7c
KR
839 file_ptr pos = adata(abfd).exec_bytes_size;
840 bfd_vma vma = 0;
841 int pad;
842
843 obj_textsec(abfd)->filepos = pos;
844 if (!obj_textsec(abfd)->user_set_vma)
845 obj_textsec(abfd)->vma = vma;
846 else
847 vma = obj_textsec(abfd)->vma;
848 pos += obj_textsec(abfd)->_raw_size;
849 vma += obj_textsec(abfd)->_raw_size;
850 obj_datasec(abfd)->filepos = pos;
851 if (!obj_datasec(abfd)->user_set_vma)
852 obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
853 vma = obj_datasec(abfd)->vma;
854
855 /* Since BSS follows data immediately, see if it needs alignment. */
856 vma += obj_datasec(abfd)->_raw_size;
857 pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma;
858 obj_datasec(abfd)->_raw_size += pad;
859 pos += obj_datasec(abfd)->_raw_size;
860
861 if (!obj_bsssec(abfd)->user_set_vma)
862 obj_bsssec(abfd)->vma = vma;
863 else
864 vma = obj_bsssec(abfd)->vma;
865 }
866 execp->a_text = obj_textsec(abfd)->_raw_size;
867 execp->a_data = obj_datasec(abfd)->_raw_size;
868 execp->a_bss = obj_bsssec(abfd)->_raw_size;
869 N_SET_MAGIC (*execp, NMAGIC);
870 break;
871 default:
872 abort ();
873 }
874#ifdef BFD_AOUT_DEBUG
875 fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
876 obj_textsec(abfd)->vma, obj_textsec(abfd)->_raw_size, obj_textsec(abfd)->filepos,
877 obj_datasec(abfd)->vma, obj_datasec(abfd)->_raw_size, obj_datasec(abfd)->filepos,
878 obj_bsssec(abfd)->vma, obj_bsssec(abfd)->_raw_size);
879#endif
d047d16a 880 return true;
ce07dd7c
KR
881}
882
4e41b5aa
SC
883/*
884FUNCTION
fa2b89f1 885 aout_<size>_new_section_hook
9e2dad8e 886
4e41b5aa
SC
887DESCRIPTION
888 Called by the BFD in response to a @code{bfd_make_section}
889 request.
890
fa2b89f1 891SYNOPSIS
4e41b5aa 892 boolean aout_<size>_new_section_hook,
9e2dad8e
JG
893 (bfd *abfd,
894 asection *newsect));
6f715d66 895*/
7ed4093a 896boolean
3f7607af 897DEFUN(NAME(aout,new_section_hook),(abfd, newsect),
9e2dad8e
JG
898 bfd *abfd AND
899 asection *newsect)
7ed4093a 900{
6db82ea7
SC
901 /* align to double at least */
902 newsect->alignment_power = bfd_get_arch_info(abfd)->section_align_power;
3f7607af 903
7ed4093a 904
6db82ea7
SC
905 if (bfd_get_format (abfd) == bfd_object)
906 {
907 if (obj_textsec(abfd) == NULL && !strcmp(newsect->name, ".text")) {
908 obj_textsec(abfd)= newsect;
909 newsect->target_index = N_TEXT | N_EXT;
910 return true;
911 }
7ed4093a 912
6db82ea7
SC
913 if (obj_datasec(abfd) == NULL && !strcmp(newsect->name, ".data")) {
914 obj_datasec(abfd) = newsect;
915 newsect->target_index = N_DATA | N_EXT;
916 return true;
917 }
7ed4093a 918
6db82ea7
SC
919 if (obj_bsssec(abfd) == NULL && !strcmp(newsect->name, ".bss")) {
920 obj_bsssec(abfd) = newsect;
921 newsect->target_index = N_BSS | N_EXT;
922 return true;
923 }
924
925 }
7ed4093a 926
6db82ea7
SC
927 /* We allow more than three sections internally */
928 return true;
7ed4093a
SC
929}
930
931boolean
9e2dad8e
JG
932 DEFUN(NAME(aout,set_section_contents),(abfd, section, location, offset, count),
933 bfd *abfd AND
934 sec_ptr section AND
935 PTR location AND
936 file_ptr offset AND
937 bfd_size_type count)
7ed4093a 938{
7b02b4ed 939 file_ptr text_end;
7b02b4ed 940 bfd_size_type text_size;
ce07dd7c 941
7ed4093a 942 if (abfd->output_has_begun == false)
9e2dad8e
JG
943 { /* set by bfd.c handler */
944 switch (abfd->direction)
945 {
946 case read_direction:
947 case no_direction:
948 bfd_error = invalid_operation;
949 return false;
ce07dd7c
KR
950
951 case write_direction:
952 if (NAME(aout,adjust_sizes_and_vmas) (abfd,
953 &text_size,
954 &text_end) == false)
955 return false;
9e2dad8e
JG
956 case both_direction:
957 break;
12e7087f 958 }
9e2dad8e 959 }
12e7087f 960
7ed4093a
SC
961 /* regardless, once we know what we're doing, we might as well get going */
962 if (section != obj_bsssec(abfd))
963 {
964 bfd_seek (abfd, section->filepos + offset, SEEK_SET);
9e2dad8e 965
7ed4093a
SC
966 if (count) {
967 return (bfd_write ((PTR)location, 1, count, abfd) == count) ?
968 true : false;
969 }
6db82ea7 970 return true;
7ed4093a
SC
971 }
972 return true;
973}
974\f
975/* Classify stabs symbols */
976
977#define sym_in_text_section(sym) \
9e2dad8e 978 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_TEXT)
7ed4093a
SC
979
980#define sym_in_data_section(sym) \
9e2dad8e 981 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_DATA)
7ed4093a
SC
982
983#define sym_in_bss_section(sym) \
9e2dad8e 984 (((sym)->type & (N_ABS | N_TEXT | N_DATA | N_BSS))== N_BSS)
7ed4093a
SC
985
986/* Symbol is undefined if type is N_UNDF|N_EXT and if it has
9e2dad8e
JG
987 zero in the "value" field. Nonzeroes there are fortrancommon
988 symbols. */
7ed4093a 989#define sym_is_undefined(sym) \
9e2dad8e 990 ((sym)->type == (N_UNDF | N_EXT) && (sym)->symbol.value == 0)
7ed4093a
SC
991
992/* Symbol is a global definition if N_EXT is on and if it has
9e2dad8e 993 a nonzero type field. */
7ed4093a 994#define sym_is_global_defn(sym) \
9e2dad8e 995 (((sym)->type & N_EXT) && (sym)->type & N_TYPE)
7ed4093a
SC
996
997/* Symbol is debugger info if any bits outside N_TYPE or N_EXT
9e2dad8e 998 are on. */
7ed4093a 999#define sym_is_debugger_info(sym) \
9e2dad8e 1000 ((sym)->type & ~(N_EXT | N_TYPE))
7ed4093a
SC
1001
1002#define sym_is_fortrancommon(sym) \
9e2dad8e 1003 (((sym)->type == (N_EXT)) && (sym)->symbol.value != 0)
7ed4093a
SC
1004
1005/* Symbol is absolute if it has N_ABS set */
1006#define sym_is_absolute(sym) \
9e2dad8e 1007 (((sym)->type & N_TYPE)== N_ABS)
7ed4093a
SC
1008
1009
1010#define sym_is_indirect(sym) \
9e2dad8e 1011 (((sym)->type & N_ABS)== N_ABS)
7ed4093a
SC
1012
1013/* Only in their own functions for ease of debugging; when sym flags have
9e2dad8e
JG
1014 stabilised these should be inlined into their (single) caller */
1015
7ed4093a 1016static void
a99c3d70
JG
1017DEFUN (translate_from_native_sym_flags, (sym_pointer, cache_ptr, abfd, statep),
1018 struct external_nlist *sym_pointer AND
1019 aout_symbol_type * cache_ptr AND
1020 bfd * abfd AND
1021 int *statep)
9e2dad8e 1022{
0f213cc2 1023 cache_ptr->symbol.section = 0;
a99c3d70 1024 if (*statep)
6db82ea7 1025 {
a99c3d70
JG
1026 /* This is an indirect symbol */
1027 cache_ptr->symbol.flags = BSF_DEBUGGING;
1028 cache_ptr->symbol.section = &bfd_und_section;
1029 *statep = 0;
6db82ea7 1030 }
a99c3d70 1031 else
6db82ea7 1032 {
a99c3d70 1033 switch (cache_ptr->type & N_TYPE)
6db82ea7 1034 {
a99c3d70
JG
1035 case N_SETA:
1036 case N_SETT:
1037 case N_SETD:
1038 case N_SETB:
1039 {
1040 char *copy = bfd_alloc (abfd, strlen (cache_ptr->symbol.name) + 1);
1041 asection *section;
1042 asection *into_section;
1043
1044 arelent_chain *reloc = (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
1045 strcpy (copy, cache_ptr->symbol.name);
1046
1047 /* Make sure that this bfd has a section with the right contructor
1048 name */
1049 section = bfd_get_section_by_name (abfd, copy);
1050 if (!section)
1051 section = bfd_make_section (abfd, copy);
1052
1053 /* Build a relocation entry for the constructor */
1054 switch ((cache_ptr->type & N_TYPE))
1055 {
1056 case N_SETA:
1057 into_section = &bfd_abs_section;
34dd8ba3 1058 cache_ptr->type = N_ABS;
a99c3d70
JG
1059 break;
1060 case N_SETT:
1061 into_section = (asection *) obj_textsec (abfd);
34dd8ba3 1062 cache_ptr->type = N_TEXT;
a99c3d70
JG
1063 break;
1064 case N_SETD:
1065 into_section = (asection *) obj_datasec (abfd);
34dd8ba3 1066 cache_ptr->type = N_DATA;
a99c3d70
JG
1067 break;
1068 case N_SETB:
1069 into_section = (asection *) obj_bsssec (abfd);
34dd8ba3 1070 cache_ptr->type = N_BSS;
a99c3d70
JG
1071 break;
1072 default:
1073 abort ();
1074 }
88dfcd68 1075
a99c3d70
JG
1076 /* Build a relocation pointing into the constuctor section
1077 pointing at the symbol in the set vector specified */
6db82ea7 1078
a99c3d70
JG
1079 reloc->relent.addend = cache_ptr->symbol.value;
1080 cache_ptr->symbol.section = into_section->symbol->section;
1081 reloc->relent.sym_ptr_ptr = into_section->symbol_ptr_ptr;
6db82ea7
SC
1082
1083
a99c3d70
JG
1084 /* We modify the symbol to belong to a section depending upon the
1085 name of the symbol - probably __CTOR__ or __DTOR__ but we don't
1086 really care, and add to the size of the section to contain a
1087 pointer to the symbol. Build a reloc entry to relocate to this
1088 symbol attached to this section. */
1089
1090 section->flags = SEC_CONSTRUCTOR;
1091
1092
1093 section->reloc_count++;
1094 section->alignment_power = 2;
1095
1096 reloc->next = section->constructor_chain;
1097 section->constructor_chain = reloc;
1098 reloc->relent.address = section->_raw_size;
1099 section->_raw_size += sizeof (int *);
1100
34dd8ba3
JG
1101 reloc->relent.howto
1102 = (obj_reloc_entry_size(abfd) == RELOC_EXT_SIZE
1103 ? howto_table_ext : howto_table_std)
1104 + CTOR_TABLE_RELOC_IDX;
1105 cache_ptr->symbol.flags |= BSF_CONSTRUCTOR;
6db82ea7 1106 }
a99c3d70
JG
1107 break;
1108 default:
1109 if (cache_ptr->type == N_WARNING)
1110 {
1111 /* This symbol is the text of a warning message, the next symbol
1112 is the symbol to associate the warning with */
1113 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_WARNING;
1114 cache_ptr->symbol.value = (bfd_vma) ((cache_ptr + 1));
1115 /* We furgle with the next symbol in place.
1116 We don't want it to be undefined, we'll trample the type */
1117 (sym_pointer + 1)->e_type[0] = 0xff;
1118 break;
1119 }
1120 if ((cache_ptr->type | N_EXT) == (N_INDR | N_EXT))
1121 {
1122 /* Two symbols in a row for an INDR message. The first symbol
0f213cc2
KR
1123 contains the name we will match, the second symbol contains
1124 the name the first name is translated into. It is supplied to
1125 us undefined. This is good, since we want to pull in any files
1126 which define it */
a99c3d70
JG
1127 cache_ptr->symbol.flags = BSF_DEBUGGING | BSF_INDIRECT;
1128 cache_ptr->symbol.value = (bfd_vma) ((cache_ptr + 1));
1129 cache_ptr->symbol.section = &bfd_ind_section;
1130 *statep = 1;
1131 }
1132
1133 else if (sym_is_debugger_info (cache_ptr))
1134 {
1135 cache_ptr->symbol.flags = BSF_DEBUGGING;
1136 /* Work out the section correct for this symbol */
1137 switch (cache_ptr->type & N_TYPE)
1138 {
1139 case N_TEXT:
1140 case N_FN:
1141 cache_ptr->symbol.section = obj_textsec (abfd);
1142 cache_ptr->symbol.value -= obj_textsec (abfd)->vma;
1143 break;
1144 case N_DATA:
1145 cache_ptr->symbol.value -= obj_datasec (abfd)->vma;
1146 cache_ptr->symbol.section = obj_datasec (abfd);
1147 break;
1148 case N_BSS:
1149 cache_ptr->symbol.section = obj_bsssec (abfd);
1150 cache_ptr->symbol.value -= obj_bsssec (abfd)->vma;
1151 break;
1152 default:
1153 case N_ABS:
1154
1155 cache_ptr->symbol.section = &bfd_abs_section;
1156 break;
1157 }
1158 }
1159 else
1160 {
1161
1162 if (sym_is_fortrancommon (cache_ptr))
1163 {
1164 cache_ptr->symbol.flags = 0;
1165 cache_ptr->symbol.section = &bfd_com_section;
1166 }
1167 else
1168 {
1169
1170
1171 }
1172
1173 /* In a.out, the value of a symbol is always relative to the
1174 * start of the file, if this is a data symbol we'll subtract
1175 * the size of the text section to get the section relative
1176 * value. If this is a bss symbol (which would be strange)
1177 * we'll subtract the size of the previous two sections
1178 * to find the section relative address.
1179 */
1180
1181 if (sym_in_text_section (cache_ptr))
1182 {
1183 cache_ptr->symbol.value -= obj_textsec (abfd)->vma;
1184 cache_ptr->symbol.section = obj_textsec (abfd);
1185 }
1186 else if (sym_in_data_section (cache_ptr))
1187 {
1188 cache_ptr->symbol.value -= obj_datasec (abfd)->vma;
1189 cache_ptr->symbol.section = obj_datasec (abfd);
1190 }
1191 else if (sym_in_bss_section (cache_ptr))
1192 {
1193 cache_ptr->symbol.section = obj_bsssec (abfd);
1194 cache_ptr->symbol.value -= obj_bsssec (abfd)->vma;
1195 }
1196 else if (sym_is_undefined (cache_ptr))
1197 {
1198 cache_ptr->symbol.flags = 0;
1199 cache_ptr->symbol.section = &bfd_und_section;
1200 }
1201 else if (sym_is_absolute (cache_ptr))
1202 {
1203 cache_ptr->symbol.section = &bfd_abs_section;
1204 }
1205
1206 if (sym_is_global_defn (cache_ptr))
1207 {
1208 cache_ptr->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
1209 }
1210 else
1211 {
1212 cache_ptr->symbol.flags = BSF_LOCAL;
1213 }
1214 }
7ed4093a 1215 }
a99c3d70 1216 }
0f213cc2
KR
1217 if (cache_ptr->symbol.section == 0)
1218 abort ();
7ed4093a
SC
1219}
1220
6db82ea7
SC
1221
1222
7ed4093a
SC
1223static void
1224DEFUN(translate_to_native_sym_flags,(sym_pointer, cache_ptr, abfd),
1225 struct external_nlist *sym_pointer AND
1226 asymbol *cache_ptr AND
1227 bfd *abfd)
1228{
1229 bfd_vma value = cache_ptr->value;
1230
10dea9ed
DHW
1231 /* mask out any existing type bits in case copying from one section
1232 to another */
1233 sym_pointer->e_type[0] &= ~N_TYPE;
a99c3d70 1234
10dea9ed 1235
6db82ea7 1236 if (bfd_get_output_section(cache_ptr) == obj_bsssec (abfd)) {
a99c3d70
JG
1237 sym_pointer->e_type[0] |= N_BSS;
1238 }
6db82ea7 1239 else if (bfd_get_output_section(cache_ptr) == obj_datasec (abfd)) {
a99c3d70
JG
1240 sym_pointer->e_type[0] |= N_DATA;
1241 }
6db82ea7 1242 else if (bfd_get_output_section(cache_ptr) == obj_textsec (abfd)) {
a99c3d70 1243 sym_pointer->e_type[0] |= N_TEXT;
7ed4093a 1244 }
a99c3d70
JG
1245 else if (bfd_get_output_section(cache_ptr) == &bfd_abs_section)
1246 {
1247 sym_pointer->e_type[0] |= N_ABS;
1248 }
6db82ea7 1249 else if (bfd_get_output_section(cache_ptr) == &bfd_und_section)
a99c3d70 1250 {
6db82ea7 1251 sym_pointer->e_type[0] = (N_UNDF | N_EXT);
a99c3d70
JG
1252 }
1253 else if (bfd_get_output_section(cache_ptr) == &bfd_ind_section)
1254 {
1255 sym_pointer->e_type[0] = N_INDR;
1256 }
1257 else if (bfd_is_com_section (bfd_get_output_section (cache_ptr))) {
1258 sym_pointer->e_type[0] = (N_UNDF | N_EXT);
1259 }
6db82ea7 1260 else {
a99c3d70 1261 if (cache_ptr->section->output_section)
e7b4046c
SC
1262 {
1263
1264 bfd_error_vector.nonrepresentable_section(abfd,
1265 bfd_get_output_section(cache_ptr)->name);
1266 }
a99c3d70 1267 else
e7b4046c
SC
1268 {
1269 bfd_error_vector.nonrepresentable_section(abfd,
1270 cache_ptr->section->name);
1271
1272 }
1273
a99c3d70 1274 }
6db82ea7 1275 /* Turn the symbol from section relative to absolute again */
7ed4093a 1276
6db82ea7
SC
1277 value += cache_ptr->section->output_section->vma + cache_ptr->section->output_offset ;
1278
1279
1280 if (cache_ptr->flags & (BSF_WARNING)) {
a99c3d70
JG
1281 (sym_pointer+1)->e_type[0] = 1;
1282 }
6db82ea7
SC
1283
1284 if (cache_ptr->flags & (BSF_GLOBAL | BSF_EXPORT)) {
a99c3d70
JG
1285 sym_pointer->e_type[0] |= N_EXT;
1286 }
6db82ea7 1287 if (cache_ptr->flags & BSF_DEBUGGING) {
34dd8ba3
JG
1288 sym_pointer->e_type[0] = ((aout_symbol_type *)cache_ptr)->type;
1289 }
1290 if (cache_ptr->flags & BSF_CONSTRUCTOR) {
1291 int type = ((aout_symbol_type *)cache_ptr)->type;
1292 switch (type)
1293 {
1294 case N_ABS: type = N_SETA; break;
1295 case N_TEXT: type = N_SETT; break;
1296 case N_DATA: type = N_SETD; break;
1297 case N_BSS: type = N_SETB; break;
1298 }
1299 sym_pointer->e_type[0] = type;
a99c3d70 1300 }
6db82ea7 1301
7ed4093a
SC
1302 PUT_WORD(abfd, value, sym_pointer->e_value);
1303}
1304\f
1305/* Native-level interface to symbols. */
1306
1307/* We read the symbols into a buffer, which is discarded when this
1308function exits. We read the strings into a buffer large enough to
1309hold them all plus all the cached symbol entries. */
1310
1311asymbol *
1312DEFUN(NAME(aout,make_empty_symbol),(abfd),
1313 bfd *abfd)
9e2dad8e
JG
1314{
1315 aout_symbol_type *new =
1316 (aout_symbol_type *)bfd_zalloc (abfd, sizeof (aout_symbol_type));
1317 new->symbol.the_bfd = abfd;
fa2b89f1 1318
9e2dad8e
JG
1319 return &new->symbol;
1320}
7ed4093a
SC
1321
1322boolean
1323DEFUN(NAME(aout,slurp_symbol_table),(abfd),
1324 bfd *abfd)
9e2dad8e
JG
1325{
1326 bfd_size_type symbol_size;
1327 bfd_size_type string_size;
1328 unsigned char string_chars[BYTES_IN_WORD];
1329 struct external_nlist *syms;
1330 char *strings;
1331 aout_symbol_type *cached;
0f213cc2 1332
9e2dad8e
JG
1333 /* If there's no work to be done, don't do any */
1334 if (obj_aout_symbols (abfd) != (aout_symbol_type *)NULL) return true;
1335 symbol_size = exec_hdr(abfd)->a_syms;
0f213cc2
KR
1336 if (symbol_size == 0)
1337 {
1338 bfd_error = no_symbols;
1339 return false;
1340 }
1341
9e2dad8e
JG
1342 bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET);
1343 if (bfd_read ((PTR)string_chars, BYTES_IN_WORD, 1, abfd) != BYTES_IN_WORD)
1344 return false;
1345 string_size = GET_WORD (abfd, string_chars);
0f213cc2 1346
9e2dad8e
JG
1347 strings =(char *) bfd_alloc(abfd, string_size + 1);
1348 cached = (aout_symbol_type *)
1349 bfd_zalloc(abfd, (bfd_size_type)(bfd_get_symcount (abfd) * sizeof(aout_symbol_type)));
1350
1351 /* malloc this, so we can free it if simply. The symbol caching
1352 might want to allocate onto the bfd's obstack */
98d43107 1353 syms = (struct external_nlist *) bfd_xmalloc(symbol_size);
9e2dad8e 1354 bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET);
0f213cc2
KR
1355 if (bfd_read ((PTR)syms, 1, symbol_size, abfd) != symbol_size)
1356 {
1357 bailout:
1358 if (syms)
1359 free (syms);
1360 if (cached)
1361 bfd_release (abfd, cached);
1362 if (strings)
1363 bfd_release (abfd, strings);
1364 return false;
1365 }
1366
9e2dad8e 1367 bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET);
0f213cc2 1368 if (bfd_read ((PTR)strings, 1, string_size, abfd) != string_size)
9e2dad8e 1369 {
0f213cc2 1370 goto bailout;
9e2dad8e 1371 }
0f213cc2
KR
1372 strings[string_size] = 0; /* Just in case. */
1373
1374 /* OK, now walk the new symtable, cacheing symbol properties */
1375 {
1376 register struct external_nlist *sym_pointer;
1377 int state = 0;
1378 register struct external_nlist *sym_end = syms + bfd_get_symcount (abfd);
1379 register aout_symbol_type *cache_ptr = cached;
1380
1381 /* Run through table and copy values */
1382 for (sym_pointer = syms, cache_ptr = cached;
1383 sym_pointer < sym_end; sym_pointer ++, cache_ptr++)
1384 {
1385 long x = GET_WORD(abfd, sym_pointer->e_strx);
1386 cache_ptr->symbol.the_bfd = abfd;
1387 if (x == 0)
1388 cache_ptr->symbol.name = "";
1389 else if (x >= 0 && x < string_size)
1390 cache_ptr->symbol.name = x + strings;
1391 else
1392 goto bailout;
1393
1394 cache_ptr->symbol.value = GET_SWORD(abfd, sym_pointer->e_value);
1395 cache_ptr->desc = bfd_h_get_16(abfd, sym_pointer->e_desc);
1396 cache_ptr->other = bfd_h_get_8(abfd, sym_pointer->e_other);
1397 cache_ptr->type = bfd_h_get_8(abfd, sym_pointer->e_type);
1398 cache_ptr->symbol.udata = 0;
1399 translate_from_native_sym_flags (sym_pointer, cache_ptr,
1400 abfd, &state);
1401 }
1402 }
1403
9e2dad8e
JG
1404 obj_aout_symbols (abfd) = cached;
1405 free((PTR)syms);
0f213cc2 1406
9e2dad8e
JG
1407 return true;
1408}
7ed4093a 1409
0f213cc2
KR
1410\f
1411/* Possible improvements:
1412 + look for strings matching trailing substrings of other strings
1413 + better data structures? balanced trees?
1414 + smaller per-string or per-symbol data? re-use some of the symbol's
1415 data fields?
1416 + also look at reducing memory use elsewhere -- maybe if we didn't have to
1417 construct the entire symbol table at once, we could get by with smaller
1418 amounts of VM? (What effect does that have on the string table
1419 reductions?)
1420 + rip this out of here, put it into its own file in bfd or libiberty, so
1421 coff and elf can use it too. I'll work on this soon, but have more
1422 pressing tasks right now.
1423
1424 A hash table might(?) be more efficient for handling exactly the cases that
1425 are handled now, but for trailing substring matches, I think we want to
1426 examine the `nearest' values (reverse-)lexically, not merely impose a strict
1427 order, nor look only for exact-match or not-match. I don't think a hash
1428 table would be very useful for that, and I don't feel like fleshing out two
1429 completely different implementations. [raeburn:930419.0331EDT] */
1430
1431#if __GNUC__ >= 2
1432#define INLINE __inline__
1433#else
1434#define INLINE
1435#endif
1436
1437struct stringtab_entry {
1438 /* Hash value for this string. Only useful so long as we aren't doing
1439 substring matches. */
1440 int hash;
1441
1442 /* Next node to look at, depending on whether the hash value of the string
1443 being searched for is less than or greater than the hash value of the
1444 current node. For now, `equal to' is lumped in with `greater than', for
1445 space efficiency. It's not a common enough case to warrant another field
1446 to be used for all nodes. */
1447 struct stringtab_entry *less;
1448 struct stringtab_entry *greater;
1449
1450 /* The string itself. */
1451 CONST char *string;
1452
1453 /* The index allocated for this string. */
1454 bfd_size_type index;
1455
1456#ifdef GATHER_STATISTICS
1457 /* How many references have there been to this string? (Not currently used;
1458 could be dumped out for anaylsis, if anyone's interested.) */
1459 unsigned long count;
1460#endif
1461
1462 /* Next node in linked list, in suggested output order. */
1463 struct stringtab_entry *next_to_output;
1464};
1465
1466struct stringtab_data {
1467 /* Tree of string table entries. */
1468 struct stringtab_entry *strings;
1469
1470 /* Fudge factor used to center top node of tree. */
1471 int hash_zero;
1472
1473 /* Next index value to issue. */
1474 bfd_size_type index;
1475
1476 /* Index used for empty strings. Cached here because checking for them
1477 is really easy, and we can avoid searching the tree. */
1478 bfd_size_type empty_string_index;
1479
1480 /* These fields indicate the two ends of a singly-linked list that indicates
1481 the order strings should be written out in. Use this order, and no
1482 seeking will need to be done, so output efficiency should be maximized. */
1483 struct stringtab_entry **end;
1484 struct stringtab_entry *output_order;
1485
1486#ifdef GATHER_STATISTICS
1487 /* Number of strings which duplicate strings already in the table. */
1488 unsigned long duplicates;
1489
1490 /* Number of bytes saved by not having to write all the duplicate strings. */
1491 unsigned long bytes_saved;
1492
1493 /* Number of zero-length strings. Currently, these all turn into
1494 references to the null byte at the end of the first string. In some
1495 cases (possibly not all? explore this...), it should be possible to
1496 simply write out a zero index value. */
1497 unsigned long empty_strings;
1498
1499 /* Number of times the hash values matched but the strings were different.
1500 Note that this includes the number of times the other string(s) occurs, so
1501 there may only be two strings hashing to the same value, even if this
1502 number is very large. */
1503 unsigned long bad_hash_matches;
1504
1505 /* Null strings aren't counted in this one.
1506 This will probably only be nonzero if we've got an input file
1507 which was produced by `ld -r' (i.e., it's already been processed
1508 through this code). Under some operating systems, native tools
1509 may make all empty strings have the same index; but the pointer
1510 check won't catch those, because to get to that stage we'd already
1511 have to compute the checksum, which requires reading the string,
1512 so we short-circuit that case with empty_string_index above. */
1513 unsigned long pointer_matches;
1514
1515 /* Number of comparisons done. I figure with the algorithms in use below,
1516 the average number of comparisons done (per symbol) should be roughly
1517 log-base-2 of the number of unique strings. */
1518 unsigned long n_compares;
1519#endif
1520};
1521
1522/* Some utility functions for the string table code. */
1523
1524static INLINE int
1525hash (string)
1526 char *string;
1527{
1528 unsigned int sum = 0;
1529 while (*string)
1530 {
1531#if 0
1532 /* This expression borrowed from some code in gnu make. */
1533 sum += *string++, sum = (sum << 7) + (sum >> 20);
1534#endif
1535 /* This appears to get a better distribution, at least for my one
1536 test case. Do some analysis on this later, get a real hash
1537 algorithm. */
1538 sum ^= sum >> 20;
1539 sum ^= sum << 7;
1540 sum += *string++;
1541 }
1542 return sum;
1543}
1544
1545static INLINE void
1546stringtab_init (tab)
1547 struct stringtab_data *tab;
1548{
1549 tab->strings = 0;
1550 tab->output_order = 0;
1551 tab->end = &tab->output_order;
1552
1553 /* Initial string table length includes size of length field. */
1554 tab->index = BYTES_IN_WORD;
1555 tab->empty_string_index = -1;
1556#ifdef GATHER_STATISTICS
1557 tab->duplicates = 0;
1558 tab->empty_strings = 0;
1559 tab->bad_hash_matches = 0;
1560 tab->pointer_matches = 0;
1561 tab->bytes_saved = 0;
1562 tab->n_compares = 0;
1563#endif
1564}
1565
1566static INLINE int
1567compare (entry, str, hash)
1568 struct stringtab_entry *entry;
1569 CONST char *str;
1570 int hash;
1571{
1572 if (hash == entry->hash)
1573 return 0;
1574 if (hash > entry->hash)
1575 return 1;
1576 if (hash < entry->hash)
1577 return -1;
1578 abort ();
1579}
1580
1581#ifdef GATHER_STATISTICS
1582/* Don't want to have to link in math library with all bfd applications... */
1583static INLINE double
1584log2 (num)
1585 int num;
1586{
1587 double d = num;
1588#if defined (__i386__) && __GNUC__ >= 2
1589 asm ("fyl2x" : "=t" (d) : "0" (d), "u" (1.0));
1590 return d;
1591#else
1592 int n = 0;
1593 while (d >= 2.0)
1594 n++, d /= 2.0;
1595 return ((d > 1.41) ? 0.5 : 0) + n;
1596#endif
1597}
1598#endif
1599
1600/* Main string table routines. */
1601/* Returns index in string table. Whether or not this actually adds an
1602 entry into the string table should be irrelevant -- it just has to
1603 return a valid index. */
1604static bfd_size_type
1605add_to_stringtab (abfd, str, tab, check)
1606 bfd *abfd;
1607 CONST char *str;
1608 struct stringtab_data *tab;
1609 int check;
1610{
1611 struct stringtab_entry **ep;
1612 struct stringtab_entry *entry;
1613 int hashval, len;
1614
1615 if (str[0] == 0)
1616 {
1617 bfd_size_type index;
1618 CONST bfd_size_type minus_one = -1;
1619
1620#ifdef GATHER_STATISTICS
1621 tab->empty_strings++;
1622#endif
1623 index = tab->empty_string_index;
1624 if (index != minus_one)
1625 {
1626 got_empty:
1627#ifdef GATHER_STATISTICS
1628 tab->bytes_saved++;
1629 tab->duplicates++;
1630#endif
1631 return index;
1632 }
1633
1634 /* Need to find it. */
1635 entry = tab->strings;
1636 if (entry)
1637 {
1638 index = entry->index + strlen (entry->string);
1639 tab->empty_string_index = index;
1640 goto got_empty;
1641 }
1642 len = 0;
1643 }
1644 else
1645 len = strlen (str);
1646
1647 /* The hash_zero value is chosen such that the first symbol gets a value of
1648 zero. With a balanced tree, this wouldn't be very useful, but without it,
1649 we might get a more even split at the top level, instead of skewing it
1650 badly should hash("/usr/lib/crt0.o") (or whatever) be far from zero. */
1651 hashval = hash (str) ^ tab->hash_zero;
1652 ep = &tab->strings;
1653 if (!*ep)
1654 {
1655 tab->hash_zero = hashval;
1656 hashval = 0;
1657 goto add_it;
1658 }
1659
1660 while (*ep)
1661 {
1662 int cmp;
1663 entry = *ep;
1664#ifdef GATHER_STATISTICS
1665 tab->n_compares++;
1666#endif
1667 cmp = compare (entry, str, hashval);
1668 if (cmp == 0)
1669 {
1670 if (entry->string == str)
1671 {
1672#ifdef GATHER_STATISTICS
1673 tab->pointer_matches++;
1674#endif
1675 goto match;
1676 }
1677 if (!strcmp (entry->string, str))
1678 {
1679 match:
1680#ifdef GATHER_STATISTICS
1681 entry->count++;
1682 tab->bytes_saved += len + 1;
1683 tab->duplicates++;
1684#endif
1685 /* If we're in the linker, and the new string is from a new
1686 input file which might have already had these reductions
1687 run over it, we want to keep the new string pointer. I
1688 don't think we're likely to see any (or nearly as many,
1689 at least) cases where a later string is in the same location
1690 as an earlier one rather than this one. */
1691 entry->string = str;
1692 return entry->index;
1693 }
1694#ifdef GATHER_STATISTICS
1695 tab->bad_hash_matches++;
1696#endif
1697 ep = &entry->greater;
1698 }
1699 else if (cmp > 0)
1700 ep = &entry->greater;
1701 else
1702 /* cmp < 0 */
1703 ep = &entry->less;
1704 }
1705
1706 /* If we get here, nothing that's in the table already matched.
1707 EP points to the `next' field at the end of the chain; stick a
1708 new entry on here. */
1709 add_it:
1710 entry = (struct stringtab_entry *) bfd_alloc_by_size_t (abfd,
1711 sizeof (struct stringtab_entry));
1712
1713 entry->less = entry->greater = 0;
1714 entry->hash = hashval;
1715 entry->index = tab->index;
1716 entry->string = str;
1717 entry->next_to_output = 0;
1718#ifdef GATHER_STATISTICS
1719 entry->count = 1;
1720#endif
1721
1722 assert (*tab->end == 0);
1723 *(tab->end) = entry;
1724 tab->end = &entry->next_to_output;
1725 assert (*tab->end == 0);
1726
1727 {
1728 tab->index += len + 1;
1729 if (len == 0)
1730 tab->empty_string_index = entry->index;
1731 }
1732 assert (*ep == 0);
1733 *ep = entry;
1734 return entry->index;
1735}
1736
1737static void
1738emit_strtab (abfd, tab)
1739 bfd *abfd;
1740 struct stringtab_data *tab;
1741{
1742 struct stringtab_entry *entry;
1743#ifdef GATHER_STATISTICS
1744 int count = 0;
1745#endif
1746
1747 /* Be sure to put string length into correct byte ordering before writing
1748 it out. */
1749 char buffer[BYTES_IN_WORD];
1750
1751 PUT_WORD (abfd, tab->index, (unsigned char *) buffer);
1752 bfd_write ((PTR) buffer, 1, BYTES_IN_WORD, abfd);
1753
1754 for (entry = tab->output_order; entry; entry = entry->next_to_output)
1755 {
1756 bfd_write ((PTR) entry->string, 1, strlen (entry->string) + 1, abfd);
1757#ifdef GATHER_STATISTICS
1758 count++;
1759#endif
1760 }
1761
1762#ifdef GATHER_STATISTICS
1763 /* Short form only, for now.
1764 To do: Specify output file. Conditionalize on environment? Detailed
1765 analysis if desired. */
1766 {
1767 int n_syms = bfd_get_symcount (abfd);
1768
1769 fprintf (stderr, "String table data for output file:\n");
1770 fprintf (stderr, " %8d symbols output\n", n_syms);
1771 fprintf (stderr, " %8d duplicate strings\n", tab->duplicates);
1772 fprintf (stderr, " %8d empty strings\n", tab->empty_strings);
1773 fprintf (stderr, " %8d unique strings output\n", count);
1774 fprintf (stderr, " %8d pointer matches\n", tab->pointer_matches);
1775 fprintf (stderr, " %8d bytes saved\n", tab->bytes_saved);
1776 fprintf (stderr, " %8d bad hash matches\n", tab->bad_hash_matches);
1777 fprintf (stderr, " %8d hash-val comparisons\n", tab->n_compares);
1778 if (n_syms)
1779 {
1780 double n_compares = tab->n_compares;
1781 double avg_compares = n_compares / n_syms;
1782 /* The second value here should usually be near one. */
1783 fprintf (stderr, "\t average %f per symbol (%f * log2 nstrings)\n",
1784 avg_compares, avg_compares / log2 (count));
1785 }
1786 }
1787#endif
1788
1789/* Old code:
1790 unsigned int count;
1791 generic = bfd_get_outsymbols(abfd);
1792 for (count = 0; count < bfd_get_symcount(abfd); count++)
1793 {
1794 asymbol *g = *(generic++);
1795
1796 if (g->name)
1797 {
1798 size_t length = strlen(g->name)+1;
1799 bfd_write((PTR)g->name, 1, length, abfd);
1800 }
1801 g->KEEPIT = (KEEPITTYPE) count;
1802 } */
1803}
7ed4093a
SC
1804
1805void
1806DEFUN(NAME(aout,write_syms),(abfd),
1807 bfd *abfd)
0f213cc2
KR
1808{
1809 unsigned int count ;
1810 asymbol **generic = bfd_get_outsymbols (abfd);
1811 struct stringtab_data strtab;
1812
1813 stringtab_init (&strtab);
1814
1815 for (count = 0; count < bfd_get_symcount (abfd); count++)
1816 {
7ed4093a
SC
1817 asymbol *g = generic[count];
1818 struct external_nlist nsp;
6db82ea7 1819
0f213cc2
KR
1820 if (g->name)
1821 PUT_WORD (abfd, add_to_stringtab (abfd, g->name, &strtab),
1822 (unsigned char *) nsp.e_strx);
1823 else
1824 PUT_WORD (abfd, 0, (unsigned char *)nsp.e_strx);
6db82ea7 1825
0f213cc2
KR
1826 if (bfd_asymbol_flavour(g) == abfd->xvec->flavour)
1827 {
1828 bfd_h_put_16(abfd, aout_symbol(g)->desc, nsp.e_desc);
1829 bfd_h_put_8(abfd, aout_symbol(g)->other, nsp.e_other);
1830 bfd_h_put_8(abfd, aout_symbol(g)->type, nsp.e_type);
1831 }
7ed4093a 1832 else
0f213cc2
KR
1833 {
1834 bfd_h_put_16(abfd,0, nsp.e_desc);
1835 bfd_h_put_8(abfd, 0, nsp.e_other);
1836 bfd_h_put_8(abfd, 0, nsp.e_type);
1837 }
7b02b4ed 1838
7d003262 1839 translate_to_native_sym_flags (&nsp, g, abfd);
7b02b4ed
JG
1840
1841 bfd_write((PTR)&nsp,1,EXTERNAL_NLIST_SIZE, abfd);
7ed4093a 1842
0f213cc2
KR
1843 /* NB: `KEEPIT' currently overlays `flags', so set this only
1844 here, at the end. */
1845 g->KEEPIT = count;
1846 }
7ed4093a 1847
0f213cc2
KR
1848 emit_strtab (abfd, &strtab);
1849}
7ed4093a 1850
0f213cc2 1851\f
7ed4093a
SC
1852unsigned int
1853DEFUN(NAME(aout,get_symtab),(abfd, location),
1854 bfd *abfd AND
1855 asymbol **location)
3f7607af 1856{
7ed4093a
SC
1857 unsigned int counter = 0;
1858 aout_symbol_type *symbase;
ce07dd7c 1859
7ed4093a 1860 if (!NAME(aout,slurp_symbol_table)(abfd)) return 0;
ce07dd7c 1861
7ed4093a
SC
1862 for (symbase = obj_aout_symbols(abfd); counter++ < bfd_get_symcount (abfd);)
1863 *(location++) = (asymbol *)( symbase++);
1864 *location++ =0;
ce07dd7c 1865 return bfd_get_symcount (abfd);
3f7607af 1866}
7ed4093a
SC
1867
1868\f
1869/* Standard reloc stuff */
1870/* Output standard relocation information to a file in target byte order. */
1871
1872void
1873DEFUN(NAME(aout,swap_std_reloc_out),(abfd, g, natptr),
1874 bfd *abfd AND
1875 arelent *g AND
1876 struct reloc_std_external *natptr)
3f7607af 1877{
6db82ea7
SC
1878 int r_index;
1879 asymbol *sym = *(g->sym_ptr_ptr);
1880 int r_extern;
1881 unsigned int r_length;
1882 int r_pcrel;
1883 int r_baserel, r_jmptable, r_relative;
1884 unsigned int r_addend;
1885 asection *output_section = sym->section->output_section;
ce07dd7c 1886
6db82ea7 1887 PUT_WORD(abfd, g->address, natptr->r_address);
ce07dd7c 1888
6db82ea7
SC
1889 r_length = g->howto->size ; /* Size as a power of two */
1890 r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
1891 /* r_baserel, r_jmptable, r_relative??? FIXME-soon */
1892 r_baserel = 0;
1893 r_jmptable = 0;
1894 r_relative = 0;
7ed4093a 1895
6db82ea7 1896 r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
7ed4093a 1897
6db82ea7
SC
1898 /* name was clobbered by aout_write_syms to be symbol index */
1899
2768b3f7
SC
1900 /* If this relocation is relative to a symbol then set the
1901 r_index to the symbols index, and the r_extern bit.
1902
1903 Absolute symbols can come in in two ways, either as an offset
1904 from the abs section, or as a symbol which has an abs value.
1905 check for that here
1906 */
1907
1908
382f2a3d 1909 if (bfd_is_com_section (output_section)
ce07dd7c
KR
1910 || output_section == &bfd_abs_section
1911 || output_section == &bfd_und_section)
1912 {
2768b3f7
SC
1913 if (bfd_abs_section.symbol == sym)
1914 {
1915 /* Whoops, looked like an abs symbol, but is really an offset
1916 from the abs section */
1917 r_index = 0;
1918 r_extern = 0;
1919 }
1920 else
1921 {
1922 /* Fill in symbol */
1923 r_extern = 1;
1924 r_index = stoi((*(g->sym_ptr_ptr))->KEEPIT);
1925
1926 }
ce07dd7c 1927 }
6db82ea7 1928 else
ce07dd7c
KR
1929 {
1930 /* Just an ordinary section */
1931 r_extern = 0;
1932 r_index = output_section->target_index;
1933 }
1934
6db82ea7
SC
1935 /* now the fun stuff */
1936 if (abfd->xvec->header_byteorder_big_p != false) {
7ed4093a
SC
1937 natptr->r_index[0] = r_index >> 16;
1938 natptr->r_index[1] = r_index >> 8;
1939 natptr->r_index[2] = r_index;
1940 natptr->r_type[0] =
6db82ea7
SC
1941 (r_extern? RELOC_STD_BITS_EXTERN_BIG: 0)
1942 | (r_pcrel? RELOC_STD_BITS_PCREL_BIG: 0)
1943 | (r_baserel? RELOC_STD_BITS_BASEREL_BIG: 0)
1944 | (r_jmptable? RELOC_STD_BITS_JMPTABLE_BIG: 0)
1945 | (r_relative? RELOC_STD_BITS_RELATIVE_BIG: 0)
1946 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG);
7ed4093a 1947 } else {
6db82ea7
SC
1948 natptr->r_index[2] = r_index >> 16;
1949 natptr->r_index[1] = r_index >> 8;
1950 natptr->r_index[0] = r_index;
1951 natptr->r_type[0] =
1952 (r_extern? RELOC_STD_BITS_EXTERN_LITTLE: 0)
7ed4093a 1953 | (r_pcrel? RELOC_STD_BITS_PCREL_LITTLE: 0)
6db82ea7
SC
1954 | (r_baserel? RELOC_STD_BITS_BASEREL_LITTLE: 0)
1955 | (r_jmptable? RELOC_STD_BITS_JMPTABLE_LITTLE: 0)
1956 | (r_relative? RELOC_STD_BITS_RELATIVE_LITTLE: 0)
1957 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE);
1958 }
3f7607af 1959}
7ed4093a
SC
1960
1961
1962/* Extended stuff */
1963/* Output extended relocation information to a file in target byte order. */
1964
1965void
1966DEFUN(NAME(aout,swap_ext_reloc_out),(abfd, g, natptr),
1967 bfd *abfd AND
1968 arelent *g AND
1969 register struct reloc_ext_external *natptr)
3f7607af 1970{
6db82ea7
SC
1971 int r_index;
1972 int r_extern;
1973 unsigned int r_type;
1974 unsigned int r_addend;
1975 asymbol *sym = *(g->sym_ptr_ptr);
1976 asection *output_section = sym->section->output_section;
1977
1978 PUT_WORD (abfd, g->address, natptr->r_address);
7ed4093a 1979
6db82ea7 1980 r_type = (unsigned int) g->howto->type;
7ed4093a 1981
6db82ea7 1982 r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
7ed4093a 1983
7ed4093a 1984
2768b3f7
SC
1985 /* If this relocation is relative to a symbol then set the
1986 r_index to the symbols index, and the r_extern bit.
1987
1988 Absolute symbols can come in in two ways, either as an offset
1989 from the abs section, or as a symbol which has an abs value.
1990 check for that here
1991 */
1992
382f2a3d 1993 if (bfd_is_com_section (output_section)
2768b3f7 1994 || output_section == &bfd_abs_section
0f213cc2 1995 || output_section == &bfd_und_section)
6db82ea7 1996 {
2768b3f7
SC
1997 if (bfd_abs_section.symbol == sym)
1998 {
1999 /* Whoops, looked like an abs symbol, but is really an offset
2000 from the abs section */
2001 r_index = 0;
2002 r_extern = 0;
2003 }
2004 else
2005 {
2006 r_extern = 1;
2007 r_index = stoi((*(g->sym_ptr_ptr))->KEEPIT);
2008 }
6db82ea7
SC
2009 }
2010 else
2011 {
2012 /* Just an ordinary section */
2013 r_extern = 0;
2014 r_index = output_section->target_index;
2015 }
2016
2017
7ed4093a
SC
2018 /* now the fun stuff */
2019 if (abfd->xvec->header_byteorder_big_p != false) {
2768b3f7
SC
2020 natptr->r_index[0] = r_index >> 16;
2021 natptr->r_index[1] = r_index >> 8;
2022 natptr->r_index[2] = r_index;
2023 natptr->r_type[0] =
2024 (r_extern? RELOC_EXT_BITS_EXTERN_BIG: 0)
2025 | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG);
2026 } else {
2027 natptr->r_index[2] = r_index >> 16;
2028 natptr->r_index[1] = r_index >> 8;
2029 natptr->r_index[0] = r_index;
2030 natptr->r_type[0] =
2031 (r_extern? RELOC_EXT_BITS_EXTERN_LITTLE: 0)
2032 | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
2033 }
7ed4093a
SC
2034
2035 PUT_WORD (abfd, r_addend, natptr->r_addend);
2036}
2037
6db82ea7
SC
2038/* BFD deals internally with all things based from the section they're
2039 in. so, something in 10 bytes into a text section with a base of
2040 50 would have a symbol (.text+10) and know .text vma was 50.
2041
2042 Aout keeps all it's symbols based from zero, so the symbol would
2043 contain 60. This macro subs the base of each section from the value
2044 to give the true offset from the section */
2045
2046
7ed4093a
SC
2047#define MOVE_ADDRESS(ad) \
2048 if (r_extern) { \
6db82ea7
SC
2049 /* undefined symbol */ \
2050 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2051 cache_ptr->addend = ad; \
2052 } else { \
2053 /* defined, section relative. replace symbol with pointer to \
2054 symbol which points to section */ \
7ed4093a
SC
2055 switch (r_index) { \
2056 case N_TEXT: \
2057 case N_TEXT | N_EXT: \
6db82ea7 2058 cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr; \
7ed4093a
SC
2059 cache_ptr->addend = ad - su->textsec->vma; \
2060 break; \
2061 case N_DATA: \
2062 case N_DATA | N_EXT: \
6db82ea7 2063 cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr; \
7ed4093a
SC
2064 cache_ptr->addend = ad - su->datasec->vma; \
2065 break; \
2066 case N_BSS: \
2067 case N_BSS | N_EXT: \
6db82ea7 2068 cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; \
7ed4093a
SC
2069 cache_ptr->addend = ad - su->bsssec->vma; \
2070 break; \
6db82ea7 2071 default: \
7ed4093a
SC
2072 case N_ABS: \
2073 case N_ABS | N_EXT: \
6db82ea7
SC
2074 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; \
2075 cache_ptr->addend = ad; \
7ed4093a
SC
2076 break; \
2077 } \
2078 } \
2079
2080void
2081DEFUN(NAME(aout,swap_ext_reloc_in), (abfd, bytes, cache_ptr, symbols),
2082 bfd *abfd AND
2083 struct reloc_ext_external *bytes AND
2084 arelent *cache_ptr AND
2085 asymbol **symbols)
2086{
2087 int r_index;
2088 int r_extern;
2089 unsigned int r_type;
6db82ea7 2090 struct aoutdata *su = &(abfd->tdata.aout_data->a);
7ed4093a
SC
2091
2092 cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
2093
2094 /* now the fun stuff */
2095 if (abfd->xvec->header_byteorder_big_p != false) {
382f2a3d
ILT
2096 r_index = (bytes->r_index[0] << 16)
2097 | (bytes->r_index[1] << 8)
2098 | bytes->r_index[2];
7ed4093a
SC
2099 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_BIG));
2100 r_type = (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_BIG)
2101 >> RELOC_EXT_BITS_TYPE_SH_BIG;
2102 } else {
382f2a3d
ILT
2103 r_index = (bytes->r_index[2] << 16)
2104 | (bytes->r_index[1] << 8)
2105 | bytes->r_index[0];
7ed4093a
SC
2106 r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
2107 r_type = (bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
2108 >> RELOC_EXT_BITS_TYPE_SH_LITTLE;
2109 }
2110
2111 cache_ptr->howto = howto_table_ext + r_type;
6db82ea7 2112 MOVE_ADDRESS(GET_SWORD(abfd, bytes->r_addend));
7ed4093a
SC
2113}
2114
2115void
2116DEFUN(NAME(aout,swap_std_reloc_in), (abfd, bytes, cache_ptr, symbols),
2117 bfd *abfd AND
2118 struct reloc_std_external *bytes AND
2119 arelent *cache_ptr AND
2120 asymbol **symbols)
2121{
2122 int r_index;
2123 int r_extern;
2124 unsigned int r_length;
2125 int r_pcrel;
2126 int r_baserel, r_jmptable, r_relative;
6db82ea7 2127 struct aoutdata *su = &(abfd->tdata.aout_data->a);
7ed4093a 2128
34dd8ba3 2129 cache_ptr->address = bfd_h_get_32 (abfd, bytes->r_address);
7ed4093a
SC
2130
2131 /* now the fun stuff */
2132 if (abfd->xvec->header_byteorder_big_p != false) {
382f2a3d
ILT
2133 r_index = (bytes->r_index[0] << 16)
2134 | (bytes->r_index[1] << 8)
2135 | bytes->r_index[2];
7ed4093a
SC
2136 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_BIG));
2137 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_BIG));
2138 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_BIG));
2139 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_BIG));
2140 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_BIG));
2141 r_length = (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_BIG)
2142 >> RELOC_STD_BITS_LENGTH_SH_BIG;
2143 } else {
382f2a3d
ILT
2144 r_index = (bytes->r_index[2] << 16)
2145 | (bytes->r_index[1] << 8)
2146 | bytes->r_index[0];
7ed4093a
SC
2147 r_extern = (0 != (bytes->r_type[0] & RELOC_STD_BITS_EXTERN_LITTLE));
2148 r_pcrel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_PCREL_LITTLE));
2149 r_baserel = (0 != (bytes->r_type[0] & RELOC_STD_BITS_BASEREL_LITTLE));
2150 r_jmptable= (0 != (bytes->r_type[0] & RELOC_STD_BITS_JMPTABLE_LITTLE));
2151 r_relative= (0 != (bytes->r_type[0] & RELOC_STD_BITS_RELATIVE_LITTLE));
2152 r_length = (bytes->r_type[0] & RELOC_STD_BITS_LENGTH_LITTLE)
2153 >> RELOC_STD_BITS_LENGTH_SH_LITTLE;
2154 }
2155
2156 cache_ptr->howto = howto_table_std + r_length + 4 * r_pcrel;
2157 /* FIXME-soon: Roll baserel, jmptable, relative bits into howto setting */
2158
2159 MOVE_ADDRESS(0);
2160}
2161
2162/* Reloc hackery */
2163
2164boolean
2165DEFUN(NAME(aout,slurp_reloc_table),(abfd, asect, symbols),
2166 bfd *abfd AND
2167 sec_ptr asect AND
2168 asymbol **symbols)
2169{
2170 unsigned int count;
2171 bfd_size_type reloc_size;
2172 PTR relocs;
2173 arelent *reloc_cache;
2174 size_t each_size;
2175
2176 if (asect->relocation) return true;
2177
2178 if (asect->flags & SEC_CONSTRUCTOR) return true;
2179
2180 if (asect == obj_datasec (abfd)) {
2181 reloc_size = exec_hdr(abfd)->a_drsize;
2182 goto doit;
2183 }
2184
2185 if (asect == obj_textsec (abfd)) {
2186 reloc_size = exec_hdr(abfd)->a_trsize;
2187 goto doit;
2188 }
2189
2190 bfd_error = invalid_operation;
2191 return false;
2192
2193 doit:
2194 bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
2195 each_size = obj_reloc_entry_size (abfd);
2196
2197 count = reloc_size / each_size;
2198
2199
2200 reloc_cache = (arelent *) bfd_zalloc (abfd, (size_t)(count * sizeof
2201 (arelent)));
2202 if (!reloc_cache) {
2203nomem:
2204 bfd_error = no_memory;
2205 return false;
2206 }
2207
2208 relocs = (PTR) bfd_alloc (abfd, reloc_size);
2209 if (!relocs) {
2210 bfd_release (abfd, reloc_cache);
2211 goto nomem;
2212 }
2213
2214 if (bfd_read (relocs, 1, reloc_size, abfd) != reloc_size) {
2215 bfd_release (abfd, relocs);
2216 bfd_release (abfd, reloc_cache);
2217 bfd_error = system_call_error;
2218 return false;
2219 }
2220
2221 if (each_size == RELOC_EXT_SIZE) {
2222 register struct reloc_ext_external *rptr = (struct reloc_ext_external *) relocs;
2223 unsigned int counter = 0;
2224 arelent *cache_ptr = reloc_cache;
2225
2226 for (; counter < count; counter++, rptr++, cache_ptr++) {
2227 NAME(aout,swap_ext_reloc_in)(abfd, rptr, cache_ptr, symbols);
2228 }
2229 } else {
2230 register struct reloc_std_external *rptr = (struct reloc_std_external*) relocs;
2231 unsigned int counter = 0;
2232 arelent *cache_ptr = reloc_cache;
2233
2234 for (; counter < count; counter++, rptr++, cache_ptr++) {
2235 NAME(aout,swap_std_reloc_in)(abfd, rptr, cache_ptr, symbols);
2236 }
2237
2238 }
2239
2240 bfd_release (abfd,relocs);
2241 asect->relocation = reloc_cache;
2242 asect->reloc_count = count;
2243 return true;
2244}
2245
2246
2247
2248/* Write out a relocation section into an object file. */
2249
2250boolean
2251DEFUN(NAME(aout,squirt_out_relocs),(abfd, section),
2252 bfd *abfd AND
2253 asection *section)
2254{
2255 arelent **generic;
2256 unsigned char *native, *natptr;
2257 size_t each_size;
2258
2259 unsigned int count = section->reloc_count;
2260 size_t natsize;
2261
2262 if (count == 0) return true;
2263
2264 each_size = obj_reloc_entry_size (abfd);
2265 natsize = each_size * count;
2266 native = (unsigned char *) bfd_zalloc (abfd, natsize);
2267 if (!native) {
2268 bfd_error = no_memory;
2269 return false;
2270 }
2271
2272 generic = section->orelocation;
2273
2274 if (each_size == RELOC_EXT_SIZE)
2275 {
2276 for (natptr = native;
2277 count != 0;
2278 --count, natptr += each_size, ++generic)
2279 NAME(aout,swap_ext_reloc_out) (abfd, *generic, (struct reloc_ext_external *)natptr);
2280 }
2281 else
2282 {
2283 for (natptr = native;
2284 count != 0;
2285 --count, natptr += each_size, ++generic)
2286 NAME(aout,swap_std_reloc_out)(abfd, *generic, (struct reloc_std_external *)natptr);
2287 }
2288
2289 if ( bfd_write ((PTR) native, 1, natsize, abfd) != natsize) {
2290 bfd_release(abfd, native);
2291 return false;
2292 }
2293 bfd_release (abfd, native);
2294
2295 return true;
2296}
2297
2298/* This is stupid. This function should be a boolean predicate */
2299unsigned int
2300DEFUN(NAME(aout,canonicalize_reloc),(abfd, section, relptr, symbols),
2301 bfd *abfd AND
2302 sec_ptr section AND
2303 arelent **relptr AND
2304 asymbol **symbols)
2305{
2306 arelent *tblptr = section->relocation;
2307 unsigned int count;
2308
2309 if (!(tblptr || NAME(aout,slurp_reloc_table)(abfd, section, symbols)))
2310 return 0;
2311
2312 if (section->flags & SEC_CONSTRUCTOR) {
2313 arelent_chain *chain = section->constructor_chain;
2314 for (count = 0; count < section->reloc_count; count ++) {
2315 *relptr ++ = &chain->relent;
2316 chain = chain->next;
2317 }
2318 }
2319 else {
2320 tblptr = section->relocation;
2321 if (!tblptr) return 0;
2322
2323 for (count = 0; count++ < section->reloc_count;)
2324 {
2325 *relptr++ = tblptr++;
2326 }
2327 }
2328 *relptr = 0;
2329
2330 return section->reloc_count;
2331}
2332
2333unsigned int
2334DEFUN(NAME(aout,get_reloc_upper_bound),(abfd, asect),
2335 bfd *abfd AND
2336 sec_ptr asect)
2337{
2338 if (bfd_get_format (abfd) != bfd_object) {
2339 bfd_error = invalid_operation;
2340 return 0;
2341 }
2342 if (asect->flags & SEC_CONSTRUCTOR) {
2343 return (sizeof (arelent *) * (asect->reloc_count+1));
2344 }
2345
2346
2347 if (asect == obj_datasec (abfd))
2348 return (sizeof (arelent *) *
2349 ((exec_hdr(abfd)->a_drsize / obj_reloc_entry_size (abfd))
2350 +1));
2351
2352 if (asect == obj_textsec (abfd))
2353 return (sizeof (arelent *) *
2354 ((exec_hdr(abfd)->a_trsize / obj_reloc_entry_size (abfd))
2355 +1));
2356
2357 bfd_error = invalid_operation;
2358 return 0;
2359}
2360
2361\f
2362 unsigned int
2363DEFUN(NAME(aout,get_symtab_upper_bound),(abfd),
2364 bfd *abfd)
2365{
2366 if (!NAME(aout,slurp_symbol_table)(abfd)) return 0;
2367
2368 return (bfd_get_symcount (abfd)+1) * (sizeof (aout_symbol_type *));
2369}
2370 alent *
2371DEFUN(NAME(aout,get_lineno),(ignore_abfd, ignore_symbol),
2372 bfd *ignore_abfd AND
2373 asymbol *ignore_symbol)
2374{
2375return (alent *)NULL;
2376}
2377
34dd8ba3
JG
2378void
2379DEFUN(NAME(aout,get_symbol_info),(ignore_abfd, symbol, ret),
2380 bfd *ignore_abfd AND
2381 asymbol *symbol AND
2382 symbol_info *ret)
2383{
2384 bfd_symbol_info (symbol, ret);
2385
2386 if (ret->type == '?')
2387 {
2388 int type_code = aout_symbol(symbol)->type & 0xff;
2389 CONST char *stab_name = aout_stab_name(type_code);
2390 static char buf[10];
2391
2392 if (stab_name == NULL)
2393 {
2394 sprintf(buf, "(%d)", type_code);
2395 stab_name = buf;
2396 }
2397 ret->type = '-';
2398 ret->stab_other = (unsigned)(aout_symbol(symbol)->other & 0xff);
2399 ret->stab_desc = (unsigned)(aout_symbol(symbol)->desc & 0xffff);
2400 ret->stab_name = stab_name;
2401 }
2402}
7ed4093a
SC
2403
2404void
2405DEFUN(NAME(aout,print_symbol),(ignore_abfd, afile, symbol, how),
2406 bfd *ignore_abfd AND
2407 PTR afile AND
2408 asymbol *symbol AND
9e2dad8e 2409 bfd_print_symbol_type how)
7ed4093a
SC
2410{
2411 FILE *file = (FILE *)afile;
2412
2413 switch (how) {
9e2dad8e 2414 case bfd_print_symbol_name:
fb3be09b
JG
2415 if (symbol->name)
2416 fprintf(file,"%s", symbol->name);
7ed4093a 2417 break;
9e2dad8e 2418 case bfd_print_symbol_more:
7ed4093a
SC
2419 fprintf(file,"%4x %2x %2x",(unsigned)(aout_symbol(symbol)->desc & 0xffff),
2420 (unsigned)(aout_symbol(symbol)->other & 0xff),
2421 (unsigned)(aout_symbol(symbol)->type));
2422 break;
9e2dad8e 2423 case bfd_print_symbol_all:
7ed4093a 2424 {
6db82ea7
SC
2425 CONST char *section_name = symbol->section->name;
2426
7ed4093a
SC
2427
2428 bfd_print_symbol_vandf((PTR)file,symbol);
2429
fb3be09b 2430 fprintf(file," %-5s %04x %02x %02x",
7ed4093a
SC
2431 section_name,
2432 (unsigned)(aout_symbol(symbol)->desc & 0xffff),
2433 (unsigned)(aout_symbol(symbol)->other & 0xff),
9e2dad8e 2434 (unsigned)(aout_symbol(symbol)->type & 0xff));
fb3be09b
JG
2435 if (symbol->name)
2436 fprintf(file," %s", symbol->name);
7ed4093a
SC
2437 }
2438 break;
2439 }
2440}
2441
2442/*
6724ff46 2443 provided a BFD, a section and an offset into the section, calculate
7ed4093a
SC
2444 and return the name of the source file and the line nearest to the
2445 wanted location.
2446*/
2447
2448boolean
2449DEFUN(NAME(aout,find_nearest_line),(abfd,
2450 section,
2451 symbols,
2452 offset,
2453 filename_ptr,
2454 functionname_ptr,
2455 line_ptr),
2456 bfd *abfd AND
2457 asection *section AND
2458 asymbol **symbols AND
2459 bfd_vma offset AND
2460 CONST char **filename_ptr AND
2461 CONST char **functionname_ptr AND
2462 unsigned int *line_ptr)
2463{
2464 /* Run down the file looking for the filename, function and linenumber */
2465 asymbol **p;
2466 static char buffer[100];
98d43107 2467 static char filename_buffer[200];
6db82ea7
SC
2468 CONST char *directory_name = NULL;
2469 CONST char *main_file_name = NULL;
2470 CONST char *current_file_name = NULL;
2471 CONST char *line_file_name = NULL; /* Value of current_file_name at line number. */
7ed4093a
SC
2472 bfd_vma high_line_vma = ~0;
2473 bfd_vma low_func_vma = 0;
2474 asymbol *func = 0;
2475 *filename_ptr = abfd->filename;
2476 *functionname_ptr = 0;
2477 *line_ptr = 0;
2478 if (symbols != (asymbol **)NULL) {
2479 for (p = symbols; *p; p++) {
2480 aout_symbol_type *q = (aout_symbol_type *)(*p);
98d43107 2481 next:
7ed4093a
SC
2482 switch (q->type){
2483 case N_SO:
3f7607af 2484 main_file_name = current_file_name = q->symbol.name;
98d43107
JG
2485 /* Look ahead to next symbol to check if that too is an N_SO. */
2486 p++;
2487 if (*p == NULL)
2488 break;
2489 q = (aout_symbol_type *)(*p);
6db82ea7 2490 if (q->type != (int)N_SO)
98d43107
JG
2491 goto next;
2492
2493 /* Found a second N_SO First is directory; second is filename. */
3f7607af
PB
2494 directory_name = current_file_name;
2495 main_file_name = current_file_name = q->symbol.name;
2496 if (obj_textsec(abfd) != section)
2497 goto done;
2498 break;
2499 case N_SOL:
2500 current_file_name = q->symbol.name;
7ed4093a 2501 break;
3f7607af 2502
7ed4093a
SC
2503 case N_SLINE:
2504
2505 case N_DSLINE:
2506 case N_BSLINE:
2507 /* We'll keep this if it resolves nearer than the one we have already */
2508 if (q->symbol.value >= offset &&
2509 q->symbol.value < high_line_vma) {
2510 *line_ptr = q->desc;
2511 high_line_vma = q->symbol.value;
3f7607af 2512 line_file_name = current_file_name;
7ed4093a
SC
2513 }
2514 break;
2515 case N_FUN:
2516 {
2517 /* We'll keep this if it is nearer than the one we have already */
2518 if (q->symbol.value >= low_func_vma &&
2519 q->symbol.value <= offset) {
2520 low_func_vma = q->symbol.value;
2521 func = (asymbol *)q;
2522 }
2523 if (*line_ptr && func) {
2524 CONST char *function = func->name;
2525 char *p;
2526 strncpy(buffer, function, sizeof(buffer)-1);
2527 buffer[sizeof(buffer)-1] = 0;
2528 /* Have to remove : stuff */
2529 p = strchr(buffer,':');
7b02b4ed 2530 if (p != NULL) { *p = '\0'; }
7ed4093a 2531 *functionname_ptr = buffer;
3f7607af 2532 goto done;
7ed4093a
SC
2533
2534 }
2535 }
2536 break;
2537 }
2538 }
2539 }
3f7607af
PB
2540
2541 done:
2542 if (*line_ptr)
2543 main_file_name = line_file_name;
2544 if (main_file_name) {
2545 if (main_file_name[0] == '/' || directory_name == NULL)
2546 *filename_ptr = main_file_name;
2547 else {
2548 sprintf(filename_buffer, "%.140s%.50s",
2549 directory_name, main_file_name);
2550 *filename_ptr = filename_buffer;
2551 }
2552 }
7ed4093a
SC
2553 return true;
2554
2555}
2556
2557int
cbdc7909
JG
2558DEFUN(NAME(aout,sizeof_headers),(abfd, execable),
2559 bfd *abfd AND
9e2dad8e 2560 boolean execable)
7ed4093a 2561{
6db82ea7 2562 return adata(abfd).exec_bytes_size;
7ed4093a 2563}
This page took 0.198724 seconds and 4 git commands to generate.