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