binutils/
[deliverable/binutils-gdb.git] / bfd / pdp11.c
CommitLineData
e135f41b 1/* BFD back-end for PDP-11 a.out binaries.
911d08a7 2 Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
e135f41b 3
42ef282f 4 This file is part of BFD, the Binary File Descriptor library.
e135f41b 5
42ef282f
NC
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
e135f41b 10
42ef282f
NC
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
e135f41b 15
42ef282f
NC
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
e135f41b
NC
19
20/* BFD backend for PDP-11, running 2.11BSD in particular.
21
22 This file was hacked up by looking hard at the existing vaxnetbsd
23 back end and the header files in 2.11BSD.
24
25 TODO
26 * support for V7 file formats
27 * support for overlay object files (see 2.11 a.out(5))
28 * support for old and very old archives
29 (see 2.11 ar(5), historical section)
dc810e39 30
e135f41b
NC
31 Search for TODO to find other areas needing more work. */
32
33#define BYTES_IN_WORD 2
34#define BYTES_IN_LONG 4
35#define ARCH_SIZE 16
36#undef TARGET_IS_BIG_ENDIAN_P
37
38#define TARGET_PAGE_SIZE 1024
39#define SEGMENT__SIZE TARGET_PAGE_SIZE
40
41#define DEFAULT_ARCH bfd_arch_pdp11
42#define DEFAULT_MID M_PDP11
43
e43d48cc
AM
44/* Do not "beautify" the CONCAT* macro args. Traditional C will not
45 remove whitespace added here, and thus will fail to concatenate
46 the tokens. */
47#define MY(OP) CONCAT2 (pdp11_aout_,OP)
48
e135f41b
NC
49/* This needs to start with a.out so GDB knows it is an a.out variant. */
50#define TARGETNAME "a.out-pdp11"
51
52/* This is the normal load address for executables. */
53#define TEXT_START_ADDR 0
54
55/* The header is not included in the text segment. */
56#define N_HEADER_IN_TEXT(x) 0
57
58/* There are no shared libraries. */
59#define N_SHARED_LIB(x) 0
60
61/* There is no flags field. */
62#define N_FLAGS(exec) 0
63
64#define N_SET_FLAGS(exec, flags) do { } while (0)
65#define N_BADMAG(x) (((x).a_info != OMAGIC) && \
66 ((x).a_info != NMAGIC) && \
67 ((x).a_info != A_MAGIC3) && \
68 ((x).a_info != A_MAGIC4) && \
69 ((x).a_info != A_MAGIC5) && \
70 ((x).a_info != A_MAGIC6))
71
72#include "bfd.h"
73
74#define external_exec pdp11_external_exec
75struct pdp11_external_exec
76 {
77 bfd_byte e_info[2]; /* magic number */
78 bfd_byte e_text[2]; /* length of text section in bytes */
79 bfd_byte e_data[2]; /* length of data section in bytes */
80 bfd_byte e_bss[2]; /* length of bss area in bytes */
81 bfd_byte e_syms[2]; /* length of symbol table in bytes */
82 bfd_byte e_entry[2]; /* start address */
83 bfd_byte e_unused[2]; /* not used */
84 bfd_byte e_flag[2]; /* relocation info stripped */
85 bfd_byte e_relocatable; /* ugly hack */
86 };
87
88#define EXEC_BYTES_SIZE (8 * 2)
89
90#define A_MAGIC1 OMAGIC
91#define OMAGIC 0407 /* ...object file or impure executable. */
92#define A_MAGIC2 NMAGIC
93#define NMAGIC 0410 /* pure executable. */
94#define ZMAGIC 0413 /* demand-paged executable. */
95#define A_MAGIC3 0411 /* separated I&D */
96#define A_MAGIC4 0405 /* overlay */
97#define A_MAGIC5 0430 /* auto-overlay (nonseparate) */
98#define A_MAGIC6 0431 /* auto-overlay (separate) */
99#define QMAGIC 0
100#define BMAGIC 0
101
102#define A_FLAG_RELOC_STRIPPED 0x0001
103
104#define external_nlist pdp11_external_nlist
105struct pdp11_external_nlist
106 {
107 bfd_byte e_unused[2]; /* unused */
108 bfd_byte e_strx[2]; /* index into string table of name */
109 bfd_byte e_type[1]; /* type of symbol */
110 bfd_byte e_ovly[1]; /* overlay number */
111 bfd_byte e_value[2]; /* value of symbol */
112 };
113
114#define EXTERNAL_NLIST_SIZE 8
115
116#define N_TXTOFF(x) (EXEC_BYTES_SIZE)
117#define N_DATOFF(x) (N_TXTOFF(x) + (x).a_text)
118#define N_TRELOFF(x) (N_DATOFF(x) + (x).a_data)
119#define N_DRELOFF(x) (N_TRELOFF(x) + (x).a_trsize)
120#define N_SYMOFF(x) (N_DRELOFF(x) + (x).a_drsize)
121#define N_STROFF(x) (N_SYMOFF(x) + (x).a_syms)
122
123#define WRITE_HEADERS(abfd, execp) pdp11_aout_write_headers (abfd, execp)
124
125#include "sysdep.h"
126#include "libbfd.h"
127#include "libaout.h"
128
129#define SWAP_MAGIC(ext) bfd_getl16 (ext)
130
131#define MY_entry_is_text_address 1
132
133#define MY_write_object_contents MY(write_object_contents)
b34976b6 134static bfd_boolean MY(write_object_contents) PARAMS ((bfd *abfd));
e135f41b
NC
135#define MY_text_includes_header 1
136
edeb6e24
AM
137static bfd_vma bfd_getp32 (const void *);
138static bfd_signed_vma bfd_getp_signed_32 (const void *);
139static void bfd_putp32 (bfd_vma, void *);
e135f41b
NC
140
141#define MY_BFD_TARGET
142
143#include "aout-target.h"
144
145const bfd_target MY(vec) =
146{
147 TARGETNAME, /* name */
148 bfd_target_aout_flavour,
149 BFD_ENDIAN_LITTLE, /* target byte order (little) */
150 BFD_ENDIAN_LITTLE, /* target headers byte order (little) */
151 (HAS_RELOC | EXEC_P | /* object flags */
152 HAS_LINENO | HAS_DEBUG |
153 HAS_SYMS | HAS_LOCALS | WP_TEXT),
154 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
155 MY_symbol_leading_char,
156 AR_PAD_CHAR, /* ar_pad_char */
157 15, /* ar_max_namelen */
158 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
159 bfd_getp32, bfd_getp_signed_32, bfd_putp32,
160 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
161 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
162 bfd_getp32, bfd_getp_signed_32, bfd_putp32,
163 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
164 {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
165 bfd_generic_archive_p, MY_core_file_p},
166 {bfd_false, MY_mkobject, /* bfd_set_format */
167 _bfd_generic_mkarchive, bfd_false},
168 {bfd_false, MY_write_object_contents, /* bfd_write_contents */
169 _bfd_write_archive_contents, bfd_false},
170
171 BFD_JUMP_TABLE_GENERIC (MY),
172 BFD_JUMP_TABLE_COPY (MY),
173 BFD_JUMP_TABLE_CORE (MY),
174 BFD_JUMP_TABLE_ARCHIVE (MY),
175 BFD_JUMP_TABLE_SYMBOLS (MY),
176 BFD_JUMP_TABLE_RELOCS (MY),
177 BFD_JUMP_TABLE_WRITE (MY),
178 BFD_JUMP_TABLE_LINK (MY),
179 BFD_JUMP_TABLE_DYNAMIC (MY),
180
181 /* Alternative_target */
182 NULL,
dc810e39 183
e135f41b
NC
184 (PTR) MY_backend_data,
185};
186
187/* start of modified aoutx.h */
188/* BFD semi-generic back-end for a.out binaries.
189 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 1998
190 Free Software Foundation, Inc.
191 Written by Cygnus Support.
192
193This file is part of BFD, the Binary File Descriptor library.
194
195This program is free software; you can redistribute it and/or modify
196it under the terms of the GNU General Public License as published by
197the Free Software Foundation; either version 2 of the License, or
198(at your option) any later version.
199
200This program is distributed in the hope that it will be useful,
201but WITHOUT ANY WARRANTY; without even the implied warranty of
202MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
203GNU General Public License for more details.
204
205You should have received a copy of the GNU General Public License
206along with this program; if not, write to the Free Software
207Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
208
209/*
210SECTION
211 a.out backends
212
213
214DESCRIPTION
215
216 BFD supports a number of different flavours of a.out format,
217 though the major differences are only the sizes of the
218 structures on disk, and the shape of the relocation
219 information.
220
221 The support is split into a basic support file @file{aoutx.h}
222 and other files which derive functions from the base. One
223 derivation file is @file{aoutf1.h} (for a.out flavour 1), and
224 adds to the basic a.out functions support for sun3, sun4, 386
225 and 29k a.out files, to create a target jump vector for a
226 specific target.
227
228 This information is further split out into more specific files
229 for each machine, including @file{sunos.c} for sun3 and sun4,
230 @file{newsos3.c} for the Sony NEWS, and @file{demo64.c} for a
231 demonstration of a 64 bit a.out format.
232
233 The base file @file{aoutx.h} defines general mechanisms for
234 reading and writing records to and from disk and various
235 other methods which BFD requires. It is included by
236 @file{aout32.c} and @file{aout64.c} to form the names
237 <<aout_32_swap_exec_header_in>>, <<aout_64_swap_exec_header_in>>, etc.
238
239 As an example, this is what goes on to make the back end for a
240 sun4, from @file{aout32.c}:
241
242| #define ARCH_SIZE 32
243| #include "aoutx.h"
244
245 Which exports names:
246
247| ...
248| aout_32_canonicalize_reloc
249| aout_32_find_nearest_line
250| aout_32_get_lineno
251| aout_32_get_reloc_upper_bound
252| ...
253
254 from @file{sunos.c}:
255
256| #define TARGET_NAME "a.out-sunos-big"
257| #define VECNAME sunos_big_vec
258| #include "aoutf1.h"
259
260 requires all the names from @file{aout32.c}, and produces the jump vector
261
262| sunos_big_vec
263
264 The file @file{host-aout.c} is a special case. It is for a large set
265 of hosts that use ``more or less standard'' a.out files, and
266 for which cross-debugging is not interesting. It uses the
267 standard 32-bit a.out support routines, but determines the
268 file offsets and addresses of the text, data, and BSS
269 sections, the machine architecture and machine type, and the
270 entry point address, in a host-dependent manner. Once these
271 values have been determined, generic code is used to handle
272 the object file.
273
274 When porting it to run on a new system, you must supply:
275
276| HOST_PAGE_SIZE
277| HOST_SEGMENT_SIZE
278| HOST_MACHINE_ARCH (optional)
279| HOST_MACHINE_MACHINE (optional)
280| HOST_TEXT_START_ADDR
281| HOST_STACK_END_ADDR
282
283 in the file @file{../include/sys/h-@var{XXX}.h} (for your host). These
284 values, plus the structures and macros defined in @file{a.out.h} on
285 your host system, will produce a BFD target that will access
286 ordinary a.out files on your host. To configure a new machine
287 to use @file{host-aout.c}, specify:
288
289| TDEFAULTS = -DDEFAULT_VECTOR=host_aout_big_vec
290| TDEPFILES= host-aout.o trad-core.o
291
292 in the @file{config/@var{XXX}.mt} file, and modify @file{configure.in}
293 to use the
294 @file{@var{XXX}.mt} file (by setting "<<bfd_target=XXX>>") when your
295 configuration is selected.
296
297*/
298
299/* Some assumptions:
300 * Any BFD with D_PAGED set is ZMAGIC, and vice versa.
301 Doesn't matter what the setting of WP_TEXT is on output, but it'll
302 get set on input.
303 * Any BFD with D_PAGED clear and WP_TEXT set is NMAGIC.
304 * Any BFD with both flags clear is OMAGIC.
305 (Just want to make these explicit, so the conditions tested in this
306 file make sense if you're more familiar with a.out than with BFD.) */
307
308#define KEEPIT udata.i
309
310#include <string.h> /* For strchr and friends */
e135f41b
NC
311#include "bfd.h"
312#include "sysdep.h"
3882b010 313#include "safe-ctype.h"
e135f41b
NC
314#include "bfdlink.h"
315
316#include "libaout.h"
317/*#include "libbfd.h"*/
318#include "aout/aout64.h"
319#include "aout/stab_gnu.h"
320#include "aout/ar.h"
321
322#undef N_TYPE
323#undef N_UNDF
324#undef N_ABS
325#undef N_TEXT
326#undef N_DATA
327#undef N_BSS
328#undef N_REG
329#undef N_FN
330#undef N_EXT
331#define N_TYPE 0x1f /* type mask */
332#define N_UNDF 0x00 /* undefined */
333#define N_ABS 0x01 /* absolute */
334#define N_TEXT 0x02 /* text segment */
335#define N_DATA 0x03 /* data segment */
336#define N_BSS 0x04 /* bss segment */
337#define N_REG 0x14 /* register symbol */
338#define N_FN 0x1f /* file name */
339
340#define N_EXT 0x20 /* external flag */
341
342#define RELOC_SIZE 2
343
344struct pdp11_aout_reloc_external
345{
346 bfd_byte e_reloc_entry[2];
347};
348
349#define RELFLG 0x0001 /* pc-relative flag */
350#define RTYPE 0x000e /* type mask */
351#define RIDXMASK 0xfff0 /* index mask */
352
353#define RABS 0x00 /* absolute */
354#define RTEXT 0x02 /* text */
355#define RDATA 0x04 /* data */
356#define RBSS 0x06 /* bss */
357#define REXT 0x08 /* external */
358
359#define RINDEX(x) (((x) & 0xfff0) >> 4)
360
b34976b6
AM
361static bfd_boolean aout_get_external_symbols PARAMS ((bfd *));
362static bfd_boolean translate_from_native_sym_flags
e135f41b 363 PARAMS ((bfd *, aout_symbol_type *));
b34976b6 364static bfd_boolean translate_to_native_sym_flags
e135f41b
NC
365 PARAMS ((bfd *, asymbol *, struct external_nlist *));
366static void adjust_o_magic PARAMS ((bfd *, struct internal_exec *));
367static void adjust_z_magic PARAMS ((bfd *, struct internal_exec *));
368static void adjust_n_magic PARAMS ((bfd *, struct internal_exec *));
369
42ef282f
NC
370static int pdp11_aout_write_headers PARAMS ((bfd *, struct internal_exec *));
371void pdp11_aout_swap_reloc_out PARAMS ((bfd *, arelent *, struct pdp11_aout_reloc_external *));
372void pdp11_aout_swap_reloc_in
373PARAMS ((bfd *, struct pdp11_aout_reloc_external *, arelent *,
374 bfd_size_type, asymbol **, bfd_size_type));
375
e135f41b
NC
376/*
377SUBSECTION
378 Relocations
379
380DESCRIPTION
381 The file @file{aoutx.h} provides for both the @emph{standard}
382 and @emph{extended} forms of a.out relocation records.
383
384 The standard records contain only an
385 address, a symbol index, and a type field. The extended records
386 (used on 29ks and sparcs) also have a full integer for an
387 addend.
388
389*/
390
391#ifndef MY_final_link_relocate
392#define MY_final_link_relocate _bfd_final_link_relocate
393#endif
394
395#ifndef MY_relocate_contents
396#define MY_relocate_contents _bfd_relocate_contents
397#endif
398
399reloc_howto_type howto_table_pdp11[] =
400{
401 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
b34976b6
AM
402HOWTO( 0, 0, 1, 16, FALSE, 0, complain_overflow_signed,0,"16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
403HOWTO( 1, 0, 1, 16, TRUE, 0, complain_overflow_signed,0,"DISP16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
e135f41b
NC
404};
405
406#define TABLE_SIZE(TABLE) (sizeof(TABLE)/sizeof(TABLE[0]))
407
408reloc_howto_type *
409NAME(aout,reloc_type_lookup) (abfd,code)
410 bfd * abfd ATTRIBUTE_UNUSED;
411 bfd_reloc_code_real_type code;
412{
413 switch (code)
414 {
415 case BFD_RELOC_16:
416 return &howto_table_pdp11[0];
417 case BFD_RELOC_16_PCREL:
418 return &howto_table_pdp11[1];
419 default:
420 return (reloc_howto_type *)NULL;
421 }
422}
423
424static int
425pdp11_aout_write_headers (abfd, execp)
426 bfd *abfd;
427 struct internal_exec *execp;
428{
429 struct external_exec exec_bytes;
430 bfd_size_type text_size;
431 file_ptr text_end;
432
433 if (adata(abfd).magic == undecided_magic)
434 NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end);
435
436 execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE;
437 execp->a_entry = bfd_get_start_address (abfd);
438
439 if (obj_textsec (abfd)->reloc_count > 0 ||
440 obj_datasec (abfd)->reloc_count > 0)
441 {
442 execp->a_trsize = execp->a_text;
443 execp->a_drsize = execp->a_data;
444 }
445 else
446 {
447 execp->a_trsize = 0;
448 execp->a_drsize = 0;
449 }
450
451 NAME(aout,swap_exec_header_out) (abfd, execp, &exec_bytes);
452
453 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
b34976b6 454 return FALSE;
e135f41b 455
dc810e39 456 if (bfd_bwrite ((PTR) &exec_bytes, (bfd_size_type) EXEC_BYTES_SIZE, abfd)
e135f41b 457 != EXEC_BYTES_SIZE)
b34976b6 458 return FALSE;
e135f41b
NC
459
460 /* Now write out reloc info, followed by syms and strings */
461
462 if (bfd_get_outsymbols (abfd) != (asymbol **) NULL
463 && bfd_get_symcount (abfd) != 0)
464 {
dc810e39 465 if (bfd_seek (abfd, (file_ptr) (N_SYMOFF(*execp)), SEEK_SET) != 0)
b34976b6 466 return FALSE;
e135f41b 467
dc810e39 468 if (! NAME(aout,write_syms) (abfd))
b34976b6 469 return FALSE;
e135f41b
NC
470 }
471
472 if (obj_textsec (abfd)->reloc_count > 0 ||
473 obj_datasec (abfd)->reloc_count > 0)
474 {
dc810e39 475 if (bfd_seek (abfd, (file_ptr) (N_TRELOFF(*execp)), SEEK_SET) != 0)
b34976b6 476 return FALSE;
e135f41b 477 if (!NAME(aout,squirt_out_relocs) (abfd, obj_textsec (abfd)))
b34976b6 478 return FALSE;
e135f41b 479
dc810e39 480 if (bfd_seek (abfd, (file_ptr) (N_DRELOFF(*execp)), SEEK_SET) != 0)
b34976b6 481 return FALSE;
dc810e39 482 if (!NAME(aout,squirt_out_relocs) (abfd, obj_datasec (abfd)))
b34976b6 483 return FALSE;
e135f41b
NC
484 }
485
b34976b6 486 return TRUE;
dc810e39 487}
e135f41b
NC
488
489/* Write an object file.
490 Section contents have already been written. We write the
491 file header, symbols, and relocation. */
492
b34976b6 493static bfd_boolean
e135f41b
NC
494MY(write_object_contents) (abfd)
495 bfd *abfd;
496{
497 struct internal_exec *execp = exec_hdr (abfd);
498
499 /* We must make certain that the magic number has been set. This
500 will normally have been done by set_section_contents, but only if
501 there actually are some section contents. */
502 if (! abfd->output_has_begun)
503 {
504 bfd_size_type text_size;
505 file_ptr text_end;
506
507 NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end);
508 }
509
510 obj_reloc_entry_size (abfd) = RELOC_SIZE;
511
512 return WRITE_HEADERS(abfd, execp);
513}
514
515/*
516SUBSECTION
517 Internal entry points
518
519DESCRIPTION
520 @file{aoutx.h} exports several routines for accessing the
521 contents of an a.out file, which are gathered and exported in
522 turn by various format specific files (eg sunos.c).
523
524*/
525
526/*
527FUNCTION
528 aout_@var{size}_swap_exec_header_in
529
530SYNOPSIS
531 void aout_@var{size}_swap_exec_header_in,
532 (bfd *abfd,
533 struct external_exec *raw_bytes,
534 struct internal_exec *execp);
535
536DESCRIPTION
537 Swap the information in an executable header @var{raw_bytes} taken
538 from a raw byte stream memory image into the internal exec header
539 structure @var{execp}.
540*/
541
542#ifndef NAME_swap_exec_header_in
543void
544NAME(aout,swap_exec_header_in) (abfd, raw_bytes, execp)
545 bfd *abfd;
546 struct external_exec *raw_bytes;
547 struct internal_exec *execp;
548{
549 struct external_exec *bytes = (struct external_exec *)raw_bytes;
550
551 /* The internal_exec structure has some fields that are unused in this
552 configuration (IE for i960), so ensure that all such uninitialized
553 fields are zero'd out. There are places where two of these structs
554 are memcmp'd, and thus the contents do matter. */
555 memset ((PTR) execp, 0, sizeof (struct internal_exec));
556 /* Now fill in fields in the execp, from the bytes in the raw data. */
557 execp->a_info = GET_MAGIC (abfd, bytes->e_info);
558 execp->a_text = GET_WORD (abfd, bytes->e_text);
559 execp->a_data = GET_WORD (abfd, bytes->e_data);
560 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
561 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
562 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
563
564 if (GET_WORD (abfd, bytes->e_flag) & A_FLAG_RELOC_STRIPPED)
565 {
566 execp->a_trsize = 0;
567 execp->a_drsize = 0;
568 }
569 else
570 {
571 execp->a_trsize = execp->a_text;
572 execp->a_drsize = execp->a_data;
573 }
574}
575#define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in)
576#endif
577
578/*
579FUNCTION
580 aout_@var{size}_swap_exec_header_out
581
582SYNOPSIS
583 void aout_@var{size}_swap_exec_header_out
584 (bfd *abfd,
585 struct internal_exec *execp,
586 struct external_exec *raw_bytes);
587
588DESCRIPTION
589 Swap the information in an internal exec header structure
590 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
591*/
592void
593NAME(aout,swap_exec_header_out) (abfd, execp, raw_bytes)
594 bfd *abfd;
595 struct internal_exec *execp;
596 struct external_exec *raw_bytes;
597{
598 struct external_exec *bytes = (struct external_exec *)raw_bytes;
599
600 /* Now fill in fields in the raw data, from the fields in the exec struct. */
601 PUT_MAGIC (abfd, execp->a_info, bytes->e_info);
602 PUT_WORD (abfd, execp->a_text, bytes->e_text);
603 PUT_WORD (abfd, execp->a_data, bytes->e_data);
604 PUT_WORD (abfd, execp->a_bss, bytes->e_bss);
605 PUT_WORD (abfd, execp->a_syms, bytes->e_syms);
606 PUT_WORD (abfd, execp->a_entry, bytes->e_entry);
607 PUT_WORD (abfd, 0, bytes->e_unused);
608
609 if ((execp->a_trsize == 0 || execp->a_text == 0) &&
610 (execp->a_drsize == 0 || execp->a_data == 0))
611 PUT_WORD (abfd, A_FLAG_RELOC_STRIPPED, bytes->e_flag);
612 else if (execp->a_trsize == execp->a_text &&
613 execp->a_drsize == execp->a_data)
614 PUT_WORD (abfd, 0, bytes->e_flag);
615 else
616 {
617 /* TODO: print a proper warning message */
618 fprintf (stderr, "BFD:%s:%d: internal error\n", __FILE__, __LINE__);
619 PUT_WORD (abfd, 0, bytes->e_flag);
620 }
621}
622
623/* Make all the section for an a.out file. */
624
b34976b6 625bfd_boolean
e135f41b
NC
626NAME(aout,make_sections) (abfd)
627 bfd *abfd;
628{
629 if (obj_textsec (abfd) == (asection *) NULL
630 && bfd_make_section (abfd, ".text") == (asection *) NULL)
b34976b6 631 return FALSE;
e135f41b
NC
632 if (obj_datasec (abfd) == (asection *) NULL
633 && bfd_make_section (abfd, ".data") == (asection *) NULL)
b34976b6 634 return FALSE;
e135f41b
NC
635 if (obj_bsssec (abfd) == (asection *) NULL
636 && bfd_make_section (abfd, ".bss") == (asection *) NULL)
b34976b6
AM
637 return FALSE;
638 return TRUE;
e135f41b
NC
639}
640
641/*
642FUNCTION
643 aout_@var{size}_some_aout_object_p
644
645SYNOPSIS
646 const bfd_target *aout_@var{size}_some_aout_object_p
647 (bfd *abfd,
648 const bfd_target *(*callback_to_real_object_p)());
649
650DESCRIPTION
651 Some a.out variant thinks that the file open in @var{abfd}
652 checking is an a.out file. Do some more checking, and set up
653 for access if it really is. Call back to the calling
654 environment's "finish up" function just before returning, to
655 handle any last-minute setup.
656*/
657
658const bfd_target *
659NAME(aout,some_aout_object_p) (abfd, execp, callback_to_real_object_p)
660 bfd *abfd;
661 struct internal_exec *execp;
662 const bfd_target *(*callback_to_real_object_p) PARAMS ((bfd *));
663{
664 struct aout_data_struct *rawptr, *oldrawptr;
665 const bfd_target *result;
dc810e39 666 bfd_size_type amt = sizeof (struct aout_data_struct);
e135f41b 667
dc810e39 668 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
e135f41b
NC
669 if (rawptr == NULL)
670 return 0;
671
672 oldrawptr = abfd->tdata.aout_data;
673 abfd->tdata.aout_data = rawptr;
674
675 /* Copy the contents of the old tdata struct.
676 In particular, we want the subformat, since for hpux it was set in
677 hp300hpux.c:swap_exec_header_in and will be used in
678 hp300hpux.c:callback. */
679 if (oldrawptr != NULL)
680 *abfd->tdata.aout_data = *oldrawptr;
681
682 abfd->tdata.aout_data->a.hdr = &rawptr->e;
683 *(abfd->tdata.aout_data->a.hdr) = *execp; /* Copy in the internal_exec struct */
684 execp = abfd->tdata.aout_data->a.hdr;
685
686 /* Set the file flags */
687 abfd->flags = BFD_NO_FLAGS;
688 if (execp->a_drsize || execp->a_trsize)
689 abfd->flags |= HAS_RELOC;
690 /* Setting of EXEC_P has been deferred to the bottom of this function */
691 if (execp->a_syms)
692 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
693 if (N_DYNAMIC(*execp))
694 abfd->flags |= DYNAMIC;
695
696 if (N_MAGIC (*execp) == ZMAGIC)
697 {
698 abfd->flags |= D_PAGED | WP_TEXT;
699 adata (abfd).magic = z_magic;
700 }
701 else if (N_MAGIC (*execp) == QMAGIC)
702 {
703 abfd->flags |= D_PAGED | WP_TEXT;
704 adata (abfd).magic = z_magic;
705 adata (abfd).subformat = q_magic_format;
706 }
707 else if (N_MAGIC (*execp) == NMAGIC)
708 {
709 abfd->flags |= WP_TEXT;
710 adata (abfd).magic = n_magic;
711 }
712 else if (N_MAGIC (*execp) == OMAGIC
713 || N_MAGIC (*execp) == BMAGIC)
714 adata (abfd).magic = o_magic;
715 else
716 {
717 /* Should have been checked with N_BADMAG before this routine
718 was called. */
719 abort ();
720 }
721
722 bfd_get_start_address (abfd) = execp->a_entry;
723
724 obj_aout_symbols (abfd) = (aout_symbol_type *)NULL;
725 bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
726
727 /* The default relocation entry size is that of traditional V7 Unix. */
728 obj_reloc_entry_size (abfd) = RELOC_SIZE;
729
730 /* The default symbol entry size is that of traditional Unix. */
731 obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
732
733#ifdef USE_MMAP
734 bfd_init_window (&obj_aout_sym_window (abfd));
735 bfd_init_window (&obj_aout_string_window (abfd));
736#endif
737 obj_aout_external_syms (abfd) = NULL;
738 obj_aout_external_strings (abfd) = NULL;
739 obj_aout_sym_hashes (abfd) = NULL;
740
741 if (! NAME(aout,make_sections) (abfd))
742 return NULL;
743
eea6121a
AM
744 obj_datasec (abfd)->size = execp->a_data;
745 obj_bsssec (abfd)->size = execp->a_bss;
e135f41b
NC
746
747 obj_textsec (abfd)->flags =
748 (execp->a_trsize != 0
749 ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
750 : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
751 obj_datasec (abfd)->flags =
752 (execp->a_drsize != 0
753 ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
754 : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
755 obj_bsssec (abfd)->flags = SEC_ALLOC;
756
757#ifdef THIS_IS_ONLY_DOCUMENTATION
758 /* The common code can't fill in these things because they depend
759 on either the start address of the text segment, the rounding
760 up of virtual addresses between segments, or the starting file
761 position of the text segment -- all of which varies among different
762 versions of a.out. */
763
764 /* Call back to the format-dependent code to fill in the rest of the
765 fields and do any further cleanup. Things that should be filled
766 in by the callback: */
767
768 struct exec *execp = exec_hdr (abfd);
769
770 obj_textsec (abfd)->size = N_TXTSIZE(*execp);
e135f41b
NC
771 /* data and bss are already filled in since they're so standard */
772
773 /* The virtual memory addresses of the sections */
774 obj_textsec (abfd)->vma = N_TXTADDR(*execp);
775 obj_datasec (abfd)->vma = N_DATADDR(*execp);
776 obj_bsssec (abfd)->vma = N_BSSADDR(*execp);
777
778 /* The file offsets of the sections */
779 obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
780 obj_datasec (abfd)->filepos = N_DATOFF(*execp);
781
782 /* The file offsets of the relocation info */
783 obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);
784 obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);
785
786 /* The file offsets of the string table and symbol table. */
787 obj_str_filepos (abfd) = N_STROFF (*execp);
788 obj_sym_filepos (abfd) = N_SYMOFF (*execp);
789
790 /* Determine the architecture and machine type of the object file. */
791 abfd->obj_arch = bfd_arch_obscure;
792
793 adata(abfd)->page_size = TARGET_PAGE_SIZE;
794 adata(abfd)->segment_size = SEGMENT_SIZE;
795 adata(abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
796
797 return abfd->xvec;
798
799 /* The architecture is encoded in various ways in various a.out variants,
800 or is not encoded at all in some of them. The relocation size depends
801 on the architecture and the a.out variant. Finally, the return value
802 is the bfd_target vector in use. If an error occurs, return zero and
803 set bfd_error to the appropriate error code.
804
805 Formats such as b.out, which have additional fields in the a.out
806 header, should cope with them in this callback as well. */
807#endif /* DOCUMENTATION */
808
809 result = (*callback_to_real_object_p)(abfd);
810
811 /* Now that the segment addresses have been worked out, take a better
812 guess at whether the file is executable. If the entry point
813 is within the text segment, assume it is. (This makes files
814 executable even if their entry point address is 0, as long as
815 their text starts at zero.).
816
817 This test had to be changed to deal with systems where the text segment
818 runs at a different location than the default. The problem is that the
819 entry address can appear to be outside the text segment, thus causing an
820 erroneous conclusion that the file isn't executable.
821
822 To fix this, we now accept any non-zero entry point as an indication of
823 executability. This will work most of the time, since only the linker
824 sets the entry point, and that is likely to be non-zero for most systems. */
825
826 if (execp->a_entry != 0
827 || (execp->a_entry >= obj_textsec(abfd)->vma
eea6121a 828 && execp->a_entry < obj_textsec(abfd)->vma + obj_textsec(abfd)->size))
e135f41b
NC
829 abfd->flags |= EXEC_P;
830#ifdef STAT_FOR_EXEC
831 else
832 {
833 struct stat stat_buf;
834
835 /* The original heuristic doesn't work in some important cases.
836 The a.out file has no information about the text start
837 address. For files (like kernels) linked to non-standard
838 addresses (ld -Ttext nnn) the entry point may not be between
839 the default text start (obj_textsec(abfd)->vma) and
840 (obj_textsec(abfd)->vma) + text size. This is not just a mach
841 issue. Many kernels are loaded at non standard addresses. */
842 if (abfd->iostream != NULL
843 && (abfd->flags & BFD_IN_MEMORY) == 0
844 && (fstat(fileno((FILE *) (abfd->iostream)), &stat_buf) == 0)
845 && ((stat_buf.st_mode & 0111) != 0))
846 abfd->flags |= EXEC_P;
847 }
848#endif /* STAT_FOR_EXEC */
849
850 if (result)
851 {
852#if 0 /* These should be set correctly anyways. */
853 abfd->sections = obj_textsec (abfd);
854 obj_textsec (abfd)->next = obj_datasec (abfd);
855 obj_datasec (abfd)->next = obj_bsssec (abfd);
856#endif
857 }
858 else
859 {
860 free (rawptr);
861 abfd->tdata.aout_data = oldrawptr;
862 }
863 return result;
864}
865
866/*
867FUNCTION
868 aout_@var{size}_mkobject
869
870SYNOPSIS
b34976b6 871 bfd_boolean aout_@var{size}_mkobject, (bfd *abfd);
e135f41b
NC
872
873DESCRIPTION
874 Initialize BFD @var{abfd} for use with a.out files.
875*/
876
b34976b6 877bfd_boolean
e135f41b
NC
878NAME(aout,mkobject) (abfd)
879 bfd *abfd;
880{
881 struct aout_data_struct *rawptr;
dc810e39 882 bfd_size_type amt = sizeof (struct aout_data_struct);
e135f41b
NC
883
884 bfd_set_error (bfd_error_system_call);
885
886 /* Use an intermediate variable for clarity */
dc810e39 887 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
e135f41b
NC
888
889 if (rawptr == NULL)
b34976b6 890 return FALSE;
e135f41b
NC
891
892 abfd->tdata.aout_data = rawptr;
893 exec_hdr (abfd) = &(rawptr->e);
894
895 obj_textsec (abfd) = (asection *)NULL;
896 obj_datasec (abfd) = (asection *)NULL;
897 obj_bsssec (abfd) = (asection *)NULL;
898
b34976b6 899 return TRUE;
e135f41b
NC
900}
901
902
903/*
904FUNCTION
905 aout_@var{size}_machine_type
906
907SYNOPSIS
908 enum machine_type aout_@var{size}_machine_type
909 (enum bfd_architecture arch,
910 unsigned long machine));
911
912DESCRIPTION
913 Keep track of machine architecture and machine type for
914 a.out's. Return the <<machine_type>> for a particular
915 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
916 and machine can't be represented in a.out format.
917
918 If the architecture is understood, machine type 0 (default)
919 is always understood.
920*/
921
922enum machine_type
923NAME(aout,machine_type) (arch, machine, unknown)
924 enum bfd_architecture arch;
925 unsigned long machine;
b34976b6 926 bfd_boolean *unknown;
e135f41b
NC
927{
928 enum machine_type arch_flags;
929
930 arch_flags = M_UNKNOWN;
b34976b6 931 *unknown = TRUE;
e135f41b
NC
932
933 switch (arch)
934 {
935 case bfd_arch_sparc:
936 if (machine == 0
937 || machine == bfd_mach_sparc
938 || machine == bfd_mach_sparc_sparclite
939 || machine == bfd_mach_sparc_v9)
940 arch_flags = M_SPARC;
941 else if (machine == bfd_mach_sparc_sparclet)
942 arch_flags = M_SPARCLET;
943 break;
944
945 case bfd_arch_m68k:
946 switch (machine)
947 {
948 case 0: arch_flags = M_68010; break;
b34976b6 949 case bfd_mach_m68000: arch_flags = M_UNKNOWN; *unknown = FALSE; break;
e135f41b
NC
950 case bfd_mach_m68010: arch_flags = M_68010; break;
951 case bfd_mach_m68020: arch_flags = M_68020; break;
952 default: arch_flags = M_UNKNOWN; break;
953 }
954 break;
955
956 case bfd_arch_i386:
250d94fd
AM
957 if (machine == 0
958 || machine == bfd_mach_i386_i386
959 || machine == bfd_mach_i386_i386_intel_syntax)
960 arch_flags = M_386;
e135f41b
NC
961 break;
962
963 case bfd_arch_a29k:
964 if (machine == 0) arch_flags = M_29K;
965 break;
966
967 case bfd_arch_arm:
968 if (machine == 0) arch_flags = M_ARM;
969 break;
970
971 case bfd_arch_mips:
972 switch (machine)
973 {
974 case 0:
975 case 2000:
976 case bfd_mach_mips3000:
977 arch_flags = M_MIPS1;
978 break;
979 case bfd_mach_mips4000: /* mips3 */
980 case bfd_mach_mips4400:
981 case bfd_mach_mips8000: /* mips4 */
982 case bfd_mach_mips6000: /* real mips2: */
983 arch_flags = M_MIPS2;
984 break;
985 default:
986 arch_flags = M_UNKNOWN;
987 break;
988 }
989 break;
990
991 case bfd_arch_ns32k:
992 switch (machine)
993 {
994 case 0: arch_flags = M_NS32532; break;
995 case 32032: arch_flags = M_NS32032; break;
996 case 32532: arch_flags = M_NS32532; break;
997 default: arch_flags = M_UNKNOWN; break;
998 }
999 break;
1000
1001 case bfd_arch_pdp11:
1002 /* TODO: arch_flags = M_PDP11; */
b34976b6 1003 *unknown = FALSE;
e135f41b
NC
1004 break;
1005
1006 case bfd_arch_vax:
b34976b6 1007 *unknown = FALSE;
e135f41b 1008 break;
dc810e39 1009
e135f41b
NC
1010 default:
1011 arch_flags = M_UNKNOWN;
1012 }
1013
1014 if (arch_flags != M_UNKNOWN)
b34976b6 1015 *unknown = FALSE;
e135f41b
NC
1016
1017 return arch_flags;
1018}
1019
1020
1021/*
1022FUNCTION
1023 aout_@var{size}_set_arch_mach
1024
1025SYNOPSIS
b34976b6 1026 bfd_boolean aout_@var{size}_set_arch_mach,
e135f41b
NC
1027 (bfd *,
1028 enum bfd_architecture arch,
1029 unsigned long machine));
1030
1031DESCRIPTION
1032 Set the architecture and the machine of the BFD @var{abfd} to the
1033 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
1034 can support the architecture required.
1035*/
1036
b34976b6 1037bfd_boolean
e135f41b
NC
1038NAME(aout,set_arch_mach) (abfd, arch, machine)
1039 bfd *abfd;
1040 enum bfd_architecture arch;
1041 unsigned long machine;
1042{
1043 if (! bfd_default_set_arch_mach (abfd, arch, machine))
b34976b6 1044 return FALSE;
e135f41b
NC
1045
1046 if (arch != bfd_arch_unknown)
1047 {
b34976b6 1048 bfd_boolean unknown;
e135f41b
NC
1049
1050 NAME(aout,machine_type) (arch, machine, &unknown);
1051 if (unknown)
b34976b6 1052 return FALSE;
e135f41b
NC
1053 }
1054
1055 obj_reloc_entry_size (abfd) = RELOC_SIZE;
1056
1057 return (*aout_backend_info(abfd)->set_sizes) (abfd);
1058}
1059
1060static void
1061adjust_o_magic (abfd, execp)
1062 bfd *abfd;
1063 struct internal_exec *execp;
1064{
1065 file_ptr pos = adata (abfd).exec_bytes_size;
1066 bfd_vma vma = 0;
1067 int pad = 0;
1068
1069 /* Text. */
1070 obj_textsec (abfd)->filepos = pos;
1071 if (! obj_textsec (abfd)->user_set_vma)
1072 obj_textsec (abfd)->vma = vma;
1073 else
1074 vma = obj_textsec (abfd)->vma;
1075
eea6121a
AM
1076 pos += obj_textsec (abfd)->size;
1077 vma += obj_textsec (abfd)->size;
e135f41b
NC
1078
1079 /* Data. */
1080 if (!obj_datasec (abfd)->user_set_vma)
1081 {
1082#if 0 /* ?? Does alignment in the file image really matter? */
1083 pad = align_power (vma, obj_datasec (abfd)->alignment_power) - vma;
1084#endif
eea6121a 1085 obj_textsec (abfd)->size += pad;
e135f41b
NC
1086 pos += pad;
1087 vma += pad;
1088 obj_datasec (abfd)->vma = vma;
1089 }
1090 else
1091 vma = obj_datasec (abfd)->vma;
1092 obj_datasec (abfd)->filepos = pos;
eea6121a
AM
1093 pos += obj_datasec (abfd)->size;
1094 vma += obj_datasec (abfd)->size;
e135f41b
NC
1095
1096 /* BSS. */
1097 if (! obj_bsssec (abfd)->user_set_vma)
1098 {
1099#if 0
1100 pad = align_power (vma, obj_bsssec (abfd)->alignment_power) - vma;
1101#endif
eea6121a 1102 obj_datasec (abfd)->size += pad;
e135f41b
NC
1103 pos += pad;
1104 vma += pad;
1105 obj_bsssec (abfd)->vma = vma;
1106 }
1107 else
1108 {
08da05b0 1109 /* The VMA of the .bss section is set by the VMA of the
e135f41b
NC
1110 .data section plus the size of the .data section. We may
1111 need to add padding bytes to make this true. */
1112 pad = obj_bsssec (abfd)->vma - vma;
1113 if (pad > 0)
1114 {
eea6121a 1115 obj_datasec (abfd)->size += pad;
e135f41b
NC
1116 pos += pad;
1117 }
1118 }
1119 obj_bsssec (abfd)->filepos = pos;
1120
1121 /* Fix up the exec header. */
eea6121a
AM
1122 execp->a_text = obj_textsec (abfd)->size;
1123 execp->a_data = obj_datasec (abfd)->size;
1124 execp->a_bss = obj_bsssec (abfd)->size;
e135f41b
NC
1125 N_SET_MAGIC (*execp, OMAGIC);
1126}
1127
1128static void
1129adjust_z_magic (abfd, execp)
1130 bfd *abfd;
1131 struct internal_exec *execp;
1132{
1133 bfd_size_type data_pad, text_pad;
1134 file_ptr text_end;
dc810e39 1135 const struct aout_backend_data *abdp;
e135f41b 1136 int ztih; /* Nonzero if text includes exec header. */
dc810e39 1137
e135f41b
NC
1138 abdp = aout_backend_info (abfd);
1139
1140 /* Text. */
1141 ztih = (abdp != NULL
1142 && (abdp->text_includes_header
1143 || obj_aout_subformat (abfd) == q_magic_format));
1144 obj_textsec(abfd)->filepos = (ztih
1145 ? adata(abfd).exec_bytes_size
1146 : adata(abfd).zmagic_disk_block_size);
1147 if (! obj_textsec(abfd)->user_set_vma)
1148 {
1149 /* ?? Do we really need to check for relocs here? */
1150 obj_textsec(abfd)->vma = ((abfd->flags & HAS_RELOC)
1151 ? 0
1152 : (ztih
1153 ? (abdp->default_text_vma
1154 + adata (abfd).exec_bytes_size)
1155 : abdp->default_text_vma));
1156 text_pad = 0;
1157 }
1158 else
1159 {
1160 /* The .text section is being loaded at an unusual address. We
1161 may need to pad it such that the .data section starts at a page
1162 boundary. */
1163 if (ztih)
1164 text_pad = ((obj_textsec (abfd)->filepos - obj_textsec (abfd)->vma)
1165 & (adata (abfd).page_size - 1));
1166 else
1167 text_pad = ((- obj_textsec (abfd)->vma)
1168 & (adata (abfd).page_size - 1));
1169 }
1170
1171 /* Find start of data. */
1172 if (ztih)
1173 {
eea6121a 1174 text_end = obj_textsec (abfd)->filepos + obj_textsec (abfd)->size;
e135f41b
NC
1175 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
1176 }
1177 else
1178 {
1179 /* Note that if page_size == zmagic_disk_block_size, then
1180 filepos == page_size, and this case is the same as the ztih
1181 case. */
eea6121a 1182 text_end = obj_textsec (abfd)->size;
e135f41b
NC
1183 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
1184 text_end += obj_textsec (abfd)->filepos;
1185 }
1186
eea6121a 1187 obj_textsec (abfd)->size += text_pad;
e135f41b
NC
1188 text_end += text_pad;
1189
1190 /* Data. */
1191 if (!obj_datasec(abfd)->user_set_vma)
1192 {
1193 bfd_vma vma;
eea6121a 1194 vma = obj_textsec(abfd)->vma + obj_textsec(abfd)->size;
e135f41b
NC
1195 obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
1196 }
1197 if (abdp && abdp->zmagic_mapped_contiguous)
1198 {
1199 text_pad = (obj_datasec(abfd)->vma
1200 - obj_textsec(abfd)->vma
eea6121a
AM
1201 - obj_textsec(abfd)->size);
1202 obj_textsec(abfd)->size += text_pad;
e135f41b
NC
1203 }
1204 obj_datasec (abfd)->filepos = (obj_textsec (abfd)->filepos
eea6121a 1205 + obj_textsec (abfd)->size);
dc810e39 1206
e135f41b 1207 /* Fix up exec header while we're at it. */
eea6121a 1208 execp->a_text = obj_textsec(abfd)->size;
e135f41b
NC
1209 if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
1210 execp->a_text += adata(abfd).exec_bytes_size;
1211 if (obj_aout_subformat (abfd) == q_magic_format)
1212 N_SET_MAGIC (*execp, QMAGIC);
1213 else
1214 N_SET_MAGIC (*execp, ZMAGIC);
1215
1216 /* Spec says data section should be rounded up to page boundary. */
eea6121a
AM
1217 obj_datasec(abfd)->size
1218 = align_power (obj_datasec(abfd)->size,
e135f41b 1219 obj_bsssec(abfd)->alignment_power);
eea6121a 1220 execp->a_data = BFD_ALIGN (obj_datasec(abfd)->size,
e135f41b 1221 adata(abfd).page_size);
eea6121a 1222 data_pad = execp->a_data - obj_datasec(abfd)->size;
e135f41b
NC
1223
1224 /* BSS. */
1225 if (!obj_bsssec(abfd)->user_set_vma)
1226 obj_bsssec(abfd)->vma = (obj_datasec(abfd)->vma
eea6121a 1227 + obj_datasec(abfd)->size);
e135f41b
NC
1228 /* If the BSS immediately follows the data section and extra space
1229 in the page is left after the data section, fudge data
1230 in the header so that the bss section looks smaller by that
1231 amount. We'll start the bss section there, and lie to the OS.
1232 (Note that a linker script, as well as the above assignment,
1233 could have explicitly set the BSS vma to immediately follow
1234 the data section.) */
1235 if (align_power (obj_bsssec(abfd)->vma, obj_bsssec(abfd)->alignment_power)
eea6121a
AM
1236 == obj_datasec(abfd)->vma + obj_datasec(abfd)->size)
1237 execp->a_bss = (data_pad > obj_bsssec(abfd)->size) ? 0 :
1238 obj_bsssec(abfd)->size - data_pad;
e135f41b 1239 else
eea6121a 1240 execp->a_bss = obj_bsssec(abfd)->size;
e135f41b
NC
1241}
1242
1243static void
1244adjust_n_magic (abfd, execp)
1245 bfd *abfd;
1246 struct internal_exec *execp;
1247{
1248 file_ptr pos = adata(abfd).exec_bytes_size;
1249 bfd_vma vma = 0;
1250 int pad;
dc810e39 1251
e135f41b
NC
1252 /* Text. */
1253 obj_textsec(abfd)->filepos = pos;
1254 if (!obj_textsec(abfd)->user_set_vma)
1255 obj_textsec(abfd)->vma = vma;
1256 else
1257 vma = obj_textsec(abfd)->vma;
eea6121a
AM
1258 pos += obj_textsec(abfd)->size;
1259 vma += obj_textsec(abfd)->size;
e135f41b
NC
1260
1261 /* Data. */
1262 obj_datasec(abfd)->filepos = pos;
1263 if (!obj_datasec(abfd)->user_set_vma)
1264 obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
1265 vma = obj_datasec(abfd)->vma;
dc810e39 1266
e135f41b 1267 /* Since BSS follows data immediately, see if it needs alignment. */
eea6121a 1268 vma += obj_datasec(abfd)->size;
e135f41b 1269 pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma;
eea6121a
AM
1270 obj_datasec(abfd)->size += pad;
1271 pos += obj_datasec(abfd)->size;
e135f41b
NC
1272
1273 /* BSS. */
1274 if (!obj_bsssec(abfd)->user_set_vma)
1275 obj_bsssec(abfd)->vma = vma;
1276 else
1277 vma = obj_bsssec(abfd)->vma;
1278
1279 /* Fix up exec header. */
eea6121a
AM
1280 execp->a_text = obj_textsec(abfd)->size;
1281 execp->a_data = obj_datasec(abfd)->size;
1282 execp->a_bss = obj_bsssec(abfd)->size;
e135f41b
NC
1283 N_SET_MAGIC (*execp, NMAGIC);
1284}
1285
b34976b6 1286bfd_boolean
e135f41b
NC
1287NAME(aout,adjust_sizes_and_vmas) (abfd, text_size, text_end)
1288 bfd *abfd;
1289 bfd_size_type *text_size;
1290 file_ptr * text_end ATTRIBUTE_UNUSED;
1291{
1292 struct internal_exec *execp = exec_hdr (abfd);
1293
1294 if (! NAME(aout,make_sections) (abfd))
b34976b6 1295 return FALSE;
e135f41b
NC
1296
1297 if (adata(abfd).magic != undecided_magic)
b34976b6 1298 return TRUE;
e135f41b 1299
eea6121a
AM
1300 obj_textsec(abfd)->size =
1301 align_power(obj_textsec(abfd)->size,
e135f41b
NC
1302 obj_textsec(abfd)->alignment_power);
1303
eea6121a 1304 *text_size = obj_textsec (abfd)->size;
e135f41b
NC
1305 /* Rule (heuristic) for when to pad to a new page. Note that there
1306 are (at least) two ways demand-paged (ZMAGIC) files have been
1307 handled. Most Berkeley-based systems start the text segment at
1308 (TARGET_PAGE_SIZE). However, newer versions of SUNOS start the text
1309 segment right after the exec header; the latter is counted in the
1310 text segment size, and is paged in by the kernel with the rest of
1311 the text. */
1312
1313 /* This perhaps isn't the right way to do this, but made it simpler for me
1314 to understand enough to implement it. Better would probably be to go
1315 right from BFD flags to alignment/positioning characteristics. But the
1316 old code was sloppy enough about handling the flags, and had enough
1317 other magic, that it was a little hard for me to understand. I think
1318 I understand it better now, but I haven't time to do the cleanup this
1319 minute. */
1320
1321 if (abfd->flags & WP_TEXT)
1322 adata(abfd).magic = n_magic;
1323 else
1324 adata(abfd).magic = o_magic;
1325
1326#ifdef BFD_AOUT_DEBUG /* requires gcc2 */
1327#if __GNUC__ >= 2
1328 fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
1329 ({ char *str;
1330 switch (adata(abfd).magic) {
1331 case n_magic: str = "NMAGIC"; break;
1332 case o_magic: str = "OMAGIC"; break;
1333 case z_magic: str = "ZMAGIC"; break;
1334 default: abort ();
1335 }
1336 str;
1337 }),
eea6121a 1338 obj_textsec(abfd)->vma, obj_textsec(abfd)->size,
e135f41b 1339 obj_textsec(abfd)->alignment_power,
eea6121a 1340 obj_datasec(abfd)->vma, obj_datasec(abfd)->size,
e135f41b 1341 obj_datasec(abfd)->alignment_power,
eea6121a 1342 obj_bsssec(abfd)->vma, obj_bsssec(abfd)->size,
e135f41b
NC
1343 obj_bsssec(abfd)->alignment_power);
1344#endif
1345#endif
1346
1347 switch (adata(abfd).magic)
1348 {
1349 case o_magic:
1350 adjust_o_magic (abfd, execp);
1351 break;
1352 case z_magic:
1353 adjust_z_magic (abfd, execp);
1354 break;
1355 case n_magic:
1356 adjust_n_magic (abfd, execp);
1357 break;
1358 default:
1359 abort ();
1360 }
1361
1362#ifdef BFD_AOUT_DEBUG
1363 fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
eea6121a 1364 obj_textsec(abfd)->vma, obj_textsec(abfd)->size,
e135f41b 1365 obj_textsec(abfd)->filepos,
eea6121a 1366 obj_datasec(abfd)->vma, obj_datasec(abfd)->size,
e135f41b 1367 obj_datasec(abfd)->filepos,
eea6121a 1368 obj_bsssec(abfd)->vma, obj_bsssec(abfd)->size);
e135f41b
NC
1369#endif
1370
b34976b6 1371 return TRUE;
e135f41b
NC
1372}
1373
1374/*
1375FUNCTION
1376 aout_@var{size}_new_section_hook
1377
1378SYNOPSIS
b34976b6 1379 bfd_boolean aout_@var{size}_new_section_hook,
e135f41b
NC
1380 (bfd *abfd,
1381 asection *newsect));
1382
1383DESCRIPTION
1384 Called by the BFD in response to a @code{bfd_make_section}
1385 request.
1386*/
b34976b6 1387bfd_boolean
e135f41b
NC
1388NAME(aout,new_section_hook) (abfd, newsect)
1389 bfd *abfd;
1390 asection *newsect;
1391{
1392 /* align to double at least */
1393 newsect->alignment_power = bfd_get_arch_info(abfd)->section_align_power;
1394
1395
1396 if (bfd_get_format (abfd) == bfd_object)
1397 {
1398 if (obj_textsec (abfd) == NULL
1399 && ! strcmp (newsect->name, ".text"))
1400 {
1401 obj_textsec(abfd)= newsect;
1402 newsect->target_index = N_TEXT;
b34976b6 1403 return TRUE;
e135f41b
NC
1404 }
1405
1406 if (obj_datasec (abfd) == NULL
1407 && ! strcmp (newsect->name, ".data"))
1408 {
1409 obj_datasec (abfd) = newsect;
1410 newsect->target_index = N_DATA;
b34976b6 1411 return TRUE;
e135f41b
NC
1412 }
1413
1414 if (obj_bsssec (abfd) == NULL
1415 && !strcmp (newsect->name, ".bss"))
1416 {
1417 obj_bsssec (abfd) = newsect;
1418 newsect->target_index = N_BSS;
b34976b6 1419 return TRUE;
e135f41b
NC
1420 }
1421 }
1422
1423 /* We allow more than three sections internally */
b34976b6 1424 return TRUE;
e135f41b
NC
1425}
1426
b34976b6 1427bfd_boolean
e135f41b
NC
1428NAME(aout,set_section_contents) (abfd, section, location, offset, count)
1429 bfd *abfd;
1430 sec_ptr section;
0f867abe 1431 const PTR location;
e135f41b
NC
1432 file_ptr offset;
1433 bfd_size_type count;
1434{
1435 file_ptr text_end;
1436 bfd_size_type text_size;
1437
1438 if (! abfd->output_has_begun)
1439 {
1440 if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
b34976b6 1441 return FALSE;
e135f41b
NC
1442 }
1443
1444 if (section == obj_bsssec (abfd))
1445 {
1446 bfd_set_error (bfd_error_no_contents);
b34976b6 1447 return FALSE;
e135f41b
NC
1448 }
1449
1450 if (section != obj_textsec (abfd)
1451 && section != obj_datasec (abfd))
1452 {
1453 (*_bfd_error_handler)
1454 ("%s: can not represent section `%s' in a.out object file format",
1455 bfd_get_filename (abfd), bfd_get_section_name (abfd, section));
1456 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 1457 return FALSE;
e135f41b
NC
1458 }
1459
1460 if (count != 0)
1461 {
1462 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
dc810e39 1463 || bfd_bwrite (location, count, abfd) != count)
b34976b6 1464 return FALSE;
e135f41b
NC
1465 }
1466
b34976b6 1467 return TRUE;
e135f41b
NC
1468}
1469\f
1470/* Read the external symbols from an a.out file. */
1471
b34976b6 1472static bfd_boolean
e135f41b
NC
1473aout_get_external_symbols (abfd)
1474 bfd *abfd;
1475{
1476 if (obj_aout_external_syms (abfd) == (struct external_nlist *) NULL)
1477 {
1478 bfd_size_type count;
1479 struct external_nlist *syms;
1480
1481 count = exec_hdr (abfd)->a_syms / EXTERNAL_NLIST_SIZE;
1482
1483#ifdef USE_MMAP
82e51918
AM
1484 if (! bfd_get_file_window (abfd, obj_sym_filepos (abfd),
1485 exec_hdr (abfd)->a_syms,
b34976b6
AM
1486 &obj_aout_sym_window (abfd), TRUE))
1487 return FALSE;
e135f41b
NC
1488 syms = (struct external_nlist *) obj_aout_sym_window (abfd).data;
1489#else
1490 /* We allocate using malloc to make the values easy to free
1491 later on. If we put them on the objalloc it might not be
1492 possible to free them. */
dc810e39 1493 syms = (struct external_nlist *) bfd_malloc (count * EXTERNAL_NLIST_SIZE);
e135f41b 1494 if (syms == (struct external_nlist *) NULL && count != 0)
b34976b6 1495 return FALSE;
e135f41b
NC
1496
1497 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
dc810e39 1498 || (bfd_bread (syms, exec_hdr (abfd)->a_syms, abfd)
e135f41b
NC
1499 != exec_hdr (abfd)->a_syms))
1500 {
1501 free (syms);
b34976b6 1502 return FALSE;
e135f41b
NC
1503 }
1504#endif
1505
1506 obj_aout_external_syms (abfd) = syms;
1507 obj_aout_external_sym_count (abfd) = count;
1508 }
dc810e39 1509
e135f41b
NC
1510 if (obj_aout_external_strings (abfd) == NULL
1511 && exec_hdr (abfd)->a_syms != 0)
1512 {
1513 unsigned char string_chars[BYTES_IN_LONG];
1514 bfd_size_type stringsize;
1515 char *strings;
1516
1517 /* Get the size of the strings. */
1518 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
dc810e39
AM
1519 || (bfd_bread ((PTR) string_chars, (bfd_size_type) BYTES_IN_LONG,
1520 abfd) != BYTES_IN_LONG))
b34976b6 1521 return FALSE;
dc810e39 1522 stringsize = H_GET_32 (abfd, string_chars);
e135f41b
NC
1523
1524#ifdef USE_MMAP
82e51918 1525 if (! bfd_get_file_window (abfd, obj_str_filepos (abfd), stringsize,
b34976b6
AM
1526 &obj_aout_string_window (abfd), TRUE))
1527 return FALSE;
e135f41b
NC
1528 strings = (char *) obj_aout_string_window (abfd).data;
1529#else
dc810e39 1530 strings = (char *) bfd_malloc (stringsize + 1);
e135f41b 1531 if (strings == NULL)
b34976b6 1532 return FALSE;
e135f41b
NC
1533
1534 /* Skip space for the string count in the buffer for convenience
1535 when using indexes. */
dc810e39 1536 if (bfd_bread (strings + 4, stringsize - 4, abfd) != stringsize - 4)
e135f41b
NC
1537 {
1538 free (strings);
b34976b6 1539 return FALSE;
e135f41b
NC
1540 }
1541#endif
1542
1543 /* Ensure that a zero index yields an empty string. */
1544 strings[0] = '\0';
1545
1546 strings[stringsize - 1] = 0;
1547
1548 obj_aout_external_strings (abfd) = strings;
1549 obj_aout_external_string_size (abfd) = stringsize;
1550 }
1551
b34976b6 1552 return TRUE;
e135f41b
NC
1553}
1554
1555/* Translate an a.out symbol into a BFD symbol. The desc, other, type
1556 and symbol->value fields of CACHE_PTR will be set from the a.out
1557 nlist structure. This function is responsible for setting
1558 symbol->flags and symbol->section, and adjusting symbol->value. */
1559
b34976b6 1560static bfd_boolean
e135f41b
NC
1561translate_from_native_sym_flags (abfd, cache_ptr)
1562 bfd *abfd;
1563 aout_symbol_type *cache_ptr;
1564{
1565 flagword visible;
1566
1567 if (cache_ptr->type == N_FN)
1568 {
1569 asection *sec;
1570
1571 /* This is a debugging symbol. */
1572
1573 cache_ptr->symbol.flags = BSF_DEBUGGING;
1574
1575 /* Work out the symbol section. */
1576 switch (cache_ptr->type & N_TYPE)
1577 {
1578 case N_TEXT:
1579 case N_FN:
1580 sec = obj_textsec (abfd);
1581 break;
1582 case N_DATA:
1583 sec = obj_datasec (abfd);
1584 break;
1585 case N_BSS:
1586 sec = obj_bsssec (abfd);
1587 break;
1588 default:
1589 case N_ABS:
1590 sec = bfd_abs_section_ptr;
1591 break;
1592 }
1593
1594 cache_ptr->symbol.section = sec;
1595 cache_ptr->symbol.value -= sec->vma;
1596
b34976b6 1597 return TRUE;
e135f41b
NC
1598 }
1599
1600 /* Get the default visibility. This does not apply to all types, so
1601 we just hold it in a local variable to use if wanted. */
1602 if ((cache_ptr->type & N_EXT) == 0)
1603 visible = BSF_LOCAL;
1604 else
1605 visible = BSF_GLOBAL;
1606
1607 switch (cache_ptr->type)
1608 {
1609 default:
1610 case N_ABS: case N_ABS | N_EXT:
1611 cache_ptr->symbol.section = bfd_abs_section_ptr;
1612 cache_ptr->symbol.flags = visible;
1613 break;
1614
1615 case N_UNDF | N_EXT:
1616 if (cache_ptr->symbol.value != 0)
1617 {
1618 /* This is a common symbol. */
1619 cache_ptr->symbol.flags = BSF_GLOBAL;
1620 cache_ptr->symbol.section = bfd_com_section_ptr;
1621 }
1622 else
1623 {
1624 cache_ptr->symbol.flags = 0;
1625 cache_ptr->symbol.section = bfd_und_section_ptr;
1626 }
1627 break;
1628
1629 case N_TEXT: case N_TEXT | N_EXT:
1630 cache_ptr->symbol.section = obj_textsec (abfd);
1631 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1632 cache_ptr->symbol.flags = visible;
1633 break;
1634
1635 case N_DATA: case N_DATA | N_EXT:
1636 cache_ptr->symbol.section = obj_datasec (abfd);
1637 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1638 cache_ptr->symbol.flags = visible;
1639 break;
1640
1641 case N_BSS: case N_BSS | N_EXT:
1642 cache_ptr->symbol.section = obj_bsssec (abfd);
1643 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1644 cache_ptr->symbol.flags = visible;
1645 break;
1646 }
1647
b34976b6 1648 return TRUE;
e135f41b
NC
1649}
1650
1651/* Set the fields of SYM_POINTER according to CACHE_PTR. */
1652
b34976b6 1653static bfd_boolean
e135f41b
NC
1654translate_to_native_sym_flags (abfd, cache_ptr, sym_pointer)
1655 bfd *abfd;
1656 asymbol *cache_ptr;
1657 struct external_nlist *sym_pointer;
1658{
1659 bfd_vma value = cache_ptr->value;
1660 asection *sec;
1661 bfd_vma off;
1662
1663 /* Mask out any existing type bits in case copying from one section
1664 to another. */
1665 sym_pointer->e_type[0] &= ~N_TYPE;
1666
1667 sec = bfd_get_section (cache_ptr);
1668 off = 0;
1669
1670 if (sec == NULL)
1671 {
1672 /* This case occurs, e.g., for the *DEBUG* section of a COFF
1673 file. */
1674 (*_bfd_error_handler)
d003868e
AM
1675 ("%B: can not represent section for symbol `%s' in a.out object file format",
1676 abfd, cache_ptr->name != NULL ? cache_ptr->name : "*unknown*");
e135f41b 1677 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 1678 return FALSE;
e135f41b
NC
1679 }
1680
1681 if (sec->output_section != NULL)
1682 {
1683 off = sec->output_offset;
1684 sec = sec->output_section;
1685 }
1686
1687 if (bfd_is_abs_section (sec))
1688 sym_pointer->e_type[0] |= N_ABS;
1689 else if (sec == obj_textsec (abfd))
1690 sym_pointer->e_type[0] |= N_TEXT;
1691 else if (sec == obj_datasec (abfd))
1692 sym_pointer->e_type[0] |= N_DATA;
1693 else if (sec == obj_bsssec (abfd))
1694 sym_pointer->e_type[0] |= N_BSS;
1695 else if (bfd_is_und_section (sec))
1696 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1697 else if (bfd_is_com_section (sec))
1698 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1699 else
1700 {
1701 (*_bfd_error_handler)
d003868e
AM
1702 ("%B: can not represent section `%A' in a.out object file format",
1703 abfd, sec);
e135f41b 1704 bfd_set_error (bfd_error_nonrepresentable_section);
b34976b6 1705 return FALSE;
e135f41b
NC
1706 }
1707
1708 /* Turn the symbol from section relative to absolute again */
1709 value += sec->vma + off;
1710
1711 if ((cache_ptr->flags & BSF_DEBUGGING) != 0)
1712 sym_pointer->e_type[0] = ((aout_symbol_type *) cache_ptr)->type;
1713 else if ((cache_ptr->flags & BSF_GLOBAL) != 0)
1714 sym_pointer->e_type[0] |= N_EXT;
1715
1716#if 0
1717 if ((cache_ptr->flags & BSF_CONSTRUCTOR) != 0)
1718 {
1719 int type = ((aout_symbol_type *) cache_ptr)->type;
1720
1721
1722 switch (type)
1723 {
1724 case N_ABS: type = N_SETA; break;
1725 case N_TEXT: type = N_SETT; break;
1726 case N_DATA: type = N_SETD; break;
1727 case N_BSS: type = N_SETB; break;
1728 }
1729 sym_pointer->e_type[0] = type;
1730 }
1731#endif
1732
1733#if 0
1734 if ((cache_ptr->flags & BSF_WEAK) != 0)
1735 {
1736 int type;
1737
1738 switch (sym_pointer->e_type[0] & N_TYPE)
1739 {
1740 default:
1741 case N_ABS: type = N_WEAKA; break;
1742 case N_TEXT: type = N_WEAKT; break;
1743 case N_DATA: type = N_WEAKD; break;
1744 case N_BSS: type = N_WEAKB; break;
1745 case N_UNDF: type = N_WEAKU; break;
1746 }
1747 sym_pointer->e_type[0] = type;
1748 }
1749#endif
1750
1751 PUT_WORD(abfd, value, sym_pointer->e_value);
1752
b34976b6 1753 return TRUE;
e135f41b
NC
1754}
1755\f
1756/* Native-level interface to symbols. */
1757
1758asymbol *
1759NAME(aout,make_empty_symbol) (abfd)
1760 bfd *abfd;
1761{
dc810e39
AM
1762 bfd_size_type amt = sizeof (aout_symbol_type);
1763 aout_symbol_type *new = (aout_symbol_type *) bfd_zalloc (abfd, amt);
e135f41b
NC
1764 if (!new)
1765 return NULL;
1766 new->symbol.the_bfd = abfd;
1767
1768 return &new->symbol;
1769}
1770
1771/* Translate a set of internal symbols into external symbols. */
1772
b34976b6 1773bfd_boolean
e135f41b
NC
1774NAME(aout,translate_symbol_table) (abfd, in, ext, count, str, strsize, dynamic)
1775 bfd *abfd;
1776 aout_symbol_type *in;
1777 struct external_nlist *ext;
1778 bfd_size_type count;
1779 char *str;
1780 bfd_size_type strsize;
b34976b6 1781 bfd_boolean dynamic;
e135f41b
NC
1782{
1783 struct external_nlist *ext_end;
1784
1785 ext_end = ext + count;
1786 for (; ext < ext_end; ext++, in++)
1787 {
1788 bfd_vma x;
1789
1790 x = GET_WORD (abfd, ext->e_strx);
1791 in->symbol.the_bfd = abfd;
1792
1793 /* For the normal symbols, the zero index points at the number
1794 of bytes in the string table but is to be interpreted as the
1795 null string. For the dynamic symbols, the number of bytes in
1796 the string table is stored in the __DYNAMIC structure and the
1797 zero index points at an actual string. */
1798 if (x == 0 && ! dynamic)
1799 in->symbol.name = "";
1800 else if (x < strsize)
1801 in->symbol.name = str + x;
1802 else
b34976b6 1803 return FALSE;
e135f41b
NC
1804
1805 in->symbol.value = GET_SWORD (abfd, ext->e_value);
1806 /* TODO: is 0 a safe value here? */
1807 in->desc = 0;
1808 in->other = 0;
dc810e39 1809 in->type = H_GET_8 (abfd, ext->e_type);
e135f41b
NC
1810 in->symbol.udata.p = NULL;
1811
1812 if (! translate_from_native_sym_flags (abfd, in))
b34976b6 1813 return FALSE;
e135f41b
NC
1814
1815 if (dynamic)
1816 in->symbol.flags |= BSF_DYNAMIC;
1817 }
1818
b34976b6 1819 return TRUE;
e135f41b
NC
1820}
1821
1822/* We read the symbols into a buffer, which is discarded when this
1823 function exits. We read the strings into a buffer large enough to
1824 hold them all plus all the cached symbol entries. */
1825
b34976b6 1826bfd_boolean
e135f41b
NC
1827NAME(aout,slurp_symbol_table) (abfd)
1828 bfd *abfd;
1829{
1830 struct external_nlist *old_external_syms;
1831 aout_symbol_type *cached;
dc810e39 1832 bfd_size_type cached_size;
e135f41b
NC
1833
1834 /* If there's no work to be done, don't do any */
1835 if (obj_aout_symbols (abfd) != (aout_symbol_type *) NULL)
b34976b6 1836 return TRUE;
e135f41b
NC
1837
1838 old_external_syms = obj_aout_external_syms (abfd);
1839
1840 if (! aout_get_external_symbols (abfd))
b34976b6 1841 return FALSE;
e135f41b 1842
dc810e39
AM
1843 cached_size = obj_aout_external_sym_count (abfd);
1844 cached_size *= sizeof (aout_symbol_type);
9bab7074 1845 cached = (aout_symbol_type *) bfd_zmalloc (cached_size);
e135f41b 1846 if (cached == NULL && cached_size != 0)
b34976b6 1847 return FALSE;
e135f41b
NC
1848
1849 /* Convert from external symbol information to internal. */
1850 if (! (NAME(aout,translate_symbol_table)
1851 (abfd, cached,
1852 obj_aout_external_syms (abfd),
1853 obj_aout_external_sym_count (abfd),
1854 obj_aout_external_strings (abfd),
1855 obj_aout_external_string_size (abfd),
b34976b6 1856 FALSE)))
e135f41b
NC
1857 {
1858 free (cached);
b34976b6 1859 return FALSE;
e135f41b
NC
1860 }
1861
1862 bfd_get_symcount (abfd) = obj_aout_external_sym_count (abfd);
1863
1864 obj_aout_symbols (abfd) = cached;
1865
1866 /* It is very likely that anybody who calls this function will not
1867 want the external symbol information, so if it was allocated
1868 because of our call to aout_get_external_symbols, we free it up
1869 right away to save space. */
1870 if (old_external_syms == (struct external_nlist *) NULL
1871 && obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
1872 {
1873#ifdef USE_MMAP
1874 bfd_free_window (&obj_aout_sym_window (abfd));
1875#else
1876 free (obj_aout_external_syms (abfd));
1877#endif
1878 obj_aout_external_syms (abfd) = NULL;
1879 }
1880
b34976b6 1881 return TRUE;
e135f41b
NC
1882}
1883\f
1884/* We use a hash table when writing out symbols so that we only write
1885 out a particular string once. This helps particularly when the
1886 linker writes out stabs debugging entries, because each different
1887 contributing object file tends to have many duplicate stabs
1888 strings.
1889
1890 This hash table code breaks dbx on SunOS 4.1.3, so we don't do it
1891 if BFD_TRADITIONAL_FORMAT is set. */
1892
1893static bfd_size_type add_to_stringtab
b34976b6
AM
1894 PARAMS ((bfd *, struct bfd_strtab_hash *, const char *, bfd_boolean));
1895static bfd_boolean emit_stringtab PARAMS ((bfd *, struct bfd_strtab_hash *));
e135f41b
NC
1896
1897/* Get the index of a string in a strtab, adding it if it is not
1898 already present. */
1899
1900static INLINE bfd_size_type
1901add_to_stringtab (abfd, tab, str, copy)
1902 bfd *abfd;
1903 struct bfd_strtab_hash *tab;
1904 const char *str;
b34976b6 1905 bfd_boolean copy;
e135f41b 1906{
b34976b6 1907 bfd_boolean hash;
e135f41b
NC
1908 bfd_size_type index;
1909
1910 /* An index of 0 always means the empty string. */
1911 if (str == 0 || *str == '\0')
1912 return 0;
1913
1914 /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
1915 doesn't understand a hashed string table. */
b34976b6 1916 hash = TRUE;
e135f41b 1917 if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
b34976b6 1918 hash = FALSE;
e135f41b
NC
1919
1920 index = _bfd_stringtab_add (tab, str, hash, copy);
1921
1922 if (index != (bfd_size_type) -1)
1923 {
1924 /* Add BYTES_IN_LONG to the return value to account for the
1925 space taken up by the string table size. */
1926 index += BYTES_IN_LONG;
1927 }
1928
1929 return index;
1930}
1931
1932/* Write out a strtab. ABFD is already at the right location in the
1933 file. */
1934
b34976b6 1935static bfd_boolean
e135f41b
NC
1936emit_stringtab (abfd, tab)
1937 register bfd *abfd;
1938 struct bfd_strtab_hash *tab;
1939{
1940 bfd_byte buffer[BYTES_IN_LONG];
1941
1942 /* The string table starts with the size. */
dc810e39
AM
1943 H_PUT_32 (abfd, _bfd_stringtab_size (tab) + BYTES_IN_LONG, buffer);
1944 if (bfd_bwrite ((PTR) buffer, (bfd_size_type) BYTES_IN_LONG, abfd)
1945 != BYTES_IN_LONG)
b34976b6 1946 return FALSE;
e135f41b
NC
1947
1948 return _bfd_stringtab_emit (abfd, tab);
1949}
1950\f
b34976b6 1951bfd_boolean
e135f41b
NC
1952NAME(aout,write_syms) (abfd)
1953 bfd *abfd;
1954{
1955 unsigned int count ;
1956 asymbol **generic = bfd_get_outsymbols (abfd);
1957 struct bfd_strtab_hash *strtab;
1958
1959 strtab = _bfd_stringtab_init ();
1960 if (strtab == NULL)
b34976b6 1961 return FALSE;
e135f41b
NC
1962
1963 for (count = 0; count < bfd_get_symcount (abfd); count++)
1964 {
1965 asymbol *g = generic[count];
1966 bfd_size_type indx;
1967 struct external_nlist nsp;
1968
dc810e39 1969 PUT_WORD (abfd, 0, nsp.e_unused);
e135f41b 1970
b34976b6 1971 indx = add_to_stringtab (abfd, strtab, g->name, FALSE);
e135f41b
NC
1972 if (indx == (bfd_size_type) -1)
1973 goto error_return;
dc810e39 1974 PUT_WORD (abfd, indx, nsp.e_strx);
e135f41b
NC
1975
1976 if (bfd_asymbol_flavour(g) == abfd->xvec->flavour)
dc810e39 1977 H_PUT_8 (abfd, aout_symbol(g)->type, nsp.e_type);
e135f41b 1978 else
dc810e39 1979 H_PUT_8 (abfd, 0, nsp.e_type);
e135f41b
NC
1980
1981 if (! translate_to_native_sym_flags (abfd, g, &nsp))
1982 goto error_return;
1983
dc810e39 1984 H_PUT_8 (abfd, 0, nsp.e_ovly);
e135f41b 1985
dc810e39 1986 if (bfd_bwrite ((PTR)&nsp, (bfd_size_type) EXTERNAL_NLIST_SIZE, abfd)
e135f41b
NC
1987 != EXTERNAL_NLIST_SIZE)
1988 goto error_return;
1989
1990 /* NB: `KEEPIT' currently overlays `udata.p', so set this only
1991 here, at the end. */
1992 g->KEEPIT = count;
1993 }
1994
1995 if (! emit_stringtab (abfd, strtab))
1996 goto error_return;
1997
1998 _bfd_stringtab_free (strtab);
1999
b34976b6 2000 return TRUE;
e135f41b
NC
2001
2002error_return:
2003 _bfd_stringtab_free (strtab);
b34976b6 2004 return FALSE;
e135f41b
NC
2005}
2006
2007\f
2008long
6cee3f79 2009NAME(aout,canonicalize_symtab) (abfd, location)
e135f41b
NC
2010 bfd *abfd;
2011 asymbol **location;
2012{
2013 unsigned int counter = 0;
2014 aout_symbol_type *symbase;
2015
2016 if (!NAME(aout,slurp_symbol_table)(abfd))
2017 return -1;
2018
2019 for (symbase = obj_aout_symbols(abfd); counter++ < bfd_get_symcount (abfd);)
2020 *(location++) = (asymbol *)( symbase++);
2021 *location++ =0;
2022 return bfd_get_symcount (abfd);
2023}
2024
2025\f
2026/* Standard reloc stuff */
2027
2028/* Extended stuff */
2029/* Output extended relocation information to a file in target byte order. */
2030
2031void
2032pdp11_aout_swap_reloc_out (abfd, g, natptr)
2033 bfd *abfd;
2034 arelent *g;
2035 register struct pdp11_aout_reloc_external *natptr;
2036{
2037 int r_index;
2038 int r_pcrel;
2039 int reloc_entry;
2040 int r_type;
2041 asymbol *sym = *(g->sym_ptr_ptr);
2042 asection *output_section = sym->section->output_section;
2043
2044 if (g->addend != 0)
2045 fprintf (stderr, "BFD: can't do this reloc addend stuff\n");
2046
2047 r_pcrel = g->howto->pc_relative;
2048
2049 if (bfd_is_abs_section (output_section))
2050 r_type = RABS;
2051 else if (output_section == obj_textsec (abfd))
2052 r_type = RTEXT;
2053 else if (output_section == obj_datasec (abfd))
2054 r_type = RDATA;
2055 else if (output_section == obj_bsssec (abfd))
2056 r_type = RBSS;
2057 else if (bfd_is_und_section (output_section))
2058 r_type = REXT;
2059 else if (bfd_is_com_section (output_section))
2060 r_type = REXT;
2061 else
2062 r_type = -1;
2063
2064 BFD_ASSERT (r_type != -1);
2065
2066 if (r_type == RABS)
2067 r_index = 0;
2068 else
2069 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
2070
2071#if 0
2072 if (bfd_is_abs_section (bfd_get_section (sym)))
2073 {
2074 r_extern = 0;
2075 r_index = N_ABS;
2076 r_type = RABS;
2077 }
2078 else if ((sym->flags & BSF_SECTION_SYM) == 0)
2079 {
2080 if (bfd_is_und_section (bfd_get_section (sym))
2081 || (sym->flags & BSF_GLOBAL) != 0)
2082 r_extern = 1;
2083 else
2084 r_extern = 0;
2085 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
2086 }
2087 else
2088 {
2089 /* Just an ordinary section */
2090 r_extern = 0;
2091 r_index = output_section->target_index;
2092 }
2093#endif
2094
2095 reloc_entry = r_index << 4 | r_type | r_pcrel;
2096
2097 PUT_WORD (abfd, reloc_entry, natptr->e_reloc_entry);
2098}
2099
2100/* BFD deals internally with all things based from the section they're
2101 in. so, something in 10 bytes into a text section with a base of
2102 50 would have a symbol (.text+10) and know .text vma was 50.
2103
2104 Aout keeps all it's symbols based from zero, so the symbol would
2105 contain 60. This macro subs the base of each section from the value
2106 to give the true offset from the section */
2107
2108
2109#define MOVE_ADDRESS(ad) \
2110 if (r_extern) \
2111 { \
2112 /* Undefined symbol. */ \
2113 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2114 cache_ptr->addend = ad; \
2115 } \
2116 else \
2117 { \
2118 /* Defined, section relative. replace symbol with pointer to \
2119 symbol which points to section. */ \
2120 switch (r_index) \
2121 { \
2122 case N_TEXT: \
2123 case N_TEXT | N_EXT: \
2124 cache_ptr->sym_ptr_ptr = obj_textsec (abfd)->symbol_ptr_ptr; \
2125 cache_ptr->addend = ad - su->textsec->vma; \
2126 break; \
2127 case N_DATA: \
2128 case N_DATA | N_EXT: \
2129 cache_ptr->sym_ptr_ptr = obj_datasec (abfd)->symbol_ptr_ptr; \
2130 cache_ptr->addend = ad - su->datasec->vma; \
2131 break; \
2132 case N_BSS: \
2133 case N_BSS | N_EXT: \
2134 cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr; \
2135 cache_ptr->addend = ad - su->bsssec->vma; \
2136 break; \
2137 default: \
2138 case N_ABS: \
2139 case N_ABS | N_EXT: \
2140 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
2141 cache_ptr->addend = ad; \
2142 break; \
2143 } \
dc810e39 2144 }
e135f41b
NC
2145
2146void
2147pdp11_aout_swap_reloc_in (abfd, bytes, cache_ptr, offset,
2148 symbols, symcount)
2149 bfd *abfd;
2150 struct pdp11_aout_reloc_external *bytes;
2151 arelent *cache_ptr;
2152 bfd_size_type offset;
2153 asymbol **symbols;
2154 bfd_size_type symcount;
2155{
2156 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2157 unsigned int r_index;
2158 int reloc_entry;
2159 int r_extern;
2160 int r_pcrel;
2161
2162 reloc_entry = GET_WORD (abfd, (PTR)bytes);
2163
2164 r_pcrel = reloc_entry & RELFLG;
2165
2166 cache_ptr->address = offset;
2167 cache_ptr->howto = howto_table_pdp11 + (r_pcrel ? 1 : 0);
2168
2169 if ((reloc_entry & RTYPE) == RABS)
2170 r_index = N_ABS;
2171 else
2172 r_index = RINDEX (reloc_entry);
2173
2174 /* r_extern reflects whether the symbol the reloc is against is
2175 local or global. */
2176 r_extern = (reloc_entry & RTYPE) == REXT;
2177
2178 if (r_extern && r_index > symcount)
2179 {
2180 /* We could arrange to return an error, but it might be useful
2181 to see the file even if it is bad. */
2182 r_extern = 0;
2183 r_index = N_ABS;
2184 }
2185
2186 MOVE_ADDRESS(0);
2187}
2188
2189/* Read and swap the relocs for a section. */
2190
b34976b6 2191bfd_boolean
e135f41b
NC
2192NAME(aout,slurp_reloc_table) (abfd, asect, symbols)
2193 bfd *abfd;
2194 sec_ptr asect;
2195 asymbol **symbols;
2196{
2197 struct pdp11_aout_reloc_external *rptr;
dc810e39 2198 bfd_size_type count;
e135f41b
NC
2199 bfd_size_type reloc_size;
2200 PTR relocs;
2201 arelent *reloc_cache;
2202 size_t each_size;
2203 unsigned int counter = 0;
2204 arelent *cache_ptr;
2205
2206 if (asect->relocation)
b34976b6 2207 return TRUE;
e135f41b
NC
2208
2209 if (asect->flags & SEC_CONSTRUCTOR)
b34976b6 2210 return TRUE;
e135f41b
NC
2211
2212 if (asect == obj_datasec (abfd))
2213 reloc_size = exec_hdr(abfd)->a_drsize;
2214 else if (asect == obj_textsec (abfd))
2215 reloc_size = exec_hdr(abfd)->a_trsize;
2216 else if (asect == obj_bsssec (abfd))
2217 reloc_size = 0;
2218 else
2219 {
2220 bfd_set_error (bfd_error_invalid_operation);
b34976b6 2221 return FALSE;
e135f41b
NC
2222 }
2223
2224 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
b34976b6 2225 return FALSE;
e135f41b
NC
2226
2227 each_size = obj_reloc_entry_size (abfd);
2228
dc810e39 2229 relocs = bfd_malloc (reloc_size);
e135f41b 2230 if (relocs == NULL && reloc_size != 0)
b34976b6 2231 return FALSE;
e135f41b 2232
dc810e39 2233 if (bfd_bread (relocs, reloc_size, abfd) != reloc_size)
e135f41b
NC
2234 {
2235 free (relocs);
b34976b6 2236 return FALSE;
e135f41b
NC
2237 }
2238
2239 count = reloc_size / each_size;
2240
2241 /* Count the number of NON-ZERO relocs, this is the count we want. */
2242 {
2243 unsigned int real_count = 0;
2244
2245 for (counter = 0; counter < count; counter++)
2246 {
2247 int x;
2248
973ffd63 2249 x = GET_WORD (abfd, (char *) relocs + each_size * counter);
e135f41b
NC
2250 if (x != 0)
2251 real_count++;
2252 }
2253
2254 count = real_count;
2255 }
2256
9bab7074 2257 reloc_cache = (arelent *) bfd_zmalloc (count * sizeof (arelent));
e135f41b 2258 if (reloc_cache == NULL && count != 0)
b34976b6 2259 return FALSE;
e135f41b
NC
2260
2261 cache_ptr = reloc_cache;
2262
2263 rptr = (struct pdp11_aout_reloc_external *) relocs;
2264 for (counter = 0;
2265 counter < count;
2266 counter++, ((char *)rptr) += RELOC_SIZE, cache_ptr++)
2267 {
2268 while (GET_WORD (abfd, (PTR)rptr) == 0)
2269 {
2270 rptr =
2271 (struct pdp11_aout_reloc_external *)
dc810e39
AM
2272 ((char *) rptr + RELOC_SIZE);
2273 if ((char *) rptr >= (char *) relocs + reloc_size)
e135f41b
NC
2274 goto done;
2275 }
2276
2277 pdp11_aout_swap_reloc_in (abfd, rptr, cache_ptr,
dc810e39
AM
2278 (bfd_size_type) ((char *) rptr - (char *) relocs),
2279 symbols,
2280 (bfd_size_type) bfd_get_symcount (abfd));
e135f41b
NC
2281 }
2282 done:
2283 /* Just in case, if rptr >= relocs + reloc_size should happen
2284 too early. */
2285 BFD_ASSERT (counter == count);
2286
2287 free (relocs);
2288
2289 asect->relocation = reloc_cache;
2290 asect->reloc_count = cache_ptr - reloc_cache;
2291
b34976b6 2292 return TRUE;
e135f41b
NC
2293}
2294
2295/* Write out a relocation section into an object file. */
2296
b34976b6 2297bfd_boolean
e135f41b
NC
2298NAME(aout,squirt_out_relocs) (abfd, section)
2299 bfd *abfd;
2300 asection *section;
2301{
2302 arelent **generic;
2303 unsigned char *native;
2304 unsigned int count = section->reloc_count;
dc810e39 2305 bfd_size_type natsize;
e135f41b
NC
2306
2307#if 0
2308 /* If we're writing an .o file, we must write
2309 relocation information, even if there is none. */
2310 if ((count == 0 || section->orelocation == NULL) &&
2311 <writing_executable>)
b34976b6 2312 return TRUE;
e135f41b
NC
2313#endif
2314
eea6121a 2315 natsize = section->size;
e135f41b
NC
2316 native = (unsigned char *) bfd_zalloc (abfd, natsize);
2317 if (!native)
b34976b6 2318 return FALSE;
e135f41b 2319
e135f41b
NC
2320 generic = section->orelocation;
2321 if (generic != NULL)
2322 {
2323 while (count > 0)
2324 {
2325 struct pdp11_aout_reloc_external *r;
2326
2327 r = (struct pdp11_aout_reloc_external *)
2328 (native + (*generic)->address);
2329 pdp11_aout_swap_reloc_out (abfd, *generic, r);
2330 count--;
2331 generic++;
2332 }
2333 }
2334
dc810e39 2335 if (bfd_bwrite ((PTR) native, natsize, abfd) != natsize)
e135f41b 2336 {
dc810e39 2337 bfd_release (abfd, native);
b34976b6 2338 return FALSE;
e135f41b
NC
2339 }
2340
2341 bfd_release (abfd, native);
b34976b6 2342 return TRUE;
e135f41b
NC
2343}
2344
2345/* This is stupid. This function should be a boolean predicate */
2346long
2347NAME(aout,canonicalize_reloc) (abfd, section, relptr, symbols)
2348 bfd *abfd;
2349 sec_ptr section;
2350 arelent **relptr;
2351 asymbol **symbols;
2352{
2353 arelent *tblptr = section->relocation;
2354 unsigned int count;
2355
2356 if (section == obj_bsssec (abfd))
2357 {
2358 *relptr = NULL;
2359 return 0;
2360 }
2361
2362 if (!(tblptr || NAME(aout,slurp_reloc_table)(abfd, section, symbols)))
2363 return -1;
2364
2365 if (section->flags & SEC_CONSTRUCTOR)
2366 {
2367 arelent_chain *chain = section->constructor_chain;
2368
2369 for (count = 0; count < section->reloc_count; count ++)
2370 {
2371 *relptr ++ = &chain->relent;
2372 chain = chain->next;
2373 }
2374 }
2375 else
2376 {
2377 tblptr = section->relocation;
2378
2379 for (count = 0; count++ < section->reloc_count;)
2380 *relptr++ = tblptr++;
2381 }
2382
2383 *relptr = 0;
2384
2385 return section->reloc_count;
2386}
2387
2388long
2389NAME(aout,get_reloc_upper_bound) (abfd, asect)
2390 bfd *abfd;
2391 sec_ptr asect;
2392{
2393 if (bfd_get_format (abfd) != bfd_object)
2394 {
2395 bfd_set_error (bfd_error_invalid_operation);
2396 return -1;
2397 }
2398
dc810e39 2399 if (asect->flags & SEC_CONSTRUCTOR)
e135f41b
NC
2400 return (sizeof (arelent *) * (asect->reloc_count+1));
2401
2402 if (asect == obj_datasec (abfd))
2403 return (sizeof (arelent *)
2404 * ((exec_hdr(abfd)->a_drsize / obj_reloc_entry_size (abfd))
2405 + 1));
2406
2407 if (asect == obj_textsec (abfd))
2408 return (sizeof (arelent *)
2409 * ((exec_hdr(abfd)->a_trsize / obj_reloc_entry_size (abfd))
2410 + 1));
2411
2412 /* TODO: why are there two if statements for obj_bsssec()? */
2413
2414 if (asect == obj_bsssec (abfd))
2415 return sizeof (arelent *);
2416
2417 if (asect == obj_bsssec (abfd))
2418 return 0;
2419
2420 bfd_set_error (bfd_error_invalid_operation);
2421 return -1;
2422}
2423
2424\f
2425long
2426NAME(aout,get_symtab_upper_bound) (abfd)
2427 bfd *abfd;
2428{
2429 if (!NAME(aout,slurp_symbol_table)(abfd))
2430 return -1;
2431
2432 return (bfd_get_symcount (abfd) + 1) * (sizeof (aout_symbol_type *));
2433}
2434
2435alent *
2436NAME(aout,get_lineno) (abfd, symbol)
2437 bfd * abfd ATTRIBUTE_UNUSED;
2438 asymbol * symbol ATTRIBUTE_UNUSED;
2439{
2440 return (alent *)NULL;
2441}
2442
2443void
2444NAME(aout,get_symbol_info) (abfd, symbol, ret)
2445 bfd * abfd ATTRIBUTE_UNUSED;
2446 asymbol *symbol;
2447 symbol_info *ret;
2448{
2449 bfd_symbol_info (symbol, ret);
2450
2451 if (ret->type == '?')
2452 {
2453 int type_code = aout_symbol(symbol)->type & 0xff;
2454 const char *stab_name = bfd_get_stab_name (type_code);
2455 static char buf[10];
2456
2457 if (stab_name == NULL)
2458 {
2459 sprintf(buf, "(%d)", type_code);
2460 stab_name = buf;
2461 }
2462 ret->type = '-';
2463 ret->stab_type = type_code;
2464 ret->stab_other = (unsigned)(aout_symbol(symbol)->other & 0xff);
2465 ret->stab_desc = (unsigned)(aout_symbol(symbol)->desc & 0xffff);
2466 ret->stab_name = stab_name;
2467 }
2468}
2469
e135f41b
NC
2470void
2471NAME(aout,print_symbol) (abfd, afile, symbol, how)
60b89a18 2472 bfd * abfd;
e135f41b
NC
2473 PTR afile;
2474 asymbol *symbol;
2475 bfd_print_symbol_type how;
2476{
2477 FILE *file = (FILE *)afile;
2478
2479 switch (how)
2480 {
2481 case bfd_print_symbol_name:
2482 if (symbol->name)
2483 fprintf(file,"%s", symbol->name);
2484 break;
2485 case bfd_print_symbol_more:
2486 fprintf(file,"%4x %2x %2x",(unsigned)(aout_symbol(symbol)->desc & 0xffff),
2487 (unsigned)(aout_symbol(symbol)->other & 0xff),
2488 (unsigned)(aout_symbol(symbol)->type));
2489 break;
2490 case bfd_print_symbol_all:
2491 {
dc810e39 2492 const char *section_name = symbol->section->name;
e135f41b 2493
60b89a18 2494 bfd_print_symbol_vandf (abfd, (PTR)file, symbol);
e135f41b
NC
2495
2496 fprintf (file," %-5s %04x %02x %02x",
2497 section_name,
2498 (unsigned)(aout_symbol(symbol)->desc & 0xffff),
2499 (unsigned)(aout_symbol(symbol)->other & 0xff),
2500 (unsigned)(aout_symbol(symbol)->type & 0xff));
2501 if (symbol->name)
2502 fprintf(file," %s", symbol->name);
2503 }
2504 break;
2505 }
2506}
2507
2508/* If we don't have to allocate more than 1MB to hold the generic
2509 symbols, we use the generic minisymbol method: it's faster, since
2510 it only translates the symbols once, not multiple times. */
2511#define MINISYM_THRESHOLD (1000000 / sizeof (asymbol))
2512
2513/* Read minisymbols. For minisymbols, we use the unmodified a.out
2514 symbols. The minisymbol_to_symbol function translates these into
2515 BFD asymbol structures. */
2516
2517long
2518NAME(aout,read_minisymbols) (abfd, dynamic, minisymsp, sizep)
2519 bfd *abfd;
b34976b6 2520 bfd_boolean dynamic;
e135f41b
NC
2521 PTR *minisymsp;
2522 unsigned int *sizep;
2523{
2524 if (dynamic)
2525 {
2526 /* We could handle the dynamic symbols here as well, but it's
2527 easier to hand them off. */
2528 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2529 }
2530
2531 if (! aout_get_external_symbols (abfd))
2532 return -1;
2533
2534 if (obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2535 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2536
2537 *minisymsp = (PTR) obj_aout_external_syms (abfd);
2538
2539 /* By passing the external symbols back from this routine, we are
2540 giving up control over the memory block. Clear
2541 obj_aout_external_syms, so that we do not try to free it
2542 ourselves. */
2543 obj_aout_external_syms (abfd) = NULL;
2544
2545 *sizep = EXTERNAL_NLIST_SIZE;
2546 return obj_aout_external_sym_count (abfd);
2547}
2548
2549/* Convert a minisymbol to a BFD asymbol. A minisymbol is just an
2550 unmodified a.out symbol. The SYM argument is a structure returned
2551 by bfd_make_empty_symbol, which we fill in here. */
2552
2553asymbol *
2554NAME(aout,minisymbol_to_symbol) (abfd, dynamic, minisym, sym)
2555 bfd *abfd;
b34976b6 2556 bfd_boolean dynamic;
e135f41b
NC
2557 const PTR minisym;
2558 asymbol *sym;
2559{
2560 if (dynamic
2561 || obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2562 return _bfd_generic_minisymbol_to_symbol (abfd, dynamic, minisym, sym);
2563
2564 memset (sym, 0, sizeof (aout_symbol_type));
2565
2566 /* We call translate_symbol_table to translate a single symbol. */
2567 if (! (NAME(aout,translate_symbol_table)
2568 (abfd,
2569 (aout_symbol_type *) sym,
2570 (struct external_nlist *) minisym,
2571 (bfd_size_type) 1,
2572 obj_aout_external_strings (abfd),
2573 obj_aout_external_string_size (abfd),
b34976b6 2574 FALSE)))
e135f41b
NC
2575 return NULL;
2576
2577 return sym;
2578}
2579
2580/*
2581 provided a BFD, a section and an offset into the section, calculate
2582 and return the name of the source file and the line nearest to the
2583 wanted location.
2584*/
2585
b34976b6 2586bfd_boolean
e135f41b
NC
2587NAME(aout,find_nearest_line)
2588 (abfd, section, symbols, offset, filename_ptr, functionname_ptr, line_ptr)
2589 bfd *abfd;
2590 asection *section;
2591 asymbol **symbols;
2592 bfd_vma offset;
dc810e39
AM
2593 const char **filename_ptr;
2594 const char **functionname_ptr;
e135f41b
NC
2595 unsigned int *line_ptr;
2596{
2597 /* Run down the file looking for the filename, function and linenumber */
2598 asymbol **p;
dc810e39
AM
2599 const char *directory_name = NULL;
2600 const char *main_file_name = NULL;
2601 const char *current_file_name = NULL;
2602 const char *line_file_name = NULL; /* Value of current_file_name at line number. */
e135f41b
NC
2603 bfd_vma low_line_vma = 0;
2604 bfd_vma low_func_vma = 0;
2605 asymbol *func = 0;
2606 size_t filelen, funclen;
2607 char *buf;
2608
2609 *filename_ptr = abfd->filename;
2610 *functionname_ptr = 0;
2611 *line_ptr = 0;
2612
2613 if (symbols != (asymbol **)NULL)
2614 {
2615 for (p = symbols; *p; p++)
2616 {
2617 aout_symbol_type *q = (aout_symbol_type *)(*p);
2618 next:
2619 switch (q->type)
2620 {
2621 case N_TEXT:
2622 /* If this looks like a file name symbol, and it comes after
2623 the line number we have found so far, but before the
2624 offset, then we have probably not found the right line
2625 number. */
2626 if (q->symbol.value <= offset
2627 && ((q->symbol.value > low_line_vma
2628 && (line_file_name != NULL
2629 || *line_ptr != 0))
2630 || (q->symbol.value > low_func_vma
2631 && func != NULL)))
2632 {
2633 const char * symname;
2634
2635 symname = q->symbol.name;
2636 if (strcmp (symname + strlen (symname) - 2, ".o") == 0)
2637 {
2638 if (q->symbol.value > low_line_vma)
2639 {
2640 *line_ptr = 0;
2641 line_file_name = NULL;
2642 }
2643 if (q->symbol.value > low_func_vma)
2644 func = NULL;
2645 }
2646 }
2647 break;
2648
2649 case N_SO:
2650 /* If this symbol is less than the offset, but greater than
2651 the line number we have found so far, then we have not
2652 found the right line number. */
2653 if (q->symbol.value <= offset)
2654 {
2655 if (q->symbol.value > low_line_vma)
2656 {
2657 *line_ptr = 0;
2658 line_file_name = NULL;
2659 }
2660 if (q->symbol.value > low_func_vma)
2661 func = NULL;
2662 }
2663
2664 main_file_name = current_file_name = q->symbol.name;
2665 /* Look ahead to next symbol to check if that too is an N_SO. */
2666 p++;
2667 if (*p == NULL)
2668 break;
2669 q = (aout_symbol_type *)(*p);
2670 if (q->type != (int)N_SO)
2671 goto next;
2672
2673 /* Found a second N_SO First is directory; second is filename. */
2674 directory_name = current_file_name;
2675 main_file_name = current_file_name = q->symbol.name;
2676 if (obj_textsec(abfd) != section)
2677 goto done;
2678 break;
2679 case N_SOL:
2680 current_file_name = q->symbol.name;
2681 break;
2682
2683 case N_SLINE:
2684 case N_DSLINE:
2685 case N_BSLINE:
2686 /* We'll keep this if it resolves nearer than the one we have
2687 already. */
2688 if (q->symbol.value >= low_line_vma
2689 && q->symbol.value <= offset)
2690 {
2691 *line_ptr = q->desc;
2692 low_line_vma = q->symbol.value;
2693 line_file_name = current_file_name;
2694 }
2695 break;
2696
2697 case N_FUN:
2698 {
2699 /* We'll keep this if it is nearer than the one we have already */
2700 if (q->symbol.value >= low_func_vma &&
2701 q->symbol.value <= offset)
2702 {
2703 low_func_vma = q->symbol.value;
2704 func = (asymbol *)q;
2705 }
2706 else if (q->symbol.value > offset)
2707 goto done;
2708 }
2709 break;
2710 }
2711 }
2712 }
2713
2714 done:
2715 if (*line_ptr != 0)
2716 main_file_name = line_file_name;
2717
2718 if (main_file_name == NULL
2719 || main_file_name[0] == '/'
2720 || directory_name == NULL)
2721 filelen = 0;
2722 else
2723 filelen = strlen (directory_name) + strlen (main_file_name);
2724 if (func == NULL)
2725 funclen = 0;
2726 else
2727 funclen = strlen (bfd_asymbol_name (func));
2728
2729 if (adata (abfd).line_buf != NULL)
2730 free (adata (abfd).line_buf);
2731 if (filelen + funclen == 0)
2732 adata (abfd).line_buf = buf = NULL;
2733 else
2734 {
dc810e39 2735 buf = (char *) bfd_malloc ((bfd_size_type) filelen + funclen + 3);
e135f41b
NC
2736 adata (abfd).line_buf = buf;
2737 if (buf == NULL)
b34976b6 2738 return FALSE;
e135f41b
NC
2739 }
2740
2741 if (main_file_name != NULL)
2742 {
2743 if (main_file_name[0] == '/' || directory_name == NULL)
2744 *filename_ptr = main_file_name;
2745 else
2746 {
2747 sprintf (buf, "%s%s", directory_name, main_file_name);
2748 *filename_ptr = buf;
2749 buf += filelen + 1;
2750 }
2751 }
2752
2753 if (func)
2754 {
2755 const char *function = func->name;
dc810e39 2756 char *colon;
e135f41b
NC
2757
2758 /* The caller expects a symbol name. We actually have a
2759 function name, without the leading underscore. Put the
2760 underscore back in, so that the caller gets a symbol name. */
2761 if (bfd_get_symbol_leading_char (abfd) == '\0')
2762 strcpy (buf, function);
2763 else
2764 {
2765 buf[0] = bfd_get_symbol_leading_char (abfd);
2766 strcpy (buf + 1, function);
2767 }
2768
2769 /* Have to remove : stuff. */
dc810e39
AM
2770 colon = strchr (buf, ':');
2771 if (colon != NULL)
2772 *colon = '\0';
e135f41b
NC
2773 *functionname_ptr = buf;
2774 }
2775
b34976b6 2776 return TRUE;
e135f41b
NC
2777}
2778
2779int
2780NAME(aout,sizeof_headers) (abfd, execable)
2781 bfd *abfd;
b34976b6 2782 bfd_boolean execable ATTRIBUTE_UNUSED;
e135f41b
NC
2783{
2784 return adata(abfd).exec_bytes_size;
2785}
2786
2787/* Free all information we have cached for this BFD. We can always
2788 read it again later if we need it. */
2789
b34976b6 2790bfd_boolean
e135f41b
NC
2791NAME(aout,bfd_free_cached_info) (abfd)
2792 bfd *abfd;
2793{
2794 asection *o;
2795
2796 if (bfd_get_format (abfd) != bfd_object)
b34976b6 2797 return TRUE;
e135f41b
NC
2798
2799#define BFCI_FREE(x) if (x != NULL) { free (x); x = NULL; }
2800 BFCI_FREE (obj_aout_symbols (abfd));
2801#ifdef USE_MMAP
2802 obj_aout_external_syms (abfd) = 0;
2803 bfd_free_window (&obj_aout_sym_window (abfd));
2804 bfd_free_window (&obj_aout_string_window (abfd));
2805 obj_aout_external_strings (abfd) = 0;
2806#else
2807 BFCI_FREE (obj_aout_external_syms (abfd));
2808 BFCI_FREE (obj_aout_external_strings (abfd));
2809#endif
2810 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2811 BFCI_FREE (o->relocation);
2812#undef BFCI_FREE
2813
b34976b6 2814 return TRUE;
e135f41b
NC
2815}
2816\f
2817/* a.out link code. */
2818
b34976b6 2819static bfd_boolean aout_link_add_object_symbols
e135f41b 2820 PARAMS ((bfd *, struct bfd_link_info *));
b34976b6
AM
2821static bfd_boolean aout_link_check_archive_element
2822 PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *));
2823static bfd_boolean aout_link_free_symbols PARAMS ((bfd *));
2824static bfd_boolean aout_link_check_ar_symbols
2825 PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *pneeded));
2826static bfd_boolean aout_link_add_symbols
e135f41b
NC
2827 PARAMS ((bfd *, struct bfd_link_info *));
2828
2829/* Routine to create an entry in an a.out link hash table. */
2830
2831struct bfd_hash_entry *
2832NAME(aout,link_hash_newfunc) (entry, table, string)
2833 struct bfd_hash_entry *entry;
2834 struct bfd_hash_table *table;
2835 const char *string;
2836{
2837 struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
2838
2839 /* Allocate the structure if it has not already been allocated by a
2840 subclass. */
2841 if (ret == (struct aout_link_hash_entry *) NULL)
2842 ret = ((struct aout_link_hash_entry *)
2843 bfd_hash_allocate (table, sizeof (struct aout_link_hash_entry)));
2844 if (ret == (struct aout_link_hash_entry *) NULL)
2845 return (struct bfd_hash_entry *) ret;
2846
2847 /* Call the allocation method of the superclass. */
2848 ret = ((struct aout_link_hash_entry *)
2849 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2850 table, string));
2851 if (ret)
2852 {
2853 /* Set local fields. */
b34976b6 2854 ret->written = FALSE;
e135f41b
NC
2855 ret->indx = -1;
2856 }
2857
2858 return (struct bfd_hash_entry *) ret;
2859}
2860
2861/* Initialize an a.out link hash table. */
2862
b34976b6 2863bfd_boolean
e135f41b
NC
2864NAME(aout,link_hash_table_init) (table, abfd, newfunc)
2865 struct aout_link_hash_table *table;
2866 bfd *abfd;
2867 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
2868 struct bfd_hash_table *,
2869 const char *));
2870{
2871 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
2872}
2873
2874/* Create an a.out link hash table. */
2875
2876struct bfd_link_hash_table *
2877NAME(aout,link_hash_table_create) (abfd)
2878 bfd *abfd;
2879{
2880 struct aout_link_hash_table *ret;
dc810e39 2881 bfd_size_type amt = sizeof (struct aout_link_hash_table);
e135f41b 2882
dc810e39 2883 ret = ((struct aout_link_hash_table *) bfd_alloc (abfd, amt));
e135f41b
NC
2884 if (ret == NULL)
2885 return (struct bfd_link_hash_table *) NULL;
2886 if (! NAME(aout,link_hash_table_init) (ret, abfd,
2887 NAME(aout,link_hash_newfunc)))
2888 {
2889 free (ret);
2890 return (struct bfd_link_hash_table *) NULL;
2891 }
2892 return &ret->root;
2893}
2894
2895/* Given an a.out BFD, add symbols to the global hash table as
2896 appropriate. */
2897
b34976b6 2898bfd_boolean
e135f41b
NC
2899NAME(aout,link_add_symbols) (abfd, info)
2900 bfd *abfd;
2901 struct bfd_link_info *info;
2902{
2903 switch (bfd_get_format (abfd))
2904 {
2905 case bfd_object:
2906 return aout_link_add_object_symbols (abfd, info);
2907 case bfd_archive:
2908 return _bfd_generic_link_add_archive_symbols
2909 (abfd, info, aout_link_check_archive_element);
2910 default:
2911 bfd_set_error (bfd_error_wrong_format);
b34976b6 2912 return FALSE;
e135f41b
NC
2913 }
2914}
2915
2916/* Add symbols from an a.out object file. */
2917
b34976b6 2918static bfd_boolean
e135f41b
NC
2919aout_link_add_object_symbols (abfd, info)
2920 bfd *abfd;
2921 struct bfd_link_info *info;
2922{
2923 if (! aout_get_external_symbols (abfd))
b34976b6 2924 return FALSE;
e135f41b 2925 if (! aout_link_add_symbols (abfd, info))
b34976b6 2926 return FALSE;
e135f41b
NC
2927 if (! info->keep_memory)
2928 {
2929 if (! aout_link_free_symbols (abfd))
b34976b6 2930 return FALSE;
e135f41b 2931 }
b34976b6 2932 return TRUE;
e135f41b
NC
2933}
2934
2935/* Check a single archive element to see if we need to include it in
2936 the link. *PNEEDED is set according to whether this element is
2937 needed in the link or not. This is called from
2938 _bfd_generic_link_add_archive_symbols. */
2939
b34976b6 2940static bfd_boolean
e135f41b
NC
2941aout_link_check_archive_element (abfd, info, pneeded)
2942 bfd *abfd;
2943 struct bfd_link_info *info;
b34976b6 2944 bfd_boolean *pneeded;
e135f41b
NC
2945{
2946 if (! aout_get_external_symbols (abfd))
b34976b6 2947 return FALSE;
e135f41b
NC
2948
2949 if (! aout_link_check_ar_symbols (abfd, info, pneeded))
b34976b6 2950 return FALSE;
e135f41b
NC
2951
2952 if (*pneeded)
2953 {
2954 if (! aout_link_add_symbols (abfd, info))
b34976b6 2955 return FALSE;
e135f41b
NC
2956 }
2957
2958 if (! info->keep_memory || ! *pneeded)
2959 {
2960 if (! aout_link_free_symbols (abfd))
b34976b6 2961 return FALSE;
e135f41b
NC
2962 }
2963
b34976b6 2964 return TRUE;
e135f41b
NC
2965}
2966
2967/* Free up the internal symbols read from an a.out file. */
2968
b34976b6 2969static bfd_boolean
e135f41b
NC
2970aout_link_free_symbols (abfd)
2971 bfd *abfd;
2972{
2973 if (obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
2974 {
2975#ifdef USE_MMAP
2976 bfd_free_window (&obj_aout_sym_window (abfd));
2977#else
2978 free ((PTR) obj_aout_external_syms (abfd));
2979#endif
2980 obj_aout_external_syms (abfd) = (struct external_nlist *) NULL;
2981 }
2982 if (obj_aout_external_strings (abfd) != (char *) NULL)
2983 {
2984#ifdef USE_MMAP
2985 bfd_free_window (&obj_aout_string_window (abfd));
2986#else
2987 free ((PTR) obj_aout_external_strings (abfd));
2988#endif
2989 obj_aout_external_strings (abfd) = (char *) NULL;
2990 }
b34976b6 2991 return TRUE;
e135f41b
NC
2992}
2993
2994/* Look through the internal symbols to see if this object file should
2995 be included in the link. We should include this object file if it
2996 defines any symbols which are currently undefined. If this object
2997 file defines a common symbol, then we may adjust the size of the
2998 known symbol but we do not include the object file in the link
2999 (unless there is some other reason to include it). */
3000
b34976b6 3001static bfd_boolean
e135f41b
NC
3002aout_link_check_ar_symbols (abfd, info, pneeded)
3003 bfd *abfd;
3004 struct bfd_link_info *info;
b34976b6 3005 bfd_boolean *pneeded;
e135f41b
NC
3006{
3007 register struct external_nlist *p;
3008 struct external_nlist *pend;
3009 char *strings;
3010
b34976b6 3011 *pneeded = FALSE;
e135f41b
NC
3012
3013 /* Look through all the symbols. */
3014 p = obj_aout_external_syms (abfd);
3015 pend = p + obj_aout_external_sym_count (abfd);
3016 strings = obj_aout_external_strings (abfd);
3017 for (; p < pend; p++)
3018 {
dc810e39 3019 int type = H_GET_8 (abfd, p->e_type);
e135f41b
NC
3020 const char *name;
3021 struct bfd_link_hash_entry *h;
3022
3023 /* Ignore symbols that are not externally visible. This is an
3024 optimization only, as we check the type more thoroughly
3025 below. */
3026 if ((type & N_EXT) == 0
3027 || type == N_FN)
3028 continue;
3029
3030 name = strings + GET_WORD (abfd, p->e_strx);
b34976b6 3031 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
e135f41b
NC
3032
3033 /* We are only interested in symbols that are currently
3034 undefined or common. */
3035 if (h == (struct bfd_link_hash_entry *) NULL
3036 || (h->type != bfd_link_hash_undefined
3037 && h->type != bfd_link_hash_common))
3038 continue;
3039
3040 if (type == (N_TEXT | N_EXT)
3041 || type == (N_DATA | N_EXT)
3042 || type == (N_BSS | N_EXT)
3043 || type == (N_ABS | N_EXT))
3044 {
3045 /* This object file defines this symbol. We must link it
3046 in. This is true regardless of whether the current
3047 definition of the symbol is undefined or common. If the
3048 current definition is common, we have a case in which we
3049 have already seen an object file including
3050 int a;
3051 and this object file from the archive includes
3052 int a = 5;
3053 In such a case we must include this object file.
3054
3055 FIXME: The SunOS 4.1.3 linker will pull in the archive
3056 element if the symbol is defined in the .data section,
3057 but not if it is defined in the .text section. That
3058 seems a bit crazy to me, and I haven't implemented it.
3059 However, it might be correct. */
3060 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
b34976b6
AM
3061 return FALSE;
3062 *pneeded = TRUE;
3063 return TRUE;
e135f41b
NC
3064 }
3065
3066 if (type == (N_UNDF | N_EXT))
3067 {
3068 bfd_vma value;
3069
3070 value = GET_WORD (abfd, p->e_value);
3071 if (value != 0)
3072 {
3073 /* This symbol is common in the object from the archive
3074 file. */
3075 if (h->type == bfd_link_hash_undefined)
3076 {
3077 bfd *symbfd;
3078 unsigned int power;
3079
3080 symbfd = h->u.undef.abfd;
3081 if (symbfd == (bfd *) NULL)
3082 {
3083 /* This symbol was created as undefined from
3084 outside BFD. We assume that we should link
3085 in the object file. This is done for the -u
3086 option in the linker. */
3087 if (! (*info->callbacks->add_archive_element) (info,
3088 abfd,
3089 name))
b34976b6
AM
3090 return FALSE;
3091 *pneeded = TRUE;
3092 return TRUE;
e135f41b
NC
3093 }
3094 /* Turn the current link symbol into a common
3095 symbol. It is already on the undefs list. */
3096 h->type = bfd_link_hash_common;
3097 h->u.c.p = ((struct bfd_link_hash_common_entry *)
3098 bfd_hash_allocate (&info->hash->table,
3099 sizeof (struct bfd_link_hash_common_entry)));
3100 if (h->u.c.p == NULL)
b34976b6 3101 return FALSE;
e135f41b
NC
3102
3103 h->u.c.size = value;
3104
3105 /* FIXME: This isn't quite right. The maximum
3106 alignment of a common symbol should be set by the
3107 architecture of the output file, not of the input
3108 file. */
3109 power = bfd_log2 (value);
3110 if (power > bfd_get_arch_info (abfd)->section_align_power)
3111 power = bfd_get_arch_info (abfd)->section_align_power;
3112 h->u.c.p->alignment_power = power;
3113
3114 h->u.c.p->section = bfd_make_section_old_way (symbfd,
3115 "COMMON");
3116 }
3117 else
3118 {
3119 /* Adjust the size of the common symbol if
3120 necessary. */
3121 if (value > h->u.c.size)
3122 h->u.c.size = value;
3123 }
3124 }
3125 }
3126 }
3127
3128 /* We do not need this object file. */
b34976b6 3129 return TRUE;
e135f41b
NC
3130}
3131
3132/* Add all symbols from an object file to the hash table. */
3133
b34976b6 3134static bfd_boolean
e135f41b
NC
3135aout_link_add_symbols (abfd, info)
3136 bfd *abfd;
3137 struct bfd_link_info *info;
3138{
b34976b6
AM
3139 bfd_boolean (*add_one_symbol)
3140 PARAMS ((struct bfd_link_info *, bfd *, const char *, flagword, asection *,
3141 bfd_vma, const char *, bfd_boolean, bfd_boolean,
3142 struct bfd_link_hash_entry **));
e135f41b
NC
3143 struct external_nlist *syms;
3144 bfd_size_type sym_count;
3145 char *strings;
b34976b6 3146 bfd_boolean copy;
e135f41b
NC
3147 struct aout_link_hash_entry **sym_hash;
3148 register struct external_nlist *p;
3149 struct external_nlist *pend;
3150
3151 syms = obj_aout_external_syms (abfd);
3152 sym_count = obj_aout_external_sym_count (abfd);
3153 strings = obj_aout_external_strings (abfd);
3154 if (info->keep_memory)
b34976b6 3155 copy = FALSE;
e135f41b 3156 else
b34976b6 3157 copy = TRUE;
e135f41b
NC
3158
3159 if (aout_backend_info (abfd)->add_dynamic_symbols != NULL)
3160 {
3161 if (! ((*aout_backend_info (abfd)->add_dynamic_symbols)
3162 (abfd, info, &syms, &sym_count, &strings)))
b34976b6 3163 return FALSE;
e135f41b
NC
3164 }
3165
3166 /* We keep a list of the linker hash table entries that correspond
3167 to particular symbols. We could just look them up in the hash
3168 table, but keeping the list is more efficient. Perhaps this
3169 should be conditional on info->keep_memory. */
3170 sym_hash = ((struct aout_link_hash_entry **)
3171 bfd_alloc (abfd,
dc810e39 3172 sym_count * sizeof (struct aout_link_hash_entry *)));
e135f41b 3173 if (sym_hash == NULL && sym_count != 0)
b34976b6 3174 return FALSE;
e135f41b
NC
3175 obj_aout_sym_hashes (abfd) = sym_hash;
3176
3177 add_one_symbol = aout_backend_info (abfd)->add_one_symbol;
3178 if (add_one_symbol == NULL)
3179 add_one_symbol = _bfd_generic_link_add_one_symbol;
3180
3181 p = syms;
3182 pend = p + sym_count;
3183 for (; p < pend; p++, sym_hash++)
3184 {
3185 int type;
3186 const char *name;
3187 bfd_vma value;
3188 asection *section;
3189 flagword flags;
3190 const char *string;
3191
3192 *sym_hash = NULL;
3193
dc810e39 3194 type = H_GET_8 (abfd, p->e_type);
e135f41b
NC
3195
3196#if 0 /* not supported in PDP-11 a.out */
3197 /* Ignore debugging symbols. */
3198 if ((type & N_STAB) != 0)
3199 continue;
3200#endif
3201
3202 name = strings + GET_WORD (abfd, p->e_strx);
3203 value = GET_WORD (abfd, p->e_value);
3204 flags = BSF_GLOBAL;
3205 string = NULL;
3206 switch (type)
3207 {
3208 default:
3209 abort ();
3210
3211 case N_UNDF:
3212 case N_ABS:
3213 case N_TEXT:
3214 case N_DATA:
3215 case N_BSS:
3216 case N_REG:
3217 case N_FN:
3218 /* Ignore symbols that are not externally visible. */
3219 continue;
3220
3221 case N_UNDF | N_EXT:
3222 if (value == 0)
3223 {
3224 section = bfd_und_section_ptr;
3225 flags = 0;
3226 }
3227 else
3228 section = bfd_com_section_ptr;
3229 break;
3230 case N_ABS | N_EXT:
3231 section = bfd_abs_section_ptr;
3232 break;
3233 case N_TEXT | N_EXT:
3234 section = obj_textsec (abfd);
3235 value -= bfd_get_section_vma (abfd, section);
3236 break;
3237 case N_DATA | N_EXT:
3238 /* Treat N_SETV symbols as N_DATA symbol; see comment in
3239 translate_from_native_sym_flags. */
3240 section = obj_datasec (abfd);
3241 value -= bfd_get_section_vma (abfd, section);
3242 break;
3243 case N_BSS | N_EXT:
3244 section = obj_bsssec (abfd);
3245 value -= bfd_get_section_vma (abfd, section);
3246 break;
3247 }
3248
3249 if (! ((*add_one_symbol)
b34976b6 3250 (info, abfd, name, flags, section, value, string, copy, FALSE,
e135f41b 3251 (struct bfd_link_hash_entry **) sym_hash)))
b34976b6 3252 return FALSE;
e135f41b
NC
3253
3254 /* Restrict the maximum alignment of a common symbol based on
3255 the architecture, since a.out has no way to represent
3256 alignment requirements of a section in a .o file. FIXME:
3257 This isn't quite right: it should use the architecture of the
3258 output file, not the input files. */
3259 if ((*sym_hash)->root.type == bfd_link_hash_common
3260 && ((*sym_hash)->root.u.c.p->alignment_power >
3261 bfd_get_arch_info (abfd)->section_align_power))
3262 (*sym_hash)->root.u.c.p->alignment_power =
3263 bfd_get_arch_info (abfd)->section_align_power;
3264
3265 /* If this is a set symbol, and we are not building sets, then
3266 it is possible for the hash entry to not have been set. In
3267 such a case, treat the symbol as not globally defined. */
3268 if ((*sym_hash)->root.type == bfd_link_hash_new)
3269 {
3270 BFD_ASSERT ((flags & BSF_CONSTRUCTOR) != 0);
3271 *sym_hash = NULL;
3272 }
3273 }
3274
b34976b6 3275 return TRUE;
e135f41b
NC
3276}
3277\f
3278/* A hash table used for header files with N_BINCL entries. */
3279
3280struct aout_link_includes_table
3281{
3282 struct bfd_hash_table root;
3283};
3284
3285/* A linked list of totals that we have found for a particular header
3286 file. */
3287
3288struct aout_link_includes_totals
3289{
3290 struct aout_link_includes_totals *next;
3291 bfd_vma total;
3292};
3293
3294/* An entry in the header file hash table. */
3295
3296struct aout_link_includes_entry
3297{
3298 struct bfd_hash_entry root;
3299 /* List of totals we have found for this file. */
3300 struct aout_link_includes_totals *totals;
3301};
3302
3303/* Look up an entry in an the header file hash table. */
3304
3305#define aout_link_includes_lookup(table, string, create, copy) \
3306 ((struct aout_link_includes_entry *) \
3307 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
3308
3309/* During the final link step we need to pass around a bunch of
3310 information, so we do it in an instance of this structure. */
3311
3312struct aout_final_link_info
3313{
3314 /* General link information. */
3315 struct bfd_link_info *info;
3316 /* Output bfd. */
3317 bfd *output_bfd;
3318 /* Reloc file positions. */
3319 file_ptr treloff, dreloff;
3320 /* File position of symbols. */
3321 file_ptr symoff;
3322 /* String table. */
3323 struct bfd_strtab_hash *strtab;
3324 /* Header file hash table. */
3325 struct aout_link_includes_table includes;
3326 /* A buffer large enough to hold the contents of any section. */
3327 bfd_byte *contents;
3328 /* A buffer large enough to hold the relocs of any section. */
3329 PTR relocs;
3330 /* A buffer large enough to hold the symbol map of any input BFD. */
3331 int *symbol_map;
3332 /* A buffer large enough to hold output symbols of any input BFD. */
3333 struct external_nlist *output_syms;
3334};
3335
3336static struct bfd_hash_entry *aout_link_includes_newfunc
3337 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
b34976b6 3338static bfd_boolean aout_link_input_bfd
e135f41b 3339 PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
b34976b6 3340static bfd_boolean aout_link_write_symbols
e135f41b 3341 PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
b34976b6 3342static bfd_boolean aout_link_write_other_symbol
e135f41b 3343 PARAMS ((struct aout_link_hash_entry *, PTR));
b34976b6 3344static bfd_boolean aout_link_input_section
e135f41b
NC
3345 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3346 asection *input_section, file_ptr *reloff_ptr,
3347 bfd_size_type rel_size));
3348static INLINE asection *aout_reloc_type_to_section
3349 PARAMS ((bfd *, int));
b34976b6 3350static bfd_boolean aout_link_reloc_link_order
e135f41b
NC
3351 PARAMS ((struct aout_final_link_info *, asection *,
3352 struct bfd_link_order *));
b34976b6 3353static bfd_boolean pdp11_aout_link_input_section
e135f41b
NC
3354 PARAMS ((struct aout_final_link_info *finfo,
3355 bfd *input_bfd,
3356 asection *input_section,
3357 struct pdp11_aout_reloc_external *relocs,
3358 bfd_size_type rel_size,
3359 bfd_byte *contents));
3360
3361/* The function to create a new entry in the header file hash table. */
3362
3363static struct bfd_hash_entry *
3364aout_link_includes_newfunc (entry, table, string)
3365 struct bfd_hash_entry *entry;
3366 struct bfd_hash_table *table;
3367 const char *string;
3368{
3369 struct aout_link_includes_entry *ret =
3370 (struct aout_link_includes_entry *) entry;
3371
3372 /* Allocate the structure if it has not already been allocated by a
3373 subclass. */
3374 if (ret == (struct aout_link_includes_entry *) NULL)
3375 ret = ((struct aout_link_includes_entry *)
3376 bfd_hash_allocate (table,
3377 sizeof (struct aout_link_includes_entry)));
3378 if (ret == (struct aout_link_includes_entry *) NULL)
3379 return (struct bfd_hash_entry *) ret;
3380
3381 /* Call the allocation method of the superclass. */
3382 ret = ((struct aout_link_includes_entry *)
3383 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
3384 if (ret)
3385 {
3386 /* Set local fields. */
3387 ret->totals = NULL;
3388 }
3389
3390 return (struct bfd_hash_entry *) ret;
3391}
3392
3393/* Do the final link step. This is called on the output BFD. The
3394 INFO structure should point to a list of BFDs linked through the
3395 link_next field which can be used to find each BFD which takes part
3396 in the output. Also, each section in ABFD should point to a list
3397 of bfd_link_order structures which list all the input sections for
3398 the output section. */
3399
b34976b6 3400bfd_boolean
e135f41b
NC
3401NAME(aout,final_link) (abfd, info, callback)
3402 bfd *abfd;
3403 struct bfd_link_info *info;
3404 void (*callback) PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *));
3405{
3406 struct aout_final_link_info aout_info;
b34976b6 3407 bfd_boolean includes_hash_initialized = FALSE;
e135f41b
NC
3408 register bfd *sub;
3409 bfd_size_type trsize, drsize;
dc810e39
AM
3410 bfd_size_type max_contents_size;
3411 bfd_size_type max_relocs_size;
3412 bfd_size_type max_sym_count;
e135f41b
NC
3413 bfd_size_type text_size;
3414 file_ptr text_end;
3415 register struct bfd_link_order *p;
3416 asection *o;
b34976b6 3417 bfd_boolean have_link_order_relocs;
e135f41b
NC
3418
3419 if (info->shared)
3420 abfd->flags |= DYNAMIC;
3421
3422 aout_info.info = info;
3423 aout_info.output_bfd = abfd;
3424 aout_info.contents = NULL;
3425 aout_info.relocs = NULL;
3426 aout_info.symbol_map = NULL;
3427 aout_info.output_syms = NULL;
3428
3429 if (! bfd_hash_table_init_n (&aout_info.includes.root,
3430 aout_link_includes_newfunc,
3431 251))
3432 goto error_return;
b34976b6 3433 includes_hash_initialized = TRUE;
e135f41b
NC
3434
3435 /* Figure out the largest section size. Also, if generating
1049f94e 3436 relocatable output, count the relocs. */
e135f41b
NC
3437 trsize = 0;
3438 drsize = 0;
3439 max_contents_size = 0;
3440 max_relocs_size = 0;
3441 max_sym_count = 0;
3442 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3443 {
3444 size_t sz;
3445
1049f94e 3446 if (info->relocatable)
e135f41b
NC
3447 {
3448 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
3449 {
3450 trsize += exec_hdr (sub)->a_trsize;
3451 drsize += exec_hdr (sub)->a_drsize;
3452 }
3453 else
3454 {
3455 /* FIXME: We need to identify the .text and .data sections
3456 and call get_reloc_upper_bound and canonicalize_reloc to
3457 work out the number of relocs needed, and then multiply
3458 by the reloc size. */
3459 (*_bfd_error_handler)
1049f94e 3460 ("%s: relocatable link from %s to %s not supported",
e135f41b
NC
3461 bfd_get_filename (abfd),
3462 sub->xvec->name, abfd->xvec->name);
3463 bfd_set_error (bfd_error_invalid_operation);
3464 goto error_return;
3465 }
3466 }
3467
3468 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
3469 {
eea6121a 3470 sz = obj_textsec (sub)->size;
e135f41b
NC
3471 if (sz > max_contents_size)
3472 max_contents_size = sz;
eea6121a 3473 sz = obj_datasec (sub)->size;
e135f41b
NC
3474 if (sz > max_contents_size)
3475 max_contents_size = sz;
3476
3477 sz = exec_hdr (sub)->a_trsize;
3478 if (sz > max_relocs_size)
3479 max_relocs_size = sz;
3480 sz = exec_hdr (sub)->a_drsize;
3481 if (sz > max_relocs_size)
3482 max_relocs_size = sz;
3483
3484 sz = obj_aout_external_sym_count (sub);
3485 if (sz > max_sym_count)
3486 max_sym_count = sz;
3487 }
3488 }
3489
1049f94e 3490 if (info->relocatable)
e135f41b
NC
3491 {
3492 if (obj_textsec (abfd) != (asection *) NULL)
3493 trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
3494 ->link_order_head)
3495 * obj_reloc_entry_size (abfd));
3496 if (obj_datasec (abfd) != (asection *) NULL)
3497 drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
3498 ->link_order_head)
3499 * obj_reloc_entry_size (abfd));
3500 }
3501
3502 exec_hdr (abfd)->a_trsize = trsize;
3503 exec_hdr (abfd)->a_drsize = drsize;
3504
3505 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
3506
3507 /* Adjust the section sizes and vmas according to the magic number.
3508 This sets a_text, a_data and a_bss in the exec_hdr and sets the
3509 filepos for each section. */
3510 if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
3511 goto error_return;
3512
3513 /* The relocation and symbol file positions differ among a.out
3514 targets. We are passed a callback routine from the backend
3515 specific code to handle this.
3516 FIXME: At this point we do not know how much space the symbol
3517 table will require. This will not work for any (nonstandard)
3518 a.out target that needs to know the symbol table size before it
3519 can compute the relocation file positions. This may or may not
3520 be the case for the hp300hpux target, for example. */
3521 (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
3522 &aout_info.symoff);
3523 obj_textsec (abfd)->rel_filepos = aout_info.treloff;
3524 obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
3525 obj_sym_filepos (abfd) = aout_info.symoff;
3526
3527 /* We keep a count of the symbols as we output them. */
3528 obj_aout_external_sym_count (abfd) = 0;
3529
3530 /* We accumulate the string table as we write out the symbols. */
3531 aout_info.strtab = _bfd_stringtab_init ();
3532 if (aout_info.strtab == NULL)
3533 goto error_return;
3534
3535 /* Allocate buffers to hold section contents and relocs. */
3536 aout_info.contents = (bfd_byte *) bfd_malloc (max_contents_size);
3537 aout_info.relocs = (PTR) bfd_malloc (max_relocs_size);
3538 aout_info.symbol_map = (int *) bfd_malloc (max_sym_count * sizeof (int *));
3539 aout_info.output_syms = ((struct external_nlist *)
3540 bfd_malloc ((max_sym_count + 1)
3541 * sizeof (struct external_nlist)));
3542 if ((aout_info.contents == NULL && max_contents_size != 0)
3543 || (aout_info.relocs == NULL && max_relocs_size != 0)
3544 || (aout_info.symbol_map == NULL && max_sym_count != 0)
3545 || aout_info.output_syms == NULL)
3546 goto error_return;
3547
3548 /* If we have a symbol named __DYNAMIC, force it out now. This is
3549 required by SunOS. Doing this here rather than in sunos.c is a
3550 hack, but it's easier than exporting everything which would be
3551 needed. */
3552 {
3553 struct aout_link_hash_entry *h;
3554
3555 h = aout_link_hash_lookup (aout_hash_table (info), "__DYNAMIC",
b34976b6 3556 FALSE, FALSE, FALSE);
e135f41b
NC
3557 if (h != NULL)
3558 aout_link_write_other_symbol (h, &aout_info);
3559 }
3560
3561 /* The most time efficient way to do the link would be to read all
3562 the input object files into memory and then sort out the
3563 information into the output file. Unfortunately, that will
3564 probably use too much memory. Another method would be to step
3565 through everything that composes the text section and write it
3566 out, and then everything that composes the data section and write
3567 it out, and then write out the relocs, and then write out the
3568 symbols. Unfortunately, that requires reading stuff from each
3569 input file several times, and we will not be able to keep all the
3570 input files open simultaneously, and reopening them will be slow.
3571
3572 What we do is basically process one input file at a time. We do
3573 everything we need to do with an input file once--copy over the
3574 section contents, handle the relocation information, and write
3575 out the symbols--and then we throw away the information we read
3576 from it. This approach requires a lot of lseeks of the output
3577 file, which is unfortunate but still faster than reopening a lot
3578 of files.
3579
3580 We use the output_has_begun field of the input BFDs to see
3581 whether we have already handled it. */
3582 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
b34976b6 3583 sub->output_has_begun = FALSE;
e135f41b
NC
3584
3585 /* Mark all sections which are to be included in the link. This
3586 will normally be every section. We need to do this so that we
3587 can identify any sections which the linker has decided to not
3588 include. */
3589 for (o = abfd->sections; o != NULL; o = o->next)
3590 {
3591 for (p = o->link_order_head; p != NULL; p = p->next)
3592 {
3593 if (p->type == bfd_indirect_link_order)
b34976b6 3594 p->u.indirect.section->linker_mark = TRUE;
e135f41b
NC
3595 }
3596 }
3597
b34976b6 3598 have_link_order_relocs = FALSE;
e135f41b
NC
3599 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3600 {
3601 for (p = o->link_order_head;
3602 p != (struct bfd_link_order *) NULL;
3603 p = p->next)
3604 {
3605 if (p->type == bfd_indirect_link_order
3606 && (bfd_get_flavour (p->u.indirect.section->owner)
3607 == bfd_target_aout_flavour))
3608 {
3609 bfd *input_bfd;
3610
3611 input_bfd = p->u.indirect.section->owner;
3612 if (! input_bfd->output_has_begun)
3613 {
3614 if (! aout_link_input_bfd (&aout_info, input_bfd))
3615 goto error_return;
b34976b6 3616 input_bfd->output_has_begun = TRUE;
e135f41b
NC
3617 }
3618 }
3619 else if (p->type == bfd_section_reloc_link_order
3620 || p->type == bfd_symbol_reloc_link_order)
3621 {
3622 /* These are handled below. */
b34976b6 3623 have_link_order_relocs = TRUE;
e135f41b
NC
3624 }
3625 else
3626 {
3627 if (! _bfd_default_link_order (abfd, info, o, p))
3628 goto error_return;
3629 }
3630 }
3631 }
3632
3633 /* Write out any symbols that we have not already written out. */
3634 aout_link_hash_traverse (aout_hash_table (info),
3635 aout_link_write_other_symbol,
3636 (PTR) &aout_info);
3637
3638 /* Now handle any relocs we were asked to create by the linker.
3639 These did not come from any input file. We must do these after
3640 we have written out all the symbols, so that we know the symbol
3641 indices to use. */
3642 if (have_link_order_relocs)
3643 {
3644 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3645 {
3646 for (p = o->link_order_head;
3647 p != (struct bfd_link_order *) NULL;
3648 p = p->next)
3649 {
3650 if (p->type == bfd_section_reloc_link_order
3651 || p->type == bfd_symbol_reloc_link_order)
3652 {
3653 if (! aout_link_reloc_link_order (&aout_info, o, p))
3654 goto error_return;
3655 }
3656 }
3657 }
3658 }
3659
3660 if (aout_info.contents != NULL)
3661 {
3662 free (aout_info.contents);
3663 aout_info.contents = NULL;
3664 }
3665 if (aout_info.relocs != NULL)
3666 {
3667 free (aout_info.relocs);
3668 aout_info.relocs = NULL;
3669 }
3670 if (aout_info.symbol_map != NULL)
3671 {
3672 free (aout_info.symbol_map);
3673 aout_info.symbol_map = NULL;
3674 }
3675 if (aout_info.output_syms != NULL)
3676 {
3677 free (aout_info.output_syms);
3678 aout_info.output_syms = NULL;
3679 }
3680 if (includes_hash_initialized)
3681 {
3682 bfd_hash_table_free (&aout_info.includes.root);
b34976b6 3683 includes_hash_initialized = FALSE;
e135f41b
NC
3684 }
3685
3686 /* Finish up any dynamic linking we may be doing. */
3687 if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
3688 {
3689 if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
3690 goto error_return;
3691 }
3692
3693 /* Update the header information. */
3694 abfd->symcount = obj_aout_external_sym_count (abfd);
3695 exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
3696 obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
3697 obj_textsec (abfd)->reloc_count =
3698 exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
3699 obj_datasec (abfd)->reloc_count =
3700 exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
3701
3702 /* Write out the string table, unless there are no symbols. */
3703 if (abfd->symcount > 0)
3704 {
3705 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
3706 || ! emit_stringtab (abfd, aout_info.strtab))
3707 goto error_return;
3708 }
3709 else if (obj_textsec (abfd)->reloc_count == 0
3710 && obj_datasec (abfd)->reloc_count == 0)
3711 {
3712 bfd_byte b;
3713
3714 b = 0;
3715 if (bfd_seek (abfd,
dc810e39
AM
3716 (file_ptr) (obj_datasec (abfd)->filepos
3717 + exec_hdr (abfd)->a_data
3718 - 1),
e135f41b 3719 SEEK_SET) != 0
dc810e39 3720 || bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
e135f41b
NC
3721 goto error_return;
3722 }
3723
b34976b6 3724 return TRUE;
e135f41b
NC
3725
3726 error_return:
3727 if (aout_info.contents != NULL)
3728 free (aout_info.contents);
3729 if (aout_info.relocs != NULL)
3730 free (aout_info.relocs);
3731 if (aout_info.symbol_map != NULL)
3732 free (aout_info.symbol_map);
3733 if (aout_info.output_syms != NULL)
3734 free (aout_info.output_syms);
3735 if (includes_hash_initialized)
3736 bfd_hash_table_free (&aout_info.includes.root);
b34976b6 3737 return FALSE;
e135f41b
NC
3738}
3739
3740/* Link an a.out input BFD into the output file. */
3741
b34976b6 3742static bfd_boolean
e135f41b
NC
3743aout_link_input_bfd (finfo, input_bfd)
3744 struct aout_final_link_info *finfo;
3745 bfd *input_bfd;
3746{
3747 bfd_size_type sym_count;
3748
3749 BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
3750
3751 /* If this is a dynamic object, it may need special handling. */
3752 if ((input_bfd->flags & DYNAMIC) != 0
3753 && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
3754 {
3755 return ((*aout_backend_info (input_bfd)->link_dynamic_object)
3756 (finfo->info, input_bfd));
3757 }
3758
3759 /* Get the symbols. We probably have them already, unless
b34976b6 3760 finfo->info->keep_memory is FALSE. */
e135f41b 3761 if (! aout_get_external_symbols (input_bfd))
b34976b6 3762 return FALSE;
e135f41b
NC
3763
3764 sym_count = obj_aout_external_sym_count (input_bfd);
3765
3766 /* Write out the symbols and get a map of the new indices. The map
3767 is placed into finfo->symbol_map. */
3768 if (! aout_link_write_symbols (finfo, input_bfd))
b34976b6 3769 return FALSE;
e135f41b
NC
3770
3771 /* Relocate and write out the sections. These functions use the
3772 symbol map created by aout_link_write_symbols. The linker_mark
3773 field will be set if these sections are to be included in the
3774 link, which will normally be the case. */
3775 if (obj_textsec (input_bfd)->linker_mark)
3776 {
3777 if (! aout_link_input_section (finfo, input_bfd,
3778 obj_textsec (input_bfd),
3779 &finfo->treloff,
3780 exec_hdr (input_bfd)->a_trsize))
b34976b6 3781 return FALSE;
e135f41b
NC
3782 }
3783 if (obj_datasec (input_bfd)->linker_mark)
3784 {
3785 if (! aout_link_input_section (finfo, input_bfd,
3786 obj_datasec (input_bfd),
3787 &finfo->dreloff,
3788 exec_hdr (input_bfd)->a_drsize))
b34976b6 3789 return FALSE;
e135f41b
NC
3790 }
3791
3792 /* If we are not keeping memory, we don't need the symbols any
3793 longer. We still need them if we are keeping memory, because the
3794 strings in the hash table point into them. */
3795 if (! finfo->info->keep_memory)
3796 {
3797 if (! aout_link_free_symbols (input_bfd))
b34976b6 3798 return FALSE;
e135f41b
NC
3799 }
3800
b34976b6 3801 return TRUE;
e135f41b
NC
3802}
3803
3804/* Adjust and write out the symbols for an a.out file. Set the new
3805 symbol indices into a symbol_map. */
3806
b34976b6 3807static bfd_boolean
e135f41b
NC
3808aout_link_write_symbols (finfo, input_bfd)
3809 struct aout_final_link_info *finfo;
3810 bfd *input_bfd;
3811{
3812 bfd *output_bfd;
3813 bfd_size_type sym_count;
3814 char *strings;
3815 enum bfd_link_strip strip;
3816 enum bfd_link_discard discard;
3817 struct external_nlist *outsym;
3818 bfd_size_type strtab_index;
3819 register struct external_nlist *sym;
3820 struct external_nlist *sym_end;
3821 struct aout_link_hash_entry **sym_hash;
3822 int *symbol_map;
b34976b6
AM
3823 bfd_boolean pass;
3824 bfd_boolean skip_next;
e135f41b
NC
3825
3826 output_bfd = finfo->output_bfd;
3827 sym_count = obj_aout_external_sym_count (input_bfd);
3828 strings = obj_aout_external_strings (input_bfd);
3829 strip = finfo->info->strip;
3830 discard = finfo->info->discard;
3831 outsym = finfo->output_syms;
3832
3833 /* First write out a symbol for this object file, unless we are
3834 discarding such symbols. */
3835 if (strip != strip_all
3836 && (strip != strip_some
3837 || bfd_hash_lookup (finfo->info->keep_hash, input_bfd->filename,
b34976b6 3838 FALSE, FALSE) != NULL)
e135f41b
NC
3839 && discard != discard_all)
3840 {
dc810e39 3841 H_PUT_8 (output_bfd, N_TEXT, outsym->e_type);
e135f41b 3842 strtab_index = add_to_stringtab (output_bfd, finfo->strtab,
b34976b6 3843 input_bfd->filename, FALSE);
e135f41b 3844 if (strtab_index == (bfd_size_type) -1)
b34976b6 3845 return FALSE;
e135f41b
NC
3846 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
3847 PUT_WORD (output_bfd,
3848 (bfd_get_section_vma (output_bfd,
3849 obj_textsec (input_bfd)->output_section)
3850 + obj_textsec (input_bfd)->output_offset),
3851 outsym->e_value);
3852 ++obj_aout_external_sym_count (output_bfd);
3853 ++outsym;
3854 }
3855
b34976b6
AM
3856 pass = FALSE;
3857 skip_next = FALSE;
e135f41b
NC
3858 sym = obj_aout_external_syms (input_bfd);
3859 sym_end = sym + sym_count;
3860 sym_hash = obj_aout_sym_hashes (input_bfd);
3861 symbol_map = finfo->symbol_map;
dc810e39 3862 memset (symbol_map, 0, (size_t) sym_count * sizeof *symbol_map);
e135f41b
NC
3863 for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
3864 {
3865 const char *name;
3866 int type;
3867 struct aout_link_hash_entry *h;
b34976b6 3868 bfd_boolean skip;
e135f41b
NC
3869 asection *symsec;
3870 bfd_vma val = 0;
b34976b6 3871 bfd_boolean copy;
e135f41b
NC
3872
3873 /* We set *symbol_map to 0 above for all symbols. If it has
3874 already been set to -1 for this symbol, it means that we are
3875 discarding it because it appears in a duplicate header file.
3876 See the N_BINCL code below. */
3877 if (*symbol_map == -1)
3878 continue;
3879
3880 /* Initialize *symbol_map to -1, which means that the symbol was
3881 not copied into the output file. We will change it later if
3882 we do copy the symbol over. */
3883 *symbol_map = -1;
3884
dc810e39 3885 type = H_GET_8 (input_bfd, sym->e_type);
e135f41b
NC
3886 name = strings + GET_WORD (input_bfd, sym->e_strx);
3887
3888 h = NULL;
3889
3890 if (pass)
3891 {
3892 /* Pass this symbol through. It is the target of an
3893 indirect or warning symbol. */
3894 val = GET_WORD (input_bfd, sym->e_value);
b34976b6 3895 pass = FALSE;
e135f41b
NC
3896 }
3897 else if (skip_next)
3898 {
3899 /* Skip this symbol, which is the target of an indirect
3900 symbol that we have changed to no longer be an indirect
3901 symbol. */
b34976b6 3902 skip_next = FALSE;
e135f41b
NC
3903 continue;
3904 }
3905 else
3906 {
3907 struct aout_link_hash_entry *hresolve;
3908
3909 /* We have saved the hash table entry for this symbol, if
3910 there is one. Note that we could just look it up again
3911 in the hash table, provided we first check that it is an
3912 external symbol. */
3913 h = *sym_hash;
3914
3915 /* Use the name from the hash table, in case the symbol was
3916 wrapped. */
3917 if (h != NULL)
3918 name = h->root.root.string;
3919
3920 /* If this is an indirect or warning symbol, then change
3921 hresolve to the base symbol. We also change *sym_hash so
3922 that the relocation routines relocate against the real
3923 symbol. */
3924 hresolve = h;
3925 if (h != (struct aout_link_hash_entry *) NULL
3926 && (h->root.type == bfd_link_hash_indirect
3927 || h->root.type == bfd_link_hash_warning))
3928 {
3929 hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
3930 while (hresolve->root.type == bfd_link_hash_indirect
3931 || hresolve->root.type == bfd_link_hash_warning)
3932 hresolve = ((struct aout_link_hash_entry *)
3933 hresolve->root.u.i.link);
3934 *sym_hash = hresolve;
3935 }
3936
3937 /* If the symbol has already been written out, skip it. */
3938 if (h != (struct aout_link_hash_entry *) NULL
3939 && h->root.type != bfd_link_hash_warning
3940 && h->written)
3941 {
3942 if ((type & N_TYPE) == N_INDR
3943 || type == N_WARNING)
b34976b6 3944 skip_next = TRUE;
e135f41b
NC
3945 *symbol_map = h->indx;
3946 continue;
3947 }
3948
3949 /* See if we are stripping this symbol. */
b34976b6 3950 skip = FALSE;
e135f41b
NC
3951 switch (strip)
3952 {
3953 case strip_none:
3954 break;
3955 case strip_debugger:
3956 if ((type & N_STAB) != 0)
b34976b6 3957 skip = TRUE;
e135f41b
NC
3958 break;
3959 case strip_some:
b34976b6 3960 if (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
e135f41b 3961 == NULL)
b34976b6 3962 skip = TRUE;
e135f41b
NC
3963 break;
3964 case strip_all:
b34976b6 3965 skip = TRUE;
e135f41b
NC
3966 break;
3967 }
3968 if (skip)
3969 {
3970 if (h != (struct aout_link_hash_entry *) NULL)
b34976b6 3971 h->written = TRUE;
e135f41b
NC
3972 continue;
3973 }
3974
3975 /* Get the value of the symbol. */
3976 if ((type & N_TYPE) == N_TEXT
3977 || type == N_WEAKT)
3978 symsec = obj_textsec (input_bfd);
3979 else if ((type & N_TYPE) == N_DATA
3980 || type == N_WEAKD)
3981 symsec = obj_datasec (input_bfd);
3982 else if ((type & N_TYPE) == N_BSS
3983 || type == N_WEAKB)
3984 symsec = obj_bsssec (input_bfd);
3985 else if ((type & N_TYPE) == N_ABS
3986 || type == N_WEAKA)
3987 symsec = bfd_abs_section_ptr;
3988 else if (((type & N_TYPE) == N_INDR
3989 && (hresolve == (struct aout_link_hash_entry *) NULL
3990 || (hresolve->root.type != bfd_link_hash_defined
3991 && hresolve->root.type != bfd_link_hash_defweak
3992 && hresolve->root.type != bfd_link_hash_common)))
3993 || type == N_WARNING)
3994 {
3995 /* Pass the next symbol through unchanged. The
3996 condition above for indirect symbols is so that if
3997 the indirect symbol was defined, we output it with
3998 the correct definition so the debugger will
3999 understand it. */
b34976b6 4000 pass = TRUE;
e135f41b
NC
4001 val = GET_WORD (input_bfd, sym->e_value);
4002 symsec = NULL;
4003 }
4004 else if ((type & N_STAB) != 0)
4005 {
4006 val = GET_WORD (input_bfd, sym->e_value);
4007 symsec = NULL;
4008 }
4009 else
4010 {
4011 /* If we get here with an indirect symbol, it means that
4012 we are outputting it with a real definition. In such
4013 a case we do not want to output the next symbol,
4014 which is the target of the indirection. */
4015 if ((type & N_TYPE) == N_INDR)
b34976b6 4016 skip_next = TRUE;
e135f41b
NC
4017
4018 symsec = NULL;
4019
4020 /* We need to get the value from the hash table. We use
4021 hresolve so that if we have defined an indirect
4022 symbol we output the final definition. */
4023 if (h == (struct aout_link_hash_entry *) NULL)
4024 {
4025 switch (type & N_TYPE)
4026 {
4027 case N_SETT:
4028 symsec = obj_textsec (input_bfd);
4029 break;
4030 case N_SETD:
4031 symsec = obj_datasec (input_bfd);
4032 break;
4033 case N_SETB:
4034 symsec = obj_bsssec (input_bfd);
4035 break;
4036 case N_SETA:
4037 symsec = bfd_abs_section_ptr;
4038 break;
4039 default:
4040 val = 0;
4041 break;
4042 }
4043 }
4044 else if (hresolve->root.type == bfd_link_hash_defined
4045 || hresolve->root.type == bfd_link_hash_defweak)
4046 {
4047 asection *input_section;
4048 asection *output_section;
4049
4050 /* This case usually means a common symbol which was
4051 turned into a defined symbol. */
4052 input_section = hresolve->root.u.def.section;
4053 output_section = input_section->output_section;
4054 BFD_ASSERT (bfd_is_abs_section (output_section)
4055 || output_section->owner == output_bfd);
4056 val = (hresolve->root.u.def.value
4057 + bfd_get_section_vma (output_bfd, output_section)
4058 + input_section->output_offset);
4059
4060 /* Get the correct type based on the section. If
4061 this is a constructed set, force it to be
4062 globally visible. */
4063 if (type == N_SETT
4064 || type == N_SETD
4065 || type == N_SETB
4066 || type == N_SETA)
4067 type |= N_EXT;
4068
4069 type &=~ N_TYPE;
4070
4071 if (output_section == obj_textsec (output_bfd))
4072 type |= (hresolve->root.type == bfd_link_hash_defined
4073 ? N_TEXT
4074 : N_WEAKT);
4075 else if (output_section == obj_datasec (output_bfd))
4076 type |= (hresolve->root.type == bfd_link_hash_defined
4077 ? N_DATA
4078 : N_WEAKD);
4079 else if (output_section == obj_bsssec (output_bfd))
4080 type |= (hresolve->root.type == bfd_link_hash_defined
4081 ? N_BSS
4082 : N_WEAKB);
4083 else
4084 type |= (hresolve->root.type == bfd_link_hash_defined
4085 ? N_ABS
4086 : N_WEAKA);
4087 }
4088 else if (hresolve->root.type == bfd_link_hash_common)
4089 val = hresolve->root.u.c.size;
4090 else if (hresolve->root.type == bfd_link_hash_undefweak)
4091 {
4092 val = 0;
4093 type = N_WEAKU;
4094 }
4095 else
4096 val = 0;
4097 }
4098 if (symsec != (asection *) NULL)
4099 val = (symsec->output_section->vma
4100 + symsec->output_offset
4101 + (GET_WORD (input_bfd, sym->e_value)
4102 - symsec->vma));
4103
4104 /* If this is a global symbol set the written flag, and if
4105 it is a local symbol see if we should discard it. */
4106 if (h != (struct aout_link_hash_entry *) NULL)
4107 {
b34976b6 4108 h->written = TRUE;
e135f41b
NC
4109 h->indx = obj_aout_external_sym_count (output_bfd);
4110 }
4111 else if ((type & N_TYPE) != N_SETT
4112 && (type & N_TYPE) != N_SETD
4113 && (type & N_TYPE) != N_SETB
4114 && (type & N_TYPE) != N_SETA)
4115 {
4116 switch (discard)
4117 {
4118 case discard_none:
f5fa8ca2 4119 case discard_sec_merge:
e135f41b
NC
4120 break;
4121 case discard_l:
4122 if ((type & N_STAB) == 0
4123 && bfd_is_local_label_name (input_bfd, name))
b34976b6 4124 skip = TRUE;
e135f41b
NC
4125 break;
4126 case discard_all:
b34976b6 4127 skip = TRUE;
e135f41b
NC
4128 break;
4129 }
4130 if (skip)
4131 {
b34976b6 4132 pass = FALSE;
e135f41b
NC
4133 continue;
4134 }
4135 }
4136
4137 /* An N_BINCL symbol indicates the start of the stabs
4138 entries for a header file. We need to scan ahead to the
4139 next N_EINCL symbol, ignoring nesting, adding up all the
4140 characters in the symbol names, not including the file
4141 numbers in types (the first number after an open
4142 parenthesis). */
4143 if (type == N_BINCL)
4144 {
4145 struct external_nlist *incl_sym;
4146 int nest;
4147 struct aout_link_includes_entry *incl_entry;
4148 struct aout_link_includes_totals *t;
4149
4150 val = 0;
4151 nest = 0;
4152 for (incl_sym = sym + 1; incl_sym < sym_end; incl_sym++)
4153 {
4154 int incl_type;
4155
dc810e39 4156 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
e135f41b
NC
4157 if (incl_type == N_EINCL)
4158 {
4159 if (nest == 0)
4160 break;
4161 --nest;
4162 }
4163 else if (incl_type == N_BINCL)
4164 ++nest;
4165 else if (nest == 0)
4166 {
4167 const char *s;
4168
4169 s = strings + GET_WORD (input_bfd, incl_sym->e_strx);
4170 for (; *s != '\0'; s++)
4171 {
4172 val += *s;
4173 if (*s == '(')
4174 {
4175 /* Skip the file number. */
4176 ++s;
3882b010 4177 while (ISDIGIT (*s))
e135f41b
NC
4178 ++s;
4179 --s;
4180 }
4181 }
4182 }
4183 }
4184
4185 /* If we have already included a header file with the
4186 same value, then replace this one with an N_EXCL
4187 symbol. */
4188 copy = ! finfo->info->keep_memory;
4189 incl_entry = aout_link_includes_lookup (&finfo->includes,
b34976b6 4190 name, TRUE, copy);
e135f41b 4191 if (incl_entry == NULL)
b34976b6 4192 return FALSE;
e135f41b
NC
4193 for (t = incl_entry->totals; t != NULL; t = t->next)
4194 if (t->total == val)
4195 break;
4196 if (t == NULL)
4197 {
4198 /* This is the first time we have seen this header
4199 file with this set of stabs strings. */
4200 t = ((struct aout_link_includes_totals *)
4201 bfd_hash_allocate (&finfo->includes.root,
4202 sizeof *t));
4203 if (t == NULL)
b34976b6 4204 return FALSE;
e135f41b
NC
4205 t->total = val;
4206 t->next = incl_entry->totals;
4207 incl_entry->totals = t;
4208 }
4209 else
4210 {
4211 int *incl_map;
4212
4213 /* This is a duplicate header file. We must change
4214 it to be an N_EXCL entry, and mark all the
4215 included symbols to prevent outputting them. */
4216 type = N_EXCL;
4217
4218 nest = 0;
4219 for (incl_sym = sym + 1, incl_map = symbol_map + 1;
4220 incl_sym < sym_end;
4221 incl_sym++, incl_map++)
4222 {
4223 int incl_type;
4224
dc810e39 4225 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
e135f41b
NC
4226 if (incl_type == N_EINCL)
4227 {
4228 if (nest == 0)
4229 {
4230 *incl_map = -1;
4231 break;
4232 }
4233 --nest;
4234 }
4235 else if (incl_type == N_BINCL)
4236 ++nest;
4237 else if (nest == 0)
4238 *incl_map = -1;
4239 }
4240 }
4241 }
4242 }
4243
4244 /* Copy this symbol into the list of symbols we are going to
4245 write out. */
dc810e39 4246 H_PUT_8 (output_bfd, type, outsym->e_type);
b34976b6 4247 copy = FALSE;
e135f41b
NC
4248 if (! finfo->info->keep_memory)
4249 {
4250 /* name points into a string table which we are going to
4251 free. If there is a hash table entry, use that string.
4252 Otherwise, copy name into memory. */
4253 if (h != (struct aout_link_hash_entry *) NULL)
4254 name = h->root.root.string;
4255 else
b34976b6 4256 copy = TRUE;
e135f41b
NC
4257 }
4258 strtab_index = add_to_stringtab (output_bfd, finfo->strtab,
4259 name, copy);
4260 if (strtab_index == (bfd_size_type) -1)
b34976b6 4261 return FALSE;
e135f41b
NC
4262 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
4263 PUT_WORD (output_bfd, val, outsym->e_value);
4264 *symbol_map = obj_aout_external_sym_count (output_bfd);
4265 ++obj_aout_external_sym_count (output_bfd);
4266 ++outsym;
4267 }
4268
4269 /* Write out the output symbols we have just constructed. */
4270 if (outsym > finfo->output_syms)
4271 {
dc810e39 4272 bfd_size_type size;
e135f41b
NC
4273
4274 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0)
b34976b6 4275 return FALSE;
dc810e39
AM
4276 size = outsym - finfo->output_syms;
4277 size *= EXTERNAL_NLIST_SIZE;
4278 if (bfd_bwrite ((PTR) finfo->output_syms, size, output_bfd) != size)
b34976b6 4279 return FALSE;
dc810e39 4280 finfo->symoff += size;
e135f41b
NC
4281 }
4282
b34976b6 4283 return TRUE;
e135f41b
NC
4284}
4285
4286/* Write out a symbol that was not associated with an a.out input
4287 object. */
4288
b34976b6 4289static bfd_boolean
e135f41b
NC
4290aout_link_write_other_symbol (h, data)
4291 struct aout_link_hash_entry *h;
4292 PTR data;
4293{
4294 struct aout_final_link_info *finfo = (struct aout_final_link_info *) data;
4295 bfd *output_bfd;
4296 int type;
4297 bfd_vma val;
4298 struct external_nlist outsym;
4299 bfd_size_type indx;
dc810e39 4300 bfd_size_type amt;
e135f41b 4301
e92d460e
AM
4302 if (h->root.type == bfd_link_hash_warning)
4303 {
4304 h = (struct aout_link_hash_entry *) h->root.u.i.link;
4305 if (h->root.type == bfd_link_hash_new)
b34976b6 4306 return TRUE;
e92d460e
AM
4307 }
4308
e135f41b
NC
4309 output_bfd = finfo->output_bfd;
4310
4311 if (aout_backend_info (output_bfd)->write_dynamic_symbol != NULL)
4312 {
4313 if (! ((*aout_backend_info (output_bfd)->write_dynamic_symbol)
4314 (output_bfd, finfo->info, h)))
4315 {
4316 /* FIXME: No way to handle errors. */
4317 abort ();
4318 }
4319 }
4320
4321 if (h->written)
b34976b6 4322 return TRUE;
e135f41b 4323
b34976b6 4324 h->written = TRUE;
e135f41b
NC
4325
4326 /* An indx of -2 means the symbol must be written. */
4327 if (h->indx != -2
4328 && (finfo->info->strip == strip_all
4329 || (finfo->info->strip == strip_some
4330 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
b34976b6
AM
4331 FALSE, FALSE) == NULL)))
4332 return TRUE;
e135f41b
NC
4333
4334 switch (h->root.type)
4335 {
4336 default:
4337 abort ();
4338 /* Avoid variable not initialized warnings. */
b34976b6 4339 return TRUE;
e135f41b
NC
4340 case bfd_link_hash_new:
4341 /* This can happen for set symbols when sets are not being
4342 built. */
b34976b6 4343 return TRUE;
e135f41b
NC
4344 case bfd_link_hash_undefined:
4345 type = N_UNDF | N_EXT;
4346 val = 0;
4347 break;
4348 case bfd_link_hash_defined:
4349 case bfd_link_hash_defweak:
4350 {
4351 asection *sec;
4352
4353 sec = h->root.u.def.section->output_section;
4354 BFD_ASSERT (bfd_is_abs_section (sec)
4355 || sec->owner == output_bfd);
4356 if (sec == obj_textsec (output_bfd))
4357 type = h->root.type == bfd_link_hash_defined ? N_TEXT : N_WEAKT;
4358 else if (sec == obj_datasec (output_bfd))
4359 type = h->root.type == bfd_link_hash_defined ? N_DATA : N_WEAKD;
4360 else if (sec == obj_bsssec (output_bfd))
4361 type = h->root.type == bfd_link_hash_defined ? N_BSS : N_WEAKB;
4362 else
4363 type = h->root.type == bfd_link_hash_defined ? N_ABS : N_WEAKA;
4364 type |= N_EXT;
4365 val = (h->root.u.def.value
4366 + sec->vma
4367 + h->root.u.def.section->output_offset);
4368 }
4369 break;
4370 case bfd_link_hash_common:
4371 type = N_UNDF | N_EXT;
4372 val = h->root.u.c.size;
4373 break;
4374 case bfd_link_hash_undefweak:
4375 type = N_WEAKU;
4376 val = 0;
4377 case bfd_link_hash_indirect:
4378 case bfd_link_hash_warning:
4379 /* FIXME: Ignore these for now. The circumstances under which
4380 they should be written out are not clear to me. */
b34976b6 4381 return TRUE;
e135f41b
NC
4382 }
4383
dc810e39 4384 H_PUT_8 (output_bfd, type, outsym.e_type);
e135f41b 4385 indx = add_to_stringtab (output_bfd, finfo->strtab, h->root.root.string,
b34976b6 4386 FALSE);
e135f41b
NC
4387 if (indx == (bfd_size_type) -1)
4388 {
4389 /* FIXME: No way to handle errors. */
4390 abort ();
4391 }
4392 PUT_WORD (output_bfd, indx, outsym.e_strx);
4393 PUT_WORD (output_bfd, val, outsym.e_value);
4394
dc810e39 4395 amt = EXTERNAL_NLIST_SIZE;
e135f41b 4396 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0
dc810e39 4397 || bfd_bwrite ((PTR) &outsym, amt, output_bfd) != amt)
e135f41b
NC
4398 {
4399 /* FIXME: No way to handle errors. */
4400 abort ();
4401 }
4402
dc810e39 4403 finfo->symoff += amt;
e135f41b
NC
4404 h->indx = obj_aout_external_sym_count (output_bfd);
4405 ++obj_aout_external_sym_count (output_bfd);
4406
b34976b6 4407 return TRUE;
e135f41b
NC
4408}
4409
4410/* Link an a.out section into the output file. */
4411
b34976b6 4412static bfd_boolean
e135f41b
NC
4413aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr,
4414 rel_size)
4415 struct aout_final_link_info *finfo;
4416 bfd *input_bfd;
4417 asection *input_section;
4418 file_ptr *reloff_ptr;
4419 bfd_size_type rel_size;
4420{
4421 bfd_size_type input_size;
4422 PTR relocs;
4423
4424 /* Get the section contents. */
eea6121a 4425 input_size = input_section->size;
e135f41b
NC
4426 if (! bfd_get_section_contents (input_bfd, input_section,
4427 (PTR) finfo->contents,
4428 (file_ptr) 0, input_size))
b34976b6 4429 return FALSE;
e135f41b
NC
4430
4431 /* Read in the relocs if we haven't already done it. */
4432 if (aout_section_data (input_section) != NULL
4433 && aout_section_data (input_section)->relocs != NULL)
4434 relocs = aout_section_data (input_section)->relocs;
4435 else
4436 {
4437 relocs = finfo->relocs;
4438 if (rel_size > 0)
4439 {
4440 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
dc810e39 4441 || bfd_bread (relocs, rel_size, input_bfd) != rel_size)
b34976b6 4442 return FALSE;
e135f41b
NC
4443 }
4444 }
4445
4446 /* Relocate the section contents. */
4447 if (! pdp11_aout_link_input_section (finfo, input_bfd, input_section,
4448 (struct pdp11_aout_reloc_external *) relocs,
4449 rel_size, finfo->contents))
b34976b6 4450 return FALSE;
e135f41b
NC
4451
4452 /* Write out the section contents. */
4453 if (! bfd_set_section_contents (finfo->output_bfd,
4454 input_section->output_section,
4455 (PTR) finfo->contents,
dc810e39 4456 (file_ptr) input_section->output_offset,
e135f41b 4457 input_size))
b34976b6 4458 return FALSE;
e135f41b 4459
1049f94e 4460 /* If we are producing relocatable output, the relocs were
e135f41b 4461 modified, and we now write them out. */
1049f94e 4462 if (finfo->info->relocatable && rel_size > 0)
e135f41b
NC
4463 {
4464 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
b34976b6 4465 return FALSE;
dc810e39 4466 if (bfd_bwrite (relocs, rel_size, finfo->output_bfd) != rel_size)
b34976b6 4467 return FALSE;
e135f41b
NC
4468 *reloff_ptr += rel_size;
4469
4470 /* Assert that the relocs have not run into the symbols, and
4471 that if these are the text relocs they have not run into the
4472 data relocs. */
4473 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
4474 && (reloff_ptr != &finfo->treloff
4475 || (*reloff_ptr
4476 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
4477 }
4478
b34976b6 4479 return TRUE;
e135f41b
NC
4480}
4481
4482/* Get the section corresponding to a reloc index. */
4483
4484static INLINE asection *
4485aout_reloc_type_to_section (abfd, type)
4486 bfd *abfd;
4487 int type;
4488{
4489 switch (type)
4490 {
4491 case RTEXT:
4492 return obj_textsec (abfd);
4493 case RDATA:
4494 return obj_datasec (abfd);
4495 case RBSS:
4496 return obj_bsssec (abfd);
4497 case RABS:
4498 return bfd_abs_section_ptr;
4499 case REXT:
4500 return bfd_und_section_ptr;
4501 default:
4502 abort ();
4503 }
4504}
4505
b34976b6 4506static bfd_boolean
e135f41b
NC
4507pdp11_aout_link_input_section (finfo, input_bfd, input_section, relocs,
4508 rel_size, contents)
4509 struct aout_final_link_info *finfo;
4510 bfd *input_bfd;
4511 asection *input_section;
4512 struct pdp11_aout_reloc_external *relocs;
4513 bfd_size_type rel_size;
4514 bfd_byte *contents;
4515{
b34976b6
AM
4516 bfd_boolean (*check_dynamic_reloc)
4517 PARAMS ((struct bfd_link_info *, bfd *, asection *,
4518 struct aout_link_hash_entry *, PTR, bfd_byte *, bfd_boolean *,
4519 bfd_vma *));
e135f41b 4520 bfd *output_bfd;
1049f94e 4521 bfd_boolean relocatable;
e135f41b
NC
4522 struct external_nlist *syms;
4523 char *strings;
4524 struct aout_link_hash_entry **sym_hashes;
4525 int *symbol_map;
4526 bfd_size_type reloc_count;
4527 register struct pdp11_aout_reloc_external *rel;
4528 struct pdp11_aout_reloc_external *rel_end;
4529
4530 output_bfd = finfo->output_bfd;
4531 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
4532
4533 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_SIZE);
4534 BFD_ASSERT (input_bfd->xvec->header_byteorder
4535 == output_bfd->xvec->header_byteorder);
4536
1049f94e 4537 relocatable = finfo->info->relocatable;
e135f41b
NC
4538 syms = obj_aout_external_syms (input_bfd);
4539 strings = obj_aout_external_strings (input_bfd);
4540 sym_hashes = obj_aout_sym_hashes (input_bfd);
4541 symbol_map = finfo->symbol_map;
4542
4543 reloc_count = rel_size / RELOC_SIZE;
4544 rel = relocs;
4545 rel_end = (struct pdp11_aout_reloc_external *)(((char *)rel) + rel_size);
4546 for (; rel < rel_end; ((char *)rel) += RELOC_SIZE)
4547 {
4548 bfd_vma r_addr;
4549 int r_index;
4550 int r_type;
4551 int r_pcrel;
4552 int r_extern;
4553 reloc_howto_type *howto;
4554 struct aout_link_hash_entry *h = NULL;
4555 bfd_vma relocation;
4556 bfd_reloc_status_type r;
4557 int reloc_entry;
4558
4559 reloc_entry = GET_WORD (input_bfd, (PTR)rel);
4560 if (reloc_entry == 0)
4561 continue;
4562
4563 {
4564 unsigned int howto_idx;
4565
4566 r_index = (reloc_entry & RIDXMASK) >> 4;
4567 r_type = reloc_entry & RTYPE;
4568 r_pcrel = reloc_entry & RELFLG;
4569 r_addr = (char *)rel - (char *)relocs;
4570
4571 r_extern = (r_type == REXT);
4572
4573 howto_idx = r_pcrel;
4574 BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_pdp11));
4575 howto = howto_table_pdp11 + howto_idx;
4576 }
4577
1049f94e 4578 if (relocatable)
e135f41b 4579 {
1049f94e 4580 /* We are generating a relocatable output file, and must
e135f41b
NC
4581 modify the reloc accordingly. */
4582 if (r_extern)
4583 {
4584 /* If we know the symbol this relocation is against,
4585 convert it into a relocation against a section. This
4586 is what the native linker does. */
4587 h = sym_hashes[r_index];
4588 if (h != (struct aout_link_hash_entry *) NULL
4589 && (h->root.type == bfd_link_hash_defined
4590 || h->root.type == bfd_link_hash_defweak))
4591 {
4592 asection *output_section;
4593
4594 /* Compute a new r_index. */
4595 output_section = h->root.u.def.section->output_section;
4596 if (output_section == obj_textsec (output_bfd))
4597 r_type = N_TEXT;
4598 else if (output_section == obj_datasec (output_bfd))
4599 r_type = N_DATA;
4600 else if (output_section == obj_bsssec (output_bfd))
4601 r_type = N_BSS;
4602 else
4603 r_type = N_ABS;
4604
4605 /* Add the symbol value and the section VMA to the
4606 addend stored in the contents. */
4607 relocation = (h->root.u.def.value
4608 + output_section->vma
4609 + h->root.u.def.section->output_offset);
4610 }
4611 else
4612 {
4613 /* We must change r_index according to the symbol
4614 map. */
4615 r_index = symbol_map[r_index];
4616
4617 if (r_index == -1)
4618 {
4619 if (h != NULL)
4620 {
4621 /* We decided to strip this symbol, but it
4622 turns out that we can't. Note that we
4623 lose the other and desc information here.
4624 I don't think that will ever matter for a
4625 global symbol. */
4626 if (h->indx < 0)
4627 {
4628 h->indx = -2;
b34976b6 4629 h->written = FALSE;
e135f41b
NC
4630 if (! aout_link_write_other_symbol (h,
4631 (PTR) finfo))
b34976b6 4632 return FALSE;
e135f41b
NC
4633 }
4634 r_index = h->indx;
4635 }
4636 else
4637 {
4638 const char *name;
4639
4640 name = strings + GET_WORD (input_bfd,
4641 syms[r_index].e_strx);
4642 if (! ((*finfo->info->callbacks->unattached_reloc)
4643 (finfo->info, name, input_bfd, input_section,
4644 r_addr)))
b34976b6 4645 return FALSE;
e135f41b
NC
4646 r_index = 0;
4647 }
4648 }
4649
4650 relocation = 0;
4651 }
4652
4653 /* Write out the new r_index value. */
4654 reloc_entry = GET_WORD (input_bfd, rel->e_reloc_entry);
4655 reloc_entry &= RIDXMASK;
4656 reloc_entry |= r_index << 4;
4657 PUT_WORD (input_bfd, reloc_entry, rel->e_reloc_entry);
4658 }
4659 else
4660 {
4661 asection *section;
4662
4663 /* This is a relocation against a section. We must
4664 adjust by the amount that the section moved. */
4665 section = aout_reloc_type_to_section (input_bfd, r_type);
4666 relocation = (section->output_section->vma
4667 + section->output_offset
4668 - section->vma);
4669 }
4670
4671 /* Change the address of the relocation. */
4672#if 0
4673 PUT_WORD (output_bfd,
4674 r_addr + input_section->output_offset,
4675 rel->r_address);
4676#else
4677fprintf (stderr, "TODO: change the address of the relocation\n");
4678#endif
4679
4680 /* Adjust a PC relative relocation by removing the reference
4681 to the original address in the section and including the
4682 reference to the new address. */
4683 if (r_pcrel)
4684 relocation -= (input_section->output_section->vma
4685 + input_section->output_offset
4686 - input_section->vma);
4687
4688#ifdef MY_relocatable_reloc
4689 MY_relocatable_reloc (howto, output_bfd, rel, relocation, r_addr);
4690#endif
4691
4692 if (relocation == 0)
4693 r = bfd_reloc_ok;
4694 else
4695 r = MY_relocate_contents (howto,
4696 input_bfd, relocation,
4697 contents + r_addr);
4698 }
4699 else
4700 {
b34976b6 4701 bfd_boolean hundef;
e135f41b
NC
4702
4703 /* We are generating an executable, and must do a full
4704 relocation. */
b34976b6 4705 hundef = FALSE;
e135f41b
NC
4706 if (r_extern)
4707 {
4708 h = sym_hashes[r_index];
4709
4710 if (h != (struct aout_link_hash_entry *) NULL
4711 && (h->root.type == bfd_link_hash_defined
4712 || h->root.type == bfd_link_hash_defweak))
4713 {
4714 relocation = (h->root.u.def.value
4715 + h->root.u.def.section->output_section->vma
4716 + h->root.u.def.section->output_offset);
4717 }
4718 else if (h != (struct aout_link_hash_entry *) NULL
4719 && h->root.type == bfd_link_hash_undefweak)
4720 relocation = 0;
4721 else
4722 {
b34976b6 4723 hundef = TRUE;
e135f41b
NC
4724 relocation = 0;
4725 }
4726 }
4727 else
4728 {
4729 asection *section;
4730
4731 section = aout_reloc_type_to_section (input_bfd, r_type);
4732 relocation = (section->output_section->vma
4733 + section->output_offset
4734 - section->vma);
4735 if (r_pcrel)
4736 relocation += input_section->vma;
4737 }
4738
4739 if (check_dynamic_reloc != NULL)
4740 {
b34976b6 4741 bfd_boolean skip;
e135f41b
NC
4742
4743 if (! ((*check_dynamic_reloc)
4744 (finfo->info, input_bfd, input_section, h,
4745 (PTR) rel, contents, &skip, &relocation)))
b34976b6 4746 return FALSE;
e135f41b
NC
4747 if (skip)
4748 continue;
4749 }
4750
4751 /* Now warn if a global symbol is undefined. We could not
4752 do this earlier, because check_dynamic_reloc might want
4753 to skip this reloc. */
4754 if (hundef && ! finfo->info->shared)
4755 {
4756 const char *name;
4757
4758 if (h != NULL)
4759 name = h->root.root.string;
4760 else
4761 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4762 if (! ((*finfo->info->callbacks->undefined_symbol)
4763 (finfo->info, name, input_bfd, input_section,
b34976b6
AM
4764 r_addr, TRUE)))
4765 return FALSE;
e135f41b
NC
4766 }
4767
4768 r = MY_final_link_relocate (howto,
4769 input_bfd, input_section,
4770 contents, r_addr, relocation,
4771 (bfd_vma) 0);
4772 }
4773
4774 if (r != bfd_reloc_ok)
4775 {
4776 switch (r)
4777 {
4778 default:
4779 case bfd_reloc_outofrange:
4780 abort ();
4781 case bfd_reloc_overflow:
4782 {
4783 const char *name;
4784
4785 if (h != NULL)
4786 name = h->root.root.string;
4787 else if (r_extern)
4788 name = strings + GET_WORD (input_bfd,
4789 syms[r_index].e_strx);
4790 else
4791 {
4792 asection *s;
4793
4794 s = aout_reloc_type_to_section (input_bfd, r_type);
4795 name = bfd_section_name (input_bfd, s);
4796 }
4797 if (! ((*finfo->info->callbacks->reloc_overflow)
4798 (finfo->info, name, howto->name,
4799 (bfd_vma) 0, input_bfd, input_section, r_addr)))
b34976b6 4800 return FALSE;
e135f41b
NC
4801 }
4802 break;
4803 }
4804 }
4805 }
4806
b34976b6 4807 return TRUE;
e135f41b
NC
4808}
4809
4810/* Handle a link order which is supposed to generate a reloc. */
4811
b34976b6 4812static bfd_boolean
e135f41b
NC
4813aout_link_reloc_link_order (finfo, o, p)
4814 struct aout_final_link_info *finfo;
4815 asection *o;
4816 struct bfd_link_order *p;
4817{
4818 struct bfd_link_order_reloc *pr;
4819 int r_index;
4820 int r_extern;
4821 reloc_howto_type *howto;
4822 file_ptr *reloff_ptr;
4823 struct reloc_std_external srel;
4824 PTR rel_ptr;
dc810e39 4825 bfd_size_type rel_size;
e135f41b
NC
4826
4827 pr = p->u.reloc.p;
4828
4829 if (p->type == bfd_section_reloc_link_order)
4830 {
4831 r_extern = 0;
4832 if (bfd_is_abs_section (pr->u.section))
4833 r_index = N_ABS | N_EXT;
4834 else
4835 {
4836 BFD_ASSERT (pr->u.section->owner == finfo->output_bfd);
4837 r_index = pr->u.section->target_index;
4838 }
4839 }
4840 else
4841 {
4842 struct aout_link_hash_entry *h;
4843
4844 BFD_ASSERT (p->type == bfd_symbol_reloc_link_order);
4845 r_extern = 1;
4846 h = ((struct aout_link_hash_entry *)
4847 bfd_wrapped_link_hash_lookup (finfo->output_bfd, finfo->info,
b34976b6 4848 pr->u.name, FALSE, FALSE, TRUE));
e135f41b
NC
4849 if (h != (struct aout_link_hash_entry *) NULL
4850 && h->indx >= 0)
4851 r_index = h->indx;
4852 else if (h != NULL)
4853 {
4854 /* We decided to strip this symbol, but it turns out that we
4855 can't. Note that we lose the other and desc information
4856 here. I don't think that will ever matter for a global
4857 symbol. */
4858 h->indx = -2;
b34976b6 4859 h->written = FALSE;
e135f41b 4860 if (! aout_link_write_other_symbol (h, (PTR) finfo))
b34976b6 4861 return FALSE;
e135f41b
NC
4862 r_index = h->indx;
4863 }
4864 else
4865 {
4866 if (! ((*finfo->info->callbacks->unattached_reloc)
4867 (finfo->info, pr->u.name, (bfd *) NULL,
4868 (asection *) NULL, (bfd_vma) 0)))
b34976b6 4869 return FALSE;
e135f41b
NC
4870 r_index = 0;
4871 }
4872 }
4873
4874 howto = bfd_reloc_type_lookup (finfo->output_bfd, pr->reloc);
4875 if (howto == 0)
4876 {
4877 bfd_set_error (bfd_error_bad_value);
b34976b6 4878 return FALSE;
e135f41b
NC
4879 }
4880
4881 if (o == obj_textsec (finfo->output_bfd))
4882 reloff_ptr = &finfo->treloff;
4883 else if (o == obj_datasec (finfo->output_bfd))
4884 reloff_ptr = &finfo->dreloff;
4885 else
4886 abort ();
4887
4888#ifdef MY_put_reloc
4889 MY_put_reloc(finfo->output_bfd, r_extern, r_index, p->offset, howto,
4890 &srel);
4891#else
4892 {
4893 int r_pcrel;
4894 int r_baserel;
4895 int r_jmptable;
4896 int r_relative;
4897 int r_length;
4898
4899 fprintf (stderr, "TODO: line %d in bfd/pdp11.c\n", __LINE__);
4900
4901 r_pcrel = howto->pc_relative;
4902 r_baserel = (howto->type & 8) != 0;
4903 r_jmptable = (howto->type & 16) != 0;
4904 r_relative = (howto->type & 32) != 0;
4905 r_length = howto->size;
4906
4907 PUT_WORD (finfo->output_bfd, p->offset, srel.r_address);
4908 if (bfd_header_big_endian (finfo->output_bfd))
4909 {
4910 srel.r_index[0] = r_index >> 16;
4911 srel.r_index[1] = r_index >> 8;
4912 srel.r_index[2] = r_index;
4913 srel.r_type[0] =
4914 ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
4915 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
4916 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
4917 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
4918 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
4919 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
4920 }
4921 else
4922 {
4923 srel.r_index[2] = r_index >> 16;
4924 srel.r_index[1] = r_index >> 8;
4925 srel.r_index[0] = r_index;
4926 srel.r_type[0] =
4927 ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
4928 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
4929 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
4930 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
4931 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
4932 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
4933 }
4934 }
4935#endif
4936 rel_ptr = (PTR) &srel;
4937
4938 /* We have to write the addend into the object file, since
4939 standard a.out relocs are in place. It would be more
4940 reliable if we had the current contents of the file here,
4941 rather than assuming zeroes, but we can't read the file since
4942 it was opened using bfd_openw. */
4943 if (pr->addend != 0)
4944 {
4945 bfd_size_type size;
4946 bfd_reloc_status_type r;
4947 bfd_byte *buf;
b34976b6 4948 bfd_boolean ok;
e135f41b
NC
4949
4950 size = bfd_get_reloc_size (howto);
4951 buf = (bfd_byte *) bfd_zmalloc (size);
4952 if (buf == (bfd_byte *) NULL)
b34976b6 4953 return FALSE;
e135f41b
NC
4954 r = MY_relocate_contents (howto, finfo->output_bfd,
4955 pr->addend, buf);
4956 switch (r)
4957 {
4958 case bfd_reloc_ok:
4959 break;
4960 default:
4961 case bfd_reloc_outofrange:
4962 abort ();
4963 case bfd_reloc_overflow:
4964 if (! ((*finfo->info->callbacks->reloc_overflow)
4965 (finfo->info,
4966 (p->type == bfd_section_reloc_link_order
4967 ? bfd_section_name (finfo->output_bfd,
4968 pr->u.section)
4969 : pr->u.name),
4970 howto->name, pr->addend, (bfd *) NULL,
4971 (asection *) NULL, (bfd_vma) 0)))
4972 {
4973 free (buf);
b34976b6 4974 return FALSE;
e135f41b
NC
4975 }
4976 break;
4977 }
4978 ok = bfd_set_section_contents (finfo->output_bfd, o,
4979 (PTR) buf,
4980 (file_ptr) p->offset,
4981 size);
4982 free (buf);
4983 if (! ok)
b34976b6 4984 return FALSE;
e135f41b
NC
4985 }
4986
dc810e39 4987 rel_size = obj_reloc_entry_size (finfo->output_bfd);
e135f41b 4988 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0
dc810e39 4989 || bfd_bwrite (rel_ptr, rel_size, finfo->output_bfd) != rel_size)
b34976b6 4990 return FALSE;
e135f41b 4991
dc810e39 4992 *reloff_ptr += rel_size;
e135f41b
NC
4993
4994 /* Assert that the relocs have not run into the symbols, and that n
4995 the text relocs have not run into the data relocs. */
4996 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
4997 && (reloff_ptr != &finfo->treloff
4998 || (*reloff_ptr
4999 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
5000
b34976b6 5001 return TRUE;
e135f41b
NC
5002}
5003/* end of modified aoutx.h */
5004
edeb6e24
AM
5005static bfd_vma
5006bfd_getp32 (const void *p)
e135f41b 5007{
edeb6e24
AM
5008 const bfd_byte *addr = p;
5009 unsigned long v;
5010 v = (unsigned long) addr[1] << 24;
5011 v |= (unsigned long) addr[0] << 16;
5012 v |= (unsigned long) addr[3] << 8;
5013 v |= (unsigned long) addr[2];
5014 return v;
e135f41b
NC
5015}
5016
5017#define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
5018
edeb6e24
AM
5019static bfd_signed_vma
5020bfd_getp_signed_32 (const void *p)
e135f41b 5021{
edeb6e24
AM
5022 const bfd_byte *addr = p;
5023 unsigned long v;
5024 v = (unsigned long) addr[1] << 24;
5025 v |= (unsigned long) addr[0] << 16;
5026 v |= (unsigned long) addr[3] << 8;
5027 v |= (unsigned long) addr[2];
5028 return COERCE32 (v);
e135f41b
NC
5029}
5030
edeb6e24
AM
5031static void
5032bfd_putp32 (bfd_vma data, void *p)
e135f41b 5033{
edeb6e24
AM
5034 bfd_byte *addr = p;
5035 addr[0] = (data >> 16) & 0xff;
5036 addr[1] = (data >> 24) & 0xff;
5037 addr[2] = (data >> 0) & 0xff;
5038 addr[3] = (data >> 8) & 0xff;
e135f41b 5039}
This page took 0.744667 seconds and 4 git commands to generate.