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