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