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