daily update
[binutils.git] / bfd / pdp11.c
blobf7514367f7d253214b82491773402f3ef5cf0674
1 /* BFD back-end for PDP-11 a.out binaries.
2 Copyright 2001, 2002 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 boolean MY(write_object_contents) PARAMS ((bfd *abfd));
135 #define MY_text_includes_header 1
137 bfd_vma bfd_getp32 PARAMS ((const bfd_byte *));
138 bfd_signed_vma bfd_getp_signed_32 PARAMS ((const bfd_byte *));
139 void bfd_putp32 PARAMS ((bfd_vma, bfd_byte *));
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 struct pdp11_aout_reloc_external
346 bfd_byte e_reloc_entry[2];
349 #define RELFLG 0x0001 /* pc-relative flag */
350 #define RTYPE 0x000e /* type mask */
351 #define RIDXMASK 0xfff0 /* index mask */
353 #define RABS 0x00 /* absolute */
354 #define RTEXT 0x02 /* text */
355 #define RDATA 0x04 /* data */
356 #define RBSS 0x06 /* bss */
357 #define REXT 0x08 /* external */
359 #define RINDEX(x) (((x) & 0xfff0) >> 4)
361 static boolean aout_get_external_symbols PARAMS ((bfd *));
362 static boolean translate_from_native_sym_flags
363 PARAMS ((bfd *, aout_symbol_type *));
364 static boolean translate_to_native_sym_flags
365 PARAMS ((bfd *, asymbol *, struct external_nlist *));
366 static void adjust_o_magic PARAMS ((bfd *, struct internal_exec *));
367 static void adjust_z_magic PARAMS ((bfd *, struct internal_exec *));
368 static void adjust_n_magic PARAMS ((bfd *, struct internal_exec *));
370 static int pdp11_aout_write_headers PARAMS ((bfd *, struct internal_exec *));
371 void pdp11_aout_swap_reloc_out PARAMS ((bfd *, arelent *, struct pdp11_aout_reloc_external *));
372 void pdp11_aout_swap_reloc_in
373 PARAMS ((bfd *, struct pdp11_aout_reloc_external *, arelent *,
374 bfd_size_type, asymbol **, bfd_size_type));
377 SUBSECTION
378 Relocations
380 DESCRIPTION
381 The file @file{aoutx.h} provides for both the @emph{standard}
382 and @emph{extended} forms of a.out relocation records.
384 The standard records contain only an
385 address, a symbol index, and a type field. The extended records
386 (used on 29ks and sparcs) also have a full integer for an
387 addend.
391 #ifndef MY_final_link_relocate
392 #define MY_final_link_relocate _bfd_final_link_relocate
393 #endif
395 #ifndef MY_relocate_contents
396 #define MY_relocate_contents _bfd_relocate_contents
397 #endif
399 reloc_howto_type howto_table_pdp11[] =
401 /* type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
402 HOWTO( 0, 0, 1, 16, false, 0, complain_overflow_signed,0,"16", true, 0x0000ffff,0x0000ffff, false),
403 HOWTO( 1, 0, 1, 16, true, 0, complain_overflow_signed,0,"DISP16", true, 0x0000ffff,0x0000ffff, false),
406 #define TABLE_SIZE(TABLE) (sizeof(TABLE)/sizeof(TABLE[0]))
408 reloc_howto_type *
409 NAME(aout,reloc_type_lookup) (abfd,code)
410 bfd * abfd ATTRIBUTE_UNUSED;
411 bfd_reloc_code_real_type code;
413 switch (code)
415 case BFD_RELOC_16:
416 return &howto_table_pdp11[0];
417 case BFD_RELOC_16_PCREL:
418 return &howto_table_pdp11[1];
419 default:
420 return (reloc_howto_type *)NULL;
424 static int
425 pdp11_aout_write_headers (abfd, execp)
426 bfd *abfd;
427 struct internal_exec *execp;
429 struct external_exec exec_bytes;
430 bfd_size_type text_size;
431 file_ptr text_end;
433 if (adata(abfd).magic == undecided_magic)
434 NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end);
436 execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE;
437 execp->a_entry = bfd_get_start_address (abfd);
439 if (obj_textsec (abfd)->reloc_count > 0 ||
440 obj_datasec (abfd)->reloc_count > 0)
442 execp->a_trsize = execp->a_text;
443 execp->a_drsize = execp->a_data;
445 else
447 execp->a_trsize = 0;
448 execp->a_drsize = 0;
451 NAME(aout,swap_exec_header_out) (abfd, execp, &exec_bytes);
453 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
454 return false;
456 if (bfd_bwrite ((PTR) &exec_bytes, (bfd_size_type) EXEC_BYTES_SIZE, abfd)
457 != EXEC_BYTES_SIZE)
458 return false;
460 /* Now write out reloc info, followed by syms and strings */
462 if (bfd_get_outsymbols (abfd) != (asymbol **) NULL
463 && bfd_get_symcount (abfd) != 0)
465 if (bfd_seek (abfd, (file_ptr) (N_SYMOFF(*execp)), SEEK_SET) != 0)
466 return false;
468 if (! NAME(aout,write_syms) (abfd))
469 return false;
472 if (obj_textsec (abfd)->reloc_count > 0 ||
473 obj_datasec (abfd)->reloc_count > 0)
475 if (bfd_seek (abfd, (file_ptr) (N_TRELOFF(*execp)), SEEK_SET) != 0)
476 return false;
477 if (!NAME(aout,squirt_out_relocs) (abfd, obj_textsec (abfd)))
478 return false;
480 if (bfd_seek (abfd, (file_ptr) (N_DRELOFF(*execp)), SEEK_SET) != 0)
481 return false;
482 if (!NAME(aout,squirt_out_relocs) (abfd, obj_datasec (abfd)))
483 return false;
486 return true;
489 /* Write an object file.
490 Section contents have already been written. We write the
491 file header, symbols, and relocation. */
493 static boolean
494 MY(write_object_contents) (abfd)
495 bfd *abfd;
497 struct internal_exec *execp = exec_hdr (abfd);
499 /* We must make certain that the magic number has been set. This
500 will normally have been done by set_section_contents, but only if
501 there actually are some section contents. */
502 if (! abfd->output_has_begun)
504 bfd_size_type text_size;
505 file_ptr text_end;
507 NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end);
510 obj_reloc_entry_size (abfd) = RELOC_SIZE;
512 return WRITE_HEADERS(abfd, execp);
516 SUBSECTION
517 Internal entry points
519 DESCRIPTION
520 @file{aoutx.h} exports several routines for accessing the
521 contents of an a.out file, which are gathered and exported in
522 turn by various format specific files (eg sunos.c).
527 FUNCTION
528 aout_@var{size}_swap_exec_header_in
530 SYNOPSIS
531 void aout_@var{size}_swap_exec_header_in,
532 (bfd *abfd,
533 struct external_exec *raw_bytes,
534 struct internal_exec *execp);
536 DESCRIPTION
537 Swap the information in an executable header @var{raw_bytes} taken
538 from a raw byte stream memory image into the internal exec header
539 structure @var{execp}.
542 #ifndef NAME_swap_exec_header_in
543 void
544 NAME(aout,swap_exec_header_in) (abfd, raw_bytes, execp)
545 bfd *abfd;
546 struct external_exec *raw_bytes;
547 struct internal_exec *execp;
549 struct external_exec *bytes = (struct external_exec *)raw_bytes;
551 /* The internal_exec structure has some fields that are unused in this
552 configuration (IE for i960), so ensure that all such uninitialized
553 fields are zero'd out. There are places where two of these structs
554 are memcmp'd, and thus the contents do matter. */
555 memset ((PTR) execp, 0, sizeof (struct internal_exec));
556 /* Now fill in fields in the execp, from the bytes in the raw data. */
557 execp->a_info = GET_MAGIC (abfd, bytes->e_info);
558 execp->a_text = GET_WORD (abfd, bytes->e_text);
559 execp->a_data = GET_WORD (abfd, bytes->e_data);
560 execp->a_bss = GET_WORD (abfd, bytes->e_bss);
561 execp->a_syms = GET_WORD (abfd, bytes->e_syms);
562 execp->a_entry = GET_WORD (abfd, bytes->e_entry);
564 if (GET_WORD (abfd, bytes->e_flag) & A_FLAG_RELOC_STRIPPED)
566 execp->a_trsize = 0;
567 execp->a_drsize = 0;
569 else
571 execp->a_trsize = execp->a_text;
572 execp->a_drsize = execp->a_data;
575 #define NAME_swap_exec_header_in NAME(aout,swap_exec_header_in)
576 #endif
579 FUNCTION
580 aout_@var{size}_swap_exec_header_out
582 SYNOPSIS
583 void aout_@var{size}_swap_exec_header_out
584 (bfd *abfd,
585 struct internal_exec *execp,
586 struct external_exec *raw_bytes);
588 DESCRIPTION
589 Swap the information in an internal exec header structure
590 @var{execp} into the buffer @var{raw_bytes} ready for writing to disk.
592 void
593 NAME(aout,swap_exec_header_out) (abfd, execp, raw_bytes)
594 bfd *abfd;
595 struct internal_exec *execp;
596 struct external_exec *raw_bytes;
598 struct external_exec *bytes = (struct external_exec *)raw_bytes;
600 /* Now fill in fields in the raw data, from the fields in the exec struct. */
601 PUT_MAGIC (abfd, execp->a_info, bytes->e_info);
602 PUT_WORD (abfd, execp->a_text, bytes->e_text);
603 PUT_WORD (abfd, execp->a_data, bytes->e_data);
604 PUT_WORD (abfd, execp->a_bss, bytes->e_bss);
605 PUT_WORD (abfd, execp->a_syms, bytes->e_syms);
606 PUT_WORD (abfd, execp->a_entry, bytes->e_entry);
607 PUT_WORD (abfd, 0, bytes->e_unused);
609 if ((execp->a_trsize == 0 || execp->a_text == 0) &&
610 (execp->a_drsize == 0 || execp->a_data == 0))
611 PUT_WORD (abfd, A_FLAG_RELOC_STRIPPED, bytes->e_flag);
612 else if (execp->a_trsize == execp->a_text &&
613 execp->a_drsize == execp->a_data)
614 PUT_WORD (abfd, 0, bytes->e_flag);
615 else
617 /* TODO: print a proper warning message */
618 fprintf (stderr, "BFD:%s:%d: internal error\n", __FILE__, __LINE__);
619 PUT_WORD (abfd, 0, bytes->e_flag);
623 /* Make all the section for an a.out file. */
625 boolean
626 NAME(aout,make_sections) (abfd)
627 bfd *abfd;
629 if (obj_textsec (abfd) == (asection *) NULL
630 && bfd_make_section (abfd, ".text") == (asection *) NULL)
631 return false;
632 if (obj_datasec (abfd) == (asection *) NULL
633 && bfd_make_section (abfd, ".data") == (asection *) NULL)
634 return false;
635 if (obj_bsssec (abfd) == (asection *) NULL
636 && bfd_make_section (abfd, ".bss") == (asection *) NULL)
637 return false;
638 return true;
642 FUNCTION
643 aout_@var{size}_some_aout_object_p
645 SYNOPSIS
646 const bfd_target *aout_@var{size}_some_aout_object_p
647 (bfd *abfd,
648 const bfd_target *(*callback_to_real_object_p)());
650 DESCRIPTION
651 Some a.out variant thinks that the file open in @var{abfd}
652 checking is an a.out file. Do some more checking, and set up
653 for access if it really is. Call back to the calling
654 environment's "finish up" function just before returning, to
655 handle any last-minute setup.
658 const bfd_target *
659 NAME(aout,some_aout_object_p) (abfd, execp, callback_to_real_object_p)
660 bfd *abfd;
661 struct internal_exec *execp;
662 const bfd_target *(*callback_to_real_object_p) PARAMS ((bfd *));
664 struct aout_data_struct *rawptr, *oldrawptr;
665 const bfd_target *result;
666 bfd_size_type amt = sizeof (struct aout_data_struct);
668 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
669 if (rawptr == NULL)
670 return 0;
672 oldrawptr = abfd->tdata.aout_data;
673 abfd->tdata.aout_data = rawptr;
675 /* Copy the contents of the old tdata struct.
676 In particular, we want the subformat, since for hpux it was set in
677 hp300hpux.c:swap_exec_header_in and will be used in
678 hp300hpux.c:callback. */
679 if (oldrawptr != NULL)
680 *abfd->tdata.aout_data = *oldrawptr;
682 abfd->tdata.aout_data->a.hdr = &rawptr->e;
683 *(abfd->tdata.aout_data->a.hdr) = *execp; /* Copy in the internal_exec struct */
684 execp = abfd->tdata.aout_data->a.hdr;
686 /* Set the file flags */
687 abfd->flags = BFD_NO_FLAGS;
688 if (execp->a_drsize || execp->a_trsize)
689 abfd->flags |= HAS_RELOC;
690 /* Setting of EXEC_P has been deferred to the bottom of this function */
691 if (execp->a_syms)
692 abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
693 if (N_DYNAMIC(*execp))
694 abfd->flags |= DYNAMIC;
696 if (N_MAGIC (*execp) == ZMAGIC)
698 abfd->flags |= D_PAGED | WP_TEXT;
699 adata (abfd).magic = z_magic;
701 else if (N_MAGIC (*execp) == QMAGIC)
703 abfd->flags |= D_PAGED | WP_TEXT;
704 adata (abfd).magic = z_magic;
705 adata (abfd).subformat = q_magic_format;
707 else if (N_MAGIC (*execp) == NMAGIC)
709 abfd->flags |= WP_TEXT;
710 adata (abfd).magic = n_magic;
712 else if (N_MAGIC (*execp) == OMAGIC
713 || N_MAGIC (*execp) == BMAGIC)
714 adata (abfd).magic = o_magic;
715 else
717 /* Should have been checked with N_BADMAG before this routine
718 was called. */
719 abort ();
722 bfd_get_start_address (abfd) = execp->a_entry;
724 obj_aout_symbols (abfd) = (aout_symbol_type *)NULL;
725 bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
727 /* The default relocation entry size is that of traditional V7 Unix. */
728 obj_reloc_entry_size (abfd) = RELOC_SIZE;
730 /* The default symbol entry size is that of traditional Unix. */
731 obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
733 #ifdef USE_MMAP
734 bfd_init_window (&obj_aout_sym_window (abfd));
735 bfd_init_window (&obj_aout_string_window (abfd));
736 #endif
737 obj_aout_external_syms (abfd) = NULL;
738 obj_aout_external_strings (abfd) = NULL;
739 obj_aout_sym_hashes (abfd) = NULL;
741 if (! NAME(aout,make_sections) (abfd))
742 return NULL;
744 obj_datasec (abfd)->_raw_size = execp->a_data;
745 obj_bsssec (abfd)->_raw_size = execp->a_bss;
747 obj_textsec (abfd)->flags =
748 (execp->a_trsize != 0
749 ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
750 : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
751 obj_datasec (abfd)->flags =
752 (execp->a_drsize != 0
753 ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
754 : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
755 obj_bsssec (abfd)->flags = SEC_ALLOC;
757 #ifdef THIS_IS_ONLY_DOCUMENTATION
758 /* The common code can't fill in these things because they depend
759 on either the start address of the text segment, the rounding
760 up of virtual addresses between segments, or the starting file
761 position of the text segment -- all of which varies among different
762 versions of a.out. */
764 /* Call back to the format-dependent code to fill in the rest of the
765 fields and do any further cleanup. Things that should be filled
766 in by the callback: */
768 struct exec *execp = exec_hdr (abfd);
770 obj_textsec (abfd)->size = N_TXTSIZE(*execp);
771 obj_textsec (abfd)->raw_size = N_TXTSIZE(*execp);
772 /* data and bss are already filled in since they're so standard */
774 /* The virtual memory addresses of the sections */
775 obj_textsec (abfd)->vma = N_TXTADDR(*execp);
776 obj_datasec (abfd)->vma = N_DATADDR(*execp);
777 obj_bsssec (abfd)->vma = N_BSSADDR(*execp);
779 /* The file offsets of the sections */
780 obj_textsec (abfd)->filepos = N_TXTOFF(*execp);
781 obj_datasec (abfd)->filepos = N_DATOFF(*execp);
783 /* The file offsets of the relocation info */
784 obj_textsec (abfd)->rel_filepos = N_TRELOFF(*execp);
785 obj_datasec (abfd)->rel_filepos = N_DRELOFF(*execp);
787 /* The file offsets of the string table and symbol table. */
788 obj_str_filepos (abfd) = N_STROFF (*execp);
789 obj_sym_filepos (abfd) = N_SYMOFF (*execp);
791 /* Determine the architecture and machine type of the object file. */
792 abfd->obj_arch = bfd_arch_obscure;
794 adata(abfd)->page_size = TARGET_PAGE_SIZE;
795 adata(abfd)->segment_size = SEGMENT_SIZE;
796 adata(abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
798 return abfd->xvec;
800 /* The architecture is encoded in various ways in various a.out variants,
801 or is not encoded at all in some of them. The relocation size depends
802 on the architecture and the a.out variant. Finally, the return value
803 is the bfd_target vector in use. If an error occurs, return zero and
804 set bfd_error to the appropriate error code.
806 Formats such as b.out, which have additional fields in the a.out
807 header, should cope with them in this callback as well. */
808 #endif /* DOCUMENTATION */
810 result = (*callback_to_real_object_p)(abfd);
812 /* Now that the segment addresses have been worked out, take a better
813 guess at whether the file is executable. If the entry point
814 is within the text segment, assume it is. (This makes files
815 executable even if their entry point address is 0, as long as
816 their text starts at zero.).
818 This test had to be changed to deal with systems where the text segment
819 runs at a different location than the default. The problem is that the
820 entry address can appear to be outside the text segment, thus causing an
821 erroneous conclusion that the file isn't executable.
823 To fix this, we now accept any non-zero entry point as an indication of
824 executability. This will work most of the time, since only the linker
825 sets the entry point, and that is likely to be non-zero for most systems. */
827 if (execp->a_entry != 0
828 || (execp->a_entry >= obj_textsec(abfd)->vma
829 && execp->a_entry < obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size))
830 abfd->flags |= EXEC_P;
831 #ifdef STAT_FOR_EXEC
832 else
834 struct stat stat_buf;
836 /* The original heuristic doesn't work in some important cases.
837 The a.out file has no information about the text start
838 address. For files (like kernels) linked to non-standard
839 addresses (ld -Ttext nnn) the entry point may not be between
840 the default text start (obj_textsec(abfd)->vma) and
841 (obj_textsec(abfd)->vma) + text size. This is not just a mach
842 issue. Many kernels are loaded at non standard addresses. */
843 if (abfd->iostream != NULL
844 && (abfd->flags & BFD_IN_MEMORY) == 0
845 && (fstat(fileno((FILE *) (abfd->iostream)), &stat_buf) == 0)
846 && ((stat_buf.st_mode & 0111) != 0))
847 abfd->flags |= EXEC_P;
849 #endif /* STAT_FOR_EXEC */
851 if (result)
853 #if 0 /* These should be set correctly anyways. */
854 abfd->sections = obj_textsec (abfd);
855 obj_textsec (abfd)->next = obj_datasec (abfd);
856 obj_datasec (abfd)->next = obj_bsssec (abfd);
857 #endif
859 else
861 free (rawptr);
862 abfd->tdata.aout_data = oldrawptr;
864 return result;
868 FUNCTION
869 aout_@var{size}_mkobject
871 SYNOPSIS
872 boolean aout_@var{size}_mkobject, (bfd *abfd);
874 DESCRIPTION
875 Initialize BFD @var{abfd} for use with a.out files.
878 boolean
879 NAME(aout,mkobject) (abfd)
880 bfd *abfd;
882 struct aout_data_struct *rawptr;
883 bfd_size_type amt = sizeof (struct aout_data_struct);
885 bfd_set_error (bfd_error_system_call);
887 /* Use an intermediate variable for clarity */
888 rawptr = (struct aout_data_struct *) bfd_zalloc (abfd, amt);
890 if (rawptr == NULL)
891 return false;
893 abfd->tdata.aout_data = rawptr;
894 exec_hdr (abfd) = &(rawptr->e);
896 obj_textsec (abfd) = (asection *)NULL;
897 obj_datasec (abfd) = (asection *)NULL;
898 obj_bsssec (abfd) = (asection *)NULL;
900 return true;
905 FUNCTION
906 aout_@var{size}_machine_type
908 SYNOPSIS
909 enum machine_type aout_@var{size}_machine_type
910 (enum bfd_architecture arch,
911 unsigned long machine));
913 DESCRIPTION
914 Keep track of machine architecture and machine type for
915 a.out's. Return the <<machine_type>> for a particular
916 architecture and machine, or <<M_UNKNOWN>> if that exact architecture
917 and machine can't be represented in a.out format.
919 If the architecture is understood, machine type 0 (default)
920 is always understood.
923 enum machine_type
924 NAME(aout,machine_type) (arch, machine, unknown)
925 enum bfd_architecture arch;
926 unsigned long machine;
927 boolean *unknown;
929 enum machine_type arch_flags;
931 arch_flags = M_UNKNOWN;
932 *unknown = true;
934 switch (arch)
936 case bfd_arch_sparc:
937 if (machine == 0
938 || machine == bfd_mach_sparc
939 || machine == bfd_mach_sparc_sparclite
940 || machine == bfd_mach_sparc_v9)
941 arch_flags = M_SPARC;
942 else if (machine == bfd_mach_sparc_sparclet)
943 arch_flags = M_SPARCLET;
944 break;
946 case bfd_arch_m68k:
947 switch (machine)
949 case 0: arch_flags = M_68010; break;
950 case bfd_mach_m68000: arch_flags = M_UNKNOWN; *unknown = false; break;
951 case bfd_mach_m68010: arch_flags = M_68010; break;
952 case bfd_mach_m68020: arch_flags = M_68020; break;
953 default: arch_flags = M_UNKNOWN; break;
955 break;
957 case bfd_arch_i386:
958 if (machine == 0
959 || machine == bfd_mach_i386_i386
960 || machine == bfd_mach_i386_i386_intel_syntax)
961 arch_flags = M_386;
962 break;
964 case bfd_arch_a29k:
965 if (machine == 0) arch_flags = M_29K;
966 break;
968 case bfd_arch_arm:
969 if (machine == 0) arch_flags = M_ARM;
970 break;
972 case bfd_arch_mips:
973 switch (machine)
975 case 0:
976 case 2000:
977 case bfd_mach_mips3000:
978 arch_flags = M_MIPS1;
979 break;
980 case bfd_mach_mips4000: /* mips3 */
981 case bfd_mach_mips4400:
982 case bfd_mach_mips8000: /* mips4 */
983 case bfd_mach_mips6000: /* real mips2: */
984 arch_flags = M_MIPS2;
985 break;
986 default:
987 arch_flags = M_UNKNOWN;
988 break;
990 break;
992 case bfd_arch_ns32k:
993 switch (machine)
995 case 0: arch_flags = M_NS32532; break;
996 case 32032: arch_flags = M_NS32032; break;
997 case 32532: arch_flags = M_NS32532; break;
998 default: arch_flags = M_UNKNOWN; break;
1000 break;
1002 case bfd_arch_pdp11:
1003 /* TODO: arch_flags = M_PDP11; */
1004 *unknown = false;
1005 break;
1007 case bfd_arch_vax:
1008 *unknown = false;
1009 break;
1011 default:
1012 arch_flags = M_UNKNOWN;
1015 if (arch_flags != M_UNKNOWN)
1016 *unknown = false;
1018 return arch_flags;
1023 FUNCTION
1024 aout_@var{size}_set_arch_mach
1026 SYNOPSIS
1027 boolean aout_@var{size}_set_arch_mach,
1028 (bfd *,
1029 enum bfd_architecture arch,
1030 unsigned long machine));
1032 DESCRIPTION
1033 Set the architecture and the machine of the BFD @var{abfd} to the
1034 values @var{arch} and @var{machine}. Verify that @var{abfd}'s format
1035 can support the architecture required.
1038 boolean
1039 NAME(aout,set_arch_mach) (abfd, arch, machine)
1040 bfd *abfd;
1041 enum bfd_architecture arch;
1042 unsigned long machine;
1044 if (! bfd_default_set_arch_mach (abfd, arch, machine))
1045 return false;
1047 if (arch != bfd_arch_unknown)
1049 boolean unknown;
1051 NAME(aout,machine_type) (arch, machine, &unknown);
1052 if (unknown)
1053 return false;
1056 obj_reloc_entry_size (abfd) = RELOC_SIZE;
1058 return (*aout_backend_info(abfd)->set_sizes) (abfd);
1061 static void
1062 adjust_o_magic (abfd, execp)
1063 bfd *abfd;
1064 struct internal_exec *execp;
1066 file_ptr pos = adata (abfd).exec_bytes_size;
1067 bfd_vma vma = 0;
1068 int pad = 0;
1070 /* Text. */
1071 obj_textsec (abfd)->filepos = pos;
1072 if (! obj_textsec (abfd)->user_set_vma)
1073 obj_textsec (abfd)->vma = vma;
1074 else
1075 vma = obj_textsec (abfd)->vma;
1077 pos += obj_textsec (abfd)->_raw_size;
1078 vma += obj_textsec (abfd)->_raw_size;
1080 /* Data. */
1081 if (!obj_datasec (abfd)->user_set_vma)
1083 #if 0 /* ?? Does alignment in the file image really matter? */
1084 pad = align_power (vma, obj_datasec (abfd)->alignment_power) - vma;
1085 #endif
1086 obj_textsec (abfd)->_raw_size += pad;
1087 pos += pad;
1088 vma += pad;
1089 obj_datasec (abfd)->vma = vma;
1091 else
1092 vma = obj_datasec (abfd)->vma;
1093 obj_datasec (abfd)->filepos = pos;
1094 pos += obj_datasec (abfd)->_raw_size;
1095 vma += obj_datasec (abfd)->_raw_size;
1097 /* BSS. */
1098 if (! obj_bsssec (abfd)->user_set_vma)
1100 #if 0
1101 pad = align_power (vma, obj_bsssec (abfd)->alignment_power) - vma;
1102 #endif
1103 obj_datasec (abfd)->_raw_size += pad;
1104 pos += pad;
1105 vma += pad;
1106 obj_bsssec (abfd)->vma = vma;
1108 else
1110 /* The VMA of the .bss section is set by the VMA of the
1111 .data section plus the size of the .data section. We may
1112 need to add padding bytes to make this true. */
1113 pad = obj_bsssec (abfd)->vma - vma;
1114 if (pad > 0)
1116 obj_datasec (abfd)->_raw_size += pad;
1117 pos += pad;
1120 obj_bsssec (abfd)->filepos = pos;
1122 /* Fix up the exec header. */
1123 execp->a_text = obj_textsec (abfd)->_raw_size;
1124 execp->a_data = obj_datasec (abfd)->_raw_size;
1125 execp->a_bss = obj_bsssec (abfd)->_raw_size;
1126 N_SET_MAGIC (*execp, OMAGIC);
1129 static void
1130 adjust_z_magic (abfd, execp)
1131 bfd *abfd;
1132 struct internal_exec *execp;
1134 bfd_size_type data_pad, text_pad;
1135 file_ptr text_end;
1136 const struct aout_backend_data *abdp;
1137 int ztih; /* Nonzero if text includes exec header. */
1139 abdp = aout_backend_info (abfd);
1141 /* Text. */
1142 ztih = (abdp != NULL
1143 && (abdp->text_includes_header
1144 || obj_aout_subformat (abfd) == q_magic_format));
1145 obj_textsec(abfd)->filepos = (ztih
1146 ? adata(abfd).exec_bytes_size
1147 : adata(abfd).zmagic_disk_block_size);
1148 if (! obj_textsec(abfd)->user_set_vma)
1150 /* ?? Do we really need to check for relocs here? */
1151 obj_textsec(abfd)->vma = ((abfd->flags & HAS_RELOC)
1153 : (ztih
1154 ? (abdp->default_text_vma
1155 + adata (abfd).exec_bytes_size)
1156 : abdp->default_text_vma));
1157 text_pad = 0;
1159 else
1161 /* The .text section is being loaded at an unusual address. We
1162 may need to pad it such that the .data section starts at a page
1163 boundary. */
1164 if (ztih)
1165 text_pad = ((obj_textsec (abfd)->filepos - obj_textsec (abfd)->vma)
1166 & (adata (abfd).page_size - 1));
1167 else
1168 text_pad = ((- obj_textsec (abfd)->vma)
1169 & (adata (abfd).page_size - 1));
1172 /* Find start of data. */
1173 if (ztih)
1175 text_end = obj_textsec (abfd)->filepos + obj_textsec (abfd)->_raw_size;
1176 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
1178 else
1180 /* Note that if page_size == zmagic_disk_block_size, then
1181 filepos == page_size, and this case is the same as the ztih
1182 case. */
1183 text_end = obj_textsec (abfd)->_raw_size;
1184 text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
1185 text_end += obj_textsec (abfd)->filepos;
1188 obj_textsec (abfd)->_raw_size += text_pad;
1189 text_end += text_pad;
1191 /* Data. */
1192 if (!obj_datasec(abfd)->user_set_vma)
1194 bfd_vma vma;
1195 vma = obj_textsec(abfd)->vma + obj_textsec(abfd)->_raw_size;
1196 obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
1198 if (abdp && abdp->zmagic_mapped_contiguous)
1200 text_pad = (obj_datasec(abfd)->vma
1201 - obj_textsec(abfd)->vma
1202 - obj_textsec(abfd)->_raw_size);
1203 obj_textsec(abfd)->_raw_size += text_pad;
1205 obj_datasec (abfd)->filepos = (obj_textsec (abfd)->filepos
1206 + obj_textsec (abfd)->_raw_size);
1208 /* Fix up exec header while we're at it. */
1209 execp->a_text = obj_textsec(abfd)->_raw_size;
1210 if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
1211 execp->a_text += adata(abfd).exec_bytes_size;
1212 if (obj_aout_subformat (abfd) == q_magic_format)
1213 N_SET_MAGIC (*execp, QMAGIC);
1214 else
1215 N_SET_MAGIC (*execp, ZMAGIC);
1217 /* Spec says data section should be rounded up to page boundary. */
1218 obj_datasec(abfd)->_raw_size
1219 = align_power (obj_datasec(abfd)->_raw_size,
1220 obj_bsssec(abfd)->alignment_power);
1221 execp->a_data = BFD_ALIGN (obj_datasec(abfd)->_raw_size,
1222 adata(abfd).page_size);
1223 data_pad = execp->a_data - obj_datasec(abfd)->_raw_size;
1225 /* BSS. */
1226 if (!obj_bsssec(abfd)->user_set_vma)
1227 obj_bsssec(abfd)->vma = (obj_datasec(abfd)->vma
1228 + obj_datasec(abfd)->_raw_size);
1229 /* If the BSS immediately follows the data section and extra space
1230 in the page is left after the data section, fudge data
1231 in the header so that the bss section looks smaller by that
1232 amount. We'll start the bss section there, and lie to the OS.
1233 (Note that a linker script, as well as the above assignment,
1234 could have explicitly set the BSS vma to immediately follow
1235 the data section.) */
1236 if (align_power (obj_bsssec(abfd)->vma, obj_bsssec(abfd)->alignment_power)
1237 == obj_datasec(abfd)->vma + obj_datasec(abfd)->_raw_size)
1238 execp->a_bss = (data_pad > obj_bsssec(abfd)->_raw_size) ? 0 :
1239 obj_bsssec(abfd)->_raw_size - data_pad;
1240 else
1241 execp->a_bss = obj_bsssec(abfd)->_raw_size;
1244 static void
1245 adjust_n_magic (abfd, execp)
1246 bfd *abfd;
1247 struct internal_exec *execp;
1249 file_ptr pos = adata(abfd).exec_bytes_size;
1250 bfd_vma vma = 0;
1251 int pad;
1253 /* Text. */
1254 obj_textsec(abfd)->filepos = pos;
1255 if (!obj_textsec(abfd)->user_set_vma)
1256 obj_textsec(abfd)->vma = vma;
1257 else
1258 vma = obj_textsec(abfd)->vma;
1259 pos += obj_textsec(abfd)->_raw_size;
1260 vma += obj_textsec(abfd)->_raw_size;
1262 /* Data. */
1263 obj_datasec(abfd)->filepos = pos;
1264 if (!obj_datasec(abfd)->user_set_vma)
1265 obj_datasec(abfd)->vma = BFD_ALIGN (vma, adata(abfd).segment_size);
1266 vma = obj_datasec(abfd)->vma;
1268 /* Since BSS follows data immediately, see if it needs alignment. */
1269 vma += obj_datasec(abfd)->_raw_size;
1270 pad = align_power (vma, obj_bsssec(abfd)->alignment_power) - vma;
1271 obj_datasec(abfd)->_raw_size += pad;
1272 pos += obj_datasec(abfd)->_raw_size;
1274 /* BSS. */
1275 if (!obj_bsssec(abfd)->user_set_vma)
1276 obj_bsssec(abfd)->vma = vma;
1277 else
1278 vma = obj_bsssec(abfd)->vma;
1280 /* Fix up exec header. */
1281 execp->a_text = obj_textsec(abfd)->_raw_size;
1282 execp->a_data = obj_datasec(abfd)->_raw_size;
1283 execp->a_bss = obj_bsssec(abfd)->_raw_size;
1284 N_SET_MAGIC (*execp, NMAGIC);
1287 boolean
1288 NAME(aout,adjust_sizes_and_vmas) (abfd, text_size, text_end)
1289 bfd *abfd;
1290 bfd_size_type *text_size;
1291 file_ptr * text_end ATTRIBUTE_UNUSED;
1293 struct internal_exec *execp = exec_hdr (abfd);
1295 if (! NAME(aout,make_sections) (abfd))
1296 return false;
1298 if (adata(abfd).magic != undecided_magic)
1299 return true;
1301 obj_textsec(abfd)->_raw_size =
1302 align_power(obj_textsec(abfd)->_raw_size,
1303 obj_textsec(abfd)->alignment_power);
1305 *text_size = obj_textsec (abfd)->_raw_size;
1306 /* Rule (heuristic) for when to pad to a new page. Note that there
1307 are (at least) two ways demand-paged (ZMAGIC) files have been
1308 handled. Most Berkeley-based systems start the text segment at
1309 (TARGET_PAGE_SIZE). However, newer versions of SUNOS start the text
1310 segment right after the exec header; the latter is counted in the
1311 text segment size, and is paged in by the kernel with the rest of
1312 the text. */
1314 /* This perhaps isn't the right way to do this, but made it simpler for me
1315 to understand enough to implement it. Better would probably be to go
1316 right from BFD flags to alignment/positioning characteristics. But the
1317 old code was sloppy enough about handling the flags, and had enough
1318 other magic, that it was a little hard for me to understand. I think
1319 I understand it better now, but I haven't time to do the cleanup this
1320 minute. */
1322 if (abfd->flags & WP_TEXT)
1323 adata(abfd).magic = n_magic;
1324 else
1325 adata(abfd).magic = o_magic;
1327 #ifdef BFD_AOUT_DEBUG /* requires gcc2 */
1328 #if __GNUC__ >= 2
1329 fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
1330 ({ char *str;
1331 switch (adata(abfd).magic) {
1332 case n_magic: str = "NMAGIC"; break;
1333 case o_magic: str = "OMAGIC"; break;
1334 case z_magic: str = "ZMAGIC"; break;
1335 default: abort ();
1337 str;
1339 obj_textsec(abfd)->vma, obj_textsec(abfd)->_raw_size,
1340 obj_textsec(abfd)->alignment_power,
1341 obj_datasec(abfd)->vma, obj_datasec(abfd)->_raw_size,
1342 obj_datasec(abfd)->alignment_power,
1343 obj_bsssec(abfd)->vma, obj_bsssec(abfd)->_raw_size,
1344 obj_bsssec(abfd)->alignment_power);
1345 #endif
1346 #endif
1348 switch (adata(abfd).magic)
1350 case o_magic:
1351 adjust_o_magic (abfd, execp);
1352 break;
1353 case z_magic:
1354 adjust_z_magic (abfd, execp);
1355 break;
1356 case n_magic:
1357 adjust_n_magic (abfd, execp);
1358 break;
1359 default:
1360 abort ();
1363 #ifdef BFD_AOUT_DEBUG
1364 fprintf (stderr, " text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
1365 obj_textsec(abfd)->vma, obj_textsec(abfd)->_raw_size,
1366 obj_textsec(abfd)->filepos,
1367 obj_datasec(abfd)->vma, obj_datasec(abfd)->_raw_size,
1368 obj_datasec(abfd)->filepos,
1369 obj_bsssec(abfd)->vma, obj_bsssec(abfd)->_raw_size);
1370 #endif
1372 return true;
1376 FUNCTION
1377 aout_@var{size}_new_section_hook
1379 SYNOPSIS
1380 boolean aout_@var{size}_new_section_hook,
1381 (bfd *abfd,
1382 asection *newsect));
1384 DESCRIPTION
1385 Called by the BFD in response to a @code{bfd_make_section}
1386 request.
1388 boolean
1389 NAME(aout,new_section_hook) (abfd, newsect)
1390 bfd *abfd;
1391 asection *newsect;
1393 /* align to double at least */
1394 newsect->alignment_power = bfd_get_arch_info(abfd)->section_align_power;
1397 if (bfd_get_format (abfd) == bfd_object)
1399 if (obj_textsec (abfd) == NULL
1400 && ! strcmp (newsect->name, ".text"))
1402 obj_textsec(abfd)= newsect;
1403 newsect->target_index = N_TEXT;
1404 return true;
1407 if (obj_datasec (abfd) == NULL
1408 && ! strcmp (newsect->name, ".data"))
1410 obj_datasec (abfd) = newsect;
1411 newsect->target_index = N_DATA;
1412 return true;
1415 if (obj_bsssec (abfd) == NULL
1416 && !strcmp (newsect->name, ".bss"))
1418 obj_bsssec (abfd) = newsect;
1419 newsect->target_index = N_BSS;
1420 return true;
1424 /* We allow more than three sections internally */
1425 return true;
1428 boolean
1429 NAME(aout,set_section_contents) (abfd, section, location, offset, count)
1430 bfd *abfd;
1431 sec_ptr section;
1432 PTR location;
1433 file_ptr offset;
1434 bfd_size_type count;
1436 file_ptr text_end;
1437 bfd_size_type text_size;
1439 if (! abfd->output_has_begun)
1441 if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
1442 return false;
1445 if (section == obj_bsssec (abfd))
1447 bfd_set_error (bfd_error_no_contents);
1448 return false;
1451 if (section != obj_textsec (abfd)
1452 && section != obj_datasec (abfd))
1454 (*_bfd_error_handler)
1455 ("%s: can not represent section `%s' in a.out object file format",
1456 bfd_get_filename (abfd), bfd_get_section_name (abfd, section));
1457 bfd_set_error (bfd_error_nonrepresentable_section);
1458 return false;
1461 if (count != 0)
1463 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
1464 || bfd_bwrite (location, count, abfd) != count)
1465 return false;
1468 return true;
1471 /* Read the external symbols from an a.out file. */
1473 static boolean
1474 aout_get_external_symbols (abfd)
1475 bfd *abfd;
1477 if (obj_aout_external_syms (abfd) == (struct external_nlist *) NULL)
1479 bfd_size_type count;
1480 struct external_nlist *syms;
1482 count = exec_hdr (abfd)->a_syms / EXTERNAL_NLIST_SIZE;
1484 #ifdef USE_MMAP
1485 if (! bfd_get_file_window (abfd, obj_sym_filepos (abfd),
1486 exec_hdr (abfd)->a_syms,
1487 &obj_aout_sym_window (abfd), true))
1488 return false;
1489 syms = (struct external_nlist *) obj_aout_sym_window (abfd).data;
1490 #else
1491 /* We allocate using malloc to make the values easy to free
1492 later on. If we put them on the objalloc it might not be
1493 possible to free them. */
1494 syms = (struct external_nlist *) bfd_malloc (count * EXTERNAL_NLIST_SIZE);
1495 if (syms == (struct external_nlist *) NULL && count != 0)
1496 return false;
1498 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1499 || (bfd_bread (syms, exec_hdr (abfd)->a_syms, abfd)
1500 != exec_hdr (abfd)->a_syms))
1502 free (syms);
1503 return false;
1505 #endif
1507 obj_aout_external_syms (abfd) = syms;
1508 obj_aout_external_sym_count (abfd) = count;
1511 if (obj_aout_external_strings (abfd) == NULL
1512 && exec_hdr (abfd)->a_syms != 0)
1514 unsigned char string_chars[BYTES_IN_LONG];
1515 bfd_size_type stringsize;
1516 char *strings;
1518 /* Get the size of the strings. */
1519 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
1520 || (bfd_bread ((PTR) string_chars, (bfd_size_type) BYTES_IN_LONG,
1521 abfd) != BYTES_IN_LONG))
1522 return false;
1523 stringsize = H_GET_32 (abfd, string_chars);
1525 #ifdef USE_MMAP
1526 if (! bfd_get_file_window (abfd, obj_str_filepos (abfd), stringsize,
1527 &obj_aout_string_window (abfd), true))
1528 return false;
1529 strings = (char *) obj_aout_string_window (abfd).data;
1530 #else
1531 strings = (char *) bfd_malloc (stringsize + 1);
1532 if (strings == NULL)
1533 return false;
1535 /* Skip space for the string count in the buffer for convenience
1536 when using indexes. */
1537 if (bfd_bread (strings + 4, stringsize - 4, abfd) != stringsize - 4)
1539 free (strings);
1540 return false;
1542 #endif
1544 /* Ensure that a zero index yields an empty string. */
1545 strings[0] = '\0';
1547 strings[stringsize - 1] = 0;
1549 obj_aout_external_strings (abfd) = strings;
1550 obj_aout_external_string_size (abfd) = stringsize;
1553 return true;
1556 /* Translate an a.out symbol into a BFD symbol. The desc, other, type
1557 and symbol->value fields of CACHE_PTR will be set from the a.out
1558 nlist structure. This function is responsible for setting
1559 symbol->flags and symbol->section, and adjusting symbol->value. */
1561 static boolean
1562 translate_from_native_sym_flags (abfd, cache_ptr)
1563 bfd *abfd;
1564 aout_symbol_type *cache_ptr;
1566 flagword visible;
1568 if (cache_ptr->type == N_FN)
1570 asection *sec;
1572 /* This is a debugging symbol. */
1574 cache_ptr->symbol.flags = BSF_DEBUGGING;
1576 /* Work out the symbol section. */
1577 switch (cache_ptr->type & N_TYPE)
1579 case N_TEXT:
1580 case N_FN:
1581 sec = obj_textsec (abfd);
1582 break;
1583 case N_DATA:
1584 sec = obj_datasec (abfd);
1585 break;
1586 case N_BSS:
1587 sec = obj_bsssec (abfd);
1588 break;
1589 default:
1590 case N_ABS:
1591 sec = bfd_abs_section_ptr;
1592 break;
1595 cache_ptr->symbol.section = sec;
1596 cache_ptr->symbol.value -= sec->vma;
1598 return true;
1601 /* Get the default visibility. This does not apply to all types, so
1602 we just hold it in a local variable to use if wanted. */
1603 if ((cache_ptr->type & N_EXT) == 0)
1604 visible = BSF_LOCAL;
1605 else
1606 visible = BSF_GLOBAL;
1608 switch (cache_ptr->type)
1610 default:
1611 case N_ABS: case N_ABS | N_EXT:
1612 cache_ptr->symbol.section = bfd_abs_section_ptr;
1613 cache_ptr->symbol.flags = visible;
1614 break;
1616 case N_UNDF | N_EXT:
1617 if (cache_ptr->symbol.value != 0)
1619 /* This is a common symbol. */
1620 cache_ptr->symbol.flags = BSF_GLOBAL;
1621 cache_ptr->symbol.section = bfd_com_section_ptr;
1623 else
1625 cache_ptr->symbol.flags = 0;
1626 cache_ptr->symbol.section = bfd_und_section_ptr;
1628 break;
1630 case N_TEXT: case N_TEXT | N_EXT:
1631 cache_ptr->symbol.section = obj_textsec (abfd);
1632 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1633 cache_ptr->symbol.flags = visible;
1634 break;
1636 case N_DATA: case N_DATA | N_EXT:
1637 cache_ptr->symbol.section = obj_datasec (abfd);
1638 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1639 cache_ptr->symbol.flags = visible;
1640 break;
1642 case N_BSS: case N_BSS | N_EXT:
1643 cache_ptr->symbol.section = obj_bsssec (abfd);
1644 cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1645 cache_ptr->symbol.flags = visible;
1646 break;
1649 return true;
1652 /* Set the fields of SYM_POINTER according to CACHE_PTR. */
1654 static boolean
1655 translate_to_native_sym_flags (abfd, cache_ptr, sym_pointer)
1656 bfd *abfd;
1657 asymbol *cache_ptr;
1658 struct external_nlist *sym_pointer;
1660 bfd_vma value = cache_ptr->value;
1661 asection *sec;
1662 bfd_vma off;
1664 /* Mask out any existing type bits in case copying from one section
1665 to another. */
1666 sym_pointer->e_type[0] &= ~N_TYPE;
1668 sec = bfd_get_section (cache_ptr);
1669 off = 0;
1671 if (sec == NULL)
1673 /* This case occurs, e.g., for the *DEBUG* section of a COFF
1674 file. */
1675 (*_bfd_error_handler)
1676 ("%s: can not represent section for symbol `%s' in a.out object file format",
1677 bfd_archive_filename (abfd),
1678 cache_ptr->name != NULL ? cache_ptr->name : "*unknown*");
1679 bfd_set_error (bfd_error_nonrepresentable_section);
1680 return false;
1683 if (sec->output_section != NULL)
1685 off = sec->output_offset;
1686 sec = sec->output_section;
1689 if (bfd_is_abs_section (sec))
1690 sym_pointer->e_type[0] |= N_ABS;
1691 else if (sec == obj_textsec (abfd))
1692 sym_pointer->e_type[0] |= N_TEXT;
1693 else if (sec == obj_datasec (abfd))
1694 sym_pointer->e_type[0] |= N_DATA;
1695 else if (sec == obj_bsssec (abfd))
1696 sym_pointer->e_type[0] |= N_BSS;
1697 else if (bfd_is_und_section (sec))
1698 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1699 else if (bfd_is_com_section (sec))
1700 sym_pointer->e_type[0] = N_UNDF | N_EXT;
1701 else
1703 (*_bfd_error_handler)
1704 ("%s: can not represent section `%s' in a.out object file format",
1705 bfd_archive_filename (abfd), bfd_get_section_name (abfd, sec));
1706 bfd_set_error (bfd_error_nonrepresentable_section);
1707 return false;
1710 /* Turn the symbol from section relative to absolute again */
1711 value += sec->vma + off;
1713 if ((cache_ptr->flags & BSF_DEBUGGING) != 0)
1714 sym_pointer->e_type[0] = ((aout_symbol_type *) cache_ptr)->type;
1715 else if ((cache_ptr->flags & BSF_GLOBAL) != 0)
1716 sym_pointer->e_type[0] |= N_EXT;
1718 #if 0
1719 if ((cache_ptr->flags & BSF_CONSTRUCTOR) != 0)
1721 int type = ((aout_symbol_type *) cache_ptr)->type;
1724 switch (type)
1726 case N_ABS: type = N_SETA; break;
1727 case N_TEXT: type = N_SETT; break;
1728 case N_DATA: type = N_SETD; break;
1729 case N_BSS: type = N_SETB; break;
1731 sym_pointer->e_type[0] = type;
1733 #endif
1735 #if 0
1736 if ((cache_ptr->flags & BSF_WEAK) != 0)
1738 int type;
1740 switch (sym_pointer->e_type[0] & N_TYPE)
1742 default:
1743 case N_ABS: type = N_WEAKA; break;
1744 case N_TEXT: type = N_WEAKT; break;
1745 case N_DATA: type = N_WEAKD; break;
1746 case N_BSS: type = N_WEAKB; break;
1747 case N_UNDF: type = N_WEAKU; break;
1749 sym_pointer->e_type[0] = type;
1751 #endif
1753 PUT_WORD(abfd, value, sym_pointer->e_value);
1755 return true;
1758 /* Native-level interface to symbols. */
1760 asymbol *
1761 NAME(aout,make_empty_symbol) (abfd)
1762 bfd *abfd;
1764 bfd_size_type amt = sizeof (aout_symbol_type);
1765 aout_symbol_type *new = (aout_symbol_type *) bfd_zalloc (abfd, amt);
1766 if (!new)
1767 return NULL;
1768 new->symbol.the_bfd = abfd;
1770 return &new->symbol;
1773 /* Translate a set of internal symbols into external symbols. */
1775 boolean
1776 NAME(aout,translate_symbol_table) (abfd, in, ext, count, str, strsize, dynamic)
1777 bfd *abfd;
1778 aout_symbol_type *in;
1779 struct external_nlist *ext;
1780 bfd_size_type count;
1781 char *str;
1782 bfd_size_type strsize;
1783 boolean dynamic;
1785 struct external_nlist *ext_end;
1787 ext_end = ext + count;
1788 for (; ext < ext_end; ext++, in++)
1790 bfd_vma x;
1792 x = GET_WORD (abfd, ext->e_strx);
1793 in->symbol.the_bfd = abfd;
1795 /* For the normal symbols, the zero index points at the number
1796 of bytes in the string table but is to be interpreted as the
1797 null string. For the dynamic symbols, the number of bytes in
1798 the string table is stored in the __DYNAMIC structure and the
1799 zero index points at an actual string. */
1800 if (x == 0 && ! dynamic)
1801 in->symbol.name = "";
1802 else if (x < strsize)
1803 in->symbol.name = str + x;
1804 else
1805 return false;
1807 in->symbol.value = GET_SWORD (abfd, ext->e_value);
1808 /* TODO: is 0 a safe value here? */
1809 in->desc = 0;
1810 in->other = 0;
1811 in->type = H_GET_8 (abfd, ext->e_type);
1812 in->symbol.udata.p = NULL;
1814 if (! translate_from_native_sym_flags (abfd, in))
1815 return false;
1817 if (dynamic)
1818 in->symbol.flags |= BSF_DYNAMIC;
1821 return true;
1824 /* We read the symbols into a buffer, which is discarded when this
1825 function exits. We read the strings into a buffer large enough to
1826 hold them all plus all the cached symbol entries. */
1828 boolean
1829 NAME(aout,slurp_symbol_table) (abfd)
1830 bfd *abfd;
1832 struct external_nlist *old_external_syms;
1833 aout_symbol_type *cached;
1834 bfd_size_type cached_size;
1836 /* If there's no work to be done, don't do any */
1837 if (obj_aout_symbols (abfd) != (aout_symbol_type *) NULL)
1838 return true;
1840 old_external_syms = obj_aout_external_syms (abfd);
1842 if (! aout_get_external_symbols (abfd))
1843 return false;
1845 cached_size = obj_aout_external_sym_count (abfd);
1846 cached_size *= sizeof (aout_symbol_type);
1847 cached = (aout_symbol_type *) bfd_zmalloc (cached_size);
1848 if (cached == NULL && cached_size != 0)
1849 return false;
1851 /* Convert from external symbol information to internal. */
1852 if (! (NAME(aout,translate_symbol_table)
1853 (abfd, cached,
1854 obj_aout_external_syms (abfd),
1855 obj_aout_external_sym_count (abfd),
1856 obj_aout_external_strings (abfd),
1857 obj_aout_external_string_size (abfd),
1858 false)))
1860 free (cached);
1861 return false;
1864 bfd_get_symcount (abfd) = obj_aout_external_sym_count (abfd);
1866 obj_aout_symbols (abfd) = cached;
1868 /* It is very likely that anybody who calls this function will not
1869 want the external symbol information, so if it was allocated
1870 because of our call to aout_get_external_symbols, we free it up
1871 right away to save space. */
1872 if (old_external_syms == (struct external_nlist *) NULL
1873 && obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
1875 #ifdef USE_MMAP
1876 bfd_free_window (&obj_aout_sym_window (abfd));
1877 #else
1878 free (obj_aout_external_syms (abfd));
1879 #endif
1880 obj_aout_external_syms (abfd) = NULL;
1883 return true;
1886 /* We use a hash table when writing out symbols so that we only write
1887 out a particular string once. This helps particularly when the
1888 linker writes out stabs debugging entries, because each different
1889 contributing object file tends to have many duplicate stabs
1890 strings.
1892 This hash table code breaks dbx on SunOS 4.1.3, so we don't do it
1893 if BFD_TRADITIONAL_FORMAT is set. */
1895 static bfd_size_type add_to_stringtab
1896 PARAMS ((bfd *, struct bfd_strtab_hash *, const char *, boolean));
1897 static boolean emit_stringtab PARAMS ((bfd *, struct bfd_strtab_hash *));
1899 /* Get the index of a string in a strtab, adding it if it is not
1900 already present. */
1902 static INLINE bfd_size_type
1903 add_to_stringtab (abfd, tab, str, copy)
1904 bfd *abfd;
1905 struct bfd_strtab_hash *tab;
1906 const char *str;
1907 boolean copy;
1909 boolean hash;
1910 bfd_size_type index;
1912 /* An index of 0 always means the empty string. */
1913 if (str == 0 || *str == '\0')
1914 return 0;
1916 /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
1917 doesn't understand a hashed string table. */
1918 hash = true;
1919 if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1920 hash = false;
1922 index = _bfd_stringtab_add (tab, str, hash, copy);
1924 if (index != (bfd_size_type) -1)
1926 /* Add BYTES_IN_LONG to the return value to account for the
1927 space taken up by the string table size. */
1928 index += BYTES_IN_LONG;
1931 return index;
1934 /* Write out a strtab. ABFD is already at the right location in the
1935 file. */
1937 static boolean
1938 emit_stringtab (abfd, tab)
1939 register bfd *abfd;
1940 struct bfd_strtab_hash *tab;
1942 bfd_byte buffer[BYTES_IN_LONG];
1944 /* The string table starts with the size. */
1945 H_PUT_32 (abfd, _bfd_stringtab_size (tab) + BYTES_IN_LONG, buffer);
1946 if (bfd_bwrite ((PTR) buffer, (bfd_size_type) BYTES_IN_LONG, abfd)
1947 != BYTES_IN_LONG)
1948 return false;
1950 return _bfd_stringtab_emit (abfd, tab);
1953 boolean
1954 NAME(aout,write_syms) (abfd)
1955 bfd *abfd;
1957 unsigned int count ;
1958 asymbol **generic = bfd_get_outsymbols (abfd);
1959 struct bfd_strtab_hash *strtab;
1961 strtab = _bfd_stringtab_init ();
1962 if (strtab == NULL)
1963 return false;
1965 for (count = 0; count < bfd_get_symcount (abfd); count++)
1967 asymbol *g = generic[count];
1968 bfd_size_type indx;
1969 struct external_nlist nsp;
1971 PUT_WORD (abfd, 0, nsp.e_unused);
1973 indx = add_to_stringtab (abfd, strtab, g->name, false);
1974 if (indx == (bfd_size_type) -1)
1975 goto error_return;
1976 PUT_WORD (abfd, indx, nsp.e_strx);
1978 if (bfd_asymbol_flavour(g) == abfd->xvec->flavour)
1979 H_PUT_8 (abfd, aout_symbol(g)->type, nsp.e_type);
1980 else
1981 H_PUT_8 (abfd, 0, nsp.e_type);
1983 if (! translate_to_native_sym_flags (abfd, g, &nsp))
1984 goto error_return;
1986 H_PUT_8 (abfd, 0, nsp.e_ovly);
1988 if (bfd_bwrite ((PTR)&nsp, (bfd_size_type) EXTERNAL_NLIST_SIZE, abfd)
1989 != EXTERNAL_NLIST_SIZE)
1990 goto error_return;
1992 /* NB: `KEEPIT' currently overlays `udata.p', so set this only
1993 here, at the end. */
1994 g->KEEPIT = count;
1997 if (! emit_stringtab (abfd, strtab))
1998 goto error_return;
2000 _bfd_stringtab_free (strtab);
2002 return true;
2004 error_return:
2005 _bfd_stringtab_free (strtab);
2006 return false;
2010 long
2011 NAME(aout,get_symtab) (abfd, location)
2012 bfd *abfd;
2013 asymbol **location;
2015 unsigned int counter = 0;
2016 aout_symbol_type *symbase;
2018 if (!NAME(aout,slurp_symbol_table)(abfd))
2019 return -1;
2021 for (symbase = obj_aout_symbols(abfd); counter++ < bfd_get_symcount (abfd);)
2022 *(location++) = (asymbol *)( symbase++);
2023 *location++ =0;
2024 return bfd_get_symcount (abfd);
2028 /* Standard reloc stuff */
2030 /* Extended stuff */
2031 /* Output extended relocation information to a file in target byte order. */
2033 void
2034 pdp11_aout_swap_reloc_out (abfd, g, natptr)
2035 bfd *abfd;
2036 arelent *g;
2037 register struct pdp11_aout_reloc_external *natptr;
2039 int r_index;
2040 int r_pcrel;
2041 int reloc_entry;
2042 int r_type;
2043 asymbol *sym = *(g->sym_ptr_ptr);
2044 asection *output_section = sym->section->output_section;
2046 if (g->addend != 0)
2047 fprintf (stderr, "BFD: can't do this reloc addend stuff\n");
2049 r_pcrel = g->howto->pc_relative;
2051 if (bfd_is_abs_section (output_section))
2052 r_type = RABS;
2053 else if (output_section == obj_textsec (abfd))
2054 r_type = RTEXT;
2055 else if (output_section == obj_datasec (abfd))
2056 r_type = RDATA;
2057 else if (output_section == obj_bsssec (abfd))
2058 r_type = RBSS;
2059 else if (bfd_is_und_section (output_section))
2060 r_type = REXT;
2061 else if (bfd_is_com_section (output_section))
2062 r_type = REXT;
2063 else
2064 r_type = -1;
2066 BFD_ASSERT (r_type != -1);
2068 if (r_type == RABS)
2069 r_index = 0;
2070 else
2071 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
2073 #if 0
2074 if (bfd_is_abs_section (bfd_get_section (sym)))
2076 r_extern = 0;
2077 r_index = N_ABS;
2078 r_type = RABS;
2080 else if ((sym->flags & BSF_SECTION_SYM) == 0)
2082 if (bfd_is_und_section (bfd_get_section (sym))
2083 || (sym->flags & BSF_GLOBAL) != 0)
2084 r_extern = 1;
2085 else
2086 r_extern = 0;
2087 r_index = (*(g->sym_ptr_ptr))->KEEPIT;
2089 else
2091 /* Just an ordinary section */
2092 r_extern = 0;
2093 r_index = output_section->target_index;
2095 #endif
2097 reloc_entry = r_index << 4 | r_type | r_pcrel;
2099 PUT_WORD (abfd, reloc_entry, natptr->e_reloc_entry);
2102 /* BFD deals internally with all things based from the section they're
2103 in. so, something in 10 bytes into a text section with a base of
2104 50 would have a symbol (.text+10) and know .text vma was 50.
2106 Aout keeps all it's symbols based from zero, so the symbol would
2107 contain 60. This macro subs the base of each section from the value
2108 to give the true offset from the section */
2111 #define MOVE_ADDRESS(ad) \
2112 if (r_extern) \
2114 /* Undefined symbol. */ \
2115 cache_ptr->sym_ptr_ptr = symbols + r_index; \
2116 cache_ptr->addend = ad; \
2118 else \
2120 /* Defined, section relative. replace symbol with pointer to \
2121 symbol which points to section. */ \
2122 switch (r_index) \
2124 case N_TEXT: \
2125 case N_TEXT | N_EXT: \
2126 cache_ptr->sym_ptr_ptr = obj_textsec (abfd)->symbol_ptr_ptr; \
2127 cache_ptr->addend = ad - su->textsec->vma; \
2128 break; \
2129 case N_DATA: \
2130 case N_DATA | N_EXT: \
2131 cache_ptr->sym_ptr_ptr = obj_datasec (abfd)->symbol_ptr_ptr; \
2132 cache_ptr->addend = ad - su->datasec->vma; \
2133 break; \
2134 case N_BSS: \
2135 case N_BSS | N_EXT: \
2136 cache_ptr->sym_ptr_ptr = obj_bsssec (abfd)->symbol_ptr_ptr; \
2137 cache_ptr->addend = ad - su->bsssec->vma; \
2138 break; \
2139 default: \
2140 case N_ABS: \
2141 case N_ABS | N_EXT: \
2142 cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
2143 cache_ptr->addend = ad; \
2144 break; \
2148 void
2149 pdp11_aout_swap_reloc_in (abfd, bytes, cache_ptr, offset,
2150 symbols, symcount)
2151 bfd *abfd;
2152 struct pdp11_aout_reloc_external *bytes;
2153 arelent *cache_ptr;
2154 bfd_size_type offset;
2155 asymbol **symbols;
2156 bfd_size_type symcount;
2158 struct aoutdata *su = &(abfd->tdata.aout_data->a);
2159 unsigned int r_index;
2160 int reloc_entry;
2161 int r_extern;
2162 int r_pcrel;
2164 reloc_entry = GET_WORD (abfd, (PTR)bytes);
2166 r_pcrel = reloc_entry & RELFLG;
2168 cache_ptr->address = offset;
2169 cache_ptr->howto = howto_table_pdp11 + (r_pcrel ? 1 : 0);
2171 if ((reloc_entry & RTYPE) == RABS)
2172 r_index = N_ABS;
2173 else
2174 r_index = RINDEX (reloc_entry);
2176 /* r_extern reflects whether the symbol the reloc is against is
2177 local or global. */
2178 r_extern = (reloc_entry & RTYPE) == REXT;
2180 if (r_extern && r_index > symcount)
2182 /* We could arrange to return an error, but it might be useful
2183 to see the file even if it is bad. */
2184 r_extern = 0;
2185 r_index = N_ABS;
2188 MOVE_ADDRESS(0);
2191 /* Read and swap the relocs for a section. */
2193 boolean
2194 NAME(aout,slurp_reloc_table) (abfd, asect, symbols)
2195 bfd *abfd;
2196 sec_ptr asect;
2197 asymbol **symbols;
2199 struct pdp11_aout_reloc_external *rptr;
2200 bfd_size_type count;
2201 bfd_size_type reloc_size;
2202 PTR relocs;
2203 arelent *reloc_cache;
2204 size_t each_size;
2205 unsigned int counter = 0;
2206 arelent *cache_ptr;
2208 if (asect->relocation)
2209 return true;
2211 if (asect->flags & SEC_CONSTRUCTOR)
2212 return true;
2214 if (asect == obj_datasec (abfd))
2215 reloc_size = exec_hdr(abfd)->a_drsize;
2216 else if (asect == obj_textsec (abfd))
2217 reloc_size = exec_hdr(abfd)->a_trsize;
2218 else if (asect == obj_bsssec (abfd))
2219 reloc_size = 0;
2220 else
2222 bfd_set_error (bfd_error_invalid_operation);
2223 return false;
2226 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2227 return false;
2229 each_size = obj_reloc_entry_size (abfd);
2231 relocs = bfd_malloc (reloc_size);
2232 if (relocs == NULL && reloc_size != 0)
2233 return false;
2235 if (bfd_bread (relocs, reloc_size, abfd) != reloc_size)
2237 free (relocs);
2238 return false;
2241 count = reloc_size / each_size;
2243 /* Count the number of NON-ZERO relocs, this is the count we want. */
2245 unsigned int real_count = 0;
2247 for (counter = 0; counter < count; counter++)
2249 int x;
2251 x = GET_WORD (abfd, (char *) relocs + each_size * counter);
2252 if (x != 0)
2253 real_count++;
2256 count = real_count;
2259 reloc_cache = (arelent *) bfd_zmalloc (count * sizeof (arelent));
2260 if (reloc_cache == NULL && count != 0)
2261 return false;
2263 cache_ptr = reloc_cache;
2265 rptr = (struct pdp11_aout_reloc_external *) relocs;
2266 for (counter = 0;
2267 counter < count;
2268 counter++, ((char *)rptr) += RELOC_SIZE, cache_ptr++)
2270 while (GET_WORD (abfd, (PTR)rptr) == 0)
2272 rptr =
2273 (struct pdp11_aout_reloc_external *)
2274 ((char *) rptr + RELOC_SIZE);
2275 if ((char *) rptr >= (char *) relocs + reloc_size)
2276 goto done;
2279 pdp11_aout_swap_reloc_in (abfd, rptr, cache_ptr,
2280 (bfd_size_type) ((char *) rptr - (char *) relocs),
2281 symbols,
2282 (bfd_size_type) bfd_get_symcount (abfd));
2284 done:
2285 /* Just in case, if rptr >= relocs + reloc_size should happen
2286 too early. */
2287 BFD_ASSERT (counter == count);
2289 free (relocs);
2291 asect->relocation = reloc_cache;
2292 asect->reloc_count = cache_ptr - reloc_cache;
2294 return true;
2297 /* Write out a relocation section into an object file. */
2299 boolean
2300 NAME(aout,squirt_out_relocs) (abfd, section)
2301 bfd *abfd;
2302 asection *section;
2304 arelent **generic;
2305 unsigned char *native;
2306 unsigned int count = section->reloc_count;
2307 bfd_size_type natsize;
2309 #if 0
2310 /* If we're writing an .o file, we must write
2311 relocation information, even if there is none. */
2312 if ((count == 0 || section->orelocation == NULL) &&
2313 <writing_executable>)
2314 return true;
2315 #endif
2317 natsize = bfd_get_section_size_before_reloc (section);
2318 native = (unsigned char *) bfd_zalloc (abfd, natsize);
2319 if (!native)
2320 return false;
2322 generic = section->orelocation;
2323 if (generic != NULL)
2325 while (count > 0)
2327 struct pdp11_aout_reloc_external *r;
2329 r = (struct pdp11_aout_reloc_external *)
2330 (native + (*generic)->address);
2331 pdp11_aout_swap_reloc_out (abfd, *generic, r);
2332 count--;
2333 generic++;
2337 if (bfd_bwrite ((PTR) native, natsize, abfd) != natsize)
2339 bfd_release (abfd, native);
2340 return false;
2343 bfd_release (abfd, native);
2344 return true;
2347 /* This is stupid. This function should be a boolean predicate */
2348 long
2349 NAME(aout,canonicalize_reloc) (abfd, section, relptr, symbols)
2350 bfd *abfd;
2351 sec_ptr section;
2352 arelent **relptr;
2353 asymbol **symbols;
2355 arelent *tblptr = section->relocation;
2356 unsigned int count;
2358 if (section == obj_bsssec (abfd))
2360 *relptr = NULL;
2361 return 0;
2364 if (!(tblptr || NAME(aout,slurp_reloc_table)(abfd, section, symbols)))
2365 return -1;
2367 if (section->flags & SEC_CONSTRUCTOR)
2369 arelent_chain *chain = section->constructor_chain;
2371 for (count = 0; count < section->reloc_count; count ++)
2373 *relptr ++ = &chain->relent;
2374 chain = chain->next;
2377 else
2379 tblptr = section->relocation;
2381 for (count = 0; count++ < section->reloc_count;)
2382 *relptr++ = tblptr++;
2385 *relptr = 0;
2387 return section->reloc_count;
2390 long
2391 NAME(aout,get_reloc_upper_bound) (abfd, asect)
2392 bfd *abfd;
2393 sec_ptr asect;
2395 if (bfd_get_format (abfd) != bfd_object)
2397 bfd_set_error (bfd_error_invalid_operation);
2398 return -1;
2401 if (asect->flags & SEC_CONSTRUCTOR)
2402 return (sizeof (arelent *) * (asect->reloc_count+1));
2404 if (asect == obj_datasec (abfd))
2405 return (sizeof (arelent *)
2406 * ((exec_hdr(abfd)->a_drsize / obj_reloc_entry_size (abfd))
2407 + 1));
2409 if (asect == obj_textsec (abfd))
2410 return (sizeof (arelent *)
2411 * ((exec_hdr(abfd)->a_trsize / obj_reloc_entry_size (abfd))
2412 + 1));
2414 /* TODO: why are there two if statements for obj_bsssec()? */
2416 if (asect == obj_bsssec (abfd))
2417 return sizeof (arelent *);
2419 if (asect == obj_bsssec (abfd))
2420 return 0;
2422 bfd_set_error (bfd_error_invalid_operation);
2423 return -1;
2427 long
2428 NAME(aout,get_symtab_upper_bound) (abfd)
2429 bfd *abfd;
2431 if (!NAME(aout,slurp_symbol_table)(abfd))
2432 return -1;
2434 return (bfd_get_symcount (abfd) + 1) * (sizeof (aout_symbol_type *));
2437 alent *
2438 NAME(aout,get_lineno) (abfd, symbol)
2439 bfd * abfd ATTRIBUTE_UNUSED;
2440 asymbol * symbol ATTRIBUTE_UNUSED;
2442 return (alent *)NULL;
2445 void
2446 NAME(aout,get_symbol_info) (abfd, symbol, ret)
2447 bfd * abfd ATTRIBUTE_UNUSED;
2448 asymbol *symbol;
2449 symbol_info *ret;
2451 bfd_symbol_info (symbol, ret);
2453 if (ret->type == '?')
2455 int type_code = aout_symbol(symbol)->type & 0xff;
2456 const char *stab_name = bfd_get_stab_name (type_code);
2457 static char buf[10];
2459 if (stab_name == NULL)
2461 sprintf(buf, "(%d)", type_code);
2462 stab_name = buf;
2464 ret->type = '-';
2465 ret->stab_type = type_code;
2466 ret->stab_other = (unsigned)(aout_symbol(symbol)->other & 0xff);
2467 ret->stab_desc = (unsigned)(aout_symbol(symbol)->desc & 0xffff);
2468 ret->stab_name = stab_name;
2472 /*ARGSUSED*/
2473 void
2474 NAME(aout,print_symbol) (abfd, afile, symbol, how)
2475 bfd * abfd;
2476 PTR afile;
2477 asymbol *symbol;
2478 bfd_print_symbol_type how;
2480 FILE *file = (FILE *)afile;
2482 switch (how)
2484 case bfd_print_symbol_name:
2485 if (symbol->name)
2486 fprintf(file,"%s", symbol->name);
2487 break;
2488 case bfd_print_symbol_more:
2489 fprintf(file,"%4x %2x %2x",(unsigned)(aout_symbol(symbol)->desc & 0xffff),
2490 (unsigned)(aout_symbol(symbol)->other & 0xff),
2491 (unsigned)(aout_symbol(symbol)->type));
2492 break;
2493 case bfd_print_symbol_all:
2495 const char *section_name = symbol->section->name;
2497 bfd_print_symbol_vandf (abfd, (PTR)file, symbol);
2499 fprintf (file," %-5s %04x %02x %02x",
2500 section_name,
2501 (unsigned)(aout_symbol(symbol)->desc & 0xffff),
2502 (unsigned)(aout_symbol(symbol)->other & 0xff),
2503 (unsigned)(aout_symbol(symbol)->type & 0xff));
2504 if (symbol->name)
2505 fprintf(file," %s", symbol->name);
2507 break;
2511 /* If we don't have to allocate more than 1MB to hold the generic
2512 symbols, we use the generic minisymbol method: it's faster, since
2513 it only translates the symbols once, not multiple times. */
2514 #define MINISYM_THRESHOLD (1000000 / sizeof (asymbol))
2516 /* Read minisymbols. For minisymbols, we use the unmodified a.out
2517 symbols. The minisymbol_to_symbol function translates these into
2518 BFD asymbol structures. */
2520 long
2521 NAME(aout,read_minisymbols) (abfd, dynamic, minisymsp, sizep)
2522 bfd *abfd;
2523 boolean dynamic;
2524 PTR *minisymsp;
2525 unsigned int *sizep;
2527 if (dynamic)
2529 /* We could handle the dynamic symbols here as well, but it's
2530 easier to hand them off. */
2531 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2534 if (! aout_get_external_symbols (abfd))
2535 return -1;
2537 if (obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2538 return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2540 *minisymsp = (PTR) obj_aout_external_syms (abfd);
2542 /* By passing the external symbols back from this routine, we are
2543 giving up control over the memory block. Clear
2544 obj_aout_external_syms, so that we do not try to free it
2545 ourselves. */
2546 obj_aout_external_syms (abfd) = NULL;
2548 *sizep = EXTERNAL_NLIST_SIZE;
2549 return obj_aout_external_sym_count (abfd);
2552 /* Convert a minisymbol to a BFD asymbol. A minisymbol is just an
2553 unmodified a.out symbol. The SYM argument is a structure returned
2554 by bfd_make_empty_symbol, which we fill in here. */
2556 asymbol *
2557 NAME(aout,minisymbol_to_symbol) (abfd, dynamic, minisym, sym)
2558 bfd *abfd;
2559 boolean dynamic;
2560 const PTR minisym;
2561 asymbol *sym;
2563 if (dynamic
2564 || obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2565 return _bfd_generic_minisymbol_to_symbol (abfd, dynamic, minisym, sym);
2567 memset (sym, 0, sizeof (aout_symbol_type));
2569 /* We call translate_symbol_table to translate a single symbol. */
2570 if (! (NAME(aout,translate_symbol_table)
2571 (abfd,
2572 (aout_symbol_type *) sym,
2573 (struct external_nlist *) minisym,
2574 (bfd_size_type) 1,
2575 obj_aout_external_strings (abfd),
2576 obj_aout_external_string_size (abfd),
2577 false)))
2578 return NULL;
2580 return sym;
2584 provided a BFD, a section and an offset into the section, calculate
2585 and return the name of the source file and the line nearest to the
2586 wanted location.
2589 boolean
2590 NAME(aout,find_nearest_line)
2591 (abfd, section, symbols, offset, filename_ptr, functionname_ptr, line_ptr)
2592 bfd *abfd;
2593 asection *section;
2594 asymbol **symbols;
2595 bfd_vma offset;
2596 const char **filename_ptr;
2597 const char **functionname_ptr;
2598 unsigned int *line_ptr;
2600 /* Run down the file looking for the filename, function and linenumber */
2601 asymbol **p;
2602 const char *directory_name = NULL;
2603 const char *main_file_name = NULL;
2604 const char *current_file_name = NULL;
2605 const char *line_file_name = NULL; /* Value of current_file_name at line number. */
2606 bfd_vma low_line_vma = 0;
2607 bfd_vma low_func_vma = 0;
2608 asymbol *func = 0;
2609 size_t filelen, funclen;
2610 char *buf;
2612 *filename_ptr = abfd->filename;
2613 *functionname_ptr = 0;
2614 *line_ptr = 0;
2616 if (symbols != (asymbol **)NULL)
2618 for (p = symbols; *p; p++)
2620 aout_symbol_type *q = (aout_symbol_type *)(*p);
2621 next:
2622 switch (q->type)
2624 case N_TEXT:
2625 /* If this looks like a file name symbol, and it comes after
2626 the line number we have found so far, but before the
2627 offset, then we have probably not found the right line
2628 number. */
2629 if (q->symbol.value <= offset
2630 && ((q->symbol.value > low_line_vma
2631 && (line_file_name != NULL
2632 || *line_ptr != 0))
2633 || (q->symbol.value > low_func_vma
2634 && func != NULL)))
2636 const char * symname;
2638 symname = q->symbol.name;
2639 if (strcmp (symname + strlen (symname) - 2, ".o") == 0)
2641 if (q->symbol.value > low_line_vma)
2643 *line_ptr = 0;
2644 line_file_name = NULL;
2646 if (q->symbol.value > low_func_vma)
2647 func = NULL;
2650 break;
2652 case N_SO:
2653 /* If this symbol is less than the offset, but greater than
2654 the line number we have found so far, then we have not
2655 found the right line number. */
2656 if (q->symbol.value <= offset)
2658 if (q->symbol.value > low_line_vma)
2660 *line_ptr = 0;
2661 line_file_name = NULL;
2663 if (q->symbol.value > low_func_vma)
2664 func = NULL;
2667 main_file_name = current_file_name = q->symbol.name;
2668 /* Look ahead to next symbol to check if that too is an N_SO. */
2669 p++;
2670 if (*p == NULL)
2671 break;
2672 q = (aout_symbol_type *)(*p);
2673 if (q->type != (int)N_SO)
2674 goto next;
2676 /* Found a second N_SO First is directory; second is filename. */
2677 directory_name = current_file_name;
2678 main_file_name = current_file_name = q->symbol.name;
2679 if (obj_textsec(abfd) != section)
2680 goto done;
2681 break;
2682 case N_SOL:
2683 current_file_name = q->symbol.name;
2684 break;
2686 case N_SLINE:
2687 case N_DSLINE:
2688 case N_BSLINE:
2689 /* We'll keep this if it resolves nearer than the one we have
2690 already. */
2691 if (q->symbol.value >= low_line_vma
2692 && q->symbol.value <= offset)
2694 *line_ptr = q->desc;
2695 low_line_vma = q->symbol.value;
2696 line_file_name = current_file_name;
2698 break;
2700 case N_FUN:
2702 /* We'll keep this if it is nearer than the one we have already */
2703 if (q->symbol.value >= low_func_vma &&
2704 q->symbol.value <= offset)
2706 low_func_vma = q->symbol.value;
2707 func = (asymbol *)q;
2709 else if (q->symbol.value > offset)
2710 goto done;
2712 break;
2717 done:
2718 if (*line_ptr != 0)
2719 main_file_name = line_file_name;
2721 if (main_file_name == NULL
2722 || main_file_name[0] == '/'
2723 || directory_name == NULL)
2724 filelen = 0;
2725 else
2726 filelen = strlen (directory_name) + strlen (main_file_name);
2727 if (func == NULL)
2728 funclen = 0;
2729 else
2730 funclen = strlen (bfd_asymbol_name (func));
2732 if (adata (abfd).line_buf != NULL)
2733 free (adata (abfd).line_buf);
2734 if (filelen + funclen == 0)
2735 adata (abfd).line_buf = buf = NULL;
2736 else
2738 buf = (char *) bfd_malloc ((bfd_size_type) filelen + funclen + 3);
2739 adata (abfd).line_buf = buf;
2740 if (buf == NULL)
2741 return false;
2744 if (main_file_name != NULL)
2746 if (main_file_name[0] == '/' || directory_name == NULL)
2747 *filename_ptr = main_file_name;
2748 else
2750 sprintf (buf, "%s%s", directory_name, main_file_name);
2751 *filename_ptr = buf;
2752 buf += filelen + 1;
2756 if (func)
2758 const char *function = func->name;
2759 char *colon;
2761 /* The caller expects a symbol name. We actually have a
2762 function name, without the leading underscore. Put the
2763 underscore back in, so that the caller gets a symbol name. */
2764 if (bfd_get_symbol_leading_char (abfd) == '\0')
2765 strcpy (buf, function);
2766 else
2768 buf[0] = bfd_get_symbol_leading_char (abfd);
2769 strcpy (buf + 1, function);
2772 /* Have to remove : stuff. */
2773 colon = strchr (buf, ':');
2774 if (colon != NULL)
2775 *colon = '\0';
2776 *functionname_ptr = buf;
2779 return true;
2783 NAME(aout,sizeof_headers) (abfd, execable)
2784 bfd *abfd;
2785 boolean execable ATTRIBUTE_UNUSED;
2787 return adata(abfd).exec_bytes_size;
2790 /* Free all information we have cached for this BFD. We can always
2791 read it again later if we need it. */
2793 boolean
2794 NAME(aout,bfd_free_cached_info) (abfd)
2795 bfd *abfd;
2797 asection *o;
2799 if (bfd_get_format (abfd) != bfd_object)
2800 return true;
2802 #define BFCI_FREE(x) if (x != NULL) { free (x); x = NULL; }
2803 BFCI_FREE (obj_aout_symbols (abfd));
2804 #ifdef USE_MMAP
2805 obj_aout_external_syms (abfd) = 0;
2806 bfd_free_window (&obj_aout_sym_window (abfd));
2807 bfd_free_window (&obj_aout_string_window (abfd));
2808 obj_aout_external_strings (abfd) = 0;
2809 #else
2810 BFCI_FREE (obj_aout_external_syms (abfd));
2811 BFCI_FREE (obj_aout_external_strings (abfd));
2812 #endif
2813 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2814 BFCI_FREE (o->relocation);
2815 #undef BFCI_FREE
2817 return true;
2820 /* a.out link code. */
2822 static boolean aout_link_add_object_symbols
2823 PARAMS ((bfd *, struct bfd_link_info *));
2824 static boolean aout_link_check_archive_element
2825 PARAMS ((bfd *, struct bfd_link_info *, boolean *));
2826 static boolean aout_link_free_symbols PARAMS ((bfd *));
2827 static boolean aout_link_check_ar_symbols
2828 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
2829 static boolean aout_link_add_symbols
2830 PARAMS ((bfd *, struct bfd_link_info *));
2832 /* Routine to create an entry in an a.out link hash table. */
2834 struct bfd_hash_entry *
2835 NAME(aout,link_hash_newfunc) (entry, table, string)
2836 struct bfd_hash_entry *entry;
2837 struct bfd_hash_table *table;
2838 const char *string;
2840 struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
2842 /* Allocate the structure if it has not already been allocated by a
2843 subclass. */
2844 if (ret == (struct aout_link_hash_entry *) NULL)
2845 ret = ((struct aout_link_hash_entry *)
2846 bfd_hash_allocate (table, sizeof (struct aout_link_hash_entry)));
2847 if (ret == (struct aout_link_hash_entry *) NULL)
2848 return (struct bfd_hash_entry *) ret;
2850 /* Call the allocation method of the superclass. */
2851 ret = ((struct aout_link_hash_entry *)
2852 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2853 table, string));
2854 if (ret)
2856 /* Set local fields. */
2857 ret->written = false;
2858 ret->indx = -1;
2861 return (struct bfd_hash_entry *) ret;
2864 /* Initialize an a.out link hash table. */
2866 boolean
2867 NAME(aout,link_hash_table_init) (table, abfd, newfunc)
2868 struct aout_link_hash_table *table;
2869 bfd *abfd;
2870 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
2871 struct bfd_hash_table *,
2872 const char *));
2874 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
2877 /* Create an a.out link hash table. */
2879 struct bfd_link_hash_table *
2880 NAME(aout,link_hash_table_create) (abfd)
2881 bfd *abfd;
2883 struct aout_link_hash_table *ret;
2884 bfd_size_type amt = sizeof (struct aout_link_hash_table);
2886 ret = ((struct aout_link_hash_table *) bfd_alloc (abfd, amt));
2887 if (ret == NULL)
2888 return (struct bfd_link_hash_table *) NULL;
2889 if (! NAME(aout,link_hash_table_init) (ret, abfd,
2890 NAME(aout,link_hash_newfunc)))
2892 free (ret);
2893 return (struct bfd_link_hash_table *) NULL;
2895 return &ret->root;
2898 /* Given an a.out BFD, add symbols to the global hash table as
2899 appropriate. */
2901 boolean
2902 NAME(aout,link_add_symbols) (abfd, info)
2903 bfd *abfd;
2904 struct bfd_link_info *info;
2906 switch (bfd_get_format (abfd))
2908 case bfd_object:
2909 return aout_link_add_object_symbols (abfd, info);
2910 case bfd_archive:
2911 return _bfd_generic_link_add_archive_symbols
2912 (abfd, info, aout_link_check_archive_element);
2913 default:
2914 bfd_set_error (bfd_error_wrong_format);
2915 return false;
2919 /* Add symbols from an a.out object file. */
2921 static boolean
2922 aout_link_add_object_symbols (abfd, info)
2923 bfd *abfd;
2924 struct bfd_link_info *info;
2926 if (! aout_get_external_symbols (abfd))
2927 return false;
2928 if (! aout_link_add_symbols (abfd, info))
2929 return false;
2930 if (! info->keep_memory)
2932 if (! aout_link_free_symbols (abfd))
2933 return false;
2935 return true;
2938 /* Check a single archive element to see if we need to include it in
2939 the link. *PNEEDED is set according to whether this element is
2940 needed in the link or not. This is called from
2941 _bfd_generic_link_add_archive_symbols. */
2943 static boolean
2944 aout_link_check_archive_element (abfd, info, pneeded)
2945 bfd *abfd;
2946 struct bfd_link_info *info;
2947 boolean *pneeded;
2949 if (! aout_get_external_symbols (abfd))
2950 return false;
2952 if (! aout_link_check_ar_symbols (abfd, info, pneeded))
2953 return false;
2955 if (*pneeded)
2957 if (! aout_link_add_symbols (abfd, info))
2958 return false;
2961 if (! info->keep_memory || ! *pneeded)
2963 if (! aout_link_free_symbols (abfd))
2964 return false;
2967 return true;
2970 /* Free up the internal symbols read from an a.out file. */
2972 static boolean
2973 aout_link_free_symbols (abfd)
2974 bfd *abfd;
2976 if (obj_aout_external_syms (abfd) != (struct external_nlist *) NULL)
2978 #ifdef USE_MMAP
2979 bfd_free_window (&obj_aout_sym_window (abfd));
2980 #else
2981 free ((PTR) obj_aout_external_syms (abfd));
2982 #endif
2983 obj_aout_external_syms (abfd) = (struct external_nlist *) NULL;
2985 if (obj_aout_external_strings (abfd) != (char *) NULL)
2987 #ifdef USE_MMAP
2988 bfd_free_window (&obj_aout_string_window (abfd));
2989 #else
2990 free ((PTR) obj_aout_external_strings (abfd));
2991 #endif
2992 obj_aout_external_strings (abfd) = (char *) NULL;
2994 return true;
2997 /* Look through the internal symbols to see if this object file should
2998 be included in the link. We should include this object file if it
2999 defines any symbols which are currently undefined. If this object
3000 file defines a common symbol, then we may adjust the size of the
3001 known symbol but we do not include the object file in the link
3002 (unless there is some other reason to include it). */
3004 static boolean
3005 aout_link_check_ar_symbols (abfd, info, pneeded)
3006 bfd *abfd;
3007 struct bfd_link_info *info;
3008 boolean *pneeded;
3010 register struct external_nlist *p;
3011 struct external_nlist *pend;
3012 char *strings;
3014 *pneeded = false;
3016 /* Look through all the symbols. */
3017 p = obj_aout_external_syms (abfd);
3018 pend = p + obj_aout_external_sym_count (abfd);
3019 strings = obj_aout_external_strings (abfd);
3020 for (; p < pend; p++)
3022 int type = H_GET_8 (abfd, p->e_type);
3023 const char *name;
3024 struct bfd_link_hash_entry *h;
3026 /* Ignore symbols that are not externally visible. This is an
3027 optimization only, as we check the type more thoroughly
3028 below. */
3029 if ((type & N_EXT) == 0
3030 || type == N_FN)
3031 continue;
3033 name = strings + GET_WORD (abfd, p->e_strx);
3034 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3036 /* We are only interested in symbols that are currently
3037 undefined or common. */
3038 if (h == (struct bfd_link_hash_entry *) NULL
3039 || (h->type != bfd_link_hash_undefined
3040 && h->type != bfd_link_hash_common))
3041 continue;
3043 if (type == (N_TEXT | N_EXT)
3044 || type == (N_DATA | N_EXT)
3045 || type == (N_BSS | N_EXT)
3046 || type == (N_ABS | N_EXT))
3048 /* This object file defines this symbol. We must link it
3049 in. This is true regardless of whether the current
3050 definition of the symbol is undefined or common. If the
3051 current definition is common, we have a case in which we
3052 have already seen an object file including
3053 int a;
3054 and this object file from the archive includes
3055 int a = 5;
3056 In such a case we must include this object file.
3058 FIXME: The SunOS 4.1.3 linker will pull in the archive
3059 element if the symbol is defined in the .data section,
3060 but not if it is defined in the .text section. That
3061 seems a bit crazy to me, and I haven't implemented it.
3062 However, it might be correct. */
3063 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3064 return false;
3065 *pneeded = true;
3066 return true;
3069 if (type == (N_UNDF | N_EXT))
3071 bfd_vma value;
3073 value = GET_WORD (abfd, p->e_value);
3074 if (value != 0)
3076 /* This symbol is common in the object from the archive
3077 file. */
3078 if (h->type == bfd_link_hash_undefined)
3080 bfd *symbfd;
3081 unsigned int power;
3083 symbfd = h->u.undef.abfd;
3084 if (symbfd == (bfd *) NULL)
3086 /* This symbol was created as undefined from
3087 outside BFD. We assume that we should link
3088 in the object file. This is done for the -u
3089 option in the linker. */
3090 if (! (*info->callbacks->add_archive_element) (info,
3091 abfd,
3092 name))
3093 return false;
3094 *pneeded = true;
3095 return true;
3097 /* Turn the current link symbol into a common
3098 symbol. It is already on the undefs list. */
3099 h->type = bfd_link_hash_common;
3100 h->u.c.p = ((struct bfd_link_hash_common_entry *)
3101 bfd_hash_allocate (&info->hash->table,
3102 sizeof (struct bfd_link_hash_common_entry)));
3103 if (h->u.c.p == NULL)
3104 return false;
3106 h->u.c.size = value;
3108 /* FIXME: This isn't quite right. The maximum
3109 alignment of a common symbol should be set by the
3110 architecture of the output file, not of the input
3111 file. */
3112 power = bfd_log2 (value);
3113 if (power > bfd_get_arch_info (abfd)->section_align_power)
3114 power = bfd_get_arch_info (abfd)->section_align_power;
3115 h->u.c.p->alignment_power = power;
3117 h->u.c.p->section = bfd_make_section_old_way (symbfd,
3118 "COMMON");
3120 else
3122 /* Adjust the size of the common symbol if
3123 necessary. */
3124 if (value > h->u.c.size)
3125 h->u.c.size = value;
3131 /* We do not need this object file. */
3132 return true;
3135 /* Add all symbols from an object file to the hash table. */
3137 static boolean
3138 aout_link_add_symbols (abfd, info)
3139 bfd *abfd;
3140 struct bfd_link_info *info;
3142 boolean (*add_one_symbol) PARAMS ((struct bfd_link_info *, bfd *,
3143 const char *, flagword, asection *,
3144 bfd_vma, const char *, boolean,
3145 boolean,
3146 struct bfd_link_hash_entry **));
3147 struct external_nlist *syms;
3148 bfd_size_type sym_count;
3149 char *strings;
3150 boolean copy;
3151 struct aout_link_hash_entry **sym_hash;
3152 register struct external_nlist *p;
3153 struct external_nlist *pend;
3155 syms = obj_aout_external_syms (abfd);
3156 sym_count = obj_aout_external_sym_count (abfd);
3157 strings = obj_aout_external_strings (abfd);
3158 if (info->keep_memory)
3159 copy = false;
3160 else
3161 copy = true;
3163 if (aout_backend_info (abfd)->add_dynamic_symbols != NULL)
3165 if (! ((*aout_backend_info (abfd)->add_dynamic_symbols)
3166 (abfd, info, &syms, &sym_count, &strings)))
3167 return false;
3170 /* We keep a list of the linker hash table entries that correspond
3171 to particular symbols. We could just look them up in the hash
3172 table, but keeping the list is more efficient. Perhaps this
3173 should be conditional on info->keep_memory. */
3174 sym_hash = ((struct aout_link_hash_entry **)
3175 bfd_alloc (abfd,
3176 sym_count * sizeof (struct aout_link_hash_entry *)));
3177 if (sym_hash == NULL && sym_count != 0)
3178 return false;
3179 obj_aout_sym_hashes (abfd) = sym_hash;
3181 add_one_symbol = aout_backend_info (abfd)->add_one_symbol;
3182 if (add_one_symbol == NULL)
3183 add_one_symbol = _bfd_generic_link_add_one_symbol;
3185 p = syms;
3186 pend = p + sym_count;
3187 for (; p < pend; p++, sym_hash++)
3189 int type;
3190 const char *name;
3191 bfd_vma value;
3192 asection *section;
3193 flagword flags;
3194 const char *string;
3196 *sym_hash = NULL;
3198 type = H_GET_8 (abfd, p->e_type);
3200 #if 0 /* not supported in PDP-11 a.out */
3201 /* Ignore debugging symbols. */
3202 if ((type & N_STAB) != 0)
3203 continue;
3204 #endif
3206 name = strings + GET_WORD (abfd, p->e_strx);
3207 value = GET_WORD (abfd, p->e_value);
3208 flags = BSF_GLOBAL;
3209 string = NULL;
3210 switch (type)
3212 default:
3213 abort ();
3215 case N_UNDF:
3216 case N_ABS:
3217 case N_TEXT:
3218 case N_DATA:
3219 case N_BSS:
3220 case N_REG:
3221 case N_FN:
3222 /* Ignore symbols that are not externally visible. */
3223 continue;
3225 case N_UNDF | N_EXT:
3226 if (value == 0)
3228 section = bfd_und_section_ptr;
3229 flags = 0;
3231 else
3232 section = bfd_com_section_ptr;
3233 break;
3234 case N_ABS | N_EXT:
3235 section = bfd_abs_section_ptr;
3236 break;
3237 case N_TEXT | N_EXT:
3238 section = obj_textsec (abfd);
3239 value -= bfd_get_section_vma (abfd, section);
3240 break;
3241 case N_DATA | N_EXT:
3242 /* Treat N_SETV symbols as N_DATA symbol; see comment in
3243 translate_from_native_sym_flags. */
3244 section = obj_datasec (abfd);
3245 value -= bfd_get_section_vma (abfd, section);
3246 break;
3247 case N_BSS | N_EXT:
3248 section = obj_bsssec (abfd);
3249 value -= bfd_get_section_vma (abfd, section);
3250 break;
3253 if (! ((*add_one_symbol)
3254 (info, abfd, name, flags, section, value, string, copy, false,
3255 (struct bfd_link_hash_entry **) sym_hash)))
3256 return false;
3258 /* Restrict the maximum alignment of a common symbol based on
3259 the architecture, since a.out has no way to represent
3260 alignment requirements of a section in a .o file. FIXME:
3261 This isn't quite right: it should use the architecture of the
3262 output file, not the input files. */
3263 if ((*sym_hash)->root.type == bfd_link_hash_common
3264 && ((*sym_hash)->root.u.c.p->alignment_power >
3265 bfd_get_arch_info (abfd)->section_align_power))
3266 (*sym_hash)->root.u.c.p->alignment_power =
3267 bfd_get_arch_info (abfd)->section_align_power;
3269 /* If this is a set symbol, and we are not building sets, then
3270 it is possible for the hash entry to not have been set. In
3271 such a case, treat the symbol as not globally defined. */
3272 if ((*sym_hash)->root.type == bfd_link_hash_new)
3274 BFD_ASSERT ((flags & BSF_CONSTRUCTOR) != 0);
3275 *sym_hash = NULL;
3279 return true;
3282 /* A hash table used for header files with N_BINCL entries. */
3284 struct aout_link_includes_table
3286 struct bfd_hash_table root;
3289 /* A linked list of totals that we have found for a particular header
3290 file. */
3292 struct aout_link_includes_totals
3294 struct aout_link_includes_totals *next;
3295 bfd_vma total;
3298 /* An entry in the header file hash table. */
3300 struct aout_link_includes_entry
3302 struct bfd_hash_entry root;
3303 /* List of totals we have found for this file. */
3304 struct aout_link_includes_totals *totals;
3307 /* Look up an entry in an the header file hash table. */
3309 #define aout_link_includes_lookup(table, string, create, copy) \
3310 ((struct aout_link_includes_entry *) \
3311 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
3313 /* During the final link step we need to pass around a bunch of
3314 information, so we do it in an instance of this structure. */
3316 struct aout_final_link_info
3318 /* General link information. */
3319 struct bfd_link_info *info;
3320 /* Output bfd. */
3321 bfd *output_bfd;
3322 /* Reloc file positions. */
3323 file_ptr treloff, dreloff;
3324 /* File position of symbols. */
3325 file_ptr symoff;
3326 /* String table. */
3327 struct bfd_strtab_hash *strtab;
3328 /* Header file hash table. */
3329 struct aout_link_includes_table includes;
3330 /* A buffer large enough to hold the contents of any section. */
3331 bfd_byte *contents;
3332 /* A buffer large enough to hold the relocs of any section. */
3333 PTR relocs;
3334 /* A buffer large enough to hold the symbol map of any input BFD. */
3335 int *symbol_map;
3336 /* A buffer large enough to hold output symbols of any input BFD. */
3337 struct external_nlist *output_syms;
3340 static struct bfd_hash_entry *aout_link_includes_newfunc
3341 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
3342 static boolean aout_link_input_bfd
3343 PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
3344 static boolean aout_link_write_symbols
3345 PARAMS ((struct aout_final_link_info *, bfd *input_bfd));
3346 static boolean aout_link_write_other_symbol
3347 PARAMS ((struct aout_link_hash_entry *, PTR));
3348 static boolean aout_link_input_section
3349 PARAMS ((struct aout_final_link_info *, bfd *input_bfd,
3350 asection *input_section, file_ptr *reloff_ptr,
3351 bfd_size_type rel_size));
3352 static INLINE asection *aout_reloc_type_to_section
3353 PARAMS ((bfd *, int));
3354 static boolean aout_link_reloc_link_order
3355 PARAMS ((struct aout_final_link_info *, asection *,
3356 struct bfd_link_order *));
3357 static boolean pdp11_aout_link_input_section
3358 PARAMS ((struct aout_final_link_info *finfo,
3359 bfd *input_bfd,
3360 asection *input_section,
3361 struct pdp11_aout_reloc_external *relocs,
3362 bfd_size_type rel_size,
3363 bfd_byte *contents));
3365 /* The function to create a new entry in the header file hash table. */
3367 static struct bfd_hash_entry *
3368 aout_link_includes_newfunc (entry, table, string)
3369 struct bfd_hash_entry *entry;
3370 struct bfd_hash_table *table;
3371 const char *string;
3373 struct aout_link_includes_entry *ret =
3374 (struct aout_link_includes_entry *) entry;
3376 /* Allocate the structure if it has not already been allocated by a
3377 subclass. */
3378 if (ret == (struct aout_link_includes_entry *) NULL)
3379 ret = ((struct aout_link_includes_entry *)
3380 bfd_hash_allocate (table,
3381 sizeof (struct aout_link_includes_entry)));
3382 if (ret == (struct aout_link_includes_entry *) NULL)
3383 return (struct bfd_hash_entry *) ret;
3385 /* Call the allocation method of the superclass. */
3386 ret = ((struct aout_link_includes_entry *)
3387 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
3388 if (ret)
3390 /* Set local fields. */
3391 ret->totals = NULL;
3394 return (struct bfd_hash_entry *) ret;
3397 /* Do the final link step. This is called on the output BFD. The
3398 INFO structure should point to a list of BFDs linked through the
3399 link_next field which can be used to find each BFD which takes part
3400 in the output. Also, each section in ABFD should point to a list
3401 of bfd_link_order structures which list all the input sections for
3402 the output section. */
3404 boolean
3405 NAME(aout,final_link) (abfd, info, callback)
3406 bfd *abfd;
3407 struct bfd_link_info *info;
3408 void (*callback) PARAMS ((bfd *, file_ptr *, file_ptr *, file_ptr *));
3410 struct aout_final_link_info aout_info;
3411 boolean includes_hash_initialized = false;
3412 register bfd *sub;
3413 bfd_size_type trsize, drsize;
3414 bfd_size_type max_contents_size;
3415 bfd_size_type max_relocs_size;
3416 bfd_size_type max_sym_count;
3417 bfd_size_type text_size;
3418 file_ptr text_end;
3419 register struct bfd_link_order *p;
3420 asection *o;
3421 boolean have_link_order_relocs;
3423 if (info->shared)
3424 abfd->flags |= DYNAMIC;
3426 aout_info.info = info;
3427 aout_info.output_bfd = abfd;
3428 aout_info.contents = NULL;
3429 aout_info.relocs = NULL;
3430 aout_info.symbol_map = NULL;
3431 aout_info.output_syms = NULL;
3433 if (! bfd_hash_table_init_n (&aout_info.includes.root,
3434 aout_link_includes_newfunc,
3435 251))
3436 goto error_return;
3437 includes_hash_initialized = true;
3439 /* Figure out the largest section size. Also, if generating
3440 relocateable output, count the relocs. */
3441 trsize = 0;
3442 drsize = 0;
3443 max_contents_size = 0;
3444 max_relocs_size = 0;
3445 max_sym_count = 0;
3446 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next)
3448 size_t sz;
3450 if (info->relocateable)
3452 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
3454 trsize += exec_hdr (sub)->a_trsize;
3455 drsize += exec_hdr (sub)->a_drsize;
3457 else
3459 /* FIXME: We need to identify the .text and .data sections
3460 and call get_reloc_upper_bound and canonicalize_reloc to
3461 work out the number of relocs needed, and then multiply
3462 by the reloc size. */
3463 (*_bfd_error_handler)
3464 ("%s: relocateable link from %s to %s not supported",
3465 bfd_get_filename (abfd),
3466 sub->xvec->name, abfd->xvec->name);
3467 bfd_set_error (bfd_error_invalid_operation);
3468 goto error_return;
3472 if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
3474 sz = bfd_section_size (sub, obj_textsec (sub));
3475 if (sz > max_contents_size)
3476 max_contents_size = sz;
3477 sz = bfd_section_size (sub, obj_datasec (sub));
3478 if (sz > max_contents_size)
3479 max_contents_size = sz;
3481 sz = exec_hdr (sub)->a_trsize;
3482 if (sz > max_relocs_size)
3483 max_relocs_size = sz;
3484 sz = exec_hdr (sub)->a_drsize;
3485 if (sz > max_relocs_size)
3486 max_relocs_size = sz;
3488 sz = obj_aout_external_sym_count (sub);
3489 if (sz > max_sym_count)
3490 max_sym_count = sz;
3494 if (info->relocateable)
3496 if (obj_textsec (abfd) != (asection *) NULL)
3497 trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
3498 ->link_order_head)
3499 * obj_reloc_entry_size (abfd));
3500 if (obj_datasec (abfd) != (asection *) NULL)
3501 drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
3502 ->link_order_head)
3503 * obj_reloc_entry_size (abfd));
3506 exec_hdr (abfd)->a_trsize = trsize;
3507 exec_hdr (abfd)->a_drsize = drsize;
3509 exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
3511 /* Adjust the section sizes and vmas according to the magic number.
3512 This sets a_text, a_data and a_bss in the exec_hdr and sets the
3513 filepos for each section. */
3514 if (! NAME(aout,adjust_sizes_and_vmas) (abfd, &text_size, &text_end))
3515 goto error_return;
3517 /* The relocation and symbol file positions differ among a.out
3518 targets. We are passed a callback routine from the backend
3519 specific code to handle this.
3520 FIXME: At this point we do not know how much space the symbol
3521 table will require. This will not work for any (nonstandard)
3522 a.out target that needs to know the symbol table size before it
3523 can compute the relocation file positions. This may or may not
3524 be the case for the hp300hpux target, for example. */
3525 (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
3526 &aout_info.symoff);
3527 obj_textsec (abfd)->rel_filepos = aout_info.treloff;
3528 obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
3529 obj_sym_filepos (abfd) = aout_info.symoff;
3531 /* We keep a count of the symbols as we output them. */
3532 obj_aout_external_sym_count (abfd) = 0;
3534 /* We accumulate the string table as we write out the symbols. */
3535 aout_info.strtab = _bfd_stringtab_init ();
3536 if (aout_info.strtab == NULL)
3537 goto error_return;
3539 /* Allocate buffers to hold section contents and relocs. */
3540 aout_info.contents = (bfd_byte *) bfd_malloc (max_contents_size);
3541 aout_info.relocs = (PTR) bfd_malloc (max_relocs_size);
3542 aout_info.symbol_map = (int *) bfd_malloc (max_sym_count * sizeof (int *));
3543 aout_info.output_syms = ((struct external_nlist *)
3544 bfd_malloc ((max_sym_count + 1)
3545 * sizeof (struct external_nlist)));
3546 if ((aout_info.contents == NULL && max_contents_size != 0)
3547 || (aout_info.relocs == NULL && max_relocs_size != 0)
3548 || (aout_info.symbol_map == NULL && max_sym_count != 0)
3549 || aout_info.output_syms == NULL)
3550 goto error_return;
3552 /* If we have a symbol named __DYNAMIC, force it out now. This is
3553 required by SunOS. Doing this here rather than in sunos.c is a
3554 hack, but it's easier than exporting everything which would be
3555 needed. */
3557 struct aout_link_hash_entry *h;
3559 h = aout_link_hash_lookup (aout_hash_table (info), "__DYNAMIC",
3560 false, false, false);
3561 if (h != NULL)
3562 aout_link_write_other_symbol (h, &aout_info);
3565 /* The most time efficient way to do the link would be to read all
3566 the input object files into memory and then sort out the
3567 information into the output file. Unfortunately, that will
3568 probably use too much memory. Another method would be to step
3569 through everything that composes the text section and write it
3570 out, and then everything that composes the data section and write
3571 it out, and then write out the relocs, and then write out the
3572 symbols. Unfortunately, that requires reading stuff from each
3573 input file several times, and we will not be able to keep all the
3574 input files open simultaneously, and reopening them will be slow.
3576 What we do is basically process one input file at a time. We do
3577 everything we need to do with an input file once--copy over the
3578 section contents, handle the relocation information, and write
3579 out the symbols--and then we throw away the information we read
3580 from it. This approach requires a lot of lseeks of the output
3581 file, which is unfortunate but still faster than reopening a lot
3582 of files.
3584 We use the output_has_begun field of the input BFDs to see
3585 whether we have already handled it. */
3586 for (sub = info->input_bfds; sub != (bfd *) NULL; sub = sub->link_next)
3587 sub->output_has_begun = false;
3589 /* Mark all sections which are to be included in the link. This
3590 will normally be every section. We need to do this so that we
3591 can identify any sections which the linker has decided to not
3592 include. */
3593 for (o = abfd->sections; o != NULL; o = o->next)
3595 for (p = o->link_order_head; p != NULL; p = p->next)
3597 if (p->type == bfd_indirect_link_order)
3598 p->u.indirect.section->linker_mark = true;
3602 have_link_order_relocs = false;
3603 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3605 for (p = o->link_order_head;
3606 p != (struct bfd_link_order *) NULL;
3607 p = p->next)
3609 if (p->type == bfd_indirect_link_order
3610 && (bfd_get_flavour (p->u.indirect.section->owner)
3611 == bfd_target_aout_flavour))
3613 bfd *input_bfd;
3615 input_bfd = p->u.indirect.section->owner;
3616 if (! input_bfd->output_has_begun)
3618 if (! aout_link_input_bfd (&aout_info, input_bfd))
3619 goto error_return;
3620 input_bfd->output_has_begun = true;
3623 else if (p->type == bfd_section_reloc_link_order
3624 || p->type == bfd_symbol_reloc_link_order)
3626 /* These are handled below. */
3627 have_link_order_relocs = true;
3629 else
3631 if (! _bfd_default_link_order (abfd, info, o, p))
3632 goto error_return;
3637 /* Write out any symbols that we have not already written out. */
3638 aout_link_hash_traverse (aout_hash_table (info),
3639 aout_link_write_other_symbol,
3640 (PTR) &aout_info);
3642 /* Now handle any relocs we were asked to create by the linker.
3643 These did not come from any input file. We must do these after
3644 we have written out all the symbols, so that we know the symbol
3645 indices to use. */
3646 if (have_link_order_relocs)
3648 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3650 for (p = o->link_order_head;
3651 p != (struct bfd_link_order *) NULL;
3652 p = p->next)
3654 if (p->type == bfd_section_reloc_link_order
3655 || p->type == bfd_symbol_reloc_link_order)
3657 if (! aout_link_reloc_link_order (&aout_info, o, p))
3658 goto error_return;
3664 if (aout_info.contents != NULL)
3666 free (aout_info.contents);
3667 aout_info.contents = NULL;
3669 if (aout_info.relocs != NULL)
3671 free (aout_info.relocs);
3672 aout_info.relocs = NULL;
3674 if (aout_info.symbol_map != NULL)
3676 free (aout_info.symbol_map);
3677 aout_info.symbol_map = NULL;
3679 if (aout_info.output_syms != NULL)
3681 free (aout_info.output_syms);
3682 aout_info.output_syms = NULL;
3684 if (includes_hash_initialized)
3686 bfd_hash_table_free (&aout_info.includes.root);
3687 includes_hash_initialized = false;
3690 /* Finish up any dynamic linking we may be doing. */
3691 if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
3693 if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
3694 goto error_return;
3697 /* Update the header information. */
3698 abfd->symcount = obj_aout_external_sym_count (abfd);
3699 exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
3700 obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
3701 obj_textsec (abfd)->reloc_count =
3702 exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
3703 obj_datasec (abfd)->reloc_count =
3704 exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
3706 /* Write out the string table, unless there are no symbols. */
3707 if (abfd->symcount > 0)
3709 if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
3710 || ! emit_stringtab (abfd, aout_info.strtab))
3711 goto error_return;
3713 else if (obj_textsec (abfd)->reloc_count == 0
3714 && obj_datasec (abfd)->reloc_count == 0)
3716 bfd_byte b;
3718 b = 0;
3719 if (bfd_seek (abfd,
3720 (file_ptr) (obj_datasec (abfd)->filepos
3721 + exec_hdr (abfd)->a_data
3722 - 1),
3723 SEEK_SET) != 0
3724 || bfd_bwrite (&b, (bfd_size_type) 1, abfd) != 1)
3725 goto error_return;
3728 return true;
3730 error_return:
3731 if (aout_info.contents != NULL)
3732 free (aout_info.contents);
3733 if (aout_info.relocs != NULL)
3734 free (aout_info.relocs);
3735 if (aout_info.symbol_map != NULL)
3736 free (aout_info.symbol_map);
3737 if (aout_info.output_syms != NULL)
3738 free (aout_info.output_syms);
3739 if (includes_hash_initialized)
3740 bfd_hash_table_free (&aout_info.includes.root);
3741 return false;
3744 /* Link an a.out input BFD into the output file. */
3746 static boolean
3747 aout_link_input_bfd (finfo, input_bfd)
3748 struct aout_final_link_info *finfo;
3749 bfd *input_bfd;
3751 bfd_size_type sym_count;
3753 BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
3755 /* If this is a dynamic object, it may need special handling. */
3756 if ((input_bfd->flags & DYNAMIC) != 0
3757 && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
3759 return ((*aout_backend_info (input_bfd)->link_dynamic_object)
3760 (finfo->info, input_bfd));
3763 /* Get the symbols. We probably have them already, unless
3764 finfo->info->keep_memory is false. */
3765 if (! aout_get_external_symbols (input_bfd))
3766 return false;
3768 sym_count = obj_aout_external_sym_count (input_bfd);
3770 /* Write out the symbols and get a map of the new indices. The map
3771 is placed into finfo->symbol_map. */
3772 if (! aout_link_write_symbols (finfo, input_bfd))
3773 return false;
3775 /* Relocate and write out the sections. These functions use the
3776 symbol map created by aout_link_write_symbols. The linker_mark
3777 field will be set if these sections are to be included in the
3778 link, which will normally be the case. */
3779 if (obj_textsec (input_bfd)->linker_mark)
3781 if (! aout_link_input_section (finfo, input_bfd,
3782 obj_textsec (input_bfd),
3783 &finfo->treloff,
3784 exec_hdr (input_bfd)->a_trsize))
3785 return false;
3787 if (obj_datasec (input_bfd)->linker_mark)
3789 if (! aout_link_input_section (finfo, input_bfd,
3790 obj_datasec (input_bfd),
3791 &finfo->dreloff,
3792 exec_hdr (input_bfd)->a_drsize))
3793 return false;
3796 /* If we are not keeping memory, we don't need the symbols any
3797 longer. We still need them if we are keeping memory, because the
3798 strings in the hash table point into them. */
3799 if (! finfo->info->keep_memory)
3801 if (! aout_link_free_symbols (input_bfd))
3802 return false;
3805 return true;
3808 /* Adjust and write out the symbols for an a.out file. Set the new
3809 symbol indices into a symbol_map. */
3811 static boolean
3812 aout_link_write_symbols (finfo, input_bfd)
3813 struct aout_final_link_info *finfo;
3814 bfd *input_bfd;
3816 bfd *output_bfd;
3817 bfd_size_type sym_count;
3818 char *strings;
3819 enum bfd_link_strip strip;
3820 enum bfd_link_discard discard;
3821 struct external_nlist *outsym;
3822 bfd_size_type strtab_index;
3823 register struct external_nlist *sym;
3824 struct external_nlist *sym_end;
3825 struct aout_link_hash_entry **sym_hash;
3826 int *symbol_map;
3827 boolean pass;
3828 boolean skip_next;
3830 output_bfd = finfo->output_bfd;
3831 sym_count = obj_aout_external_sym_count (input_bfd);
3832 strings = obj_aout_external_strings (input_bfd);
3833 strip = finfo->info->strip;
3834 discard = finfo->info->discard;
3835 outsym = finfo->output_syms;
3837 /* First write out a symbol for this object file, unless we are
3838 discarding such symbols. */
3839 if (strip != strip_all
3840 && (strip != strip_some
3841 || bfd_hash_lookup (finfo->info->keep_hash, input_bfd->filename,
3842 false, false) != NULL)
3843 && discard != discard_all)
3845 H_PUT_8 (output_bfd, N_TEXT, outsym->e_type);
3846 strtab_index = add_to_stringtab (output_bfd, finfo->strtab,
3847 input_bfd->filename, false);
3848 if (strtab_index == (bfd_size_type) -1)
3849 return false;
3850 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
3851 PUT_WORD (output_bfd,
3852 (bfd_get_section_vma (output_bfd,
3853 obj_textsec (input_bfd)->output_section)
3854 + obj_textsec (input_bfd)->output_offset),
3855 outsym->e_value);
3856 ++obj_aout_external_sym_count (output_bfd);
3857 ++outsym;
3860 pass = false;
3861 skip_next = false;
3862 sym = obj_aout_external_syms (input_bfd);
3863 sym_end = sym + sym_count;
3864 sym_hash = obj_aout_sym_hashes (input_bfd);
3865 symbol_map = finfo->symbol_map;
3866 memset (symbol_map, 0, (size_t) sym_count * sizeof *symbol_map);
3867 for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
3869 const char *name;
3870 int type;
3871 struct aout_link_hash_entry *h;
3872 boolean skip;
3873 asection *symsec;
3874 bfd_vma val = 0;
3875 boolean copy;
3877 /* We set *symbol_map to 0 above for all symbols. If it has
3878 already been set to -1 for this symbol, it means that we are
3879 discarding it because it appears in a duplicate header file.
3880 See the N_BINCL code below. */
3881 if (*symbol_map == -1)
3882 continue;
3884 /* Initialize *symbol_map to -1, which means that the symbol was
3885 not copied into the output file. We will change it later if
3886 we do copy the symbol over. */
3887 *symbol_map = -1;
3889 type = H_GET_8 (input_bfd, sym->e_type);
3890 name = strings + GET_WORD (input_bfd, sym->e_strx);
3892 h = NULL;
3894 if (pass)
3896 /* Pass this symbol through. It is the target of an
3897 indirect or warning symbol. */
3898 val = GET_WORD (input_bfd, sym->e_value);
3899 pass = false;
3901 else if (skip_next)
3903 /* Skip this symbol, which is the target of an indirect
3904 symbol that we have changed to no longer be an indirect
3905 symbol. */
3906 skip_next = false;
3907 continue;
3909 else
3911 struct aout_link_hash_entry *hresolve;
3913 /* We have saved the hash table entry for this symbol, if
3914 there is one. Note that we could just look it up again
3915 in the hash table, provided we first check that it is an
3916 external symbol. */
3917 h = *sym_hash;
3919 /* Use the name from the hash table, in case the symbol was
3920 wrapped. */
3921 if (h != NULL)
3922 name = h->root.root.string;
3924 /* If this is an indirect or warning symbol, then change
3925 hresolve to the base symbol. We also change *sym_hash so
3926 that the relocation routines relocate against the real
3927 symbol. */
3928 hresolve = h;
3929 if (h != (struct aout_link_hash_entry *) NULL
3930 && (h->root.type == bfd_link_hash_indirect
3931 || h->root.type == bfd_link_hash_warning))
3933 hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
3934 while (hresolve->root.type == bfd_link_hash_indirect
3935 || hresolve->root.type == bfd_link_hash_warning)
3936 hresolve = ((struct aout_link_hash_entry *)
3937 hresolve->root.u.i.link);
3938 *sym_hash = hresolve;
3941 /* If the symbol has already been written out, skip it. */
3942 if (h != (struct aout_link_hash_entry *) NULL
3943 && h->root.type != bfd_link_hash_warning
3944 && h->written)
3946 if ((type & N_TYPE) == N_INDR
3947 || type == N_WARNING)
3948 skip_next = true;
3949 *symbol_map = h->indx;
3950 continue;
3953 /* See if we are stripping this symbol. */
3954 skip = false;
3955 switch (strip)
3957 case strip_none:
3958 break;
3959 case strip_debugger:
3960 if ((type & N_STAB) != 0)
3961 skip = true;
3962 break;
3963 case strip_some:
3964 if (bfd_hash_lookup (finfo->info->keep_hash, name, false, false)
3965 == NULL)
3966 skip = true;
3967 break;
3968 case strip_all:
3969 skip = true;
3970 break;
3972 if (skip)
3974 if (h != (struct aout_link_hash_entry *) NULL)
3975 h->written = true;
3976 continue;
3979 /* Get the value of the symbol. */
3980 if ((type & N_TYPE) == N_TEXT
3981 || type == N_WEAKT)
3982 symsec = obj_textsec (input_bfd);
3983 else if ((type & N_TYPE) == N_DATA
3984 || type == N_WEAKD)
3985 symsec = obj_datasec (input_bfd);
3986 else if ((type & N_TYPE) == N_BSS
3987 || type == N_WEAKB)
3988 symsec = obj_bsssec (input_bfd);
3989 else if ((type & N_TYPE) == N_ABS
3990 || type == N_WEAKA)
3991 symsec = bfd_abs_section_ptr;
3992 else if (((type & N_TYPE) == N_INDR
3993 && (hresolve == (struct aout_link_hash_entry *) NULL
3994 || (hresolve->root.type != bfd_link_hash_defined
3995 && hresolve->root.type != bfd_link_hash_defweak
3996 && hresolve->root.type != bfd_link_hash_common)))
3997 || type == N_WARNING)
3999 /* Pass the next symbol through unchanged. The
4000 condition above for indirect symbols is so that if
4001 the indirect symbol was defined, we output it with
4002 the correct definition so the debugger will
4003 understand it. */
4004 pass = true;
4005 val = GET_WORD (input_bfd, sym->e_value);
4006 symsec = NULL;
4008 else if ((type & N_STAB) != 0)
4010 val = GET_WORD (input_bfd, sym->e_value);
4011 symsec = NULL;
4013 else
4015 /* If we get here with an indirect symbol, it means that
4016 we are outputting it with a real definition. In such
4017 a case we do not want to output the next symbol,
4018 which is the target of the indirection. */
4019 if ((type & N_TYPE) == N_INDR)
4020 skip_next = true;
4022 symsec = NULL;
4024 /* We need to get the value from the hash table. We use
4025 hresolve so that if we have defined an indirect
4026 symbol we output the final definition. */
4027 if (h == (struct aout_link_hash_entry *) NULL)
4029 switch (type & N_TYPE)
4031 case N_SETT:
4032 symsec = obj_textsec (input_bfd);
4033 break;
4034 case N_SETD:
4035 symsec = obj_datasec (input_bfd);
4036 break;
4037 case N_SETB:
4038 symsec = obj_bsssec (input_bfd);
4039 break;
4040 case N_SETA:
4041 symsec = bfd_abs_section_ptr;
4042 break;
4043 default:
4044 val = 0;
4045 break;
4048 else if (hresolve->root.type == bfd_link_hash_defined
4049 || hresolve->root.type == bfd_link_hash_defweak)
4051 asection *input_section;
4052 asection *output_section;
4054 /* This case usually means a common symbol which was
4055 turned into a defined symbol. */
4056 input_section = hresolve->root.u.def.section;
4057 output_section = input_section->output_section;
4058 BFD_ASSERT (bfd_is_abs_section (output_section)
4059 || output_section->owner == output_bfd);
4060 val = (hresolve->root.u.def.value
4061 + bfd_get_section_vma (output_bfd, output_section)
4062 + input_section->output_offset);
4064 /* Get the correct type based on the section. If
4065 this is a constructed set, force it to be
4066 globally visible. */
4067 if (type == N_SETT
4068 || type == N_SETD
4069 || type == N_SETB
4070 || type == N_SETA)
4071 type |= N_EXT;
4073 type &=~ N_TYPE;
4075 if (output_section == obj_textsec (output_bfd))
4076 type |= (hresolve->root.type == bfd_link_hash_defined
4077 ? N_TEXT
4078 : N_WEAKT);
4079 else if (output_section == obj_datasec (output_bfd))
4080 type |= (hresolve->root.type == bfd_link_hash_defined
4081 ? N_DATA
4082 : N_WEAKD);
4083 else if (output_section == obj_bsssec (output_bfd))
4084 type |= (hresolve->root.type == bfd_link_hash_defined
4085 ? N_BSS
4086 : N_WEAKB);
4087 else
4088 type |= (hresolve->root.type == bfd_link_hash_defined
4089 ? N_ABS
4090 : N_WEAKA);
4092 else if (hresolve->root.type == bfd_link_hash_common)
4093 val = hresolve->root.u.c.size;
4094 else if (hresolve->root.type == bfd_link_hash_undefweak)
4096 val = 0;
4097 type = N_WEAKU;
4099 else
4100 val = 0;
4102 if (symsec != (asection *) NULL)
4103 val = (symsec->output_section->vma
4104 + symsec->output_offset
4105 + (GET_WORD (input_bfd, sym->e_value)
4106 - symsec->vma));
4108 /* If this is a global symbol set the written flag, and if
4109 it is a local symbol see if we should discard it. */
4110 if (h != (struct aout_link_hash_entry *) NULL)
4112 h->written = true;
4113 h->indx = obj_aout_external_sym_count (output_bfd);
4115 else if ((type & N_TYPE) != N_SETT
4116 && (type & N_TYPE) != N_SETD
4117 && (type & N_TYPE) != N_SETB
4118 && (type & N_TYPE) != N_SETA)
4120 switch (discard)
4122 case discard_none:
4123 case discard_sec_merge:
4124 break;
4125 case discard_l:
4126 if ((type & N_STAB) == 0
4127 && bfd_is_local_label_name (input_bfd, name))
4128 skip = true;
4129 break;
4130 case discard_all:
4131 skip = true;
4132 break;
4134 if (skip)
4136 pass = false;
4137 continue;
4141 /* An N_BINCL symbol indicates the start of the stabs
4142 entries for a header file. We need to scan ahead to the
4143 next N_EINCL symbol, ignoring nesting, adding up all the
4144 characters in the symbol names, not including the file
4145 numbers in types (the first number after an open
4146 parenthesis). */
4147 if (type == N_BINCL)
4149 struct external_nlist *incl_sym;
4150 int nest;
4151 struct aout_link_includes_entry *incl_entry;
4152 struct aout_link_includes_totals *t;
4154 val = 0;
4155 nest = 0;
4156 for (incl_sym = sym + 1; incl_sym < sym_end; incl_sym++)
4158 int incl_type;
4160 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
4161 if (incl_type == N_EINCL)
4163 if (nest == 0)
4164 break;
4165 --nest;
4167 else if (incl_type == N_BINCL)
4168 ++nest;
4169 else if (nest == 0)
4171 const char *s;
4173 s = strings + GET_WORD (input_bfd, incl_sym->e_strx);
4174 for (; *s != '\0'; s++)
4176 val += *s;
4177 if (*s == '(')
4179 /* Skip the file number. */
4180 ++s;
4181 while (ISDIGIT (*s))
4182 ++s;
4183 --s;
4189 /* If we have already included a header file with the
4190 same value, then replace this one with an N_EXCL
4191 symbol. */
4192 copy = ! finfo->info->keep_memory;
4193 incl_entry = aout_link_includes_lookup (&finfo->includes,
4194 name, true, copy);
4195 if (incl_entry == NULL)
4196 return false;
4197 for (t = incl_entry->totals; t != NULL; t = t->next)
4198 if (t->total == val)
4199 break;
4200 if (t == NULL)
4202 /* This is the first time we have seen this header
4203 file with this set of stabs strings. */
4204 t = ((struct aout_link_includes_totals *)
4205 bfd_hash_allocate (&finfo->includes.root,
4206 sizeof *t));
4207 if (t == NULL)
4208 return false;
4209 t->total = val;
4210 t->next = incl_entry->totals;
4211 incl_entry->totals = t;
4213 else
4215 int *incl_map;
4217 /* This is a duplicate header file. We must change
4218 it to be an N_EXCL entry, and mark all the
4219 included symbols to prevent outputting them. */
4220 type = N_EXCL;
4222 nest = 0;
4223 for (incl_sym = sym + 1, incl_map = symbol_map + 1;
4224 incl_sym < sym_end;
4225 incl_sym++, incl_map++)
4227 int incl_type;
4229 incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
4230 if (incl_type == N_EINCL)
4232 if (nest == 0)
4234 *incl_map = -1;
4235 break;
4237 --nest;
4239 else if (incl_type == N_BINCL)
4240 ++nest;
4241 else if (nest == 0)
4242 *incl_map = -1;
4248 /* Copy this symbol into the list of symbols we are going to
4249 write out. */
4250 H_PUT_8 (output_bfd, type, outsym->e_type);
4251 copy = false;
4252 if (! finfo->info->keep_memory)
4254 /* name points into a string table which we are going to
4255 free. If there is a hash table entry, use that string.
4256 Otherwise, copy name into memory. */
4257 if (h != (struct aout_link_hash_entry *) NULL)
4258 name = h->root.root.string;
4259 else
4260 copy = true;
4262 strtab_index = add_to_stringtab (output_bfd, finfo->strtab,
4263 name, copy);
4264 if (strtab_index == (bfd_size_type) -1)
4265 return false;
4266 PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
4267 PUT_WORD (output_bfd, val, outsym->e_value);
4268 *symbol_map = obj_aout_external_sym_count (output_bfd);
4269 ++obj_aout_external_sym_count (output_bfd);
4270 ++outsym;
4273 /* Write out the output symbols we have just constructed. */
4274 if (outsym > finfo->output_syms)
4276 bfd_size_type size;
4278 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0)
4279 return false;
4280 size = outsym - finfo->output_syms;
4281 size *= EXTERNAL_NLIST_SIZE;
4282 if (bfd_bwrite ((PTR) finfo->output_syms, size, output_bfd) != size)
4283 return false;
4284 finfo->symoff += size;
4287 return true;
4290 /* Write out a symbol that was not associated with an a.out input
4291 object. */
4293 static boolean
4294 aout_link_write_other_symbol (h, data)
4295 struct aout_link_hash_entry *h;
4296 PTR data;
4298 struct aout_final_link_info *finfo = (struct aout_final_link_info *) data;
4299 bfd *output_bfd;
4300 int type;
4301 bfd_vma val;
4302 struct external_nlist outsym;
4303 bfd_size_type indx;
4304 bfd_size_type amt;
4306 if (h->root.type == bfd_link_hash_warning)
4308 h = (struct aout_link_hash_entry *) h->root.u.i.link;
4309 if (h->root.type == bfd_link_hash_new)
4310 return true;
4313 output_bfd = finfo->output_bfd;
4315 if (aout_backend_info (output_bfd)->write_dynamic_symbol != NULL)
4317 if (! ((*aout_backend_info (output_bfd)->write_dynamic_symbol)
4318 (output_bfd, finfo->info, h)))
4320 /* FIXME: No way to handle errors. */
4321 abort ();
4325 if (h->written)
4326 return true;
4328 h->written = true;
4330 /* An indx of -2 means the symbol must be written. */
4331 if (h->indx != -2
4332 && (finfo->info->strip == strip_all
4333 || (finfo->info->strip == strip_some
4334 && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string,
4335 false, false) == NULL)))
4336 return true;
4338 switch (h->root.type)
4340 default:
4341 abort ();
4342 /* Avoid variable not initialized warnings. */
4343 return true;
4344 case bfd_link_hash_new:
4345 /* This can happen for set symbols when sets are not being
4346 built. */
4347 return true;
4348 case bfd_link_hash_undefined:
4349 type = N_UNDF | N_EXT;
4350 val = 0;
4351 break;
4352 case bfd_link_hash_defined:
4353 case bfd_link_hash_defweak:
4355 asection *sec;
4357 sec = h->root.u.def.section->output_section;
4358 BFD_ASSERT (bfd_is_abs_section (sec)
4359 || sec->owner == output_bfd);
4360 if (sec == obj_textsec (output_bfd))
4361 type = h->root.type == bfd_link_hash_defined ? N_TEXT : N_WEAKT;
4362 else if (sec == obj_datasec (output_bfd))
4363 type = h->root.type == bfd_link_hash_defined ? N_DATA : N_WEAKD;
4364 else if (sec == obj_bsssec (output_bfd))
4365 type = h->root.type == bfd_link_hash_defined ? N_BSS : N_WEAKB;
4366 else
4367 type = h->root.type == bfd_link_hash_defined ? N_ABS : N_WEAKA;
4368 type |= N_EXT;
4369 val = (h->root.u.def.value
4370 + sec->vma
4371 + h->root.u.def.section->output_offset);
4373 break;
4374 case bfd_link_hash_common:
4375 type = N_UNDF | N_EXT;
4376 val = h->root.u.c.size;
4377 break;
4378 case bfd_link_hash_undefweak:
4379 type = N_WEAKU;
4380 val = 0;
4381 case bfd_link_hash_indirect:
4382 case bfd_link_hash_warning:
4383 /* FIXME: Ignore these for now. The circumstances under which
4384 they should be written out are not clear to me. */
4385 return true;
4388 H_PUT_8 (output_bfd, type, outsym.e_type);
4389 indx = add_to_stringtab (output_bfd, finfo->strtab, h->root.root.string,
4390 false);
4391 if (indx == (bfd_size_type) -1)
4393 /* FIXME: No way to handle errors. */
4394 abort ();
4396 PUT_WORD (output_bfd, indx, outsym.e_strx);
4397 PUT_WORD (output_bfd, val, outsym.e_value);
4399 amt = EXTERNAL_NLIST_SIZE;
4400 if (bfd_seek (output_bfd, finfo->symoff, SEEK_SET) != 0
4401 || bfd_bwrite ((PTR) &outsym, amt, output_bfd) != amt)
4403 /* FIXME: No way to handle errors. */
4404 abort ();
4407 finfo->symoff += amt;
4408 h->indx = obj_aout_external_sym_count (output_bfd);
4409 ++obj_aout_external_sym_count (output_bfd);
4411 return true;
4414 /* Link an a.out section into the output file. */
4416 static boolean
4417 aout_link_input_section (finfo, input_bfd, input_section, reloff_ptr,
4418 rel_size)
4419 struct aout_final_link_info *finfo;
4420 bfd *input_bfd;
4421 asection *input_section;
4422 file_ptr *reloff_ptr;
4423 bfd_size_type rel_size;
4425 bfd_size_type input_size;
4426 PTR relocs;
4428 /* Get the section contents. */
4429 input_size = bfd_section_size (input_bfd, input_section);
4430 if (! bfd_get_section_contents (input_bfd, input_section,
4431 (PTR) finfo->contents,
4432 (file_ptr) 0, input_size))
4433 return false;
4435 /* Read in the relocs if we haven't already done it. */
4436 if (aout_section_data (input_section) != NULL
4437 && aout_section_data (input_section)->relocs != NULL)
4438 relocs = aout_section_data (input_section)->relocs;
4439 else
4441 relocs = finfo->relocs;
4442 if (rel_size > 0)
4444 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4445 || bfd_bread (relocs, rel_size, input_bfd) != rel_size)
4446 return false;
4450 /* Relocate the section contents. */
4451 if (! pdp11_aout_link_input_section (finfo, input_bfd, input_section,
4452 (struct pdp11_aout_reloc_external *) relocs,
4453 rel_size, finfo->contents))
4454 return false;
4456 /* Write out the section contents. */
4457 if (! bfd_set_section_contents (finfo->output_bfd,
4458 input_section->output_section,
4459 (PTR) finfo->contents,
4460 (file_ptr) input_section->output_offset,
4461 input_size))
4462 return false;
4464 /* If we are producing relocateable output, the relocs were
4465 modified, and we now write them out. */
4466 if (finfo->info->relocateable && rel_size > 0)
4468 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
4469 return false;
4470 if (bfd_bwrite (relocs, rel_size, finfo->output_bfd) != rel_size)
4471 return false;
4472 *reloff_ptr += rel_size;
4474 /* Assert that the relocs have not run into the symbols, and
4475 that if these are the text relocs they have not run into the
4476 data relocs. */
4477 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
4478 && (reloff_ptr != &finfo->treloff
4479 || (*reloff_ptr
4480 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
4483 return true;
4486 /* Get the section corresponding to a reloc index. */
4488 static INLINE asection *
4489 aout_reloc_type_to_section (abfd, type)
4490 bfd *abfd;
4491 int type;
4493 switch (type)
4495 case RTEXT:
4496 return obj_textsec (abfd);
4497 case RDATA:
4498 return obj_datasec (abfd);
4499 case RBSS:
4500 return obj_bsssec (abfd);
4501 case RABS:
4502 return bfd_abs_section_ptr;
4503 case REXT:
4504 return bfd_und_section_ptr;
4505 default:
4506 abort ();
4510 static boolean
4511 pdp11_aout_link_input_section (finfo, input_bfd, input_section, relocs,
4512 rel_size, contents)
4513 struct aout_final_link_info *finfo;
4514 bfd *input_bfd;
4515 asection *input_section;
4516 struct pdp11_aout_reloc_external *relocs;
4517 bfd_size_type rel_size;
4518 bfd_byte *contents;
4520 boolean (*check_dynamic_reloc) PARAMS ((struct bfd_link_info *,
4521 bfd *, asection *,
4522 struct aout_link_hash_entry *,
4523 PTR, bfd_byte *, boolean *,
4524 bfd_vma *));
4525 bfd *output_bfd;
4526 boolean relocateable;
4527 struct external_nlist *syms;
4528 char *strings;
4529 struct aout_link_hash_entry **sym_hashes;
4530 int *symbol_map;
4531 bfd_size_type reloc_count;
4532 register struct pdp11_aout_reloc_external *rel;
4533 struct pdp11_aout_reloc_external *rel_end;
4535 output_bfd = finfo->output_bfd;
4536 check_dynamic_reloc = aout_backend_info (output_bfd)->check_dynamic_reloc;
4538 BFD_ASSERT (obj_reloc_entry_size (input_bfd) == RELOC_SIZE);
4539 BFD_ASSERT (input_bfd->xvec->header_byteorder
4540 == output_bfd->xvec->header_byteorder);
4542 relocateable = finfo->info->relocateable;
4543 syms = obj_aout_external_syms (input_bfd);
4544 strings = obj_aout_external_strings (input_bfd);
4545 sym_hashes = obj_aout_sym_hashes (input_bfd);
4546 symbol_map = finfo->symbol_map;
4548 reloc_count = rel_size / RELOC_SIZE;
4549 rel = relocs;
4550 rel_end = (struct pdp11_aout_reloc_external *)(((char *)rel) + rel_size);
4551 for (; rel < rel_end; ((char *)rel) += RELOC_SIZE)
4553 bfd_vma r_addr;
4554 int r_index;
4555 int r_type;
4556 int r_pcrel;
4557 int r_extern;
4558 reloc_howto_type *howto;
4559 struct aout_link_hash_entry *h = NULL;
4560 bfd_vma relocation;
4561 bfd_reloc_status_type r;
4562 int reloc_entry;
4564 reloc_entry = GET_WORD (input_bfd, (PTR)rel);
4565 if (reloc_entry == 0)
4566 continue;
4569 unsigned int howto_idx;
4571 r_index = (reloc_entry & RIDXMASK) >> 4;
4572 r_type = reloc_entry & RTYPE;
4573 r_pcrel = reloc_entry & RELFLG;
4574 r_addr = (char *)rel - (char *)relocs;
4576 r_extern = (r_type == REXT);
4578 howto_idx = r_pcrel;
4579 BFD_ASSERT (howto_idx < TABLE_SIZE (howto_table_pdp11));
4580 howto = howto_table_pdp11 + howto_idx;
4583 if (relocateable)
4585 /* We are generating a relocateable output file, and must
4586 modify the reloc accordingly. */
4587 if (r_extern)
4589 /* If we know the symbol this relocation is against,
4590 convert it into a relocation against a section. This
4591 is what the native linker does. */
4592 h = sym_hashes[r_index];
4593 if (h != (struct aout_link_hash_entry *) NULL
4594 && (h->root.type == bfd_link_hash_defined
4595 || h->root.type == bfd_link_hash_defweak))
4597 asection *output_section;
4599 /* Compute a new r_index. */
4600 output_section = h->root.u.def.section->output_section;
4601 if (output_section == obj_textsec (output_bfd))
4602 r_type = N_TEXT;
4603 else if (output_section == obj_datasec (output_bfd))
4604 r_type = N_DATA;
4605 else if (output_section == obj_bsssec (output_bfd))
4606 r_type = N_BSS;
4607 else
4608 r_type = N_ABS;
4610 /* Add the symbol value and the section VMA to the
4611 addend stored in the contents. */
4612 relocation = (h->root.u.def.value
4613 + output_section->vma
4614 + h->root.u.def.section->output_offset);
4616 else
4618 /* We must change r_index according to the symbol
4619 map. */
4620 r_index = symbol_map[r_index];
4622 if (r_index == -1)
4624 if (h != NULL)
4626 /* We decided to strip this symbol, but it
4627 turns out that we can't. Note that we
4628 lose the other and desc information here.
4629 I don't think that will ever matter for a
4630 global symbol. */
4631 if (h->indx < 0)
4633 h->indx = -2;
4634 h->written = false;
4635 if (! aout_link_write_other_symbol (h,
4636 (PTR) finfo))
4637 return false;
4639 r_index = h->indx;
4641 else
4643 const char *name;
4645 name = strings + GET_WORD (input_bfd,
4646 syms[r_index].e_strx);
4647 if (! ((*finfo->info->callbacks->unattached_reloc)
4648 (finfo->info, name, input_bfd, input_section,
4649 r_addr)))
4650 return false;
4651 r_index = 0;
4655 relocation = 0;
4658 /* Write out the new r_index value. */
4659 reloc_entry = GET_WORD (input_bfd, rel->e_reloc_entry);
4660 reloc_entry &= RIDXMASK;
4661 reloc_entry |= r_index << 4;
4662 PUT_WORD (input_bfd, reloc_entry, rel->e_reloc_entry);
4664 else
4666 asection *section;
4668 /* This is a relocation against a section. We must
4669 adjust by the amount that the section moved. */
4670 section = aout_reloc_type_to_section (input_bfd, r_type);
4671 relocation = (section->output_section->vma
4672 + section->output_offset
4673 - section->vma);
4676 /* Change the address of the relocation. */
4677 #if 0
4678 PUT_WORD (output_bfd,
4679 r_addr + input_section->output_offset,
4680 rel->r_address);
4681 #else
4682 fprintf (stderr, "TODO: change the address of the relocation\n");
4683 #endif
4685 /* Adjust a PC relative relocation by removing the reference
4686 to the original address in the section and including the
4687 reference to the new address. */
4688 if (r_pcrel)
4689 relocation -= (input_section->output_section->vma
4690 + input_section->output_offset
4691 - input_section->vma);
4693 #ifdef MY_relocatable_reloc
4694 MY_relocatable_reloc (howto, output_bfd, rel, relocation, r_addr);
4695 #endif
4697 if (relocation == 0)
4698 r = bfd_reloc_ok;
4699 else
4700 r = MY_relocate_contents (howto,
4701 input_bfd, relocation,
4702 contents + r_addr);
4704 else
4706 boolean hundef;
4708 /* We are generating an executable, and must do a full
4709 relocation. */
4710 hundef = false;
4711 if (r_extern)
4713 h = sym_hashes[r_index];
4715 if (h != (struct aout_link_hash_entry *) NULL
4716 && (h->root.type == bfd_link_hash_defined
4717 || h->root.type == bfd_link_hash_defweak))
4719 relocation = (h->root.u.def.value
4720 + h->root.u.def.section->output_section->vma
4721 + h->root.u.def.section->output_offset);
4723 else if (h != (struct aout_link_hash_entry *) NULL
4724 && h->root.type == bfd_link_hash_undefweak)
4725 relocation = 0;
4726 else
4728 hundef = true;
4729 relocation = 0;
4732 else
4734 asection *section;
4736 section = aout_reloc_type_to_section (input_bfd, r_type);
4737 relocation = (section->output_section->vma
4738 + section->output_offset
4739 - section->vma);
4740 if (r_pcrel)
4741 relocation += input_section->vma;
4744 if (check_dynamic_reloc != NULL)
4746 boolean skip;
4748 if (! ((*check_dynamic_reloc)
4749 (finfo->info, input_bfd, input_section, h,
4750 (PTR) rel, contents, &skip, &relocation)))
4751 return false;
4752 if (skip)
4753 continue;
4756 /* Now warn if a global symbol is undefined. We could not
4757 do this earlier, because check_dynamic_reloc might want
4758 to skip this reloc. */
4759 if (hundef && ! finfo->info->shared)
4761 const char *name;
4763 if (h != NULL)
4764 name = h->root.root.string;
4765 else
4766 name = strings + GET_WORD (input_bfd, syms[r_index].e_strx);
4767 if (! ((*finfo->info->callbacks->undefined_symbol)
4768 (finfo->info, name, input_bfd, input_section,
4769 r_addr, true)))
4770 return false;
4773 r = MY_final_link_relocate (howto,
4774 input_bfd, input_section,
4775 contents, r_addr, relocation,
4776 (bfd_vma) 0);
4779 if (r != bfd_reloc_ok)
4781 switch (r)
4783 default:
4784 case bfd_reloc_outofrange:
4785 abort ();
4786 case bfd_reloc_overflow:
4788 const char *name;
4790 if (h != NULL)
4791 name = h->root.root.string;
4792 else if (r_extern)
4793 name = strings + GET_WORD (input_bfd,
4794 syms[r_index].e_strx);
4795 else
4797 asection *s;
4799 s = aout_reloc_type_to_section (input_bfd, r_type);
4800 name = bfd_section_name (input_bfd, s);
4802 if (! ((*finfo->info->callbacks->reloc_overflow)
4803 (finfo->info, name, howto->name,
4804 (bfd_vma) 0, input_bfd, input_section, r_addr)))
4805 return false;
4807 break;
4812 return true;
4815 /* Handle a link order which is supposed to generate a reloc. */
4817 static boolean
4818 aout_link_reloc_link_order (finfo, o, p)
4819 struct aout_final_link_info *finfo;
4820 asection *o;
4821 struct bfd_link_order *p;
4823 struct bfd_link_order_reloc *pr;
4824 int r_index;
4825 int r_extern;
4826 reloc_howto_type *howto;
4827 file_ptr *reloff_ptr;
4828 struct reloc_std_external srel;
4829 PTR rel_ptr;
4830 bfd_size_type rel_size;
4832 pr = p->u.reloc.p;
4834 if (p->type == bfd_section_reloc_link_order)
4836 r_extern = 0;
4837 if (bfd_is_abs_section (pr->u.section))
4838 r_index = N_ABS | N_EXT;
4839 else
4841 BFD_ASSERT (pr->u.section->owner == finfo->output_bfd);
4842 r_index = pr->u.section->target_index;
4845 else
4847 struct aout_link_hash_entry *h;
4849 BFD_ASSERT (p->type == bfd_symbol_reloc_link_order);
4850 r_extern = 1;
4851 h = ((struct aout_link_hash_entry *)
4852 bfd_wrapped_link_hash_lookup (finfo->output_bfd, finfo->info,
4853 pr->u.name, false, false, true));
4854 if (h != (struct aout_link_hash_entry *) NULL
4855 && h->indx >= 0)
4856 r_index = h->indx;
4857 else if (h != NULL)
4859 /* We decided to strip this symbol, but it turns out that we
4860 can't. Note that we lose the other and desc information
4861 here. I don't think that will ever matter for a global
4862 symbol. */
4863 h->indx = -2;
4864 h->written = false;
4865 if (! aout_link_write_other_symbol (h, (PTR) finfo))
4866 return false;
4867 r_index = h->indx;
4869 else
4871 if (! ((*finfo->info->callbacks->unattached_reloc)
4872 (finfo->info, pr->u.name, (bfd *) NULL,
4873 (asection *) NULL, (bfd_vma) 0)))
4874 return false;
4875 r_index = 0;
4879 howto = bfd_reloc_type_lookup (finfo->output_bfd, pr->reloc);
4880 if (howto == 0)
4882 bfd_set_error (bfd_error_bad_value);
4883 return false;
4886 if (o == obj_textsec (finfo->output_bfd))
4887 reloff_ptr = &finfo->treloff;
4888 else if (o == obj_datasec (finfo->output_bfd))
4889 reloff_ptr = &finfo->dreloff;
4890 else
4891 abort ();
4893 #ifdef MY_put_reloc
4894 MY_put_reloc(finfo->output_bfd, r_extern, r_index, p->offset, howto,
4895 &srel);
4896 #else
4898 int r_pcrel;
4899 int r_baserel;
4900 int r_jmptable;
4901 int r_relative;
4902 int r_length;
4904 fprintf (stderr, "TODO: line %d in bfd/pdp11.c\n", __LINE__);
4906 r_pcrel = howto->pc_relative;
4907 r_baserel = (howto->type & 8) != 0;
4908 r_jmptable = (howto->type & 16) != 0;
4909 r_relative = (howto->type & 32) != 0;
4910 r_length = howto->size;
4912 PUT_WORD (finfo->output_bfd, p->offset, srel.r_address);
4913 if (bfd_header_big_endian (finfo->output_bfd))
4915 srel.r_index[0] = r_index >> 16;
4916 srel.r_index[1] = r_index >> 8;
4917 srel.r_index[2] = r_index;
4918 srel.r_type[0] =
4919 ((r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
4920 | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
4921 | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
4922 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
4923 | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
4924 | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
4926 else
4928 srel.r_index[2] = r_index >> 16;
4929 srel.r_index[1] = r_index >> 8;
4930 srel.r_index[0] = r_index;
4931 srel.r_type[0] =
4932 ((r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
4933 | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
4934 | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
4935 | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
4936 | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
4937 | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
4940 #endif
4941 rel_ptr = (PTR) &srel;
4943 /* We have to write the addend into the object file, since
4944 standard a.out relocs are in place. It would be more
4945 reliable if we had the current contents of the file here,
4946 rather than assuming zeroes, but we can't read the file since
4947 it was opened using bfd_openw. */
4948 if (pr->addend != 0)
4950 bfd_size_type size;
4951 bfd_reloc_status_type r;
4952 bfd_byte *buf;
4953 boolean ok;
4955 size = bfd_get_reloc_size (howto);
4956 buf = (bfd_byte *) bfd_zmalloc (size);
4957 if (buf == (bfd_byte *) NULL)
4958 return false;
4959 r = MY_relocate_contents (howto, finfo->output_bfd,
4960 pr->addend, buf);
4961 switch (r)
4963 case bfd_reloc_ok:
4964 break;
4965 default:
4966 case bfd_reloc_outofrange:
4967 abort ();
4968 case bfd_reloc_overflow:
4969 if (! ((*finfo->info->callbacks->reloc_overflow)
4970 (finfo->info,
4971 (p->type == bfd_section_reloc_link_order
4972 ? bfd_section_name (finfo->output_bfd,
4973 pr->u.section)
4974 : pr->u.name),
4975 howto->name, pr->addend, (bfd *) NULL,
4976 (asection *) NULL, (bfd_vma) 0)))
4978 free (buf);
4979 return false;
4981 break;
4983 ok = bfd_set_section_contents (finfo->output_bfd, o,
4984 (PTR) buf,
4985 (file_ptr) p->offset,
4986 size);
4987 free (buf);
4988 if (! ok)
4989 return false;
4992 rel_size = obj_reloc_entry_size (finfo->output_bfd);
4993 if (bfd_seek (finfo->output_bfd, *reloff_ptr, SEEK_SET) != 0
4994 || bfd_bwrite (rel_ptr, rel_size, finfo->output_bfd) != rel_size)
4995 return false;
4997 *reloff_ptr += rel_size;
4999 /* Assert that the relocs have not run into the symbols, and that n
5000 the text relocs have not run into the data relocs. */
5001 BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (finfo->output_bfd)
5002 && (reloff_ptr != &finfo->treloff
5003 || (*reloff_ptr
5004 <= obj_datasec (finfo->output_bfd)->rel_filepos)));
5006 return true;
5008 /* end of modified aoutx.h */
5010 bfd_vma
5011 bfd_getp32 (addr)
5012 const bfd_byte *addr;
5014 return (((((bfd_vma)addr[1] << 8) | addr[0]) << 8)
5015 | addr[3]) << 8 | addr[2];
5018 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
5020 bfd_signed_vma
5021 bfd_getp_signed_32 (addr)
5022 const bfd_byte *addr;
5024 return COERCE32((((((bfd_vma)addr[1] << 8) | addr[0]) << 8)
5025 | addr[3]) << 8 | addr[2]);
5028 void
5029 bfd_putp32 (data, addr)
5030 bfd_vma data;
5031 bfd_byte *addr;
5033 addr[0] = (bfd_byte)(data >> 16);
5034 addr[1] = (bfd_byte)(data >> 24);
5035 addr[2] = (bfd_byte)data;
5036 addr[3] = (bfd_byte)(data >> 8);