Coverage Report

Created: 2026-04-04 08:16

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