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