file ChangeLog.csl was initially added on branch csl-arm-20050325-branch.
[binutils.git] / bfd / pdp11.c
blob1b4e95eb788ec6f560bad71f742bbcea83919b52
1 /* BFD back-end for PDP-11 a.out binaries.
2 Copyright 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
20 /* BFD backend for PDP-11, running 2.11BSD in particular.
22 This file was hacked up by looking hard at the existing vaxnetbsd
23 back end and the header files in 2.11BSD.
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)
31 Search for TODO to find other areas needing more work. */
33 #define BYTES_IN_WORD 2
34 #define BYTES_IN_LONG 4
35 #define ARCH_SIZE 16
36 #undef TARGET_IS_BIG_ENDIAN_P
38 #define TARGET_PAGE_SIZE 1024
39 #define SEGMENT__SIZE TARGET_PAGE_SIZE
41 #define DEFAULT_ARCH bfd_arch_pdp11
42 #define DEFAULT_MID M_PDP11
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)
49 /* This needs to start with a.out so GDB knows it is an a.out variant. */
50 #define TARGETNAME "a.out-pdp11"
52 /* This is the normal load address for executables. */
53 #define TEXT_START_ADDR 0
55 /* The header is not included in the text segment. */
56 #define N_HEADER_IN_TEXT(x) 0
58 /* There are no shared libraries. */
59 #define N_SHARED_LIB(x) 0
61 /* There is no flags field. */
62 #define N_FLAGS(exec) 0
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))
72 #include "bfd.h"
74 #define external_exec pdp11_external_exec
75 struct pdp11_external_exec
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 */
88 #define EXEC_BYTES_SIZE (8 * 2)
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
102 #define A_FLAG_RELOC_STRIPPED 0x0001
104 #define external_nlist pdp11_external_nlist
105 struct pdp11_external_nlist
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 */
114 #define EXTERNAL_NLIST_SIZE 8
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)
123 #define WRITE_HEADERS(abfd, execp) pdp11_aout_write_headers (abfd, execp)
125 #include "sysdep.h"
126 #include "libbfd.h"
127 #include "libaout.h"
129 #define SWAP_MAGIC(ext) bfd_getl16 (ext)
131 #define MY_entry_is_text_address 1
133 #define MY_write_object_contents MY(write_object_contents)
134 static bfd_boolean MY(write_object_contents) PARAMS ((bfd *abfd));
135 #define MY_text_includes_header 1
137 static bfd_vma bfd_getp32 (const void *);
138 static bfd_signed_vma bfd_getp_signed_32 (const void *);
139 static void bfd_putp32 (bfd_vma, void *);
141 #define MY_BFD_TARGET
143 #include "aout-target.h"
145 const bfd_target MY(vec) =
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},
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),
181 /* Alternative_target */
182 NULL,
184 (PTR) MY_backend_data,
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.
193 This file is part of BFD, the Binary File Descriptor library.
195 This program is free software; you can redistribute it and/or modify
196 it under the terms of the GNU General Public License as published by
197 the Free Software Foundation; either version 2 of the License, or
198 (at your option) any later version.
200 This program is distributed in the hope that it will be useful,
201 but WITHOUT ANY WARRANTY; without even the implied warranty of
202 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
203 GNU General Public License for more details.
205 You should have received a copy of the GNU General Public License
206 along with this program; if not, write to the Free Software
207 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
210 SECTION
211 a.out backends
214 DESCRIPTION
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.
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.
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.
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.
239 As an example, this is what goes on to make the back end for a
240 sun4, from @file{aout32.c}:
242 | #define ARCH_SIZE 32
243 | #include "aoutx.h"
245 Which exports names:
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 | ...
254 from @file{sunos.c}:
256 | #define TARGET_NAME "a.out-sunos-big"
257 | #define VECNAME sunos_big_vec
258 | #include "aoutf1.h"
260 requires all the names from @file{aout32.c}, and produces the jump vector
262 | sunos_big_vec
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.
274 When porting it to run on a new system, you must supply:
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
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:
289 | TDEFAULTS = -DDEFAULT_VECTOR=host_aout_big_vec
290 | TDEPFILES= host-aout.o trad-core.o
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.
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.) */
308 #define KEEPIT udata.i
310 #include <string.h> /* For strchr and friends */
311 #include "bfd.h"
312 #include "sysdep.h"
313 #include "safe-ctype.h"
314 #include "bfdlink.h"
316 #include "libaout.h"
317 /*#include "libbfd.h"*/
318 #include "aout/aout64.h"
319 #include "aout/stab_gnu.h"
320 #include "aout/ar.h"
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 */
340 #define N_EXT 0x20 /* external flag */
342 #define RELOC_SIZE 2
344 #define RELFLG 0x0001 /* pc-relative flag */
345 #define RTYPE 0x000e /* type mask */
346 #define RIDXMASK 0xfff0 /* index mask */
348 #define RABS 0x00 /* absolute */
349 #define RTEXT 0x02 /* text */
350 #define RDATA 0x04 /* data */
351 #define RBSS 0x06 /* bss */
352 #define REXT 0x08 /* external */
354 #define RINDEX(x) (((x) & 0xfff0) >> 4)
356 static bfd_boolean aout_get_external_symbols PARAMS ((bfd *));
357 static bfd_boolean translate_from_native_sym_flags
358 PARAMS ((bfd *, aout_symbol_type *));
359 static bfd_boolean translate_to_native_sym_flags
360 PARAMS ((bfd *, asymbol *, struct external_nlist *));
361 static void adjust_o_magic PARAMS ((bfd *, struct internal_exec *));
362 static void adjust_z_magic PARAMS ((bfd *, struct internal_exec *));
363 static void adjust_n_magic PARAMS ((bfd *, struct internal_exec *));
365 static int pdp11_aout_write_headers PARAMS ((bfd *, struct internal_exec *));
366 void pdp11_aout_swap_reloc_out PARAMS ((bfd *, arelent *, bfd_byte *));
367 void pdp11_aout_swap_reloc_in
368 PARAMS ((bfd *, bfd_byte *, arelent *,
369 bfd_size_type, asymbol **, bfd_size_type));
372 SUBSECTION
373 Relocations
375 DESCRIPTION
376 The file @file{aoutx.h} provides for both the @emph{standard}
377 and @emph{extended} forms of a.out relocation records.
379 The standard records contain only an
380 address, a symbol index, and a type field. The extended records
381 (used on 29ks and sparcs) also have a full integer for an
382 addend.
386 #ifndef MY_final_link_relocate
387 #define MY_final_link_relocate _bfd_final_link_relocate
388 #endif
390 #ifndef MY_relocate_contents
391 #define MY_relocate_contents _bfd_relocate_contents
392 #endif
394 reloc_howto_type howto_table_pdp11[] =
396 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
397 HOWTO( 0, 0, 1, 16, FALSE, 0, complain_overflow_signed,0,"16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
398 HOWTO( 1, 0, 1, 16, TRUE, 0, complain_overflow_signed,0,"DISP16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
401 #define TABLE_SIZE(TABLE) (sizeof(TABLE)/sizeof(TABLE[0]))
403 reloc_howto_type *
404 NAME(aout,reloc_type_lookup) (abfd,code)
405 bfd * abfd ATTRIBUTE_UNUSED;
406 bfd_reloc_code_real_type code;
408 switch (code)
410 case BFD_RELOC_16:
411 return &howto_table_pdp11[0];
412 case BFD_RELOC_16_PCREL:
413 return &howto_table_pdp11[1];
414 default:
415 return (reloc_howto_type *)NULL;
419 static int
420 pdp11_aout_write_headers (abfd, execp)
421 bfd *abfd;
422 struct internal_exec *execp;
424 struct external_exec exec_bytes;
425 bfd_size_type text_size;
426 file_ptr text_end;
428 if (adata(abfd).magic == undecided_magic)
429 NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end);
431 execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE;
432 execp->a_entry = bfd_get_start_address (abfd);
434 if (obj_textsec (abfd)->reloc_count > 0 ||
435 obj_datasec (abfd)->reloc_count > 0)
437 execp->a_trsize = execp->a_text;
438 execp->a_drsize = execp->a_data;
440 else
442 execp->a_trsize = 0;
443 execp->a_drsize = 0;
446 NAME(aout,swap_exec_header_out) (abfd, execp, &exec_bytes);
448 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
449 return FALSE;
451 if (bfd_bwrite ((PTR) &exec_bytes, (bfd_size_type) EXEC_BYTES_SIZE, abfd)
452 != EXEC_BYTES_SIZE)
453 return FALSE;
455 /* Now write out reloc info, followed by syms and strings */
457 if (bfd_get_outsymbols (abfd) != (asymbol **) NULL
458 && bfd_get_symcount (abfd) != 0)
460 if (bfd_seek (abfd, (file_ptr) (N_SYMOFF(*execp)), SEEK_SET) != 0)
461 return FALSE;
463 if (! NAME(aout,write_syms) (abfd))
464 return FALSE;
467 if (obj_textsec (abfd)->reloc_count > 0 ||
468 obj_datasec (abfd)->reloc_count > 0)
470 if (bfd_seek (abfd, (file_ptr) (N_TRELOFF(*execp)), SEEK_SET) != 0)
471 return FALSE;
472 if (!NAME(aout,squirt_out_relocs) (abfd, obj_textsec (abfd)))
473 return FALSE;
475 if (bfd_seek (abfd, (file_ptr) (N_DRELOFF(*execp)), SEEK_SET) != 0)
476 return FALSE;
477 if (!NAME(aout,squirt_out_relocs) (abfd, obj_datasec (abfd)))
478 return FALSE;
481 return TRUE;
484 /* Write an object file.
485 Section contents have already been written. We write the
486 file header, symbols, and relocation. */
488 static bfd_boolean
489 MY(write_object_contents) (abfd)
490 bfd *abfd;
492 struct internal_exec *execp = exec_hdr (abfd);
494 /* We must make certain that the magic number has been set. This
495 will normally have been done by set_section_contents, but only if
496 there actually are some section contents. */
497 if (! abfd->output_has_begun)
499 bfd_size_type text_size;
500 file_ptr text_end;
502 NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end);
505 obj_reloc_entry_size (abfd) = RELOC_SIZE;
507 return WRITE_HEADERS(abfd, execp);
511 SUBSECTION
512 Internal entry points
514 DESCRIPTION
515 @file{aoutx.h} exports several routines for accessing the
516 contents of an a.out file, which are gathered and exported in
517 turn by various format specific files (eg sunos.c).
522 FUNCTION
523 aout_@var{size}_swap_exec_header_in
525 SYNOPSIS
526 void aout_@var{size}_swap_exec_header_in,
527 (bfd *abfd,
528 struct external_exec *raw_bytes,
529 struct internal_exec *execp);
531 DESCRIPTION
532 Swap the information in an executable header @var{raw_bytes} taken
533 from a raw byte stream memory image into the internal exec header
534 structure @var{execp}.
537 #ifndef NAME_swap_exec_header_in
538 void
539 NAME(aout,swap_exec_header_in) (abfd, raw_bytes, execp)
540 bfd *abfd;
541 struct external_exec *raw_bytes;
542 struct internal_exec *execp;
544 struct external_exec *bytes = (struct external_exec *)raw_bytes;
546 /* The internal_exec structure has some fields that are unused in this
547 configuration (IE for i960), so ensure that all such uninitialized
548 fields are zero'd out. There are places where two of these structs
549 are memcmp'd, and thus the contents do matter. */
550 memset ((PTR) execp, 0, sizeof (struct internal_exec));
551 /* Now fill in fields in the execp, from the bytes in the raw data. */
552 execp->a_info = GET_MAGIC (abfd, bytes->e_info);
553 execp->a_text = GET_WORD (abfd, bytes->e_text);
554 execp->a_data = GET_WORD (abfd, bytes->e_data);
555 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
556 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
557 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
559 if (GET_WORD (abfd, bytes->e_flag) & A_FLAG_RELOC_STRIPPED)
561 execp->a_trsize = 0;
562 execp->a_drsize = 0;
564 else
566 execp->a_trsize = execp->a_text;
567 execp->a_drsize = execp->a_data;
570 #define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in)
571 #endif
574 FUNCTION
575 aout_@var{size}_swap_exec_header_out
577 SYNOPSIS
578 void aout_@var{size}_swap_exec_header_out
579 (bfd *abfd,
580 struct internal_exec *execp,
581 struct external_exec *raw_bytes);
583 DESCRIPTION
584 Swap the information in an internal exec header structure
585 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
587 void
588 NAME(aout,swap_exec_header_out) (abfd, execp, raw_bytes)
589 bfd *abfd;
590 struct internal_exec *execp;
591 struct external_exec *raw_bytes;
593 struct external_exec *bytes = (struct external_exec *)raw_bytes;
595 /* Now fill in fields in the raw data, from the fields in the exec struct. */
596 PUT_MAGIC (abfd, execp->a_info, bytes->e_info);
597 PUT_WORD (abfd, execp->a_text, bytes->e_text);
598 PUT_WORD (abfd, execp->a_data, bytes->e_data);
599 PUT_WORD (abfd, execp->a_bss, bytes->e_bss);
600 PUT_WORD (abfd, execp->a_syms, bytes->e_syms);
601 PUT_WORD (abfd, execp->a_entry, bytes->e_entry);
602 PUT_WORD (abfd, 0, bytes->e_unused);
604 if ((execp->a_trsize == 0 || execp->a_text == 0) &&
605 (execp->a_drsize == 0 || execp->a_data == 0))
606 PUT_WORD (abfd, A_FLAG_RELOC_STRIPPED, bytes->e_flag);
607 else if (execp->a_trsize == execp->a_text &&
608 execp->a_drsize == execp->a_data)
609 PUT_WORD (abfd, 0, bytes->e_flag);
610 else
612 /* TODO: print a proper warning message */
613 fprintf (stderr, "BFD:%s:%d: internal error\n", __FILE__, __LINE__);
614 PUT_WORD (abfd, 0, bytes->e_flag);
618 /* Make all the section for an a.out file. */
620 bfd_boolean
621 NAME(aout,make_sections) (abfd)
622 bfd *abfd;
624 if (obj_textsec (abfd) == (asection *) NULL
625 && bfd_make_section (abfd, ".text") == (asection *) NULL)
626 return FALSE;
627 if (obj_datasec (abfd) == (asection *) NULL
628 && bfd_make_section (abfd, ".data") == (asection *) NULL)
629 return FALSE;
630 if (obj_bsssec (abfd) == (asection *) NULL
631 && bfd_make_section (abfd, ".bss") == (asection *) NULL)
632 return FALSE;
633 return TRUE;
637 FUNCTION
638 aout_@var{size}_some_aout_object_p
640 SYNOPSIS
641 const bfd_target *aout_@var{size}_some_aout_object_p
642 (bfd *abfd,
643 const bfd_target *(*callback_to_real_object_p)());
645 DESCRIPTION
646 Some a.out variant thinks that the file open in @var{abfd}
647 checking is an a.out file. Do some more checking, and set up
648 for access if it really is. Call back to the calling
649 environment's "finish up" function just before returning, to
650 handle any last-minute setup.
653 const bfd_target *
654 NAME(aout,some_aout_object_p) (abfd, execp, callback_to_real_object_p)
655 bfd *abfd;
656 struct internal_exec *execp;
657 const bfd_target *(*callback_to_real_object_p) PARAMS ((bfd *));
659 struct aout_data_struct *rawptr, *oldrawptr;
660 const bfd_target *result;
661 bfd_size_type amt = sizeof (struct aout_data_struct);
663 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
664 if (rawptr == NULL)
665 return 0;
667 oldrawptr = abfd->tdata.aout_data;
668 abfd->tdata.aout_data = rawptr;
670 /* Copy the contents of the old tdata struct.
671 In particular, we want the subformat, since for hpux it was set in
672 hp300hpux.c:swap_exec_header_in and will be used in
673 hp300hpux.c:callback. */
674 if (oldrawptr != NULL)
675 *abfd->tdata.aout_data = *oldrawptr;
677 abfd->tdata.aout_data->a.hdr = &rawptr->e;
678 *(abfd->tdata.aout_data->a.hdr) = *execp; /* Copy in the internal_exec struct */
679 execp = abfd->tdata.aout_data->a.hdr;
681 /* Set the file flags */
682 abfd->flags = BFD_NO_FLAGS;
683 if (execp->a_drsize || execp->a_trsize)
684 abfd->flags |= HAS_RELOC;
685 /* Setting of EXEC_P has been deferred to the bottom of this function */
686 if (execp->a_syms)
687 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
688 if (N_DYNAMIC(*execp))
689 abfd->flags |= DYNAMIC;
691 if (N_MAGIC (*execp) == ZMAGIC)
693 abfd->flags |= D_PAGED | WP_TEXT;
694 adata (abfd).magic = z_magic;
696 else if (N_MAGIC (*execp) == QMAGIC)
698 abfd->flags |= D_PAGED | WP_TEXT;
699 adata (abfd).magic = z_magic;
700 adata (abfd).subformat = q_magic_format;
702 else if (N_MAGIC (*execp) == NMAGIC)
704 abfd->flags |= WP_TEXT;
705 adata (abfd).magic = n_magic;
707 else if (N_MAGIC (*execp) == OMAGIC
708 || N_MAGIC (*execp) == BMAGIC)
709 adata (abfd).magic = o_magic;
710 else
712 /* Should have been checked with N_BADMAG before this routine
713 was called. */
714 abort ();
717 bfd_get_start_address (abfd) = execp->a_entry;
719 obj_aout_symbols (abfd) = (aout_symbol_type *)NULL;
720 bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
722 /* The default relocation entry size is that of traditional V7 Unix. */
723 obj_reloc_entry_size (abfd) = RELOC_SIZE;
725 /* The default symbol entry size is that of traditional Unix. */
726 obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
728 #ifdef USE_MMAP
729 bfd_init_window (&obj_aout_sym_window (abfd));
730 bfd_init_window (&obj_aout_string_window (abfd));
731 #endif
732 obj_aout_external_syms (abfd) = NULL;
733 obj_aout_external_strings (abfd) = NULL;
734 obj_aout_sym_hashes (abfd) = NULL;
736 if (! NAME(aout,make_sections) (abfd))
737 return NULL;
739 obj_datasec (abfd)->size = execp->a_data;
740 obj_bsssec (abfd)->size = execp->a_bss;
742 obj_textsec (abfd)->flags =
743 (execp->a_trsize != 0
744 ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
745 : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
746 obj_datasec (abfd)->flags =
747 (execp->a_drsize != 0
748 ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
749 : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
750 obj_bsssec (abfd)->flags = SEC_ALLOC;
752 #ifdef THIS_IS_ONLY_DOCUMENTATION
753 /* The common code can't fill in these things because they depend
754 on either the start address of the text segment, the rounding
755 up of virtual addresses between segments, or the starting file
756 position of the text segment -- all of which varies among different
757 versions of a.out. */
759 /* Call back to the format-dependent code to fill in the rest of the
760 fields and do any further cleanup. Things that should be filled
761 in by the callback: */
763 struct exec *execp = exec_hdr (abfd);
765 obj_textsec (abfd)->size = N_TXTSIZE(*execp);
766 /* data and bss are already filled in since they're so standard */
768 /* The virtual memory addresses of the sections */
769 obj_textsec (abfd)->vma = N_TXTADDR(*execp);
770 obj_datasec (abfd)->vma = N_DATADDR(*execp);
771 obj_bsssec (abfd)->vma = N_BSSADDR(*execp);
773 /* The file offsets of the sections */
774 obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
775 obj_datasec (abfd)->filepos = N_DATOFF(*execp);
777 /* The file offsets of the relocation info */
778 obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);
779 obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);
781 /* The file offsets of the string table and symbol table. */
782 obj_str_filepos (abfd) = N_STROFF (*execp);
783 obj_sym_filepos (abfd) = N_SYMOFF (*execp);
785 /* Determine the architecture and machine type of the object file. */
786 abfd->obj_arch = bfd_arch_obscure;
788 adata(abfd)->page_size = TARGET_PAGE_SIZE;
789 adata(abfd)->segment_size = SEGMENT_SIZE;
790 adata(abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
792 return abfd->xvec;
794 /* The architecture is encoded in various ways in various a.out variants,
795 or is not encoded at all in some of them. The relocation size depends
796 on the architecture and the a.out variant. Finally, the return value
797 is the bfd_target vector in use. If an error occurs, return zero and
798 set bfd_error to the appropriate error code.
800 Formats such as b.out, which have additional fields in the a.out
801 header, should cope with them in this callback as well. */
802 #endif /* DOCUMENTATION */
804 result = (*callback_to_real_object_p)(abfd);
806 /* Now that the segment addresses have been worked out, take a better
807 guess at whether the file is executable. If the entry point
808 is within the text segment, assume it is. (This makes files
809 executable even if their entry point address is 0, as long as
810 their text starts at zero.).
812 This test had to be changed to deal with systems where the text segment
813 runs at a different location than the default. The problem is that the
814 entry address can appear to be outside the text segment, thus causing an
815 erroneous conclusion that the file isn't executable.
817 To fix this, we now accept any non-zero entry point as an indication of
818 executability. This will work most of the time, since only the linker
819 sets the entry point, and that is likely to be non-zero for most systems. */
821 if (execp->a_entry != 0
822 || (execp->a_entry >= obj_textsec(abfd)->vma
823 && execp->a_entry < obj_textsec(abfd)->vma + obj_textsec(abfd)->size))
824 abfd->flags |= EXEC_P;
825 #ifdef STAT_FOR_EXEC
826 else
828 struct stat stat_buf;
830 /* The original heuristic doesn't work in some important cases.
831 The a.out file has no information about the text start
832 address. For files (like kernels) linked to non-standard
833 addresses (ld -Ttext nnn) the entry point may not be between
834 the default text start (obj_textsec(abfd)->vma) and
835 (obj_textsec(abfd)->vma) + text size. This is not just a mach
836 issue. Many kernels are loaded at non standard addresses. */
837 if (abfd->iostream != NULL
838 && (abfd->flags & BFD_IN_MEMORY) == 0
839 && (fstat(fileno((FILE *) (abfd->iostream)), &stat_buf) == 0)
840 && ((stat_buf.st_mode & 0111) != 0))
841 abfd->flags |= EXEC_P;
843 #endif /* STAT_FOR_EXEC */
845 if (!result)
847 free (rawptr);
848 abfd->tdata.aout_data = oldrawptr;
850 return result;
854 FUNCTION
855 aout_@var{size}_mkobject
857 SYNOPSIS
858 bfd_boolean aout_@var{size}_mkobject, (bfd *abfd);
860 DESCRIPTION
861 Initialize BFD @var{abfd} for use with a.out files.
864 bfd_boolean
865 NAME(aout,mkobject) (abfd)
866 bfd *abfd;
868 struct aout_data_struct *rawptr;
869 bfd_size_type amt = sizeof (struct aout_data_struct);
871 bfd_set_error (bfd_error_system_call);
873 /* Use an intermediate variable for clarity */
874 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
876 if (rawptr == NULL)
877 return FALSE;
879 abfd->tdata.aout_data = rawptr;
880 exec_hdr (abfd) = &(rawptr->e);
882 obj_textsec (abfd) = (asection *)NULL;
883 obj_datasec (abfd) = (asection *)NULL;
884 obj_bsssec (abfd) = (asection *)NULL;
886 return TRUE;
891 FUNCTION
892 aout_@var{size}_machine_type
894 SYNOPSIS
895 enum machine_type aout_@var{size}_machine_type
896 (enum bfd_architecture arch,
897 unsigned long machine));
899 DESCRIPTION
900 Keep track of machine architecture and machine type for
901 a.out's. Return the <<machine_type>> for a particular
902 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
903 and machine can't be represented in a.out format.
905 If the architecture is understood, machine type 0 (default)
906 is always understood.
909 enum machine_type
910 NAME(aout,machine_type) (arch, machine, unknown)
911 enum bfd_architecture arch;
912 unsigned long machine;
913 bfd_boolean *unknown;
915 enum machine_type arch_flags;
917 arch_flags = M_UNKNOWN;
918 *unknown = TRUE;
920 switch (arch)
922 case bfd_arch_sparc:
923 if (machine == 0
924 || machine == bfd_mach_sparc
925 || machine == bfd_mach_sparc_sparclite
926 || machine == bfd_mach_sparc_v9)
927 arch_flags = M_SPARC;
928 else if (machine == bfd_mach_sparc_sparclet)
929 arch_flags = M_SPARCLET;
930 break;
932 case bfd_arch_m68k:
933 switch (machine)
935 case 0: arch_flags = M_68010; break;
936 case bfd_mach_m68000: arch_flags = M_UNKNOWN; *unknown = FALSE; break;
937 case bfd_mach_m68010: arch_flags = M_68010; break;
938 case bfd_mach_m68020: arch_flags = M_68020; break;
939 default: arch_flags = M_UNKNOWN; break;
941 break;
943 case bfd_arch_i386:
944 if (machine == 0
945 || machine == bfd_mach_i386_i386
946 || machine == bfd_mach_i386_i386_intel_syntax)
947 arch_flags = M_386;
948 break;
950 case bfd_arch_a29k:
951 if (machine == 0) arch_flags = M_29K;
952 break;
954 case bfd_arch_arm:
955 if (machine == 0) arch_flags = M_ARM;
956 break;
958 case bfd_arch_mips:
959 switch (machine)
961 case 0:
962 case 2000:
963 case bfd_mach_mips3000:
964 arch_flags = M_MIPS1;
965 break;
966 case bfd_mach_mips4000: /* mips3 */
967 case bfd_mach_mips4400:
968 case bfd_mach_mips8000: /* mips4 */
969 case bfd_mach_mips6000: /* real mips2: */
970 arch_flags = M_MIPS2;
971 break;
972 default:
973 arch_flags = M_UNKNOWN;
974 break;
976 break;
978 case bfd_arch_ns32k:
979 switch (machine)
981 case 0: arch_flags = M_NS32532; break;
982 case 32032: arch_flags = M_NS32032; break;
983 case 32532: arch_flags = M_NS32532; break;
984 default: arch_flags = M_UNKNOWN; break;
986 break;
988 case bfd_arch_pdp11:
989 /* TODO: arch_flags = M_PDP11; */
990 *unknown = FALSE;
991 break;
993 case bfd_arch_vax:
994 *unknown = FALSE;
995 break;
997 default:
998 arch_flags = M_UNKNOWN;
1001 if (arch_flags != M_UNKNOWN)
1002 *unknown = FALSE;
1004 return arch_flags;
1009 FUNCTION
1010 aout_@var{size}_set_arch_mach
1012 SYNOPSIS
1013 bfd_boolean aout_@var{size}_set_arch_mach,
1014 (bfd *,
1015 enum bfd_architecture arch,
1016 unsigned long machine));
1018 DESCRIPTION
1019 Set the architecture and the machine of the BFD @var{abfd} to the
1020 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
1021 can support the architecture required.
1024 bfd_boolean
1025 NAME(aout,set_arch_mach) (abfd, arch, machine)
1026 bfd *abfd;
1027 enum bfd_architecture arch;
1028 unsigned long machine;
1030 if (! bfd_default_set_arch_mach (abfd, arch, machine))
1031 return FALSE;
1033 if (arch != bfd_arch_unknown)
1035 bfd_boolean unknown;
1037 NAME(aout,machine_type) (arch, machine, &unknown);
1038 if (unknown)
1039 return FALSE;
1042 obj_reloc_entry_size (abfd) = RELOC_SIZE;
1044 return (*aout_backend_info(abfd)->set_sizes) (abfd);
1047 static void
1048 adjust_o_magic (abfd, execp)
1049 bfd *abfd;
1050 struct internal_exec *execp;
1052 file_ptr pos = adata (abfd).exec_bytes_size;
1053 bfd_vma vma = 0;
1054 int pad = 0;
1056 /* Text. */
1057 obj_textsec (abfd)->filepos = pos;
1058 if (! obj_textsec (abfd)->user_set_vma)
1059 obj_textsec (abfd)->vma = vma;
1060 else
1061 vma = obj_textsec (abfd)->vma;
1063 pos += obj_textsec (abfd)->size;
1064 vma += obj_textsec (abfd)->size;
1066 /* Data. */
1067 if (!obj_datasec (abfd)->user_set_vma)
1069 obj_textsec (abfd)->size += pad;
1070 pos += pad;
1071 vma += pad;
1072 obj_datasec (abfd)->vma = vma;
1074 else
1075 vma = obj_datasec (abfd)->vma;
1076 obj_datasec (abfd)->filepos = pos;
1077 pos += obj_datasec (abfd)->size;
1078 vma += obj_datasec (abfd)->size;
1080 /* BSS. */
1081 if (! obj_bsssec (abfd)->user_set_vma)
1083 obj_datasec (abfd)->size += pad;
1084 pos += pad;
1085 vma += pad;
1086 obj_bsssec (abfd)->vma = vma;
1088 else
1090 /* The VMA of the .bss section is set by the VMA of the
1091 .data section plus the size of the .data section. We may
1092 need to add padding bytes to make this true. */
1093 pad = obj_bsssec (abfd)->vma - vma;
1094 if (pad > 0)
1096 obj_datasec (abfd)->size += pad;
1097 pos += pad;
1100 obj_bsssec (abfd)->filepos = pos;
1102 /* Fix up the exec header. */
1103 execp->a_text = obj_textsec (abfd)->size;
1104 execp->a_data = obj_datasec (abfd)->size;
1105 execp->a_bss = obj_bsssec (abfd)->size;
1106 N_SET_MAGIC (*execp, OMAGIC);
1109 static void
1110 adjust_z_magic (abfd, execp)
1111 bfd *abfd;
1112 struct internal_exec *execp;
1114 bfd_size_type data_pad, text_pad;
1115 file_ptr text_end;
1116 const struct aout_backend_data *abdp;
1117 int ztih; /* Nonzero if text includes exec header. */
1119 abdp = aout_backend_info (abfd);
1121 /* Text. */
1122 ztih = (abdp != NULL
1123 && (abdp->text_includes_header
1124 || obj_aout_subformat (abfd) == q_magic_format));
1125 obj_textsec(abfd)->filepos = (ztih
1126 ? adata(abfd).exec_bytes_size
1127 : adata(abfd).zmagic_disk_block_size);
1128 if (! obj_textsec(abfd)->user_set_vma)
1130 /* ?? Do we really need to check for relocs here? */
1131 obj_textsec(abfd)->vma = ((abfd->flags & HAS_RELOC)
1133 : (ztih
1134 ? (abdp->default_text_vma
1135 + adata (abfd).exec_bytes_size)
1136 : abdp->default_text_vma));
1137 text_pad = 0;
1139 else
1141 /* The .text section is being loaded at an unusual address. We
1142 may need to pad it such that the .data section starts at a page
1143 boundary. */
1144 if (ztih)
1145 text_pad = ((obj_textsec (abfd)->filepos - obj_textsec (abfd)->vma)
1146 & (adata (abfd).page_size - 1));
1147 else
1148 text_pad = ((- obj_textsec (abfd)->vma)
1149 & (adata (abfd).page_size - 1));
1152 /* Find start of data. */
1153 if (ztih)
1155 text_end = obj_textsec (abfd)->filepos + obj_textsec (abfd)->size;
1156 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
1158 else
1160 /* Note that if page_size == zmagic_disk_block_size, then
1161 filepos == page_size, and this case is the same as the ztih
1162 case. */
1163 text_end = obj_textsec (abfd)->size;
1164 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
1165 text_end += obj_textsec (abfd)->filepos;
1168 obj_textsec (abfd)->size += text_pad;
1169 text_end += text_pad;
1171 /* Data. */
1172 if (!obj_datasec(abfd)->user_set_vma)
1174 bfd_vma vma;
1175 vma = obj_textsec(abfd)->vma + obj_textsec(abfd)->size;
1176 obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
1178 if (abdp && abdp->zmagic_mapped_contiguous)
1180 text_pad = (obj_datasec(abfd)->vma
1181 - obj_textsec(abfd)->vma
1182 - obj_textsec(abfd)->size);
1183 obj_textsec(abfd)->size += text_pad;
1185 obj_datasec (abfd)->filepos = (obj_textsec (abfd)->filepos
1186 + obj_textsec (abfd)->size);
1188 /* Fix up exec header while we're at it. */
1189 execp->a_text = obj_textsec(abfd)->size;
1190 if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
1191 execp->a_text += adata(abfd).exec_bytes_size;
1192 if (obj_aout_subformat (abfd) == q_magic_format)
1193 N_SET_MAGIC (*execp, QMAGIC);
1194 else
1195 N_SET_MAGIC (*execp, ZMAGIC);
1197 /* Spec says data section should be rounded up to page boundary. */
1198 obj_datasec(abfd)->size
1199 = align_power (obj_datasec(abfd)->size,
1200 obj_bsssec(abfd)->alignment_power);
1201 execp->a_data = BFD_ALIGN (obj_datasec(abfd)->size,
1202 adata(abfd).page_size);
1203 data_pad = execp->a_data - obj_datasec(abfd)->size;
1205 /* BSS. */
1206 if (!obj_bsssec(abfd)->user_set_vma)
1207 obj_bsssec(abfd)->vma = (obj_datasec(abfd)->vma
1208 + obj_datasec(abfd)->size);
1209 /* If the BSS immediately follows the data section and extra space
1210 in the page is left after the data section, fudge data
1211 in the header so that the bss section looks smaller by that
1212 amount. We'll start the bss section there, and lie to the OS.
1213 (Note that a linker script, as well as the above assignment,
1214 could have explicitly set the BSS vma to immediately follow
1215 the data section.) */
1216 if (align_power (obj_bsssec(abfd)->vma, obj_bsssec(abfd)->alignment_power)
1217 == obj_datasec(abfd)->vma + obj_datasec(abfd)->size)
1218 execp->a_bss = (data_pad > obj_bsssec(abfd)->size) ? 0 :
1219 obj_bsssec(abfd)->size - data_pad;
1220 else
1221 execp->a_bss = obj_bsssec(abfd)->size;
1224 static void
1225 adjust_n_magic (abfd, execp)
1226 bfd *abfd;
1227 struct internal_exec *execp;
1229 file_ptr pos = adata(abfd).exec_bytes_size;
1230 bfd_vma vma = 0;
1231 int pad;
1233 /* Text. */
1234 obj_textsec(abfd)->filepos = pos;
1235 if (!obj_textsec(abfd)->user_set_vma)
1236 obj_textsec(abfd)->vma = vma;
1237 else
1238 vma = obj_textsec(abfd)->vma;
1239 pos += obj_textsec(abfd)->size;
1240 vma += obj_textsec(abfd)->size;
1242 /* Data. */
1243 obj_datasec(abfd)->filepos = pos;
1244 if (!obj_datasec(abfd)->user_set_vma)
1245 obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
1246 vma = obj_datasec(abfd)->vma;
1248 /* Since BSS follows data immediately, see if it needs alignment. */
1249 vma += obj_datasec(abfd)->size;
1250 pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma;
1251 obj_datasec(abfd)->size += pad;
1252 pos += obj_datasec(abfd)->size;
1254 /* BSS. */
1255 if (!obj_bsssec(abfd)->user_set_vma)
1256 obj_bsssec(abfd)->vma = vma;
1257 else
1258 vma = obj_bsssec(abfd)->vma;
1260 /* Fix up exec header. */
1261 execp->a_text = obj_textsec(abfd)->size;
1262 execp->a_data = obj_datasec(abfd)->size;
1263 execp->a_bss = obj_bsssec(abfd)->size;
1264 N_SET_MAGIC (*execp, NMAGIC);
1267 bfd_boolean
1268 NAME(aout,adjust_sizes_and_vmas) (abfd, text_size, text_end)
1269 bfd *abfd;
1270 bfd_size_type *text_size;
1271 file_ptr * text_end ATTRIBUTE_UNUSED;
1273 struct internal_exec *execp = exec_hdr (abfd);
1275 if (! NAME(aout,make_sections) (abfd))
1276 return FALSE;
1278 if (adata(abfd).magic != undecided_magic)
1279 return TRUE;
1281 obj_textsec(abfd)->size =
1282 align_power(obj_textsec(abfd)->size,
1283 obj_textsec(abfd)->alignment_power);
1285 *text_size = obj_textsec (abfd)->size;
1286 /* Rule (heuristic) for when to pad to a new page. Note that there
1287 are (at least) two ways demand-paged (ZMAGIC) files have been
1288 handled. Most Berkeley-based systems start the text segment at
1289 (TARGET_PAGE_SIZE). However, newer versions of SUNOS start the text
1290 segment right after the exec header; the latter is counted in the
1291 text segment size, and is paged in by the kernel with the rest of
1292 the text. */
1294 /* This perhaps isn't the right way to do this, but made it simpler for me
1295 to understand enough to implement it. Better would probably be to go
1296 right from BFD flags to alignment/positioning characteristics. But the
1297 old code was sloppy enough about handling the flags, and had enough
1298 other magic, that it was a little hard for me to understand. I think
1299 I understand it better now, but I haven't time to do the cleanup this
1300 minute. */
1302 if (abfd->flags & WP_TEXT)
1303 adata(abfd).magic = n_magic;
1304 else
1305 adata(abfd).magic = o_magic;
1307 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
1308 #if __GNUC__ >= 2
1309 fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
1310 ({ char *str;
1311 switch (adata(abfd).magic) {
1312 case n_magic: str = "NMAGIC"; break;
1313 case o_magic: str = "OMAGIC"; break;
1314 case z_magic: str = "ZMAGIC"; break;
1315 default: abort ();
1317 str;
1319 obj_textsec(abfd)->vma, obj_textsec(abfd)->size,
1320 obj_textsec(abfd)->alignment_power,
1321 obj_datasec(abfd)->vma, obj_datasec(abfd)->size,
1322 obj_datasec(abfd)->alignment_power,
1323 obj_bsssec(abfd)->vma, obj_bsssec(abfd)->size,
1324 obj_bsssec(abfd)->alignment_power);
1325 #endif
1326 #endif
1328 switch (adata(abfd).magic)
1330 case o_magic:
1331 adjust_o_magic (abfd, execp);
1332 break;
1333 case z_magic:
1334 adjust_z_magic (abfd, execp);
1335 break;
1336 case n_magic:
1337 adjust_n_magic (abfd, execp);
1338 break;
1339 default:
1340 abort ();
1343 #ifdef BFD_AOUT_DEBUG
1344 fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
1345 obj_textsec(abfd)->vma, obj_textsec(abfd)->size,
1346 obj_textsec(abfd)->filepos,
1347 obj_datasec(abfd)->vma, obj_datasec(abfd)->size,
1348 obj_datasec(abfd)->filepos,
1349 obj_bsssec(abfd)->vma, obj_bsssec(abfd)->size);
1350 #endif
1352 return TRUE;
1356 FUNCTION
1357 aout_@var{size}_new_section_hook
1359 SYNOPSIS
1360 bfd_boolean aout_@var{size}_new_section_hook,
1361 (bfd *abfd,
1362 asection *newsect));
1364 DESCRIPTION
1365 Called by the BFD in response to a @code{bfd_make_section}
1366 request.
1368 bfd_boolean
1369 NAME(aout,new_section_hook) (abfd, newsect)
1370 bfd *abfd;
1371 asection *newsect;
1373 /* align to double at least */
1374 newsect->alignment_power = bfd_get_arch_info(abfd)->section_align_power;
1377 if (bfd_get_format (abfd) == bfd_object)
1379 if (obj_textsec (abfd) == NULL
1380 && ! strcmp (newsect->name, ".text"))
1382 obj_textsec(abfd)= newsect;
1383 newsect->target_index = N_TEXT;
1384 return TRUE;
1387 if (obj_datasec (abfd) == NULL
1388 && ! strcmp (newsect->name, ".data"))
1390 obj_datasec (abfd) = newsect;
1391 newsect->target_index = N_DATA;
1392 return TRUE;
1395 if (obj_bsssec (abfd) == NULL
1396 && !strcmp (newsect->name, ".bss"))
1398 obj_bsssec (abfd) = newsect;
1399 newsect->target_index = N_BSS;
1400 return TRUE;
1404 /* We allow more than three sections internally */
1405 return TRUE;
1408 bfd_boolean
1409 NAME(aout,set_section_contents) (abfd, section, location, offset, count)
1410 bfd *abfd;
1411 sec_ptr section;
1412 const PTR location;
1413 file_ptr offset;
1414 bfd_size_type count;
1416 file_ptr text_end;
1417 bfd_size_type text_size;
1419 if (! abfd->output_has_begun)
1421 if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
1422 return FALSE;
1425 if (section == obj_bsssec (abfd))
1427 bfd_set_error (bfd_error_no_contents);
1428 return FALSE;
1431 if (section != obj_textsec (abfd)
1432 && section != obj_datasec (abfd))
1434 (*_bfd_error_handler)
1435 ("%s: can not represent section `%s' in a.out object file format",
1436 bfd_get_filename (abfd), bfd_get_section_name (abfd, section));
1437 bfd_set_error (bfd_error_nonrepresentable_section);
1438 return FALSE;
1441 if (count != 0)
1443 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
1444 || bfd_bwrite (location, count, abfd) != count)
1445 return FALSE;
1448 return TRUE;
1451 /* Read the external symbols from an a.out file. */
1453 static bfd_boolean
1454 aout_get_external_symbols (abfd)
1455 bfd *abfd;
1457 if (obj_aout_external_syms (abfd) == (struct external_nlist *) NULL)
1459 bfd_size_type count;
1460 struct external_nlist *syms;
1462 count = exec_hdr (abfd)->a_syms / EXTERNAL_NLIST_SIZE;
1464 #ifdef USE_MMAP
1465 if (! bfd_get_file_window (abfd, obj_sym_filepos (abfd),
1466 exec_hdr (abfd)->a_syms,
1467 &obj_aout_sym_window (abfd), TRUE))
1468 return FALSE;
1469 syms = (struct external_nlist *) obj_aout_sym_window (abfd).data;
1470 #else
1471 /* We allocate using malloc to make the values easy to free
1472 later on. If we put them on the objalloc it might not be
1473 possible to free them. */
1474 syms = (struct external_nlist *) bfd_malloc (count * EXTERNAL_NLIST_SIZE);
1475 if (syms == (struct external_nlist *) NULL && count != 0)
1476 return FALSE;
1478 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1479 || (bfd_bread (syms, exec_hdr (abfd)->a_syms, abfd)
1480 != exec_hdr (abfd)->a_syms))
1482 free (syms);
1483 return FALSE;
1485 #endif
1487 obj_aout_external_syms (abfd) = syms;
1488 obj_aout_external_sym_count (abfd) = count;
1491 if (obj_aout_external_strings (abfd) == NULL
1492 && exec_hdr (abfd)->a_syms != 0)
1494 unsigned char string_chars[BYTES_IN_LONG];
1495 bfd_size_type stringsize;
1496 char *strings;
1498 /* Get the size of the strings. */
1499 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
1500 || (bfd_bread ((PTR) string_chars, (bfd_size_type) BYTES_IN_LONG,
1501 abfd) != BYTES_IN_LONG))
1502 return FALSE;
1503 stringsize = H_GET_32 (abfd, string_chars);
1505 #ifdef USE_MMAP
1506 if (! bfd_get_file_window (abfd, obj_str_filepos (abfd), stringsize,
1507 &obj_aout_string_window (abfd), TRUE))
1508 return FALSE;
1509 strings = (char *) obj_aout_string_window (abfd).data;
1510 #else
1511 strings = (char *) bfd_malloc (stringsize + 1);
1512 if (strings == NULL)
1513 return FALSE;
1515 /* Skip space for the string count in the buffer for convenience
1516 when using indexes. */
1517 if (bfd_bread (strings + 4, stringsize - 4, abfd) != stringsize - 4)
1519 free (strings);
1520 return FALSE;
1522 #endif
1524 /* Ensure that a zero index yields an empty string. */
1525 strings[0] = '\0';
1527 strings[stringsize - 1] = 0;
1529 obj_aout_external_strings (abfd) = strings;
1530 obj_aout_external_string_size (abfd) = stringsize;
1533 return TRUE;
1536 /* Translate an a.out symbol into a BFD symbol. The desc, other, type
1537 and symbol->value fields of CACHE_PTR will be set from the a.out
1538 nlist structure. This function is responsible for setting
1539 symbol->flags and symbol->section, and adjusting symbol->value. */
1541 static bfd_boolean
1542 translate_from_native_sym_flags (abfd, cache_ptr)
1543 bfd *abfd;
1544 aout_symbol_type *cache_ptr;
1546 flagword visible;
1548 if (cache_ptr->type == N_FN)
1550 asection *sec;
1552 /* This is a debugging symbol. */
1554 cache_ptr->symbol.flags = BSF_DEBUGGING;
1556 /* Work out the symbol section. */
1557 switch (cache_ptr->type & N_TYPE)
1559 case N_TEXT:
1560 case N_FN:
1561 sec = obj_textsec (abfd);
1562 break;
1563 case N_DATA:
1564 sec = obj_datasec (abfd);
1565 break;
1566 case N_BSS:
1567 sec = obj_bsssec (abfd);
1568 break;
1569 default:
1570 case N_ABS:
1571 sec = bfd_abs_section_ptr;
1572 break;
1575 cache_ptr->symbol.section = sec;
1576 cache_ptr->symbol.value -= sec->vma;
1578 return TRUE;
1581 /* Get the default visibility. This does not apply to all types, so
1582 we just hold it in a local variable to use if wanted. */
1583 if ((cache_ptr->type & N_EXT) == 0)
1584 visible = BSF_LOCAL;
1585 else
1586 visible = BSF_GLOBAL;
1588 switch (cache_ptr->type)
1590 default:
1591 case N_ABS: case N_ABS | N_EXT:
1592 cache_ptr->symbol.section = bfd_abs_section_ptr;
1593 cache_ptr->symbol.flags = visible;
1594 break;
1596 case N_UNDF | N_EXT:
1597 if (cache_ptr->symbol.value != 0)
1599 /* This is a common symbol. */
1600 cache_ptr->symbol.flags = BSF_GLOBAL;
1601 cache_ptr->symbol.section = bfd_com_section_ptr;
1603 else
1605 cache_ptr->symbol.flags = 0;
1606 cache_ptr->symbol.section = bfd_und_section_ptr;
1608 break;
1610 case N_TEXT: case N_TEXT | N_EXT:
1611 cache_ptr->symbol.section = obj_textsec (abfd);
1612 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1613 cache_ptr->symbol.flags = visible;
1614 break;
1616 case N_DATA: case N_DATA | N_EXT:
1617 cache_ptr->symbol.section = obj_datasec (abfd);
1618 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1619 cache_ptr->symbol.flags = visible;
1620 break;
1622 case N_BSS: case N_BSS | N_EXT:
1623 cache_ptr->symbol.section = obj_bsssec (abfd);
1624 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1625 cache_ptr->symbol.flags = visible;
1626 break;
1629 return TRUE;
1632 /* Set the fields of SYM_POINTER according to CACHE_PTR. */
1634 static bfd_boolean
1635 translate_to_native_sym_flags (abfd, cache_ptr, sym_pointer)
1636 bfd *abfd;
1637 asymbol *cache_ptr;
1638 struct external_nlist *sym_pointer;
1640 bfd_vma value = cache_ptr->value;
1641 asection *sec;
1642 bfd_vma off;
1644 /* Mask out any existing type bits in case copying from one section
1645 to another. */
1646 sym_pointer->e_type[0] &= ~N_TYPE;
1648 sec = bfd_get_section (cache_ptr);
1649 off = 0;
1651 if (sec == NULL)
1653 /* This case occurs, e.g., for the *DEBUG* section of a COFF
1654 file. */
1655 (*_bfd_error_handler)
1656 ("%B: can not represent section for symbol `%s' in a.out object file format",
1657 abfd, cache_ptr->name != NULL ? cache_ptr->name : "*unknown*");
1658 bfd_set_error (bfd_error_nonrepresentable_section);
1659 return FALSE;
1662 if (sec->output_section != NULL)
1664 off = sec->output_offset;
1665 sec = sec->output_section;
1668 if (bfd_is_abs_section (sec))
1669 sym_pointer->e_type[0] |= N_ABS;
1670 else if (sec == obj_textsec (abfd))
1671 sym_pointer->e_type[0] |= N_TEXT;
1672 else if (sec == obj_datasec (abfd))
1673 sym_pointer->e_type[0] |= N_DATA;
1674 else if (sec == obj_bsssec (abfd))
1675 sym_pointer->e_type[0] |= N_BSS;
1676 else if (bfd_is_und_section (sec))
1677 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1678 else if (bfd_is_com_section (sec))
1679 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1680 else
1682 (*_bfd_error_handler)
1683 ("%B: can not represent section `%A' in a.out object file format",
1684 abfd, sec);
1685 bfd_set_error (bfd_error_nonrepresentable_section);
1686 return FALSE;
1689 /* Turn the symbol from section relative to absolute again */
1690 value += sec->vma + off;
1692 if ((cache_ptr->flags & BSF_DEBUGGING) != 0)
1693 sym_pointer->e_type[0] = ((aout_symbol_type *) cache_ptr)->type;
1694 else if ((cache_ptr->flags & BSF_GLOBAL) != 0)
1695 sym_pointer->e_type[0] |= N_EXT;
1697 PUT_WORD(abfd, value, sym_pointer->e_value);
1699 return TRUE;
1702 /* Native-level interface to symbols. */
1704 asymbol *
1705 NAME(aout,make_empty_symbol) (abfd)
1706 bfd *abfd;
1708 bfd_size_type amt = sizeof (aout_symbol_type);
1709 aout_symbol_type *new = (aout_symbol_type *) bfd_zalloc (abfd, amt);
1710 if (!new)
1711 return NULL;
1712 new->symbol.the_bfd = abfd;
1714 return &new->symbol;
1717 /* Translate a set of internal symbols into external symbols. */
1719 bfd_boolean
1720 NAME(aout,translate_symbol_table) (abfd, in, ext, count, str, strsize, dynamic)
1721 bfd *abfd;
1722 aout_symbol_type *in;
1723 struct external_nlist *ext;
1724 bfd_size_type count;
1725 char *str;
1726 bfd_size_type strsize;
1727 bfd_boolean dynamic;
1729 struct external_nlist *ext_end;
1731 ext_end = ext + count;
1732 for (; ext < ext_end; ext++, in++)
1734 bfd_vma x;
1736 x = GET_WORD (abfd, ext->e_strx);
1737 in->symbol.the_bfd = abfd;
1739 /* For the normal symbols, the zero index points at the number
1740 of bytes in the string table but is to be interpreted as the
1741 null string. For the dynamic symbols, the number of bytes in
1742 the string table is stored in the __DYNAMIC structure and the
1743 zero index points at an actual string. */
1744 if (x == 0 && ! dynamic)
1745 in->symbol.name = "";
1746 else if (x < strsize)
1747 in->symbol.name = str + x;
1748 else
1749 return FALSE;
1751 in->symbol.value = GET_SWORD (abfd, ext->e_value);
1752 /* TODO: is 0 a safe value here? */
1753 in->desc = 0;
1754 in->other = 0;
1755 in->type = H_GET_8 (abfd, ext->e_type);
1756 in->symbol.udata.p = NULL;
1758 if (! translate_from_native_sym_flags (abfd, in))
1759 return FALSE;
1761 if (dynamic)
1762 in->symbol.flags |= BSF_DYNAMIC;
1765 return TRUE;
1768 /* We read the symbols into a buffer, which is discarded when this
1769 function exits. We read the strings into a buffer large enough to
1770 hold them all plus all the cached symbol entries. */
1772 bfd_boolean
1773 NAME(aout,slurp_symbol_table) (abfd)
1774 bfd *abfd;
1776 struct external_nlist *old_external_syms;
1777 aout_symbol_type *cached;
1778 bfd_size_type cached_size;
1780 /* If there's no work to be done, don't do any */
1781 if (obj_aout_symbols (abfd) != (aout_symbol_type *) NULL)
1782 return TRUE;
1784 old_external_syms = obj_aout_external_syms (abfd);
1786 if (! aout_get_external_symbols (abfd))
1787 return FALSE;
1789 cached_size = obj_aout_external_sym_count (abfd);
1790 cached_size *= sizeof (aout_symbol_type);
1791 cached = (aout_symbol_type *) bfd_zmalloc (cached_size);
1792 if (cached == NULL && cached_size != 0)
1793 return FALSE;
1795 /* Convert from external symbol information to internal. */
1796 if (! (NAME(aout,translate_symbol_table)
1797 (abfd, cached,
1798 obj_aout_external_syms (abfd),
1799 obj_aout_external_sym_count (abfd),
1800 obj_aout_external_strings (abfd),
1801 obj_aout_external_string_size (abfd),
1802 FALSE)))
1804 free (cached);
1805 return FALSE;
1808 bfd_get_symcount (abfd) = obj_aout_external_sym_count (abfd);
1810 obj_aout_symbols (abfd) = cached;
1812 /* It is very likely that anybody who calls this function will not
1813 want the external symbol information, so if it was allocated
1814 because of our call to aout_get_external_symbols, we free it up
1815 right away to save space. */
1816 if (old_external_syms == (struct external_nlist *) NULL
1817 && obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
1819 #ifdef USE_MMAP
1820 bfd_free_window (&obj_aout_sym_window (abfd));
1821 #else
1822 free (obj_aout_external_syms (abfd));
1823 #endif
1824 obj_aout_external_syms (abfd) = NULL;
1827 return TRUE;
1830 /* We use a hash table when writing out symbols so that we only write
1831 out a particular string once. This helps particularly when the
1832 linker writes out stabs debugging entries, because each different
1833 contributing object file tends to have many duplicate stabs
1834 strings.
1836 This hash table code breaks dbx on SunOS 4.1.3, so we don't do it
1837 if BFD_TRADITIONAL_FORMAT is set. */
1839 static bfd_size_type add_to_stringtab
1840 PARAMS ((bfd *, struct bfd_strtab_hash *, const char *, bfd_boolean));
1841 static bfd_boolean emit_stringtab PARAMS ((bfd *, struct bfd_strtab_hash *));
1843 /* Get the index of a string in a strtab, adding it if it is not
1844 already present. */
1846 static INLINE bfd_size_type
1847 add_to_stringtab (abfd, tab, str, copy)
1848 bfd *abfd;
1849 struct bfd_strtab_hash *tab;
1850 const char *str;
1851 bfd_boolean copy;
1853 bfd_boolean hash;
1854 bfd_size_type index;
1856 /* An index of 0 always means the empty string. */
1857 if (str == 0 || *str == '\0')
1858 return 0;
1860 /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
1861 doesn't understand a hashed string table. */
1862 hash = TRUE;
1863 if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1864 hash = FALSE;
1866 index = _bfd_stringtab_add (tab, str, hash, copy);
1868 if (index != (bfd_size_type) -1)
1870 /* Add BYTES_IN_LONG to the return value to account for the
1871 space taken up by the string table size. */
1872 index += BYTES_IN_LONG;
1875 return index;
1878 /* Write out a strtab. ABFD is already at the right location in the
1879 file. */
1881 static bfd_boolean
1882 emit_stringtab (abfd, tab)
1883 register bfd *abfd;
1884 struct bfd_strtab_hash *tab;
1886 bfd_byte buffer[BYTES_IN_LONG];
1888 /* The string table starts with the size. */
1889 H_PUT_32 (abfd, _bfd_stringtab_size (tab) + BYTES_IN_LONG, buffer);
1890 if (bfd_bwrite ((PTR) buffer, (bfd_size_type) BYTES_IN_LONG, abfd)
1891 != BYTES_IN_LONG)
1892 return FALSE;
1894 return _bfd_stringtab_emit (abfd, tab);
1897 bfd_boolean
1898 NAME(aout,write_syms) (abfd)
1899 bfd *abfd;
1901 unsigned int count ;
1902 asymbol **generic = bfd_get_outsymbols (abfd);
1903 struct bfd_strtab_hash *strtab;
1905 strtab = _bfd_stringtab_init ();
1906 if (strtab == NULL)
1907 return FALSE;
1909 for (count = 0; count < bfd_get_symcount (abfd); count++)
1911 asymbol *g = generic[count];
1912 bfd_size_type indx;
1913 struct external_nlist nsp;
1915 PUT_WORD (abfd, 0, nsp.e_unused);
1917 indx = add_to_stringtab (abfd, strtab, g->name, FALSE);
1918 if (indx == (bfd_size_type) -1)
1919 goto error_return;
1920 PUT_WORD (abfd, indx, nsp.e_strx);
1922 if (bfd_asymbol_flavour(g) == abfd->xvec->flavour)
1923 H_PUT_8 (abfd, aout_symbol(g)->type, nsp.e_type);
1924 else
1925 H_PUT_8 (abfd, 0, nsp.e_type);
1927 if (! translate_to_native_sym_flags (abfd, g, &nsp))
1928 goto error_return;
1930 H_PUT_8 (abfd, 0, nsp.e_ovly);
1932 if (bfd_bwrite ((PTR)&nsp, (bfd_size_type) EXTERNAL_NLIST_SIZE, abfd)
1933 != EXTERNAL_NLIST_SIZE)
1934 goto error_return;
1936 /* NB: `KEEPIT' currently overlays `udata.p', so set this only
1937 here, at the end. */
1938 g->KEEPIT = count;
1941 if (! emit_stringtab (abfd, strtab))
1942 goto error_return;
1944 _bfd_stringtab_free (strtab);
1946 return TRUE;
1948 error_return:
1949 _bfd_stringtab_free (strtab);
1950 return FALSE;
1954 long
1955 NAME(aout,canonicalize_symtab) (abfd, location)
1956 bfd *abfd;
1957 asymbol **location;
1959 unsigned int counter = 0;
1960 aout_symbol_type *symbase;
1962 if (!NAME(aout,slurp_symbol_table)(abfd))
1963 return -1;
1965 for (symbase = obj_aout_symbols(abfd); counter++ < bfd_get_symcount (abfd);)
1966 *(location++) = (asymbol *)( symbase++);
1967 *location++ =0;
1968 return bfd_get_symcount (abfd);
1972 /* Standard reloc stuff */
1974 /* Extended stuff */
1975 /* Output extended relocation information to a file in target byte order. */
1977 void
1978 pdp11_aout_swap_reloc_out (abfd, g, natptr)
1979 bfd *abfd;
1980 arelent *g;
1981 bfd_byte *natptr;
1983 int r_index;
1984 int r_pcrel;
1985 int reloc_entry;
1986 int r_type;
1987 asymbol *sym = *(g->sym_ptr_ptr);
1988 asection *output_section = sym->section->output_section;
1990 if (g->addend != 0)
1991 fprintf (stderr, "BFD: can't do this reloc addend stuff\n");
1993 r_pcrel = g->howto->pc_relative;
1995 if (bfd_is_abs_section (output_section))
1996 r_type = RABS;
1997 else if (output_section == obj_textsec (abfd))
1998 r_type = RTEXT;
1999 else if (output_section == obj_datasec (abfd))
2000 r_type = RDATA;
2001 else if (output_section == obj_bsssec (abfd))
2002 r_type = RBSS;
2003 else if (bfd_is_und_section (output_section))
2004 r_type = REXT;
2005 else if (bfd_is_com_section (output_section))
2006 r_type = REXT;
2007 else
2008 r_type = -1;
2010 BFD_ASSERT (r_type != -1);
2012 if (r_type == RABS)
2013 r_index = 0;
2014 else
2015 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
2017 reloc_entry = r_index << 4 | r_type | r_pcrel;
2019 PUT_WORD (abfd, reloc_entry, natptr);
2022 /* BFD deals internally with all things based from the section they're
2023 in. so, something in 10 bytes into a text section with a base of
2024 50 would have a symbol (.text+10) and know .text vma was 50.
2026 Aout keeps all it's symbols based from zero, so the symbol would
2027 contain 60. This macro subs the base of each section from the value
2028 to give the true offset from the section */
2031 #define MOVE_ADDRESS(ad) \
2032 if (r_extern) \
2034 /* Undefined symbol. */ \
2035 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2036 cache_ptr->addend = ad; \
2038 else \
2040 /* Defined, section relative. replace symbol with pointer to \
2041 symbol which points to section. */ \
2042 switch (r_index) \
2044 case N_TEXT: \
2045 case N_TEXT | N_EXT: \
2046 cache_ptr->sym_ptr_ptr = obj_textsec (abfd)->symbol_ptr_ptr; \
2047 cache_ptr->addend = ad - su->textsec->vma; \
2048 break; \
2049 case N_DATA: \
2050 case N_DATA | N_EXT: \
2051 cache_ptr->sym_ptr_ptr = obj_datasec (abfd)->symbol_ptr_ptr; \
2052 cache_ptr->addend = ad - su->datasec->vma; \
2053 break; \
2054 case N_BSS: \
2055 case N_BSS | N_EXT: \
2056 cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr; \
2057 cache_ptr->addend = ad - su->bsssec->vma; \
2058 break; \
2059 default: \
2060 case N_ABS: \
2061 case N_ABS | N_EXT: \
2062 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
2063 cache_ptr->addend = ad; \
2064 break; \
2068 void
2069 pdp11_aout_swap_reloc_in (abfd, bytes, cache_ptr, offset,
2070 symbols, symcount)
2071 bfd *abfd;
2072 bfd_byte *bytes;
2073 arelent *cache_ptr;
2074 bfd_size_type offset;
2075 asymbol **symbols;
2076 bfd_size_type symcount;
2078 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2079 unsigned int r_index;
2080 int reloc_entry;
2081 int r_extern;
2082 int r_pcrel;
2084 reloc_entry = GET_WORD (abfd, (PTR)bytes);
2086 r_pcrel = reloc_entry & RELFLG;
2088 cache_ptr->address = offset;
2089 cache_ptr->howto = howto_table_pdp11 + (r_pcrel ? 1 : 0);
2091 if ((reloc_entry & RTYPE) == RABS)
2092 r_index = N_ABS;
2093 else
2094 r_index = RINDEX (reloc_entry);
2096 /* r_extern reflects whether the symbol the reloc is against is
2097 local or global. */
2098 r_extern = (reloc_entry & RTYPE) == REXT;
2100 if (r_extern && r_index > symcount)
2102 /* We could arrange to return an error, but it might be useful
2103 to see the file even if it is bad. */
2104 r_extern = 0;
2105 r_index = N_ABS;
2108 MOVE_ADDRESS(0);
2111 /* Read and swap the relocs for a section. */
2113 bfd_boolean
2114 NAME(aout,slurp_reloc_table) (abfd, asect, symbols)
2115 bfd *abfd;
2116 sec_ptr asect;
2117 asymbol **symbols;
2119 bfd_byte *rptr;
2120 bfd_size_type count;
2121 bfd_size_type reloc_size;
2122 PTR relocs;
2123 arelent *reloc_cache;
2124 size_t each_size;
2125 unsigned int counter = 0;
2126 arelent *cache_ptr;
2128 if (asect->relocation)
2129 return TRUE;
2131 if (asect->flags & SEC_CONSTRUCTOR)
2132 return TRUE;
2134 if (asect == obj_datasec (abfd))
2135 reloc_size = exec_hdr(abfd)->a_drsize;
2136 else if (asect == obj_textsec (abfd))
2137 reloc_size = exec_hdr(abfd)->a_trsize;
2138 else if (asect == obj_bsssec (abfd))
2139 reloc_size = 0;
2140 else
2142 bfd_set_error (bfd_error_invalid_operation);
2143 return FALSE;
2146 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2147 return FALSE;
2149 each_size = obj_reloc_entry_size (abfd);
2151 relocs = bfd_malloc (reloc_size);
2152 if (relocs == NULL && reloc_size != 0)
2153 return FALSE;
2155 if (bfd_bread (relocs, reloc_size, abfd) != reloc_size)
2157 free (relocs);
2158 return FALSE;
2161 count = reloc_size / each_size;
2163 /* Count the number of NON-ZERO relocs, this is the count we want. */
2165 unsigned int real_count = 0;
2167 for (counter = 0; counter < count; counter++)
2169 int x;
2171 x = GET_WORD (abfd, (char *) relocs + each_size * counter);
2172 if (x != 0)
2173 real_count++;
2176 count = real_count;
2179 reloc_cache = (arelent *) bfd_zmalloc (count * sizeof (arelent));
2180 if (reloc_cache == NULL && count != 0)
2181 return FALSE;
2183 cache_ptr = reloc_cache;
2185 rptr = relocs;
2186 for (counter = 0;
2187 counter < count;
2188 counter++, rptr += RELOC_SIZE, cache_ptr++)
2190 while (GET_WORD (abfd, (PTR)rptr) == 0)
2192 rptr += RELOC_SIZE;
2193 if ((char *) rptr >= (char *) relocs + reloc_size)
2194 goto done;
2197 pdp11_aout_swap_reloc_in (abfd, rptr, cache_ptr,
2198 (bfd_size_type) ((char *) rptr - (char *) relocs),
2199 symbols,
2200 (bfd_size_type) bfd_get_symcount (abfd));
2202 done:
2203 /* Just in case, if rptr >= relocs + reloc_size should happen
2204 too early. */
2205 BFD_ASSERT (counter == count);
2207 free (relocs);
2209 asect->relocation = reloc_cache;
2210 asect->reloc_count = cache_ptr - reloc_cache;
2212 return TRUE;
2215 /* Write out a relocation section into an object file. */
2217 bfd_boolean
2218 NAME(aout,squirt_out_relocs) (abfd, section)
2219 bfd *abfd;
2220 asection *section;
2222 arelent **generic;
2223 unsigned char *native;
2224 unsigned int count = section->reloc_count;
2225 bfd_size_type natsize;
2227 natsize = section->size;
2228 native = (unsigned char *) bfd_zalloc (abfd, natsize);
2229 if (!native)
2230 return FALSE;
2232 generic = section->orelocation;
2233 if (generic != NULL)
2235 while (count > 0)
2237 bfd_byte *r;
2239 r = native + (*generic)->address;
2240 pdp11_aout_swap_reloc_out (abfd, *generic, r);
2241 count--;
2242 generic++;
2246 if (bfd_bwrite ((PTR) native, natsize, abfd) != natsize)
2248 bfd_release (abfd, native);
2249 return FALSE;
2252 bfd_release (abfd, native);
2253 return TRUE;
2256 /* This is stupid. This function should be a boolean predicate */
2257 long
2258 NAME(aout,canonicalize_reloc) (abfd, section, relptr, symbols)
2259 bfd *abfd;
2260 sec_ptr section;
2261 arelent **relptr;
2262 asymbol **symbols;
2264 arelent *tblptr = section->relocation;
2265 unsigned int count;
2267 if (section == obj_bsssec (abfd))
2269 *relptr = NULL;
2270 return 0;
2273 if (!(tblptr || NAME(aout,slurp_reloc_table)(abfd, section, symbols)))
2274 return -1;
2276 if (section->flags & SEC_CONSTRUCTOR)
2278 arelent_chain *chain = section->constructor_chain;
2280 for (count = 0; count < section->reloc_count; count ++)
2282 *relptr ++ = &chain->relent;
2283 chain = chain->next;
2286 else
2288 tblptr = section->relocation;
2290 for (count = 0; count++ < section->reloc_count;)
2291 *relptr++ = tblptr++;
2294 *relptr = 0;
2296 return section->reloc_count;
2299 long
2300 NAME(aout,get_reloc_upper_bound) (abfd, asect)
2301 bfd *abfd;
2302 sec_ptr asect;
2304 if (bfd_get_format (abfd) != bfd_object)
2306 bfd_set_error (bfd_error_invalid_operation);
2307 return -1;
2310 if (asect->flags & SEC_CONSTRUCTOR)
2311 return (sizeof (arelent *) * (asect->reloc_count+1));
2313 if (asect == obj_datasec (abfd))
2314 return (sizeof (arelent *)
2315 * ((exec_hdr(abfd)->a_drsize / obj_reloc_entry_size (abfd))
2316 + 1));
2318 if (asect == obj_textsec (abfd))
2319 return (sizeof (arelent *)
2320 * ((exec_hdr(abfd)->a_trsize / obj_reloc_entry_size (abfd))
2321 + 1));
2323 /* TODO: why are there two if statements for obj_bsssec()? */
2325 if (asect == obj_bsssec (abfd))
2326 return sizeof (arelent *);
2328 if (asect == obj_bsssec (abfd))
2329 return 0;
2331 bfd_set_error (bfd_error_invalid_operation);
2332 return -1;
2336 long
2337 NAME(aout,get_symtab_upper_bound) (abfd)
2338 bfd *abfd;
2340 if (!NAME(aout,slurp_symbol_table)(abfd))
2341 return -1;
2343 return (bfd_get_symcount (abfd) + 1) * (sizeof (aout_symbol_type *));
2346 alent *
2347 NAME(aout,get_lineno) (abfd, symbol)
2348 bfd * abfd ATTRIBUTE_UNUSED;
2349 asymbol * symbol ATTRIBUTE_UNUSED;
2351 return (alent *)NULL;
2354 void
2355 NAME(aout,get_symbol_info) (abfd, symbol, ret)
2356 bfd * abfd ATTRIBUTE_UNUSED;
2357 asymbol *symbol;
2358 symbol_info *ret;
2360 bfd_symbol_info (symbol, ret);
2362 if (ret->type == '?')
2364 int type_code = aout_symbol(symbol)->type & 0xff;
2365 const char *stab_name = bfd_get_stab_name (type_code);
2366 static char buf[10];
2368 if (stab_name == NULL)
2370 sprintf(buf, "(%d)", type_code);
2371 stab_name = buf;
2373 ret->type = '-';
2374 ret->stab_type = type_code;
2375 ret->stab_other = (unsigned)(aout_symbol(symbol)->other & 0xff);
2376 ret->stab_desc = (unsigned)(aout_symbol(symbol)->desc & 0xffff);
2377 ret->stab_name = stab_name;
2381 void
2382 NAME(aout,print_symbol) (abfd, afile, symbol, how)
2383 bfd * abfd;
2384 PTR afile;
2385 asymbol *symbol;
2386 bfd_print_symbol_type how;
2388 FILE *file = (FILE *)afile;
2390 switch (how)
2392 case bfd_print_symbol_name:
2393 if (symbol->name)
2394 fprintf(file,"%s", symbol->name);
2395 break;
2396 case bfd_print_symbol_more:
2397 fprintf(file,"%4x %2x %2x",(unsigned)(aout_symbol(symbol)->desc & 0xffff),
2398 (unsigned)(aout_symbol(symbol)->other & 0xff),
2399 (unsigned)(aout_symbol(symbol)->type));
2400 break;
2401 case bfd_print_symbol_all:
2403 const char *section_name = symbol->section->name;
2405 bfd_print_symbol_vandf (abfd, (PTR)file, symbol);
2407 fprintf (file," %-5s %04x %02x %02x",
2408 section_name,
2409 (unsigned)(aout_symbol(symbol)->desc & 0xffff),
2410 (unsigned)(aout_symbol(symbol)->other & 0xff),
2411 (unsigned)(aout_symbol(symbol)->type & 0xff));
2412 if (symbol->name)
2413 fprintf(file," %s", symbol->name);
2415 break;
2419 /* If we don't have to allocate more than 1MB to hold the generic
2420 symbols, we use the generic minisymbol method: it's faster, since
2421 it only translates the symbols once, not multiple times. */
2422 #define MINISYM_THRESHOLD (1000000 / sizeof (asymbol))
2424 /* Read minisymbols. For minisymbols, we use the unmodified a.out
2425 symbols. The minisymbol_to_symbol function translates these into
2426 BFD asymbol structures. */
2428 long
2429 NAME(aout,read_minisymbols) (abfd, dynamic, minisymsp, sizep)
2430 bfd *abfd;
2431 bfd_boolean dynamic;
2432 PTR *minisymsp;
2433 unsigned int *sizep;
2435 if (dynamic)
2437 /* We could handle the dynamic symbols here as well, but it's
2438 easier to hand them off. */
2439 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2442 if (! aout_get_external_symbols (abfd))
2443 return -1;
2445 if (obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2446 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2448 *minisymsp = (PTR) obj_aout_external_syms (abfd);
2450 /* By passing the external symbols back from this routine, we are
2451 giving up control over the memory block. Clear
2452 obj_aout_external_syms, so that we do not try to free it
2453 ourselves. */
2454 obj_aout_external_syms (abfd) = NULL;
2456 *sizep = EXTERNAL_NLIST_SIZE;
2457 return obj_aout_external_sym_count (abfd);
2460 /* Convert a minisymbol to a BFD asymbol. A minisymbol is just an
2461 unmodified a.out symbol. The SYM argument is a structure returned
2462 by bfd_make_empty_symbol, which we fill in here. */
2464 asymbol *
2465 NAME(aout,minisymbol_to_symbol) (abfd, dynamic, minisym, sym)
2466 bfd *abfd;
2467 bfd_boolean dynamic;
2468 const PTR minisym;
2469 asymbol *sym;
2471 if (dynamic
2472 || obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2473 return _bfd_generic_minisymbol_to_symbol (abfd, dynamic, minisym, sym);
2475 memset (sym, 0, sizeof (aout_symbol_type));
2477 /* We call translate_symbol_table to translate a single symbol. */
2478 if (! (NAME(aout,translate_symbol_table)
2479 (abfd,
2480 (aout_symbol_type *) sym,
2481 (struct external_nlist *) minisym,
2482 (bfd_size_type) 1,
2483 obj_aout_external_strings (abfd),
2484 obj_aout_external_string_size (abfd),
2485 FALSE)))
2486 return NULL;
2488 return sym;
2492 provided a BFD, a section and an offset into the section, calculate
2493 and return the name of the source file and the line nearest to the
2494 wanted location.
2497 bfd_boolean
2498 NAME(aout,find_nearest_line)
2499 (abfd, section, symbols, offset, filename_ptr, functionname_ptr, line_ptr)
2500 bfd *abfd;
2501 asection *section;
2502 asymbol **symbols;
2503 bfd_vma offset;
2504 const char **filename_ptr;
2505 const char **functionname_ptr;
2506 unsigned int *line_ptr;
2508 /* Run down the file looking for the filename, function and linenumber */
2509 asymbol **p;
2510 const char *directory_name = NULL;
2511 const char *main_file_name = NULL;
2512 const char *current_file_name = NULL;
2513 const char *line_file_name = NULL; /* Value of current_file_name at line number. */
2514 bfd_vma low_line_vma = 0;
2515 bfd_vma low_func_vma = 0;
2516 asymbol *func = 0;
2517 size_t filelen, funclen;
2518 char *buf;
2520 *filename_ptr = abfd->filename;
2521 *functionname_ptr = 0;
2522 *line_ptr = 0;
2524 if (symbols != (asymbol **)NULL)
2526 for (p = symbols; *p; p++)
2528 aout_symbol_type *q = (aout_symbol_type *)(*p);
2529 next:
2530 switch (q->type)
2532 case N_TEXT:
2533 /* If this looks like a file name symbol, and it comes after
2534 the line number we have found so far, but before the
2535 offset, then we have probably not found the right line
2536 number. */
2537 if (q->symbol.value <= offset
2538 && ((q->symbol.value > low_line_vma
2539 && (line_file_name != NULL
2540 || *line_ptr != 0))
2541 || (q->symbol.value > low_func_vma
2542 && func != NULL)))
2544 const char * symname;
2546 symname = q->symbol.name;
2547 if (strcmp (symname + strlen (symname) - 2, ".o") == 0)
2549 if (q->symbol.value > low_line_vma)
2551 *line_ptr = 0;
2552 line_file_name = NULL;
2554 if (q->symbol.value > low_func_vma)
2555 func = NULL;
2558 break;
2560 case N_SO:
2561 /* If this symbol is less than the offset, but greater than
2562 the line number we have found so far, then we have not
2563 found the right line number. */
2564 if (q->symbol.value <= offset)
2566 if (q->symbol.value > low_line_vma)
2568 *line_ptr = 0;
2569 line_file_name = NULL;
2571 if (q->symbol.value > low_func_vma)
2572 func = NULL;
2575 main_file_name = current_file_name = q->symbol.name;
2576 /* Look ahead to next symbol to check if that too is an N_SO. */
2577 p++;
2578 if (*p == NULL)
2579 break;
2580 q = (aout_symbol_type *)(*p);
2581 if (q->type != (int)N_SO)
2582 goto next;
2584 /* Found a second N_SO First is directory; second is filename. */
2585 directory_name = current_file_name;
2586 main_file_name = current_file_name = q->symbol.name;
2587 if (obj_textsec(abfd) != section)
2588 goto done;
2589 break;
2590 case N_SOL:
2591 current_file_name = q->symbol.name;
2592 break;
2594 case N_SLINE:
2595 case N_DSLINE:
2596 case N_BSLINE:
2597 /* We'll keep this if it resolves nearer than the one we have
2598 already. */
2599 if (q->symbol.value >= low_line_vma
2600 && q->symbol.value <= offset)
2602 *line_ptr = q->desc;
2603 low_line_vma = q->symbol.value;
2604 line_file_name = current_file_name;
2606 break;
2608 case N_FUN:
2610 /* We'll keep this if it is nearer than the one we have already */
2611 if (q->symbol.value >= low_func_vma &&
2612 q->symbol.value <= offset)
2614 low_func_vma = q->symbol.value;
2615 func = (asymbol *)q;
2617 else if (q->symbol.value > offset)
2618 goto done;
2620 break;
2625 done:
2626 if (*line_ptr != 0)
2627 main_file_name = line_file_name;
2629 if (main_file_name == NULL
2630 || main_file_name[0] == '/'
2631 || directory_name == NULL)
2632 filelen = 0;
2633 else
2634 filelen = strlen (directory_name) + strlen (main_file_name);
2635 if (func == NULL)
2636 funclen = 0;
2637 else
2638 funclen = strlen (bfd_asymbol_name (func));
2640 if (adata (abfd).line_buf != NULL)
2641 free (adata (abfd).line_buf);
2642 if (filelen + funclen == 0)
2643 adata (abfd).line_buf = buf = NULL;
2644 else
2646 buf = (char *) bfd_malloc ((bfd_size_type) filelen + funclen + 3);
2647 adata (abfd).line_buf = buf;
2648 if (buf == NULL)
2649 return FALSE;
2652 if (main_file_name != NULL)
2654 if (main_file_name[0] == '/' || directory_name == NULL)
2655 *filename_ptr = main_file_name;
2656 else
2658 sprintf (buf, "%s%s", directory_name, main_file_name);
2659 *filename_ptr = buf;
2660 buf += filelen + 1;
2664 if (func)
2666 const char *function = func->name;
2667 char *colon;
2669 /* The caller expects a symbol name. We actually have a
2670 function name, without the leading underscore. Put the
2671 underscore back in, so that the caller gets a symbol name. */
2672 if (bfd_get_symbol_leading_char (abfd) == '\0')
2673 strcpy (buf, function);
2674 else
2676 buf[0] = bfd_get_symbol_leading_char (abfd);
2677 strcpy (buf + 1, function);
2680 /* Have to remove : stuff. */
2681 colon = strchr (buf, ':');
2682 if (colon != NULL)
2683 *colon = '\0';
2684 *functionname_ptr = buf;
2687 return TRUE;
2691 NAME(aout,sizeof_headers) (abfd, execable)
2692 bfd *abfd;
2693 bfd_boolean execable ATTRIBUTE_UNUSED;
2695 return adata(abfd).exec_bytes_size;
2698 /* Free all information we have cached for this BFD. We can always
2699 read it again later if we need it. */
2701 bfd_boolean
2702 NAME(aout,bfd_free_cached_info) (abfd)
2703 bfd *abfd;
2705 asection *o;
2707 if (bfd_get_format (abfd) != bfd_object)
2708 return TRUE;
2710 #define BFCI_FREE(x) if (x != NULL) { free (x); x = NULL; }
2711 BFCI_FREE (obj_aout_symbols (abfd));
2712 #ifdef USE_MMAP
2713 obj_aout_external_syms (abfd) = 0;
2714 bfd_free_window (&obj_aout_sym_window (abfd));
2715 bfd_free_window (&obj_aout_string_window (abfd));
2716 obj_aout_external_strings (abfd) = 0;
2717 #else
2718 BFCI_FREE (obj_aout_external_syms (abfd));
2719 BFCI_FREE (obj_aout_external_strings (abfd));
2720 #endif
2721 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2722 BFCI_FREE (o->relocation);
2723 #undef BFCI_FREE
2725 return TRUE;
2728 /* a.out link code. */
2730 static bfd_boolean aout_link_add_object_symbols
2731 PARAMS ((bfd *, struct bfd_link_info *));
2732 static bfd_boolean aout_link_check_archive_element
2733 PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *));
2734 static bfd_boolean aout_link_free_symbols PARAMS ((bfd *));
2735 static bfd_boolean aout_link_check_ar_symbols
2736 PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *pneeded));
2737 static bfd_boolean aout_link_add_symbols
2738 PARAMS ((bfd *, struct bfd_link_info *));
2740 /* Routine to create an entry in an a.out link hash table. */
2742 struct bfd_hash_entry *
2743 NAME(aout,link_hash_newfunc) (entry, table, string)
2744 struct bfd_hash_entry *entry;
2745 struct bfd_hash_table *table;
2746 const char *string;
2748 struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
2750 /* Allocate the structure if it has not already been allocated by a
2751 subclass. */
2752 if (ret == (struct aout_link_hash_entry *) NULL)
2753 ret = ((struct aout_link_hash_entry *)
2754 bfd_hash_allocate (table, sizeof (struct aout_link_hash_entry)));
2755 if (ret == (struct aout_link_hash_entry *) NULL)
2756 return (struct bfd_hash_entry *) ret;
2758 /* Call the allocation method of the superclass. */
2759 ret = ((struct aout_link_hash_entry *)
2760 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2761 table, string));
2762 if (ret)
2764 /* Set local fields. */
2765 ret->written = FALSE;
2766 ret->indx = -1;
2769 return (struct bfd_hash_entry *) ret;
2772 /* Initialize an a.out link hash table. */
2774 bfd_boolean
2775 NAME(aout,link_hash_table_init) (table, abfd, newfunc)
2776 struct aout_link_hash_table *table;
2777 bfd *abfd;
2778 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
2779 struct bfd_hash_table *,
2780 const char *));
2782 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
2785 /* Create an a.out link hash table. */
2787 struct bfd_link_hash_table *
2788 NAME(aout,link_hash_table_create) (abfd)
2789 bfd *abfd;
2791 struct aout_link_hash_table *ret;
2792 bfd_size_type amt = sizeof (struct aout_link_hash_table);
2794 ret = ((struct aout_link_hash_table *) bfd_alloc (abfd, amt));
2795 if (ret == NULL)
2796 return (struct bfd_link_hash_table *) NULL;
2797 if (! NAME(aout,link_hash_table_init) (ret, abfd,
2798 NAME(aout,link_hash_newfunc)))
2800 free (ret);
2801 return (struct bfd_link_hash_table *) NULL;
2803 return &ret->root;
2806 /* Given an a.out BFD, add symbols to the global hash table as
2807 appropriate. */
2809 bfd_boolean
2810 NAME(aout,link_add_symbols) (abfd, info)
2811 bfd *abfd;
2812 struct bfd_link_info *info;
2814 switch (bfd_get_format (abfd))
2816 case bfd_object:
2817 return aout_link_add_object_symbols (abfd, info);
2818 case bfd_archive:
2819 return _bfd_generic_link_add_archive_symbols
2820 (abfd, info, aout_link_check_archive_element);
2821 default:
2822 bfd_set_error (bfd_error_wrong_format);
2823 return FALSE;
2827 /* Add symbols from an a.out object file. */
2829 static bfd_boolean
2830 aout_link_add_object_symbols (abfd, info)
2831 bfd *abfd;
2832 struct bfd_link_info *info;
2834 if (! aout_get_external_symbols (abfd))
2835 return FALSE;
2836 if (! aout_link_add_symbols (abfd, info))
2837 return FALSE;
2838 if (! info->keep_memory)
2840 if (! aout_link_free_symbols (abfd))
2841 return FALSE;
2843 return TRUE;
2846 /* Check a single archive element to see if we need to include it in
2847 the link. *PNEEDED is set according to whether this element is
2848 needed in the link or not. This is called from
2849 _bfd_generic_link_add_archive_symbols. */
2851 static bfd_boolean
2852 aout_link_check_archive_element (abfd, info, pneeded)
2853 bfd *abfd;
2854 struct bfd_link_info *info;
2855 bfd_boolean *pneeded;
2857 if (! aout_get_external_symbols (abfd))
2858 return FALSE;
2860 if (! aout_link_check_ar_symbols (abfd, info, pneeded))
2861 return FALSE;
2863 if (*pneeded)
2865 if (! aout_link_add_symbols (abfd, info))
2866 return FALSE;
2869 if (! info->keep_memory || ! *pneeded)
2871 if (! aout_link_free_symbols (abfd))
2872 return FALSE;
2875 return TRUE;
2878 /* Free up the internal symbols read from an a.out file. */
2880 static bfd_boolean
2881 aout_link_free_symbols (abfd)
2882 bfd *abfd;
2884 if (obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
2886 #ifdef USE_MMAP
2887 bfd_free_window (&obj_aout_sym_window (abfd));
2888 #else
2889 free ((PTR) obj_aout_external_syms (abfd));
2890 #endif
2891 obj_aout_external_syms (abfd) = (struct external_nlist *) NULL;
2893 if (obj_aout_external_strings (abfd) != (char *) NULL)
2895 #ifdef USE_MMAP
2896 bfd_free_window (&obj_aout_string_window (abfd));
2897 #else
2898 free ((PTR) obj_aout_external_strings (abfd));
2899 #endif
2900 obj_aout_external_strings (abfd) = (char *) NULL;
2902 return TRUE;
2905 /* Look through the internal symbols to see if this object file should
2906 be included in the link. We should include this object file if it
2907 defines any symbols which are currently undefined. If this object
2908 file defines a common symbol, then we may adjust the size of the
2909 known symbol but we do not include the object file in the link
2910 (unless there is some other reason to include it). */
2912 static bfd_boolean
2913 aout_link_check_ar_symbols (abfd, info, pneeded)
2914 bfd *abfd;
2915 struct bfd_link_info *info;
2916 bfd_boolean *pneeded;
2918 register struct external_nlist *p;
2919 struct external_nlist *pend;
2920 char *strings;
2922 *pneeded = FALSE;
2924 /* Look through all the symbols. */
2925 p = obj_aout_external_syms (abfd);
2926 pend = p + obj_aout_external_sym_count (abfd);
2927 strings = obj_aout_external_strings (abfd);
2928 for (; p < pend; p++)
2930 int type = H_GET_8 (abfd, p->e_type);
2931 const char *name;
2932 struct bfd_link_hash_entry *h;
2934 /* Ignore symbols that are not externally visible. This is an
2935 optimization only, as we check the type more thoroughly
2936 below. */
2937 if ((type & N_EXT) == 0
2938 || type == N_FN)
2939 continue;
2941 name = strings + GET_WORD (abfd, p->e_strx);
2942 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
2944 /* We are only interested in symbols that are currently
2945 undefined or common. */
2946 if (h == (struct bfd_link_hash_entry *) NULL
2947 || (h->type != bfd_link_hash_undefined
2948 && h->type != bfd_link_hash_common))
2949 continue;
2951 if (type == (N_TEXT | N_EXT)
2952 || type == (N_DATA | N_EXT)
2953 || type == (N_BSS | N_EXT)
2954 || type == (N_ABS | N_EXT))
2956 /* This object file defines this symbol. We must link it
2957 in. This is true regardless of whether the current
2958 definition of the symbol is undefined or common. If the
2959 current definition is common, we have a case in which we
2960 have already seen an object file including
2961 int a;
2962 and this object file from the archive includes
2963 int a = 5;
2964 In such a case we must include this object file.
2966 FIXME: The SunOS 4.1.3 linker will pull in the archive
2967 element if the symbol is defined in the .data section,
2968 but not if it is defined in the .text section. That
2969 seems a bit crazy to me, and I haven't implemented it.
2970 However, it might be correct. */
2971 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
2972 return FALSE;
2973 *pneeded = TRUE;
2974 return TRUE;
2977 if (type == (N_UNDF | N_EXT))
2979 bfd_vma value;
2981 value = GET_WORD (abfd, p->e_value);
2982 if (value != 0)
2984 /* This symbol is common in the object from the archive
2985 file. */
2986 if (h->type == bfd_link_hash_undefined)
2988 bfd *symbfd;
2989 unsigned int power;
2991 symbfd = h->u.undef.abfd;
2992 if (symbfd == (bfd *) NULL)
2994 /* This symbol was created as undefined from
2995 outside BFD. We assume that we should link
2996 in the object file. This is done for the -u
2997 option in the linker. */
2998 if (! (*info->callbacks->add_archive_element) (info,
2999 abfd,
3000 name))
3001 return FALSE;
3002 *pneeded = TRUE;
3003 return TRUE;
3005 /* Turn the current link symbol into a common
3006 symbol. It is already on the undefs list. */
3007 h->type = bfd_link_hash_common;
3008 h->u.c.p = ((struct bfd_link_hash_common_entry *)
3009 bfd_hash_allocate (&info->hash->table,
3010 sizeof (struct bfd_link_hash_common_entry)));
3011 if (h->u.c.p == NULL)
3012 return FALSE;
3014 h->u.c.size = value;
3016 /* FIXME: This isn't quite right. The maximum
3017 alignment of a common symbol should be set by the
3018 architecture of the output file, not of the input
3019 file. */
3020 power = bfd_log2 (value);
3021 if (power > bfd_get_arch_info (abfd)->section_align_power)
3022 power = bfd_get_arch_info (abfd)->section_align_power;
3023 h->u.c.p->alignment_power = power;
3025 h->u.c.p->section = bfd_make_section_old_way (symbfd,
3026 "COMMON");
3028 else
3030 /* Adjust the size of the common symbol if
3031 necessary. */
3032 if (value > h->u.c.size)
3033 h->u.c.size = value;
3039 /* We do not need this object file. */
3040 return TRUE;
3043 /* Add all symbols from an object file to the hash table. */
3045 static bfd_boolean
3046 aout_link_add_symbols (abfd, info)
3047 bfd *abfd;
3048 struct bfd_link_info *info;
3050 bfd_boolean (*add_one_symbol)
3051 PARAMS ((struct bfd_link_info *, bfd *, const char *, flagword, asection *,
3052 bfd_vma, const char *, bfd_boolean, bfd_boolean,
3053 struct bfd_link_hash_entry **));
3054 struct external_nlist *syms;
3055 bfd_size_type sym_count;
3056 char *strings;
3057 bfd_boolean copy;
3058 struct aout_link_hash_entry **sym_hash;
3059 register struct external_nlist *p;
3060 struct external_nlist *pend;
3062 syms = obj_aout_external_syms (abfd);
3063 sym_count = obj_aout_external_sym_count (abfd);
3064 strings = obj_aout_external_strings (abfd);
3065 if (info->keep_memory)
3066 copy = FALSE;
3067 else
3068 copy = TRUE;
3070 if (aout_backend_info (abfd)->add_dynamic_symbols != NULL)
3072 if (! ((*aout_backend_info (abfd)->add_dynamic_symbols)
3073 (abfd, info, &syms, &sym_count, &strings)))
3074 return FALSE;
3077 /* We keep a list of the linker hash table entries that correspond
3078 to particular symbols. We could just look them up in the hash
3079 table, but keeping the list is more efficient. Perhaps this
3080 should be conditional on info->keep_memory. */
3081 sym_hash = ((struct aout_link_hash_entry **)
3082 bfd_alloc (abfd,
3083 sym_count * sizeof (struct aout_link_hash_entry *)));
3084 if (sym_hash == NULL && sym_count != 0)
3085 return FALSE;
3086 obj_aout_sym_hashes (abfd) = sym_hash;
3088 add_one_symbol = aout_backend_info (abfd)->add_one_symbol;
3089 if (add_one_symbol == NULL)
3090 add_one_symbol = _bfd_generic_link_add_one_symbol;
3092 p = syms;
3093 pend = p + sym_count;
3094 for (; p < pend; p++, sym_hash++)
3096 int type;
3097 const char *name;
3098 bfd_vma value;
3099 asection *section;
3100 flagword flags;
3101 const char *string;
3103 *sym_hash = NULL;
3105 type = H_GET_8 (abfd, p->e_type);
3107 name = strings + GET_WORD (abfd, p->e_strx);
3108 value = GET_WORD (abfd, p->e_value);
3109 flags = BSF_GLOBAL;
3110 string = NULL;
3111 switch (type)
3113 default:
3114 abort ();
3116 case N_UNDF:
3117 case N_ABS:
3118 case N_TEXT:
3119 case N_DATA:
3120 case N_BSS:
3121 case N_REG:
3122 case N_FN:
3123 /* Ignore symbols that are not externally visible. */
3124 continue;
3126 case N_UNDF | N_EXT:
3127 if (value == 0)
3129 section = bfd_und_section_ptr;
3130 flags = 0;
3132 else
3133 section = bfd_com_section_ptr;
3134 break;
3135 case N_ABS | N_EXT:
3136 section = bfd_abs_section_ptr;
3137 break;
3138 case N_TEXT | N_EXT:
3139 section = obj_textsec (abfd);
3140 value -= bfd_get_section_vma (abfd, section);
3141 break;
3142 case N_DATA | N_EXT:
3143 /* Treat N_SETV symbols as N_DATA symbol; see comment in
3144 translate_from_native_sym_flags. */
3145 section = obj_datasec (abfd);
3146 value -= bfd_get_section_vma (abfd, section);
3147 break;
3148 case N_BSS | N_EXT:
3149 section = obj_bsssec (abfd);
3150 value -= bfd_get_section_vma (abfd, section);
3151 break;
3154 if (! ((*add_one_symbol)
3155 (info, abfd, name, flags, section, value, string, copy, FALSE,
3156 (struct bfd_link_hash_entry **) sym_hash)))
3157 return FALSE;
3159 /* Restrict the maximum alignment of a common symbol based on
3160 the architecture, since a.out has no way to represent
3161 alignment requirements of a section in a .o file. FIXME:
3162 This isn't quite right: it should use the architecture of the
3163 output file, not the input files. */
3164 if ((*sym_hash)->root.type == bfd_link_hash_common
3165 && ((*sym_hash)->root.u.c.p->alignment_power >
3166 bfd_get_arch_info (abfd)->section_align_power))
3167 (*sym_hash)->root.u.c.p->alignment_power =
3168 bfd_get_arch_info (abfd)->section_align_power;
3170 /* If this is a set symbol, and we are not building sets, then
3171 it is possible for the hash entry to not have been set. In
3172 such a case, treat the symbol as not globally defined. */
3173 if ((*sym_hash)->root.type == bfd_link_hash_new)
3175 BFD_ASSERT ((flags & BSF_CONSTRUCTOR) != 0);
3176 *sym_hash = NULL;
3180 return TRUE;
3183 /* A hash table used for header files with N_BINCL entries. */
3185 struct aout_link_includes_table
3187 struct bfd_hash_table root;
3190 /* A linked list of totals that we have found for a particular header
3191 file. */
3193 struct aout_link_includes_totals
3195 struct aout_link_includes_totals *next;
3196 bfd_vma total;
3199 /* An entry in the header file hash table. */
3201 struct aout_link_includes_entry
3203 struct bfd_hash_entry root;
3204 /* List of totals we have found for this file. */
3205 struct aout_link_includes_totals *totals;
3208 /* Look up an entry in an the header file hash table. */
3210 #define aout_link_includes_lookup(table, string, create, copy) \
3211 ((struct aout_link_includes_entry *) \
3212 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
3214 /* During the final link step we need to pass around a bunch of
3215 information, so we do it in an instance of this structure. */
3217 struct aout_final_link_info
3219 /* General link information. */
3220 struct bfd_link_info *info;
3221 /* Output bfd. */
3222 bfd *output_bfd;
3223 /* Reloc file positions. */
3224 file_ptr treloff, dreloff;
3225 /* File position of symbols. */
3226 file_ptr symoff;
3227 /* String table. */
3228 struct bfd_strtab_hash *strtab;
3229 /* Header file hash table. */
3230 struct aout_link_includes_table includes;
3231 /* A buffer large enough to hold the contents of any section. */
3232 bfd_byte *contents;
3233 /* A buffer large enough to hold the relocs of any section. */
3234 PTR relocs;
3235 /* A buffer large enough to hold the symbol map of any input BFD. */
3236 int *symbol_map;
3237 /* A buffer large enough to hold output symbols of any input BFD. */
3238 struct external_nlist *output_syms;
3241 static struct bfd_hash_entry *aout_link_includes_newfunc
3242 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
3243 static bfd_boolean aout_link_input_bfd
3244 PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
3245 static bfd_boolean aout_link_write_symbols
3246 PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
3247 static bfd_boolean aout_link_write_other_symbol
3248 PARAMS ((struct aout_link_hash_entry *, PTR));
3249 static bfd_boolean aout_link_input_section
3250 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3251 asection *input_section, file_ptr *reloff_ptr,
3252 bfd_size_type rel_size));
3253 static INLINE asection *aout_reloc_type_to_section
3254 PARAMS ((bfd *, int));
3255 static bfd_boolean aout_link_reloc_link_order
3256 PARAMS ((struct aout_final_link_info *, asection *,
3257 struct bfd_link_order *));
3258 static bfd_boolean pdp11_aout_link_input_section
3259 PARAMS ((struct aout_final_link_info *finfo,
3260 bfd *input_bfd,
3261 asection *input_section,
3262 bfd_byte *relocs,
3263 bfd_size_type rel_size,
3264 bfd_byte *contents));
3266 /* The function to create a new entry in the header file hash table. */
3268 static struct bfd_hash_entry *
3269 aout_link_includes_newfunc (entry, table, string)
3270 struct bfd_hash_entry *entry;
3271 struct bfd_hash_table *table;
3272 const char *string;
3274 struct aout_link_includes_entry *ret =
3275 (struct aout_link_includes_entry *) entry;
3277 /* Allocate the structure if it has not already been allocated by a
3278 subclass. */
3279 if (ret == (struct aout_link_includes_entry *) NULL)
3280 ret = ((struct aout_link_includes_entry *)
3281 bfd_hash_allocate (table,
3282 sizeof (struct aout_link_includes_entry)));
3283 if (ret == (struct aout_link_includes_entry *) NULL)
3284 return (struct bfd_hash_entry *) ret;
3286 /* Call the allocation method of the superclass. */
3287 ret = ((struct aout_link_includes_entry *)
3288 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
3289 if (ret)
3291 /* Set local fields. */
3292 ret->totals = NULL;
3295 return (struct bfd_hash_entry *) ret;
3298 /* Do the final link step. This is called on the output BFD. The
3299 INFO structure should point to a list of BFDs linked through the
3300 link_next field which can be used to find each BFD which takes part
3301 in the output. Also, each section in ABFD should point to a list
3302 of bfd_link_order structures which list all the input sections for
3303 the output section. */
3305 bfd_boolean
3306 NAME(aout,final_link) (abfd, info, callback)
3307 bfd *abfd;
3308 struct bfd_link_info *info;
3309 void (*callback) PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *));
3311 struct aout_final_link_info aout_info;
3312 bfd_boolean includes_hash_initialized = FALSE;
3313 register bfd *sub;
3314 bfd_size_type trsize, drsize;
3315 bfd_size_type max_contents_size;
3316 bfd_size_type max_relocs_size;
3317 bfd_size_type max_sym_count;
3318 bfd_size_type text_size;
3319 file_ptr text_end;
3320 register struct bfd_link_order *p;
3321 asection *o;
3322 bfd_boolean have_link_order_relocs;
3324 if (info->shared)
3325 abfd->flags |= DYNAMIC;
3327 aout_info.info = info;
3328 aout_info.output_bfd = abfd;
3329 aout_info.contents = NULL;
3330 aout_info.relocs = NULL;
3331 aout_info.symbol_map = NULL;
3332 aout_info.output_syms = NULL;
3334 if (! bfd_hash_table_init_n (&aout_info.includes.root,
3335 aout_link_includes_newfunc,
3336 251))
3337 goto error_return;
3338 includes_hash_initialized = TRUE;
3340 /* Figure out the largest section size. Also, if generating
3341 relocatable output, count the relocs. */
3342 trsize = 0;
3343 drsize = 0;
3344 max_contents_size = 0;
3345 max_relocs_size = 0;
3346 max_sym_count = 0;
3347 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3349 size_t sz;
3351 if (info->relocatable)
3353 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
3355 trsize += exec_hdr (sub)->a_trsize;
3356 drsize += exec_hdr (sub)->a_drsize;
3358 else
3360 /* FIXME: We need to identify the .text and .data sections
3361 and call get_reloc_upper_bound and canonicalize_reloc to
3362 work out the number of relocs needed, and then multiply
3363 by the reloc size. */
3364 (*_bfd_error_handler)
3365 ("%s: relocatable link from %s to %s not supported",
3366 bfd_get_filename (abfd),
3367 sub->xvec->name, abfd->xvec->name);
3368 bfd_set_error (bfd_error_invalid_operation);
3369 goto error_return;
3373 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
3375 sz = obj_textsec (sub)->size;
3376 if (sz > max_contents_size)
3377 max_contents_size = sz;
3378 sz = obj_datasec (sub)->size;
3379 if (sz > max_contents_size)
3380 max_contents_size = sz;
3382 sz = exec_hdr (sub)->a_trsize;
3383 if (sz > max_relocs_size)
3384 max_relocs_size = sz;
3385 sz = exec_hdr (sub)->a_drsize;
3386 if (sz > max_relocs_size)
3387 max_relocs_size = sz;
3389 sz = obj_aout_external_sym_count (sub);
3390 if (sz > max_sym_count)
3391 max_sym_count = sz;
3395 if (info->relocatable)
3397 if (obj_textsec (abfd) != (asection *) NULL)
3398 trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
3399 ->link_order_head)
3400 * obj_reloc_entry_size (abfd));
3401 if (obj_datasec (abfd) != (asection *) NULL)
3402 drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
3403 ->link_order_head)
3404 * obj_reloc_entry_size (abfd));
3407 exec_hdr (abfd)->a_trsize = trsize;
3408 exec_hdr (abfd)->a_drsize = drsize;
3410 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
3412 /* Adjust the section sizes and vmas according to the magic number.
3413 This sets a_text, a_data and a_bss in the exec_hdr and sets the
3414 filepos for each section. */
3415 if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
3416 goto error_return;
3418 /* The relocation and symbol file positions differ among a.out
3419 targets. We are passed a callback routine from the backend
3420 specific code to handle this.
3421 FIXME: At this point we do not know how much space the symbol
3422 table will require. This will not work for any (nonstandard)
3423 a.out target that needs to know the symbol table size before it
3424 can compute the relocation file positions. This may or may not
3425 be the case for the hp300hpux target, for example. */
3426 (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
3427 &aout_info.symoff);
3428 obj_textsec (abfd)->rel_filepos = aout_info.treloff;
3429 obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
3430 obj_sym_filepos (abfd) = aout_info.symoff;
3432 /* We keep a count of the symbols as we output them. */
3433 obj_aout_external_sym_count (abfd) = 0;
3435 /* We accumulate the string table as we write out the symbols. */
3436 aout_info.strtab = _bfd_stringtab_init ();
3437 if (aout_info.strtab == NULL)
3438 goto error_return;
3440 /* Allocate buffers to hold section contents and relocs. */
3441 aout_info.contents = (bfd_byte *) bfd_malloc (max_contents_size);
3442 aout_info.relocs = (PTR) bfd_malloc (max_relocs_size);
3443 aout_info.symbol_map = (int *) bfd_malloc (max_sym_count * sizeof (int *));
3444 aout_info.output_syms = ((struct external_nlist *)
3445 bfd_malloc ((max_sym_count + 1)
3446 * sizeof (struct external_nlist)));
3447 if ((aout_info.contents == NULL && max_contents_size != 0)
3448 || (aout_info.relocs == NULL && max_relocs_size != 0)
3449 || (aout_info.symbol_map == NULL && max_sym_count != 0)
3450 || aout_info.output_syms == NULL)
3451 goto error_return;
3453 /* If we have a symbol named __DYNAMIC, force it out now. This is
3454 required by SunOS. Doing this here rather than in sunos.c is a
3455 hack, but it's easier than exporting everything which would be
3456 needed. */
3458 struct aout_link_hash_entry *h;
3460 h = aout_link_hash_lookup (aout_hash_table (info), "__DYNAMIC",
3461 FALSE, FALSE, FALSE);
3462 if (h != NULL)
3463 aout_link_write_other_symbol (h, &aout_info);
3466 /* The most time efficient way to do the link would be to read all
3467 the input object files into memory and then sort out the
3468 information into the output file. Unfortunately, that will
3469 probably use too much memory. Another method would be to step
3470 through everything that composes the text section and write it
3471 out, and then everything that composes the data section and write
3472 it out, and then write out the relocs, and then write out the
3473 symbols. Unfortunately, that requires reading stuff from each
3474 input file several times, and we will not be able to keep all the
3475 input files open simultaneously, and reopening them will be slow.
3477 What we do is basically process one input file at a time. We do
3478 everything we need to do with an input file once--copy over the
3479 section contents, handle the relocation information, and write
3480 out the symbols--and then we throw away the information we read
3481 from it. This approach requires a lot of lseeks of the output
3482 file, which is unfortunate but still faster than reopening a lot
3483 of files.
3485 We use the output_has_begun field of the input BFDs to see
3486 whether we have already handled it. */
3487 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
3488 sub->output_has_begun = FALSE;
3490 /* Mark all sections which are to be included in the link. This
3491 will normally be every section. We need to do this so that we
3492 can identify any sections which the linker has decided to not
3493 include. */
3494 for (o = abfd->sections; o != NULL; o = o->next)
3496 for (p = o->link_order_head; p != NULL; p = p->next)
3498 if (p->type == bfd_indirect_link_order)
3499 p->u.indirect.section->linker_mark = TRUE;
3503 have_link_order_relocs = FALSE;
3504 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3506 for (p = o->link_order_head;
3507 p != (struct bfd_link_order *) NULL;
3508 p = p->next)
3510 if (p->type == bfd_indirect_link_order
3511 && (bfd_get_flavour (p->u.indirect.section->owner)
3512 == bfd_target_aout_flavour))
3514 bfd *input_bfd;
3516 input_bfd = p->u.indirect.section->owner;
3517 if (! input_bfd->output_has_begun)
3519 if (! aout_link_input_bfd (&aout_info, input_bfd))
3520 goto error_return;
3521 input_bfd->output_has_begun = TRUE;
3524 else if (p->type == bfd_section_reloc_link_order
3525 || p->type == bfd_symbol_reloc_link_order)
3527 /* These are handled below. */
3528 have_link_order_relocs = TRUE;
3530 else
3532 if (! _bfd_default_link_order (abfd, info, o, p))
3533 goto error_return;
3538 /* Write out any symbols that we have not already written out. */
3539 aout_link_hash_traverse (aout_hash_table (info),
3540 aout_link_write_other_symbol,
3541 (PTR) &aout_info);
3543 /* Now handle any relocs we were asked to create by the linker.
3544 These did not come from any input file. We must do these after
3545 we have written out all the symbols, so that we know the symbol
3546 indices to use. */
3547 if (have_link_order_relocs)
3549 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3551 for (p = o->link_order_head;
3552 p != (struct bfd_link_order *) NULL;
3553 p = p->next)
3555 if (p->type == bfd_section_reloc_link_order
3556 || p->type == bfd_symbol_reloc_link_order)
3558 if (! aout_link_reloc_link_order (&aout_info, o, p))
3559 goto error_return;
3565 if (aout_info.contents != NULL)
3567 free (aout_info.contents);
3568 aout_info.contents = NULL;
3570 if (aout_info.relocs != NULL)
3572 free (aout_info.relocs);
3573 aout_info.relocs = NULL;
3575 if (aout_info.symbol_map != NULL)
3577 free (aout_info.symbol_map);
3578 aout_info.symbol_map = NULL;
3580 if (aout_info.output_syms != NULL)
3582 free (aout_info.output_syms);
3583 aout_info.output_syms = NULL;
3585 if (includes_hash_initialized)
3587 bfd_hash_table_free (&aout_info.includes.root);
3588 includes_hash_initialized = FALSE;
3591 /* Finish up any dynamic linking we may be doing. */
3592 if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
3594 if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
3595 goto error_return;
3598 /* Update the header information. */
3599 abfd->symcount = obj_aout_external_sym_count (abfd);
3600 exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
3601 obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
3602 obj_textsec (abfd)->reloc_count =
3603 exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
3604 obj_datasec (abfd)->reloc_count =
3605 exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
3607 /* Write out the string table, unless there are no symbols. */
3608 if (abfd->symcount > 0)
3610 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
3611 || ! emit_stringtab (abfd, aout_info.strtab))
3612 goto error_return;
3614 else if (obj_textsec (abfd)->reloc_count == 0
3615 && obj_datasec (abfd)->reloc_count == 0)
3617 bfd_byte b;
3619 b = 0;
3620 if (bfd_seek (abfd,
3621 (file_ptr) (obj_datasec (abfd)->filepos
3622 + exec_hdr (abfd)->a_data
3623 - 1),
3624 SEEK_SET) != 0
3625 || bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
3626 goto error_return;
3629 return TRUE;
3631 error_return:
3632 if (aout_info.contents != NULL)
3633 free (aout_info.contents);
3634 if (aout_info.relocs != NULL)
3635 free (aout_info.relocs);
3636 if (aout_info.symbol_map != NULL)
3637 free (aout_info.symbol_map);
3638 if (aout_info.output_syms != NULL)
3639 free (aout_info.output_syms);
3640 if (includes_hash_initialized)
3641 bfd_hash_table_free (&aout_info.includes.root);
3642 return FALSE;
3645 /* Link an a.out input BFD into the output file. */
3647 static bfd_boolean
3648 aout_link_input_bfd (finfo, input_bfd)
3649 struct aout_final_link_info *finfo;
3650 bfd *input_bfd;
3652 bfd_size_type sym_count;
3654 BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
3656 /* If this is a dynamic object, it may need special handling. */
3657 if ((input_bfd->flags & DYNAMIC) != 0
3658 && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
3660 return ((*aout_backend_info (input_bfd)->link_dynamic_object)
3661 (finfo->info, input_bfd));
3664 /* Get the symbols. We probably have them already, unless
3665 finfo->info->keep_memory is FALSE. */
3666 if (! aout_get_external_symbols (input_bfd))
3667 return FALSE;
3669 sym_count = obj_aout_external_sym_count (input_bfd);
3671 /* Write out the symbols and get a map of the new indices. The map
3672 is placed into finfo->symbol_map. */
3673 if (! aout_link_write_symbols (finfo, input_bfd))
3674 return FALSE;
3676 /* Relocate and write out the sections. These functions use the
3677 symbol map created by aout_link_write_symbols. The linker_mark
3678 field will be set if these sections are to be included in the
3679 link, which will normally be the case. */
3680 if (obj_textsec (input_bfd)->linker_mark)
3682 if (! aout_link_input_section (finfo, input_bfd,
3683 obj_textsec (input_bfd),
3684 &finfo->treloff,
3685 exec_hdr (input_bfd)->a_trsize))
3686 return FALSE;
3688 if (obj_datasec (input_bfd)->linker_mark)
3690 if (! aout_link_input_section (finfo, input_bfd,
3691 obj_datasec (input_bfd),
3692 &finfo->dreloff,
3693 exec_hdr (input_bfd)->a_drsize))
3694 return FALSE;
3697 /* If we are not keeping memory, we don't need the symbols any
3698 longer. We still need them if we are keeping memory, because the
3699 strings in the hash table point into them. */
3700 if (! finfo->info->keep_memory)
3702 if (! aout_link_free_symbols (input_bfd))
3703 return FALSE;
3706 return TRUE;
3709 /* Adjust and write out the symbols for an a.out file. Set the new
3710 symbol indices into a symbol_map. */
3712 static bfd_boolean
3713 aout_link_write_symbols (finfo, input_bfd)
3714 struct aout_final_link_info *finfo;
3715 bfd *input_bfd;
3717 bfd *output_bfd;
3718 bfd_size_type sym_count;
3719 char *strings;
3720 enum bfd_link_strip strip;
3721 enum bfd_link_discard discard;
3722 struct external_nlist *outsym;
3723 bfd_size_type strtab_index;
3724 register struct external_nlist *sym;
3725 struct external_nlist *sym_end;
3726 struct aout_link_hash_entry **sym_hash;
3727 int *symbol_map;
3728 bfd_boolean pass;
3729 bfd_boolean skip_next;
3731 output_bfd = finfo->output_bfd;
3732 sym_count = obj_aout_external_sym_count (input_bfd);
3733 strings = obj_aout_external_strings (input_bfd);
3734 strip = finfo->info->strip;
3735 discard = finfo->info->discard;
3736 outsym = finfo->output_syms;
3738 /* First write out a symbol for this object file, unless we are
3739 discarding such symbols. */
3740 if (strip != strip_all
3741 && (strip != strip_some
3742 || bfd_hash_lookup (finfo->info->keep_hash, input_bfd->filename,
3743 FALSE, FALSE) != NULL)
3744 && discard != discard_all)
3746 H_PUT_8 (output_bfd, N_TEXT, outsym->e_type);
3747 strtab_index = add_to_stringtab (output_bfd, finfo->strtab,
3748 input_bfd->filename, FALSE);
3749 if (strtab_index == (bfd_size_type) -1)
3750 return FALSE;
3751 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
3752 PUT_WORD (output_bfd,
3753 (bfd_get_section_vma (output_bfd,
3754 obj_textsec (input_bfd)->output_section)
3755 + obj_textsec (input_bfd)->output_offset),
3756 outsym->e_value);
3757 ++obj_aout_external_sym_count (output_bfd);
3758 ++outsym;
3761 pass = FALSE;
3762 skip_next = FALSE;
3763 sym = obj_aout_external_syms (input_bfd);
3764 sym_end = sym + sym_count;
3765 sym_hash = obj_aout_sym_hashes (input_bfd);
3766 symbol_map = finfo->symbol_map;
3767 memset (symbol_map, 0, (size_t) sym_count * sizeof *symbol_map);
3768 for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
3770 const char *name;
3771 int type;
3772 struct aout_link_hash_entry *h;
3773 bfd_boolean skip;
3774 asection *symsec;
3775 bfd_vma val = 0;
3776 bfd_boolean copy;
3778 /* We set *symbol_map to 0 above for all symbols. If it has
3779 already been set to -1 for this symbol, it means that we are
3780 discarding it because it appears in a duplicate header file.
3781 See the N_BINCL code below. */
3782 if (*symbol_map == -1)
3783 continue;
3785 /* Initialize *symbol_map to -1, which means that the symbol was
3786 not copied into the output file. We will change it later if
3787 we do copy the symbol over. */
3788 *symbol_map = -1;
3790 type = H_GET_8 (input_bfd, sym->e_type);
3791 name = strings + GET_WORD (input_bfd, sym->e_strx);
3793 h = NULL;
3795 if (pass)
3797 /* Pass this symbol through. It is the target of an
3798 indirect or warning symbol. */
3799 val = GET_WORD (input_bfd, sym->e_value);
3800 pass = FALSE;
3802 else if (skip_next)
3804 /* Skip this symbol, which is the target of an indirect
3805 symbol that we have changed to no longer be an indirect
3806 symbol. */
3807 skip_next = FALSE;
3808 continue;
3810 else
3812 struct aout_link_hash_entry *hresolve;
3814 /* We have saved the hash table entry for this symbol, if
3815 there is one. Note that we could just look it up again
3816 in the hash table, provided we first check that it is an
3817 external symbol. */
3818 h = *sym_hash;
3820 /* Use the name from the hash table, in case the symbol was
3821 wrapped. */
3822 if (h != NULL)
3823 name = h->root.root.string;
3825 /* If this is an indirect or warning symbol, then change
3826 hresolve to the base symbol. We also change *sym_hash so
3827 that the relocation routines relocate against the real
3828 symbol. */
3829 hresolve = h;
3830 if (h != (struct aout_link_hash_entry *) NULL
3831 && (h->root.type == bfd_link_hash_indirect
3832 || h->root.type == bfd_link_hash_warning))
3834 hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
3835 while (hresolve->root.type == bfd_link_hash_indirect
3836 || hresolve->root.type == bfd_link_hash_warning)
3837 hresolve = ((struct aout_link_hash_entry *)
3838 hresolve->root.u.i.link);
3839 *sym_hash = hresolve;
3842 /* If the symbol has already been written out, skip it. */
3843 if (h != (struct aout_link_hash_entry *) NULL
3844 && h->root.type != bfd_link_hash_warning
3845 && h->written)
3847 if ((type & N_TYPE) == N_INDR
3848 || type == N_WARNING)
3849 skip_next = TRUE;
3850 *symbol_map = h->indx;
3851 continue;
3854 /* See if we are stripping this symbol. */
3855 skip = FALSE;
3856 switch (strip)
3858 case strip_none:
3859 break;
3860 case strip_debugger:
3861 if ((type & N_STAB) != 0)
3862 skip = TRUE;
3863 break;
3864 case strip_some:
3865 if (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE)
3866 == NULL)
3867 skip = TRUE;
3868 break;
3869 case strip_all:
3870 skip = TRUE;
3871 break;
3873 if (skip)
3875 if (h != (struct aout_link_hash_entry *) NULL)
3876 h->written = TRUE;
3877 continue;
3880 /* Get the value of the symbol. */
3881 if ((type & N_TYPE) == N_TEXT
3882 || type == N_WEAKT)
3883 symsec = obj_textsec (input_bfd);
3884 else if ((type & N_TYPE) == N_DATA
3885 || type == N_WEAKD)
3886 symsec = obj_datasec (input_bfd);
3887 else if ((type & N_TYPE) == N_BSS
3888 || type == N_WEAKB)
3889 symsec = obj_bsssec (input_bfd);
3890 else if ((type & N_TYPE) == N_ABS
3891 || type == N_WEAKA)
3892 symsec = bfd_abs_section_ptr;
3893 else if (((type & N_TYPE) == N_INDR
3894 && (hresolve == (struct aout_link_hash_entry *) NULL
3895 || (hresolve->root.type != bfd_link_hash_defined
3896 && hresolve->root.type != bfd_link_hash_defweak
3897 && hresolve->root.type != bfd_link_hash_common)))
3898 || type == N_WARNING)
3900 /* Pass the next symbol through unchanged. The
3901 condition above for indirect symbols is so that if
3902 the indirect symbol was defined, we output it with
3903 the correct definition so the debugger will
3904 understand it. */
3905 pass = TRUE;
3906 val = GET_WORD (input_bfd, sym->e_value);
3907 symsec = NULL;
3909 else if ((type & N_STAB) != 0)
3911 val = GET_WORD (input_bfd, sym->e_value);
3912 symsec = NULL;
3914 else
3916 /* If we get here with an indirect symbol, it means that
3917 we are outputting it with a real definition. In such
3918 a case we do not want to output the next symbol,
3919 which is the target of the indirection. */
3920 if ((type & N_TYPE) == N_INDR)
3921 skip_next = TRUE;
3923 symsec = NULL;
3925 /* We need to get the value from the hash table. We use
3926 hresolve so that if we have defined an indirect
3927 symbol we output the final definition. */
3928 if (h == (struct aout_link_hash_entry *) NULL)
3930 switch (type & N_TYPE)
3932 case N_SETT:
3933 symsec = obj_textsec (input_bfd);
3934 break;
3935 case N_SETD:
3936 symsec = obj_datasec (input_bfd);
3937 break;
3938 case N_SETB:
3939 symsec = obj_bsssec (input_bfd);
3940 break;
3941 case N_SETA:
3942 symsec = bfd_abs_section_ptr;
3943 break;
3944 default:
3945 val = 0;
3946 break;
3949 else if (hresolve->root.type == bfd_link_hash_defined
3950 || hresolve->root.type == bfd_link_hash_defweak)
3952 asection *input_section;
3953 asection *output_section;
3955 /* This case usually means a common symbol which was
3956 turned into a defined symbol. */
3957 input_section = hresolve->root.u.def.section;
3958 output_section = input_section->output_section;
3959 BFD_ASSERT (bfd_is_abs_section (output_section)
3960 || output_section->owner == output_bfd);
3961 val = (hresolve->root.u.def.value
3962 + bfd_get_section_vma (output_bfd, output_section)
3963 + input_section->output_offset);
3965 /* Get the correct type based on the section. If
3966 this is a constructed set, force it to be
3967 globally visible. */
3968 if (type == N_SETT
3969 || type == N_SETD
3970 || type == N_SETB
3971 || type == N_SETA)
3972 type |= N_EXT;
3974 type &=~ N_TYPE;
3976 if (output_section == obj_textsec (output_bfd))
3977 type |= (hresolve->root.type == bfd_link_hash_defined
3978 ? N_TEXT
3979 : N_WEAKT);
3980 else if (output_section == obj_datasec (output_bfd))
3981 type |= (hresolve->root.type == bfd_link_hash_defined
3982 ? N_DATA
3983 : N_WEAKD);
3984 else if (output_section == obj_bsssec (output_bfd))
3985 type |= (hresolve->root.type == bfd_link_hash_defined
3986 ? N_BSS
3987 : N_WEAKB);
3988 else
3989 type |= (hresolve->root.type == bfd_link_hash_defined
3990 ? N_ABS
3991 : N_WEAKA);
3993 else if (hresolve->root.type == bfd_link_hash_common)
3994 val = hresolve->root.u.c.size;
3995 else if (hresolve->root.type == bfd_link_hash_undefweak)
3997 val = 0;
3998 type = N_WEAKU;
4000 else
4001 val = 0;
4003 if (symsec != (asection *) NULL)
4004 val = (symsec->output_section->vma
4005 + symsec->output_offset
4006 + (GET_WORD (input_bfd, sym->e_value)
4007 - symsec->vma));
4009 /* If this is a global symbol set the written flag, and if
4010 it is a local symbol see if we should discard it. */
4011 if (h != (struct aout_link_hash_entry *) NULL)
4013 h->written = TRUE;
4014 h->indx = obj_aout_external_sym_count (output_bfd);
4016 else if ((type & N_TYPE) != N_SETT
4017 && (type & N_TYPE) != N_SETD
4018 && (type & N_TYPE) != N_SETB
4019 && (type & N_TYPE) != N_SETA)
4021 switch (discard)
4023 case discard_none:
4024 case discard_sec_merge:
4025 break;
4026 case discard_l:
4027 if ((type & N_STAB) == 0
4028 && bfd_is_local_label_name (input_bfd, name))
4029 skip = TRUE;
4030 break;
4031 case discard_all:
4032 skip = TRUE;
4033 break;
4035 if (skip)
4037 pass = FALSE;
4038 continue;
4042 /* An N_BINCL symbol indicates the start of the stabs
4043 entries for a header file. We need to scan ahead to the
4044 next N_EINCL symbol, ignoring nesting, adding up all the
4045 characters in the symbol names, not including the file
4046 numbers in types (the first number after an open
4047 parenthesis). */
4048 if (type == N_BINCL)
4050 struct external_nlist *incl_sym;
4051 int nest;
4052 struct aout_link_includes_entry *incl_entry;
4053 struct aout_link_includes_totals *t;
4055 val = 0;
4056 nest = 0;
4057 for (incl_sym = sym + 1; incl_sym < sym_end; incl_sym++)
4059 int incl_type;
4061 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
4062 if (incl_type == N_EINCL)
4064 if (nest == 0)
4065 break;
4066 --nest;
4068 else if (incl_type == N_BINCL)
4069 ++nest;
4070 else if (nest == 0)
4072 const char *s;
4074 s = strings + GET_WORD (input_bfd, incl_sym->e_strx);
4075 for (; *s != '\0'; s++)
4077 val += *s;
4078 if (*s == '(')
4080 /* Skip the file number. */
4081 ++s;
4082 while (ISDIGIT (*s))
4083 ++s;
4084 --s;
4090 /* If we have already included a header file with the
4091 same value, then replace this one with an N_EXCL
4092 symbol. */
4093 copy = ! finfo->info->keep_memory;
4094 incl_entry = aout_link_includes_lookup (&finfo->includes,
4095 name, TRUE, copy);
4096 if (incl_entry == NULL)
4097 return FALSE;
4098 for (t = incl_entry->totals; t != NULL; t = t->next)
4099 if (t->total == val)
4100 break;
4101 if (t == NULL)
4103 /* This is the first time we have seen this header
4104 file with this set of stabs strings. */
4105 t = ((struct aout_link_includes_totals *)
4106 bfd_hash_allocate (&finfo->includes.root,
4107 sizeof *t));
4108 if (t == NULL)
4109 return FALSE;
4110 t->total = val;
4111 t->next = incl_entry->totals;
4112 incl_entry->totals = t;
4114 else
4116 int *incl_map;
4118 /* This is a duplicate header file. We must change
4119 it to be an N_EXCL entry, and mark all the
4120 included symbols to prevent outputting them. */
4121 type = N_EXCL;
4123 nest = 0;
4124 for (incl_sym = sym + 1, incl_map = symbol_map + 1;
4125 incl_sym < sym_end;
4126 incl_sym++, incl_map++)
4128 int incl_type;
4130 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
4131 if (incl_type == N_EINCL)
4133 if (nest == 0)
4135 *incl_map = -1;
4136 break;
4138 --nest;
4140 else if (incl_type == N_BINCL)
4141 ++nest;
4142 else if (nest == 0)
4143 *incl_map = -1;
4149 /* Copy this symbol into the list of symbols we are going to
4150 write out. */
4151 H_PUT_8 (output_bfd, type, outsym->e_type);
4152 copy = FALSE;
4153 if (! finfo->info->keep_memory)
4155 /* name points into a string table which we are going to
4156 free. If there is a hash table entry, use that string.
4157 Otherwise, copy name into memory. */
4158 if (h != (struct aout_link_hash_entry *) NULL)
4159 name = h->root.root.string;
4160 else
4161 copy = TRUE;
4163 strtab_index = add_to_stringtab (output_bfd, finfo->strtab,
4164 name, copy);
4165 if (strtab_index == (bfd_size_type) -1)
4166 return FALSE;
4167 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
4168 PUT_WORD (output_bfd, val, outsym->e_value);
4169 *symbol_map = obj_aout_external_sym_count (output_bfd);
4170 ++obj_aout_external_sym_count (output_bfd);
4171 ++outsym;
4174 /* Write out the output symbols we have just constructed. */
4175 if (outsym > finfo->output_syms)
4177 bfd_size_type size;
4179 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0)
4180 return FALSE;
4181 size = outsym - finfo->output_syms;
4182 size *= EXTERNAL_NLIST_SIZE;
4183 if (bfd_bwrite ((PTR) finfo->output_syms, size, output_bfd) != size)
4184 return FALSE;
4185 finfo->symoff += size;
4188 return TRUE;
4191 /* Write out a symbol that was not associated with an a.out input
4192 object. */
4194 static bfd_boolean
4195 aout_link_write_other_symbol (h, data)
4196 struct aout_link_hash_entry *h;
4197 PTR data;
4199 struct aout_final_link_info *finfo = (struct aout_final_link_info *) data;
4200 bfd *output_bfd;
4201 int type;
4202 bfd_vma val;
4203 struct external_nlist outsym;
4204 bfd_size_type indx;
4205 bfd_size_type amt;
4207 if (h->root.type == bfd_link_hash_warning)
4209 h = (struct aout_link_hash_entry *) h->root.u.i.link;
4210 if (h->root.type == bfd_link_hash_new)
4211 return TRUE;
4214 output_bfd = finfo->output_bfd;
4216 if (aout_backend_info (output_bfd)->write_dynamic_symbol != NULL)
4218 if (! ((*aout_backend_info (output_bfd)->write_dynamic_symbol)
4219 (output_bfd, finfo->info, h)))
4221 /* FIXME: No way to handle errors. */
4222 abort ();
4226 if (h->written)
4227 return TRUE;
4229 h->written = TRUE;
4231 /* An indx of -2 means the symbol must be written. */
4232 if (h->indx != -2
4233 && (finfo->info->strip == strip_all
4234 || (finfo->info->strip == strip_some
4235 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
4236 FALSE, FALSE) == NULL)))
4237 return TRUE;
4239 switch (h->root.type)
4241 default:
4242 abort ();
4243 /* Avoid variable not initialized warnings. */
4244 return TRUE;
4245 case bfd_link_hash_new:
4246 /* This can happen for set symbols when sets are not being
4247 built. */
4248 return TRUE;
4249 case bfd_link_hash_undefined:
4250 type = N_UNDF | N_EXT;
4251 val = 0;
4252 break;
4253 case bfd_link_hash_defined:
4254 case bfd_link_hash_defweak:
4256 asection *sec;
4258 sec = h->root.u.def.section->output_section;
4259 BFD_ASSERT (bfd_is_abs_section (sec)
4260 || sec->owner == output_bfd);
4261 if (sec == obj_textsec (output_bfd))
4262 type = h->root.type == bfd_link_hash_defined ? N_TEXT : N_WEAKT;
4263 else if (sec == obj_datasec (output_bfd))
4264 type = h->root.type == bfd_link_hash_defined ? N_DATA : N_WEAKD;
4265 else if (sec == obj_bsssec (output_bfd))
4266 type = h->root.type == bfd_link_hash_defined ? N_BSS : N_WEAKB;
4267 else
4268 type = h->root.type == bfd_link_hash_defined ? N_ABS : N_WEAKA;
4269 type |= N_EXT;
4270 val = (h->root.u.def.value
4271 + sec->vma
4272 + h->root.u.def.section->output_offset);
4274 break;
4275 case bfd_link_hash_common:
4276 type = N_UNDF | N_EXT;
4277 val = h->root.u.c.size;
4278 break;
4279 case bfd_link_hash_undefweak:
4280 type = N_WEAKU;
4281 val = 0;
4282 case bfd_link_hash_indirect:
4283 case bfd_link_hash_warning:
4284 /* FIXME: Ignore these for now. The circumstances under which
4285 they should be written out are not clear to me. */
4286 return TRUE;
4289 H_PUT_8 (output_bfd, type, outsym.e_type);
4290 indx = add_to_stringtab (output_bfd, finfo->strtab, h->root.root.string,
4291 FALSE);
4292 if (indx == (bfd_size_type) -1)
4294 /* FIXME: No way to handle errors. */
4295 abort ();
4297 PUT_WORD (output_bfd, indx, outsym.e_strx);
4298 PUT_WORD (output_bfd, val, outsym.e_value);
4300 amt = EXTERNAL_NLIST_SIZE;
4301 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0
4302 || bfd_bwrite ((PTR) &outsym, amt, output_bfd) != amt)
4304 /* FIXME: No way to handle errors. */
4305 abort ();
4308 finfo->symoff += amt;
4309 h->indx = obj_aout_external_sym_count (output_bfd);
4310 ++obj_aout_external_sym_count (output_bfd);
4312 return TRUE;
4315 /* Link an a.out section into the output file. */
4317 static bfd_boolean
4318 aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr,
4319 rel_size)
4320 struct aout_final_link_info *finfo;
4321 bfd *input_bfd;
4322 asection *input_section;
4323 file_ptr *reloff_ptr;
4324 bfd_size_type rel_size;
4326 bfd_size_type input_size;
4327 PTR relocs;
4329 /* Get the section contents. */
4330 input_size = input_section->size;
4331 if (! bfd_get_section_contents (input_bfd, input_section,
4332 (PTR) finfo->contents,
4333 (file_ptr) 0, input_size))
4334 return FALSE;
4336 /* Read in the relocs if we haven't already done it. */
4337 if (aout_section_data (input_section) != NULL
4338 && aout_section_data (input_section)->relocs != NULL)
4339 relocs = aout_section_data (input_section)->relocs;
4340 else
4342 relocs = finfo->relocs;
4343 if (rel_size > 0)
4345 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4346 || bfd_bread (relocs, rel_size, input_bfd) != rel_size)
4347 return FALSE;
4351 /* Relocate the section contents. */
4352 if (! pdp11_aout_link_input_section (finfo, input_bfd, input_section,
4353 (bfd_byte *) relocs,
4354 rel_size, finfo->contents))
4355 return FALSE;
4357 /* Write out the section contents. */
4358 if (! bfd_set_section_contents (finfo->output_bfd,
4359 input_section->output_section,
4360 (PTR) finfo->contents,
4361 (file_ptr) input_section->output_offset,
4362 input_size))
4363 return FALSE;
4365 /* If we are producing relocatable output, the relocs were
4366 modified, and we now write them out. */
4367 if (finfo->info->relocatable && rel_size > 0)
4369 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
4370 return FALSE;
4371 if (bfd_bwrite (relocs, rel_size, finfo->output_bfd) != rel_size)
4372 return FALSE;
4373 *reloff_ptr += rel_size;
4375 /* Assert that the relocs have not run into the symbols, and
4376 that if these are the text relocs they have not run into the
4377 data relocs. */
4378 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
4379 && (reloff_ptr != &finfo->treloff
4380 || (*reloff_ptr
4381 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
4384 return TRUE;
4387 /* Get the section corresponding to a reloc index. */
4389 static INLINE asection *
4390 aout_reloc_type_to_section (abfd, type)
4391 bfd *abfd;
4392 int type;
4394 switch (type)
4396 case RTEXT:
4397 return obj_textsec (abfd);
4398 case RDATA:
4399 return obj_datasec (abfd);
4400 case RBSS:
4401 return obj_bsssec (abfd);
4402 case RABS:
4403 return bfd_abs_section_ptr;
4404 case REXT:
4405 return bfd_und_section_ptr;
4406 default:
4407 abort ();
4411 static bfd_boolean
4412 pdp11_aout_link_input_section (finfo, input_bfd, input_section, relocs,
4413 rel_size, contents)
4414 struct aout_final_link_info *finfo;
4415 bfd *input_bfd;
4416 asection *input_section;
4417 bfd_byte *relocs;
4418 bfd_size_type rel_size;
4419 bfd_byte *contents;
4421 bfd_boolean (*check_dynamic_reloc)
4422 PARAMS ((struct bfd_link_info *, bfd *, asection *,
4423 struct aout_link_hash_entry *, PTR, bfd_byte *, bfd_boolean *,
4424 bfd_vma *));
4425 bfd *output_bfd;
4426 bfd_boolean relocatable;
4427 struct external_nlist *syms;
4428 char *strings;
4429 struct aout_link_hash_entry **sym_hashes;
4430 int *symbol_map;
4431 bfd_size_type reloc_count;
4432 bfd_byte *rel;
4433 bfd_byte *rel_end;
4435 output_bfd = finfo->output_bfd;
4436 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
4438 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_SIZE);
4439 BFD_ASSERT (input_bfd->xvec->header_byteorder
4440 == output_bfd->xvec->header_byteorder);
4442 relocatable = finfo->info->relocatable;
4443 syms = obj_aout_external_syms (input_bfd);
4444 strings = obj_aout_external_strings (input_bfd);
4445 sym_hashes = obj_aout_sym_hashes (input_bfd);
4446 symbol_map = finfo->symbol_map;
4448 reloc_count = rel_size / RELOC_SIZE;
4449 rel = relocs;
4450 rel_end = rel + rel_size;
4451 for (; rel < rel_end; rel += RELOC_SIZE)
4453 bfd_vma r_addr;
4454 int r_index;
4455 int r_type;
4456 int r_pcrel;
4457 int r_extern;
4458 reloc_howto_type *howto;
4459 struct aout_link_hash_entry *h = NULL;
4460 bfd_vma relocation;
4461 bfd_reloc_status_type r;
4462 int reloc_entry;
4464 reloc_entry = GET_WORD (input_bfd, (PTR)rel);
4465 if (reloc_entry == 0)
4466 continue;
4469 unsigned int howto_idx;
4471 r_index = (reloc_entry & RIDXMASK) >> 4;
4472 r_type = reloc_entry & RTYPE;
4473 r_pcrel = reloc_entry & RELFLG;
4474 r_addr = (char *)rel - (char *)relocs;
4476 r_extern = (r_type == REXT);
4478 howto_idx = r_pcrel;
4479 BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_pdp11));
4480 howto = howto_table_pdp11 + howto_idx;
4483 if (relocatable)
4485 /* We are generating a relocatable output file, and must
4486 modify the reloc accordingly. */
4487 if (r_extern)
4489 /* If we know the symbol this relocation is against,
4490 convert it into a relocation against a section. This
4491 is what the native linker does. */
4492 h = sym_hashes[r_index];
4493 if (h != (struct aout_link_hash_entry *) NULL
4494 && (h->root.type == bfd_link_hash_defined
4495 || h->root.type == bfd_link_hash_defweak))
4497 asection *output_section;
4499 /* Compute a new r_index. */
4500 output_section = h->root.u.def.section->output_section;
4501 if (output_section == obj_textsec (output_bfd))
4502 r_type = N_TEXT;
4503 else if (output_section == obj_datasec (output_bfd))
4504 r_type = N_DATA;
4505 else if (output_section == obj_bsssec (output_bfd))
4506 r_type = N_BSS;
4507 else
4508 r_type = N_ABS;
4510 /* Add the symbol value and the section VMA to the
4511 addend stored in the contents. */
4512 relocation = (h->root.u.def.value
4513 + output_section->vma
4514 + h->root.u.def.section->output_offset);
4516 else
4518 /* We must change r_index according to the symbol
4519 map. */
4520 r_index = symbol_map[r_index];
4522 if (r_index == -1)
4524 if (h != NULL)
4526 /* We decided to strip this symbol, but it
4527 turns out that we can't. Note that we
4528 lose the other and desc information here.
4529 I don't think that will ever matter for a
4530 global symbol. */
4531 if (h->indx < 0)
4533 h->indx = -2;
4534 h->written = FALSE;
4535 if (! aout_link_write_other_symbol (h,
4536 (PTR) finfo))
4537 return FALSE;
4539 r_index = h->indx;
4541 else
4543 const char *name;
4545 name = strings + GET_WORD (input_bfd,
4546 syms[r_index].e_strx);
4547 if (! ((*finfo->info->callbacks->unattached_reloc)
4548 (finfo->info, name, input_bfd, input_section,
4549 r_addr)))
4550 return FALSE;
4551 r_index = 0;
4555 relocation = 0;
4558 /* Write out the new r_index value. */
4559 reloc_entry = GET_WORD (input_bfd, rel);
4560 reloc_entry &= RIDXMASK;
4561 reloc_entry |= r_index << 4;
4562 PUT_WORD (input_bfd, reloc_entry, rel);
4564 else
4566 asection *section;
4568 /* This is a relocation against a section. We must
4569 adjust by the amount that the section moved. */
4570 section = aout_reloc_type_to_section (input_bfd, r_type);
4571 relocation = (section->output_section->vma
4572 + section->output_offset
4573 - section->vma);
4576 /* Change the address of the relocation. */
4577 fprintf (stderr, "TODO: change the address of the relocation\n");
4579 /* Adjust a PC relative relocation by removing the reference
4580 to the original address in the section and including the
4581 reference to the new address. */
4582 if (r_pcrel)
4583 relocation -= (input_section->output_section->vma
4584 + input_section->output_offset
4585 - input_section->vma);
4587 #ifdef MY_relocatable_reloc
4588 MY_relocatable_reloc (howto, output_bfd, rel, relocation, r_addr);
4589 #endif
4591 if (relocation == 0)
4592 r = bfd_reloc_ok;
4593 else
4594 r = MY_relocate_contents (howto,
4595 input_bfd, relocation,
4596 contents + r_addr);
4598 else
4600 bfd_boolean hundef;
4602 /* We are generating an executable, and must do a full
4603 relocation. */
4604 hundef = FALSE;
4605 if (r_extern)
4607 h = sym_hashes[r_index];
4609 if (h != (struct aout_link_hash_entry *) NULL
4610 && (h->root.type == bfd_link_hash_defined
4611 || h->root.type == bfd_link_hash_defweak))
4613 relocation = (h->root.u.def.value
4614 + h->root.u.def.section->output_section->vma
4615 + h->root.u.def.section->output_offset);
4617 else if (h != (struct aout_link_hash_entry *) NULL
4618 && h->root.type == bfd_link_hash_undefweak)
4619 relocation = 0;
4620 else
4622 hundef = TRUE;
4623 relocation = 0;
4626 else
4628 asection *section;
4630 section = aout_reloc_type_to_section (input_bfd, r_type);
4631 relocation = (section->output_section->vma
4632 + section->output_offset
4633 - section->vma);
4634 if (r_pcrel)
4635 relocation += input_section->vma;
4638 if (check_dynamic_reloc != NULL)
4640 bfd_boolean skip;
4642 if (! ((*check_dynamic_reloc)
4643 (finfo->info, input_bfd, input_section, h,
4644 (PTR) rel, contents, &skip, &relocation)))
4645 return FALSE;
4646 if (skip)
4647 continue;
4650 /* Now warn if a global symbol is undefined. We could not
4651 do this earlier, because check_dynamic_reloc might want
4652 to skip this reloc. */
4653 if (hundef && ! finfo->info->shared)
4655 const char *name;
4657 if (h != NULL)
4658 name = h->root.root.string;
4659 else
4660 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4661 if (! ((*finfo->info->callbacks->undefined_symbol)
4662 (finfo->info, name, input_bfd, input_section,
4663 r_addr, TRUE)))
4664 return FALSE;
4667 r = MY_final_link_relocate (howto,
4668 input_bfd, input_section,
4669 contents, r_addr, relocation,
4670 (bfd_vma) 0);
4673 if (r != bfd_reloc_ok)
4675 switch (r)
4677 default:
4678 case bfd_reloc_outofrange:
4679 abort ();
4680 case bfd_reloc_overflow:
4682 const char *name;
4684 if (h != NULL)
4685 name = NULL;
4686 else if (r_extern)
4687 name = strings + GET_WORD (input_bfd,
4688 syms[r_index].e_strx);
4689 else
4691 asection *s;
4693 s = aout_reloc_type_to_section (input_bfd, r_type);
4694 name = bfd_section_name (input_bfd, s);
4696 if (! ((*finfo->info->callbacks->reloc_overflow)
4697 (finfo->info, (h ? &h->root : NULL), name,
4698 howto->name, (bfd_vma) 0, input_bfd,
4699 input_section, r_addr)))
4700 return FALSE;
4702 break;
4707 return TRUE;
4710 /* Handle a link order which is supposed to generate a reloc. */
4712 static bfd_boolean
4713 aout_link_reloc_link_order (finfo, o, p)
4714 struct aout_final_link_info *finfo;
4715 asection *o;
4716 struct bfd_link_order *p;
4718 struct bfd_link_order_reloc *pr;
4719 int r_index;
4720 int r_extern;
4721 reloc_howto_type *howto;
4722 file_ptr *reloff_ptr;
4723 struct reloc_std_external srel;
4724 PTR rel_ptr;
4725 bfd_size_type rel_size;
4727 pr = p->u.reloc.p;
4729 if (p->type == bfd_section_reloc_link_order)
4731 r_extern = 0;
4732 if (bfd_is_abs_section (pr->u.section))
4733 r_index = N_ABS | N_EXT;
4734 else
4736 BFD_ASSERT (pr->u.section->owner == finfo->output_bfd);
4737 r_index = pr->u.section->target_index;
4740 else
4742 struct aout_link_hash_entry *h;
4744 BFD_ASSERT (p->type == bfd_symbol_reloc_link_order);
4745 r_extern = 1;
4746 h = ((struct aout_link_hash_entry *)
4747 bfd_wrapped_link_hash_lookup (finfo->output_bfd, finfo->info,
4748 pr->u.name, FALSE, FALSE, TRUE));
4749 if (h != (struct aout_link_hash_entry *) NULL
4750 && h->indx >= 0)
4751 r_index = h->indx;
4752 else if (h != NULL)
4754 /* We decided to strip this symbol, but it turns out that we
4755 can't. Note that we lose the other and desc information
4756 here. I don't think that will ever matter for a global
4757 symbol. */
4758 h->indx = -2;
4759 h->written = FALSE;
4760 if (! aout_link_write_other_symbol (h, (PTR) finfo))
4761 return FALSE;
4762 r_index = h->indx;
4764 else
4766 if (! ((*finfo->info->callbacks->unattached_reloc)
4767 (finfo->info, pr->u.name, (bfd *) NULL,
4768 (asection *) NULL, (bfd_vma) 0)))
4769 return FALSE;
4770 r_index = 0;
4774 howto = bfd_reloc_type_lookup (finfo->output_bfd, pr->reloc);
4775 if (howto == 0)
4777 bfd_set_error (bfd_error_bad_value);
4778 return FALSE;
4781 if (o == obj_textsec (finfo->output_bfd))
4782 reloff_ptr = &finfo->treloff;
4783 else if (o == obj_datasec (finfo->output_bfd))
4784 reloff_ptr = &finfo->dreloff;
4785 else
4786 abort ();
4788 #ifdef MY_put_reloc
4789 MY_put_reloc(finfo->output_bfd, r_extern, r_index, p->offset, howto,
4790 &srel);
4791 #else
4793 int r_pcrel;
4794 int r_baserel;
4795 int r_jmptable;
4796 int r_relative;
4797 int r_length;
4799 fprintf (stderr, "TODO: line %d in bfd/pdp11.c\n", __LINE__);
4801 r_pcrel = howto->pc_relative;
4802 r_baserel = (howto->type & 8) != 0;
4803 r_jmptable = (howto->type & 16) != 0;
4804 r_relative = (howto->type & 32) != 0;
4805 r_length = howto->size;
4807 PUT_WORD (finfo->output_bfd, p->offset, srel.r_address);
4808 if (bfd_header_big_endian (finfo->output_bfd))
4810 srel.r_index[0] = r_index >> 16;
4811 srel.r_index[1] = r_index >> 8;
4812 srel.r_index[2] = r_index;
4813 srel.r_type[0] =
4814 ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
4815 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
4816 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
4817 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
4818 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
4819 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
4821 else
4823 srel.r_index[2] = r_index >> 16;
4824 srel.r_index[1] = r_index >> 8;
4825 srel.r_index[0] = r_index;
4826 srel.r_type[0] =
4827 ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
4828 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
4829 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
4830 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
4831 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
4832 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
4835 #endif
4836 rel_ptr = (PTR) &srel;
4838 /* We have to write the addend into the object file, since
4839 standard a.out relocs are in place. It would be more
4840 reliable if we had the current contents of the file here,
4841 rather than assuming zeroes, but we can't read the file since
4842 it was opened using bfd_openw. */
4843 if (pr->addend != 0)
4845 bfd_size_type size;
4846 bfd_reloc_status_type r;
4847 bfd_byte *buf;
4848 bfd_boolean ok;
4850 size = bfd_get_reloc_size (howto);
4851 buf = (bfd_byte *) bfd_zmalloc (size);
4852 if (buf == (bfd_byte *) NULL)
4853 return FALSE;
4854 r = MY_relocate_contents (howto, finfo->output_bfd,
4855 pr->addend, buf);
4856 switch (r)
4858 case bfd_reloc_ok:
4859 break;
4860 default:
4861 case bfd_reloc_outofrange:
4862 abort ();
4863 case bfd_reloc_overflow:
4864 if (! ((*finfo->info->callbacks->reloc_overflow)
4865 (finfo->info, NULL,
4866 (p->type == bfd_section_reloc_link_order
4867 ? bfd_section_name (finfo->output_bfd,
4868 pr->u.section)
4869 : pr->u.name),
4870 howto->name, pr->addend, (bfd *) NULL,
4871 (asection *) NULL, (bfd_vma) 0)))
4873 free (buf);
4874 return FALSE;
4876 break;
4878 ok = bfd_set_section_contents (finfo->output_bfd, o,
4879 (PTR) buf,
4880 (file_ptr) p->offset,
4881 size);
4882 free (buf);
4883 if (! ok)
4884 return FALSE;
4887 rel_size = obj_reloc_entry_size (finfo->output_bfd);
4888 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0
4889 || bfd_bwrite (rel_ptr, rel_size, finfo->output_bfd) != rel_size)
4890 return FALSE;
4892 *reloff_ptr += rel_size;
4894 /* Assert that the relocs have not run into the symbols, and that n
4895 the text relocs have not run into the data relocs. */
4896 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
4897 && (reloff_ptr != &finfo->treloff
4898 || (*reloff_ptr
4899 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
4901 return TRUE;
4903 /* end of modified aoutx.h */
4905 static bfd_vma
4906 bfd_getp32 (const void *p)
4908 const bfd_byte *addr = p;
4909 unsigned long v;
4910 v = (unsigned long) addr[1] << 24;
4911 v |= (unsigned long) addr[0] << 16;
4912 v |= (unsigned long) addr[3] << 8;
4913 v |= (unsigned long) addr[2];
4914 return v;
4917 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
4919 static bfd_signed_vma
4920 bfd_getp_signed_32 (const void *p)
4922 const bfd_byte *addr = p;
4923 unsigned long v;
4924 v = (unsigned long) addr[1] << 24;
4925 v |= (unsigned long) addr[0] << 16;
4926 v |= (unsigned long) addr[3] << 8;
4927 v |= (unsigned long) addr[2];
4928 return COERCE32 (v);
4931 static void
4932 bfd_putp32 (bfd_vma data, void *p)
4934 bfd_byte *addr = p;
4935 addr[0] = (data >> 16) & 0xff;
4936 addr[1] = (data >> 24) & 0xff;
4937 addr[2] = (data >> 0) & 0xff;
4938 addr[3] = (data >> 8) & 0xff;