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