Coverage Report

Created: 2025-06-24 06:45

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