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