Coverage Report

Created: 2024-05-21 06:29

/src/binutils-gdb/bfd/pdp11.c
Line
Count
Source (jump to first uncovered line)
1
/* BFD back-end for PDP-11 a.out binaries.
2
   Copyright (C) 2001-2024 Free Software Foundation, Inc.
3
4
   This file is part of BFD, the Binary File Descriptor library.
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA. */
20
21
22
/* BFD backend for PDP-11, running 2.11BSD in particular.
23
24
   This file was hacked up by looking hard at the existing vaxnetbsd
25
   back end and the header files in 2.11BSD.  The symbol table format
26
   of 2.11BSD has been extended to accommodate .stab symbols.  See
27
   struct pdp11_external_nlist below for details.
28
29
   TODO
30
   * support for V7 file formats
31
   * support for overlay object files (see 2.11 a.out(5))
32
   * support for old and very old archives
33
   (see 2.11 ar(5), historical section)
34
35
   Search for TODO to find other areas needing more work.  */
36
37
#define BYTES_IN_WORD 2
38
1.06k
#define BYTES_IN_LONG 4
39
#define ARCH_SIZE 16
40
#undef TARGET_IS_BIG_ENDIAN_P
41
42
70.1k
#define TARGET_PAGE_SIZE  8192
43
#define SEGMENT__SIZE TARGET_PAGE_SIZE
44
45
19.9k
#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
19.9k
#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
21.6k
#define TEXT_START_ADDR   0
58
59
/* The header is not included in the text segment.  */
60
43.2k
#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
1.68M
#define N_BADMAG(x) (N_MAGIC(x) != OMAGIC  \
67
1.68M
         && N_MAGIC(x) != NMAGIC  \
68
1.68M
         && N_MAGIC(x) != IMAGIC  \
69
1.68M
         && 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
1.82M
#define EXEC_BYTES_SIZE (8 * 2)
90
91
#define A_MAGIC1  OMAGIC
92
3.41M
#define OMAGIC    0407  /* ...object file or impure executable.  */
93
#define A_MAGIC2  NMAGIC
94
3.38M
#define NMAGIC    0410  /* Pure executable.  */
95
1.86M
#define ZMAGIC    0413  /* Demand-paged executable.  */
96
3.40M
#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
114k
#define QMAGIC    0
102
#define BMAGIC    0
103
104
19.9k
#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
20.4k
#define EXTERNAL_NLIST_SIZE 8
130
131
119k
#define N_TXTOFF(x) (EXEC_BYTES_SIZE)
132
99.9k
#define N_DATOFF(x) (N_TXTOFF(x) + (x)->a_text)
133
79.9k
#define N_TRELOFF(x)  (N_DATOFF(x) + (x)->a_data)
134
59.9k
#define N_DRELOFF(x)  (N_TRELOFF(x) + (x)->a_trsize)
135
39.9k
#define N_SYMOFF(x) (N_DRELOFF(x) + (x)->a_drsize)
136
19.9k
#define N_STROFF(x) (N_SYMOFF(x) + (x)->a_syms)
137
138
0
#define WRITE_HEADERS(abfd, execp) pdp11_aout_write_headers (abfd, execp)
139
140
#include "libbfd.h"
141
#include "libaout.h"
142
143
1.70M
#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
0
#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
0
#define N_TYPE    0x1f  /* Type mask.  */
187
94
#define N_UNDF    0x00  /* Undefined.  */
188
178k
#define N_ABS   0x01  /* Absolute.  */
189
21.0k
#define N_TEXT    0x02  /* Text segment.  */
190
20.3k
#define N_DATA    0x03  /* Data segment.  */
191
20.5k
#define N_BSS   0x04  /* Bss segment.  */
192
0
#define N_REG   0x14  /* Register symbol.  */
193
112
#define N_FN    0x1f  /* File name.  */
194
125k
#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
81.4k
#define RELOC_SIZE 2
203
204
40.2k
#define RELFLG    0x0001  /* PC-relative flag.  */
205
80.4k
#define RTYPE   0x000e  /* Type mask.  */
206
0
#define RIDXMASK  0xfff0  /* Index mask.  */
207
208
40.2k
#define RABS    0x00  /* Absolute.  */
209
0
#define RTEXT   0x02  /* Text.  */
210
0
#define RDATA   0x04  /* Data.  */
211
0
#define RBSS    0x06  /* Bss.  */
212
40.2k
#define REXT    0x08  /* External.  */
213
214
26.5k
#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
43.3k
{
340
43.3k
  if (type == N_GSYM || type == N_FUN)
341
153
    return strchr (name, ':') != NULL;
342
43.1k
  return type > N_FUN;
343
43.3k
}
344
345
static int
346
pdp11_aout_write_headers (bfd *abfd, struct internal_exec *execp)
347
0
{
348
0
  struct external_exec exec_bytes;
349
350
0
  if (adata(abfd).magic == undecided_magic)
351
0
    NAME (aout, adjust_sizes_and_vmas) (abfd);
352
353
0
  execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE;
354
0
  execp->a_entry = bfd_get_start_address (abfd);
355
356
0
  if (obj_textsec (abfd)->reloc_count > 0
357
0
      || obj_datasec (abfd)->reloc_count > 0)
358
0
    {
359
0
      execp->a_trsize = execp->a_text;
360
0
      execp->a_drsize = execp->a_data;
361
0
    }
362
0
  else
363
0
    {
364
0
      execp->a_trsize = 0;
365
0
      execp->a_drsize = 0;
366
0
    }
367
368
0
  if (!NAME (aout, swap_exec_header_out) (abfd, execp, & exec_bytes))
369
0
    return false;
370
371
0
  if (bfd_seek (abfd, 0, SEEK_SET) != 0)
372
0
    return false;
373
374
0
  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
0
  if (bfd_get_outsymbols (abfd) != NULL
379
0
      && bfd_get_symcount (abfd) != 0)
380
0
    {
381
0
      if (bfd_seek (abfd, N_SYMOFF (execp), SEEK_SET) != 0)
382
0
  return false;
383
384
0
      if (! NAME (aout, write_syms) (abfd))
385
0
  return false;
386
0
    }
387
388
0
  if (obj_textsec (abfd)->reloc_count > 0
389
0
      || obj_datasec (abfd)->reloc_count > 0)
390
0
    {
391
0
      if (bfd_seek (abfd, N_TRELOFF (execp), SEEK_SET) != 0
392
0
    || !NAME (aout, squirt_out_relocs) (abfd, obj_textsec (abfd))
393
0
    || bfd_seek (abfd, N_DRELOFF (execp), SEEK_SET) != 0
394
0
    || !NAME (aout, squirt_out_relocs) (abfd, obj_datasec (abfd)))
395
0
  return false;
396
0
    }
397
398
0
  return true;
399
0
}
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
0
{
408
0
  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
0
  if (! abfd->output_has_begun)
414
0
    NAME (aout, adjust_sizes_and_vmas) (abfd);
415
416
0
  obj_reloc_entry_size (abfd) = RELOC_SIZE;
417
418
0
  return WRITE_HEADERS (abfd, execp);
419
0
}
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
19.9k
{
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
19.9k
  memset ((void *) execp, 0, sizeof (struct internal_exec));
436
  /* Now fill in fields in the execp, from the bytes in the raw data.  */
437
19.9k
  execp->a_info   = GET_MAGIC (abfd, bytes->e_info);
438
19.9k
  execp->a_text   = GET_WORD (abfd, bytes->e_text);
439
19.9k
  execp->a_data   = GET_WORD (abfd, bytes->e_data);
440
19.9k
  execp->a_bss    = GET_WORD (abfd, bytes->e_bss);
441
19.9k
  execp->a_syms   = GET_WORD (abfd, bytes->e_syms);
442
19.9k
  execp->a_entry  = GET_WORD (abfd, bytes->e_entry);
443
444
19.9k
  if (GET_WORD (abfd, bytes->e_flag) & A_FLAG_RELOC_STRIPPED)
445
8.08k
    {
446
8.08k
      execp->a_trsize = 0;
447
8.08k
      execp->a_drsize = 0;
448
8.08k
    }
449
11.9k
  else
450
11.9k
    {
451
11.9k
      execp->a_trsize = execp->a_text;
452
11.9k
      execp->a_drsize = execp->a_data;
453
11.9k
    }
454
19.9k
}
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
0
{
465
0
  const char *err = NULL;
466
0
  uint64_t val;
467
0
#define MAXVAL(x) ((UINT64_C (1) << (8 * sizeof (x) - 1) << 1) - 1)
468
0
  if ((val = execp->a_text) > MAXVAL (bytes->e_text))
469
0
    err = "e_text";
470
0
  else if ((val = execp->a_data) > MAXVAL (bytes->e_data))
471
0
    err = "e_data";
472
0
  else if ((val = execp->a_bss) > MAXVAL (bytes->e_bss))
473
0
    err = "e_bss";
474
0
  else if ((val = execp->a_syms) > MAXVAL (bytes->e_syms))
475
0
    err = "e_syms";
476
0
  else if ((val = execp->a_entry) > MAXVAL (bytes->e_entry))
477
0
    err = "e_entry";
478
0
#undef MAXVAL
479
0
  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
0
  PUT_MAGIC (abfd, execp->a_info,    bytes->e_info);
489
0
  PUT_WORD (abfd, execp->a_text,    bytes->e_text);
490
0
  PUT_WORD (abfd, execp->a_data,    bytes->e_data);
491
0
  PUT_WORD (abfd, execp->a_bss,     bytes->e_bss);
492
0
  PUT_WORD (abfd, execp->a_syms,    bytes->e_syms);
493
0
  PUT_WORD (abfd, execp->a_entry,   bytes->e_entry);
494
0
  PUT_WORD (abfd, 0,        bytes->e_unused);
495
496
0
  if ((execp->a_trsize == 0 || execp->a_text == 0)
497
0
      && (execp->a_drsize == 0 || execp->a_data == 0))
498
0
    PUT_WORD (abfd, A_FLAG_RELOC_STRIPPED, bytes->e_flag);
499
0
  else if (execp->a_trsize == execp->a_text
500
0
     && execp->a_drsize == execp->a_data)
501
0
    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
0
  return true;
509
0
}
510
511
/* Make all the section for an a.out file.  */
512
513
bool
514
NAME (aout, make_sections) (bfd *abfd)
515
19.9k
{
516
19.9k
  if (obj_textsec (abfd) == NULL && bfd_make_section (abfd, ".text") == NULL)
517
0
    return false;
518
19.9k
  if (obj_datasec (abfd) == NULL && bfd_make_section (abfd, ".data") == NULL)
519
0
    return false;
520
19.9k
  if (obj_bsssec (abfd) == NULL  && bfd_make_section (abfd, ".bss") == NULL)
521
0
    return false;
522
19.9k
  return true;
523
19.9k
}
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
19.9k
{
536
19.9k
  struct aout_data_struct *rawptr, *oldrawptr;
537
19.9k
  bfd_cleanup result;
538
19.9k
  size_t amt = sizeof (*rawptr);
539
540
19.9k
  rawptr = bfd_zalloc (abfd, amt);
541
19.9k
  if (rawptr == NULL)
542
0
    return NULL;
543
544
19.9k
  oldrawptr = abfd->tdata.aout_data;
545
19.9k
  abfd->tdata.aout_data = rawptr;
546
547
  /* Copy the contents of the old tdata struct.  */
548
19.9k
  if (oldrawptr != NULL)
549
0
    *abfd->tdata.aout_data = *oldrawptr;
550
551
19.9k
  abfd->tdata.aout_data->a.hdr = &rawptr->e;
552
  /* Copy in the internal_exec struct.  */
553
19.9k
  *(abfd->tdata.aout_data->a.hdr) = *execp;
554
19.9k
  execp = abfd->tdata.aout_data->a.hdr;
555
556
  /* Set the file flags.  */
557
19.9k
  abfd->flags = BFD_NO_FLAGS;
558
19.9k
  if (execp->a_drsize || execp->a_trsize)
559
10.0k
    abfd->flags |= HAS_RELOC;
560
  /* Setting of EXEC_P has been deferred to the bottom of this function.  */
561
19.9k
  if (execp->a_syms)
562
12.3k
    abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
563
19.9k
  if (N_DYNAMIC (execp))
564
0
    abfd->flags |= DYNAMIC;
565
566
19.9k
  if (N_MAGIC (execp) == ZMAGIC)
567
7.21k
    {
568
7.21k
      abfd->flags |= D_PAGED | WP_TEXT;
569
7.21k
      adata (abfd).magic = z_magic;
570
7.21k
    }
571
12.7k
  else if (N_MAGIC (execp) == NMAGIC)
572
7.01k
    {
573
7.01k
      abfd->flags |= WP_TEXT;
574
7.01k
      adata (abfd).magic = n_magic;
575
7.01k
    }
576
5.77k
  else if (N_MAGIC (execp) == OMAGIC)
577
4.49k
    adata (abfd).magic = o_magic;
578
1.27k
  else if (N_MAGIC (execp) == IMAGIC)
579
1.27k
    adata (abfd).magic = i_magic;
580
0
  else
581
0
    {
582
      /* Should have been checked with N_BADMAG before this routine
583
   was called.  */
584
0
      abort ();
585
0
    }
586
587
19.9k
  abfd->start_address = execp->a_entry;
588
589
19.9k
  abfd->symcount = execp->a_syms / sizeof (struct external_nlist);
590
591
  /* The default relocation entry size is that of traditional V7 Unix.  */
592
19.9k
  obj_reloc_entry_size (abfd) = RELOC_SIZE;
593
594
  /* The default symbol entry size is that of traditional Unix.  */
595
19.9k
  obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
596
597
19.9k
  if (! NAME (aout, make_sections) (abfd))
598
0
    goto error_ret;
599
600
19.9k
  obj_datasec (abfd)->size = execp->a_data;
601
19.9k
  obj_bsssec (abfd)->size = execp->a_bss;
602
603
19.9k
  obj_textsec (abfd)->flags =
604
19.9k
    (execp->a_trsize != 0
605
19.9k
     ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
606
19.9k
     : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
607
19.9k
  obj_datasec (abfd)->flags =
608
19.9k
    (execp->a_drsize != 0
609
19.9k
     ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
610
19.9k
     : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
611
19.9k
  obj_bsssec (abfd)->flags = SEC_ALLOC;
612
613
#ifdef THIS_IS_ONLY_DOCUMENTATION
614
  /* The common code can't fill in these things because they depend
615
     on either the start address of the text segment, the rounding
616
     up of virtual addresses between segments, or the starting file
617
     position of the text segment -- all of which varies among different
618
     versions of a.out.  */
619
620
  /* Call back to the format-dependent code to fill in the rest of the
621
     fields and do any further cleanup.  Things that should be filled
622
     in by the callback:  */
623
  struct exec *execp = exec_hdr (abfd);
624
625
  obj_textsec (abfd)->size = N_TXTSIZE (execp);
626
  /* Data and bss are already filled in since they're so standard.  */
627
628
  /* The virtual memory addresses of the sections.  */
629
  obj_textsec (abfd)->vma = N_TXTADDR (execp);
630
  obj_datasec (abfd)->vma = N_DATADDR (execp);
631
  obj_bsssec  (abfd)->vma = N_BSSADDR (execp);
632
633
  /* The file offsets of the sections.  */
634
  obj_textsec (abfd)->filepos = N_TXTOFF (execp);
635
  obj_datasec (abfd)->filepos = N_DATOFF (execp);
636
637
  /* The file offsets of the relocation info.  */
638
  obj_textsec (abfd)->rel_filepos = N_TRELOFF (execp);
639
  obj_datasec (abfd)->rel_filepos = N_DRELOFF (execp);
640
641
  /* The file offsets of the string table and symbol table.  */
642
  obj_str_filepos (abfd) = N_STROFF (execp);
643
  obj_sym_filepos (abfd) = N_SYMOFF (execp);
644
645
  /* Determine the architecture and machine type of the object file.  */
646
  abfd->obj_arch = bfd_arch_obscure;
647
648
  adata (abfd)->page_size = TARGET_PAGE_SIZE;
649
  adata (abfd)->segment_size = SEGMENT_SIZE;
650
  adata (abfd)->exec_bytes_size = EXEC_BYTES_SIZE;
651
652
  return _bfd_no_cleanup;
653
654
  /* The architecture is encoded in various ways in various a.out variants,
655
     or is not encoded at all in some of them.  The relocation size depends
656
     on the architecture and the a.out variant.  Finally, the return value
657
     is the bfd_target vector in use.  If an error occurs, return zero and
658
     set bfd_error to the appropriate error code.
659
660
     Formats such as b.out, which have additional fields in the a.out
661
     header, should cope with them in this callback as well.  */
662
#endif  /* DOCUMENTATION */
663
664
19.9k
  result = (*callback_to_real_object_p) (abfd);
665
666
  /* Now that the segment addresses have been worked out, take a better
667
     guess at whether the file is executable.  If the entry point
668
     is within the text segment, assume it is.  (This makes files
669
     executable even if their entry point address is 0, as long as
670
     their text starts at zero.).
671
672
     This test had to be changed to deal with systems where the text segment
673
     runs at a different location than the default.  The problem is that the
674
     entry address can appear to be outside the text segment, thus causing an
675
     erroneous conclusion that the file isn't executable.
676
677
     To fix this, we now accept any non-zero entry point as an indication of
678
     executability.  This will work most of the time, since only the linker
679
     sets the entry point, and that is likely to be non-zero for most systems.  */
680
681
19.9k
  if (execp->a_entry != 0
682
19.9k
      || (execp->a_entry >= obj_textsec (abfd)->vma
683
6.72k
    && execp->a_entry < (obj_textsec (abfd)->vma
684
6.72k
             + obj_textsec (abfd)->size)
685
6.72k
    && execp->a_trsize == 0
686
6.72k
    && execp->a_drsize == 0))
687
14.6k
    abfd->flags |= EXEC_P;
688
#ifdef STAT_FOR_EXEC
689
  else
690
    {
691
      struct stat stat_buf;
692
693
      /* The original heuristic doesn't work in some important cases.
694
  The a.out file has no information about the text start
695
  address.  For files (like kernels) linked to non-standard
696
  addresses (ld -Ttext nnn) the entry point may not be between
697
  the default text start (obj_textsec(abfd)->vma) and
698
  (obj_textsec(abfd)->vma) + text size.  This is not just a mach
699
  issue.  Many kernels are loaded at non standard addresses.  */
700
      if (abfd->iostream != NULL
701
    && (abfd->flags & BFD_IN_MEMORY) == 0
702
    && (fstat (fileno ((FILE *) (abfd->iostream)), &stat_buf) == 0)
703
    && ((stat_buf.st_mode & 0111) != 0))
704
  abfd->flags |= EXEC_P;
705
    }
706
#endif /* STAT_FOR_EXEC */
707
708
19.9k
  if (result)
709
19.9k
    return result;
710
711
0
 error_ret:
712
0
  bfd_release (abfd, rawptr);
713
0
  abfd->tdata.aout_data = oldrawptr;
714
0
  return NULL;
715
19.9k
}
716
717
/* Initialize ABFD for use with a.out files.  */
718
719
bool
720
NAME (aout, mkobject) (bfd *abfd)
721
0
{
722
0
  struct aout_data_struct  *rawptr;
723
0
  size_t amt = sizeof (struct aout_data_struct);
724
725
0
  bfd_set_error (bfd_error_system_call);
726
727
  /* Use an intermediate variable for clarity.  */
728
0
  rawptr = bfd_zalloc (abfd, amt);
729
730
0
  if (rawptr == NULL)
731
0
    return false;
732
733
0
  abfd->tdata.aout_data = rawptr;
734
0
  exec_hdr (abfd) = &(rawptr->e);
735
736
0
  obj_textsec (abfd) = NULL;
737
0
  obj_datasec (abfd) = NULL;
738
0
  obj_bsssec (abfd)  = NULL;
739
740
0
  return true;
741
0
}
742
743
/* Keep track of machine architecture and machine type for
744
   a.out's. Return the <<machine_type>> for a particular
745
   architecture and machine, or <<M_UNKNOWN>> if that exact architecture
746
   and machine can't be represented in a.out format.
747
748
   If the architecture is understood, machine type 0 (default)
749
   is always understood.  */
750
751
enum machine_type
752
NAME (aout, machine_type) (enum bfd_architecture arch,
753
         unsigned long machine,
754
         bool *unknown)
755
0
{
756
0
  enum machine_type arch_flags;
757
758
0
  arch_flags = M_UNKNOWN;
759
0
  *unknown = true;
760
761
0
  switch (arch)
762
0
    {
763
0
    case bfd_arch_sparc:
764
0
      if (machine == 0
765
0
    || machine == bfd_mach_sparc
766
0
    || machine == bfd_mach_sparc_sparclite
767
0
    || machine == bfd_mach_sparc_v9)
768
0
  arch_flags = M_SPARC;
769
0
      else if (machine == bfd_mach_sparc_sparclet)
770
0
  arch_flags = M_SPARCLET;
771
0
      break;
772
773
0
    case bfd_arch_i386:
774
0
      if (machine == 0
775
0
    || machine == bfd_mach_i386_i386
776
0
    || machine == bfd_mach_i386_i386_intel_syntax)
777
0
  arch_flags = M_386;
778
0
      break;
779
780
0
    case bfd_arch_arm:
781
0
      if (machine == 0) arch_flags = M_ARM;
782
0
      break;
783
784
0
    case bfd_arch_mips:
785
0
      switch (machine)
786
0
  {
787
0
  case 0:
788
0
  case 2000:
789
0
  case bfd_mach_mips3000:
790
0
    arch_flags = M_MIPS1;
791
0
    break;
792
0
  case bfd_mach_mips4000: /* MIPS3 */
793
0
  case bfd_mach_mips4400:
794
0
  case bfd_mach_mips8000: /* MIPS4 */
795
0
  case bfd_mach_mips6000: /* Real MIPS2: */
796
0
    arch_flags = M_MIPS2;
797
0
    break;
798
0
  default:
799
0
    arch_flags = M_UNKNOWN;
800
0
    break;
801
0
  }
802
0
      break;
803
804
0
    case bfd_arch_ns32k:
805
0
      switch (machine)
806
0
  {
807
0
  case 0:     arch_flags = M_NS32532; break;
808
0
  case 32032:   arch_flags = M_NS32032; break;
809
0
  case 32532:   arch_flags = M_NS32532; break;
810
0
  default:    arch_flags = M_UNKNOWN; break;
811
0
  }
812
0
      break;
813
814
0
    case bfd_arch_pdp11:
815
      /* TODO: arch_flags = M_PDP11; */
816
0
      *unknown = false;
817
0
      break;
818
819
0
    case bfd_arch_vax:
820
0
      *unknown = false;
821
0
      break;
822
823
0
    default:
824
0
      arch_flags = M_UNKNOWN;
825
0
    }
826
827
0
  if (arch_flags != M_UNKNOWN)
828
0
    *unknown = false;
829
830
0
  return arch_flags;
831
0
}
832
833
/* Set the architecture and the machine of the ABFD to the
834
   values ARCH and MACHINE.  Verify that @ABFD's format
835
   can support the architecture required.  */
836
837
bool
838
NAME (aout, set_arch_mach) (bfd *abfd,
839
          enum bfd_architecture arch,
840
          unsigned long machine)
841
0
{
842
0
  if (! bfd_default_set_arch_mach (abfd, arch, machine))
843
0
    return false;
844
845
0
  if (arch != bfd_arch_unknown)
846
0
    {
847
0
      bool unknown;
848
849
0
      NAME (aout, machine_type) (arch, machine, &unknown);
850
0
      if (unknown)
851
0
  return false;
852
0
    }
853
854
0
  obj_reloc_entry_size (abfd) = RELOC_SIZE;
855
856
0
  return (*aout_backend_info(abfd)->set_sizes) (abfd);
857
0
}
858
859
static void
860
adjust_o_magic (bfd *abfd, struct internal_exec *execp)
861
0
{
862
0
  file_ptr pos = adata (abfd).exec_bytes_size;
863
0
  bfd_vma vma = 0;
864
0
  int pad = 0;
865
0
  asection *text = obj_textsec (abfd);
866
0
  asection *data = obj_datasec (abfd);
867
0
  asection *bss = obj_bsssec (abfd);
868
869
  /* Text.  */
870
0
  text->filepos = pos;
871
0
  if (!text->user_set_vma)
872
0
    text->vma = vma;
873
0
  else
874
0
    vma = text->vma;
875
876
0
  pos += execp->a_text;
877
0
  vma += execp->a_text;
878
879
  /* Data.  */
880
0
  if (!data->user_set_vma)
881
0
    {
882
0
      pos += pad;
883
0
      vma += pad;
884
0
      data->vma = vma;
885
0
    }
886
0
  else
887
0
    vma = data->vma;
888
0
  execp->a_text += pad;
889
890
0
  data->filepos = pos;
891
0
  pos += data->size;
892
0
  vma += data->size;
893
894
  /* BSS.  */
895
0
  if (!bss->user_set_vma)
896
0
    {
897
0
      pos += pad;
898
0
      vma += pad;
899
0
      bss->vma = vma;
900
0
    }
901
0
  else if (data->size > 0 || bss->size > 0) /* PR25677: for objcopy --extract-symbol */
902
0
    {
903
      /* The VMA of the .bss section is set by the VMA of the
904
   .data section plus the size of the .data section.  We may
905
   need to add padding bytes to make this true.  */
906
0
      pad = bss->vma - vma;
907
0
      if (pad < 0)
908
0
  pad = 0;
909
0
      pos += pad;
910
0
    }
911
0
  execp->a_data = data->size + pad;
912
0
  bss->filepos = pos;
913
0
  execp->a_bss = bss->size;
914
915
0
  N_SET_MAGIC (execp, OMAGIC);
916
0
}
917
918
static void
919
adjust_z_magic (bfd *abfd, struct internal_exec *execp)
920
0
{
921
0
  bfd_size_type data_pad, text_pad;
922
0
  file_ptr text_end;
923
0
  const struct aout_backend_data *abdp;
924
  /* TRUE if text includes exec header.  */
925
0
  bool ztih;
926
0
  asection *text = obj_textsec (abfd);
927
0
  asection *data = obj_datasec (abfd);
928
0
  asection *bss = obj_bsssec (abfd);
929
930
0
  abdp = aout_backend_info (abfd);
931
932
  /* Text.  */
933
0
  ztih = (abdp != NULL
934
0
    && (abdp->text_includes_header
935
0
        || obj_aout_subformat (abfd) == q_magic_format));
936
0
  text->filepos = (ztih
937
0
       ? adata (abfd).exec_bytes_size
938
0
       : adata (abfd).zmagic_disk_block_size);
939
0
  if (!text->user_set_vma)
940
0
    {
941
      /* ?? Do we really need to check for relocs here?  */
942
0
      text->vma = ((abfd->flags & HAS_RELOC)
943
0
       ? 0
944
0
       : (ztih
945
0
          ? abdp->default_text_vma + adata (abfd).exec_bytes_size
946
0
          : abdp->default_text_vma));
947
0
      text_pad = 0;
948
0
    }
949
0
  else
950
0
    {
951
      /* The .text section is being loaded at an unusual address.  We
952
   may need to pad it such that the .data section starts at a page
953
   boundary.  */
954
0
      if (ztih)
955
0
  text_pad = ((text->filepos - text->vma)
956
0
        & (adata (abfd).page_size - 1));
957
0
      else
958
0
  text_pad = (-text->vma
959
0
        & (adata (abfd).page_size - 1));
960
0
    }
961
962
  /* Find start of data.  */
963
0
  if (ztih)
964
0
    {
965
0
      text_end = text->filepos + execp->a_text;
966
0
      text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
967
0
    }
968
0
  else
969
0
    {
970
      /* Note that if page_size == zmagic_disk_block_size, then
971
   filepos == page_size, and this case is the same as the ztih
972
   case.  */
973
0
      text_end = execp->a_text;
974
0
      text_pad += BFD_ALIGN (text_end, adata (abfd).page_size) - text_end;
975
0
      text_end += text->filepos;
976
0
    }
977
0
  execp->a_text += text_pad;
978
979
  /* Data.  */
980
0
  if (!data->user_set_vma)
981
0
    {
982
0
      bfd_vma vma;
983
0
      vma = text->vma + execp->a_text;
984
0
      data->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
985
0
    }
986
0
  if (abdp && abdp->zmagic_mapped_contiguous)
987
0
    {
988
0
      text_pad = data->vma - (text->vma + execp->a_text);
989
      /* Only pad the text section if the data
990
   section is going to be placed after it.  */
991
0
      if (text_pad > 0)
992
0
  execp->a_text += text_pad;
993
0
    }
994
0
  data->filepos = text->filepos + execp->a_text;
995
996
  /* Fix up exec header while we're at it.  */
997
0
  if (ztih && (!abdp || (abdp && !abdp->exec_header_not_counted)))
998
0
    execp->a_text += adata (abfd).exec_bytes_size;
999
0
  N_SET_MAGIC (execp, ZMAGIC);
1000
1001
  /* Spec says data section should be rounded up to page boundary.  */
1002
0
  execp->a_data = align_power (data->size, bss->alignment_power);
1003
0
  execp->a_data = BFD_ALIGN (execp->a_data, adata (abfd).page_size);
1004
0
  data_pad = execp->a_data - data->size;
1005
1006
  /* BSS.  */
1007
0
  if (!bss->user_set_vma)
1008
0
    bss->vma = data->vma + execp->a_data;
1009
  /* If the BSS immediately follows the data section and extra space
1010
     in the page is left after the data section, fudge data
1011
     in the header so that the bss section looks smaller by that
1012
     amount.  We'll start the bss section there, and lie to the OS.
1013
     (Note that a linker script, as well as the above assignment,
1014
     could have explicitly set the BSS vma to immediately follow
1015
     the data section.)  */
1016
0
  if (align_power (bss->vma, bss->alignment_power) == data->vma + execp->a_data)
1017
0
    execp->a_bss = data_pad > bss->size ? 0 : bss->size - data_pad;
1018
0
  else
1019
0
    execp->a_bss = bss->size;
1020
0
}
1021
1022
static void
1023
adjust_n_magic (bfd *abfd, struct internal_exec *execp)
1024
0
{
1025
0
  file_ptr pos = adata (abfd).exec_bytes_size;
1026
0
  bfd_vma vma = 0;
1027
0
  int pad;
1028
0
  asection *text = obj_textsec (abfd);
1029
0
  asection *data = obj_datasec (abfd);
1030
0
  asection *bss = obj_bsssec (abfd);
1031
1032
  /* Text.  */
1033
0
  text->filepos = pos;
1034
0
  if (!text->user_set_vma)
1035
0
    text->vma = vma;
1036
0
  else
1037
0
    vma = text->vma;
1038
0
  pos += execp->a_text;
1039
0
  vma += execp->a_text;
1040
1041
  /* Data.  */
1042
0
  data->filepos = pos;
1043
0
  if (!data->user_set_vma)
1044
0
    data->vma = BFD_ALIGN (vma, adata (abfd).segment_size);
1045
0
  vma = data->vma;
1046
1047
  /* Since BSS follows data immediately, see if it needs alignment.  */
1048
0
  vma += data->size;
1049
0
  pad = align_power (vma, bss->alignment_power) - vma;
1050
0
  execp->a_data = data->size + pad;
1051
0
  pos += execp->a_data;
1052
1053
  /* BSS.  */
1054
0
  if (!bss->user_set_vma)
1055
0
    bss->vma = vma;
1056
0
  else
1057
0
    vma = bss->vma;
1058
1059
  /* Fix up exec header.  */
1060
0
  execp->a_bss = bss->size;
1061
0
  N_SET_MAGIC (execp, NMAGIC);
1062
0
}
1063
1064
static void
1065
adjust_i_magic (bfd *abfd, struct internal_exec *execp)
1066
0
{
1067
0
  file_ptr pos = adata (abfd).exec_bytes_size;
1068
0
  bfd_vma vma = 0;
1069
0
  int pad;
1070
0
  asection *text = obj_textsec (abfd);
1071
0
  asection *data = obj_datasec (abfd);
1072
0
  asection *bss = obj_bsssec (abfd);
1073
1074
  /* Text.  */
1075
0
  text->filepos = pos;
1076
0
  if (!text->user_set_vma)
1077
0
    text->vma = vma;
1078
0
  else
1079
0
    vma = text->vma;
1080
0
  pos += execp->a_text;
1081
1082
  /* Data.  */
1083
0
  data->filepos = pos;
1084
0
  if (!data->user_set_vma)
1085
0
    data->vma = 0;
1086
0
  vma = data->vma;
1087
1088
  /* Since BSS follows data immediately, see if it needs alignment.  */
1089
0
  vma += data->size;
1090
0
  pad = align_power (vma, bss->alignment_power) - vma;
1091
0
  execp->a_data = data->size + pad;
1092
0
  pos += execp->a_data;
1093
1094
  /* BSS.  */
1095
0
  if (!bss->user_set_vma)
1096
0
    bss->vma = vma;
1097
0
  else
1098
0
    vma = bss->vma;
1099
1100
  /* Fix up exec header.  */
1101
0
  execp->a_bss = bss->size;
1102
0
  N_SET_MAGIC (execp, IMAGIC);
1103
0
}
1104
1105
bool
1106
NAME (aout, adjust_sizes_and_vmas) (bfd *abfd)
1107
0
{
1108
0
  struct internal_exec *execp = exec_hdr (abfd);
1109
1110
0
  if (! NAME (aout, make_sections) (abfd))
1111
0
    return false;
1112
1113
0
  if (adata (abfd).magic != undecided_magic)
1114
0
    return true;
1115
1116
0
  execp->a_text = align_power (obj_textsec (abfd)->size,
1117
0
             obj_textsec (abfd)->alignment_power);
1118
1119
  /* Rule (heuristic) for when to pad to a new page.  Note that there
1120
     are (at least) two ways demand-paged (ZMAGIC) files have been
1121
     handled.  Most Berkeley-based systems start the text segment at
1122
     (TARGET_PAGE_SIZE).  However, newer versions of SUNOS start the text
1123
     segment right after the exec header; the latter is counted in the
1124
     text segment size, and is paged in by the kernel with the rest of
1125
     the text.  */
1126
1127
  /* This perhaps isn't the right way to do this, but made it simpler for me
1128
     to understand enough to implement it.  Better would probably be to go
1129
     right from BFD flags to alignment/positioning characteristics.  But the
1130
     old code was sloppy enough about handling the flags, and had enough
1131
     other magic, that it was a little hard for me to understand.  I think
1132
     I understand it better now, but I haven't time to do the cleanup this
1133
     minute.  */
1134
1135
0
  if (separate_i_d)
1136
0
    adata (abfd).magic = i_magic;
1137
0
  else if (abfd->flags & WP_TEXT)
1138
0
    adata (abfd).magic = n_magic;
1139
0
  else
1140
0
    adata (abfd).magic = o_magic;
1141
1142
#ifdef BFD_AOUT_DEBUG /* requires gcc2 */
1143
#if __GNUC__ >= 2
1144
  fprintf (stderr, "%s text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x,%x>\n",
1145
     ({ char *str;
1146
        switch (adata (abfd).magic)
1147
    {
1148
    case n_magic: str = "NMAGIC"; break;
1149
    case o_magic: str = "OMAGIC"; break;
1150
    case i_magic: str = "IMAGIC"; break;
1151
    case z_magic: str = "ZMAGIC"; break;
1152
    default: abort ();
1153
    }
1154
        str;
1155
      }),
1156
     obj_textsec (abfd)->vma, obj_textsec (abfd)->size,
1157
    obj_textsec (abfd)->alignment_power,
1158
     obj_datasec (abfd)->vma, obj_datasec (abfd)->size,
1159
    obj_datasec (abfd)->alignment_power,
1160
     obj_bsssec (abfd)->vma, obj_bsssec (abfd)->size,
1161
    obj_bsssec (abfd)->alignment_power);
1162
#endif
1163
#endif
1164
1165
0
  switch (adata (abfd).magic)
1166
0
    {
1167
0
    case o_magic:
1168
0
      adjust_o_magic (abfd, execp);
1169
0
      break;
1170
0
    case z_magic:
1171
0
      adjust_z_magic (abfd, execp);
1172
0
      break;
1173
0
    case n_magic:
1174
0
      adjust_n_magic (abfd, execp);
1175
0
      break;
1176
0
    case i_magic:
1177
0
      adjust_i_magic (abfd, execp);
1178
0
      break;
1179
0
    default:
1180
0
      abort ();
1181
0
    }
1182
1183
#ifdef BFD_AOUT_DEBUG
1184
  fprintf (stderr, "       text=<%x,%x,%x> data=<%x,%x,%x> bss=<%x,%x>\n",
1185
     obj_textsec (abfd)->vma, execp->a_text,
1186
    obj_textsec (abfd)->filepos,
1187
     obj_datasec (abfd)->vma, execp->a_data,
1188
    obj_datasec (abfd)->filepos,
1189
     obj_bsssec (abfd)->vma, execp->a_bss);
1190
#endif
1191
1192
0
  return true;
1193
0
}
1194
1195
/* Called by the BFD in response to a bfd_make_section request.  */
1196
1197
bool
1198
NAME (aout, new_section_hook) (bfd *abfd, asection *newsect)
1199
59.9k
{
1200
  /* Align to double at least.  */
1201
59.9k
  newsect->alignment_power = bfd_get_arch_info(abfd)->section_align_power;
1202
1203
59.9k
  if (bfd_get_format (abfd) == bfd_object)
1204
59.9k
    {
1205
59.9k
      if (obj_textsec (abfd) == NULL
1206
59.9k
    && !strcmp (newsect->name, ".text"))
1207
19.9k
  {
1208
19.9k
    obj_textsec(abfd)= newsect;
1209
19.9k
    newsect->target_index = N_TEXT;
1210
19.9k
  }
1211
39.9k
      else if (obj_datasec (abfd) == NULL
1212
39.9k
         && !strcmp (newsect->name, ".data"))
1213
19.9k
  {
1214
19.9k
    obj_datasec (abfd) = newsect;
1215
19.9k
    newsect->target_index = N_DATA;
1216
19.9k
  }
1217
19.9k
      else if (obj_bsssec (abfd) == NULL
1218
19.9k
         && !strcmp (newsect->name, ".bss"))
1219
19.9k
  {
1220
19.9k
    obj_bsssec (abfd) = newsect;
1221
19.9k
    newsect->target_index = N_BSS;
1222
19.9k
  }
1223
59.9k
    }
1224
1225
  /* We allow more than three sections internally.  */
1226
59.9k
  return _bfd_generic_new_section_hook (abfd, newsect);
1227
59.9k
}
1228
1229
bool
1230
NAME (aout, set_section_contents) (bfd *abfd,
1231
           sec_ptr section,
1232
           const void * location,
1233
           file_ptr offset,
1234
           bfd_size_type count)
1235
0
{
1236
0
  if (! abfd->output_has_begun)
1237
0
    {
1238
0
      if (! NAME (aout, adjust_sizes_and_vmas) (abfd))
1239
0
  return false;
1240
0
    }
1241
1242
0
  if (section == obj_bsssec (abfd))
1243
0
    {
1244
0
      bfd_set_error (bfd_error_no_contents);
1245
0
      return false;
1246
0
    }
1247
1248
0
  if (section != obj_textsec (abfd)
1249
0
      && section != obj_datasec (abfd))
1250
0
    {
1251
0
      _bfd_error_handler
1252
  /* xgettext:c-format */
1253
0
  (_("%pB: can not represent section `%pA' in a.out object file format"),
1254
0
   abfd, section);
1255
0
      bfd_set_error (bfd_error_nonrepresentable_section);
1256
0
      return false;
1257
0
    }
1258
1259
0
  if (count != 0)
1260
0
    {
1261
0
      if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
1262
0
    || bfd_write (location, count, abfd) != count)
1263
0
  return false;
1264
0
    }
1265
1266
0
  return true;
1267
0
}
1268

1269
/* Read the external symbols from an a.out file.  */
1270
1271
static bool
1272
aout_get_external_symbols (bfd *abfd)
1273
579
{
1274
579
  if (obj_aout_external_syms (abfd) == NULL)
1275
471
    {
1276
471
      bfd_size_type count;
1277
471
      struct external_nlist *syms = NULL;
1278
471
      bfd_size_type amt = exec_hdr (abfd)->a_syms;
1279
1280
471
      count = amt / EXTERNAL_NLIST_SIZE;
1281
1282
      /* PR 17512: file: 011f5a08.  */
1283
471
      if (count == 0)
1284
11
  return true;
1285
1286
      /* We allocate using malloc to make the values easy to free
1287
   later on.  If we put them on the objalloc it might not be
1288
   possible to free them.  */
1289
460
      if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
1290
0
  return false;
1291
460
      syms = _bfd_malloc_and_read (abfd, amt, amt);
1292
460
      if (syms == NULL)
1293
186
  return false;
1294
1295
274
      obj_aout_external_syms (abfd) = syms;
1296
274
      obj_aout_external_sym_count (abfd) = count;
1297
274
    }
1298
1299
382
  if (obj_aout_external_strings (abfd) == NULL
1300
382
      && exec_hdr (abfd)->a_syms != 0)
1301
274
    {
1302
274
      unsigned char string_chars[BYTES_IN_LONG];
1303
274
      bfd_size_type stringsize;
1304
274
      char *strings;
1305
274
      bfd_size_type amt = BYTES_IN_LONG;
1306
1307
      /* Get the size of the strings.  */
1308
274
      if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
1309
274
    || bfd_read (string_chars, amt, abfd) != amt)
1310
3
  return false;
1311
271
      stringsize = H_GET_32 (abfd, string_chars);
1312
271
      if (stringsize == 0)
1313
119
  stringsize = 1;
1314
152
      else if (stringsize + 1 < BYTES_IN_LONG + 1
1315
152
         || (size_t) stringsize != stringsize)
1316
3
  {
1317
3
    bfd_set_error (bfd_error_bad_value);
1318
3
    return false;
1319
3
  }
1320
1321
268
      strings = (char *) bfd_malloc (stringsize + 1);
1322
268
      if (strings == NULL)
1323
0
  return false;
1324
1325
268
      if (stringsize >= BYTES_IN_LONG)
1326
149
  {
1327
149
    amt = stringsize - BYTES_IN_LONG;
1328
149
    if (bfd_read (strings + BYTES_IN_LONG, amt, abfd) != amt)
1329
77
      {
1330
77
        free (strings);
1331
77
        return false;
1332
77
      }
1333
149
  }
1334
1335
      /* Ensure that a zero index yields an empty string.  */
1336
191
      if (stringsize >= BYTES_IN_WORD)
1337
72
  memset (strings, 0, BYTES_IN_LONG);
1338
1339
      /* Ensure that the string buffer is NUL terminated.  */
1340
191
      strings[stringsize] = 0;
1341
1342
191
      obj_aout_external_strings (abfd) = strings;
1343
191
      obj_aout_external_string_size (abfd) = stringsize;
1344
191
    }
1345
1346
299
  return true;
1347
382
}
1348
1349
/* Translate an a.out symbol into a BFD symbol.  The desc, other, type
1350
   and symbol->value fields of CACHE_PTR will be set from the a.out
1351
   nlist structure.  This function is responsible for setting
1352
   symbol->flags and symbol->section, and adjusting symbol->value.  */
1353
1354
static bool
1355
translate_from_native_sym_flags (bfd *abfd,
1356
         aout_symbol_type *cache_ptr)
1357
43.3k
{
1358
43.3k
  flagword visible;
1359
1360
43.3k
  if (is_stab (cache_ptr->type, cache_ptr->symbol.name))
1361
327
    {
1362
327
      asection *sec;
1363
1364
      /* This is a debugging symbol.  */
1365
327
      cache_ptr->symbol.flags = BSF_DEBUGGING;
1366
1367
      /* Work out the symbol section.  */
1368
327
      switch (cache_ptr->type)
1369
327
  {
1370
58
  case N_SO:
1371
68
  case N_SOL:
1372
70
  case N_FUN:
1373
86
  case N_ENTRY:
1374
112
  case N_SLINE:
1375
112
  case N_FN:
1376
112
    sec = obj_textsec (abfd);
1377
112
    break;
1378
27
  case N_STSYM:
1379
50
  case N_DSLINE:
1380
50
    sec = obj_datasec (abfd);
1381
50
    break;
1382
19
  case N_LCSYM:
1383
41
  case N_BSLINE:
1384
41
    sec = obj_bsssec (abfd);
1385
41
    break;
1386
124
  default:
1387
124
    sec = bfd_abs_section_ptr;
1388
124
    break;
1389
327
  }
1390
1391
327
      cache_ptr->symbol.section = sec;
1392
327
      cache_ptr->symbol.value -= sec->vma;
1393
1394
327
      return true;
1395
327
    }
1396
1397
  /* Get the default visibility.  This does not apply to all types, so
1398
     we just hold it in a local variable to use if wanted.  */
1399
42.9k
  if ((cache_ptr->type & N_EXT) == 0)
1400
42.7k
    visible = BSF_LOCAL;
1401
276
  else
1402
276
    visible = BSF_GLOBAL;
1403
1404
42.9k
  switch (cache_ptr->type)
1405
42.9k
    {
1406
42.4k
    default:
1407
42.5k
    case N_ABS: case N_ABS | N_EXT:
1408
42.5k
      cache_ptr->symbol.section = bfd_abs_section_ptr;
1409
42.5k
      cache_ptr->symbol.flags = visible;
1410
42.5k
      break;
1411
1412
94
    case N_UNDF | N_EXT:
1413
94
      if (cache_ptr->symbol.value != 0)
1414
43
  {
1415
    /* This is a common symbol.  */
1416
43
    cache_ptr->symbol.flags = BSF_GLOBAL;
1417
43
    cache_ptr->symbol.section = bfd_com_section_ptr;
1418
43
  }
1419
51
      else
1420
51
  {
1421
51
    cache_ptr->symbol.flags = 0;
1422
51
    cache_ptr->symbol.section = bfd_und_section_ptr;
1423
51
  }
1424
94
      break;
1425
1426
158
    case N_TEXT: case N_TEXT | N_EXT:
1427
158
      cache_ptr->symbol.section = obj_textsec (abfd);
1428
158
      cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1429
158
      cache_ptr->symbol.flags = visible;
1430
158
      break;
1431
1432
131
    case N_DATA: case N_DATA | N_EXT:
1433
131
      cache_ptr->symbol.section = obj_datasec (abfd);
1434
131
      cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1435
131
      cache_ptr->symbol.flags = visible;
1436
131
      break;
1437
1438
76
    case N_BSS: case N_BSS | N_EXT:
1439
76
      cache_ptr->symbol.section = obj_bsssec (abfd);
1440
76
      cache_ptr->symbol.value -= cache_ptr->symbol.section->vma;
1441
76
      cache_ptr->symbol.flags = visible;
1442
76
      break;
1443
42.9k
    }
1444
1445
42.9k
  return true;
1446
42.9k
}
1447
1448
/* Set the fields of SYM_POINTER according to CACHE_PTR.  */
1449
1450
static bool
1451
translate_to_native_sym_flags (bfd *abfd,
1452
             asymbol *cache_ptr,
1453
             struct external_nlist *sym_pointer)
1454
0
{
1455
0
  bfd_vma value = cache_ptr->value;
1456
0
  asection *sec;
1457
0
  bfd_vma off;
1458
0
  const char *name = cache_ptr->name != NULL ? cache_ptr->name : "*unknown*";
1459
1460
  /* Mask out any existing type bits in case copying from one section
1461
     to another.  */
1462
0
  if (!is_stab (sym_pointer->e_type[0], name))
1463
0
    sym_pointer->e_type[0] &= ~N_TYPE;
1464
1465
0
  sec = bfd_asymbol_section (cache_ptr);
1466
0
  off = 0;
1467
1468
0
  if (sec == NULL)
1469
0
    {
1470
      /* This case occurs, e.g., for the *DEBUG* section of a COFF
1471
   file.  */
1472
0
      _bfd_error_handler
1473
  /* xgettext:c-format */
1474
0
  (_("%pB: can not represent section for symbol `%s' in a.out object file format"),
1475
0
   abfd, name);
1476
0
      bfd_set_error (bfd_error_nonrepresentable_section);
1477
0
      return false;
1478
0
    }
1479
1480
0
  if (sec->output_section != NULL)
1481
0
    {
1482
0
      off = sec->output_offset;
1483
0
      sec = sec->output_section;
1484
0
    }
1485
1486
0
  if (bfd_is_abs_section (sec))
1487
0
    sym_pointer->e_type[0] |= N_ABS;
1488
0
  else if (sec == obj_textsec (abfd))
1489
0
    sym_pointer->e_type[0] |= N_TEXT;
1490
0
  else if (sec == obj_datasec (abfd))
1491
0
    sym_pointer->e_type[0] |= N_DATA;
1492
0
  else if (sec == obj_bsssec (abfd))
1493
0
    sym_pointer->e_type[0] |= N_BSS;
1494
0
  else if (bfd_is_und_section (sec))
1495
0
    sym_pointer->e_type[0] = N_UNDF | N_EXT;
1496
0
  else if (bfd_is_com_section (sec))
1497
0
    sym_pointer->e_type[0] = N_UNDF | N_EXT;
1498
0
  else
1499
0
    {
1500
0
      _bfd_error_handler
1501
  /* xgettext:c-format */
1502
0
  (_("%pB: can not represent section `%pA' in a.out object file format"),
1503
0
   abfd, sec);
1504
0
      bfd_set_error (bfd_error_nonrepresentable_section);
1505
0
      return false;
1506
0
    }
1507
1508
  /* Turn the symbol from section relative to absolute again */
1509
0
  value += sec->vma + off;
1510
1511
0
  if ((cache_ptr->flags & BSF_DEBUGGING) != 0)
1512
0
    sym_pointer->e_type[0] = ((aout_symbol_type *) cache_ptr)->type;
1513
0
  else if ((cache_ptr->flags & BSF_GLOBAL) != 0)
1514
0
    sym_pointer->e_type[0] |= N_EXT;
1515
1516
0
  PUT_WORD(abfd, value, sym_pointer->e_value);
1517
1518
0
  return true;
1519
0
}
1520

1521
/* Native-level interface to symbols. */
1522
1523
asymbol *
1524
NAME (aout, make_empty_symbol) (bfd *abfd)
1525
60.3k
{
1526
60.3k
  size_t amt = sizeof (aout_symbol_type);
1527
60.3k
  aout_symbol_type *new_symbol_type = bfd_zalloc (abfd, amt);
1528
1529
60.3k
  if (!new_symbol_type)
1530
0
    return NULL;
1531
60.3k
  new_symbol_type->symbol.the_bfd = abfd;
1532
1533
60.3k
  return &new_symbol_type->symbol;
1534
60.3k
}
1535
1536
/* Translate a set of external symbols into internal symbols.  */
1537
1538
bool
1539
NAME (aout, translate_symbol_table) (bfd *abfd,
1540
             aout_symbol_type *in,
1541
             struct external_nlist *ext,
1542
             bfd_size_type count,
1543
             char *str,
1544
             bfd_size_type strsize,
1545
             bool dynamic)
1546
199
{
1547
199
  struct external_nlist *ext_end;
1548
1549
199
  ext_end = ext + count;
1550
43.5k
  for (; ext < ext_end; ext++, in++)
1551
43.3k
    {
1552
43.3k
      bfd_vma x;
1553
43.3k
      int ovly;
1554
1555
43.3k
      x = GET_WORD (abfd, ext->e_strx);
1556
43.3k
      in->symbol.the_bfd = abfd;
1557
1558
      /* For the normal symbols, the zero index points at the number
1559
   of bytes in the string table but is to be interpreted as the
1560
   null string.  For the dynamic symbols, the number of bytes in
1561
   the string table is stored in the __DYNAMIC structure and the
1562
   zero index points at an actual string.  */
1563
43.3k
      if (x == 0 && ! dynamic)
1564
43.0k
  in->symbol.name = "";
1565
363
      else if (x < strsize)
1566
321
  in->symbol.name = str + x;
1567
42
      else
1568
42
  {
1569
42
    _bfd_error_handler
1570
42
      (_("%pB: invalid string offset %" PRIu64 " >= %" PRIu64),
1571
42
       abfd, (uint64_t) x, (uint64_t) strsize);
1572
42
    bfd_set_error (bfd_error_bad_value);
1573
42
    return false;
1574
42
  }
1575
1576
43.3k
      ovly = H_GET_8 (abfd, ext->e_ovly);
1577
43.3k
      if (ovly != 0)
1578
13
  {
1579
13
    _bfd_error_handler
1580
13
      (_("%pB: symbol indicates overlay (not supported)"), abfd);
1581
13
    bfd_set_error (bfd_error_bad_value);
1582
13
    return false;
1583
13
  }
1584
1585
43.3k
      in->symbol.value = GET_WORD (abfd,  ext->e_value);
1586
      /* e_desc is zero for normal symbols but for .stab symbols it
1587
   carries the desc field in our extended 2.11BSD format. */
1588
43.3k
      in->desc = H_GET_16 (abfd, ext->e_desc);
1589
43.3k
      in->other = 0;
1590
43.3k
      in->type = H_GET_8 (abfd,  ext->e_type);
1591
43.3k
      in->symbol.udata.p = NULL;
1592
1593
43.3k
      if (! translate_from_native_sym_flags (abfd, in))
1594
0
  return false;
1595
1596
43.3k
      if (dynamic)
1597
0
  in->symbol.flags |= BSF_DYNAMIC;
1598
43.3k
    }
1599
1600
144
  return true;
1601
199
}
1602
1603
/* We read the symbols into a buffer, which is discarded when this
1604
   function exits.  We read the strings into a buffer large enough to
1605
   hold them all plus all the cached symbol entries.  */
1606
1607
bool
1608
NAME (aout, slurp_symbol_table) (bfd *abfd)
1609
665
{
1610
665
  struct external_nlist *old_external_syms;
1611
665
  aout_symbol_type *cached;
1612
665
  bfd_size_type cached_size;
1613
1614
  /* If there's no work to be done, don't do any.  */
1615
665
  if (obj_aout_symbols (abfd) != NULL)
1616
264
    return true;
1617
1618
401
  old_external_syms = obj_aout_external_syms (abfd);
1619
1620
401
  if (! aout_get_external_symbols (abfd))
1621
202
    return false;
1622
1623
199
  cached_size = obj_aout_external_sym_count (abfd);
1624
199
  cached_size *= sizeof (aout_symbol_type);
1625
199
  cached = bfd_zmalloc (cached_size);
1626
199
  if (cached == NULL && cached_size != 0)
1627
0
    return false;
1628
1629
  /* Convert from external symbol information to internal.  */
1630
199
  if (! (NAME (aout, translate_symbol_table)
1631
199
   (abfd, cached,
1632
199
    obj_aout_external_syms (abfd),
1633
199
    obj_aout_external_sym_count (abfd),
1634
199
    obj_aout_external_strings (abfd),
1635
199
    obj_aout_external_string_size (abfd),
1636
199
    false)))
1637
55
    {
1638
55
      free (cached);
1639
55
      return false;
1640
55
    }
1641
1642
144
  abfd->symcount = obj_aout_external_sym_count (abfd);
1643
1644
144
  obj_aout_symbols (abfd) = cached;
1645
1646
  /* It is very likely that anybody who calls this function will not
1647
     want the external symbol information, so if it was allocated
1648
     because of our call to aout_get_external_symbols, we free it up
1649
     right away to save space.  */
1650
144
  if (old_external_syms == NULL
1651
144
      && obj_aout_external_syms (abfd) != NULL)
1652
38
    {
1653
38
      free (obj_aout_external_syms (abfd));
1654
38
      obj_aout_external_syms (abfd) = NULL;
1655
38
    }
1656
1657
144
  return true;
1658
199
}
1659

1660
/* We use a hash table when writing out symbols so that we only write
1661
   out a particular string once.  This helps particularly when the
1662
   linker writes out stabs debugging entries, because each different
1663
   contributing object file tends to have many duplicate stabs
1664
   strings.
1665
1666
   This hash table code breaks dbx on SunOS 4.1.3, so we don't do it
1667
   if BFD_TRADITIONAL_FORMAT is set.  */
1668
1669
/* Get the index of a string in a strtab, adding it if it is not
1670
   already present.  */
1671
1672
static inline bfd_size_type
1673
add_to_stringtab (bfd *abfd,
1674
      struct bfd_strtab_hash *tab,
1675
      const char *str,
1676
      bool copy)
1677
0
{
1678
0
  bool hash;
1679
0
  bfd_size_type str_index;
1680
1681
  /* An index of 0 always means the empty string.  */
1682
0
  if (str == 0 || *str == '\0')
1683
0
    return 0;
1684
1685
  /* Don't hash if BFD_TRADITIONAL_FORMAT is set, because SunOS dbx
1686
     doesn't understand a hashed string table.  */
1687
0
  hash = true;
1688
0
  if ((abfd->flags & BFD_TRADITIONAL_FORMAT) != 0)
1689
0
    hash = false;
1690
1691
0
  str_index = _bfd_stringtab_add (tab, str, hash, copy);
1692
1693
0
  if (str_index != (bfd_size_type) -1)
1694
    /* Add BYTES_IN_LONG to the return value to account for the
1695
       space taken up by the string table size.  */
1696
0
    str_index += BYTES_IN_LONG;
1697
1698
0
  return str_index;
1699
0
}
1700
1701
/* Write out a strtab.  ABFD is already at the right location in the
1702
   file.  */
1703
1704
static bool
1705
emit_stringtab (bfd *abfd, struct bfd_strtab_hash *tab)
1706
0
{
1707
0
  bfd_byte buffer[BYTES_IN_LONG];
1708
1709
  /* The string table starts with the size.  */
1710
0
  H_PUT_32 (abfd, _bfd_stringtab_size (tab) + BYTES_IN_LONG, buffer);
1711
0
  if (bfd_write (buffer, BYTES_IN_LONG, abfd) != BYTES_IN_LONG)
1712
0
    return false;
1713
1714
0
  return _bfd_stringtab_emit (abfd, tab);
1715
0
}
1716

1717
bool
1718
NAME (aout, write_syms) (bfd *abfd)
1719
0
{
1720
0
  unsigned int count ;
1721
0
  asymbol **generic = bfd_get_outsymbols (abfd);
1722
0
  struct bfd_strtab_hash *strtab;
1723
1724
0
  strtab = _bfd_stringtab_init ();
1725
0
  if (strtab == NULL)
1726
0
    return false;
1727
1728
0
  for (count = 0; count < bfd_get_symcount (abfd); count++)
1729
0
    {
1730
0
      asymbol *g = generic[count];
1731
0
      bfd_size_type indx;
1732
0
      struct external_nlist nsp;
1733
1734
0
      indx = add_to_stringtab (abfd, strtab, g->name, false);
1735
0
      if (indx == (bfd_size_type) -1)
1736
0
  goto error_return;
1737
0
      PUT_WORD (abfd, indx, nsp.e_strx);
1738
1739
0
      if (bfd_asymbol_flavour(g) == abfd->xvec->flavour)
1740
0
  {
1741
0
    H_PUT_16 (abfd, aout_symbol (g)->desc,  nsp.e_desc);
1742
0
    H_PUT_8 (abfd, 0, nsp.e_ovly);
1743
0
    H_PUT_8 (abfd, aout_symbol (g)->type,  nsp.e_type);
1744
0
  }
1745
0
      else
1746
0
  {
1747
0
    H_PUT_16 (abfd, 0, nsp.e_desc);
1748
0
    H_PUT_8 (abfd, 0, nsp.e_ovly);
1749
0
    H_PUT_8 (abfd, 0, nsp.e_type);
1750
0
  }
1751
1752
0
      if (! translate_to_native_sym_flags (abfd, g, &nsp))
1753
0
  goto error_return;
1754
1755
0
      if (bfd_write (&nsp, EXTERNAL_NLIST_SIZE, abfd) != EXTERNAL_NLIST_SIZE)
1756
0
  goto error_return;
1757
1758
      /* NB: `KEEPIT' currently overlays `udata.p', so set this only
1759
   here, at the end.  */
1760
0
      g->KEEPIT = count;
1761
0
    }
1762
1763
0
  if (! emit_stringtab (abfd, strtab))
1764
0
    goto error_return;
1765
1766
0
  _bfd_stringtab_free (strtab);
1767
1768
0
  return true;
1769
1770
0
 error_return:
1771
0
  _bfd_stringtab_free (strtab);
1772
0
  return false;
1773
0
}
1774
1775

1776
long
1777
NAME (aout, canonicalize_symtab) (bfd *abfd, asymbol **location)
1778
204
{
1779
204
  unsigned int counter = 0;
1780
204
  aout_symbol_type *symbase;
1781
1782
204
  if (!NAME (aout, slurp_symbol_table) (abfd))
1783
0
    return -1;
1784
1785
51.1k
  for (symbase = obj_aout_symbols (abfd); counter++ < bfd_get_symcount (abfd);)
1786
50.9k
    *(location++) = (asymbol *)(symbase++);
1787
204
  *location++ =0;
1788
204
  return bfd_get_symcount (abfd);
1789
204
}
1790
1791

1792
/* Output extended relocation information to a file in target byte order.  */
1793
1794
static void
1795
pdp11_aout_swap_reloc_out (bfd *abfd, arelent *g, bfd_byte *natptr)
1796
0
{
1797
0
  int r_index;
1798
0
  int r_pcrel;
1799
0
  int reloc_entry;
1800
0
  int r_type;
1801
0
  asymbol *sym = *(g->sym_ptr_ptr);
1802
0
  asection *output_section = sym->section->output_section;
1803
1804
0
  if (g->addend != 0)
1805
0
    fprintf (stderr, "BFD: can't do this reloc addend stuff\n");
1806
1807
0
  r_pcrel = g->howto->pc_relative;
1808
1809
0
  if (bfd_is_abs_section (output_section))
1810
0
    r_type = RABS;
1811
0
  else if (output_section == obj_textsec (abfd))
1812
0
    r_type = RTEXT;
1813
0
  else if (output_section == obj_datasec (abfd))
1814
0
    r_type = RDATA;
1815
0
  else if (output_section == obj_bsssec (abfd))
1816
0
    r_type = RBSS;
1817
0
  else if (bfd_is_und_section (output_section))
1818
0
    r_type = REXT;
1819
0
  else if (bfd_is_com_section (output_section))
1820
0
    r_type = REXT;
1821
0
  else
1822
0
    r_type = -1;
1823
1824
0
  BFD_ASSERT (r_type != -1);
1825
1826
0
  if (r_type == RABS)
1827
0
    r_index = 0;
1828
0
  else
1829
0
    r_index = (*(g->sym_ptr_ptr))->KEEPIT;
1830
1831
0
  reloc_entry = r_index << 4 | r_type | r_pcrel;
1832
1833
0
  PUT_WORD (abfd, reloc_entry, natptr);
1834
0
}
1835
1836
/* BFD deals internally with all things based from the section they're
1837
   in. so, something in 10 bytes into a text section  with a base of
1838
   50 would have a symbol (.text+10) and know .text vma was 50.
1839
1840
   Aout keeps all it's symbols based from zero, so the symbol would
1841
   contain 60. This macro subs the base of each section from the value
1842
   to give the true offset from the section */
1843
1844
1845
#define MOVE_ADDRESS(ad)            \
1846
40.2k
  if (r_extern)               \
1847
40.2k
    {                 \
1848
876
      /* Undefined symbol.  */            \
1849
876
      if (symbols != NULL && r_index < bfd_get_symcount (abfd))   \
1850
876
  cache_ptr->sym_ptr_ptr = symbols + r_index;     \
1851
876
      else                \
1852
876
  cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
1853
876
      cache_ptr->addend = ad;           \
1854
876
    }                  \
1855
40.2k
  else                  \
1856
40.2k
    {                 \
1857
39.3k
      /* Defined, section relative. replace symbol with pointer to  \
1858
39.3k
   symbol which points to section.  */        \
1859
39.3k
      switch (r_index)              \
1860
39.3k
  {               \
1861
288
  case N_TEXT:             \
1862
301
  case N_TEXT | N_EXT:           \
1863
301
    cache_ptr->sym_ptr_ptr  = obj_textsec (abfd)->symbol_ptr_ptr;  \
1864
301
    cache_ptr->addend = ad  - su->textsec->vma;     \
1865
301
    break;             \
1866
288
  case N_DATA:             \
1867
99
  case N_DATA | N_EXT:           \
1868
99
    cache_ptr->sym_ptr_ptr  = obj_datasec (abfd)->symbol_ptr_ptr;  \
1869
99
    cache_ptr->addend = ad - su->datasec->vma;      \
1870
99
    break;             \
1871
191
  case N_BSS:             \
1872
220
  case N_BSS | N_EXT:           \
1873
220
    cache_ptr->sym_ptr_ptr  = obj_bsssec (abfd)->symbol_ptr_ptr;  \
1874
220
    cache_ptr->addend = ad - su->bsssec->vma;     \
1875
220
    break;             \
1876
21.5k
  default:              \
1877
38.7k
  case N_ABS:             \
1878
38.7k
  case N_ABS | N_EXT:           \
1879
38.7k
    cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
1880
38.7k
    cache_ptr->addend = ad;         \
1881
38.7k
    break;             \
1882
39.3k
  }               \
1883
39.3k
    }
1884
1885
static void
1886
pdp11_aout_swap_reloc_in (bfd *    abfd,
1887
        bfd_byte *   bytes,
1888
        arelent *  cache_ptr,
1889
        bfd_size_type  offset,
1890
        asymbol **   symbols,
1891
        bfd_size_type  symcount)
1892
40.2k
{
1893
40.2k
  struct aoutdata *su = &(abfd->tdata.aout_data->a);
1894
40.2k
  unsigned int r_index;
1895
40.2k
  int reloc_entry;
1896
40.2k
  int r_extern;
1897
40.2k
  int r_pcrel;
1898
1899
40.2k
  reloc_entry = GET_WORD (abfd, (void *) bytes);
1900
1901
40.2k
  r_pcrel = reloc_entry & RELFLG;
1902
1903
40.2k
  cache_ptr->address = offset;
1904
40.2k
  cache_ptr->howto = howto_table_pdp11 + (r_pcrel ? 1 : 0);
1905
1906
40.2k
  if ((reloc_entry & RTYPE) == RABS)
1907
13.6k
    r_index = N_ABS;
1908
26.5k
  else
1909
26.5k
    r_index = RINDEX (reloc_entry);
1910
1911
  /* r_extern reflects whether the symbol the reloc is against is
1912
     local or global.  */
1913
40.2k
  r_extern = (reloc_entry & RTYPE) == REXT;
1914
1915
40.2k
  if (r_extern && r_index >= symcount)
1916
2.81k
    {
1917
      /* We could arrange to return an error, but it might be useful
1918
   to see the file even if it is bad.  FIXME: Of course this
1919
   means that objdump -r *doesn't* see the actual reloc, and
1920
   objcopy silently writes a different reloc.  */
1921
2.81k
      r_extern = 0;
1922
2.81k
      r_index = N_ABS;
1923
2.81k
    }
1924
1925
40.2k
  MOVE_ADDRESS(0);
1926
40.2k
}
1927
1928
/* Read and swap the relocs for a section.  */
1929
1930
bool
1931
NAME (aout, slurp_reloc_table) (bfd *abfd, sec_ptr asect, asymbol **symbols)
1932
276
{
1933
276
  bfd_byte *rptr;
1934
276
  bfd_size_type count;
1935
276
  bfd_size_type reloc_size;
1936
276
  void * relocs;
1937
276
  arelent *reloc_cache;
1938
276
  size_t each_size;
1939
276
  unsigned int counter = 0;
1940
276
  arelent *cache_ptr;
1941
1942
276
  if (asect->relocation)
1943
0
    return true;
1944
1945
276
  if (asect->flags & SEC_CONSTRUCTOR)
1946
0
    return true;
1947
1948
276
  if (asect == obj_datasec (abfd))
1949
115
    reloc_size = exec_hdr(abfd)->a_drsize;
1950
161
  else if (asect == obj_textsec (abfd))
1951
161
    reloc_size = exec_hdr(abfd)->a_trsize;
1952
0
  else if (asect == obj_bsssec (abfd))
1953
0
    reloc_size = 0;
1954
0
  else
1955
0
    {
1956
0
      bfd_set_error (bfd_error_invalid_operation);
1957
0
      return false;
1958
0
    }
1959
1960
276
  if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
1961
0
    return false;
1962
276
  relocs = _bfd_malloc_and_read (abfd, reloc_size, reloc_size);
1963
276
  if (relocs == NULL && reloc_size != 0)
1964
108
    return false;
1965
1966
168
  each_size = obj_reloc_entry_size (abfd);
1967
168
  count = reloc_size / each_size;
1968
1969
  /* Count the number of NON-ZERO relocs, this is the count we want.  */
1970
168
  {
1971
168
    unsigned int real_count = 0;
1972
1973
62.0k
    for (counter = 0; counter < count; counter++)
1974
61.8k
      {
1975
61.8k
  int x;
1976
1977
61.8k
  x = GET_WORD (abfd, (char *) relocs + each_size * counter);
1978
61.8k
  if (x != 0)
1979
40.2k
    real_count++;
1980
61.8k
      }
1981
1982
168
    count = real_count;
1983
168
  }
1984
1985
168
  reloc_cache = bfd_zmalloc (count * sizeof (arelent));
1986
168
  if (reloc_cache == NULL && count != 0)
1987
0
    return false;
1988
1989
168
  cache_ptr = reloc_cache;
1990
1991
168
  rptr = relocs;
1992
168
  for (counter = 0;
1993
40.3k
       counter < count;
1994
40.2k
       counter++, rptr += RELOC_SIZE, cache_ptr++)
1995
40.2k
    {
1996
61.5k
      while (GET_WORD (abfd, (void *) rptr) == 0)
1997
21.2k
  {
1998
21.2k
    rptr += RELOC_SIZE;
1999
21.2k
    if ((char *) rptr >= (char *) relocs + reloc_size)
2000
0
      goto done;
2001
21.2k
  }
2002
2003
40.2k
      pdp11_aout_swap_reloc_in (abfd, rptr, cache_ptr,
2004
40.2k
        (bfd_size_type) ((char *) rptr - (char *) relocs),
2005
40.2k
        symbols,
2006
40.2k
        (bfd_size_type) bfd_get_symcount (abfd));
2007
40.2k
    }
2008
168
 done:
2009
  /* Just in case, if rptr >= relocs + reloc_size should happen
2010
     too early.  */
2011
168
  BFD_ASSERT (counter == count);
2012
2013
168
  free (relocs);
2014
2015
168
  asect->relocation = reloc_cache;
2016
168
  asect->reloc_count = cache_ptr - reloc_cache;
2017
2018
168
  return true;
2019
168
}
2020
2021
/* Write out a relocation section into an object file.  */
2022
2023
bool
2024
NAME (aout, squirt_out_relocs) (bfd *abfd, asection *section)
2025
0
{
2026
0
  arelent **generic;
2027
0
  unsigned char *native;
2028
0
  unsigned int count = section->reloc_count;
2029
0
  bfd_size_type natsize;
2030
2031
0
  natsize = section->size;
2032
0
  native = bfd_zalloc (abfd, natsize);
2033
0
  if (!native)
2034
0
    return false;
2035
2036
0
  generic = section->orelocation;
2037
0
  if (generic != NULL)
2038
0
    {
2039
0
      while (count > 0)
2040
0
  {
2041
0
    bfd_byte *r;
2042
2043
0
    if ((*generic)->howto == NULL
2044
0
        || (*generic)->sym_ptr_ptr == NULL)
2045
0
      {
2046
0
        bfd_set_error (bfd_error_invalid_operation);
2047
0
        _bfd_error_handler (_("%pB: attempt to write out "
2048
0
            "unknown reloc type"), abfd);
2049
0
        bfd_release (abfd, native);
2050
0
        return false;
2051
0
      }
2052
0
    r = native + (*generic)->address;
2053
0
    pdp11_aout_swap_reloc_out (abfd, *generic, r);
2054
0
    count--;
2055
0
    generic++;
2056
0
  }
2057
0
    }
2058
2059
0
  if (bfd_write (native, natsize, abfd) != natsize)
2060
0
    {
2061
0
      bfd_release (abfd, native);
2062
0
      return false;
2063
0
    }
2064
2065
0
  bfd_release (abfd, native);
2066
0
  return true;
2067
0
}
2068
2069
/* This is stupid.  This function should be a boolean predicate.  */
2070
2071
long
2072
NAME (aout, canonicalize_reloc) (bfd *abfd,
2073
         sec_ptr section,
2074
         arelent **relptr,
2075
         asymbol **symbols)
2076
276
{
2077
276
  arelent *tblptr = section->relocation;
2078
276
  unsigned int count;
2079
2080
276
  if (section == obj_bsssec (abfd))
2081
0
    {
2082
0
      *relptr = NULL;
2083
0
      return 0;
2084
0
    }
2085
2086
276
  if (!(tblptr || NAME (aout, slurp_reloc_table)(abfd, section, symbols)))
2087
108
    return -1;
2088
2089
168
  if (section->flags & SEC_CONSTRUCTOR)
2090
0
    {
2091
0
      arelent_chain *chain = section->constructor_chain;
2092
2093
0
      for (count = 0; count < section->reloc_count; count ++)
2094
0
  {
2095
0
    *relptr ++ = &chain->relent;
2096
0
    chain = chain->next;
2097
0
  }
2098
0
    }
2099
168
  else
2100
168
    {
2101
168
      tblptr = section->relocation;
2102
2103
40.3k
      for (count = 0; count++ < section->reloc_count;)
2104
40.2k
  *relptr++ = tblptr++;
2105
168
    }
2106
2107
168
  *relptr = 0;
2108
2109
168
  return section->reloc_count;
2110
276
}
2111
2112
long
2113
NAME (aout, get_reloc_upper_bound) (bfd *abfd, sec_ptr asect)
2114
322
{
2115
322
  size_t count, raw;
2116
2117
322
  if (asect->flags & SEC_CONSTRUCTOR)
2118
0
    count = asect->reloc_count;
2119
322
  else if (asect == obj_datasec (abfd))
2120
143
    count = exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
2121
179
  else if (asect == obj_textsec (abfd))
2122
179
    count = exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
2123
0
  else if (asect == obj_bsssec (abfd))
2124
0
    count = 0;
2125
0
  else
2126
0
    {
2127
0
      bfd_set_error (bfd_error_invalid_operation);
2128
0
      return -1;
2129
0
    }
2130
2131
322
  if (count >= LONG_MAX / sizeof (arelent *)
2132
322
      || _bfd_mul_overflow (count, obj_reloc_entry_size (abfd), &raw))
2133
0
    {
2134
0
      bfd_set_error (bfd_error_file_too_big);
2135
0
      return -1;
2136
0
    }
2137
322
  if (!bfd_write_p (abfd))
2138
322
    {
2139
322
      ufile_ptr filesize = bfd_get_file_size (abfd);
2140
322
      if (filesize != 0 && raw > filesize)
2141
46
  {
2142
46
    bfd_set_error (bfd_error_file_truncated);
2143
46
    return -1;
2144
46
  }
2145
322
    }
2146
276
  return (count + 1) * sizeof (arelent *);
2147
322
}
2148
2149

2150
long
2151
NAME (aout, get_symtab_upper_bound) (bfd *abfd)
2152
461
{
2153
461
  if (!NAME (aout, slurp_symbol_table) (abfd))
2154
257
    return -1;
2155
2156
204
  return (bfd_get_symcount (abfd) + 1) * (sizeof (aout_symbol_type *));
2157
461
}
2158
2159
alent *
2160
NAME (aout, get_lineno) (bfd * abfd ATTRIBUTE_UNUSED,
2161
       asymbol * symbol ATTRIBUTE_UNUSED)
2162
0
{
2163
0
  return NULL;
2164
0
}
2165
2166
void
2167
NAME (aout, get_symbol_info) (bfd * abfd ATTRIBUTE_UNUSED,
2168
            asymbol *symbol,
2169
            symbol_info *ret)
2170
16.8k
{
2171
16.8k
  bfd_symbol_info (symbol, ret);
2172
2173
16.8k
  if (ret->type == '?')
2174
23
    {
2175
23
      int type_code = aout_symbol(symbol)->type & 0xff;
2176
23
      const char *stab_name = bfd_get_stab_name (type_code);
2177
23
      static char buf[10];
2178
2179
23
      if (stab_name == NULL)
2180
7
  {
2181
7
    sprintf(buf, "(%d)", type_code);
2182
7
    stab_name = buf;
2183
7
  }
2184
23
      ret->type = '-';
2185
23
      ret->stab_type  = type_code;
2186
23
      ret->stab_other = (unsigned) (aout_symbol(symbol)->other & 0xff);
2187
23
      ret->stab_desc  = (unsigned) (aout_symbol(symbol)->desc & 0xffff);
2188
23
      ret->stab_name  = stab_name;
2189
23
    }
2190
16.8k
}
2191
2192
void
2193
NAME (aout, print_symbol) (bfd * abfd,
2194
         void * afile,
2195
         asymbol *symbol,
2196
         bfd_print_symbol_type how)
2197
0
{
2198
0
  FILE *file = (FILE *) afile;
2199
2200
0
  switch (how)
2201
0
    {
2202
0
    case bfd_print_symbol_name:
2203
0
      if (symbol->name)
2204
0
  fprintf(file,"%s", symbol->name);
2205
0
      break;
2206
0
    case bfd_print_symbol_more:
2207
0
      fprintf(file,"%4x %2x %2x",
2208
0
        (unsigned) (aout_symbol (symbol)->desc & 0xffff),
2209
0
        (unsigned) (aout_symbol (symbol)->other & 0xff),
2210
0
        (unsigned) (aout_symbol (symbol)->type));
2211
0
      break;
2212
0
    case bfd_print_symbol_all:
2213
0
      {
2214
0
  const char *section_name = symbol->section->name;
2215
2216
0
  bfd_print_symbol_vandf (abfd, (void *) file, symbol);
2217
2218
0
  fprintf (file," %-5s %04x %02x %02x",
2219
0
     section_name,
2220
0
     (unsigned) (aout_symbol (symbol)->desc & 0xffff),
2221
0
     (unsigned) (aout_symbol (symbol)->other & 0xff),
2222
0
     (unsigned) (aout_symbol (symbol)->type  & 0xff));
2223
0
  if (symbol->name)
2224
0
    fprintf(file," %s", symbol->name);
2225
0
      }
2226
0
      break;
2227
0
    }
2228
0
}
2229
2230
/* If we don't have to allocate more than 1MB to hold the generic
2231
   symbols, we use the generic minisymbol method: it's faster, since
2232
   it only translates the symbols once, not multiple times.  */
2233
257k
#define MINISYM_THRESHOLD (1000000 / sizeof (asymbol))
2234
2235
/* Read minisymbols.  For minisymbols, we use the unmodified a.out
2236
   symbols.  The minisymbol_to_symbol function translates these into
2237
   BFD asymbol structures.  */
2238
2239
long
2240
NAME (aout, read_minisymbols) (bfd *abfd,
2241
             bool dynamic,
2242
             void * *minisymsp,
2243
             unsigned int *sizep)
2244
178
{
2245
178
  if (dynamic)
2246
    /* We could handle the dynamic symbols here as well, but it's
2247
       easier to hand them off.  */
2248
0
    return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2249
2250
178
  if (! aout_get_external_symbols (abfd))
2251
67
    return -1;
2252
2253
111
  if (obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2254
111
    return _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep);
2255
2256
0
  *minisymsp = (void *) obj_aout_external_syms (abfd);
2257
2258
  /* By passing the external symbols back from this routine, we are
2259
     giving up control over the memory block.  Clear
2260
     obj_aout_external_syms, so that we do not try to free it
2261
     ourselves.  */
2262
0
  obj_aout_external_syms (abfd) = NULL;
2263
2264
0
  *sizep = EXTERNAL_NLIST_SIZE;
2265
0
  return obj_aout_external_sym_count (abfd);
2266
111
}
2267
2268
/* Convert a minisymbol to a BFD asymbol.  A minisymbol is just an
2269
   unmodified a.out symbol.  The SYM argument is a structure returned
2270
   by bfd_make_empty_symbol, which we fill in here.  */
2271
2272
asymbol *
2273
NAME (aout, minisymbol_to_symbol) (bfd *abfd,
2274
           bool dynamic,
2275
           const void * minisym,
2276
           asymbol *sym)
2277
257k
{
2278
257k
  if (dynamic
2279
257k
      || obj_aout_external_sym_count (abfd) < MINISYM_THRESHOLD)
2280
257k
    return _bfd_generic_minisymbol_to_symbol (abfd, dynamic, minisym, sym);
2281
2282
0
  memset (sym, 0, sizeof (aout_symbol_type));
2283
2284
  /* We call translate_symbol_table to translate a single symbol.  */
2285
0
  if (! (NAME (aout, translate_symbol_table)
2286
0
   (abfd,
2287
0
    (aout_symbol_type *) sym,
2288
0
    (struct external_nlist *) minisym,
2289
0
    (bfd_size_type) 1,
2290
0
    obj_aout_external_strings (abfd),
2291
0
    obj_aout_external_string_size (abfd),
2292
0
    false)))
2293
0
    return NULL;
2294
2295
0
  return sym;
2296
0
}
2297
2298
/* Provided a BFD, a section and an offset into the section, calculate
2299
   and return the name of the source file and the line nearest to the
2300
   wanted location.  */
2301
2302
bool
2303
NAME (aout, find_nearest_line) (bfd *abfd,
2304
        asymbol **symbols,
2305
        asection *section,
2306
        bfd_vma offset,
2307
        const char **filename_ptr,
2308
        const char **functionname_ptr,
2309
        unsigned int *line_ptr,
2310
        unsigned int *discriminator_ptr)
2311
189
{
2312
  /* Run down the file looking for the filename, function and linenumber.  */
2313
189
  asymbol **p;
2314
189
  const char *directory_name = NULL;
2315
189
  const char *main_file_name = NULL;
2316
189
  const char *current_file_name = NULL;
2317
189
  const char *line_file_name = NULL; /* Value of current_file_name at line number.  */
2318
189
  bfd_vma low_line_vma = 0;
2319
189
  bfd_vma low_func_vma = 0;
2320
189
  asymbol *func = 0;
2321
189
  size_t filelen, funclen;
2322
189
  char *buf;
2323
2324
189
  *filename_ptr = bfd_get_filename (abfd);
2325
189
  *functionname_ptr = NULL;
2326
189
  *line_ptr = 0;
2327
189
  if (discriminator_ptr)
2328
61
    *discriminator_ptr = 0;
2329
2330
189
  if (symbols != NULL)
2331
134
    {
2332
1.10k
      for (p = symbols; *p; p++)
2333
1.00k
  {
2334
1.00k
    aout_symbol_type  *q = (aout_symbol_type *)(*p);
2335
1.03k
  next:
2336
1.03k
    switch (q->type)
2337
1.03k
      {
2338
197
      case N_TEXT:
2339
        /* If this looks like a file name symbol, and it comes after
2340
     the line number we have found so far, but before the
2341
     offset, then we have probably not found the right line
2342
     number.  */
2343
197
        if (q->symbol.value <= offset
2344
197
      && ((q->symbol.value > low_line_vma
2345
129
           && (line_file_name != NULL
2346
100
         || *line_ptr != 0))
2347
129
          || (q->symbol.value > low_func_vma
2348
88
        && func != NULL)))
2349
54
    {
2350
54
      const char * symname;
2351
2352
54
      symname = q->symbol.name;
2353
2354
54
      if (symname != NULL
2355
54
          && strlen (symname) > 2
2356
54
          && strcmp (symname + strlen (symname) - 2, ".o") == 0)
2357
13
        {
2358
13
          if (q->symbol.value > low_line_vma)
2359
10
      {
2360
10
        *line_ptr = 0;
2361
10
        line_file_name = NULL;
2362
10
      }
2363
13
          if (q->symbol.value > low_func_vma)
2364
7
      func = NULL;
2365
13
        }
2366
54
    }
2367
197
        break;
2368
2369
45
      case N_SO:
2370
        /* If this symbol is less than the offset, but greater than
2371
     the line number we have found so far, then we have not
2372
     found the right line number.  */
2373
45
        if (q->symbol.value <= offset)
2374
34
    {
2375
34
      if (q->symbol.value > low_line_vma)
2376
22
        {
2377
22
          *line_ptr = 0;
2378
22
          line_file_name = NULL;
2379
22
        }
2380
34
      if (q->symbol.value > low_func_vma)
2381
19
        func = NULL;
2382
34
    }
2383
2384
45
        main_file_name = current_file_name = q->symbol.name;
2385
        /* Look ahead to next symbol to check if that too is an N_SO.  */
2386
45
        p++;
2387
45
        if (*p == NULL)
2388
17
    goto done;
2389
28
        q = (aout_symbol_type *)(*p);
2390
28
        if (q->type != (int) N_SO)
2391
21
    goto next;
2392
2393
        /* Found a second N_SO  First is directory; second is filename.  */
2394
7
        directory_name = current_file_name;
2395
7
        main_file_name = current_file_name = q->symbol.name;
2396
7
        if (obj_textsec(abfd) != section)
2397
7
    goto done;
2398
0
        break;
2399
5
      case N_SOL:
2400
5
        current_file_name = q->symbol.name;
2401
5
        break;
2402
2403
8
      case N_SLINE:
2404
34
      case N_DSLINE:
2405
61
      case N_BSLINE:
2406
        /* We'll keep this if it resolves nearer than the one we have
2407
     already.  */
2408
61
        if (q->symbol.value >= low_line_vma
2409
61
      && q->symbol.value <= offset)
2410
35
    {
2411
35
      *line_ptr = q->desc;
2412
35
      low_line_vma = q->symbol.value;
2413
35
      line_file_name = current_file_name;
2414
35
    }
2415
61
        break;
2416
2417
63
      case N_FUN:
2418
63
        {
2419
    /* We'll keep this if it is nearer than the one we have already.  */
2420
63
    if (q->symbol.value >= low_func_vma &&
2421
63
        q->symbol.value <= offset)
2422
40
      {
2423
40
        low_func_vma = q->symbol.value;
2424
40
        func = (asymbol *) q;
2425
40
      }
2426
23
    else if (q->symbol.value > offset)
2427
13
      goto done;
2428
63
        }
2429
50
        break;
2430
1.03k
      }
2431
1.03k
  }
2432
134
    }
2433
2434
189
 done:
2435
189
  if (*line_ptr != 0)
2436
21
    main_file_name = line_file_name;
2437
2438
189
  if (main_file_name == NULL
2439
189
      || main_file_name[0] == '/'
2440
189
      || directory_name == NULL)
2441
182
    filelen = 0;
2442
7
  else
2443
7
    filelen = strlen (directory_name) + strlen (main_file_name);
2444
189
  if (func == NULL)
2445
158
    funclen = 0;
2446
31
  else
2447
31
    funclen = strlen (bfd_asymbol_name (func));
2448
2449
189
  free (adata (abfd).line_buf);
2450
189
  if (filelen + funclen == 0)
2451
177
    adata (abfd).line_buf = buf = NULL;
2452
12
  else
2453
12
    {
2454
12
      buf = bfd_malloc ((bfd_size_type) filelen + funclen + 3);
2455
12
      adata (abfd).line_buf = buf;
2456
12
      if (buf == NULL)
2457
0
  return false;
2458
12
    }
2459
2460
189
  if (main_file_name != NULL)
2461
36
    {
2462
36
      if (main_file_name[0] == '/' || directory_name == NULL)
2463
29
  *filename_ptr = main_file_name;
2464
7
      else
2465
7
  {
2466
7
    if (buf == NULL)
2467
      /* PR binutils/20891: In a corrupt input file both
2468
         main_file_name and directory_name can be empty...  */
2469
6
      * filename_ptr = NULL;
2470
1
    else
2471
1
      {
2472
1
        snprintf (buf, filelen + 1, "%s%s", directory_name,
2473
1
      main_file_name);
2474
1
        *filename_ptr = buf;
2475
1
        buf += filelen + 1;
2476
1
      }
2477
7
  }
2478
36
    }
2479
2480
189
  if (func)
2481
31
    {
2482
31
      const char *function = func->name;
2483
31
      char *colon;
2484
2485
31
      if (buf == NULL)
2486
19
  {
2487
    /* PR binutils/20892: In a corrupt input file func can be empty.  */
2488
19
    * functionname_ptr = NULL;
2489
19
    return true;
2490
19
  }
2491
      /* The caller expects a symbol name.  We actually have a
2492
   function name, without the leading underscore.  Put the
2493
   underscore back in, so that the caller gets a symbol name.  */
2494
12
      if (bfd_get_symbol_leading_char (abfd) == '\0')
2495
0
  strcpy (buf, function);
2496
12
      else
2497
12
  {
2498
12
    buf[0] = bfd_get_symbol_leading_char (abfd);
2499
12
    strcpy (buf + 1, function);
2500
12
  }
2501
2502
      /* Have to remove : stuff.  */
2503
12
      colon = strchr (buf, ':');
2504
12
      if (colon != NULL)
2505
1
  *colon = '\0';
2506
12
      *functionname_ptr = buf;
2507
12
    }
2508
2509
170
  return true;
2510
189
}
2511
2512
int
2513
NAME (aout, sizeof_headers) (bfd *abfd,
2514
           struct bfd_link_info *info ATTRIBUTE_UNUSED)
2515
0
{
2516
0
  return adata (abfd).exec_bytes_size;
2517
0
}
2518
2519
/* Throw away most malloc'd and alloc'd information for this BFD.  */
2520
2521
bool
2522
NAME (aout, bfd_free_cached_info) (bfd *abfd)
2523
25.2k
{
2524
25.2k
  if ((bfd_get_format (abfd) == bfd_object
2525
25.2k
       || bfd_get_format (abfd) == bfd_core)
2526
25.2k
      && abfd->tdata.aout_data != NULL)
2527
13.6k
    {
2528
95.7k
#define BFCI_FREE(x) do { free (x); x = NULL; } while (0)
2529
13.6k
      BFCI_FREE (adata (abfd).line_buf);
2530
13.6k
      BFCI_FREE (obj_aout_symbols (abfd));
2531
13.6k
      BFCI_FREE (obj_aout_external_syms (abfd));
2532
13.6k
      BFCI_FREE (obj_aout_external_strings (abfd));
2533
54.7k
      for (asection *o = abfd->sections; o != NULL; o = o->next)
2534
41.0k
  BFCI_FREE (o->relocation);
2535
13.6k
#undef BFCI_FREE
2536
13.6k
    }
2537
2538
25.2k
  return _bfd_generic_bfd_free_cached_info (abfd);
2539
25.2k
}
2540

2541
/* Routine to create an entry in an a.out link hash table.  */
2542
2543
struct bfd_hash_entry *
2544
NAME (aout, link_hash_newfunc) (struct bfd_hash_entry *entry,
2545
        struct bfd_hash_table *table,
2546
        const char *string)
2547
0
{
2548
0
  struct aout_link_hash_entry *ret = (struct aout_link_hash_entry *) entry;
2549
2550
  /* Allocate the structure if it has not already been allocated by a
2551
     subclass.  */
2552
0
  if (ret == NULL)
2553
0
    ret = bfd_hash_allocate (table, sizeof (* ret));
2554
0
  if (ret == NULL)
2555
0
    return NULL;
2556
2557
  /* Call the allocation method of the superclass.  */
2558
0
  ret = (struct aout_link_hash_entry *)
2559
0
   _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
2560
0
  if (ret)
2561
0
    {
2562
      /* Set local fields.  */
2563
0
      ret->written = false;
2564
0
      ret->indx = -1;
2565
0
    }
2566
2567
0
  return (struct bfd_hash_entry *) ret;
2568
0
}
2569
2570
/* Initialize an a.out link hash table.  */
2571
2572
bool
2573
NAME (aout, link_hash_table_init) (struct aout_link_hash_table *table,
2574
           bfd *abfd,
2575
           struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
2576
                     struct bfd_hash_table *,
2577
                     const char *),
2578
           unsigned int entsize)
2579
0
{
2580
0
  return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
2581
0
}
2582
2583
/* Create an a.out link hash table.  */
2584
2585
struct bfd_link_hash_table *
2586
NAME (aout, link_hash_table_create) (bfd *abfd)
2587
0
{
2588
0
  struct aout_link_hash_table *ret;
2589
0
  size_t amt = sizeof (struct aout_link_hash_table);
2590
2591
0
  ret = bfd_malloc (amt);
2592
0
  if (ret == NULL)
2593
0
    return NULL;
2594
0
  if (! NAME (aout, link_hash_table_init) (ret, abfd,
2595
0
             NAME (aout, link_hash_newfunc),
2596
0
             sizeof (struct aout_link_hash_entry)))
2597
0
    {
2598
0
      free (ret);
2599
0
      return NULL;
2600
0
    }
2601
0
  return &ret->root;
2602
0
}
2603
2604
/* Free up the internal symbols read from an a.out file.  */
2605
2606
static bool
2607
aout_link_free_symbols (bfd *abfd)
2608
0
{
2609
0
  if (obj_aout_external_syms (abfd) != NULL)
2610
0
    {
2611
0
      free ((void *) obj_aout_external_syms (abfd));
2612
0
      obj_aout_external_syms (abfd) = NULL;
2613
0
    }
2614
2615
0
  if (obj_aout_external_strings (abfd) != NULL)
2616
0
    {
2617
0
      free ((void *) obj_aout_external_strings (abfd));
2618
0
      obj_aout_external_strings (abfd) = NULL;
2619
0
    }
2620
0
  return true;
2621
0
}
2622
2623
/* Given an a.out BFD, add symbols to the global hash table as
2624
   appropriate.  */
2625
2626
bool
2627
NAME (aout, link_add_symbols) (bfd *abfd, struct bfd_link_info *info)
2628
0
{
2629
0
  switch (bfd_get_format (abfd))
2630
0
    {
2631
0
    case bfd_object:
2632
0
      return aout_link_add_object_symbols (abfd, info);
2633
0
    case bfd_archive:
2634
0
      return _bfd_generic_link_add_archive_symbols
2635
0
  (abfd, info, aout_link_check_archive_element);
2636
0
    default:
2637
0
      bfd_set_error (bfd_error_wrong_format);
2638
0
      return false;
2639
0
    }
2640
0
}
2641
2642
/* Add symbols from an a.out object file.  */
2643
2644
static bool
2645
aout_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
2646
0
{
2647
0
  if (! aout_get_external_symbols (abfd))
2648
0
    return false;
2649
0
  if (! aout_link_add_symbols (abfd, info))
2650
0
    return false;
2651
0
  if (! info->keep_memory)
2652
0
    {
2653
0
      if (! aout_link_free_symbols (abfd))
2654
0
  return false;
2655
0
    }
2656
0
  return true;
2657
0
}
2658
2659
/* Look through the internal symbols to see if this object file should
2660
   be included in the link.  We should include this object file if it
2661
   defines any symbols which are currently undefined.  If this object
2662
   file defines a common symbol, then we may adjust the size of the
2663
   known symbol but we do not include the object file in the link
2664
   (unless there is some other reason to include it).  */
2665
2666
static bool
2667
aout_link_check_ar_symbols (bfd *abfd,
2668
          struct bfd_link_info *info,
2669
          bool *pneeded,
2670
          bfd **subsbfd)
2671
0
{
2672
0
  struct external_nlist *p;
2673
0
  struct external_nlist *pend;
2674
0
  char *strings;
2675
2676
0
  *pneeded = false;
2677
2678
  /* Look through all the symbols.  */
2679
0
  p = obj_aout_external_syms (abfd);
2680
0
  pend = p + obj_aout_external_sym_count (abfd);
2681
0
  strings = obj_aout_external_strings (abfd);
2682
0
  for (; p < pend; p++)
2683
0
    {
2684
0
      int type = H_GET_8 (abfd, p->e_type);
2685
0
      const char *name = strings + GET_WORD (abfd, p->e_strx);
2686
0
      struct bfd_link_hash_entry *h;
2687
2688
      /* Ignore symbols that are not externally visible.  This is an
2689
   optimization only, as we check the type more thoroughly
2690
   below.  */
2691
0
      if ((type & N_EXT) == 0
2692
0
    || is_stab(type, name)
2693
0
    || type == N_FN)
2694
0
  continue;
2695
2696
0
      h = bfd_link_hash_lookup (info->hash, name, false, false, true);
2697
2698
      /* We are only interested in symbols that are currently
2699
   undefined or common.  */
2700
0
      if (h == NULL
2701
0
    || (h->type != bfd_link_hash_undefined
2702
0
        && h->type != bfd_link_hash_common))
2703
0
  continue;
2704
2705
0
      if (type == (N_TEXT | N_EXT)
2706
0
    || type == (N_DATA | N_EXT)
2707
0
    || type == (N_BSS | N_EXT)
2708
0
    || type == (N_ABS | N_EXT))
2709
0
  {
2710
    /* This object file defines this symbol.  We must link it
2711
       in.  This is true regardless of whether the current
2712
       definition of the symbol is undefined or common.  If the
2713
       current definition is common, we have a case in which we
2714
       have already seen an object file including
2715
     int a;
2716
       and this object file from the archive includes
2717
     int a = 5;
2718
       In such a case we must include this object file.
2719
2720
       FIXME: The SunOS 4.1.3 linker will pull in the archive
2721
       element if the symbol is defined in the .data section,
2722
       but not if it is defined in the .text section.  That
2723
       seems a bit crazy to me, and I haven't implemented it.
2724
       However, it might be correct.  */
2725
0
    if (!(*info->callbacks
2726
0
    ->add_archive_element) (info, abfd, name, subsbfd))
2727
0
      continue;
2728
0
    *pneeded = true;
2729
0
    return true;
2730
0
  }
2731
2732
0
      if (type == (N_UNDF | N_EXT))
2733
0
  {
2734
0
    bfd_vma value;
2735
2736
0
    value = GET_WORD (abfd, p->e_value);
2737
0
    if (value != 0)
2738
0
      {
2739
        /* This symbol is common in the object from the archive
2740
     file.  */
2741
0
        if (h->type == bfd_link_hash_undefined)
2742
0
    {
2743
0
      bfd *symbfd;
2744
0
      unsigned int power;
2745
2746
0
      symbfd = h->u.undef.abfd;
2747
0
      if (symbfd == NULL)
2748
0
        {
2749
          /* This symbol was created as undefined from
2750
       outside BFD.  We assume that we should link
2751
       in the object file.  This is done for the -u
2752
       option in the linker.  */
2753
0
          if (!(*info->callbacks
2754
0
          ->add_archive_element) (info, abfd, name, subsbfd))
2755
0
      return false;
2756
0
          *pneeded = true;
2757
0
          return true;
2758
0
        }
2759
      /* Turn the current link symbol into a common
2760
         symbol.  It is already on the undefs list.  */
2761
0
      h->type = bfd_link_hash_common;
2762
0
      h->u.c.p = bfd_hash_allocate (&info->hash->table,
2763
0
            sizeof (struct bfd_link_hash_common_entry));
2764
0
      if (h->u.c.p == NULL)
2765
0
        return false;
2766
2767
0
      h->u.c.size = value;
2768
2769
      /* FIXME: This isn't quite right.  The maximum
2770
         alignment of a common symbol should be set by the
2771
         architecture of the output file, not of the input
2772
         file.  */
2773
0
      power = bfd_log2 (value);
2774
0
      if (power > bfd_get_arch_info (abfd)->section_align_power)
2775
0
        power = bfd_get_arch_info (abfd)->section_align_power;
2776
0
      h->u.c.p->alignment_power = power;
2777
2778
0
      h->u.c.p->section = bfd_make_section_old_way (symbfd,
2779
0
                "COMMON");
2780
0
    }
2781
0
        else
2782
0
    {
2783
      /* Adjust the size of the common symbol if
2784
         necessary.  */
2785
0
      if (value > h->u.c.size)
2786
0
        h->u.c.size = value;
2787
0
    }
2788
0
      }
2789
0
  }
2790
0
    }
2791
2792
  /* We do not need this object file.  */
2793
0
  return true;
2794
0
}
2795
2796
/* Check a single archive element to see if we need to include it in
2797
   the link.  *PNEEDED is set according to whether this element is
2798
   needed in the link or not.  This is called from
2799
   _bfd_generic_link_add_archive_symbols.  */
2800
2801
static bool
2802
aout_link_check_archive_element (bfd *abfd,
2803
         struct bfd_link_info *info,
2804
         struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED,
2805
         const char *name ATTRIBUTE_UNUSED,
2806
         bool *pneeded)
2807
0
{
2808
0
  bfd *oldbfd;
2809
0
  bool needed;
2810
2811
0
  if (!aout_get_external_symbols (abfd))
2812
0
    return false;
2813
2814
0
  oldbfd = abfd;
2815
0
  if (!aout_link_check_ar_symbols (abfd, info, pneeded, &abfd))
2816
0
    return false;
2817
2818
0
  needed = *pneeded;
2819
0
  if (needed)
2820
0
    {
2821
      /* Potentially, the add_archive_element hook may have set a
2822
   substitute BFD for us.  */
2823
0
      if (abfd != oldbfd)
2824
0
  {
2825
0
    if (!info->keep_memory
2826
0
        && !aout_link_free_symbols (oldbfd))
2827
0
      return false;
2828
0
    if (!aout_get_external_symbols (abfd))
2829
0
      return false;
2830
0
  }
2831
0
      if (!aout_link_add_symbols (abfd, info))
2832
0
  return false;
2833
0
    }
2834
2835
0
  if (!info->keep_memory || !needed)
2836
0
    {
2837
0
      if (!aout_link_free_symbols (abfd))
2838
0
  return false;
2839
0
    }
2840
2841
0
  return true;
2842
0
}
2843
2844
/* Add all symbols from an object file to the hash table.  */
2845
2846
static bool
2847
aout_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
2848
0
{
2849
0
  bool (*add_one_symbol)
2850
0
    (struct bfd_link_info *, bfd *, const char *, flagword, asection *,
2851
0
     bfd_vma, const char *, bool, bool, struct bfd_link_hash_entry **);
2852
0
  struct external_nlist *syms;
2853
0
  bfd_size_type sym_count;
2854
0
  char *strings;
2855
0
  bool copy;
2856
0
  struct aout_link_hash_entry **sym_hash;
2857
0
  struct external_nlist *p;
2858
0
  struct external_nlist *pend;
2859
2860
0
  syms = obj_aout_external_syms (abfd);
2861
0
  sym_count = obj_aout_external_sym_count (abfd);
2862
0
  strings = obj_aout_external_strings (abfd);
2863
0
  if (info->keep_memory)
2864
0
    copy = false;
2865
0
  else
2866
0
    copy = true;
2867
2868
0
  if (aout_backend_info (abfd)->add_dynamic_symbols != NULL)
2869
0
    {
2870
0
      if (! ((*aout_backend_info (abfd)->add_dynamic_symbols)
2871
0
       (abfd, info, &syms, &sym_count, &strings)))
2872
0
  return false;
2873
0
    }
2874
2875
  /* We keep a list of the linker hash table entries that correspond
2876
     to particular symbols.  We could just look them up in the hash
2877
     table, but keeping the list is more efficient.  Perhaps this
2878
     should be conditional on info->keep_memory.  */
2879
0
  sym_hash = bfd_alloc (abfd,
2880
0
      sym_count * sizeof (struct aout_link_hash_entry *));
2881
0
  if (sym_hash == NULL && sym_count != 0)
2882
0
    return false;
2883
0
  obj_aout_sym_hashes (abfd) = sym_hash;
2884
2885
0
  add_one_symbol = aout_backend_info (abfd)->add_one_symbol;
2886
0
  if (add_one_symbol == NULL)
2887
0
    add_one_symbol = _bfd_generic_link_add_one_symbol;
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 (! ((*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
  /* Read in the relocs if we haven't already done it.  */
3666
0
  if (aout_section_data (input_section) != NULL
3667
0
      && aout_section_data (input_section)->relocs != NULL)
3668
0
    relocs = aout_section_data (input_section)->relocs;
3669
0
  else
3670
0
    {
3671
0
      relocs = flaginfo->relocs;
3672
0
      if (rel_size > 0)
3673
0
  {
3674
0
    if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
3675
0
        || bfd_read (relocs, rel_size, input_bfd) != rel_size)
3676
0
      return false;
3677
0
  }
3678
0
    }
3679
3680
  /* Relocate the section contents.  */
3681
0
  if (! pdp11_aout_link_input_section (flaginfo, input_bfd, input_section,
3682
0
               (bfd_byte *) relocs,
3683
0
               rel_size, flaginfo->contents))
3684
0
    return false;
3685
3686
  /* Write out the section contents.  */
3687
0
  if (! bfd_set_section_contents (flaginfo->output_bfd,
3688
0
          input_section->output_section,
3689
0
          (void *) flaginfo->contents,
3690
0
          (file_ptr) input_section->output_offset,
3691
0
          input_size))
3692
0
    return false;
3693
3694
  /* If we are producing relocatable output, the relocs were
3695
     modified, and we now write them out.  */
3696
0
  if (bfd_link_relocatable (flaginfo->info) && rel_size > 0)
3697
0
    {
3698
0
      if (bfd_seek (flaginfo->output_bfd, *reloff_ptr, SEEK_SET) != 0)
3699
0
  return false;
3700
0
      if (bfd_write (relocs, rel_size, flaginfo->output_bfd) != rel_size)
3701
0
  return false;
3702
0
      *reloff_ptr += rel_size;
3703
3704
      /* Assert that the relocs have not run into the symbols, and
3705
   that if these are the text relocs they have not run into the
3706
   data relocs.  */
3707
0
      BFD_ASSERT (*reloff_ptr <= obj_sym_filepos (flaginfo->output_bfd)
3708
0
      && (reloff_ptr != &flaginfo->treloff
3709
0
          || (*reloff_ptr
3710
0
        <= obj_datasec (flaginfo->output_bfd)->rel_filepos)));
3711
0
    }
3712
3713
0
  return true;
3714
0
}
3715
3716
/* Link an a.out input BFD into the output file.  */
3717
3718
static bool
3719
aout_link_input_bfd (struct aout_final_link_info *flaginfo, bfd *input_bfd)
3720
0
{
3721
0
  BFD_ASSERT (bfd_get_format (input_bfd) == bfd_object);
3722
3723
  /* If this is a dynamic object, it may need special handling.  */
3724
0
  if ((input_bfd->flags & DYNAMIC) != 0
3725
0
      && aout_backend_info (input_bfd)->link_dynamic_object != NULL)
3726
0
    return ((*aout_backend_info (input_bfd)->link_dynamic_object)
3727
0
      (flaginfo->info, input_bfd));
3728
3729
  /* Get the symbols.  We probably have them already, unless
3730
     flaginfo->info->keep_memory is FALSE.  */
3731
0
  if (! aout_get_external_symbols (input_bfd))
3732
0
    return false;
3733
3734
  /* Write out the symbols and get a map of the new indices.  The map
3735
     is placed into flaginfo->symbol_map.  */
3736
0
  if (! aout_link_write_symbols (flaginfo, input_bfd))
3737
0
    return false;
3738
3739
  /* Relocate and write out the sections.  These functions use the
3740
     symbol map created by aout_link_write_symbols.  The linker_mark
3741
     field will be set if these sections are to be included in the
3742
     link, which will normally be the case.  */
3743
0
  if (obj_textsec (input_bfd)->linker_mark)
3744
0
    {
3745
0
      if (! aout_link_input_section (flaginfo, input_bfd,
3746
0
             obj_textsec (input_bfd),
3747
0
             &flaginfo->treloff,
3748
0
             exec_hdr (input_bfd)->a_trsize))
3749
0
  return false;
3750
0
    }
3751
0
  if (obj_datasec (input_bfd)->linker_mark)
3752
0
    {
3753
0
      if (! aout_link_input_section (flaginfo, input_bfd,
3754
0
             obj_datasec (input_bfd),
3755
0
             &flaginfo->dreloff,
3756
0
             exec_hdr (input_bfd)->a_drsize))
3757
0
  return false;
3758
0
    }
3759
3760
  /* If we are not keeping memory, we don't need the symbols any
3761
     longer.  We still need them if we are keeping memory, because the
3762
     strings in the hash table point into them.  */
3763
0
  if (! flaginfo->info->keep_memory)
3764
0
    {
3765
0
      if (! aout_link_free_symbols (input_bfd))
3766
0
  return false;
3767
0
    }
3768
3769
0
  return true;
3770
0
}
3771
3772
/* Do the final link step.  This is called on the output BFD.  The
3773
   INFO structure should point to a list of BFDs linked through the
3774
   link.next field which can be used to find each BFD which takes part
3775
   in the output.  Also, each section in ABFD should point to a list
3776
   of bfd_link_order structures which list all the input sections for
3777
   the output section.  */
3778
3779
bool
3780
NAME (aout, final_link) (bfd *abfd,
3781
       struct bfd_link_info *info,
3782
       void (*callback) (bfd *, file_ptr *, file_ptr *, file_ptr *))
3783
0
{
3784
0
  struct aout_final_link_info aout_info;
3785
0
  bool includes_hash_initialized = false;
3786
0
  bfd *sub;
3787
0
  bfd_size_type trsize, drsize;
3788
0
  bfd_size_type max_contents_size;
3789
0
  bfd_size_type max_relocs_size;
3790
0
  bfd_size_type max_sym_count;
3791
0
  struct bfd_link_order *p;
3792
0
  asection *o;
3793
0
  bool have_link_order_relocs;
3794
3795
0
  if (bfd_link_pic (info))
3796
0
    abfd->flags |= DYNAMIC;
3797
3798
0
  separate_i_d = info->separate_code;
3799
0
  aout_info.info = info;
3800
0
  aout_info.output_bfd = abfd;
3801
0
  aout_info.contents = NULL;
3802
0
  aout_info.relocs = NULL;
3803
0
  aout_info.symbol_map = NULL;
3804
0
  aout_info.output_syms = NULL;
3805
3806
0
  if (!bfd_hash_table_init_n (&aout_info.includes.root,
3807
0
            aout_link_includes_newfunc,
3808
0
            sizeof (struct aout_link_includes_entry),
3809
0
            251))
3810
0
    goto error_return;
3811
0
  includes_hash_initialized = true;
3812
3813
  /* Figure out the largest section size.  Also, if generating
3814
     relocatable output, count the relocs.  */
3815
0
  trsize = 0;
3816
0
  drsize = 0;
3817
0
  max_contents_size = 0;
3818
0
  max_relocs_size = 0;
3819
0
  max_sym_count = 0;
3820
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3821
0
    {
3822
0
      size_t sz;
3823
3824
0
      if (bfd_link_relocatable (info))
3825
0
  {
3826
0
    if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
3827
0
      {
3828
0
        trsize += exec_hdr (sub)->a_trsize;
3829
0
        drsize += exec_hdr (sub)->a_drsize;
3830
0
      }
3831
0
    else
3832
0
      {
3833
        /* FIXME: We need to identify the .text and .data sections
3834
     and call get_reloc_upper_bound and canonicalize_reloc to
3835
     work out the number of relocs needed, and then multiply
3836
     by the reloc size.  */
3837
0
        _bfd_error_handler
3838
    /* xgettext:c-format */
3839
0
    (_("%pB: relocatable link from %s to %s not supported"),
3840
0
     abfd, sub->xvec->name, abfd->xvec->name);
3841
0
        bfd_set_error (bfd_error_invalid_operation);
3842
0
        goto error_return;
3843
0
      }
3844
0
  }
3845
3846
0
      if (bfd_get_flavour (sub) == bfd_target_aout_flavour)
3847
0
  {
3848
0
    sz = obj_textsec (sub)->size;
3849
0
    if (sz > max_contents_size)
3850
0
      max_contents_size = sz;
3851
0
    sz = obj_datasec (sub)->size;
3852
0
    if (sz > max_contents_size)
3853
0
      max_contents_size = sz;
3854
3855
0
    sz = exec_hdr (sub)->a_trsize;
3856
0
    if (sz > max_relocs_size)
3857
0
      max_relocs_size = sz;
3858
0
    sz = exec_hdr (sub)->a_drsize;
3859
0
    if (sz > max_relocs_size)
3860
0
      max_relocs_size = sz;
3861
3862
0
    sz = obj_aout_external_sym_count (sub);
3863
0
    if (sz > max_sym_count)
3864
0
      max_sym_count = sz;
3865
0
  }
3866
0
    }
3867
3868
0
  if (bfd_link_relocatable (info))
3869
0
    {
3870
0
      if (obj_textsec (abfd) != NULL)
3871
0
  trsize += (_bfd_count_link_order_relocs (obj_textsec (abfd)
3872
0
             ->map_head.link_order)
3873
0
       * obj_reloc_entry_size (abfd));
3874
0
      if (obj_datasec (abfd) != NULL)
3875
0
  drsize += (_bfd_count_link_order_relocs (obj_datasec (abfd)
3876
0
             ->map_head.link_order)
3877
0
       * obj_reloc_entry_size (abfd));
3878
0
    }
3879
3880
0
  exec_hdr (abfd)->a_trsize = trsize;
3881
0
  exec_hdr (abfd)->a_drsize = drsize;
3882
0
  exec_hdr (abfd)->a_entry = bfd_get_start_address (abfd);
3883
3884
  /* Adjust the section sizes and vmas according to the magic number.
3885
     This sets a_text, a_data and a_bss in the exec_hdr and sets the
3886
     filepos for each section.  */
3887
0
  if (! NAME (aout, adjust_sizes_and_vmas) (abfd))
3888
0
    goto error_return;
3889
3890
  /* The relocation and symbol file positions differ among a.out
3891
     targets.  We are passed a callback routine from the backend
3892
     specific code to handle this.
3893
     FIXME: At this point we do not know how much space the symbol
3894
     table will require.  This will not work for any (nonstandard)
3895
     a.out target that needs to know the symbol table size before it
3896
     can compute the relocation file positions.  */
3897
0
  (*callback) (abfd, &aout_info.treloff, &aout_info.dreloff,
3898
0
         &aout_info.symoff);
3899
0
  obj_textsec (abfd)->rel_filepos = aout_info.treloff;
3900
0
  obj_datasec (abfd)->rel_filepos = aout_info.dreloff;
3901
0
  obj_sym_filepos (abfd) = aout_info.symoff;
3902
3903
  /* We keep a count of the symbols as we output them.  */
3904
0
  obj_aout_external_sym_count (abfd) = 0;
3905
3906
  /* We accumulate the string table as we write out the symbols.  */
3907
0
  aout_info.strtab = _bfd_stringtab_init ();
3908
0
  if (aout_info.strtab == NULL)
3909
0
    goto error_return;
3910
3911
  /* Allocate buffers to hold section contents and relocs.  */
3912
0
  aout_info.contents = bfd_malloc (max_contents_size);
3913
0
  aout_info.relocs = bfd_malloc (max_relocs_size);
3914
0
  aout_info.symbol_map = bfd_malloc (max_sym_count * sizeof (int *));
3915
0
  aout_info.output_syms = bfd_malloc ((max_sym_count + 1)
3916
0
              * sizeof (struct external_nlist));
3917
0
  if ((aout_info.contents == NULL && max_contents_size != 0)
3918
0
      || (aout_info.relocs == NULL && max_relocs_size != 0)
3919
0
      || (aout_info.symbol_map == NULL && max_sym_count != 0)
3920
0
      || aout_info.output_syms == NULL)
3921
0
    goto error_return;
3922
3923
  /* If we have a symbol named __DYNAMIC, force it out now.  This is
3924
     required by SunOS.  Doing this here rather than in sunos.c is a
3925
     hack, but it's easier than exporting everything which would be
3926
     needed.  */
3927
0
  {
3928
0
    struct aout_link_hash_entry *h;
3929
3930
0
    h = aout_link_hash_lookup (aout_hash_table (info), "__DYNAMIC",
3931
0
             false, false, false);
3932
0
    if (h != NULL)
3933
0
      aout_link_write_other_symbol (&h->root.root, &aout_info);
3934
0
  }
3935
3936
  /* The most time efficient way to do the link would be to read all
3937
     the input object files into memory and then sort out the
3938
     information into the output file.  Unfortunately, that will
3939
     probably use too much memory.  Another method would be to step
3940
     through everything that composes the text section and write it
3941
     out, and then everything that composes the data section and write
3942
     it out, and then write out the relocs, and then write out the
3943
     symbols.  Unfortunately, that requires reading stuff from each
3944
     input file several times, and we will not be able to keep all the
3945
     input files open simultaneously, and reopening them will be slow.
3946
3947
     What we do is basically process one input file at a time.  We do
3948
     everything we need to do with an input file once--copy over the
3949
     section contents, handle the relocation information, and write
3950
     out the symbols--and then we throw away the information we read
3951
     from it.  This approach requires a lot of lseeks of the output
3952
     file, which is unfortunate but still faster than reopening a lot
3953
     of files.
3954
3955
     We use the output_has_begun field of the input BFDs to see
3956
     whether we have already handled it.  */
3957
0
  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3958
0
    sub->output_has_begun = false;
3959
3960
  /* Mark all sections which are to be included in the link.  This
3961
     will normally be every section.  We need to do this so that we
3962
     can identify any sections which the linker has decided to not
3963
     include.  */
3964
0
  for (o = abfd->sections; o != NULL; o = o->next)
3965
0
    {
3966
0
      for (p = o->map_head.link_order; p != NULL; p = p->next)
3967
0
  if (p->type == bfd_indirect_link_order)
3968
0
    p->u.indirect.section->linker_mark = true;
3969
0
    }
3970
3971
0
  have_link_order_relocs = false;
3972
0
  for (o = abfd->sections; o != NULL; o = o->next)
3973
0
    {
3974
0
      for (p = o->map_head.link_order;
3975
0
     p != NULL;
3976
0
     p = p->next)
3977
0
  {
3978
0
    if (p->type == bfd_indirect_link_order
3979
0
        && (bfd_get_flavour (p->u.indirect.section->owner)
3980
0
      == bfd_target_aout_flavour))
3981
0
      {
3982
0
        bfd *input_bfd;
3983
3984
0
        input_bfd = p->u.indirect.section->owner;
3985
0
        if (! input_bfd->output_has_begun)
3986
0
    {
3987
0
      if (! aout_link_input_bfd (&aout_info, input_bfd))
3988
0
        goto error_return;
3989
0
      input_bfd->output_has_begun = true;
3990
0
    }
3991
0
      }
3992
0
    else if (p->type == bfd_section_reloc_link_order
3993
0
       || p->type == bfd_symbol_reloc_link_order)
3994
      /* These are handled below.  */
3995
0
      have_link_order_relocs = true;
3996
0
    else
3997
0
      {
3998
0
        if (! _bfd_default_link_order (abfd, info, o, p))
3999
0
    goto error_return;
4000
0
      }
4001
0
  }
4002
0
    }
4003
4004
  /* Write out any symbols that we have not already written out.  */
4005
0
  bfd_hash_traverse (&info->hash->table,
4006
0
         aout_link_write_other_symbol,
4007
0
         &aout_info);
4008
4009
  /* Now handle any relocs we were asked to create by the linker.
4010
     These did not come from any input file.  We must do these after
4011
     we have written out all the symbols, so that we know the symbol
4012
     indices to use.  */
4013
0
  if (have_link_order_relocs)
4014
0
    {
4015
0
      for (o = abfd->sections; o != NULL; o = o->next)
4016
0
  {
4017
0
    for (p = o->map_head.link_order;
4018
0
         p != NULL;
4019
0
         p = p->next)
4020
0
      {
4021
0
        if (p->type == bfd_section_reloc_link_order
4022
0
      || p->type == bfd_symbol_reloc_link_order)
4023
0
    {
4024
0
      if (! aout_link_reloc_link_order (&aout_info, o, p))
4025
0
        goto error_return;
4026
0
    }
4027
0
      }
4028
0
  }
4029
0
    }
4030
4031
0
  free (aout_info.contents);
4032
0
  aout_info.contents = NULL;
4033
0
  free (aout_info.relocs);
4034
0
  aout_info.relocs = NULL;
4035
0
  free (aout_info.symbol_map);
4036
0
  aout_info.symbol_map = NULL;
4037
0
  free (aout_info.output_syms);
4038
0
  aout_info.output_syms = NULL;
4039
0
  if (includes_hash_initialized)
4040
0
    {
4041
0
      bfd_hash_table_free (&aout_info.includes.root);
4042
0
      includes_hash_initialized = false;
4043
0
    }
4044
4045
  /* Finish up any dynamic linking we may be doing.  */
4046
0
  if (aout_backend_info (abfd)->finish_dynamic_link != NULL)
4047
0
    {
4048
0
      if (! (*aout_backend_info (abfd)->finish_dynamic_link) (abfd, info))
4049
0
  goto error_return;
4050
0
    }
4051
4052
  /* Update the header information.  */
4053
0
  abfd->symcount = obj_aout_external_sym_count (abfd);
4054
0
  exec_hdr (abfd)->a_syms = abfd->symcount * EXTERNAL_NLIST_SIZE;
4055
0
  obj_str_filepos (abfd) = obj_sym_filepos (abfd) + exec_hdr (abfd)->a_syms;
4056
0
  obj_textsec (abfd)->reloc_count =
4057
0
    exec_hdr (abfd)->a_trsize / obj_reloc_entry_size (abfd);
4058
0
  obj_datasec (abfd)->reloc_count =
4059
0
    exec_hdr (abfd)->a_drsize / obj_reloc_entry_size (abfd);
4060
4061
  /* Write out the string table, unless there are no symbols.  */
4062
0
  if (abfd->symcount > 0)
4063
0
    {
4064
0
      if (bfd_seek (abfd, obj_str_filepos (abfd), SEEK_SET) != 0
4065
0
    || ! emit_stringtab (abfd, aout_info.strtab))
4066
0
  goto error_return;
4067
0
    }
4068
0
  else if (obj_textsec (abfd)->reloc_count == 0
4069
0
     && obj_datasec (abfd)->reloc_count == 0)
4070
0
    {
4071
      /* The layout of a typical a.out file is header, text, data,
4072
   relocs, symbols, string table.  When there are no relocs,
4073
   symbols or string table, the last thing in the file is data
4074
   and a_data may be rounded up.  However we may have a smaller
4075
   sized .data section and thus not written final padding.  The
4076
   same thing can happen with text if there is no data.  Write
4077
   final padding here to extend the file.  */
4078
0
      file_ptr pos = 0;
4079
4080
0
      if (exec_hdr (abfd)->a_data > obj_datasec (abfd)->size)
4081
0
  pos = obj_datasec (abfd)->filepos + exec_hdr (abfd)->a_data;
4082
0
      else if (obj_datasec (abfd)->size == 0
4083
0
         && exec_hdr (abfd)->a_text > obj_textsec (abfd)->size)
4084
0
  pos = obj_textsec (abfd)->filepos + exec_hdr (abfd)->a_text;
4085
0
      if (pos != 0)
4086
0
  {
4087
0
    bfd_byte b = 0;
4088
4089
0
    if (bfd_seek (abfd, pos - 1, SEEK_SET) != 0
4090
0
        || bfd_write (&b, 1, abfd) != 1)
4091
0
      goto error_return;
4092
0
  }
4093
0
    }
4094
4095
0
  return true;
4096
4097
0
 error_return:
4098
0
  free (aout_info.contents);
4099
0
  free (aout_info.relocs);
4100
0
  free (aout_info.symbol_map);
4101
0
  free (aout_info.output_syms);
4102
0
  if (includes_hash_initialized)
4103
0
    bfd_hash_table_free (&aout_info.includes.root);
4104
0
  return false;
4105
0
}
4106
4107
/* Adjust and write out the symbols for an a.out file.  Set the new
4108
   symbol indices into a symbol_map.  */
4109
4110
static bool
4111
aout_link_write_symbols (struct aout_final_link_info *flaginfo, bfd *input_bfd)
4112
0
{
4113
0
  bfd *output_bfd;
4114
0
  bfd_size_type sym_count;
4115
0
  char *strings;
4116
0
  enum bfd_link_strip strip;
4117
0
  enum bfd_link_discard discard;
4118
0
  struct external_nlist *outsym;
4119
0
  bfd_size_type strtab_index;
4120
0
  struct external_nlist *sym;
4121
0
  struct external_nlist *sym_end;
4122
0
  struct aout_link_hash_entry **sym_hash;
4123
0
  int *symbol_map;
4124
0
  bool pass;
4125
0
  bool skip_next;
4126
4127
0
  output_bfd = flaginfo->output_bfd;
4128
0
  sym_count = obj_aout_external_sym_count (input_bfd);
4129
0
  strings = obj_aout_external_strings (input_bfd);
4130
0
  strip = flaginfo->info->strip;
4131
0
  discard = flaginfo->info->discard;
4132
0
  outsym = flaginfo->output_syms;
4133
4134
  /* First write out a symbol for this object file, unless we are
4135
     discarding such symbols.  */
4136
0
  if (strip != strip_all
4137
0
      && (strip != strip_some
4138
0
    || bfd_hash_lookup (flaginfo->info->keep_hash,
4139
0
            bfd_get_filename (input_bfd),
4140
0
            false, false) != NULL)
4141
0
      && discard != discard_all)
4142
0
    {
4143
0
      H_PUT_8 (output_bfd, N_TEXT, outsym->e_type);
4144
0
      H_PUT_8 (output_bfd, 0, outsym->e_ovly);
4145
0
      H_PUT_16 (output_bfd, 0, outsym->e_desc);
4146
0
      strtab_index = add_to_stringtab (output_bfd, flaginfo->strtab,
4147
0
               bfd_get_filename (input_bfd), false);
4148
0
      if (strtab_index == (bfd_size_type) -1)
4149
0
  return false;
4150
0
      PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
4151
0
      PUT_WORD (output_bfd,
4152
0
    (bfd_section_vma (obj_textsec (input_bfd)->output_section)
4153
0
     + obj_textsec (input_bfd)->output_offset),
4154
0
    outsym->e_value);
4155
0
      ++obj_aout_external_sym_count (output_bfd);
4156
0
      ++outsym;
4157
0
    }
4158
4159
0
  pass = false;
4160
0
  skip_next = false;
4161
0
  sym = obj_aout_external_syms (input_bfd);
4162
0
  sym_end = sym + sym_count;
4163
0
  sym_hash = obj_aout_sym_hashes (input_bfd);
4164
0
  symbol_map = flaginfo->symbol_map;
4165
0
  memset (symbol_map, 0, (size_t) sym_count * sizeof *symbol_map);
4166
0
  for (; sym < sym_end; sym++, sym_hash++, symbol_map++)
4167
0
    {
4168
0
      const char *name;
4169
0
      int type;
4170
0
      struct aout_link_hash_entry *h;
4171
0
      bool skip;
4172
0
      asection *symsec;
4173
0
      bfd_vma val = 0;
4174
0
      bool copy;
4175
4176
      /* We set *symbol_map to 0 above for all symbols.  If it has
4177
   already been set to -1 for this symbol, it means that we are
4178
   discarding it because it appears in a duplicate header file.
4179
   See the N_BINCL code below.  */
4180
0
      if (*symbol_map == -1)
4181
0
  continue;
4182
4183
      /* Initialize *symbol_map to -1, which means that the symbol was
4184
   not copied into the output file.  We will change it later if
4185
   we do copy the symbol over.  */
4186
0
      *symbol_map = -1;
4187
4188
0
      type = H_GET_8 (input_bfd, sym->e_type);
4189
0
      name = strings + GET_WORD (input_bfd, sym->e_strx);
4190
4191
0
      h = NULL;
4192
4193
0
      if (pass)
4194
0
  {
4195
    /* Pass this symbol through.  It is the target of an
4196
       indirect or warning symbol.  */
4197
0
    val = GET_WORD (input_bfd, sym->e_value);
4198
0
    pass = false;
4199
0
  }
4200
0
      else if (skip_next)
4201
0
  {
4202
    /* Skip this symbol, which is the target of an indirect
4203
       symbol that we have changed to no longer be an indirect
4204
       symbol.  */
4205
0
    skip_next = false;
4206
0
    continue;
4207
0
  }
4208
0
      else
4209
0
  {
4210
0
    struct aout_link_hash_entry *hresolve;
4211
4212
    /* We have saved the hash table entry for this symbol, if
4213
       there is one.  Note that we could just look it up again
4214
       in the hash table, provided we first check that it is an
4215
       external symbol. */
4216
0
    h = *sym_hash;
4217
4218
    /* Use the name from the hash table, in case the symbol was
4219
       wrapped.  */
4220
0
    if (h != NULL)
4221
0
      name = h->root.root.string;
4222
4223
    /* If this is an indirect or warning symbol, then change
4224
       hresolve to the base symbol.  We also change *sym_hash so
4225
       that the relocation routines relocate against the real
4226
       symbol.  */
4227
0
    hresolve = h;
4228
0
    if (h != NULL
4229
0
        && (h->root.type == bfd_link_hash_indirect
4230
0
      || h->root.type == bfd_link_hash_warning))
4231
0
      {
4232
0
        hresolve = (struct aout_link_hash_entry *) h->root.u.i.link;
4233
0
        while (hresolve->root.type == bfd_link_hash_indirect
4234
0
         || hresolve->root.type == bfd_link_hash_warning)
4235
0
    hresolve = ((struct aout_link_hash_entry *)
4236
0
          hresolve->root.u.i.link);
4237
0
        *sym_hash = hresolve;
4238
0
      }
4239
4240
    /* If the symbol has already been written out, skip it.  */
4241
0
    if (h != NULL
4242
0
        && h->root.type != bfd_link_hash_warning
4243
0
        && h->written)
4244
0
      {
4245
0
        if ((type & N_TYPE) == N_INDR
4246
0
      || type == N_WARNING)
4247
0
    skip_next = true;
4248
0
        *symbol_map = h->indx;
4249
0
        continue;
4250
0
      }
4251
4252
    /* See if we are stripping this symbol.  */
4253
0
    skip = false;
4254
0
    switch (strip)
4255
0
      {
4256
0
      case strip_none:
4257
0
        break;
4258
0
      case strip_debugger:
4259
0
        if (is_stab (type, name))
4260
0
    skip = true;
4261
0
        break;
4262
0
      case strip_some:
4263
0
        if (bfd_hash_lookup (flaginfo->info->keep_hash, name,
4264
0
           false, false) == NULL)
4265
0
    skip = true;
4266
0
        break;
4267
0
      case strip_all:
4268
0
        skip = true;
4269
0
        break;
4270
0
      }
4271
0
    if (skip)
4272
0
      {
4273
0
        if (h != NULL)
4274
0
    h->written = true;
4275
0
        continue;
4276
0
      }
4277
4278
    /* Get the value of the symbol.  */
4279
0
    if (is_stab (type, name))
4280
0
      {
4281
0
        switch (type)
4282
0
    {
4283
0
    default:
4284
0
      symsec = bfd_abs_section_ptr;
4285
0
      break;
4286
0
    case N_SO:
4287
0
    case N_SOL:
4288
0
    case N_FUN:
4289
0
    case N_ENTRY:
4290
0
    case N_SLINE:
4291
0
    case N_FN:
4292
0
      symsec = obj_textsec (input_bfd);
4293
0
      break;
4294
0
    case N_STSYM:
4295
0
    case N_DSLINE:
4296
0
      symsec = obj_datasec (input_bfd);
4297
0
      break;
4298
0
    case N_LCSYM:
4299
0
    case N_BSLINE:
4300
0
      symsec = obj_bsssec (input_bfd);
4301
0
      break;
4302
0
    }
4303
0
        val = GET_WORD (input_bfd, sym->e_value);
4304
0
      }
4305
0
    else if ((type & N_TYPE) == N_TEXT
4306
0
        || type == N_WEAKT)
4307
0
      symsec = obj_textsec (input_bfd);
4308
0
    else if ((type & N_TYPE) == N_DATA
4309
0
       || type == N_WEAKD)
4310
0
      symsec = obj_datasec (input_bfd);
4311
0
    else if ((type & N_TYPE) == N_BSS
4312
0
       || type == N_WEAKB)
4313
0
      symsec = obj_bsssec (input_bfd);
4314
0
    else if ((type & N_TYPE) == N_ABS
4315
0
       || type == N_WEAKA)
4316
0
      symsec = bfd_abs_section_ptr;
4317
0
    else if (((type & N_TYPE) == N_INDR
4318
0
        && (hresolve == NULL
4319
0
      || (hresolve->root.type != bfd_link_hash_defined
4320
0
          && hresolve->root.type != bfd_link_hash_defweak
4321
0
          && hresolve->root.type != bfd_link_hash_common)))
4322
0
       || type == N_WARNING)
4323
0
      {
4324
        /* Pass the next symbol through unchanged.  The
4325
     condition above for indirect symbols is so that if
4326
     the indirect symbol was defined, we output it with
4327
     the correct definition so the debugger will
4328
     understand it.  */
4329
0
        pass = true;
4330
0
        val = GET_WORD (input_bfd, sym->e_value);
4331
0
        symsec = NULL;
4332
0
      }
4333
0
    else
4334
0
      {
4335
        /* If we get here with an indirect symbol, it means that
4336
     we are outputting it with a real definition.  In such
4337
     a case we do not want to output the next symbol,
4338
     which is the target of the indirection.  */
4339
0
        if ((type & N_TYPE) == N_INDR)
4340
0
    skip_next = true;
4341
4342
0
        symsec = NULL;
4343
4344
        /* We need to get the value from the hash table.  We use
4345
     hresolve so that if we have defined an indirect
4346
     symbol we output the final definition.  */
4347
0
        if (h == NULL)
4348
0
    {
4349
0
      switch (type & N_TYPE)
4350
0
        {
4351
0
        case N_SETT:
4352
0
          symsec = obj_textsec (input_bfd);
4353
0
          break;
4354
0
        case N_SETD:
4355
0
          symsec = obj_datasec (input_bfd);
4356
0
          break;
4357
0
        case N_SETB:
4358
0
          symsec = obj_bsssec (input_bfd);
4359
0
          break;
4360
0
        case N_SETA:
4361
0
          symsec = bfd_abs_section_ptr;
4362
0
          break;
4363
0
        default:
4364
0
          val = 0;
4365
0
          break;
4366
0
        }
4367
0
    }
4368
0
        else if (hresolve->root.type == bfd_link_hash_defined
4369
0
           || hresolve->root.type == bfd_link_hash_defweak)
4370
0
    {
4371
0
      asection *input_section;
4372
0
      asection *output_section;
4373
4374
      /* This case usually means a common symbol which was
4375
         turned into a defined symbol.  */
4376
0
      input_section = hresolve->root.u.def.section;
4377
0
      output_section = input_section->output_section;
4378
0
      BFD_ASSERT (bfd_is_abs_section (output_section)
4379
0
            || output_section->owner == output_bfd);
4380
0
      val = (hresolve->root.u.def.value
4381
0
       + bfd_section_vma (output_section)
4382
0
       + input_section->output_offset);
4383
4384
      /* Get the correct type based on the section.  If
4385
         this is a constructed set, force it to be
4386
         globally visible.  */
4387
0
      if (type == N_SETT
4388
0
          || type == N_SETD
4389
0
          || type == N_SETB
4390
0
          || type == N_SETA)
4391
0
        type |= N_EXT;
4392
4393
0
      type &=~ N_TYPE;
4394
4395
0
      if (output_section == obj_textsec (output_bfd))
4396
0
        type |= (hresolve->root.type == bfd_link_hash_defined
4397
0
           ? N_TEXT
4398
0
           : N_WEAKT);
4399
0
      else if (output_section == obj_datasec (output_bfd))
4400
0
        type |= (hresolve->root.type == bfd_link_hash_defined
4401
0
           ? N_DATA
4402
0
           : N_WEAKD);
4403
0
      else if (output_section == obj_bsssec (output_bfd))
4404
0
        type |= (hresolve->root.type == bfd_link_hash_defined
4405
0
           ? N_BSS
4406
0
           : N_WEAKB);
4407
0
      else
4408
0
        type |= (hresolve->root.type == bfd_link_hash_defined
4409
0
           ? N_ABS
4410
0
           : N_WEAKA);
4411
0
    }
4412
0
        else if (hresolve->root.type == bfd_link_hash_common)
4413
0
    val = hresolve->root.u.c.size;
4414
0
        else if (hresolve->root.type == bfd_link_hash_undefweak)
4415
0
    {
4416
0
      val = 0;
4417
0
      type = N_WEAKU;
4418
0
    }
4419
0
        else
4420
0
    val = 0;
4421
0
      }
4422
0
    if (symsec != NULL)
4423
0
      val = (symsec->output_section->vma
4424
0
       + symsec->output_offset
4425
0
       + (GET_WORD (input_bfd, sym->e_value)
4426
0
          - symsec->vma));
4427
4428
    /* If this is a global symbol set the written flag, and if
4429
       it is a local symbol see if we should discard it.  */
4430
0
    if (h != NULL)
4431
0
      {
4432
0
        h->written = true;
4433
0
        h->indx = obj_aout_external_sym_count (output_bfd);
4434
0
      }
4435
0
    else if ((type & N_TYPE) != N_SETT
4436
0
       && (type & N_TYPE) != N_SETD
4437
0
       && (type & N_TYPE) != N_SETB
4438
0
       && (type & N_TYPE) != N_SETA)
4439
0
      {
4440
0
        switch (discard)
4441
0
    {
4442
0
    case discard_none:
4443
0
    case discard_sec_merge:
4444
0
      break;
4445
0
    case discard_l:
4446
0
      if (!is_stab (type, name)
4447
0
          && bfd_is_local_label_name (input_bfd, name))
4448
0
        skip = true;
4449
0
      break;
4450
0
    case discard_all:
4451
0
      skip = true;
4452
0
      break;
4453
0
    }
4454
0
        if (skip)
4455
0
    {
4456
0
      pass = false;
4457
0
      continue;
4458
0
    }
4459
0
      }
4460
4461
    /* An N_BINCL symbol indicates the start of the stabs
4462
       entries for a header file.  We need to scan ahead to the
4463
       next N_EINCL symbol, ignoring nesting, adding up all the
4464
       characters in the symbol names, not including the file
4465
       numbers in types (the first number after an open
4466
       parenthesis).  */
4467
0
    if (type == N_BINCL)
4468
0
      {
4469
0
        struct external_nlist *incl_sym;
4470
0
        int nest;
4471
0
        struct aout_link_includes_entry *incl_entry;
4472
0
        struct aout_link_includes_totals *t;
4473
4474
0
        val = 0;
4475
0
        nest = 0;
4476
0
        for (incl_sym = sym + 1; incl_sym < sym_end; incl_sym++)
4477
0
    {
4478
0
      int incl_type;
4479
4480
0
      incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
4481
0
      if (incl_type == N_EINCL)
4482
0
        {
4483
0
          if (nest == 0)
4484
0
      break;
4485
0
          --nest;
4486
0
        }
4487
0
      else if (incl_type == N_BINCL)
4488
0
        ++nest;
4489
0
      else if (nest == 0)
4490
0
        {
4491
0
          const char *s;
4492
4493
0
          s = strings + GET_WORD (input_bfd, incl_sym->e_strx);
4494
0
          for (; *s != '\0'; s++)
4495
0
      {
4496
0
        val += *s;
4497
0
        if (*s == '(')
4498
0
          {
4499
            /* Skip the file number.  */
4500
0
            ++s;
4501
0
            while (ISDIGIT (*s))
4502
0
        ++s;
4503
0
            --s;
4504
0
          }
4505
0
      }
4506
0
        }
4507
0
    }
4508
4509
        /* If we have already included a header file with the
4510
     same value, then replace this one with an N_EXCL
4511
     symbol.  */
4512
0
        copy = ! flaginfo->info->keep_memory;
4513
0
        incl_entry = aout_link_includes_lookup (&flaginfo->includes,
4514
0
                  name, true, copy);
4515
0
        if (incl_entry == NULL)
4516
0
    return false;
4517
0
        for (t = incl_entry->totals; t != NULL; t = t->next)
4518
0
    if (t->total == val)
4519
0
      break;
4520
0
        if (t == NULL)
4521
0
    {
4522
      /* This is the first time we have seen this header
4523
         file with this set of stabs strings.  */
4524
0
      t = bfd_hash_allocate (&flaginfo->includes.root,
4525
0
           sizeof *t);
4526
0
      if (t == NULL)
4527
0
        return false;
4528
0
      t->total = val;
4529
0
      t->next = incl_entry->totals;
4530
0
      incl_entry->totals = t;
4531
0
    }
4532
0
        else
4533
0
    {
4534
0
      int *incl_map;
4535
4536
      /* This is a duplicate header file.  We must change
4537
         it to be an N_EXCL entry, and mark all the
4538
         included symbols to prevent outputting them.  */
4539
0
      type = N_EXCL;
4540
4541
0
      nest = 0;
4542
0
      for (incl_sym = sym + 1, incl_map = symbol_map + 1;
4543
0
           incl_sym < sym_end;
4544
0
           incl_sym++, incl_map++)
4545
0
        {
4546
0
          int incl_type;
4547
4548
0
          incl_type = H_GET_8 (input_bfd, incl_sym->e_type);
4549
0
          if (incl_type == N_EINCL)
4550
0
      {
4551
0
        if (nest == 0)
4552
0
          {
4553
0
            *incl_map = -1;
4554
0
            break;
4555
0
          }
4556
0
        --nest;
4557
0
      }
4558
0
          else if (incl_type == N_BINCL)
4559
0
      ++nest;
4560
0
          else if (nest == 0)
4561
0
      *incl_map = -1;
4562
0
        }
4563
0
    }
4564
0
      }
4565
0
  }
4566
4567
      /* Copy this symbol into the list of symbols we are going to
4568
   write out.  */
4569
0
      H_PUT_8 (output_bfd, type, outsym->e_type);
4570
0
      H_PUT_8 (output_bfd, H_GET_8 (input_bfd, sym->e_ovly), outsym->e_ovly);
4571
0
      H_PUT_16 (output_bfd, H_GET_16 (input_bfd, sym->e_desc), outsym->e_desc);
4572
0
      copy = false;
4573
0
      if (! flaginfo->info->keep_memory)
4574
0
  {
4575
    /* name points into a string table which we are going to
4576
       free.  If there is a hash table entry, use that string.
4577
       Otherwise, copy name into memory.  */
4578
0
    if (h != NULL)
4579
0
      name = h->root.root.string;
4580
0
    else
4581
0
      copy = true;
4582
0
  }
4583
0
      strtab_index = add_to_stringtab (output_bfd, flaginfo->strtab,
4584
0
               name, copy);
4585
0
      if (strtab_index == (bfd_size_type) -1)
4586
0
  return false;
4587
0
      PUT_WORD (output_bfd, strtab_index, outsym->e_strx);
4588
0
      PUT_WORD (output_bfd, val, outsym->e_value);
4589
0
      *symbol_map = obj_aout_external_sym_count (output_bfd);
4590
0
      ++obj_aout_external_sym_count (output_bfd);
4591
0
      ++outsym;
4592
0
    }
4593
4594
  /* Write out the output symbols we have just constructed.  */
4595
0
  if (outsym > flaginfo->output_syms)
4596
0
    {
4597
0
      bfd_size_type size;
4598
4599
0
      if (bfd_seek (output_bfd, flaginfo->symoff, SEEK_SET) != 0)
4600
0
  return false;
4601
0
      size = outsym - flaginfo->output_syms;
4602
0
      size *= EXTERNAL_NLIST_SIZE;
4603
0
      if (bfd_write (flaginfo->output_syms, size, output_bfd) != size)
4604
0
  return false;
4605
0
      flaginfo->symoff += size;
4606
0
    }
4607
4608
0
  return true;
4609
0
}
4610
4611
/* Write out a symbol that was not associated with an a.out input
4612
   object.  */
4613
4614
static bfd_vma
4615
bfd_getp32 (const void *p)
4616
41.2k
{
4617
41.2k
  const bfd_byte *addr = p;
4618
41.2k
  unsigned long v;
4619
4620
41.2k
  v = (unsigned long) addr[1] << 24;
4621
41.2k
  v |= (unsigned long) addr[0] << 16;
4622
41.2k
  v |= (unsigned long) addr[3] << 8;
4623
41.2k
  v |= (unsigned long) addr[2];
4624
41.2k
  return v;
4625
41.2k
}
4626
4627
0
#define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
4628
4629
static bfd_signed_vma
4630
bfd_getp_signed_32 (const void *p)
4631
0
{
4632
0
  const bfd_byte *addr = p;
4633
0
  unsigned long v;
4634
4635
0
  v = (unsigned long) addr[1] << 24;
4636
0
  v |= (unsigned long) addr[0] << 16;
4637
0
  v |= (unsigned long) addr[3] << 8;
4638
0
  v |= (unsigned long) addr[2];
4639
0
  return COERCE32 (v);
4640
0
}
4641
4642
static void
4643
bfd_putp32 (bfd_vma data, void *p)
4644
0
{
4645
0
  bfd_byte *addr = p;
4646
4647
0
  addr[0] = (data >> 16) & 0xff;
4648
0
  addr[1] = (data >> 24) & 0xff;
4649
0
  addr[2] = (data >> 0) & 0xff;
4650
0
  addr[3] = (data >> 8) & 0xff;
4651
0
}
4652
4653
const bfd_target MY (vec) =
4654
{
4655
  TARGETNAME,     /* Name.  */
4656
  bfd_target_aout_flavour,
4657
  BFD_ENDIAN_LITTLE,    /* Target byte order (little).  */
4658
  BFD_ENDIAN_LITTLE,    /* Target headers byte order (little).  */
4659
  (HAS_RELOC | EXEC_P |   /* Object flags.  */
4660
   HAS_LINENO | HAS_DEBUG |
4661
   HAS_SYMS | HAS_LOCALS | WP_TEXT),
4662
  (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
4663
  MY_symbol_leading_char,
4664
  AR_PAD_CHAR,      /* AR_pad_char.  */
4665
  15,       /* AR_max_namelen.  */
4666
  0,        /* match priority.  */
4667
  TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols.  */
4668
  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
4669
     bfd_getp32, bfd_getp_signed_32, bfd_putp32,
4670
     bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data.  */
4671
  bfd_getl64, bfd_getl_signed_64, bfd_putl64,
4672
     bfd_getp32, bfd_getp_signed_32, bfd_putp32,
4673
     bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Headers.  */
4674
  {       /* bfd_check_format.  */
4675
    _bfd_dummy_target,
4676
    MY_object_p,
4677
    bfd_generic_archive_p,
4678
    MY_core_file_p
4679
  },
4680
  {       /* bfd_set_format.  */
4681
    _bfd_bool_bfd_false_error,
4682
    MY_mkobject,
4683
    _bfd_generic_mkarchive,
4684
    _bfd_bool_bfd_false_error
4685
  },
4686
  {     /* bfd_write_contents.  */
4687
    _bfd_bool_bfd_false_error,
4688
    MY_write_object_contents,
4689
    _bfd_write_archive_contents,
4690
    _bfd_bool_bfd_false_error
4691
  },
4692
4693
  BFD_JUMP_TABLE_GENERIC (MY),
4694
  BFD_JUMP_TABLE_COPY (MY),
4695
  BFD_JUMP_TABLE_CORE (MY),
4696
  BFD_JUMP_TABLE_ARCHIVE (MY),
4697
  BFD_JUMP_TABLE_SYMBOLS (MY),
4698
  BFD_JUMP_TABLE_RELOCS (MY),
4699
  BFD_JUMP_TABLE_WRITE (MY),
4700
  BFD_JUMP_TABLE_LINK (MY),
4701
  BFD_JUMP_TABLE_DYNAMIC (MY),
4702
4703
  /* Alternative_target.  */
4704
  NULL,
4705
4706
  (void *) MY_backend_data
4707
};