Import updated translations supplied by the Translation Project.
[deliverable/binutils-gdb.git] / bfd / coffgen.c
CommitLineData
252b5132 1/* Support for the generic parts of COFF, for BFD.
4b95cf5c 2 Copyright (C) 1990-2014 Free Software Foundation, Inc.
252b5132
RH
3 Written by Cygnus Support.
4
c8e7bf0d 5 This file is part of BFD, the Binary File Descriptor library.
252b5132 6
c8e7bf0d
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
cd123cb7 9 the Free Software Foundation; either version 3 of the License, or
c8e7bf0d 10 (at your option) any later version.
252b5132 11
c8e7bf0d
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
c8e7bf0d
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
cd123cb7
NC
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
252b5132
RH
21
22/* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
23 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
24
25/* This file contains COFF code that is not dependent on any
26 particular COFF target. There is only one version of this file in
27 libbfd.a, so no target specific code may be put in here. Or, to
28 put it another way,
29
30 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
31
32 If you need to add some target specific behaviour, add a new hook
33 function to bfd_coff_backend_data.
34
35 Some of these functions are also called by the ECOFF routines.
36 Those functions may not use any COFF specific information, such as
37 coff_data (abfd). */
38
252b5132 39#include "sysdep.h"
3db64b00 40#include "bfd.h"
252b5132
RH
41#include "libbfd.h"
42#include "coff/internal.h"
43#include "libcoff.h"
44
252b5132
RH
45/* Take a section header read from a coff file (in HOST byte order),
46 and make a BFD "section" out of it. This is used by ECOFF. */
c8e7bf0d 47
b34976b6 48static bfd_boolean
c8e7bf0d
NC
49make_a_section_from_file (bfd *abfd,
50 struct internal_scnhdr *hdr,
51 unsigned int target_index)
252b5132
RH
52{
53 asection *return_section;
54 char *name;
b34976b6 55 bfd_boolean result = TRUE;
7c8ca0e4 56 flagword flags;
252b5132
RH
57
58 name = NULL;
59
88183869
DK
60 /* Handle long section names as in PE. On reading, we want to
61 accept long names if the format permits them at all, regardless
62 of the current state of the flag that dictates if we would generate
63 them in outputs; this construct checks if that is the case by
64 attempting to set the flag, without changing its state; the call
65 will fail for formats that do not support long names at all. */
66 if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd))
252b5132
RH
67 && hdr->s_name[0] == '/')
68 {
69 char buf[SCNNMLEN];
70 long strindex;
71 char *p;
72 const char *strings;
73
0408dee6
DK
74 /* Flag that this BFD uses long names, even though the format might
75 expect them to be off by default. This won't directly affect the
76 format of any output BFD created from this one, but the information
77 can be used to decide what to do. */
78 bfd_coff_set_long_section_names (abfd, TRUE);
252b5132
RH
79 memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
80 buf[SCNNMLEN - 1] = '\0';
81 strindex = strtol (buf, &p, 10);
82 if (*p == '\0' && strindex >= 0)
83 {
84 strings = _bfd_coff_read_string_table (abfd);
85 if (strings == NULL)
b34976b6 86 return FALSE;
252b5132
RH
87 /* FIXME: For extra safety, we should make sure that
88 strindex does not run us past the end, but right now we
89 don't know the length of the string table. */
90 strings += strindex;
a50b1753 91 name = (char *) bfd_alloc (abfd,
a29a8af8 92 (bfd_size_type) strlen (strings) + 1 + 1);
252b5132 93 if (name == NULL)
b34976b6 94 return FALSE;
252b5132
RH
95 strcpy (name, strings);
96 }
97 }
98
99 if (name == NULL)
100 {
101 /* Assorted wastage to null-terminate the name, thanks AT&T! */
a50b1753 102 name = (char *) bfd_alloc (abfd,
a29a8af8 103 (bfd_size_type) sizeof (hdr->s_name) + 1 + 1);
252b5132 104 if (name == NULL)
b34976b6 105 return FALSE;
252b5132
RH
106 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
107 name[sizeof (hdr->s_name)] = 0;
108 }
109
110 return_section = bfd_make_section_anyway (abfd, name);
111 if (return_section == NULL)
b34976b6 112 return FALSE;
252b5132
RH
113
114 return_section->vma = hdr->s_vaddr;
115 return_section->lma = hdr->s_paddr;
eea6121a 116 return_section->size = hdr->s_size;
252b5132
RH
117 return_section->filepos = hdr->s_scnptr;
118 return_section->rel_filepos = hdr->s_relptr;
119 return_section->reloc_count = hdr->s_nreloc;
120
121 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
122
123 return_section->line_filepos = hdr->s_lnnoptr;
124
125 return_section->lineno_count = hdr->s_nlnno;
126 return_section->userdata = NULL;
c8e7bf0d 127 return_section->next = NULL;
252b5132 128 return_section->target_index = target_index;
7c8ca0e4
NC
129
130 if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
131 & flags))
b34976b6 132 result = FALSE;
dc810e39 133
7c8ca0e4 134 return_section->flags = flags;
252b5132
RH
135
136 /* At least on i386-coff, the line number count for a shared library
137 section must be ignored. */
138 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
139 return_section->lineno_count = 0;
140
141 if (hdr->s_nreloc != 0)
142 return_section->flags |= SEC_RELOC;
c8e7bf0d 143 /* FIXME: should this check 'hdr->s_size > 0'. */
252b5132
RH
144 if (hdr->s_scnptr != 0)
145 return_section->flags |= SEC_HAS_CONTENTS;
7c8ca0e4 146
a29a8af8
KT
147 /* Compress/decompress DWARF debug sections with names: .debug_* and
148 .zdebug_*, after the section flags is set. */
149 if ((flags & SEC_DEBUGGING)
150 && ((name[1] == 'd' && name[6] == '_')
151 || (name[1] == 'z' && name[7] == '_')))
152 {
153 enum { nothing, compress, decompress } action = nothing;
154 char *new_name = NULL;
155
156 if (bfd_is_section_compressed (abfd, return_section))
157 {
158 /* Compressed section. Check if we should decompress. */
159 if ((abfd->flags & BFD_DECOMPRESS))
160 action = decompress;
161 }
162 else if (!bfd_is_section_compressed (abfd, return_section))
163 {
164 /* Normal section. Check if we should compress. */
165 if ((abfd->flags & BFD_COMPRESS) && return_section->size != 0)
166 action = compress;
167 }
168
169 switch (action)
170 {
171 case nothing:
172 break;
173 case compress:
174 if (!bfd_init_section_compress_status (abfd, return_section))
175 {
176 (*_bfd_error_handler)
177 (_("%B: unable to initialize compress status for section %s"),
178 abfd, name);
179 return FALSE;
180 }
181 if (name[1] != 'z')
182 {
183 unsigned int len = strlen (name);
184
185 new_name = bfd_alloc (abfd, len + 2);
186 if (new_name == NULL)
187 return FALSE;
188 new_name[0] = '.';
189 new_name[1] = 'z';
190 memcpy (new_name + 2, name + 1, len);
191 }
192 break;
193 case decompress:
194 if (!bfd_init_section_decompress_status (abfd, return_section))
195 {
196 (*_bfd_error_handler)
197 (_("%B: unable to initialize decompress status for section %s"),
198 abfd, name);
199 return FALSE;
200 }
201 if (name[1] == 'z')
202 {
203 unsigned int len = strlen (name);
204
205 new_name = bfd_alloc (abfd, len);
206 if (new_name == NULL)
207 return FALSE;
208 new_name[0] = '.';
209 memcpy (new_name + 1, name + 2, len - 1);
210 }
211 break;
212 }
213 if (new_name != NULL)
214 bfd_rename_section (abfd, return_section, new_name);
215 }
216
7c8ca0e4 217 return result;
252b5132
RH
218}
219
220/* Read in a COFF object and make it into a BFD. This is used by
221 ECOFF as well. */
ce63b7b3
KT
222const bfd_target *
223coff_real_object_p (bfd *,
224 unsigned,
225 struct internal_filehdr *,
226 struct internal_aouthdr *);
227const bfd_target *
c8e7bf0d
NC
228coff_real_object_p (bfd *abfd,
229 unsigned nscns,
230 struct internal_filehdr *internal_f,
231 struct internal_aouthdr *internal_a)
252b5132
RH
232{
233 flagword oflags = abfd->flags;
234 bfd_vma ostart = bfd_get_start_address (abfd);
c8e7bf0d
NC
235 void * tdata;
236 void * tdata_save;
237 bfd_size_type readsize; /* Length of file_info. */
252b5132
RH
238 unsigned int scnhsz;
239 char *external_sections;
240
241 if (!(internal_f->f_flags & F_RELFLG))
242 abfd->flags |= HAS_RELOC;
243 if ((internal_f->f_flags & F_EXEC))
244 abfd->flags |= EXEC_P;
245 if (!(internal_f->f_flags & F_LNNO))
246 abfd->flags |= HAS_LINENO;
247 if (!(internal_f->f_flags & F_LSYMS))
248 abfd->flags |= HAS_LOCALS;
249
250 /* FIXME: How can we set D_PAGED correctly? */
251 if ((internal_f->f_flags & F_EXEC) != 0)
252 abfd->flags |= D_PAGED;
253
254 bfd_get_symcount (abfd) = internal_f->f_nsyms;
255 if (internal_f->f_nsyms)
256 abfd->flags |= HAS_SYMS;
257
258 if (internal_a != (struct internal_aouthdr *) NULL)
259 bfd_get_start_address (abfd) = internal_a->entry;
260 else
261 bfd_get_start_address (abfd) = 0;
262
263 /* Set up the tdata area. ECOFF uses its own routine, and overrides
264 abfd->flags. */
487e54f2 265 tdata_save = abfd->tdata.any;
c8e7bf0d 266 tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
252b5132 267 if (tdata == NULL)
487e54f2 268 goto fail2;
252b5132
RH
269
270 scnhsz = bfd_coff_scnhsz (abfd);
dc810e39 271 readsize = (bfd_size_type) nscns * scnhsz;
a50b1753 272 external_sections = (char *) bfd_alloc (abfd, readsize);
252b5132
RH
273 if (!external_sections)
274 goto fail;
275
c8e7bf0d 276 if (bfd_bread ((void *) external_sections, readsize, abfd) != readsize)
252b5132
RH
277 goto fail;
278
363d7b14 279 /* Set the arch/mach *before* swapping in sections; section header swapping
244148ad 280 may depend on arch/mach info. */
c8e7bf0d 281 if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
363d7b14
TW
282 goto fail;
283
e16bb312 284 /* Now copy data as required; construct all asections etc. */
252b5132
RH
285 if (nscns != 0)
286 {
287 unsigned int i;
288 for (i = 0; i < nscns; i++)
289 {
290 struct internal_scnhdr tmp;
291 bfd_coff_swap_scnhdr_in (abfd,
c8e7bf0d
NC
292 (void *) (external_sections + i * scnhsz),
293 (void *) & tmp);
252b5132
RH
294 if (! make_a_section_from_file (abfd, &tmp, i + 1))
295 goto fail;
296 }
297 }
298
252b5132
RH
299 return abfd->xvec;
300
301 fail:
302 bfd_release (abfd, tdata);
487e54f2
AM
303 fail2:
304 abfd->tdata.any = tdata_save;
252b5132
RH
305 abfd->flags = oflags;
306 bfd_get_start_address (abfd) = ostart;
307 return (const bfd_target *) NULL;
308}
309
310/* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
311 not a COFF file. This is also used by ECOFF. */
312
313const bfd_target *
c8e7bf0d 314coff_object_p (bfd *abfd)
252b5132 315{
dc810e39
AM
316 bfd_size_type filhsz;
317 bfd_size_type aoutsz;
318 unsigned int nscns;
c8e7bf0d 319 void * filehdr;
252b5132
RH
320 struct internal_filehdr internal_f;
321 struct internal_aouthdr internal_a;
322
c8e7bf0d 323 /* Figure out how much to read. */
252b5132
RH
324 filhsz = bfd_coff_filhsz (abfd);
325 aoutsz = bfd_coff_aoutsz (abfd);
326
327 filehdr = bfd_alloc (abfd, filhsz);
328 if (filehdr == NULL)
487e54f2 329 return NULL;
dc810e39 330 if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
252b5132
RH
331 {
332 if (bfd_get_error () != bfd_error_system_call)
333 bfd_set_error (bfd_error_wrong_format);
487e54f2
AM
334 bfd_release (abfd, filehdr);
335 return NULL;
252b5132
RH
336 }
337 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
338 bfd_release (abfd, filehdr);
339
b8819ab2
NC
340 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
341 (less than aoutsz) used in object files and AOUTSZ (equal to
342 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
343 expects this header to be aoutsz bytes in length, so we use that
344 value in the call to bfd_alloc below. But we must be careful to
345 only read in f_opthdr bytes in the call to bfd_bread. We should
346 also attempt to catch corrupt or non-COFF binaries with a strange
347 value for f_opthdr. */
82e51918 348 if (! bfd_coff_bad_format_hook (abfd, &internal_f)
95f7d9f7 349 || internal_f.f_opthdr > aoutsz)
252b5132
RH
350 {
351 bfd_set_error (bfd_error_wrong_format);
487e54f2 352 return NULL;
252b5132
RH
353 }
354 nscns = internal_f.f_nscns;
355
356 if (internal_f.f_opthdr)
357 {
c8e7bf0d 358 void * opthdr;
252b5132
RH
359
360 opthdr = bfd_alloc (abfd, aoutsz);
361 if (opthdr == NULL)
487e54f2 362 return NULL;
dc810e39 363 if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
252b5132
RH
364 != internal_f.f_opthdr)
365 {
487e54f2
AM
366 bfd_release (abfd, opthdr);
367 return NULL;
252b5132 368 }
c8e7bf0d 369 bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
487e54f2 370 bfd_release (abfd, opthdr);
252b5132
RH
371 }
372
373 return coff_real_object_p (abfd, nscns, &internal_f,
374 (internal_f.f_opthdr != 0
375 ? &internal_a
376 : (struct internal_aouthdr *) NULL));
377}
378
379/* Get the BFD section from a COFF symbol section number. */
380
381asection *
91d6fa6a 382coff_section_from_bfd_index (bfd *abfd, int section_index)
252b5132 383{
198beae2 384 struct bfd_section *answer = abfd->sections;
252b5132 385
91d6fa6a 386 if (section_index == N_ABS)
252b5132 387 return bfd_abs_section_ptr;
91d6fa6a 388 if (section_index == N_UNDEF)
252b5132 389 return bfd_und_section_ptr;
91d6fa6a 390 if (section_index == N_DEBUG)
252b5132
RH
391 return bfd_abs_section_ptr;
392
393 while (answer)
394 {
91d6fa6a 395 if (answer->target_index == section_index)
252b5132
RH
396 return answer;
397 answer = answer->next;
398 }
399
400 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
401 has a bad symbol table in biglitpow.o. */
402 return bfd_und_section_ptr;
403}
404
405/* Get the upper bound of a COFF symbol table. */
406
407long
c8e7bf0d 408coff_get_symtab_upper_bound (bfd *abfd)
252b5132
RH
409{
410 if (!bfd_coff_slurp_symbol_table (abfd))
411 return -1;
412
413 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
414}
415
252b5132
RH
416/* Canonicalize a COFF symbol table. */
417
418long
c8e7bf0d 419coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
252b5132
RH
420{
421 unsigned int counter;
422 coff_symbol_type *symbase;
423 coff_symbol_type **location = (coff_symbol_type **) alocation;
424
425 if (!bfd_coff_slurp_symbol_table (abfd))
426 return -1;
427
428 symbase = obj_symbols (abfd);
429 counter = bfd_get_symcount (abfd);
430 while (counter-- > 0)
431 *location++ = symbase++;
432
433 *location = NULL;
434
435 return bfd_get_symcount (abfd);
436}
437
438/* Get the name of a symbol. The caller must pass in a buffer of size
439 >= SYMNMLEN + 1. */
440
441const char *
c8e7bf0d
NC
442_bfd_coff_internal_syment_name (bfd *abfd,
443 const struct internal_syment *sym,
444 char *buf)
252b5132
RH
445{
446 /* FIXME: It's not clear this will work correctly if sizeof
447 (_n_zeroes) != 4. */
448 if (sym->_n._n_n._n_zeroes != 0
449 || sym->_n._n_n._n_offset == 0)
450 {
451 memcpy (buf, sym->_n._n_name, SYMNMLEN);
452 buf[SYMNMLEN] = '\0';
453 return buf;
454 }
455 else
456 {
457 const char *strings;
458
459 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
460 strings = obj_coff_strings (abfd);
461 if (strings == NULL)
462 {
463 strings = _bfd_coff_read_string_table (abfd);
464 if (strings == NULL)
465 return NULL;
466 }
467 return strings + sym->_n._n_n._n_offset;
468 }
469}
470
471/* Read in and swap the relocs. This returns a buffer holding the
b34976b6 472 relocs for section SEC in file ABFD. If CACHE is TRUE and
252b5132
RH
473 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
474 the function is called again. If EXTERNAL_RELOCS is not NULL, it
475 is a buffer large enough to hold the unswapped relocs. If
476 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
b34976b6 477 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
252b5132
RH
478 value must be INTERNAL_RELOCS. The function returns NULL on error. */
479
480struct internal_reloc *
c8e7bf0d
NC
481_bfd_coff_read_internal_relocs (bfd *abfd,
482 asection *sec,
483 bfd_boolean cache,
484 bfd_byte *external_relocs,
485 bfd_boolean require_internal,
486 struct internal_reloc *internal_relocs)
252b5132
RH
487{
488 bfd_size_type relsz;
489 bfd_byte *free_external = NULL;
490 struct internal_reloc *free_internal = NULL;
491 bfd_byte *erel;
492 bfd_byte *erel_end;
493 struct internal_reloc *irel;
dc810e39 494 bfd_size_type amt;
252b5132 495
9d7038d3
MS
496 if (sec->reloc_count == 0)
497 return internal_relocs; /* Nothing to do. */
498
252b5132
RH
499 if (coff_section_data (abfd, sec) != NULL
500 && coff_section_data (abfd, sec)->relocs != NULL)
501 {
502 if (! require_internal)
503 return coff_section_data (abfd, sec)->relocs;
504 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
505 sec->reloc_count * sizeof (struct internal_reloc));
506 return internal_relocs;
507 }
508
509 relsz = bfd_coff_relsz (abfd);
510
dc810e39 511 amt = sec->reloc_count * relsz;
252b5132
RH
512 if (external_relocs == NULL)
513 {
a50b1753 514 free_external = (bfd_byte *) bfd_malloc (amt);
9d7038d3 515 if (free_external == NULL)
252b5132
RH
516 goto error_return;
517 external_relocs = free_external;
518 }
519
520 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
dc810e39 521 || bfd_bread (external_relocs, amt, abfd) != amt)
252b5132
RH
522 goto error_return;
523
524 if (internal_relocs == NULL)
525 {
dc810e39
AM
526 amt = sec->reloc_count;
527 amt *= sizeof (struct internal_reloc);
a50b1753 528 free_internal = (struct internal_reloc *) bfd_malloc (amt);
9d7038d3 529 if (free_internal == NULL)
252b5132
RH
530 goto error_return;
531 internal_relocs = free_internal;
532 }
533
534 /* Swap in the relocs. */
535 erel = external_relocs;
536 erel_end = erel + relsz * sec->reloc_count;
537 irel = internal_relocs;
538 for (; erel < erel_end; erel += relsz, irel++)
c8e7bf0d 539 bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
252b5132
RH
540
541 if (free_external != NULL)
542 {
543 free (free_external);
544 free_external = NULL;
545 }
546
9ee2139f 547 if (cache && free_internal != NULL)
252b5132 548 {
9ee2139f 549 if (coff_section_data (abfd, sec) == NULL)
252b5132 550 {
9ee2139f
MS
551 amt = sizeof (struct coff_section_tdata);
552 sec->used_by_bfd = bfd_zalloc (abfd, amt);
553 if (sec->used_by_bfd == NULL)
554 goto error_return;
555 coff_section_data (abfd, sec)->contents = NULL;
252b5132 556 }
9ee2139f 557 coff_section_data (abfd, sec)->relocs = free_internal;
252b5132
RH
558 }
559
560 return internal_relocs;
561
562 error_return:
563 if (free_external != NULL)
564 free (free_external);
565 if (free_internal != NULL)
566 free (free_internal);
567 return NULL;
568}
569
570/* Set lineno_count for the output sections of a COFF file. */
571
572int
c8e7bf0d 573coff_count_linenumbers (bfd *abfd)
252b5132
RH
574{
575 unsigned int limit = bfd_get_symcount (abfd);
576 unsigned int i;
577 int total = 0;
578 asymbol **p;
579 asection *s;
580
581 if (limit == 0)
582 {
583 /* This may be from the backend linker, in which case the
584 lineno_count in the sections is correct. */
585 for (s = abfd->sections; s != NULL; s = s->next)
586 total += s->lineno_count;
587 return total;
588 }
589
590 for (s = abfd->sections; s != NULL; s = s->next)
591 BFD_ASSERT (s->lineno_count == 0);
592
593 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
594 {
595 asymbol *q_maybe = *p;
596
9bd09e22 597 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
252b5132
RH
598 {
599 coff_symbol_type *q = coffsymbol (q_maybe);
600
601 /* The AIX 4.1 compiler can sometimes generate line numbers
602 attached to debugging symbols. We try to simply ignore
603 those here. */
604 if (q->lineno != NULL
605 && q->symbol.section->owner != NULL)
606 {
607 /* This symbol has line numbers. Increment the owning
608 section's linenumber count. */
609 alent *l = q->lineno;
610
84c254c6 611 do
252b5132 612 {
84c254c6 613 asection * sec = q->symbol.section->output_section;
b34976b6 614
84c254c6
NC
615 /* Do not try to update fields in read-only sections. */
616 if (! bfd_is_const_section (sec))
617 sec->lineno_count ++;
618
252b5132 619 ++total;
252b5132
RH
620 ++l;
621 }
84c254c6 622 while (l->line_number != 0);
252b5132
RH
623 }
624 }
625 }
626
627 return total;
628}
629
630/* Takes a bfd and a symbol, returns a pointer to the coff specific
631 area of the symbol if there is one. */
632
252b5132 633coff_symbol_type *
c8e7bf0d
NC
634coff_symbol_from (bfd *ignore_abfd ATTRIBUTE_UNUSED,
635 asymbol *symbol)
252b5132 636{
9bd09e22 637 if (!bfd_family_coff (bfd_asymbol_bfd (symbol)))
252b5132
RH
638 return (coff_symbol_type *) NULL;
639
640 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
641 return (coff_symbol_type *) NULL;
642
643 return (coff_symbol_type *) symbol;
644}
645
646static void
c8e7bf0d
NC
647fixup_symbol_value (bfd *abfd,
648 coff_symbol_type *coff_symbol_ptr,
649 struct internal_syment *syment)
252b5132 650{
c8e7bf0d 651 /* Normalize the symbol flags. */
68ffbac6 652 if (coff_symbol_ptr->symbol.section
ac38308c 653 && bfd_is_com_section (coff_symbol_ptr->symbol.section))
252b5132 654 {
c8e7bf0d 655 /* A common symbol is undefined with a value. */
252b5132
RH
656 syment->n_scnum = N_UNDEF;
657 syment->n_value = coff_symbol_ptr->symbol.value;
658 }
703153b5
ILT
659 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
660 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
252b5132
RH
661 {
662 syment->n_value = coff_symbol_ptr->symbol.value;
663 }
664 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
665 {
666 syment->n_scnum = N_UNDEF;
667 syment->n_value = 0;
668 }
7bb9db4d 669 /* FIXME: Do we need to handle the absolute section here? */
252b5132
RH
670 else
671 {
672 if (coff_symbol_ptr->symbol.section)
673 {
674 syment->n_scnum =
675 coff_symbol_ptr->symbol.section->output_section->target_index;
676
677 syment->n_value = (coff_symbol_ptr->symbol.value
678 + coff_symbol_ptr->symbol.section->output_offset);
679 if (! obj_pe (abfd))
34cbe64e
TW
680 {
681 syment->n_value += (syment->n_sclass == C_STATLAB)
682 ? coff_symbol_ptr->symbol.section->output_section->lma
683 : coff_symbol_ptr->symbol.section->output_section->vma;
684 }
252b5132
RH
685 }
686 else
687 {
688 BFD_ASSERT (0);
689 /* This can happen, but I don't know why yet (steve@cygnus.com) */
690 syment->n_scnum = N_ABS;
691 syment->n_value = coff_symbol_ptr->symbol.value;
692 }
693 }
694}
695
696/* Run through all the symbols in the symbol table and work out what
697 their indexes into the symbol table will be when output.
698
699 Coff requires that each C_FILE symbol points to the next one in the
700 chain, and that the last one points to the first external symbol. We
701 do that here too. */
702
b34976b6 703bfd_boolean
c8e7bf0d 704coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
252b5132
RH
705{
706 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
707 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
708 unsigned int native_index = 0;
c8e7bf0d 709 struct internal_syment *last_file = NULL;
252b5132
RH
710 unsigned int symbol_index;
711
712 /* COFF demands that undefined symbols come after all other symbols.
713 Since we don't need to impose this extra knowledge on all our
714 client programs, deal with that here. Sort the symbol table;
715 just move the undefined symbols to the end, leaving the rest
716 alone. The O'Reilly book says that defined global symbols come
717 at the end before the undefined symbols, so we do that here as
718 well. */
719 /* @@ Do we have some condition we could test for, so we don't always
720 have to do this? I don't think relocatability is quite right, but
721 I'm not certain. [raeburn:19920508.1711EST] */
722 {
723 asymbol **newsyms;
724 unsigned int i;
dc810e39 725 bfd_size_type amt;
252b5132 726
dc810e39 727 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
a50b1753 728 newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
252b5132 729 if (!newsyms)
b34976b6 730 return FALSE;
252b5132
RH
731 bfd_ptr->outsymbols = newsyms;
732 for (i = 0; i < symbol_count; i++)
733 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
734 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
735 && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
736 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
737 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
738 == 0))))
739 *newsyms++ = symbol_ptr_ptr[i];
740
741 for (i = 0; i < symbol_count; i++)
742 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
743 && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
744 && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
745 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
746 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
747 != 0))))
748 *newsyms++ = symbol_ptr_ptr[i];
749
750 *first_undef = newsyms - bfd_ptr->outsymbols;
751
752 for (i = 0; i < symbol_count; i++)
753 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
754 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
755 *newsyms++ = symbol_ptr_ptr[i];
756 *newsyms = (asymbol *) NULL;
757 symbol_ptr_ptr = bfd_ptr->outsymbols;
758 }
759
760 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
761 {
762 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
244148ad 763 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
252b5132
RH
764 if (coff_symbol_ptr && coff_symbol_ptr->native)
765 {
766 combined_entry_type *s = coff_symbol_ptr->native;
767 int i;
768
769 if (s->u.syment.n_sclass == C_FILE)
770 {
c8e7bf0d 771 if (last_file != NULL)
252b5132
RH
772 last_file->n_value = native_index;
773 last_file = &(s->u.syment);
774 }
775 else
c8e7bf0d
NC
776 /* Modify the symbol values according to their section and
777 type. */
778 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
252b5132 779
252b5132
RH
780 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
781 s[i].offset = native_index++;
782 }
783 else
c8e7bf0d 784 native_index++;
252b5132 785 }
c8e7bf0d 786
252b5132
RH
787 obj_conv_table_size (bfd_ptr) = native_index;
788
b34976b6 789 return TRUE;
252b5132
RH
790}
791
792/* Run thorough the symbol table again, and fix it so that all
793 pointers to entries are changed to the entries' index in the output
794 symbol table. */
795
796void
c8e7bf0d 797coff_mangle_symbols (bfd *bfd_ptr)
252b5132
RH
798{
799 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
800 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
801 unsigned int symbol_index;
802
803 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
804 {
805 coff_symbol_type *coff_symbol_ptr =
806 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
807
808 if (coff_symbol_ptr && coff_symbol_ptr->native)
809 {
810 int i;
811 combined_entry_type *s = coff_symbol_ptr->native;
812
813 if (s->fix_value)
814 {
815 /* FIXME: We should use a union here. */
dc810e39 816 s->u.syment.n_value =
d2df793a
NC
817 (bfd_hostptr_t) ((combined_entry_type *)
818 ((bfd_hostptr_t) s->u.syment.n_value))->offset;
252b5132
RH
819 s->fix_value = 0;
820 }
821 if (s->fix_line)
822 {
823 /* The value is the offset into the line number entries
824 for the symbol's section. On output, the symbol's
825 section should be N_DEBUG. */
826 s->u.syment.n_value =
827 (coff_symbol_ptr->symbol.section->output_section->line_filepos
828 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
829 coff_symbol_ptr->symbol.section =
830 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
831 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
832 }
833 for (i = 0; i < s->u.syment.n_numaux; i++)
834 {
835 combined_entry_type *a = s + i + 1;
836 if (a->fix_tag)
837 {
838 a->u.auxent.x_sym.x_tagndx.l =
839 a->u.auxent.x_sym.x_tagndx.p->offset;
840 a->fix_tag = 0;
841 }
842 if (a->fix_end)
843 {
844 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
845 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
846 a->fix_end = 0;
847 }
848 if (a->fix_scnlen)
849 {
850 a->u.auxent.x_csect.x_scnlen.l =
851 a->u.auxent.x_csect.x_scnlen.p->offset;
852 a->fix_scnlen = 0;
853 }
854 }
855 }
856 }
857}
858
859static void
c8e7bf0d
NC
860coff_fix_symbol_name (bfd *abfd,
861 asymbol *symbol,
862 combined_entry_type *native,
863 bfd_size_type *string_size_p,
864 asection **debug_string_section_p,
865 bfd_size_type *debug_string_size_p)
252b5132
RH
866{
867 unsigned int name_length;
868 union internal_auxent *auxent;
869 char *name = (char *) (symbol->name);
870
c8e7bf0d 871 if (name == NULL)
252b5132 872 {
c8e7bf0d 873 /* COFF symbols always have names, so we'll make one up. */
252b5132
RH
874 symbol->name = "strange";
875 name = (char *) symbol->name;
876 }
877 name_length = strlen (name);
878
879 if (native->u.syment.n_sclass == C_FILE
880 && native->u.syment.n_numaux > 0)
881 {
692b7d62
ILT
882 unsigned int filnmlen;
883
7f6d05e8
CP
884 if (bfd_coff_force_symnames_in_strings (abfd))
885 {
244148ad 886 native->u.syment._n._n_n._n_offset =
7f6d05e8
CP
887 (*string_size_p + STRING_SIZE_SIZE);
888 native->u.syment._n._n_n._n_zeroes = 0;
889 *string_size_p += 6; /* strlen(".file") + 1 */
890 }
891 else
892 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
893
252b5132
RH
894 auxent = &(native + 1)->u.auxent;
895
692b7d62
ILT
896 filnmlen = bfd_coff_filnmlen (abfd);
897
252b5132
RH
898 if (bfd_coff_long_filenames (abfd))
899 {
692b7d62 900 if (name_length <= filnmlen)
c8e7bf0d 901 strncpy (auxent->x_file.x_fname, name, filnmlen);
252b5132
RH
902 else
903 {
904 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
905 auxent->x_file.x_n.x_zeroes = 0;
906 *string_size_p += name_length + 1;
907 }
908 }
909 else
910 {
692b7d62
ILT
911 strncpy (auxent->x_file.x_fname, name, filnmlen);
912 if (name_length > filnmlen)
913 name[filnmlen] = '\0';
252b5132
RH
914 }
915 }
916 else
917 {
7f6d05e8 918 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
c8e7bf0d
NC
919 /* This name will fit into the symbol neatly. */
920 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
921
252b5132
RH
922 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
923 {
924 native->u.syment._n._n_n._n_offset = (*string_size_p
925 + STRING_SIZE_SIZE);
926 native->u.syment._n._n_n._n_zeroes = 0;
927 *string_size_p += name_length + 1;
928 }
929 else
930 {
dc810e39 931 file_ptr filepos;
7f6d05e8
CP
932 bfd_byte buf[4];
933 int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
252b5132
RH
934
935 /* This name should be written into the .debug section. For
936 some reason each name is preceded by a two byte length
937 and also followed by a null byte. FIXME: We assume that
938 the .debug section has already been created, and that it
939 is large enough. */
940 if (*debug_string_section_p == (asection *) NULL)
941 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
942 filepos = bfd_tell (abfd);
7f6d05e8 943 if (prefix_len == 4)
dc810e39 944 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
7f6d05e8 945 else
dc810e39 946 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
7f6d05e8 947
252b5132
RH
948 if (!bfd_set_section_contents (abfd,
949 *debug_string_section_p,
c8e7bf0d 950 (void *) buf,
252b5132 951 (file_ptr) *debug_string_size_p,
7f6d05e8 952 (bfd_size_type) prefix_len)
252b5132
RH
953 || !bfd_set_section_contents (abfd,
954 *debug_string_section_p,
c8e7bf0d 955 (void *) symbol->name,
dc810e39
AM
956 (file_ptr) (*debug_string_size_p
957 + prefix_len),
252b5132
RH
958 (bfd_size_type) name_length + 1))
959 abort ();
960 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
961 abort ();
244148ad 962 native->u.syment._n._n_n._n_offset =
7f6d05e8 963 *debug_string_size_p + prefix_len;
252b5132 964 native->u.syment._n._n_n._n_zeroes = 0;
7f6d05e8 965 *debug_string_size_p += name_length + 1 + prefix_len;
252b5132
RH
966 }
967 }
968}
969
970/* We need to keep track of the symbol index so that when we write out
971 the relocs we can get the index for a symbol. This method is a
972 hack. FIXME. */
973
974#define set_index(symbol, idx) ((symbol)->udata.i = (idx))
975
976/* Write a symbol out to a COFF file. */
977
b34976b6 978static bfd_boolean
c8e7bf0d
NC
979coff_write_symbol (bfd *abfd,
980 asymbol *symbol,
981 combined_entry_type *native,
982 bfd_vma *written,
983 bfd_size_type *string_size_p,
984 asection **debug_string_section_p,
985 bfd_size_type *debug_string_size_p)
252b5132
RH
986{
987 unsigned int numaux = native->u.syment.n_numaux;
988 int type = native->u.syment.n_type;
a50b1753 989 int n_sclass = (int) native->u.syment.n_sclass;
88e59394
DK
990 asection *output_section = symbol->section->output_section
991 ? symbol->section->output_section
992 : symbol->section;
c8e7bf0d 993 void * buf;
252b5132
RH
994 bfd_size_type symesz;
995
996 if (native->u.syment.n_sclass == C_FILE)
997 symbol->flags |= BSF_DEBUGGING;
998
999 if (symbol->flags & BSF_DEBUGGING
1000 && bfd_is_abs_section (symbol->section))
c8e7bf0d
NC
1001 native->u.syment.n_scnum = N_DEBUG;
1002
252b5132 1003 else if (bfd_is_abs_section (symbol->section))
c8e7bf0d
NC
1004 native->u.syment.n_scnum = N_ABS;
1005
252b5132 1006 else if (bfd_is_und_section (symbol->section))
c8e7bf0d
NC
1007 native->u.syment.n_scnum = N_UNDEF;
1008
252b5132 1009 else
c8e7bf0d 1010 native->u.syment.n_scnum =
88e59394 1011 output_section->target_index;
252b5132
RH
1012
1013 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
1014 debug_string_section_p, debug_string_size_p);
1015
1016 symesz = bfd_coff_symesz (abfd);
1017 buf = bfd_alloc (abfd, symesz);
1018 if (!buf)
b34976b6 1019 return FALSE;
252b5132 1020 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
dc810e39 1021 if (bfd_bwrite (buf, symesz, abfd) != symesz)
b34976b6 1022 return FALSE;
252b5132
RH
1023 bfd_release (abfd, buf);
1024
1025 if (native->u.syment.n_numaux > 0)
1026 {
1027 bfd_size_type auxesz;
1028 unsigned int j;
1029
1030 auxesz = bfd_coff_auxesz (abfd);
1031 buf = bfd_alloc (abfd, auxesz);
1032 if (!buf)
b34976b6 1033 return FALSE;
252b5132
RH
1034 for (j = 0; j < native->u.syment.n_numaux; j++)
1035 {
1036 bfd_coff_swap_aux_out (abfd,
1037 &((native + j + 1)->u.auxent),
96d56e9f 1038 type, n_sclass, (int) j,
252b5132
RH
1039 native->u.syment.n_numaux,
1040 buf);
dc810e39 1041 if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
b34976b6 1042 return FALSE;
252b5132
RH
1043 }
1044 bfd_release (abfd, buf);
1045 }
1046
1047 /* Store the index for use when we write out the relocs. */
1048 set_index (symbol, *written);
1049
1050 *written += numaux + 1;
b34976b6 1051 return TRUE;
252b5132
RH
1052}
1053
1054/* Write out a symbol to a COFF file that does not come from a COFF
1055 file originally. This symbol may have been created by the linker,
1056 or we may be linking a non COFF file to a COFF file. */
1057
e7ebb214 1058bfd_boolean
c8e7bf0d
NC
1059coff_write_alien_symbol (bfd *abfd,
1060 asymbol *symbol,
e7ebb214 1061 struct internal_syment *isym,
c8e7bf0d
NC
1062 bfd_vma *written,
1063 bfd_size_type *string_size_p,
1064 asection **debug_string_section_p,
1065 bfd_size_type *debug_string_size_p)
252b5132
RH
1066{
1067 combined_entry_type *native;
e7ebb214 1068 combined_entry_type dummy[2];
88e59394
DK
1069 asection *output_section = symbol->section->output_section
1070 ? symbol->section->output_section
1071 : symbol->section;
07c7ed6d 1072 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
e7ebb214 1073 bfd_boolean ret;
252b5132 1074
07c7ed6d
KT
1075 if ((!link_info || link_info->strip_discarded)
1076 && !bfd_is_abs_section (symbol->section)
1077 && symbol->section->output_section == bfd_abs_section_ptr)
1078 {
1079 symbol->name = "";
e7ebb214
JB
1080 if (isym != NULL)
1081 memset (isym, 0, sizeof(*isym));
07c7ed6d
KT
1082 return TRUE;
1083 }
e7ebb214 1084 native = dummy;
252b5132
RH
1085 native->u.syment.n_type = T_NULL;
1086 native->u.syment.n_flags = 0;
e7ebb214 1087 native->u.syment.n_numaux = 0;
252b5132
RH
1088 if (bfd_is_und_section (symbol->section))
1089 {
1090 native->u.syment.n_scnum = N_UNDEF;
1091 native->u.syment.n_value = symbol->value;
1092 }
1093 else if (bfd_is_com_section (symbol->section))
1094 {
1095 native->u.syment.n_scnum = N_UNDEF;
1096 native->u.syment.n_value = symbol->value;
1097 }
e7ebb214
JB
1098 else if (symbol->flags & BSF_FILE)
1099 {
1100 native->u.syment.n_scnum = N_DEBUG;
1101 native->u.syment.n_numaux = 1;
1102 }
252b5132
RH
1103 else if (symbol->flags & BSF_DEBUGGING)
1104 {
1105 /* There isn't much point to writing out a debugging symbol
1106 unless we are prepared to convert it into COFF debugging
1107 format. So, we just ignore them. We must clobber the symbol
1108 name to keep it from being put in the string table. */
1109 symbol->name = "";
e7ebb214
JB
1110 if (isym != NULL)
1111 memset (isym, 0, sizeof(*isym));
b34976b6 1112 return TRUE;
252b5132
RH
1113 }
1114 else
1115 {
88e59394 1116 native->u.syment.n_scnum = output_section->target_index;
252b5132
RH
1117 native->u.syment.n_value = (symbol->value
1118 + symbol->section->output_offset);
1119 if (! obj_pe (abfd))
88e59394 1120 native->u.syment.n_value += output_section->vma;
252b5132 1121
08da05b0 1122 /* Copy the any flags from the file header into the symbol.
252b5132
RH
1123 FIXME: Why? */
1124 {
1125 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1126 if (c != (coff_symbol_type *) NULL)
1127 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1128 }
1129 }
1130
1131 native->u.syment.n_type = 0;
e7ebb214
JB
1132 if (symbol->flags & BSF_FILE)
1133 native->u.syment.n_sclass = C_FILE;
1134 else if (symbol->flags & BSF_LOCAL)
252b5132
RH
1135 native->u.syment.n_sclass = C_STAT;
1136 else if (symbol->flags & BSF_WEAK)
1137 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
1138 else
1139 native->u.syment.n_sclass = C_EXT;
252b5132 1140
e7ebb214
JB
1141 ret = coff_write_symbol (abfd, symbol, native, written, string_size_p,
1142 debug_string_section_p, debug_string_size_p);
1143 if (isym != NULL)
1144 *isym = native->u.syment;
1145 return ret;
252b5132
RH
1146}
1147
1148/* Write a native symbol to a COFF file. */
1149
b34976b6 1150static bfd_boolean
c8e7bf0d
NC
1151coff_write_native_symbol (bfd *abfd,
1152 coff_symbol_type *symbol,
1153 bfd_vma *written,
1154 bfd_size_type *string_size_p,
1155 asection **debug_string_section_p,
1156 bfd_size_type *debug_string_size_p)
252b5132
RH
1157{
1158 combined_entry_type *native = symbol->native;
1159 alent *lineno = symbol->lineno;
07c7ed6d
KT
1160 struct bfd_link_info *link_info = coff_data (abfd)->link_info;
1161
1162 if ((!link_info || link_info->strip_discarded)
1163 && !bfd_is_abs_section (symbol->symbol.section)
1164 && symbol->symbol.section->output_section == bfd_abs_section_ptr)
1165 {
1166 symbol->symbol.name = "";
1167 return TRUE;
1168 }
252b5132
RH
1169
1170 /* If this symbol has an associated line number, we must store the
1171 symbol index in the line number field. We also tag the auxent to
1172 point to the right place in the lineno table. */
1173 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
1174 {
1175 unsigned int count = 0;
c8e7bf0d 1176
252b5132
RH
1177 lineno[count].u.offset = *written;
1178 if (native->u.syment.n_numaux)
1179 {
1180 union internal_auxent *a = &((native + 1)->u.auxent);
1181
1182 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1183 symbol->symbol.section->output_section->moving_line_filepos;
1184 }
1185
1186 /* Count and relocate all other linenumbers. */
1187 count++;
1188 while (lineno[count].line_number != 0)
1189 {
252b5132
RH
1190 lineno[count].u.offset +=
1191 (symbol->symbol.section->output_section->vma
1192 + symbol->symbol.section->output_offset);
252b5132
RH
1193 count++;
1194 }
b34976b6 1195 symbol->done_lineno = TRUE;
252b5132 1196
84c254c6
NC
1197 if (! bfd_is_const_section (symbol->symbol.section->output_section))
1198 symbol->symbol.section->output_section->moving_line_filepos +=
1199 count * bfd_coff_linesz (abfd);
252b5132
RH
1200 }
1201
1202 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1203 string_size_p, debug_string_section_p,
1204 debug_string_size_p);
1205}
1206
e144674a
NC
1207static void
1208null_error_handler (const char * fmt ATTRIBUTE_UNUSED, ...)
1209{
1210}
1211
252b5132
RH
1212/* Write out the COFF symbols. */
1213
b34976b6 1214bfd_boolean
c8e7bf0d 1215coff_write_symbols (bfd *abfd)
252b5132
RH
1216{
1217 bfd_size_type string_size;
1218 asection *debug_string_section;
1219 bfd_size_type debug_string_size;
1220 unsigned int i;
1221 unsigned int limit = bfd_get_symcount (abfd);
f075ee0c 1222 bfd_vma written = 0;
252b5132
RH
1223 asymbol **p;
1224
1225 string_size = 0;
1226 debug_string_section = NULL;
1227 debug_string_size = 0;
1228
1229 /* If this target supports long section names, they must be put into
1230 the string table. This is supported by PE. This code must
1231 handle section names just as they are handled in
1232 coff_write_object_contents. */
1233 if (bfd_coff_long_section_names (abfd))
1234 {
1235 asection *o;
1236
1237 for (o = abfd->sections; o != NULL; o = o->next)
1238 {
1239 size_t len;
1240
1241 len = strlen (o->name);
1242 if (len > SCNNMLEN)
1243 string_size += len + 1;
1244 }
1245 }
1246
c8e7bf0d 1247 /* Seek to the right place. */
252b5132 1248 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
b34976b6 1249 return FALSE;
252b5132 1250
c8e7bf0d 1251 /* Output all the symbols we have. */
252b5132
RH
1252 written = 0;
1253 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
1254 {
1255 asymbol *symbol = *p;
1256 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
1257
1258 if (c_symbol == (coff_symbol_type *) NULL
1259 || c_symbol->native == (combined_entry_type *) NULL)
1260 {
e7ebb214
JB
1261 if (!coff_write_alien_symbol (abfd, symbol, NULL, &written,
1262 &string_size, &debug_string_section,
252b5132 1263 &debug_string_size))
b34976b6 1264 return FALSE;
252b5132
RH
1265 }
1266 else
1267 {
e144674a
NC
1268 if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
1269 {
1270 bfd_error_handler_type current_error_handler;
96d56e9f 1271 enum coff_symbol_classification sym_class;
e144674a
NC
1272 unsigned char *n_sclass;
1273
1274 /* Suppress error reporting by bfd_coff_classify_symbol.
1275 Error messages can be generated when we are processing a local
1276 symbol which has no associated section and we do not have to
1277 worry about this, all we need to know is that it is local. */
1278 current_error_handler = bfd_set_error_handler (null_error_handler);
96d56e9f
NC
1279 sym_class = bfd_coff_classify_symbol (abfd,
1280 &c_symbol->native->u.syment);
e144674a 1281 (void) bfd_set_error_handler (current_error_handler);
96d56e9f 1282
e144674a
NC
1283 n_sclass = &c_symbol->native->u.syment.n_sclass;
1284
1285 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1286 we cannot retain the existing sclass from the original symbol.
1287 Weak symbols only have one valid sclass, so just set it always.
1288 If it is not local class and should be, set it C_STAT.
1289 If it is global and not classified as global, or if it is
1290 weak (which is also classified as global), set it C_EXT. */
1291
1292 if (symbol->flags & BSF_WEAK)
1293 *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
96d56e9f 1294 else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
e144674a
NC
1295 *n_sclass = C_STAT;
1296 else if (symbol->flags & BSF_GLOBAL
96d56e9f 1297 && (sym_class != COFF_SYMBOL_GLOBAL
e144674a
NC
1298#ifdef COFF_WITH_PE
1299 || *n_sclass == C_NT_WEAK
1300#endif
1301 || *n_sclass == C_WEAKEXT))
1302 c_symbol->native->u.syment.n_sclass = C_EXT;
1303 }
1304
252b5132
RH
1305 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1306 &string_size, &debug_string_section,
1307 &debug_string_size))
b34976b6 1308 return FALSE;
252b5132
RH
1309 }
1310 }
1311
1312 obj_raw_syment_count (abfd) = written;
1313
c8e7bf0d 1314 /* Now write out strings. */
252b5132
RH
1315 if (string_size != 0)
1316 {
1317 unsigned int size = string_size + STRING_SIZE_SIZE;
1318 bfd_byte buffer[STRING_SIZE_SIZE];
1319
1320#if STRING_SIZE_SIZE == 4
dc810e39 1321 H_PUT_32 (abfd, size, buffer);
252b5132 1322#else
dc810e39 1323 #error Change H_PUT_32
252b5132 1324#endif
c8e7bf0d 1325 if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd)
dc810e39 1326 != sizeof (buffer))
b34976b6 1327 return FALSE;
252b5132
RH
1328
1329 /* Handle long section names. This code must handle section
1330 names just as they are handled in coff_write_object_contents. */
1331 if (bfd_coff_long_section_names (abfd))
1332 {
1333 asection *o;
1334
1335 for (o = abfd->sections; o != NULL; o = o->next)
1336 {
1337 size_t len;
1338
1339 len = strlen (o->name);
1340 if (len > SCNNMLEN)
1341 {
dc810e39
AM
1342 if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
1343 != len + 1)
b34976b6 1344 return FALSE;
252b5132
RH
1345 }
1346 }
1347 }
1348
1349 for (p = abfd->outsymbols, i = 0;
1350 i < limit;
1351 i++, p++)
1352 {
1353 asymbol *q = *p;
1354 size_t name_length = strlen (q->name);
1355 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1356 size_t maxlen;
1357
1358 /* Figure out whether the symbol name should go in the string
1359 table. Symbol names that are short enough are stored
1360 directly in the syment structure. File names permit a
1361 different, longer, length in the syment structure. On
1362 XCOFF, some symbol names are stored in the .debug section
1363 rather than in the string table. */
1364
1365 if (c_symbol == NULL
1366 || c_symbol->native == NULL)
c8e7bf0d
NC
1367 /* This is not a COFF symbol, so it certainly is not a
1368 file name, nor does it go in the .debug section. */
1369 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
1370
252b5132
RH
1371 else if (bfd_coff_symname_in_debug (abfd,
1372 &c_symbol->native->u.syment))
c8e7bf0d
NC
1373 /* This symbol name is in the XCOFF .debug section.
1374 Don't write it into the string table. */
1375 maxlen = name_length;
1376
252b5132
RH
1377 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1378 && c_symbol->native->u.syment.n_numaux > 0)
7f6d05e8 1379 {
244148ad 1380 if (bfd_coff_force_symnames_in_strings (abfd))
dc810e39
AM
1381 {
1382 if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
b34976b6 1383 return FALSE;
dc810e39 1384 }
7f6d05e8
CP
1385 maxlen = bfd_coff_filnmlen (abfd);
1386 }
252b5132 1387 else
7f6d05e8 1388 maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
252b5132
RH
1389
1390 if (name_length > maxlen)
1391 {
c8e7bf0d 1392 if (bfd_bwrite ((void *) (q->name), (bfd_size_type) name_length + 1,
dc810e39 1393 abfd) != name_length + 1)
b34976b6 1394 return FALSE;
252b5132
RH
1395 }
1396 }
1397 }
1398 else
1399 {
1400 /* We would normally not write anything here, but we'll write
1401 out 4 so that any stupid coff reader which tries to read the
1402 string table even when there isn't one won't croak. */
1403 unsigned int size = STRING_SIZE_SIZE;
1404 bfd_byte buffer[STRING_SIZE_SIZE];
1405
1406#if STRING_SIZE_SIZE == 4
dc810e39 1407 H_PUT_32 (abfd, size, buffer);
252b5132 1408#else
dc810e39 1409 #error Change H_PUT_32
252b5132 1410#endif
c8e7bf0d 1411 if (bfd_bwrite ((void *) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
252b5132 1412 != STRING_SIZE_SIZE)
b34976b6 1413 return FALSE;
252b5132
RH
1414 }
1415
1416 /* Make sure the .debug section was created to be the correct size.
1417 We should create it ourselves on the fly, but we don't because
1418 BFD won't let us write to any section until we know how large all
1419 the sections are. We could still do it by making another pass
1420 over the symbols. FIXME. */
1421 BFD_ASSERT (debug_string_size == 0
1422 || (debug_string_section != (asection *) NULL
1423 && (BFD_ALIGN (debug_string_size,
1424 1 << debug_string_section->alignment_power)
eea6121a 1425 == debug_string_section->size)));
252b5132 1426
b34976b6 1427 return TRUE;
252b5132
RH
1428}
1429
b34976b6 1430bfd_boolean
c8e7bf0d 1431coff_write_linenumbers (bfd *abfd)
252b5132
RH
1432{
1433 asection *s;
1434 bfd_size_type linesz;
c8e7bf0d 1435 void * buff;
252b5132
RH
1436
1437 linesz = bfd_coff_linesz (abfd);
1438 buff = bfd_alloc (abfd, linesz);
1439 if (!buff)
b34976b6 1440 return FALSE;
252b5132
RH
1441 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1442 {
1443 if (s->lineno_count)
1444 {
1445 asymbol **q = abfd->outsymbols;
1446 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
b34976b6 1447 return FALSE;
c8e7bf0d 1448 /* Find all the linenumbers in this section. */
252b5132
RH
1449 while (*q)
1450 {
1451 asymbol *p = *q;
1452 if (p->section->output_section == s)
1453 {
1454 alent *l =
1455 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1456 (bfd_asymbol_bfd (p), p));
1457 if (l)
1458 {
c8e7bf0d 1459 /* Found a linenumber entry, output. */
252b5132 1460 struct internal_lineno out;
c8e7bf0d 1461 memset ((void *) & out, 0, sizeof (out));
252b5132
RH
1462 out.l_lnno = 0;
1463 out.l_addr.l_symndx = l->u.offset;
1464 bfd_coff_swap_lineno_out (abfd, &out, buff);
dc810e39
AM
1465 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1466 != linesz)
b34976b6 1467 return FALSE;
252b5132
RH
1468 l++;
1469 while (l->line_number)
1470 {
1471 out.l_lnno = l->line_number;
1472 out.l_addr.l_symndx = l->u.offset;
1473 bfd_coff_swap_lineno_out (abfd, &out, buff);
dc810e39
AM
1474 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
1475 != linesz)
b34976b6 1476 return FALSE;
252b5132
RH
1477 l++;
1478 }
1479 }
1480 }
1481 q++;
1482 }
1483 }
1484 }
1485 bfd_release (abfd, buff);
b34976b6 1486 return TRUE;
252b5132
RH
1487}
1488
252b5132 1489alent *
c8e7bf0d 1490coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
252b5132
RH
1491{
1492 return coffsymbol (symbol)->lineno;
1493}
1494
252b5132
RH
1495/* This function transforms the offsets into the symbol table into
1496 pointers to syments. */
1497
1498static void
c8e7bf0d
NC
1499coff_pointerize_aux (bfd *abfd,
1500 combined_entry_type *table_base,
1501 combined_entry_type *symbol,
1502 unsigned int indaux,
1503 combined_entry_type *auxent)
252b5132
RH
1504{
1505 unsigned int type = symbol->u.syment.n_type;
96d56e9f 1506 unsigned int n_sclass = symbol->u.syment.n_sclass;
252b5132
RH
1507
1508 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1509 {
1510 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1511 (abfd, table_base, symbol, indaux, auxent))
1512 return;
1513 }
1514
c8e7bf0d 1515 /* Don't bother if this is a file or a section. */
96d56e9f 1516 if (n_sclass == C_STAT && type == T_NULL)
252b5132 1517 return;
96d56e9f 1518 if (n_sclass == C_FILE)
252b5132
RH
1519 return;
1520
c8e7bf0d
NC
1521 /* Otherwise patch up. */
1522#define N_TMASK coff_data (abfd)->local_n_tmask
252b5132 1523#define N_BTSHFT coff_data (abfd)->local_n_btshft
68ffbac6 1524
96d56e9f
NC
1525 if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
1526 || n_sclass == C_FCN)
252b5132
RH
1527 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1528 {
1529 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
1530 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1531 auxent->fix_end = 1;
1532 }
1533 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1534 generate one, so we must be careful to ignore it. */
1535 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1536 {
1537 auxent->u.auxent.x_sym.x_tagndx.p =
1538 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1539 auxent->fix_tag = 1;
1540 }
1541}
1542
1543/* Allocate space for the ".debug" section, and read it.
1544 We did not read the debug section until now, because
244148ad 1545 we didn't want to go to the trouble until someone needed it. */
252b5132
RH
1546
1547static char *
c8e7bf0d 1548build_debug_section (bfd *abfd)
252b5132
RH
1549{
1550 char *debug_section;
dc810e39
AM
1551 file_ptr position;
1552 bfd_size_type sec_size;
252b5132
RH
1553
1554 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1555
1556 if (!sect)
1557 {
1558 bfd_set_error (bfd_error_no_debug_section);
1559 return NULL;
1560 }
1561
eea6121a 1562 sec_size = sect->size;
a50b1753 1563 debug_section = (char *) bfd_alloc (abfd, sec_size);
252b5132
RH
1564 if (debug_section == NULL)
1565 return NULL;
1566
244148ad 1567 /* Seek to the beginning of the `.debug' section and read it.
252b5132
RH
1568 Save the current position first; it is needed by our caller.
1569 Then read debug section and reset the file pointer. */
1570
1571 position = bfd_tell (abfd);
1572 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
dc810e39 1573 || bfd_bread (debug_section, sec_size, abfd) != sec_size
252b5132
RH
1574 || bfd_seek (abfd, position, SEEK_SET) != 0)
1575 return NULL;
1576 return debug_section;
1577}
1578
252b5132
RH
1579/* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1580 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1581 be \0-terminated. */
c8e7bf0d 1582
252b5132 1583static char *
c8e7bf0d 1584copy_name (bfd *abfd, char *name, size_t maxlen)
252b5132 1585{
dc810e39 1586 size_t len;
252b5132
RH
1587 char *newname;
1588
1589 for (len = 0; len < maxlen; ++len)
c8e7bf0d
NC
1590 if (name[len] == '\0')
1591 break;
1592
a50b1753 1593 if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
c8e7bf0d 1594 return NULL;
252b5132 1595
252b5132
RH
1596 strncpy (newname, name, len);
1597 newname[len] = '\0';
1598 return newname;
1599}
1600
1601/* Read in the external symbols. */
1602
b34976b6 1603bfd_boolean
c8e7bf0d 1604_bfd_coff_get_external_symbols (bfd *abfd)
252b5132
RH
1605{
1606 bfd_size_type symesz;
dc810e39 1607 bfd_size_type size;
c8e7bf0d 1608 void * syms;
252b5132
RH
1609
1610 if (obj_coff_external_syms (abfd) != NULL)
b34976b6 1611 return TRUE;
252b5132
RH
1612
1613 symesz = bfd_coff_symesz (abfd);
1614
1615 size = obj_raw_syment_count (abfd) * symesz;
353c5574
MS
1616 if (size == 0)
1617 return TRUE;
252b5132 1618
f54498b4
NC
1619 /* PR binutils/17512: Do not even try to load
1620 a symbol table bigger than the entire file... */
1621 if (size >= (bfd_size_type) bfd_get_size (abfd))
1622 return FALSE;
1623
c8e7bf0d 1624 syms = bfd_malloc (size);
353c5574 1625 if (syms == NULL)
b34976b6 1626 return FALSE;
252b5132
RH
1627
1628 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
dc810e39 1629 || bfd_bread (syms, size, abfd) != size)
252b5132
RH
1630 {
1631 if (syms != NULL)
1632 free (syms);
b34976b6 1633 return FALSE;
252b5132
RH
1634 }
1635
1636 obj_coff_external_syms (abfd) = syms;
1637
b34976b6 1638 return TRUE;
252b5132
RH
1639}
1640
1641/* Read in the external strings. The strings are not loaded until
1642 they are needed. This is because we have no simple way of
1643 detecting a missing string table in an archive. */
1644
1645const char *
c8e7bf0d 1646_bfd_coff_read_string_table (bfd *abfd)
252b5132
RH
1647{
1648 char extstrsize[STRING_SIZE_SIZE];
dc810e39 1649 bfd_size_type strsize;
252b5132 1650 char *strings;
dc810e39 1651 file_ptr pos;
252b5132
RH
1652
1653 if (obj_coff_strings (abfd) != NULL)
1654 return obj_coff_strings (abfd);
1655
1656 if (obj_sym_filepos (abfd) == 0)
1657 {
1658 bfd_set_error (bfd_error_no_symbols);
1659 return NULL;
1660 }
1661
dc810e39
AM
1662 pos = obj_sym_filepos (abfd);
1663 pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
1664 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
252b5132 1665 return NULL;
244148ad 1666
dc810e39
AM
1667 if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
1668 != sizeof extstrsize)
252b5132
RH
1669 {
1670 if (bfd_get_error () != bfd_error_file_truncated)
1671 return NULL;
1672
1673 /* There is no string table. */
1674 strsize = STRING_SIZE_SIZE;
1675 }
1676 else
1677 {
1678#if STRING_SIZE_SIZE == 4
dc810e39 1679 strsize = H_GET_32 (abfd, extstrsize);
252b5132 1680#else
dc810e39 1681 #error Change H_GET_32
252b5132
RH
1682#endif
1683 }
1684
1685 if (strsize < STRING_SIZE_SIZE)
1686 {
1687 (*_bfd_error_handler)
d003868e 1688 (_("%B: bad string table size %lu"), abfd, (unsigned long) strsize);
252b5132
RH
1689 bfd_set_error (bfd_error_bad_value);
1690 return NULL;
1691 }
1692
a50b1753 1693 strings = (char *) bfd_malloc (strsize);
252b5132
RH
1694 if (strings == NULL)
1695 return NULL;
1696
dc810e39 1697 if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
252b5132
RH
1698 != strsize - STRING_SIZE_SIZE)
1699 {
1700 free (strings);
1701 return NULL;
1702 }
1703
1704 obj_coff_strings (abfd) = strings;
1705
1706 return strings;
1707}
1708
1709/* Free up the external symbols and strings read from a COFF file. */
1710
b34976b6 1711bfd_boolean
c8e7bf0d 1712_bfd_coff_free_symbols (bfd *abfd)
252b5132
RH
1713{
1714 if (obj_coff_external_syms (abfd) != NULL
1715 && ! obj_coff_keep_syms (abfd))
1716 {
1717 free (obj_coff_external_syms (abfd));
1718 obj_coff_external_syms (abfd) = NULL;
1719 }
1720 if (obj_coff_strings (abfd) != NULL
1721 && ! obj_coff_keep_strings (abfd))
1722 {
1723 free (obj_coff_strings (abfd));
1724 obj_coff_strings (abfd) = NULL;
1725 }
b34976b6 1726 return TRUE;
252b5132
RH
1727}
1728
1729/* Read a symbol table into freshly bfd_allocated memory, swap it, and
1730 knit the symbol names into a normalized form. By normalized here I
1731 mean that all symbols have an n_offset pointer that points to a null-
1732 terminated string. */
1733
1734combined_entry_type *
c8e7bf0d 1735coff_get_normalized_symtab (bfd *abfd)
252b5132
RH
1736{
1737 combined_entry_type *internal;
1738 combined_entry_type *internal_ptr;
1739 combined_entry_type *symbol_ptr;
1740 combined_entry_type *internal_end;
dc810e39 1741 size_t symesz;
252b5132
RH
1742 char *raw_src;
1743 char *raw_end;
1744 const char *string_table = NULL;
1745 char *debug_section = NULL;
dc810e39 1746 bfd_size_type size;
252b5132
RH
1747
1748 if (obj_raw_syments (abfd) != NULL)
1749 return obj_raw_syments (abfd);
1750
1751 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
a50b1753 1752 internal = (combined_entry_type *) bfd_zalloc (abfd, size);
252b5132
RH
1753 if (internal == NULL && size != 0)
1754 return NULL;
1755 internal_end = internal + obj_raw_syment_count (abfd);
7e760b06 1756
252b5132
RH
1757 if (! _bfd_coff_get_external_symbols (abfd))
1758 return NULL;
1759
1760 raw_src = (char *) obj_coff_external_syms (abfd);
1761
c8e7bf0d 1762 /* Mark the end of the symbols. */
252b5132
RH
1763 symesz = bfd_coff_symesz (abfd);
1764 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
1765
1766 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1767 probably possible. If one shows up, it will probably kill us. */
1768
c8e7bf0d 1769 /* Swap all the raw entries. */
252b5132
RH
1770 for (internal_ptr = internal;
1771 raw_src < raw_end;
1772 raw_src += symesz, internal_ptr++)
1773 {
252b5132 1774 unsigned int i;
7e760b06 1775
c8e7bf0d
NC
1776 bfd_coff_swap_sym_in (abfd, (void *) raw_src,
1777 (void *) & internal_ptr->u.syment);
252b5132
RH
1778 symbol_ptr = internal_ptr;
1779
1780 for (i = 0;
1781 i < symbol_ptr->u.syment.n_numaux;
1782 i++)
1783 {
1784 internal_ptr++;
7e760b06
NC
1785 /* PR 17512: Prevent buffer overrun. */
1786 if (internal_ptr >= internal_end)
1787 return NULL;
1788
252b5132 1789 raw_src += symesz;
c8e7bf0d 1790 bfd_coff_swap_aux_in (abfd, (void *) raw_src,
252b5132
RH
1791 symbol_ptr->u.syment.n_type,
1792 symbol_ptr->u.syment.n_sclass,
dc810e39 1793 (int) i, symbol_ptr->u.syment.n_numaux,
252b5132
RH
1794 &(internal_ptr->u.auxent));
1795 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1796 internal_ptr);
1797 }
1798 }
1799
1800 /* Free the raw symbols, but not the strings (if we have them). */
b34976b6 1801 obj_coff_keep_strings (abfd) = TRUE;
252b5132
RH
1802 if (! _bfd_coff_free_symbols (abfd))
1803 return NULL;
1804
1805 for (internal_ptr = internal; internal_ptr < internal_end;
1806 internal_ptr++)
1807 {
1808 if (internal_ptr->u.syment.n_sclass == C_FILE
1809 && internal_ptr->u.syment.n_numaux > 0)
1810 {
c8e7bf0d
NC
1811 /* Make a file symbol point to the name in the auxent, since
1812 the text ".file" is redundant. */
252b5132
RH
1813 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1814 {
c8e7bf0d 1815 /* The filename is a long one, point into the string table. */
252b5132
RH
1816 if (string_table == NULL)
1817 {
1818 string_table = _bfd_coff_read_string_table (abfd);
1819 if (string_table == NULL)
1820 return NULL;
1821 }
1822
1823 internal_ptr->u.syment._n._n_n._n_offset =
d2df793a 1824 ((bfd_hostptr_t)
252b5132
RH
1825 (string_table
1826 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
1827 }
1828 else
1829 {
7bb9db4d
ILT
1830 /* Ordinary short filename, put into memory anyway. The
1831 Microsoft PE tools sometimes store a filename in
1832 multiple AUX entries. */
ec0ef80e
DD
1833 if (internal_ptr->u.syment.n_numaux > 1
1834 && coff_data (abfd)->pe)
c8e7bf0d 1835 internal_ptr->u.syment._n._n_n._n_offset =
d2df793a 1836 ((bfd_hostptr_t)
c8e7bf0d
NC
1837 copy_name (abfd,
1838 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1839 internal_ptr->u.syment.n_numaux * symesz));
ec0ef80e 1840 else
c8e7bf0d 1841 internal_ptr->u.syment._n._n_n._n_offset =
d2df793a 1842 ((bfd_hostptr_t)
c8e7bf0d
NC
1843 copy_name (abfd,
1844 (internal_ptr + 1)->u.auxent.x_file.x_fname,
1845 (size_t) bfd_coff_filnmlen (abfd)));
252b5132
RH
1846 }
1847 }
1848 else
1849 {
1850 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1851 {
1852 /* This is a "short" name. Make it long. */
dc810e39
AM
1853 size_t i;
1854 char *newstring;
252b5132 1855
c8e7bf0d 1856 /* Find the length of this string without walking into memory
252b5132
RH
1857 that isn't ours. */
1858 for (i = 0; i < 8; ++i)
dc810e39
AM
1859 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1860 break;
252b5132 1861
a50b1753 1862 newstring = (char *) bfd_zalloc (abfd, (bfd_size_type) (i + 1));
dc810e39 1863 if (newstring == NULL)
c8e7bf0d 1864 return NULL;
dc810e39 1865 strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
d2df793a 1866 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) newstring;
252b5132
RH
1867 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1868 }
1869 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
649aeae3 1870 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) "";
252b5132
RH
1871 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1872 {
1873 /* Long name already. Point symbol at the string in the
1874 table. */
1875 if (string_table == NULL)
1876 {
1877 string_table = _bfd_coff_read_string_table (abfd);
1878 if (string_table == NULL)
1879 return NULL;
1880 }
1881 internal_ptr->u.syment._n._n_n._n_offset =
d2df793a 1882 ((bfd_hostptr_t)
252b5132
RH
1883 (string_table
1884 + internal_ptr->u.syment._n._n_n._n_offset));
1885 }
1886 else
1887 {
1888 /* Long name in debug section. Very similar. */
1889 if (debug_section == NULL)
1890 debug_section = build_debug_section (abfd);
d2df793a 1891 internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t)
252b5132
RH
1892 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1893 }
1894 }
1895 internal_ptr += internal_ptr->u.syment.n_numaux;
1896 }
1897
1898 obj_raw_syments (abfd) = internal;
1899 BFD_ASSERT (obj_raw_syment_count (abfd)
1900 == (unsigned int) (internal_ptr - internal));
1901
c8e7bf0d
NC
1902 return internal;
1903}
252b5132
RH
1904
1905long
c8e7bf0d 1906coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
252b5132
RH
1907{
1908 if (bfd_get_format (abfd) != bfd_object)
1909 {
1910 bfd_set_error (bfd_error_invalid_operation);
1911 return -1;
1912 }
1913 return (asect->reloc_count + 1) * sizeof (arelent *);
1914}
1915
1916asymbol *
c8e7bf0d 1917coff_make_empty_symbol (bfd *abfd)
252b5132 1918{
dc810e39 1919 bfd_size_type amt = sizeof (coff_symbol_type);
d3ce72d0 1920 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
c8e7bf0d 1921
d3ce72d0 1922 if (new_symbol == NULL)
c8e7bf0d 1923 return NULL;
d3ce72d0
NC
1924 new_symbol->symbol.section = 0;
1925 new_symbol->native = 0;
1926 new_symbol->lineno = NULL;
1927 new_symbol->done_lineno = FALSE;
1928 new_symbol->symbol.the_bfd = abfd;
c8e7bf0d 1929
d3ce72d0 1930 return & new_symbol->symbol;
252b5132
RH
1931}
1932
1933/* Make a debugging symbol. */
1934
1935asymbol *
c8e7bf0d
NC
1936coff_bfd_make_debug_symbol (bfd *abfd,
1937 void * ptr ATTRIBUTE_UNUSED,
1938 unsigned long sz ATTRIBUTE_UNUSED)
252b5132 1939{
dc810e39 1940 bfd_size_type amt = sizeof (coff_symbol_type);
d3ce72d0 1941 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
c8e7bf0d 1942
d3ce72d0 1943 if (new_symbol == NULL)
c8e7bf0d 1944 return NULL;
252b5132
RH
1945 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1946 (but shouldn't be a constant). */
dc810e39 1947 amt = sizeof (combined_entry_type) * 10;
d3ce72d0
NC
1948 new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
1949 if (!new_symbol->native)
c8e7bf0d 1950 return NULL;
d3ce72d0
NC
1951 new_symbol->symbol.section = bfd_abs_section_ptr;
1952 new_symbol->symbol.flags = BSF_DEBUGGING;
1953 new_symbol->lineno = NULL;
1954 new_symbol->done_lineno = FALSE;
1955 new_symbol->symbol.the_bfd = abfd;
68ffbac6 1956
d3ce72d0 1957 return & new_symbol->symbol;
252b5132
RH
1958}
1959
252b5132 1960void
c8e7bf0d 1961coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
252b5132
RH
1962{
1963 bfd_symbol_info (symbol, ret);
c8e7bf0d 1964
252b5132
RH
1965 if (coffsymbol (symbol)->native != NULL
1966 && coffsymbol (symbol)->native->fix_value)
c8e7bf0d 1967 ret->value = coffsymbol (symbol)->native->u.syment.n_value -
d2df793a 1968 (bfd_hostptr_t) obj_raw_syments (abfd);
252b5132
RH
1969}
1970
1971/* Return the COFF syment for a symbol. */
1972
b34976b6 1973bfd_boolean
c8e7bf0d
NC
1974bfd_coff_get_syment (bfd *abfd,
1975 asymbol *symbol,
1976 struct internal_syment *psyment)
252b5132
RH
1977{
1978 coff_symbol_type *csym;
1979
1980 csym = coff_symbol_from (abfd, symbol);
1981 if (csym == NULL || csym->native == NULL)
1982 {
1983 bfd_set_error (bfd_error_invalid_operation);
b34976b6 1984 return FALSE;
252b5132
RH
1985 }
1986
1987 *psyment = csym->native->u.syment;
1988
1989 if (csym->native->fix_value)
dc810e39 1990 psyment->n_value = psyment->n_value -
d2df793a 1991 (bfd_hostptr_t) obj_raw_syments (abfd);
252b5132
RH
1992
1993 /* FIXME: We should handle fix_line here. */
1994
b34976b6 1995 return TRUE;
252b5132
RH
1996}
1997
1998/* Return the COFF auxent for a symbol. */
1999
b34976b6 2000bfd_boolean
c8e7bf0d
NC
2001bfd_coff_get_auxent (bfd *abfd,
2002 asymbol *symbol,
2003 int indx,
2004 union internal_auxent *pauxent)
252b5132
RH
2005{
2006 coff_symbol_type *csym;
2007 combined_entry_type *ent;
2008
2009 csym = coff_symbol_from (abfd, symbol);
2010
2011 if (csym == NULL
2012 || csym->native == NULL
2013 || indx >= csym->native->u.syment.n_numaux)
2014 {
2015 bfd_set_error (bfd_error_invalid_operation);
b34976b6 2016 return FALSE;
252b5132
RH
2017 }
2018
2019 ent = csym->native + indx + 1;
2020
2021 *pauxent = ent->u.auxent;
2022
2023 if (ent->fix_tag)
2024 pauxent->x_sym.x_tagndx.l =
2025 ((combined_entry_type *) pauxent->x_sym.x_tagndx.p
2026 - obj_raw_syments (abfd));
2027
2028 if (ent->fix_end)
2029 pauxent->x_sym.x_fcnary.x_fcn.x_endndx.l =
2030 ((combined_entry_type *) pauxent->x_sym.x_fcnary.x_fcn.x_endndx.p
2031 - obj_raw_syments (abfd));
2032
2033 if (ent->fix_scnlen)
2034 pauxent->x_csect.x_scnlen.l =
2035 ((combined_entry_type *) pauxent->x_csect.x_scnlen.p
2036 - obj_raw_syments (abfd));
2037
b34976b6 2038 return TRUE;
252b5132
RH
2039}
2040
2041/* Print out information about COFF symbol. */
2042
2043void
c8e7bf0d
NC
2044coff_print_symbol (bfd *abfd,
2045 void * filep,
2046 asymbol *symbol,
2047 bfd_print_symbol_type how)
252b5132 2048{
c8e7bf0d 2049 FILE * file = (FILE *) filep;
252b5132
RH
2050
2051 switch (how)
2052 {
2053 case bfd_print_symbol_name:
2054 fprintf (file, "%s", symbol->name);
2055 break;
2056
2057 case bfd_print_symbol_more:
2058 fprintf (file, "coff %s %s",
2059 coffsymbol (symbol)->native ? "n" : "g",
2060 coffsymbol (symbol)->lineno ? "l" : " ");
2061 break;
2062
2063 case bfd_print_symbol_all:
2064 if (coffsymbol (symbol)->native)
2065 {
beb1bf64 2066 bfd_vma val;
252b5132
RH
2067 unsigned int aux;
2068 combined_entry_type *combined = coffsymbol (symbol)->native;
2069 combined_entry_type *root = obj_raw_syments (abfd);
2070 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
2071
2072 fprintf (file, "[%3ld]", (long) (combined - root));
2073
2074 if (! combined->fix_value)
beb1bf64 2075 val = (bfd_vma) combined->u.syment.n_value;
252b5132 2076 else
d2df793a 2077 val = combined->u.syment.n_value - (bfd_hostptr_t) root;
252b5132 2078
7920ce38 2079 fprintf (file, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
252b5132
RH
2080 combined->u.syment.n_scnum,
2081 combined->u.syment.n_flags,
2082 combined->u.syment.n_type,
2083 combined->u.syment.n_sclass,
745c12f8 2084 combined->u.syment.n_numaux);
ebf12fbe 2085 bfd_fprintf_vma (abfd, file, val);
745c12f8 2086 fprintf (file, " %s", symbol->name);
252b5132
RH
2087
2088 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
2089 {
2090 combined_entry_type *auxp = combined + aux + 1;
2091 long tagndx;
2092
2093 if (auxp->fix_tag)
2094 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
2095 else
2096 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
2097
2098 fprintf (file, "\n");
2099
2100 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
2101 continue;
2102
2103 switch (combined->u.syment.n_sclass)
2104 {
2105 case C_FILE:
2106 fprintf (file, "File ");
2107 break;
2108
2109 case C_STAT:
2110 if (combined->u.syment.n_type == T_NULL)
c8e7bf0d 2111 /* Probably a section symbol ? */
252b5132
RH
2112 {
2113 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
0af1713e 2114 (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
252b5132
RH
2115 auxp->u.auxent.x_scn.x_nreloc,
2116 auxp->u.auxent.x_scn.x_nlinno);
2117 if (auxp->u.auxent.x_scn.x_checksum != 0
2118 || auxp->u.auxent.x_scn.x_associated != 0
2119 || auxp->u.auxent.x_scn.x_comdat != 0)
2120 fprintf (file, " checksum 0x%lx assoc %d comdat %d",
2121 auxp->u.auxent.x_scn.x_checksum,
2122 auxp->u.auxent.x_scn.x_associated,
2123 auxp->u.auxent.x_scn.x_comdat);
2124 break;
2125 }
c8e7bf0d 2126 /* Otherwise fall through. */
312191a6 2127 case C_EXT:
8602d4fe 2128 case C_AIX_WEAKEXT:
312191a6
ILT
2129 if (ISFCN (combined->u.syment.n_type))
2130 {
cea4409c
AM
2131 long next, llnos;
2132
2133 if (auxp->fix_end)
2134 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2135 - root);
2136 else
2137 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
2138 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
312191a6 2139 fprintf (file,
3d66c4f7 2140 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
0af1713e
AM
2141 tagndx,
2142 (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
cea4409c 2143 llnos, next);
312191a6
ILT
2144 break;
2145 }
c8e7bf0d 2146 /* Otherwise fall through. */
252b5132
RH
2147 default:
2148 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
2149 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
2150 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
2151 tagndx);
2152 if (auxp->fix_end)
2153 fprintf (file, " endndx %ld",
2154 ((long)
2155 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
2156 - root)));
2157 break;
2158 }
2159 }
2160
2161 if (l)
2162 {
2163 fprintf (file, "\n%s :", l->u.sym->name);
2164 l++;
2165 while (l->line_number)
2166 {
745c12f8 2167 fprintf (file, "\n%4d : ", l->line_number);
ebf12fbe 2168 bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
252b5132
RH
2169 l++;
2170 }
2171 }
2172 }
2173 else
2174 {
c8e7bf0d 2175 bfd_print_symbol_vandf (abfd, (void *) file, symbol);
252b5132
RH
2176 fprintf (file, " %-5s %s %s %s",
2177 symbol->section->name,
2178 coffsymbol (symbol)->native ? "n" : "g",
2179 coffsymbol (symbol)->lineno ? "l" : " ",
2180 symbol->name);
2181 }
2182 }
2183}
2184
2185/* Return whether a symbol name implies a local symbol. In COFF,
2186 local symbols generally start with ``.L''. Most targets use this
2187 function for the is_local_label_name entry point, but some may
2188 override it. */
2189
b34976b6 2190bfd_boolean
c8e7bf0d
NC
2191_bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
2192 const char *name)
252b5132 2193{
b34976b6 2194 return name[0] == '.' && name[1] == 'L';
252b5132
RH
2195}
2196
9a968f43
NC
2197/* Provided a BFD, a section and an offset (in bytes, not octets) into the
2198 section, calculate and return the name of the source file and the line
2199 nearest to the wanted location. */
435b1e90 2200
b34976b6 2201bfd_boolean
fc28f9aa 2202coff_find_nearest_line_with_names (bfd *abfd,
fc28f9aa 2203 asymbol **symbols,
fb167eb2 2204 asection *section,
fc28f9aa
TG
2205 bfd_vma offset,
2206 const char **filename_ptr,
2207 const char **functionname_ptr,
fb167eb2
AM
2208 unsigned int *line_ptr,
2209 const struct dwarf_debug_section *debug_sections)
252b5132 2210{
b34976b6 2211 bfd_boolean found;
252b5132
RH
2212 unsigned int i;
2213 unsigned int line_base;
2214 coff_data_type *cof = coff_data (abfd);
c8e7bf0d 2215 /* Run through the raw syments if available. */
252b5132
RH
2216 combined_entry_type *p;
2217 combined_entry_type *pend;
2218 alent *l;
2219 struct coff_section_tdata *sec_data;
dc810e39 2220 bfd_size_type amt;
252b5132
RH
2221
2222 /* Before looking through the symbol table, try to use a .stab
2223 section to find the information. */
2224 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
2225 &found, filename_ptr,
2226 functionname_ptr, line_ptr,
51db3708 2227 &coff_data(abfd)->line_info))
b34976b6 2228 return FALSE;
51db3708 2229
4ca29a6a 2230 if (found)
b34976b6 2231 return TRUE;
4ca29a6a 2232
51db3708 2233 /* Also try examining DWARF2 debugging information. */
fb167eb2 2234 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
51db3708 2235 filename_ptr, functionname_ptr,
fb167eb2 2236 line_ptr, NULL, debug_sections, 0,
51db3708 2237 &coff_data(abfd)->dwarf2_find_line_info))
b34976b6 2238 return TRUE;
51db3708 2239
252b5132
RH
2240 *filename_ptr = 0;
2241 *functionname_ptr = 0;
2242 *line_ptr = 0;
2243
c8e7bf0d 2244 /* Don't try and find line numbers in a non coff file. */
9bd09e22 2245 if (!bfd_family_coff (abfd))
b34976b6 2246 return FALSE;
252b5132
RH
2247
2248 if (cof == NULL)
b34976b6 2249 return FALSE;
252b5132
RH
2250
2251 /* Find the first C_FILE symbol. */
2252 p = cof->raw_syments;
2253 if (!p)
b34976b6 2254 return FALSE;
252b5132
RH
2255
2256 pend = p + cof->raw_syment_count;
2257 while (p < pend)
2258 {
2259 if (p->u.syment.n_sclass == C_FILE)
2260 break;
2261 p += 1 + p->u.syment.n_numaux;
2262 }
2263
2264 if (p < pend)
2265 {
2266 bfd_vma sec_vma;
2267 bfd_vma maxdiff;
2268
2269 /* Look through the C_FILE symbols to find the best one. */
2270 sec_vma = bfd_get_section_vma (abfd, section);
2271 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2272 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
2273 while (1)
2274 {
4d9e2b27 2275 bfd_vma file_addr;
252b5132
RH
2276 combined_entry_type *p2;
2277
2278 for (p2 = p + 1 + p->u.syment.n_numaux;
2279 p2 < pend;
2280 p2 += 1 + p2->u.syment.n_numaux)
2281 {
2282 if (p2->u.syment.n_scnum > 0
2283 && (section
2284 == coff_section_from_bfd_index (abfd,
2285 p2->u.syment.n_scnum)))
2286 break;
2287 if (p2->u.syment.n_sclass == C_FILE)
2288 {
2289 p2 = pend;
2290 break;
2291 }
2292 }
2293
4d9e2b27
NC
2294 file_addr = (bfd_vma) p2->u.syment.n_value;
2295 /* PR 11512: Include the section address of the function name symbol. */
2296 if (p2->u.syment.n_scnum > 0)
2297 file_addr += coff_section_from_bfd_index (abfd,
2298 p2->u.syment.n_scnum)->vma;
798c1fb8
ILT
2299 /* We use <= MAXDIFF here so that if we get a zero length
2300 file, we actually use the next file entry. */
252b5132 2301 if (p2 < pend
4d9e2b27
NC
2302 && offset + sec_vma >= file_addr
2303 && offset + sec_vma - file_addr <= maxdiff)
252b5132
RH
2304 {
2305 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2306 maxdiff = offset + sec_vma - p2->u.syment.n_value;
2307 }
2308
2309 /* Avoid endless loops on erroneous files by ensuring that
2310 we always move forward in the file. */
cea4409c 2311 if (p >= cof->raw_syments + p->u.syment.n_value)
252b5132
RH
2312 break;
2313
2314 p = cof->raw_syments + p->u.syment.n_value;
2315 if (p > pend || p->u.syment.n_sclass != C_FILE)
2316 break;
2317 }
2318 }
2319
c8e7bf0d 2320 /* Now wander though the raw linenumbers of the section. */
70df0c05 2321 /* If we have been called on this section before, and the offset we
252b5132
RH
2322 want is further down then we can prime the lookup loop. */
2323 sec_data = coff_section_data (abfd, section);
2324 if (sec_data != NULL
2325 && sec_data->i > 0
2326 && offset >= sec_data->offset)
2327 {
2328 i = sec_data->i;
2329 *functionname_ptr = sec_data->function;
2330 line_base = sec_data->line_base;
2331 }
2332 else
2333 {
2334 i = 0;
2335 line_base = 0;
2336 }
2337
2338 if (section->lineno != NULL)
2339 {
798c1fb8
ILT
2340 bfd_vma last_value = 0;
2341
252b5132
RH
2342 l = &section->lineno[i];
2343
2344 for (; i < section->lineno_count; i++)
2345 {
2346 if (l->line_number == 0)
2347 {
c8e7bf0d 2348 /* Get the symbol this line number points at. */
252b5132
RH
2349 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2350 if (coff->symbol.value > offset)
2351 break;
2352 *functionname_ptr = coff->symbol.name;
798c1fb8 2353 last_value = coff->symbol.value;
252b5132
RH
2354 if (coff->native)
2355 {
2356 combined_entry_type *s = coff->native;
2357 s = s + 1 + s->u.syment.n_numaux;
2358
2359 /* In XCOFF a debugging symbol can follow the
2360 function symbol. */
2361 if (s->u.syment.n_scnum == N_DEBUG)
2362 s = s + 1 + s->u.syment.n_numaux;
2363
2364 /* S should now point to the .bf of the function. */
2365 if (s->u.syment.n_numaux)
2366 {
2367 /* The linenumber is stored in the auxent. */
2368 union internal_auxent *a = &((s + 1)->u.auxent);
2369 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2370 *line_ptr = line_base;
2371 }
2372 }
2373 }
2374 else
2375 {
2376 if (l->u.offset > offset)
2377 break;
2378 *line_ptr = l->line_number + line_base - 1;
2379 }
2380 l++;
2381 }
798c1fb8
ILT
2382
2383 /* If we fell off the end of the loop, then assume that this
2384 symbol has no line number info. Otherwise, symbols with no
2385 line number info get reported with the line number of the
2386 last line of the last symbol which does have line number
2387 info. We use 0x100 as a slop to account for cases where the
2388 last line has executable code. */
2389 if (i >= section->lineno_count
2390 && last_value != 0
2391 && offset - last_value > 0x100)
2392 {
2393 *functionname_ptr = NULL;
2394 *line_ptr = 0;
2395 }
252b5132
RH
2396 }
2397
2398 /* Cache the results for the next call. */
2399 if (sec_data == NULL && section->owner == abfd)
2400 {
dc810e39 2401 amt = sizeof (struct coff_section_tdata);
c8e7bf0d 2402 section->used_by_bfd = bfd_zalloc (abfd, amt);
252b5132
RH
2403 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
2404 }
2405 if (sec_data != NULL)
2406 {
2407 sec_data->offset = offset;
70df0c05 2408 sec_data->i = i - 1;
252b5132
RH
2409 sec_data->function = *functionname_ptr;
2410 sec_data->line_base = line_base;
2411 }
2412
b34976b6 2413 return TRUE;
252b5132
RH
2414}
2415
fc28f9aa
TG
2416bfd_boolean
2417coff_find_nearest_line (bfd *abfd,
fc28f9aa 2418 asymbol **symbols,
fb167eb2 2419 asection *section,
fc28f9aa
TG
2420 bfd_vma offset,
2421 const char **filename_ptr,
2422 const char **functionname_ptr,
fb167eb2
AM
2423 unsigned int *line_ptr,
2424 unsigned int *discriminator_ptr)
fc28f9aa 2425{
fb167eb2
AM
2426 if (discriminator_ptr)
2427 *discriminator_ptr = 0;
2428 return coff_find_nearest_line_with_names (abfd, symbols, section, offset,
fc28f9aa 2429 filename_ptr, functionname_ptr,
fb167eb2 2430 line_ptr, dwarf_debug_sections);
fc28f9aa
TG
2431}
2432
4ab527b0
FF
2433bfd_boolean
2434coff_find_inliner_info (bfd *abfd,
2435 const char **filename_ptr,
2436 const char **functionname_ptr,
2437 unsigned int *line_ptr)
2438{
2439 bfd_boolean found;
2440
2441 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
2442 functionname_ptr, line_ptr,
2443 &coff_data(abfd)->dwarf2_find_line_info);
2444 return (found);
2445}
2446
252b5132 2447int
a6b96beb 2448coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
252b5132
RH
2449{
2450 size_t size;
2451
a6b96beb 2452 if (!info->relocatable)
c8e7bf0d 2453 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
252b5132 2454 else
c8e7bf0d 2455 size = bfd_coff_filhsz (abfd);
252b5132
RH
2456
2457 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2458 return size;
2459}
2460
2461/* Change the class of a coff symbol held by BFD. */
c8e7bf0d 2462
b34976b6 2463bfd_boolean
c8e7bf0d
NC
2464bfd_coff_set_symbol_class (bfd * abfd,
2465 asymbol * symbol,
96d56e9f 2466 unsigned int symbol_class)
252b5132
RH
2467{
2468 coff_symbol_type * csym;
2469
2470 csym = coff_symbol_from (abfd, symbol);
2471 if (csym == NULL)
2472 {
2473 bfd_set_error (bfd_error_invalid_operation);
b34976b6 2474 return FALSE;
252b5132
RH
2475 }
2476 else if (csym->native == NULL)
2477 {
2478 /* This is an alien symbol which no native coff backend data.
2479 We cheat here by creating a fake native entry for it and
2480 then filling in the class. This code is based on that in
2481 coff_write_alien_symbol(). */
244148ad 2482
252b5132 2483 combined_entry_type * native;
dc810e39 2484 bfd_size_type amt = sizeof (* native);
252b5132 2485
a50b1753 2486 native = (combined_entry_type *) bfd_zalloc (abfd, amt);
252b5132 2487 if (native == NULL)
b34976b6 2488 return FALSE;
252b5132 2489
252b5132 2490 native->u.syment.n_type = T_NULL;
96d56e9f 2491 native->u.syment.n_sclass = symbol_class;
244148ad 2492
252b5132
RH
2493 if (bfd_is_und_section (symbol->section))
2494 {
2495 native->u.syment.n_scnum = N_UNDEF;
2496 native->u.syment.n_value = symbol->value;
2497 }
2498 else if (bfd_is_com_section (symbol->section))
2499 {
2500 native->u.syment.n_scnum = N_UNDEF;
2501 native->u.syment.n_value = symbol->value;
2502 }
2503 else
2504 {
2505 native->u.syment.n_scnum =
2506 symbol->section->output_section->target_index;
2507 native->u.syment.n_value = (symbol->value
2508 + symbol->section->output_offset);
2509 if (! obj_pe (abfd))
2510 native->u.syment.n_value += symbol->section->output_section->vma;
244148ad 2511
08da05b0 2512 /* Copy the any flags from the file header into the symbol.
252b5132
RH
2513 FIXME: Why? */
2514 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
2515 }
244148ad 2516
252b5132
RH
2517 csym->native = native;
2518 }
2519 else
96d56e9f 2520 csym->native->u.syment.n_sclass = symbol_class;
244148ad 2521
b34976b6 2522 return TRUE;
252b5132 2523}
082b7297
L
2524
2525struct coff_comdat_info *
2526bfd_coff_get_comdat_section (bfd *abfd, struct bfd_section *sec)
2527{
fedf8d51
AM
2528 if (bfd_get_flavour (abfd) == bfd_target_coff_flavour
2529 && coff_section_data (abfd, sec) != NULL)
082b7297
L
2530 return coff_section_data (abfd, sec)->comdat;
2531 else
2532 return NULL;
2533}
c77ec726
AM
2534
2535bfd_boolean
2536_bfd_coff_section_already_linked (bfd *abfd,
2537 asection *sec,
2538 struct bfd_link_info *info)
2539{
2540 flagword flags;
2541 const char *name, *key;
2542 struct bfd_section_already_linked *l;
2543 struct bfd_section_already_linked_hash_entry *already_linked_list;
2544 struct coff_comdat_info *s_comdat;
2545
2546 flags = sec->flags;
2547 if ((flags & SEC_LINK_ONCE) == 0)
2548 return FALSE;
2549
2550 /* The COFF backend linker doesn't support group sections. */
2551 if ((flags & SEC_GROUP) != 0)
2552 return FALSE;
2553
2554 name = bfd_get_section_name (abfd, sec);
2555 s_comdat = bfd_coff_get_comdat_section (abfd, sec);
2556
2557 if (s_comdat != NULL)
2558 key = s_comdat->name;
2559 else
2560 {
2561 if (CONST_STRNEQ (name, ".gnu.linkonce.")
2562 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
2563 key++;
2564 else
2565 /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2566 .xdata$<key> and .pdata$<key> only the first of which has a
2567 comdat key. Should these all match the LTO IR key? */
2568 key = name;
2569 }
2570
2571 already_linked_list = bfd_section_already_linked_table_lookup (key);
2572
2573 for (l = already_linked_list->entry; l != NULL; l = l->next)
2574 {
2575 struct coff_comdat_info *l_comdat;
2576
2577 l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
2578
2579 /* The section names must match, and both sections must be
2580 comdat and have the same comdat name, or both sections must
2581 be non-comdat. LTO IR plugin sections are an exception. They
2582 are always named .gnu.linkonce.t.<key> (<key> is some string)
2583 and match any comdat section with comdat name of <key>, and
2584 any linkonce section with the same suffix, ie.
2585 .gnu.linkonce.*.<key>. */
2586 if (((s_comdat != NULL) == (l_comdat != NULL)
2587 && strcmp (name, l->sec->name) == 0)
2588 || (l->sec->owner->flags & BFD_PLUGIN) != 0)
2589 {
2590 /* The section has already been linked. See if we should
2591 issue a warning. */
2592 return _bfd_handle_already_linked (sec, l, info);
2593 }
2594 }
2595
2596 /* This is the first section with this name. Record it. */
2597 if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
2598 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
2599 return FALSE;
2600}
This page took 0.827105 seconds and 4 git commands to generate.