Merge in stuff accidently commited to sh3e branch
[deliverable/binutils-gdb.git] / bfd / coffgen.c
CommitLineData
075caafd 1/* Support for the generic parts of COFF, for BFD.
6dc6a81a 2 Copyright 1990, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
075caafd
ILT
3 Written by Cygnus Support.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
ae115e51 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
075caafd
ILT
20
21/* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
22 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
23
24/* This file contains COFF code that is not dependent on any
25 particular COFF target. There is only one version of this file in
26 libbfd.a, so no target specific code may be put in here. Or, to
27 put it another way,
28
29 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
30
31 If you need to add some target specific behaviour, add a new hook
32 function to bfd_coff_backend_data.
33
34 Some of these functions are also called by the ECOFF routines.
35 Those functions may not use any COFF specific information, such as
36 coff_data (abfd). */
37
38#include "bfd.h"
39#include "sysdep.h"
40#include "libbfd.h"
41#include "coff/internal.h"
42#include "libcoff.h"
43
f0500a41
ILT
44static void coff_fix_symbol_name
45 PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_size_type *,
46 asection **, bfd_size_type *));
47static boolean coff_write_symbol
48 PARAMS ((bfd *, asymbol *, combined_entry_type *, unsigned int *,
49 bfd_size_type *, asection **, bfd_size_type *));
50static boolean coff_write_alien_symbol
51 PARAMS ((bfd *, asymbol *, unsigned int *, bfd_size_type *,
52 asection **, bfd_size_type *));
53static boolean coff_write_native_symbol
54 PARAMS ((bfd *, coff_symbol_type *, unsigned int *, bfd_size_type *,
55 asection **, bfd_size_type *));
c80cc833
ILT
56static void coff_pointerize_aux
57 PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
58 unsigned int, combined_entry_type *));
bfe8224f 59
9fbe895a
ILT
60#define STRING_SIZE_SIZE (4)
61
075caafd
ILT
62/* Take a section header read from a coff file (in HOST byte order),
63 and make a BFD "section" out of it. This is used by ECOFF. */
6dc6a81a 64static boolean
25057836 65make_a_section_from_file (abfd, hdr, target_index)
6dc6a81a
ILT
66 bfd *abfd;
67 struct internal_scnhdr *hdr;
25057836 68 unsigned int target_index;
075caafd 69{
6dc6a81a 70 asection *return_section;
075caafd 71 char *name;
6dc6a81a 72
075caafd 73 /* Assorted wastage to null-terminate the name, thanks AT&T! */
6dc6a81a
ILT
74 name = bfd_alloc (abfd, sizeof (hdr->s_name) + 1);
75 if (name == NULL)
76 {
d1ad85a6 77 bfd_set_error (bfd_error_no_memory);
075caafd
ILT
78 return false;
79 }
6dc6a81a 80 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
075caafd
ILT
81 name[sizeof (hdr->s_name)] = 0;
82
c80cc833 83 return_section = bfd_make_section_anyway (abfd, name);
075caafd
ILT
84 if (return_section == NULL)
85 return false;
86
87 /* s_paddr is presumed to be = to s_vaddr */
88
89 return_section->vma = hdr->s_vaddr;
c7e76b5e 90 return_section->lma = return_section->vma;
075caafd
ILT
91 return_section->_raw_size = hdr->s_size;
92 return_section->filepos = hdr->s_scnptr;
6dc6a81a 93 return_section->rel_filepos = hdr->s_relptr;
075caafd
ILT
94 return_section->reloc_count = hdr->s_nreloc;
95
96 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
97
6dc6a81a 98 return_section->line_filepos = hdr->s_lnnoptr;
075caafd
ILT
99
100 return_section->lineno_count = hdr->s_nlnno;
101 return_section->userdata = NULL;
102 return_section->next = (asection *) NULL;
e8fbe6d9 103 return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name);
075caafd
ILT
104
105 return_section->target_index = target_index;
106
2d1e6c9c
KR
107 /* At least on i386-coff, the line number count for a shared library
108 section must be ignored. */
c16313f0 109 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2d1e6c9c
KR
110 return_section->lineno_count = 0;
111
075caafd
ILT
112 if (hdr->s_nreloc != 0)
113 return_section->flags |= SEC_RELOC;
114 /* FIXME: should this check 'hdr->s_size > 0' */
115 if (hdr->s_scnptr != 0)
116 return_section->flags |= SEC_HAS_CONTENTS;
117 return true;
118}
119
120/* Read in a COFF object and make it into a BFD. This is used by
121 ECOFF as well. */
122
2f3508ad 123static const bfd_target *
25057836 124coff_real_object_p (abfd, nscns, internal_f, internal_a)
6dc6a81a
ILT
125 bfd *abfd;
126 unsigned nscns;
25057836
JL
127 struct internal_filehdr *internal_f;
128 struct internal_aouthdr *internal_a;
075caafd 129{
c7e76b5e
ILT
130 flagword oflags = abfd->flags;
131 bfd_vma ostart = bfd_get_start_address (abfd);
075caafd 132 PTR tdata;
6dc6a81a 133 size_t readsize; /* length of file_info */
075caafd
ILT
134 unsigned int scnhsz;
135 char *external_sections;
136
c7e76b5e
ILT
137 if (!(internal_f->f_flags & F_RELFLG))
138 abfd->flags |= HAS_RELOC;
139 if ((internal_f->f_flags & F_EXEC))
140 abfd->flags |= EXEC_P;
141 if (!(internal_f->f_flags & F_LNNO))
142 abfd->flags |= HAS_LINENO;
143 if (!(internal_f->f_flags & F_LSYMS))
144 abfd->flags |= HAS_LOCALS;
145
146 /* FIXME: How can we set D_PAGED correctly? */
147 if ((internal_f->f_flags & F_EXEC) != 0)
148 abfd->flags |= D_PAGED;
149
150 bfd_get_symcount (abfd) = internal_f->f_nsyms;
151 if (internal_f->f_nsyms)
152 abfd->flags |= HAS_SYMS;
153
154 if (internal_a != (struct internal_aouthdr *) NULL)
155 bfd_get_start_address (abfd) = internal_a->entry;
156 else
157 bfd_get_start_address (abfd) = 0;
158
159 /* Set up the tdata area. ECOFF uses its own routine, and overrides
160 abfd->flags. */
27f524a3 161 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
075caafd
ILT
162 if (tdata == NULL)
163 return 0;
164
165 scnhsz = bfd_coff_scnhsz (abfd);
166 readsize = nscns * scnhsz;
6dc6a81a 167 external_sections = (char *) bfd_alloc (abfd, readsize);
9783e04a
DM
168 if (!external_sections)
169 {
d1ad85a6 170 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
171 goto fail;
172 }
075caafd 173
6dc6a81a 174 if (bfd_read ((PTR) external_sections, 1, readsize, abfd) != readsize)
075caafd 175 goto fail;
075caafd
ILT
176
177 /* Now copy data as required; construct all asections etc */
6dc6a81a
ILT
178 if (nscns != 0)
179 {
180 unsigned int i;
181 for (i = 0; i < nscns; i++)
182 {
183 struct internal_scnhdr tmp;
c7e76b5e
ILT
184 bfd_coff_swap_scnhdr_in (abfd,
185 (PTR) (external_sections + i * scnhsz),
6dc6a81a
ILT
186 (PTR) & tmp);
187 make_a_section_from_file (abfd, &tmp, i + 1);
188 }
075caafd 189 }
075caafd 190
6dc6a81a
ILT
191 /* make_abs_section (abfd); */
192
075caafd
ILT
193 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
194 goto fail;
195
075caafd 196 return abfd->xvec;
c7e76b5e 197
075caafd 198 fail:
6dc6a81a 199 bfd_release (abfd, tdata);
c7e76b5e
ILT
200 abfd->flags = oflags;
201 bfd_get_start_address (abfd) = ostart;
6dc6a81a 202 return (const bfd_target *) NULL;
075caafd
ILT
203}
204
d1ad85a6 205/* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
075caafd
ILT
206 not a COFF file. This is also used by ECOFF. */
207
2f3508ad 208const bfd_target *
25057836 209coff_object_p (abfd)
6dc6a81a 210 bfd *abfd;
075caafd
ILT
211{
212 unsigned int filhsz;
213 unsigned int aoutsz;
6dc6a81a 214 int nscns;
075caafd
ILT
215 PTR filehdr;
216 struct internal_filehdr internal_f;
217 struct internal_aouthdr internal_a;
218
075caafd
ILT
219 /* figure out how much to read */
220 filhsz = bfd_coff_filhsz (abfd);
221 aoutsz = bfd_coff_aoutsz (abfd);
222
223 filehdr = bfd_alloc (abfd, filhsz);
224 if (filehdr == NULL)
225 return 0;
6dc6a81a 226 if (bfd_read (filehdr, 1, filhsz, abfd) != filhsz)
25057836
JL
227 {
228 if (bfd_get_error () != bfd_error_system_call)
229 bfd_set_error (bfd_error_wrong_format);
230 return 0;
231 }
6dc6a81a 232 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
075caafd
ILT
233 bfd_release (abfd, filehdr);
234
6dc6a81a
ILT
235 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false)
236 {
237 bfd_set_error (bfd_error_wrong_format);
238 return 0;
239 }
240 nscns = internal_f.f_nscns;
075caafd 241
6dc6a81a
ILT
242 if (internal_f.f_opthdr)
243 {
244 PTR opthdr;
075caafd 245
6dc6a81a
ILT
246 opthdr = bfd_alloc (abfd, aoutsz);
247 if (opthdr == NULL)
248 return 0;;
249 if (bfd_read (opthdr, 1, aoutsz, abfd) != aoutsz)
250 {
251 return 0;
252 }
253 bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) & internal_a);
075caafd 254 }
075caafd
ILT
255
256 /* Seek past the opt hdr stuff */
6dc6a81a 257 if (bfd_seek (abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET)
c16313f0
ILT
258 != 0)
259 return NULL;
075caafd 260
6dc6a81a
ILT
261 return coff_real_object_p (abfd, nscns, &internal_f,
262 (internal_f.f_opthdr != 0
263 ? &internal_a
264 : (struct internal_aouthdr *) NULL));
075caafd
ILT
265}
266
267/* Get the BFD section from a COFF symbol section number. */
268
e8fbe6d9 269asection *
25057836 270coff_section_from_bfd_index (abfd, index)
6dc6a81a
ILT
271 bfd *abfd;
272 int index;
075caafd
ILT
273{
274 struct sec *answer = abfd->sections;
275
6dc6a81a 276 if (index == N_ABS)
e8fbe6d9 277 return bfd_abs_section_ptr;
075caafd 278 if (index == N_UNDEF)
e8fbe6d9 279 return bfd_und_section_ptr;
6dc6a81a
ILT
280 if (index == N_DEBUG)
281 return bfd_abs_section_ptr;
282
283 while (answer)
284 {
075caafd 285 if (answer->target_index == index)
6dc6a81a 286 return answer;
075caafd
ILT
287 answer = answer->next;
288 }
c16313f0
ILT
289
290 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
291 has a bad symbol table in biglitpow.o. */
e8fbe6d9 292 return bfd_und_section_ptr;
075caafd
ILT
293}
294
295/* Get the upper bound of a COFF symbol table. */
296
326e32d7 297long
6dc6a81a
ILT
298coff_get_symtab_upper_bound (abfd)
299 bfd *abfd;
075caafd 300{
6dc6a81a 301 if (!bfd_coff_slurp_symbol_table (abfd))
326e32d7 302 return -1;
075caafd 303
6dc6a81a 304 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
075caafd
ILT
305}
306
307
308/* Canonicalize a COFF symbol table. */
309
326e32d7 310long
25057836 311coff_get_symtab (abfd, alocation)
74942465
ILT
312 bfd *abfd;
313 asymbol **alocation;
075caafd 314{
74942465
ILT
315 unsigned int counter;
316 coff_symbol_type *symbase;
317 coff_symbol_type **location = (coff_symbol_type **) alocation;
318
6dc6a81a 319 if (!bfd_coff_slurp_symbol_table (abfd))
74942465
ILT
320 return -1;
321
322 symbase = obj_symbols (abfd);
323 counter = bfd_get_symcount (abfd);
324 while (counter-- > 0)
325 *location++ = symbase++;
326
327 *location = NULL;
328
329 return bfd_get_symcount (abfd);
075caafd
ILT
330}
331
867d923d
ILT
332/* Get the name of a symbol. The caller must pass in a buffer of size
333 >= SYMNMLEN + 1. */
334
335const char *
336_bfd_coff_internal_syment_name (abfd, sym, buf)
337 bfd *abfd;
338 const struct internal_syment *sym;
339 char *buf;
340{
341 /* FIXME: It's not clear this will work correctly if sizeof
342 (_n_zeroes) != 4. */
343 if (sym->_n._n_n._n_zeroes != 0
344 || sym->_n._n_n._n_offset == 0)
345 {
346 memcpy (buf, sym->_n._n_name, SYMNMLEN);
347 buf[SYMNMLEN] = '\0';
348 return buf;
349 }
350 else
351 {
352 const char *strings;
353
354 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
355 strings = obj_coff_strings (abfd);
356 if (strings == NULL)
357 {
358 strings = _bfd_coff_read_string_table (abfd);
359 if (strings == NULL)
360 return NULL;
361 }
362 return strings + sym->_n._n_n._n_offset;
363 }
364}
365
366/* Read in and swap the relocs. This returns a buffer holding the
367 relocs for section SEC in file ABFD. If CACHE is true and
368 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
369 the function is called again. If EXTERNAL_RELOCS is not NULL, it
370 is a buffer large enough to hold the unswapped relocs. If
371 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
372 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
373 value must be INTERNAL_RELOCS. The function returns NULL on error. */
374
375struct internal_reloc *
376_bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
377 require_internal, internal_relocs)
378 bfd *abfd;
379 asection *sec;
380 boolean cache;
381 bfd_byte *external_relocs;
382 boolean require_internal;
383 struct internal_reloc *internal_relocs;
384{
385 bfd_size_type relsz;
386 bfd_byte *free_external = NULL;
387 struct internal_reloc *free_internal = NULL;
388 bfd_byte *erel;
389 bfd_byte *erel_end;
390 struct internal_reloc *irel;
391
392 if (coff_section_data (abfd, sec) != NULL
393 && coff_section_data (abfd, sec)->relocs != NULL)
394 {
395 if (! require_internal)
396 return coff_section_data (abfd, sec)->relocs;
397 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
398 sec->reloc_count * sizeof (struct internal_reloc));
399 return internal_relocs;
400 }
401
402 relsz = bfd_coff_relsz (abfd);
403
404 if (external_relocs == NULL)
405 {
406 free_external = (bfd_byte *) malloc (sec->reloc_count * relsz);
407 if (free_external == NULL && sec->reloc_count > 0)
408 {
409 bfd_set_error (bfd_error_no_memory);
410 goto error_return;
411 }
412 external_relocs = free_external;
413 }
414
415 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
416 || (bfd_read (external_relocs, relsz, sec->reloc_count, abfd)
417 != relsz * sec->reloc_count))
418 goto error_return;
419
420 if (internal_relocs == NULL)
421 {
422 free_internal = ((struct internal_reloc *)
423 malloc (sec->reloc_count
424 * sizeof (struct internal_reloc)));
425 if (free_internal == NULL && sec->reloc_count > 0)
426 {
427 bfd_set_error (bfd_error_no_memory);
428 goto error_return;
429 }
430 internal_relocs = free_internal;
431 }
432
433 /* Swap in the relocs. */
434 erel = external_relocs;
435 erel_end = erel + relsz * sec->reloc_count;
436 irel = internal_relocs;
437 for (; erel < erel_end; erel += relsz, irel++)
438 bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
439
440 if (free_external != NULL)
441 {
442 free (free_external);
443 free_external = NULL;
444 }
445
446 if (cache && free_internal != NULL)
447 {
448 if (coff_section_data (abfd, sec) == NULL)
449 {
450 sec->used_by_bfd =
451 (PTR) bfd_zalloc (abfd,
452 sizeof (struct coff_section_tdata));
453 if (sec->used_by_bfd == NULL)
454 {
455 bfd_set_error (bfd_error_no_memory);
456 goto error_return;
457 }
458 coff_section_data (abfd, sec)->contents = NULL;
459 }
460 coff_section_data (abfd, sec)->relocs = free_internal;
461 }
462
463 return internal_relocs;
464
465 error_return:
466 if (free_external != NULL)
467 free (free_external);
468 if (free_internal != NULL)
469 free (free_internal);
470 return NULL;
471}
472
075caafd
ILT
473/* Set lineno_count for the output sections of a COFF file. */
474
27f524a3 475int
25057836 476coff_count_linenumbers (abfd)
69645d10 477 bfd *abfd;
075caafd 478{
6dc6a81a 479 unsigned int limit = bfd_get_symcount (abfd);
69645d10 480 unsigned int i;
27f524a3 481 int total = 0;
69645d10
ILT
482 asymbol **p;
483 asection *s;
484
485 if (limit == 0)
486 {
487 /* This may be from the backend linker, in which case the
488 lineno_count in the sections is correct. */
489 for (s = abfd->sections; s != NULL; s = s->next)
490 total += s->lineno_count;
491 return total;
492 }
493
494 for (s = abfd->sections; s != NULL; s = s->next)
495 BFD_ASSERT (s->lineno_count == 0);
496
497 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
498 {
499 asymbol *q_maybe = *p;
500
501 if (bfd_asymbol_flavour (q_maybe) == bfd_target_coff_flavour)
502 {
503 coff_symbol_type *q = coffsymbol (q_maybe);
504
d7731c7d
ILT
505 /* The AIX 4.1 compiler can sometimes generate line numbers
506 attached to debugging symbols. We try to simply ignore
507 those here. */
508 if (q->lineno != NULL
509 && q->symbol.section->owner != NULL)
69645d10
ILT
510 {
511 /* This symbol has line numbers. Increment the owning
6dc6a81a 512 section's linenumber count. */
69645d10
ILT
513 alent *l = q->lineno;
514
515 ++q->symbol.section->output_section->lineno_count;
516 ++total;
517 ++l;
518 while (l->line_number != 0)
519 {
520 ++total;
521 ++q->symbol.section->output_section->lineno_count;
522 ++l;
523 }
524 }
075caafd 525 }
075caafd 526 }
69645d10 527
27f524a3 528 return total;
075caafd
ILT
529}
530
531/* Takes a bfd and a symbol, returns a pointer to the coff specific
532 area of the symbol if there is one. */
533
330595d0 534/*ARGSUSED*/
075caafd 535coff_symbol_type *
25057836 536coff_symbol_from (ignore_abfd, symbol)
6dc6a81a
ILT
537 bfd *ignore_abfd;
538 asymbol *symbol;
075caafd 539{
6dc6a81a
ILT
540 if (bfd_asymbol_flavour (symbol) != bfd_target_coff_flavour)
541 return (coff_symbol_type *) NULL;
075caafd 542
6dc6a81a
ILT
543 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
544 return (coff_symbol_type *) NULL;
075caafd 545
6dc6a81a 546 return (coff_symbol_type *) symbol;
075caafd
ILT
547}
548
549static void
25057836
JL
550fixup_symbol_value (coff_symbol_ptr, syment)
551 coff_symbol_type *coff_symbol_ptr;
552 struct internal_syment *syment;
075caafd
ILT
553{
554
555 /* Normalize the symbol flags */
6dc6a81a
ILT
556 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
557 {
558 /* a common symbol is undefined with a value */
559 syment->n_scnum = N_UNDEF;
560 syment->n_value = coff_symbol_ptr->symbol.value;
075caafd 561 }
6dc6a81a
ILT
562 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING)
563 {
075caafd
ILT
564 syment->n_value = coff_symbol_ptr->symbol.value;
565 }
6dc6a81a
ILT
566 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
567 {
568 syment->n_scnum = N_UNDEF;
569 syment->n_value = 0;
570 }
571 else
572 {
573 if (coff_symbol_ptr->symbol.section)
574 {
575 syment->n_scnum =
576 coff_symbol_ptr->symbol.section->output_section->target_index;
577
578 syment->n_value =
579 coff_symbol_ptr->symbol.value +
580 coff_symbol_ptr->symbol.section->output_offset +
581 coff_symbol_ptr->symbol.section->output_section->vma;
582 }
583 else
584 {
585 BFD_ASSERT (0);
586 /* This can happen, but I don't know why yet (steve@cygnus.com) */
587 syment->n_scnum = N_ABS;
588 syment->n_value = coff_symbol_ptr->symbol.value;
589 }
590 }
075caafd
ILT
591}
592
6dc6a81a
ILT
593/* Run through all the symbols in the symbol table and work out what
594 their indexes into the symbol table will be when output.
075caafd 595
6dc6a81a
ILT
596 Coff requires that each C_FILE symbol points to the next one in the
597 chain, and that the last one points to the first external symbol. We
598 do that here too. */
075caafd 599
9783e04a 600boolean
c7e76b5e 601coff_renumber_symbols (bfd_ptr, first_undef)
25057836 602 bfd *bfd_ptr;
c7e76b5e 603 int *first_undef;
075caafd 604{
6dc6a81a 605 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
075caafd
ILT
606 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
607 unsigned int native_index = 0;
6dc6a81a 608 struct internal_syment *last_file = (struct internal_syment *) NULL;
075caafd
ILT
609 unsigned int symbol_index;
610
611 /* COFF demands that undefined symbols come after all other symbols.
c7e76b5e
ILT
612 Since we don't need to impose this extra knowledge on all our
613 client programs, deal with that here. Sort the symbol table;
614 just move the undefined symbols to the end, leaving the rest
615 alone. The O'Reilly book says that defined global symbols come
616 at the end before the undefined symbols, so we do that here as
617 well. */
075caafd
ILT
618 /* @@ Do we have some condition we could test for, so we don't always
619 have to do this? I don't think relocatability is quite right, but
620 I'm not certain. [raeburn:19920508.1711EST] */
621 {
622 asymbol **newsyms;
ae115e51 623 unsigned int i;
075caafd
ILT
624
625 newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
626 sizeof (asymbol *)
627 * (symbol_count + 1));
9783e04a
DM
628 if (!newsyms)
629 {
d1ad85a6 630 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
631 return false;
632 }
075caafd
ILT
633 bfd_ptr->outsymbols = newsyms;
634 for (i = 0; i < symbol_count; i++)
5f710a3a
ILT
635 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
636 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
7938b4cb 637 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
5f710a3a
ILT
638 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_FUNCTION))
639 != BSF_GLOBAL)))
075caafd 640 *newsyms++ = symbol_ptr_ptr[i];
c7e76b5e
ILT
641
642 for (i = 0; i < symbol_count; i++)
643 if (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
7938b4cb
ILT
644 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
645 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL
646 | BSF_NOT_AT_END
647 | BSF_FUNCTION))
648 == BSF_GLOBAL)))
c7e76b5e
ILT
649 *newsyms++ = symbol_ptr_ptr[i];
650
651 *first_undef = newsyms - bfd_ptr->outsymbols;
652
075caafd 653 for (i = 0; i < symbol_count; i++)
5f710a3a
ILT
654 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
655 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
075caafd
ILT
656 *newsyms++ = symbol_ptr_ptr[i];
657 *newsyms = (asymbol *) NULL;
658 symbol_ptr_ptr = bfd_ptr->outsymbols;
659 }
660
661 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
6dc6a81a
ILT
662 {
663 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
c7e76b5e 664 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
6dc6a81a
ILT
665 if (coff_symbol_ptr && coff_symbol_ptr->native)
666 {
075caafd
ILT
667 combined_entry_type *s = coff_symbol_ptr->native;
668 int i;
669
670 if (s->u.syment.n_sclass == C_FILE)
6dc6a81a
ILT
671 {
672 if (last_file != (struct internal_syment *) NULL)
673 last_file->n_value = native_index;
674 last_file = &(s->u.syment);
675 }
676 else
677 {
678
679 /* Modify the symbol values according to their section and
680 type */
681
682 fixup_symbol_value (coff_symbol_ptr, &(s->u.syment));
683 }
684 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
685 s[i].offset = native_index++;
075caafd 686 }
6dc6a81a
ILT
687 else
688 {
075caafd
ILT
689 native_index++;
690 }
6dc6a81a 691 }
075caafd 692 obj_conv_table_size (bfd_ptr) = native_index;
c7e76b5e 693
9783e04a 694 return true;
075caafd
ILT
695}
696
6dc6a81a
ILT
697/* Run thorough the symbol table again, and fix it so that all
698 pointers to entries are changed to the entries' index in the output
699 symbol table. */
075caafd 700
075caafd 701void
9783e04a
DM
702coff_mangle_symbols (bfd_ptr)
703 bfd *bfd_ptr;
075caafd 704{
9783e04a 705 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
075caafd
ILT
706 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
707 unsigned int symbol_index;
708
709 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
9783e04a
DM
710 {
711 coff_symbol_type *coff_symbol_ptr =
6dc6a81a 712 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
075caafd 713
9783e04a
DM
714 if (coff_symbol_ptr && coff_symbol_ptr->native)
715 {
075caafd
ILT
716 int i;
717 combined_entry_type *s = coff_symbol_ptr->native;
718
9783e04a
DM
719 if (s->fix_value)
720 {
721 /* FIXME: We should use a union here. */
722 s->u.syment.n_value =
723 ((combined_entry_type *) s->u.syment.n_value)->offset;
724 s->fix_value = 0;
075caafd 725 }
49488f2b
ILT
726 if (s->fix_line)
727 {
728 /* The value is the offset into the line number entries
729 for the symbol's section. On output, the symbol's
730 section should be N_DEBUG. */
731 s->u.syment.n_value =
732 (coff_symbol_ptr->symbol.section->output_section->line_filepos
733 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
734 coff_symbol_ptr->symbol.section =
735 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
736 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
737 }
6dc6a81a 738 for (i = 0; i < s->u.syment.n_numaux; i++)
9783e04a
DM
739 {
740 combined_entry_type *a = s + i + 1;
741 if (a->fix_tag)
742 {
743 a->u.auxent.x_sym.x_tagndx.l =
744 a->u.auxent.x_sym.x_tagndx.p->offset;
745 a->fix_tag = 0;
746 }
747 if (a->fix_end)
748 {
749 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
750 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
751 a->fix_end = 0;
752 }
753 if (a->fix_scnlen)
754 {
755 a->u.auxent.x_csect.x_scnlen.l =
756 a->u.auxent.x_csect.x_scnlen.p->offset;
757 a->fix_scnlen = 0;
758 }
075caafd 759 }
075caafd 760 }
9783e04a 761 }
075caafd
ILT
762}
763
075caafd 764static void
f0500a41
ILT
765coff_fix_symbol_name (abfd, symbol, native, string_size_p,
766 debug_string_section_p, debug_string_size_p)
25057836
JL
767 bfd *abfd;
768 asymbol *symbol;
769 combined_entry_type *native;
f0500a41
ILT
770 bfd_size_type *string_size_p;
771 asection **debug_string_section_p;
772 bfd_size_type *debug_string_size_p;
075caafd 773{
6dc6a81a 774 unsigned int name_length;
075caafd 775 union internal_auxent *auxent;
6dc6a81a 776 char *name = (char *) (symbol->name);
075caafd 777
6dc6a81a
ILT
778 if (name == (char *) NULL)
779 {
780 /* coff symbols always have names, so we'll make one up */
781 symbol->name = "strange";
782 name = (char *) symbol->name;
783 }
784 name_length = strlen (name);
075caafd 785
4372f33f
ILT
786 if (native->u.syment.n_sclass == C_FILE
787 && native->u.syment.n_numaux > 0)
6dc6a81a
ILT
788 {
789 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
790 auxent = &(native + 1)->u.auxent;
075caafd 791
6dc6a81a
ILT
792 if (bfd_coff_long_filenames (abfd))
793 {
794 if (name_length <= FILNMLEN)
795 {
796 strncpy (auxent->x_file.x_fname, name, FILNMLEN);
797 }
798 else
799 {
f0500a41 800 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
6dc6a81a 801 auxent->x_file.x_n.x_zeroes = 0;
f0500a41 802 *string_size_p += name_length + 1;
6dc6a81a
ILT
803 }
804 }
805 else
806 {
807 strncpy (auxent->x_file.x_fname, name, FILNMLEN);
808 if (name_length > FILNMLEN)
809 {
810 name[FILNMLEN] = '\0';
811 }
812 }
075caafd 813 }
075caafd 814 else
4372f33f 815 {
9783e04a
DM
816 if (name_length <= SYMNMLEN)
817 {
075caafd 818 /* This name will fit into the symbol neatly */
6dc6a81a 819 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
075caafd 820 }
6dc6a81a 821 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
9783e04a 822 {
f0500a41
ILT
823 native->u.syment._n._n_n._n_offset = (*string_size_p
824 + STRING_SIZE_SIZE);
075caafd 825 native->u.syment._n._n_n._n_zeroes = 0;
f0500a41 826 *string_size_p += name_length + 1;
075caafd 827 }
9783e04a
DM
828 else
829 {
830 long filepos;
831 bfd_byte buf[2];
832
833 /* This name should be written into the .debug section. For
834 some reason each name is preceded by a two byte length
835 and also followed by a null byte. FIXME: We assume that
836 the .debug section has already been created, and that it
837 is large enough. */
f0500a41
ILT
838 if (*debug_string_section_p == (asection *) NULL)
839 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
9783e04a
DM
840 filepos = bfd_tell (abfd);
841 bfd_put_16 (abfd, name_length + 1, buf);
6dc6a81a 842 if (!bfd_set_section_contents (abfd,
f0500a41 843 *debug_string_section_p,
6dc6a81a 844 (PTR) buf,
f0500a41 845 (file_ptr) *debug_string_size_p,
6dc6a81a
ILT
846 (bfd_size_type) 2)
847 || !bfd_set_section_contents (abfd,
f0500a41 848 *debug_string_section_p,
6dc6a81a 849 (PTR) symbol->name,
f0500a41
ILT
850 ((file_ptr) *debug_string_size_p
851 + 2),
6dc6a81a 852 (bfd_size_type) name_length + 1))
9783e04a
DM
853 abort ();
854 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
855 abort ();
f0500a41 856 native->u.syment._n._n_n._n_offset = *debug_string_size_p + 2;
9783e04a 857 native->u.syment._n._n_n._n_zeroes = 0;
f0500a41 858 *debug_string_size_p += name_length + 3;
9783e04a
DM
859 }
860 }
075caafd
ILT
861}
862
bfe8224f
ILT
863/* We need to keep track of the symbol index so that when we write out
864 the relocs we can get the index for a symbol. This method is a
865 hack. FIXME. */
866
74942465 867#define set_index(symbol, idx) ((symbol)->udata.i = (idx))
bfe8224f
ILT
868
869/* Write a symbol out to a COFF file. */
075caafd 870
bfe8224f 871static boolean
f0500a41
ILT
872coff_write_symbol (abfd, symbol, native, written, string_size_p,
873 debug_string_section_p, debug_string_size_p)
25057836
JL
874 bfd *abfd;
875 asymbol *symbol;
876 combined_entry_type *native;
bfe8224f 877 unsigned int *written;
f0500a41
ILT
878 bfd_size_type *string_size_p;
879 asection **debug_string_section_p;
880 bfd_size_type *debug_string_size_p;
075caafd 881{
bfe8224f
ILT
882 unsigned int numaux = native->u.syment.n_numaux;
883 int type = native->u.syment.n_type;
6dc6a81a 884 int class = native->u.syment.n_sclass;
075caafd
ILT
885 PTR buf;
886 bfd_size_type symesz;
887
075caafd 888 if (native->u.syment.n_sclass == C_FILE)
6dc6a81a 889 symbol->flags |= BSF_DEBUGGING;
075caafd 890
49488f2b
ILT
891 if (symbol->flags & BSF_DEBUGGING
892 && bfd_is_abs_section (symbol->section))
bfe8224f 893 {
6dc6a81a 894 native->u.syment.n_scnum = N_DEBUG;
bfe8224f 895 }
6dc6a81a 896 else if (bfd_is_abs_section (symbol->section))
bfe8224f 897 {
6dc6a81a 898 native->u.syment.n_scnum = N_ABS;
bfe8224f 899 }
e8fbe6d9 900 else if (bfd_is_und_section (symbol->section))
bfe8224f
ILT
901 {
902 native->u.syment.n_scnum = N_UNDEF;
903 }
6dc6a81a 904 else
bfe8224f
ILT
905 {
906 native->u.syment.n_scnum =
907 symbol->section->output_section->target_index;
908 }
6dc6a81a 909
f0500a41
ILT
910 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
911 debug_string_section_p, debug_string_size_p);
075caafd
ILT
912
913 symesz = bfd_coff_symesz (abfd);
914 buf = bfd_alloc (abfd, symesz);
9783e04a
DM
915 if (!buf)
916 {
d1ad85a6 917 bfd_set_error (bfd_error_no_memory);
bfe8224f 918 return false;
9783e04a 919 }
bfe8224f
ILT
920 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
921 if (bfd_write (buf, 1, symesz, abfd) != symesz)
922 return false;
075caafd
ILT
923 bfd_release (abfd, buf);
924
925 if (native->u.syment.n_numaux > 0)
926 {
927 bfd_size_type auxesz;
928 unsigned int j;
929
930 auxesz = bfd_coff_auxesz (abfd);
931 buf = bfd_alloc (abfd, auxesz);
9783e04a
DM
932 if (!buf)
933 {
d1ad85a6 934 bfd_set_error (bfd_error_no_memory);
bfe8224f 935 return false;
9783e04a 936 }
6dc6a81a 937 for (j = 0; j < native->u.syment.n_numaux; j++)
075caafd 938 {
bfe8224f
ILT
939 bfd_coff_swap_aux_out (abfd,
940 &((native + j + 1)->u.auxent),
941 type,
942 class,
943 j,
944 native->u.syment.n_numaux,
945 buf);
6dc6a81a 946 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
bfe8224f 947 return false;
075caafd
ILT
948 }
949 bfd_release (abfd, buf);
950 }
bfe8224f
ILT
951
952 /* Store the index for use when we write out the relocs. */
953 set_index (symbol, *written);
954
955 *written += numaux + 1;
956 return true;
075caafd
ILT
957}
958
bfe8224f
ILT
959/* Write out a symbol to a COFF file that does not come from a COFF
960 file originally. This symbol may have been created by the linker,
961 or we may be linking a non COFF file to a COFF file. */
075caafd 962
bfe8224f 963static boolean
f0500a41
ILT
964coff_write_alien_symbol (abfd, symbol, written, string_size_p,
965 debug_string_section_p, debug_string_size_p)
25057836
JL
966 bfd *abfd;
967 asymbol *symbol;
bfe8224f 968 unsigned int *written;
f0500a41
ILT
969 bfd_size_type *string_size_p;
970 asection **debug_string_section_p;
971 bfd_size_type *debug_string_size_p;
075caafd 972{
075caafd
ILT
973 combined_entry_type *native;
974 combined_entry_type dummy;
bfe8224f 975
075caafd 976 native = &dummy;
6dc6a81a
ILT
977 native->u.syment.n_type = T_NULL;
978 native->u.syment.n_flags = 0;
e8fbe6d9 979 if (bfd_is_und_section (symbol->section))
bfe8224f
ILT
980 {
981 native->u.syment.n_scnum = N_UNDEF;
982 native->u.syment.n_value = symbol->value;
075caafd 983 }
e61cfdf8 984 else if (bfd_is_com_section (symbol->section))
bfe8224f
ILT
985 {
986 native->u.syment.n_scnum = N_UNDEF;
987 native->u.syment.n_value = symbol->value;
075caafd 988 }
bfe8224f 989 else if (symbol->flags & BSF_DEBUGGING)
075caafd 990 {
a74d1517
ILT
991 /* There isn't much point to writing out a debugging symbol
992 unless we are prepared to convert it into COFF debugging
715cde57
ILT
993 format. So, we just ignore them. We must clobber the symbol
994 name to keep it from being put in the string table. */
995 symbol->name = "";
a74d1517 996 return true;
075caafd 997 }
bfe8224f
ILT
998 else
999 {
1000 native->u.syment.n_scnum =
1001 symbol->section->output_section->target_index;
1002 native->u.syment.n_value = (symbol->value
1003 + symbol->section->output_section->vma
1004 + symbol->section->output_offset);
1005
1006 /* Copy the any flags from the the file header into the symbol.
6dc6a81a 1007 FIXME: Why? */
bfe8224f
ILT
1008 {
1009 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1010 if (c != (coff_symbol_type *) NULL)
1011 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1012 }
075caafd
ILT
1013 }
1014
bfe8224f 1015 native->u.syment.n_type = 0;
075caafd 1016 if (symbol->flags & BSF_LOCAL)
bfe8224f 1017 native->u.syment.n_sclass = C_STAT;
075caafd 1018 else
bfe8224f
ILT
1019 native->u.syment.n_sclass = C_EXT;
1020 native->u.syment.n_numaux = 0;
075caafd 1021
f0500a41
ILT
1022 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1023 debug_string_section_p, debug_string_size_p);
075caafd
ILT
1024}
1025
bfe8224f
ILT
1026/* Write a native symbol to a COFF file. */
1027
1028static boolean
f0500a41
ILT
1029coff_write_native_symbol (abfd, symbol, written, string_size_p,
1030 debug_string_section_p, debug_string_size_p)
25057836
JL
1031 bfd *abfd;
1032 coff_symbol_type *symbol;
bfe8224f 1033 unsigned int *written;
f0500a41
ILT
1034 bfd_size_type *string_size_p;
1035 asection **debug_string_section_p;
1036 bfd_size_type *debug_string_size_p;
075caafd 1037{
075caafd 1038 combined_entry_type *native = symbol->native;
bfe8224f 1039 alent *lineno = symbol->lineno;
075caafd 1040
bfe8224f
ILT
1041 /* If this symbol has an associated line number, we must store the
1042 symbol index in the line number field. We also tag the auxent to
1043 point to the right place in the lineno table. */
d7731c7d 1044 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
bfe8224f
ILT
1045 {
1046 unsigned int count = 0;
1047 lineno[count].u.offset = *written;
1048 if (native->u.syment.n_numaux)
1049 {
6dc6a81a 1050 union internal_auxent *a = &((native + 1)->u.auxent);
075caafd 1051
bfe8224f
ILT
1052 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1053 symbol->symbol.section->output_section->moving_line_filepos;
1054 }
075caafd 1055
bfe8224f
ILT
1056 /* Count and relocate all other linenumbers. */
1057 count++;
1058 while (lineno[count].line_number != 0)
1059 {
075caafd 1060#if 0
bfe8224f
ILT
1061 /* 13 april 92. sac
1062 I've been told this, but still need proof:
1063 > The second bug is also in `bfd/coffcode.h'. This bug
1064 > causes the linker to screw up the pc-relocations for
1065 > all the line numbers in COFF code. This bug isn't only
1066 > specific to A29K implementations, but affects all
1067 > systems using COFF format binaries. Note that in COFF
1068 > object files, the line number core offsets output by
1069 > the assembler are relative to the start of each
1070 > procedure, not to the start of the .text section. This
1071 > patch relocates the line numbers relative to the
1072 > `native->u.syment.n_value' instead of the section
1073 > virtual address.
1074 > modular!olson@cs.arizona.edu (Jon Olson)
6dc6a81a 1075 */
bfe8224f 1076 lineno[count].u.offset += native->u.syment.n_value;
075caafd 1077#else
bfe8224f
ILT
1078 lineno[count].u.offset +=
1079 (symbol->symbol.section->output_section->vma
1080 + symbol->symbol.section->output_offset);
075caafd 1081#endif
bfe8224f
ILT
1082 count++;
1083 }
1084 symbol->done_lineno = true;
6dc6a81a 1085
bfe8224f
ILT
1086 symbol->symbol.section->output_section->moving_line_filepos +=
1087 count * bfd_coff_linesz (abfd);
1088 }
1089
f0500a41
ILT
1090 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1091 string_size_p, debug_string_section_p,
1092 debug_string_size_p);
075caafd
ILT
1093}
1094
bfe8224f
ILT
1095/* Write out the COFF symbols. */
1096
1097boolean
25057836 1098coff_write_symbols (abfd)
bfe8224f 1099 bfd *abfd;
075caafd 1100{
f0500a41
ILT
1101 bfd_size_type string_size;
1102 asection *debug_string_section;
1103 bfd_size_type debug_string_size;
bfe8224f 1104 unsigned int i;
6dc6a81a 1105 unsigned int limit = bfd_get_symcount (abfd);
bfe8224f
ILT
1106 unsigned int written = 0;
1107 asymbol **p;
075caafd
ILT
1108
1109 string_size = 0;
f0500a41 1110 debug_string_section = NULL;
9783e04a 1111 debug_string_size = 0;
075caafd
ILT
1112
1113 /* Seek to the right place */
6dc6a81a 1114 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
bfe8224f 1115 return false;
075caafd
ILT
1116
1117 /* Output all the symbols we have */
1118
1119 written = 0;
1120 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
bfe8224f
ILT
1121 {
1122 asymbol *symbol = *p;
1123 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
075caafd 1124
bfe8224f 1125 if (c_symbol == (coff_symbol_type *) NULL
6dc6a81a 1126 || c_symbol->native == (combined_entry_type *) NULL)
bfe8224f 1127 {
f0500a41
ILT
1128 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1129 &debug_string_section,
1130 &debug_string_size))
bfe8224f
ILT
1131 return false;
1132 }
1133 else
1134 {
f0500a41
ILT
1135 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1136 &string_size, &debug_string_section,
1137 &debug_string_size))
bfe8224f
ILT
1138 return false;
1139 }
1140 }
075caafd 1141
74942465
ILT
1142 obj_raw_syment_count (abfd) = written;
1143
075caafd
ILT
1144 /* Now write out strings */
1145
1146 if (string_size != 0)
6dc6a81a
ILT
1147 {
1148 unsigned int size = string_size + STRING_SIZE_SIZE;
1149 bfd_byte buffer[STRING_SIZE_SIZE];
075caafd 1150
9fbe895a 1151#if STRING_SIZE_SIZE == 4
6dc6a81a 1152 bfd_h_put_32 (abfd, size, buffer);
9fbe895a
ILT
1153#else
1154 #error Change bfd_h_put_32
1155#endif
6dc6a81a
ILT
1156 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1157 return false;
1158 for (p = abfd->outsymbols, i = 0;
1159 i < limit;
1160 i++, p++)
1161 {
1162 asymbol *q = *p;
1163 size_t name_length = strlen (q->name);
1164 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1165 size_t maxlen;
1166
1167 /* Figure out whether the symbol name should go in the string
1168 table. Symbol names that are short enough are stored
1169 directly in the syment structure. File names permit a
1170 different, longer, length in the syment structure. On
1171 XCOFF, some symbol names are stored in the .debug section
1172 rather than in the string table. */
1173
1174 if (c_symbol == NULL
1175 || c_symbol->native == NULL)
1176 {
1177 /* This is not a COFF symbol, so it certainly is not a
1178 file name, nor does it go in the .debug section. */
1179 maxlen = SYMNMLEN;
1180 }
1181 else if (bfd_coff_symname_in_debug (abfd,
1182 &c_symbol->native->u.syment))
1183 {
1184 /* This symbol name is in the XCOFF .debug section.
1185 Don't write it into the string table. */
1186 maxlen = name_length;
1187 }
4372f33f
ILT
1188 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1189 && c_symbol->native->u.syment.n_numaux > 0)
6dc6a81a
ILT
1190 maxlen = FILNMLEN;
1191 else
1192 maxlen = SYMNMLEN;
1193
1194 if (name_length > maxlen)
1195 {
1196 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1197 != name_length + 1)
1198 return false;
1199 }
1200 }
1201 }
bfe8224f
ILT
1202 else
1203 {
1204 /* We would normally not write anything here, but we'll write
6dc6a81a
ILT
1205 out 4 so that any stupid coff reader which tries to read the
1206 string table even when there isn't one won't croak. */
9fbe895a
ILT
1207 unsigned int size = STRING_SIZE_SIZE;
1208 bfd_byte buffer[STRING_SIZE_SIZE];
bfe8224f 1209
9fbe895a 1210#if STRING_SIZE_SIZE == 4
bfe8224f 1211 bfd_h_put_32 (abfd, size, buffer);
9fbe895a
ILT
1212#else
1213 #error Change bfd_h_put_32
1214#endif
1215 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1216 != STRING_SIZE_SIZE)
bfe8224f
ILT
1217 return false;
1218 }
9783e04a 1219
bfe8224f
ILT
1220 /* Make sure the .debug section was created to be the correct size.
1221 We should create it ourselves on the fly, but we don't because
1222 BFD won't let us write to any section until we know how large all
1223 the sections are. We could still do it by making another pass
1224 over the symbols. FIXME. */
9783e04a
DM
1225 BFD_ASSERT (debug_string_size == 0
1226 || (debug_string_section != (asection *) NULL
1227 && (BFD_ALIGN (debug_string_size,
1228 1 << debug_string_section->alignment_power)
1229 == bfd_section_size (abfd, debug_string_section))));
bfe8224f
ILT
1230
1231 return true;
075caafd
ILT
1232}
1233
9783e04a 1234boolean
25057836 1235coff_write_linenumbers (abfd)
6dc6a81a 1236 bfd *abfd;
075caafd 1237{
6dc6a81a 1238 asection *s;
075caafd
ILT
1239 bfd_size_type linesz;
1240 PTR buff;
1241
1242 linesz = bfd_coff_linesz (abfd);
1243 buff = bfd_alloc (abfd, linesz);
9783e04a
DM
1244 if (!buff)
1245 {
d1ad85a6 1246 bfd_set_error (bfd_error_no_memory);
25057836 1247 return false;
9783e04a 1248 }
6dc6a81a
ILT
1249 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1250 {
1251 if (s->lineno_count)
1252 {
1253 asymbol **q = abfd->outsymbols;
1254 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1255 return false;
1256 /* Find all the linenumbers in this section */
1257 while (*q)
1258 {
1259 asymbol *p = *q;
1260 if (p->section->output_section == s)
1261 {
1262 alent *l =
1263 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1264 (bfd_asymbol_bfd (p), p));
1265 if (l)
1266 {
1267 /* Found a linenumber entry, output */
1268 struct internal_lineno out;
1269 memset ((PTR) & out, 0, sizeof (out));
1270 out.l_lnno = 0;
1271 out.l_addr.l_symndx = l->u.offset;
1272 bfd_coff_swap_lineno_out (abfd, &out, buff);
1273 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1274 return false;
1275 l++;
1276 while (l->line_number)
1277 {
1278 out.l_lnno = l->line_number;
1279 out.l_addr.l_symndx = l->u.offset;
1280 bfd_coff_swap_lineno_out (abfd, &out, buff);
1281 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1282 return false;
1283 l++;
1284 }
1285 }
1286 }
1287 q++;
27f524a3 1288 }
075caafd 1289 }
075caafd 1290 }
075caafd 1291 bfd_release (abfd, buff);
9783e04a 1292 return true;
075caafd
ILT
1293}
1294
6dc6a81a
ILT
1295/*ARGSUSED */
1296alent *
25057836 1297coff_get_lineno (ignore_abfd, symbol)
6dc6a81a
ILT
1298 bfd *ignore_abfd;
1299 asymbol *symbol;
075caafd 1300{
6dc6a81a 1301 return coffsymbol (symbol)->lineno;
075caafd
ILT
1302}
1303
1304asymbol *
1305coff_section_symbol (abfd, name)
1306 bfd *abfd;
1307 char *name;
1308{
1309 asection *sec = bfd_make_section_old_way (abfd, name);
1310 asymbol *sym;
1311 combined_entry_type *csym;
1312
1313 sym = sec->symbol;
4c3721d5 1314 csym = coff_symbol_from (abfd, sym)->native;
075caafd
ILT
1315 /* Make sure back-end COFF stuff is there. */
1316 if (csym == 0)
1317 {
6dc6a81a
ILT
1318 struct foo
1319 {
1320 coff_symbol_type sym;
1321 /* @@FIXME This shouldn't use a fixed size!! */
1322 combined_entry_type e[10];
1323 };
075caafd
ILT
1324 struct foo *f;
1325 f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
9783e04a
DM
1326 if (!f)
1327 {
d1ad85a6 1328 bfd_set_error (bfd_error_no_error);
9783e04a
DM
1329 return NULL;
1330 }
075caafd
ILT
1331 memset ((char *) f, 0, sizeof (*f));
1332 coff_symbol_from (abfd, sym)->native = csym = f->e;
1333 }
1334 csym[0].u.syment.n_sclass = C_STAT;
1335 csym[0].u.syment.n_numaux = 1;
6dc6a81a 1336/* SF_SET_STATICS (sym); @@ ??? */
4c3721d5
ILT
1337 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1338 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1339 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1340
1341 if (sec->output_section == NULL)
075caafd 1342 {
4c3721d5
ILT
1343 sec->output_section = sec;
1344 sec->output_offset = 0;
075caafd 1345 }
4c3721d5 1346
075caafd
ILT
1347 return sym;
1348}
1349
1350/* This function transforms the offsets into the symbol table into
1351 pointers to syments. */
1352
1353static void
c80cc833 1354coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
25057836
JL
1355 bfd *abfd;
1356 combined_entry_type *table_base;
c80cc833
ILT
1357 combined_entry_type *symbol;
1358 unsigned int indaux;
25057836 1359 combined_entry_type *auxent;
075caafd 1360{
c80cc833
ILT
1361 int type = symbol->u.syment.n_type;
1362 int class = symbol->u.syment.n_sclass;
1363
1364 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1365 {
1366 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1367 (abfd, table_base, symbol, indaux, auxent))
1368 return;
1369 }
1370
075caafd 1371 /* Don't bother if this is a file or a section */
6dc6a81a
ILT
1372 if (class == C_STAT && type == T_NULL)
1373 return;
1374 if (class == C_FILE)
1375 return;
075caafd
ILT
1376
1377 /* Otherwise patch up */
1378#define N_TMASK coff_data (abfd)->local_n_tmask
1379#define N_BTSHFT coff_data (abfd)->local_n_btshft
6dc6a81a 1380 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK)
69645d10
ILT
1381 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1382 {
1383 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
c80cc833 1384 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
075caafd
ILT
1385 auxent->fix_end = 1;
1386 }
1387 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1388 generate one, so we must be careful to ignore it. */
6dc6a81a
ILT
1389 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1390 {
075caafd 1391 auxent->u.auxent.x_sym.x_tagndx.p =
6dc6a81a 1392 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
075caafd
ILT
1393 auxent->fix_tag = 1;
1394 }
1395}
1396
075caafd
ILT
1397/* Allocate space for the ".debug" section, and read it.
1398 We did not read the debug section until now, because
1399 we didn't want to go to the trouble until someone needed it. */
1400
1401static char *
25057836
JL
1402build_debug_section (abfd)
1403 bfd *abfd;
075caafd
ILT
1404{
1405 char *debug_section;
1406 long position;
1407
1408 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1409
6dc6a81a
ILT
1410 if (!sect)
1411 {
1412 bfd_set_error (bfd_error_no_debug_section);
1413 return NULL;
1414 }
075caafd
ILT
1415
1416 debug_section = (PTR) bfd_alloc (abfd,
1417 bfd_get_section_size_before_reloc (sect));
6dc6a81a
ILT
1418 if (debug_section == NULL)
1419 {
1420 bfd_set_error (bfd_error_no_memory);
1421 return NULL;
1422 }
075caafd
ILT
1423
1424 /* Seek to the beginning of the `.debug' section and read it.
1425 Save the current position first; it is needed by our caller.
1426 Then read debug section and reset the file pointer. */
1427
1428 position = bfd_tell (abfd);
c16313f0 1429 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
6dc6a81a 1430 || (bfd_read (debug_section,
c16313f0 1431 bfd_get_section_size_before_reloc (sect), 1, abfd)
6dc6a81a 1432 != bfd_get_section_size_before_reloc (sect))
c16313f0 1433 || bfd_seek (abfd, position, SEEK_SET) != 0)
075caafd 1434 return NULL;
075caafd
ILT
1435 return debug_section;
1436}
1437
1438
1439/* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
6dc6a81a
ILT
1440 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1441 be \0-terminated. */
075caafd 1442static char *
25057836
JL
1443copy_name (abfd, name, maxlen)
1444 bfd *abfd;
1445 char *name;
1446 int maxlen;
075caafd 1447{
6dc6a81a 1448 int len;
075caafd
ILT
1449 char *newname;
1450
6dc6a81a
ILT
1451 for (len = 0; len < maxlen; ++len)
1452 {
1453 if (name[len] == '\0')
1454 {
1455 break;
1456 }
075caafd 1457 }
075caafd 1458
6dc6a81a
ILT
1459 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1460 {
1461 bfd_set_error (bfd_error_no_memory);
1462 return (NULL);
1463 }
1464 strncpy (newname, name, len);
075caafd
ILT
1465 newname[len] = '\0';
1466 return newname;
1467}
1468
ae115e51
ILT
1469/* Read in the external symbols. */
1470
1471boolean
1472_bfd_coff_get_external_symbols (abfd)
1473 bfd *abfd;
1474{
1475 bfd_size_type symesz;
1476 size_t size;
1477 PTR syms;
1478
1479 if (obj_coff_external_syms (abfd) != NULL)
1480 return true;
1481
1482 symesz = bfd_coff_symesz (abfd);
1483
1484 size = obj_raw_syment_count (abfd) * symesz;
1485
90a7abbc 1486 syms = (PTR) malloc (size);
ae115e51
ILT
1487 if (syms == NULL && size != 0)
1488 {
1489 bfd_set_error (bfd_error_no_memory);
1490 return false;
1491 }
1492
1493 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1494 || bfd_read (syms, size, 1, abfd) != size)
1495 {
1496 if (syms != NULL)
1497 free (syms);
1498 return false;
1499 }
1500
1501 obj_coff_external_syms (abfd) = syms;
1502
1503 return true;
1504}
1505
1506/* Read in the external strings. The strings are not loaded until
1507 they are needed. This is because we have no simple way of
1508 detecting a missing string table in an archive. */
1509
1510const char *
1511_bfd_coff_read_string_table (abfd)
1512 bfd *abfd;
1513{
1514 char extstrsize[STRING_SIZE_SIZE];
1515 size_t strsize;
1516 char *strings;
1517
1518 if (obj_coff_strings (abfd) != NULL)
1519 return obj_coff_strings (abfd);
1520
1521 if (bfd_seek (abfd,
1522 (obj_sym_filepos (abfd)
1523 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1524 SEEK_SET) != 0)
1525 return NULL;
1526
1527 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1528 {
1529 if (bfd_get_error () != bfd_error_file_truncated)
1530 return NULL;
1531
1532 /* There is no string table. */
1533 strsize = STRING_SIZE_SIZE;
1534 }
1535 else
1536 {
1537#if STRING_SIZE_SIZE == 4
1538 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1539#else
1540 #error Change bfd_h_get_32
1541#endif
1542 }
1543
90a7abbc
ILT
1544 if (strsize < STRING_SIZE_SIZE)
1545 {
1546 (*_bfd_error_handler)
1547 ("%s: bad string table size %lu", bfd_get_filename (abfd),
1548 (unsigned long) strsize);
1549 bfd_set_error (bfd_error_bad_value);
1550 return NULL;
1551 }
1552
1553 strings = (char *) malloc (strsize);
ae115e51
ILT
1554 if (strings == NULL)
1555 {
1556 bfd_set_error (bfd_error_no_memory);
1557 return NULL;
1558 }
1559
1560 if (bfd_read (strings + STRING_SIZE_SIZE,
1561 strsize - STRING_SIZE_SIZE, 1, abfd)
1562 != strsize - STRING_SIZE_SIZE)
1563 {
1564 free (strings);
1565 return NULL;
1566 }
1567
1568 obj_coff_strings (abfd) = strings;
1569
1570 return strings;
1571}
1572
1573/* Free up the external symbols and strings read from a COFF file. */
1574
1575boolean
1576_bfd_coff_free_symbols (abfd)
1577 bfd *abfd;
1578{
1579 if (obj_coff_external_syms (abfd) != NULL
1580 && ! obj_coff_keep_syms (abfd))
1581 {
1582 free (obj_coff_external_syms (abfd));
1583 obj_coff_external_syms (abfd) = NULL;
1584 }
1585 if (obj_coff_strings (abfd) != NULL
1586 && ! obj_coff_keep_strings (abfd))
1587 {
1588 free (obj_coff_strings (abfd));
1589 obj_coff_strings (abfd) = NULL;
1590 }
1591 return true;
1592}
1593
075caafd
ILT
1594/* Read a symbol table into freshly bfd_allocated memory, swap it, and
1595 knit the symbol names into a normalized form. By normalized here I
1596 mean that all symbols have an n_offset pointer that points to a null-
1597 terminated string. */
1598
1599combined_entry_type *
25057836 1600coff_get_normalized_symtab (abfd)
6dc6a81a 1601 bfd *abfd;
075caafd 1602{
6dc6a81a
ILT
1603 combined_entry_type *internal;
1604 combined_entry_type *internal_ptr;
1605 combined_entry_type *symbol_ptr;
1606 combined_entry_type *internal_end;
075caafd 1607 bfd_size_type symesz;
075caafd
ILT
1608 char *raw_src;
1609 char *raw_end;
ae115e51 1610 const char *string_table = NULL;
6dc6a81a
ILT
1611 char *debug_section = NULL;
1612 unsigned long size;
075caafd 1613
74942465
ILT
1614 if (obj_raw_syments (abfd) != NULL)
1615 return obj_raw_syments (abfd);
1616
1617 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1618 internal = (combined_entry_type *) bfd_alloc (abfd, size);
1619 if (internal == NULL && size != 0)
9783e04a 1620 {
d1ad85a6 1621 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1622 return NULL;
1623 }
69645d10 1624 internal_end = internal + obj_raw_syment_count (abfd);
075caafd 1625
ae115e51 1626 if (! _bfd_coff_get_external_symbols (abfd))
74942465
ILT
1627 return NULL;
1628
ae115e51
ILT
1629 raw_src = (char *) obj_coff_external_syms (abfd);
1630
075caafd 1631 /* mark the end of the symbols */
ae115e51
ILT
1632 symesz = bfd_coff_symesz (abfd);
1633 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
74942465 1634
6dc6a81a
ILT
1635 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1636 probably possible. If one shows up, it will probably kill us. */
075caafd
ILT
1637
1638 /* Swap all the raw entries */
ae115e51 1639 for (internal_ptr = internal;
075caafd 1640 raw_src < raw_end;
6dc6a81a
ILT
1641 raw_src += symesz, internal_ptr++)
1642 {
075caafd
ILT
1643
1644 unsigned int i;
6dc6a81a
ILT
1645 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1646 (PTR) & internal_ptr->u.syment);
9783e04a 1647 internal_ptr->fix_value = 0;
075caafd
ILT
1648 internal_ptr->fix_tag = 0;
1649 internal_ptr->fix_end = 0;
9783e04a 1650 internal_ptr->fix_scnlen = 0;
075caafd 1651 symbol_ptr = internal_ptr;
85fe7cff 1652
075caafd
ILT
1653 for (i = 0;
1654 i < symbol_ptr->u.syment.n_numaux;
6dc6a81a 1655 i++)
075caafd 1656 {
6dc6a81a
ILT
1657 internal_ptr++;
1658 raw_src += symesz;
1659
1660 internal_ptr->fix_value = 0;
1661 internal_ptr->fix_tag = 0;
1662 internal_ptr->fix_end = 0;
1663 internal_ptr->fix_scnlen = 0;
1664 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1665 symbol_ptr->u.syment.n_type,
1666 symbol_ptr->u.syment.n_sclass,
1667 i, symbol_ptr->u.syment.n_numaux,
1668 &(internal_ptr->u.auxent));
c80cc833
ILT
1669 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1670 internal_ptr);
6dc6a81a 1671 }
075caafd
ILT
1672 }
1673
ae115e51
ILT
1674 /* Free the raw symbols, but not the strings (if we have them). */
1675 obj_coff_keep_strings (abfd) = true;
1676 if (! _bfd_coff_free_symbols (abfd))
1677 return NULL;
075caafd
ILT
1678
1679 for (internal_ptr = internal; internal_ptr < internal_end;
6dc6a81a
ILT
1680 internal_ptr++)
1681 {
1682 if (internal_ptr->u.syment.n_sclass == C_FILE
1683 && internal_ptr->u.syment.n_numaux > 0)
1684 {
1685 /* make a file symbol point to the name in the auxent, since
1686 the text ".file" is redundant */
1687 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1688 {
1689 /* the filename is a long one, point into the string table */
1690 if (string_table == NULL)
ae115e51
ILT
1691 {
1692 string_table = _bfd_coff_read_string_table (abfd);
1693 if (string_table == NULL)
1694 return NULL;
1695 }
075caafd 1696
6dc6a81a 1697 internal_ptr->u.syment._n._n_n._n_offset =
ae115e51
ILT
1698 ((long)
1699 (string_table
1700 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
6dc6a81a
ILT
1701 }
1702 else
1703 {
1704 /* ordinary short filename, put into memory anyway */
1705 internal_ptr->u.syment._n._n_n._n_offset = (long)
1706 copy_name (abfd, (internal_ptr + 1)->u.auxent.x_file.x_fname,
1707 FILNMLEN);
1708 }
1709 }
1710 else
1711 {
1712 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1713 {
1714 /* This is a "short" name. Make it long. */
1715 unsigned long i = 0;
1716 char *newstring = NULL;
1717
1718 /* find the length of this string without walking into memory
1719 that isn't ours. */
1720 for (i = 0; i < 8; ++i)
1721 {
1722 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1723 {
1724 break;
1725 } /* if end of string */
1726 } /* possible lengths of this string. */
1727
1728 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1729 {
1730 bfd_set_error (bfd_error_no_memory);
1731 return (NULL);
1732 } /* on error */
1733 memset (newstring, 0, i);
1734 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1735 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1736 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1737 }
1738 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1739 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1740 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1741 {
1742 /* Long name already. Point symbol at the string in the
1743 table. */
1744 if (string_table == NULL)
ae115e51
ILT
1745 {
1746 string_table = _bfd_coff_read_string_table (abfd);
1747 if (string_table == NULL)
1748 return NULL;
1749 }
1750 internal_ptr->u.syment._n._n_n._n_offset =
1751 ((long int)
1752 (string_table
1753 + internal_ptr->u.syment._n._n_n._n_offset));
6dc6a81a
ILT
1754 }
1755 else
1756 {
1757 /* Long name in debug section. Very similar. */
1758 if (debug_section == NULL)
1759 debug_section = build_debug_section (abfd);
1760 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1761 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1762 }
1763 }
1764 internal_ptr += internal_ptr->u.syment.n_numaux;
1765 }
1766
1767 obj_raw_syments (abfd) = internal;
ae115e51
ILT
1768 BFD_ASSERT (obj_raw_syment_count (abfd)
1769 == (unsigned int) (internal_ptr - internal));
075caafd
ILT
1770
1771 return (internal);
1772} /* coff_get_normalized_symtab() */
1773
326e32d7 1774long
25057836 1775coff_get_reloc_upper_bound (abfd, asect)
6dc6a81a
ILT
1776 bfd *abfd;
1777 sec_ptr asect;
075caafd 1778{
6dc6a81a
ILT
1779 if (bfd_get_format (abfd) != bfd_object)
1780 {
1781 bfd_set_error (bfd_error_invalid_operation);
1782 return -1;
1783 }
1784 return (asect->reloc_count + 1) * sizeof (arelent *);
075caafd
ILT
1785}
1786
1787asymbol *
25057836 1788coff_make_empty_symbol (abfd)
6dc6a81a 1789 bfd *abfd;
075caafd 1790{
6dc6a81a
ILT
1791 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1792 if (new == NULL)
1793 {
1794 bfd_set_error (bfd_error_no_memory);
1795 return (NULL);
1796 } /* on error */
9783e04a 1797 memset (new, 0, sizeof *new);
075caafd
ILT
1798 new->symbol.section = 0;
1799 new->native = 0;
1800 new->lineno = (alent *) NULL;
1801 new->done_lineno = false;
1802 new->symbol.the_bfd = abfd;
1803 return &new->symbol;
1804}
1805
2d1e6c9c
KR
1806/* Make a debugging symbol. */
1807
075caafd 1808asymbol *
2d1e6c9c
KR
1809coff_bfd_make_debug_symbol (abfd, ptr, sz)
1810 bfd *abfd;
1811 PTR ptr;
1812 unsigned long sz;
075caafd 1813{
6dc6a81a
ILT
1814 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1815 if (new == NULL)
1816 {
1817 bfd_set_error (bfd_error_no_memory);
1818 return (NULL);
1819 } /* on error */
075caafd
ILT
1820 /* @@ This shouldn't be using a constant multiplier. */
1821 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
9783e04a
DM
1822 if (!new->native)
1823 {
d1ad85a6 1824 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1825 return (NULL);
1826 } /* on error */
6dc6a81a
ILT
1827 new->symbol.section = bfd_abs_section_ptr;
1828 new->symbol.flags = BSF_DEBUGGING;
075caafd
ILT
1829 new->lineno = (alent *) NULL;
1830 new->done_lineno = false;
1831 new->symbol.the_bfd = abfd;
1832 return &new->symbol;
1833}
1834
6dc6a81a 1835/*ARGSUSED */
2d1e6c9c
KR
1836void
1837coff_get_symbol_info (abfd, symbol, ret)
1838 bfd *abfd;
1839 asymbol *symbol;
1840 symbol_info *ret;
1841{
1842 bfd_symbol_info (symbol, ret);
867d923d
ILT
1843 if (coffsymbol (symbol)->native != NULL
1844 && coffsymbol (symbol)->native->fix_value)
1845 {
1846 combined_entry_type *psym;
1847
1848 psym = ((combined_entry_type *)
1849 coffsymbol (symbol)->native->u.syment.n_value);
1850 ret->value = (bfd_vma) (psym - obj_raw_syments (abfd));
1851 }
2d1e6c9c
KR
1852}
1853
e61cfdf8
ILT
1854/* Print out information about COFF symbol. */
1855
075caafd 1856void
e61cfdf8
ILT
1857coff_print_symbol (abfd, filep, symbol, how)
1858 bfd *abfd;
1859 PTR filep;
1860 asymbol *symbol;
1861 bfd_print_symbol_type how;
075caafd 1862{
e61cfdf8
ILT
1863 FILE *file = (FILE *) filep;
1864
1865 switch (how)
1866 {
075caafd 1867 case bfd_print_symbol_name:
e61cfdf8 1868 fprintf (file, "%s", symbol->name);
075caafd 1869 break;
e61cfdf8 1870
075caafd 1871 case bfd_print_symbol_more:
e61cfdf8 1872 fprintf (file, "coff %s %s",
6dc6a81a
ILT
1873 coffsymbol (symbol)->native ? "n" : "g",
1874 coffsymbol (symbol)->lineno ? "l" : " ");
075caafd 1875 break;
075caafd 1876
e61cfdf8 1877 case bfd_print_symbol_all:
6dc6a81a 1878 if (coffsymbol (symbol)->native)
075caafd 1879 {
49488f2b 1880 unsigned long val;
e61cfdf8
ILT
1881 unsigned int aux;
1882 combined_entry_type *combined = coffsymbol (symbol)->native;
1883 combined_entry_type *root = obj_raw_syments (abfd);
6dc6a81a
ILT
1884 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
1885
1886 fprintf (file, "[%3ld]", (long) (combined - root));
e61cfdf8 1887
49488f2b
ILT
1888 if (! combined->fix_value)
1889 val = (unsigned long) combined->u.syment.n_value;
1890 else
1891 val = ((unsigned long)
1892 ((combined_entry_type *) combined->u.syment.n_value
1893 - root));
1894
e61cfdf8 1895 fprintf (file,
6dc6a81a 1896 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
e61cfdf8
ILT
1897 combined->u.syment.n_scnum,
1898 combined->u.syment.n_flags,
1899 combined->u.syment.n_type,
1900 combined->u.syment.n_sclass,
1901 combined->u.syment.n_numaux,
49488f2b 1902 val,
e61cfdf8
ILT
1903 symbol->name);
1904
6dc6a81a 1905 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
e61cfdf8
ILT
1906 {
1907 combined_entry_type *auxp = combined + aux + 1;
1908 long tagndx;
1909
1910 if (auxp->fix_tag)
1911 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
1912 else
1913 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
1914
1915 fprintf (file, "\n");
0fc9ada9
ILT
1916
1917 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
1918 continue;
1919
e61cfdf8
ILT
1920 switch (combined->u.syment.n_sclass)
1921 {
1922 case C_FILE:
1923 fprintf (file, "File ");
1924 break;
e61cfdf8 1925
de733a0e
KR
1926 case C_STAT:
1927 if (combined->u.syment.n_type == T_NULL)
1928 /* probably a section symbol? */
1929 {
1930 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
1931 (long) auxp->u.auxent.x_scn.x_scnlen,
1932 auxp->u.auxent.x_scn.x_nreloc,
1933 auxp->u.auxent.x_scn.x_nlinno);
1934 break;
1935 }
1936 /* else fall through */
1937
1938 default:
4c3721d5 1939 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
e61cfdf8
ILT
1940 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
1941 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
1942 tagndx);
69645d10
ILT
1943 if (auxp->fix_end)
1944 fprintf (file, " endndx %ld",
1945 ((long)
1946 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
1947 - root)));
e61cfdf8
ILT
1948 break;
1949 }
075caafd 1950 }
6dc6a81a 1951
e61cfdf8
ILT
1952 if (l)
1953 {
e4b6b3e7 1954 fprintf (file, "\n%s :", l->u.sym->name);
e61cfdf8 1955 l++;
6dc6a81a 1956 while (l->line_number)
e61cfdf8 1957 {
4c3721d5
ILT
1958 fprintf (file, "\n%4d : 0x%lx",
1959 l->line_number,
1960 ((unsigned long)
1961 (l->u.offset + symbol->section->vma)));
e61cfdf8
ILT
1962 l++;
1963 }
1964 }
6dc6a81a 1965 }
e61cfdf8 1966 else
075caafd 1967 {
e61cfdf8
ILT
1968 bfd_print_symbol_vandf ((PTR) file, symbol);
1969 fprintf (file, " %-5s %s %s %s",
1970 symbol->section->name,
6dc6a81a
ILT
1971 coffsymbol (symbol)->native ? "n" : "g",
1972 coffsymbol (symbol)->lineno ? "l" : " ",
e61cfdf8 1973 symbol->name);
075caafd 1974 }
075caafd
ILT
1975 }
1976}
1977
1978/* Provided a BFD, a section and an offset into the section, calculate
1979 and return the name of the source file and the line nearest to the
1980 wanted location. */
1981
330595d0 1982/*ARGSUSED*/
075caafd 1983boolean
25057836
JL
1984coff_find_nearest_line (abfd, section, ignore_symbols, offset, filename_ptr,
1985 functionname_ptr, line_ptr)
6dc6a81a
ILT
1986 bfd *abfd;
1987 asection *section;
1988 asymbol **ignore_symbols;
1989 bfd_vma offset;
1990 CONST char **filename_ptr;
1991 CONST char **functionname_ptr;
1992 unsigned int *line_ptr;
075caafd 1993{
a74d1517 1994 unsigned int i;
25b5a53d 1995 unsigned int line_base;
6dc6a81a 1996 coff_data_type *cof = coff_data (abfd);
075caafd
ILT
1997 /* Run through the raw syments if available */
1998 combined_entry_type *p;
a74d1517 1999 combined_entry_type *pend;
6dc6a81a 2000 alent *l;
25b5a53d 2001 struct coff_section_tdata *sec_data;
075caafd
ILT
2002
2003 *filename_ptr = 0;
2004 *functionname_ptr = 0;
2005 *line_ptr = 0;
2006
2007 /* Don't try and find line numbers in a non coff file */
2008 if (abfd->xvec->flavour != bfd_target_coff_flavour)
2009 return false;
2010
2011 if (cof == NULL)
2012 return false;
2013
a74d1517 2014 /* Find the first C_FILE symbol. */
075caafd 2015 p = cof->raw_syments;
a74d1517
ILT
2016 pend = p + cof->raw_syment_count;
2017 while (p < pend)
2018 {
2019 if (p->u.syment.n_sclass == C_FILE)
2020 break;
2021 p += 1 + p->u.syment.n_numaux;
2022 }
075caafd 2023
a74d1517
ILT
2024 if (p < pend)
2025 {
6d04c6d4
ILT
2026 bfd_vma maxdiff;
2027
a74d1517 2028 /* Look through the C_FILE symbols to find the best one. */
075caafd 2029 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
6d04c6d4 2030 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
a74d1517
ILT
2031 while (1)
2032 {
2033 combined_entry_type *p2;
2034
6d04c6d4
ILT
2035 for (p2 = p + 1 + p->u.syment.n_numaux;
2036 p2 < pend;
2037 p2 += 1 + p2->u.syment.n_numaux)
a74d1517 2038 {
6d04c6d4
ILT
2039 if (p2->u.syment.n_scnum > 0
2040 && (section
2041 == coff_section_from_bfd_index (abfd,
2042 p2->u.syment.n_scnum)))
a74d1517 2043 break;
6d04c6d4
ILT
2044 if (p2->u.syment.n_sclass == C_FILE)
2045 {
2046 p2 = pend;
2047 break;
2048 }
a74d1517 2049 }
6d04c6d4 2050
a74d1517 2051 if (p2 < pend
ae115e51
ILT
2052 && offset >= (bfd_vma) p2->u.syment.n_value
2053 && offset - (bfd_vma) p2->u.syment.n_value < maxdiff)
6d04c6d4
ILT
2054 {
2055 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2056 maxdiff = offset - p2->u.syment.n_value;
2057 }
2058
2059 /* Avoid endless loops on erroneous files by ensuring that
2060 we always move forward in the file. */
2061 if (p - cof->raw_syments >= p->u.syment.n_value)
a74d1517
ILT
2062 break;
2063
6d04c6d4
ILT
2064 p = cof->raw_syments + p->u.syment.n_value;
2065 if (p > pend || p->u.syment.n_sclass != C_FILE)
2066 break;
a74d1517 2067 }
075caafd 2068 }
a74d1517 2069
075caafd 2070 /* Now wander though the raw linenumbers of the section */
25b5a53d
ILT
2071 /* If we have been called on this section before, and the offset we
2072 want is further down then we can prime the lookup loop. */
2073 sec_data = coff_section_data (abfd, section);
2074 if (sec_data != NULL
2075 && sec_data->i > 0
2076 && offset >= sec_data->offset)
2077 {
2078 i = sec_data->i;
2079 *functionname_ptr = sec_data->function;
2080 line_base = sec_data->line_base;
6dc6a81a
ILT
2081 }
2082 else
2083 {
2084 i = 0;
25b5a53d 2085 line_base = 0;
6dc6a81a 2086 }
25b5a53d 2087
85fe7cff 2088 l = &section->lineno[i];
075caafd 2089
6dc6a81a
ILT
2090 for (; i < section->lineno_count; i++)
2091 {
2092 if (l->line_number == 0)
2093 {
2094 /* Get the symbol this line number points at */
2095 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2096 if (coff->symbol.value > offset)
2097 break;
2098 *functionname_ptr = coff->symbol.name;
2099 if (coff->native)
2100 {
2101 combined_entry_type *s = coff->native;
2102 s = s + 1 + s->u.syment.n_numaux;
2103
2104 /* In XCOFF a debugging symbol can follow the function
2105 symbol. */
2106 if (s->u.syment.n_scnum == N_DEBUG)
2107 s = s + 1 + s->u.syment.n_numaux;
2108
2109 /*
2110 S should now point to the .bf of the function
2111 */
2112 if (s->u.syment.n_numaux)
2113 {
2114 /*
2115 The linenumber is stored in the auxent
2116 */
2117 union internal_auxent *a = &((s + 1)->u.auxent);
2118 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2119 *line_ptr = line_base;
2120 }
2121 }
075caafd 2122 }
6dc6a81a
ILT
2123 else
2124 {
2125 if (l->u.offset + bfd_get_section_vma (abfd, section) > offset)
2126 break;
2127 *line_ptr = l->line_number + line_base - 1;
2128 }
2129 l++;
075caafd 2130 }
075caafd 2131
25b5a53d
ILT
2132 /* Cache the results for the next call. */
2133 if (sec_data == NULL)
2134 {
2135 section->used_by_bfd =
2136 ((PTR) bfd_zalloc (abfd,
2137 sizeof (struct coff_section_tdata)));
867d923d 2138 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
25b5a53d
ILT
2139 }
2140 if (sec_data != NULL)
2141 {
2142 sec_data->offset = offset;
2143 sec_data->i = i;
2144 sec_data->function = *functionname_ptr;
2145 sec_data->line_base = line_base;
2146 }
075caafd
ILT
2147
2148 return true;
2149}
2150
2151int
25057836
JL
2152coff_sizeof_headers (abfd, reloc)
2153 bfd *abfd;
2154 boolean reloc;
075caafd 2155{
6dc6a81a 2156 size_t size;
075caafd 2157
6dc6a81a
ILT
2158 if (reloc == false)
2159 {
2160 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
075caafd 2161 }
6dc6a81a
ILT
2162 else
2163 {
2164 size = bfd_coff_filhsz (abfd);
075caafd
ILT
2165 }
2166
6dc6a81a
ILT
2167 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2168 return size;
075caafd 2169}
This page took 0.226985 seconds and 4 git commands to generate.