source: branches/libc-0.6/src/binutils/bfd/pdp11.c

Last change on this file was 607, checked in by bird, 22 years ago

Initial revision

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