]> Git Repo - binutils.git/blame - bfd/coffgen.c
* Makefile.in (sysdep.h): Copy Ian's version of this from
[binutils.git] / bfd / coffgen.c
CommitLineData
075caafd 1/* Support for the generic parts of COFF, for BFD.
9783e04a 2 Copyright 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
075caafd
ILT
3 Written by Cygnus Support.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21/* Most of this hacked by Steve Chamberlain, [email protected].
22 Split out of coffcode.h by Ian Taylor, [email protected]. */
23
24/* This file contains COFF code that is not dependent on any
25 particular COFF target. There is only one version of this file in
26 libbfd.a, so no target specific code may be put in here. Or, to
27 put it another way,
28
29 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
30
31 If you need to add some target specific behaviour, add a new hook
32 function to bfd_coff_backend_data.
33
34 Some of these functions are also called by the ECOFF routines.
35 Those functions may not use any COFF specific information, such as
36 coff_data (abfd). */
37
38#include "bfd.h"
39#include "sysdep.h"
40#include "libbfd.h"
41#include "coff/internal.h"
42#include "libcoff.h"
43
bfe8224f
ILT
44static boolean coff_write_symbol PARAMS ((bfd *, asymbol *,
45 combined_entry_type *,
46 unsigned int *));
47static boolean coff_write_alien_symbol PARAMS ((bfd *, asymbol *,
48 unsigned int *));
49static boolean coff_write_native_symbol PARAMS ((bfd *, coff_symbol_type *,
50 unsigned int *));
51
075caafd
ILT
52static asection bfd_debug_section = { "*DEBUG*" };
53
54/* Take a section header read from a coff file (in HOST byte order),
55 and make a BFD "section" out of it. This is used by ECOFF. */
56static boolean
25057836
JL
57make_a_section_from_file (abfd, hdr, target_index)
58 bfd *abfd;
59 struct internal_scnhdr *hdr;
60 unsigned int target_index;
075caafd
ILT
61{
62 asection *return_section;
63 char *name;
64
65 /* Assorted wastage to null-terminate the name, thanks AT&T! */
66 name = bfd_alloc(abfd, sizeof (hdr->s_name)+1);
67 if (name == NULL) {
d1ad85a6 68 bfd_set_error (bfd_error_no_memory);
075caafd
ILT
69 return false;
70 }
71 strncpy(name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
72 name[sizeof (hdr->s_name)] = 0;
73
74 return_section = bfd_make_section(abfd, name);
75 if (return_section == NULL)
76 return_section = bfd_coff_make_section_hook (abfd, name);
4c3721d5
ILT
77
78 /* Handle several sections of the same name. For example, if an executable
79 has two .bss sections, GDB better be able to find both of them
80 (PR 3562). */
81 if (return_section == NULL)
82 return_section = bfd_make_section_anyway (abfd, name);
83
075caafd
ILT
84 if (return_section == NULL)
85 return false;
86
87 /* s_paddr is presumed to be = to s_vaddr */
88
89 return_section->vma = hdr->s_vaddr;
90 return_section->_raw_size = hdr->s_size;
91 return_section->filepos = hdr->s_scnptr;
92 return_section->rel_filepos = hdr->s_relptr;
93 return_section->reloc_count = hdr->s_nreloc;
94
95 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
96
97 return_section->line_filepos = hdr->s_lnnoptr;
98
99 return_section->lineno_count = hdr->s_nlnno;
100 return_section->userdata = NULL;
101 return_section->next = (asection *) NULL;
102 return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr);
103
104 return_section->target_index = target_index;
105
2d1e6c9c
KR
106 /* At least on i386-coff, the line number count for a shared library
107 section must be ignored. */
c16313f0 108 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2d1e6c9c
KR
109 return_section->lineno_count = 0;
110
075caafd
ILT
111 if (hdr->s_nreloc != 0)
112 return_section->flags |= SEC_RELOC;
113 /* FIXME: should this check 'hdr->s_size > 0' */
114 if (hdr->s_scnptr != 0)
115 return_section->flags |= SEC_HAS_CONTENTS;
116 return true;
117}
118
119/* Read in a COFF object and make it into a BFD. This is used by
120 ECOFF as well. */
121
122static
123bfd_target *
25057836
JL
124coff_real_object_p (abfd, nscns, internal_f, internal_a)
125 bfd *abfd;
126 unsigned nscns;
127 struct internal_filehdr *internal_f;
128 struct internal_aouthdr *internal_a;
075caafd
ILT
129{
130 PTR tdata;
131 size_t readsize; /* length of file_info */
132 unsigned int scnhsz;
133 char *external_sections;
134
135 /* Build a play area */
27f524a3 136 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
075caafd
ILT
137 if (tdata == NULL)
138 return 0;
139
140 scnhsz = bfd_coff_scnhsz (abfd);
141 readsize = nscns * scnhsz;
142 external_sections = (char *)bfd_alloc(abfd, readsize);
9783e04a
DM
143 if (!external_sections)
144 {
d1ad85a6 145 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
146 goto fail;
147 }
075caafd
ILT
148
149 if (bfd_read((PTR)external_sections, 1, readsize, abfd) != readsize) {
150 goto fail;
151 }
152
153 /* Now copy data as required; construct all asections etc */
154 if (nscns != 0) {
155 unsigned int i;
156 for (i = 0; i < nscns; i++) {
157 struct internal_scnhdr tmp;
158 bfd_coff_swap_scnhdr_in(abfd, (PTR) (external_sections + i * scnhsz),
159 (PTR) &tmp);
160 make_a_section_from_file(abfd,&tmp, i+1);
161 }
162 }
163
164/* make_abs_section(abfd);*/
165
166 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
167 goto fail;
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
179 bfd_get_symcount(abfd) = internal_f->f_nsyms;
180 if (internal_f->f_nsyms)
181 abfd->flags |= HAS_SYMS;
182
27f524a3
ILT
183 if (internal_a != (struct internal_aouthdr *) NULL)
184 bfd_get_start_address (abfd) = internal_a->entry;
185 else
186 bfd_get_start_address (abfd) = 0;
075caafd
ILT
187
188 return abfd->xvec;
189 fail:
190 bfd_release(abfd, tdata);
191 return (bfd_target *)NULL;
192}
193
d1ad85a6 194/* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
075caafd
ILT
195 not a COFF file. This is also used by ECOFF. */
196
197bfd_target *
25057836
JL
198coff_object_p (abfd)
199 bfd *abfd;
075caafd
ILT
200{
201 unsigned int filhsz;
202 unsigned int aoutsz;
203 int nscns;
204 PTR filehdr;
205 struct internal_filehdr internal_f;
206 struct internal_aouthdr internal_a;
207
075caafd
ILT
208 /* figure out how much to read */
209 filhsz = bfd_coff_filhsz (abfd);
210 aoutsz = bfd_coff_aoutsz (abfd);
211
212 filehdr = bfd_alloc (abfd, filhsz);
213 if (filehdr == NULL)
214 return 0;
215 if (bfd_read(filehdr, 1, filhsz, abfd) != filhsz)
25057836
JL
216 {
217 if (bfd_get_error () != bfd_error_system_call)
218 bfd_set_error (bfd_error_wrong_format);
219 return 0;
220 }
075caafd
ILT
221 bfd_coff_swap_filehdr_in(abfd, filehdr, &internal_f);
222 bfd_release (abfd, filehdr);
223
224 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false) {
d1ad85a6 225 bfd_set_error (bfd_error_wrong_format);
075caafd
ILT
226 return 0;
227 }
228 nscns =internal_f.f_nscns;
229
230 if (internal_f.f_opthdr) {
231 PTR opthdr;
232
233 opthdr = bfd_alloc (abfd, aoutsz);
234 if (opthdr == NULL)
235 return 0;;
236 if (bfd_read(opthdr, 1,aoutsz, abfd) != aoutsz) {
237 return 0;
238 }
239 bfd_coff_swap_aouthdr_in(abfd, opthdr, (PTR)&internal_a);
240 }
241
242 /* Seek past the opt hdr stuff */
c16313f0
ILT
243 if (bfd_seek(abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET)
244 != 0)
245 return NULL;
075caafd 246
27f524a3
ILT
247 return coff_real_object_p(abfd, nscns, &internal_f,
248 (internal_f.f_opthdr != 0
249 ? &internal_a
250 : (struct internal_aouthdr *) NULL));
075caafd
ILT
251}
252
253/* Get the BFD section from a COFF symbol section number. */
254
255struct sec *
25057836
JL
256coff_section_from_bfd_index (abfd, index)
257 bfd *abfd;
258 int index;
075caafd
ILT
259{
260 struct sec *answer = abfd->sections;
261
262 if (index == N_ABS)
263 {
264 return &bfd_abs_section;
265 }
266 if (index == N_UNDEF)
267 {
268 return &bfd_und_section;
269 }
270 if(index == N_DEBUG)
271 {
272 return &bfd_debug_section;
273
274 }
275
276 while (answer) {
277 if (answer->target_index == index)
278 return answer;
279 answer = answer->next;
280 }
c16313f0
ILT
281
282 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
283 has a bad symbol table in biglitpow.o. */
284 return &bfd_und_section;
075caafd
ILT
285}
286
287/* Get the upper bound of a COFF symbol table. */
288
326e32d7 289long
075caafd
ILT
290coff_get_symtab_upper_bound(abfd)
291bfd *abfd;
292{
293 if (!bfd_coff_slurp_symbol_table(abfd))
326e32d7 294 return -1;
075caafd
ILT
295
296 return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *));
297}
298
299
300/* Canonicalize a COFF symbol table. */
301
326e32d7 302long
25057836
JL
303coff_get_symtab (abfd, alocation)
304 bfd *abfd;
305 asymbol **alocation;
075caafd
ILT
306{
307 unsigned int counter = 0;
308 coff_symbol_type *symbase;
309 coff_symbol_type **location = (coff_symbol_type **) (alocation);
310 if (!bfd_coff_slurp_symbol_table(abfd))
326e32d7 311 return -1;
075caafd
ILT
312
313 symbase = obj_symbols(abfd);
314 while (counter < bfd_get_symcount(abfd))
315 {
316 /* This nasty code looks at the symbol to decide whether or
317 not it is descibes a constructor/destructor entry point. It
318 is structured this way to (hopefully) speed non matches */
319#if 0
320 if (0 && symbase->symbol.name[9] == '$')
321 {
322 bfd_constructor_entry(abfd,
323 (asymbol **)location,
324 symbase->symbol.name[10] == 'I' ?
325 "CTOR" : "DTOR");
326 }
327#endif
328 *(location++) = symbase++;
329 counter++;
330 }
331 *location++ = 0;
332 return bfd_get_symcount(abfd);
333}
334
335/* Set lineno_count for the output sections of a COFF file. */
336
27f524a3 337int
25057836
JL
338coff_count_linenumbers (abfd)
339 bfd *abfd;
075caafd
ILT
340{
341 unsigned int limit = bfd_get_symcount(abfd);
342 unsigned int i;
27f524a3 343 int total = 0;
075caafd 344 asymbol **p;
27f524a3
ILT
345 {
346 asection *s = abfd->sections->output_section;
347 while (s) {
348 BFD_ASSERT(s->lineno_count == 0);
349 s = s->next;
350 }
351 }
075caafd
ILT
352
353
354 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) {
355 asymbol *q_maybe = *p;
356 if (bfd_asymbol_flavour(q_maybe) == bfd_target_coff_flavour) {
357 coff_symbol_type *q = coffsymbol(q_maybe);
358 if (q->lineno) {
359 /*
360 This symbol has a linenumber, increment the owning
361 section's linenumber count
362 */
363 alent *l = q->lineno;
364 q->symbol.section->output_section->lineno_count++;
27f524a3 365 total ++;
075caafd
ILT
366 l++;
367 while (l->line_number) {
27f524a3 368 total ++;
075caafd
ILT
369 q->symbol.section->output_section->lineno_count++;
370 l++;
371 }
372 }
373 }
374 }
27f524a3 375 return total;
075caafd
ILT
376}
377
378/* Takes a bfd and a symbol, returns a pointer to the coff specific
379 area of the symbol if there is one. */
380
330595d0 381/*ARGSUSED*/
075caafd 382coff_symbol_type *
25057836
JL
383coff_symbol_from (ignore_abfd, symbol)
384 bfd *ignore_abfd;
385 asymbol *symbol;
075caafd
ILT
386{
387 if (bfd_asymbol_flavour(symbol) != bfd_target_coff_flavour)
388 return (coff_symbol_type *)NULL;
389
390 if (bfd_asymbol_bfd(symbol)->tdata.coff_obj_data == (coff_data_type*)NULL)
391 return (coff_symbol_type *)NULL;
392
393 return (coff_symbol_type *) symbol;
394}
395
396static void
25057836
JL
397fixup_symbol_value (coff_symbol_ptr, syment)
398 coff_symbol_type *coff_symbol_ptr;
399 struct internal_syment *syment;
075caafd
ILT
400{
401
402 /* Normalize the symbol flags */
e61cfdf8 403 if (bfd_is_com_section (coff_symbol_ptr->symbol.section)) {
075caafd
ILT
404 /* a common symbol is undefined with a value */
405 syment->n_scnum = N_UNDEF;
406 syment->n_value = coff_symbol_ptr->symbol.value;
407 }
408 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) {
409 syment->n_value = coff_symbol_ptr->symbol.value;
410 }
411 else if (coff_symbol_ptr->symbol.section == & bfd_und_section) {
412 syment->n_scnum = N_UNDEF;
413 syment->n_value = 0;
414 }
415 else {
416 if (coff_symbol_ptr->symbol.section) {
417 syment->n_scnum =
418 coff_symbol_ptr->symbol.section->output_section->target_index;
419
420 syment->n_value =
421 coff_symbol_ptr->symbol.value +
422 coff_symbol_ptr->symbol.section->output_offset +
423 coff_symbol_ptr->symbol.section->output_section->vma;
424 }
425 else {
426 BFD_ASSERT(0);
427 /* This can happen, but I don't know why yet ([email protected]) */
428 syment->n_scnum = N_ABS;
429 syment->n_value = coff_symbol_ptr->symbol.value;
430 }
431 }
432}
433
434/* run through all the symbols in the symbol table and work out what
435 their indexes into the symbol table will be when output
436
437 Coff requires that each C_FILE symbol points to the next one in the
438 chain, and that the last one points to the first external symbol. We
439 do that here too.
440
441*/
9783e04a 442boolean
25057836
JL
443coff_renumber_symbols (bfd_ptr)
444 bfd *bfd_ptr;
075caafd
ILT
445{
446 unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
447 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
448 unsigned int native_index = 0;
449 struct internal_syment *last_file = (struct internal_syment *)NULL;
450 unsigned int symbol_index;
451
452 /* COFF demands that undefined symbols come after all other symbols.
453 Since we don't need to impose this extra knowledge on all our client
454 programs, deal with that here. Sort the symbol table; just move the
455 undefined symbols to the end, leaving the rest alone. */
456 /* @@ Do we have some condition we could test for, so we don't always
457 have to do this? I don't think relocatability is quite right, but
458 I'm not certain. [raeburn:19920508.1711EST] */
459 {
460 asymbol **newsyms;
461 int i;
462
463 newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
464 sizeof (asymbol *)
465 * (symbol_count + 1));
9783e04a
DM
466 if (!newsyms)
467 {
d1ad85a6 468 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
469 return false;
470 }
075caafd
ILT
471 bfd_ptr->outsymbols = newsyms;
472 for (i = 0; i < symbol_count; i++)
473 if (symbol_ptr_ptr[i]->section != &bfd_und_section)
474 *newsyms++ = symbol_ptr_ptr[i];
475 for (i = 0; i < symbol_count; i++)
476 if (symbol_ptr_ptr[i]->section == &bfd_und_section)
477 *newsyms++ = symbol_ptr_ptr[i];
478 *newsyms = (asymbol *) NULL;
479 symbol_ptr_ptr = bfd_ptr->outsymbols;
480 }
481
482 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
483 {
484 coff_symbol_type *coff_symbol_ptr = coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
485 if (coff_symbol_ptr && coff_symbol_ptr->native) {
486 combined_entry_type *s = coff_symbol_ptr->native;
487 int i;
488
489 if (s->u.syment.n_sclass == C_FILE)
490 {
491 if (last_file != (struct internal_syment *)NULL) {
492 last_file->n_value = native_index;
493 }
494 last_file = &(s->u.syment);
495 }
496 else {
497
498 /* Modify the symbol values according to their section and
499 type */
500
501 fixup_symbol_value(coff_symbol_ptr, &(s->u.syment));
502 }
503 for (i = 0; i < s->u.syment.n_numaux + 1; i++) {
504 s[i].offset = native_index ++;
505 }
506 }
507 else {
508 native_index++;
509 }
510 }
511 obj_conv_table_size (bfd_ptr) = native_index;
9783e04a 512 return true;
075caafd
ILT
513}
514
515/*
516 Run thorough the symbol table again, and fix it so that all pointers to
517 entries are changed to the entries' index in the output symbol table.
518
519*/
520void
9783e04a
DM
521coff_mangle_symbols (bfd_ptr)
522 bfd *bfd_ptr;
075caafd 523{
9783e04a 524 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
075caafd
ILT
525 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
526 unsigned int symbol_index;
527
528 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
9783e04a
DM
529 {
530 coff_symbol_type *coff_symbol_ptr =
531 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
075caafd 532
9783e04a
DM
533 if (coff_symbol_ptr && coff_symbol_ptr->native)
534 {
075caafd
ILT
535 int i;
536 combined_entry_type *s = coff_symbol_ptr->native;
537
9783e04a
DM
538 if (s->fix_value)
539 {
540 /* FIXME: We should use a union here. */
541 s->u.syment.n_value =
542 ((combined_entry_type *) s->u.syment.n_value)->offset;
543 s->fix_value = 0;
075caafd 544 }
9783e04a
DM
545 for (i = 0; i < s->u.syment.n_numaux ; i++)
546 {
547 combined_entry_type *a = s + i + 1;
548 if (a->fix_tag)
549 {
550 a->u.auxent.x_sym.x_tagndx.l =
551 a->u.auxent.x_sym.x_tagndx.p->offset;
552 a->fix_tag = 0;
553 }
554 if (a->fix_end)
555 {
556 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
557 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
558 a->fix_end = 0;
559 }
560 if (a->fix_scnlen)
561 {
562 a->u.auxent.x_csect.x_scnlen.l =
563 a->u.auxent.x_csect.x_scnlen.p->offset;
564 a->fix_scnlen = 0;
565 }
075caafd 566 }
075caafd 567 }
9783e04a 568 }
075caafd
ILT
569}
570
9783e04a
DM
571static bfd_size_type string_size;
572static bfd_size_type debug_string_size;
573static asection *debug_string_section;
075caafd
ILT
574
575static void
25057836
JL
576coff_fix_symbol_name (abfd, symbol, native)
577 bfd *abfd;
578 asymbol *symbol;
579 combined_entry_type *native;
075caafd
ILT
580{
581 unsigned int name_length;
582 union internal_auxent *auxent;
583 char * name = ( char *)(symbol->name);
584
585 if (name == (char *) NULL) {
586 /* coff symbols always have names, so we'll make one up */
587 symbol->name = "strange";
588 name = (char *)symbol->name;
589 }
590 name_length = strlen(name);
591
592 if (native->u.syment.n_sclass == C_FILE) {
593 strncpy(native->u.syment._n._n_name, ".file", SYMNMLEN);
594 auxent = &(native+1)->u.auxent;
595
596 if (bfd_coff_long_filenames (abfd)) {
597 if (name_length <= FILNMLEN) {
598 strncpy(auxent->x_file.x_fname, name, FILNMLEN);
599 }
600 else {
601 auxent->x_file.x_n.x_offset = string_size + 4;
602 auxent->x_file.x_n.x_zeroes = 0;
603 string_size += name_length + 1;
604 }
605 }
606 else {
607 strncpy(auxent->x_file.x_fname, name, FILNMLEN);
608 if (name_length > FILNMLEN) {
609 name[FILNMLEN] = '\0';
610 }
611 }
612 }
613 else
9783e04a
DM
614 { /* NOT A C_FILE SYMBOL */
615 if (name_length <= SYMNMLEN)
616 {
075caafd
ILT
617 /* This name will fit into the symbol neatly */
618 strncpy(native->u.syment._n._n_name, symbol->name, SYMNMLEN);
619 }
9783e04a
DM
620 else if (! bfd_coff_symname_in_debug (abfd, &native->u.syment))
621 {
075caafd
ILT
622 native->u.syment._n._n_n._n_offset = string_size + 4;
623 native->u.syment._n._n_n._n_zeroes = 0;
624 string_size += name_length + 1;
625 }
9783e04a
DM
626 else
627 {
628 long filepos;
629 bfd_byte buf[2];
630
631 /* This name should be written into the .debug section. For
632 some reason each name is preceded by a two byte length
633 and also followed by a null byte. FIXME: We assume that
634 the .debug section has already been created, and that it
635 is large enough. */
636 if (debug_string_section == (asection *) NULL)
637 debug_string_section = bfd_get_section_by_name (abfd, ".debug");
638 filepos = bfd_tell (abfd);
639 bfd_put_16 (abfd, name_length + 1, buf);
640 if (! bfd_set_section_contents (abfd,
641 debug_string_section,
642 (PTR) buf,
643 (file_ptr) debug_string_size,
644 (bfd_size_type) 2)
645 || ! bfd_set_section_contents (abfd,
646 debug_string_section,
647 (PTR) symbol->name,
648 (file_ptr) debug_string_size + 2,
649 (bfd_size_type) name_length + 1))
650 abort ();
651 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
652 abort ();
653 native->u.syment._n._n_n._n_offset = debug_string_size + 2;
654 native->u.syment._n._n_n._n_zeroes = 0;
655 debug_string_size += name_length + 3;
656 }
657 }
075caafd
ILT
658}
659
bfe8224f
ILT
660/* We need to keep track of the symbol index so that when we write out
661 the relocs we can get the index for a symbol. This method is a
662 hack. FIXME. */
663
664#define set_index(symbol, idx) ((symbol)->udata = (PTR) (idx))
665
666/* Write a symbol out to a COFF file. */
075caafd 667
bfe8224f 668static boolean
25057836
JL
669coff_write_symbol (abfd, symbol, native, written)
670 bfd *abfd;
671 asymbol *symbol;
672 combined_entry_type *native;
bfe8224f 673 unsigned int *written;
075caafd 674{
bfe8224f
ILT
675 unsigned int numaux = native->u.syment.n_numaux;
676 int type = native->u.syment.n_type;
677 int class = native->u.syment.n_sclass;
075caafd
ILT
678 PTR buf;
679 bfd_size_type symesz;
680
bfe8224f
ILT
681 /* @@ bfd_debug_section isn't accessible outside this file, but we
682 know that C_FILE symbols belong there. So move them. */
075caafd
ILT
683 if (native->u.syment.n_sclass == C_FILE)
684 symbol->section = &bfd_debug_section;
685
686 if (symbol->section == &bfd_abs_section)
bfe8224f
ILT
687 {
688 native->u.syment.n_scnum = N_ABS;
689 }
075caafd 690 else if (symbol->section == &bfd_debug_section)
bfe8224f
ILT
691 {
692 native->u.syment.n_scnum = N_DEBUG;
693 }
075caafd 694 else if (symbol->section == &bfd_und_section)
bfe8224f
ILT
695 {
696 native->u.syment.n_scnum = N_UNDEF;
697 }
075caafd 698 else
bfe8224f
ILT
699 {
700 native->u.syment.n_scnum =
701 symbol->section->output_section->target_index;
702 }
075caafd 703
bfe8224f 704 coff_fix_symbol_name (abfd, symbol, native);
075caafd
ILT
705
706 symesz = bfd_coff_symesz (abfd);
707 buf = bfd_alloc (abfd, symesz);
9783e04a
DM
708 if (!buf)
709 {
d1ad85a6 710 bfd_set_error (bfd_error_no_memory);
bfe8224f 711 return false;
9783e04a 712 }
bfe8224f
ILT
713 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
714 if (bfd_write (buf, 1, symesz, abfd) != symesz)
715 return false;
075caafd
ILT
716 bfd_release (abfd, buf);
717
718 if (native->u.syment.n_numaux > 0)
719 {
720 bfd_size_type auxesz;
721 unsigned int j;
722
723 auxesz = bfd_coff_auxesz (abfd);
724 buf = bfd_alloc (abfd, auxesz);
9783e04a
DM
725 if (!buf)
726 {
d1ad85a6 727 bfd_set_error (bfd_error_no_memory);
bfe8224f 728 return false;
9783e04a 729 }
075caafd
ILT
730 for (j = 0; j < native->u.syment.n_numaux; j++)
731 {
bfe8224f
ILT
732 bfd_coff_swap_aux_out (abfd,
733 &((native + j + 1)->u.auxent),
734 type,
735 class,
736 j,
737 native->u.syment.n_numaux,
738 buf);
739 if (bfd_write (buf, 1, auxesz, abfd)!= auxesz)
740 return false;
075caafd
ILT
741 }
742 bfd_release (abfd, buf);
743 }
bfe8224f
ILT
744
745 /* Store the index for use when we write out the relocs. */
746 set_index (symbol, *written);
747
748 *written += numaux + 1;
749 return true;
075caafd
ILT
750}
751
bfe8224f
ILT
752/* Write out a symbol to a COFF file that does not come from a COFF
753 file originally. This symbol may have been created by the linker,
754 or we may be linking a non COFF file to a COFF file. */
075caafd 755
bfe8224f 756static boolean
25057836
JL
757coff_write_alien_symbol (abfd, symbol, written)
758 bfd *abfd;
759 asymbol *symbol;
bfe8224f 760 unsigned int *written;
075caafd 761{
075caafd
ILT
762 combined_entry_type *native;
763 combined_entry_type dummy;
bfe8224f 764
075caafd
ILT
765 native = &dummy;
766 native->u.syment.n_type = T_NULL;
767 native->u.syment.n_flags = 0;
768 if (symbol->section == &bfd_und_section)
bfe8224f
ILT
769 {
770 native->u.syment.n_scnum = N_UNDEF;
771 native->u.syment.n_value = symbol->value;
075caafd 772 }
e61cfdf8 773 else if (bfd_is_com_section (symbol->section))
bfe8224f
ILT
774 {
775 native->u.syment.n_scnum = N_UNDEF;
776 native->u.syment.n_value = symbol->value;
075caafd 777 }
bfe8224f 778 else if (symbol->flags & BSF_DEBUGGING)
075caafd 779 {
bfe8224f
ILT
780 /* Remove the symbol name so that it does not take up any space.
781 COFF won't know what to do with it anyhow. */
782 symbol->name = "";
075caafd 783 }
bfe8224f
ILT
784 else
785 {
786 native->u.syment.n_scnum =
787 symbol->section->output_section->target_index;
788 native->u.syment.n_value = (symbol->value
789 + symbol->section->output_section->vma
790 + symbol->section->output_offset);
791
792 /* Copy the any flags from the the file header into the symbol.
793 FIXME: Why? */
794 {
795 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
796 if (c != (coff_symbol_type *) NULL)
797 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
798 }
075caafd
ILT
799 }
800
bfe8224f 801 native->u.syment.n_type = 0;
075caafd 802 if (symbol->flags & BSF_LOCAL)
bfe8224f 803 native->u.syment.n_sclass = C_STAT;
075caafd 804 else
bfe8224f
ILT
805 native->u.syment.n_sclass = C_EXT;
806 native->u.syment.n_numaux = 0;
075caafd 807
bfe8224f 808 return coff_write_symbol (abfd, symbol, native, written);
075caafd
ILT
809}
810
bfe8224f
ILT
811/* Write a native symbol to a COFF file. */
812
813static boolean
25057836
JL
814coff_write_native_symbol (abfd, symbol, written)
815 bfd *abfd;
816 coff_symbol_type *symbol;
bfe8224f 817 unsigned int *written;
075caafd 818{
075caafd 819 combined_entry_type *native = symbol->native;
bfe8224f 820 alent *lineno = symbol->lineno;
075caafd 821
bfe8224f
ILT
822 /* If this symbol has an associated line number, we must store the
823 symbol index in the line number field. We also tag the auxent to
824 point to the right place in the lineno table. */
825 if (lineno && !symbol->done_lineno)
826 {
827 unsigned int count = 0;
828 lineno[count].u.offset = *written;
829 if (native->u.syment.n_numaux)
830 {
831 union internal_auxent *a = &((native+1)->u.auxent);
075caafd 832
bfe8224f
ILT
833 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
834 symbol->symbol.section->output_section->moving_line_filepos;
835 }
075caafd 836
bfe8224f
ILT
837 /* Count and relocate all other linenumbers. */
838 count++;
839 while (lineno[count].line_number != 0)
840 {
075caafd 841#if 0
bfe8224f
ILT
842 /* 13 april 92. sac
843 I've been told this, but still need proof:
844 > The second bug is also in `bfd/coffcode.h'. This bug
845 > causes the linker to screw up the pc-relocations for
846 > all the line numbers in COFF code. This bug isn't only
847 > specific to A29K implementations, but affects all
848 > systems using COFF format binaries. Note that in COFF
849 > object files, the line number core offsets output by
850 > the assembler are relative to the start of each
851 > procedure, not to the start of the .text section. This
852 > patch relocates the line numbers relative to the
853 > `native->u.syment.n_value' instead of the section
854 > virtual address.
855 > [email protected] (Jon Olson)
856 */
857 lineno[count].u.offset += native->u.syment.n_value;
075caafd 858#else
bfe8224f
ILT
859 lineno[count].u.offset +=
860 (symbol->symbol.section->output_section->vma
861 + symbol->symbol.section->output_offset);
075caafd 862#endif
bfe8224f
ILT
863 count++;
864 }
865 symbol->done_lineno = true;
075caafd 866
bfe8224f
ILT
867 symbol->symbol.section->output_section->moving_line_filepos +=
868 count * bfd_coff_linesz (abfd);
869 }
870
871 return coff_write_symbol (abfd, &(symbol->symbol), native, written);
075caafd
ILT
872}
873
bfe8224f
ILT
874/* Write out the COFF symbols. */
875
876boolean
25057836 877coff_write_symbols (abfd)
bfe8224f 878 bfd *abfd;
075caafd 879{
bfe8224f
ILT
880 unsigned int i;
881 unsigned int limit = bfd_get_symcount(abfd);
882 unsigned int written = 0;
883 asymbol **p;
075caafd
ILT
884
885 string_size = 0;
9783e04a 886 debug_string_size = 0;
075caafd
ILT
887
888 /* Seek to the right place */
bfe8224f
ILT
889 if (bfd_seek (abfd, obj_sym_filepos(abfd), SEEK_SET) != 0)
890 return false;
075caafd
ILT
891
892 /* Output all the symbols we have */
893
894 written = 0;
895 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
bfe8224f
ILT
896 {
897 asymbol *symbol = *p;
898 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
075caafd 899
bfe8224f
ILT
900 if (c_symbol == (coff_symbol_type *) NULL
901 || c_symbol->native == (combined_entry_type *)NULL)
902 {
903 if (! coff_write_alien_symbol (abfd, symbol, &written))
904 return false;
905 }
906 else
907 {
908 if (! coff_write_native_symbol (abfd, c_symbol, &written))
909 return false;
910 }
911 }
075caafd 912
bfe8224f 913 bfd_get_symcount (abfd) = written;
075caafd
ILT
914
915 /* Now write out strings */
916
917 if (string_size != 0)
918 {
bfe8224f 919 unsigned int size = string_size + 4;
075caafd
ILT
920 bfd_byte buffer[4];
921
bfe8224f 922 bfd_h_put_32 (abfd, size, buffer);
c16313f0
ILT
923 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
924 return false;
075caafd
ILT
925 for (p = abfd->outsymbols, i = 0;
926 i < limit;
927 i++, p++)
bfe8224f
ILT
928 {
929 asymbol *q = *p;
930 size_t name_length = strlen (q->name);
931 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
932 size_t maxlen;
933
934 /* Figure out whether the symbol name should go in the string
935 table. Symbol names that are short enough are stored
936 directly in the syment structure. File names permit a
937 different, longer, length in the syment structure. On
938 XCOFF, some symbol names are stored in the .debug section
939 rather than in the string table. */
940
941 if (c_symbol == NULL
942 || c_symbol->native == NULL)
943 {
944 /* This is not a COFF symbol, so it certainly is not a
945 file name, nor does it go in the .debug section. */
946 maxlen = SYMNMLEN;
947 }
948 else if (bfd_coff_symname_in_debug (abfd,
949 &c_symbol->native->u.syment))
950 {
951 /* This symbol name is in the XCOFF .debug section.
952 Don't write it into the string table. */
953 maxlen = name_length;
954 }
955 else if (c_symbol->native->u.syment.n_sclass == C_FILE)
956 maxlen = FILNMLEN;
957 else
958 maxlen = SYMNMLEN;
959
960 if (name_length > maxlen)
961 {
962 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
963 != name_length + 1)
964 return false;
965 }
966 }
075caafd 967 }
bfe8224f
ILT
968 else
969 {
970 /* We would normally not write anything here, but we'll write
971 out 4 so that any stupid coff reader which tries to read the
972 string table even when there isn't one won't croak. */
973 unsigned int size = 4;
974 bfd_byte buffer[4];
975
976 bfd_h_put_32 (abfd, size, buffer);
977 if (bfd_write ((PTR) buffer, 1, 4, abfd) != 4)
978 return false;
979 }
9783e04a 980
bfe8224f
ILT
981 /* Make sure the .debug section was created to be the correct size.
982 We should create it ourselves on the fly, but we don't because
983 BFD won't let us write to any section until we know how large all
984 the sections are. We could still do it by making another pass
985 over the symbols. FIXME. */
9783e04a
DM
986 BFD_ASSERT (debug_string_size == 0
987 || (debug_string_section != (asection *) NULL
988 && (BFD_ALIGN (debug_string_size,
989 1 << debug_string_section->alignment_power)
990 == bfd_section_size (abfd, debug_string_section))));
bfe8224f
ILT
991
992 return true;
075caafd
ILT
993}
994
9783e04a 995boolean
25057836
JL
996coff_write_linenumbers (abfd)
997 bfd *abfd;
075caafd
ILT
998{
999 asection *s;
1000 bfd_size_type linesz;
1001 PTR buff;
1002
1003 linesz = bfd_coff_linesz (abfd);
1004 buff = bfd_alloc (abfd, linesz);
9783e04a
DM
1005 if (!buff)
1006 {
d1ad85a6 1007 bfd_set_error (bfd_error_no_memory);
25057836 1008 return false;
9783e04a 1009 }
075caafd
ILT
1010 for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
1011 if (s->lineno_count) {
1012 asymbol **q = abfd->outsymbols;
c16313f0
ILT
1013 if (bfd_seek(abfd, s->line_filepos, SEEK_SET) != 0)
1014 return false;
075caafd
ILT
1015 /* Find all the linenumbers in this section */
1016 while (*q) {
1017 asymbol *p = *q;
27f524a3
ILT
1018 if (p->section->output_section == s) {
1019 alent *l =
1020 BFD_SEND(bfd_asymbol_bfd(p), _get_lineno, (bfd_asymbol_bfd(p), p));
1021 if (l) {
1022 /* Found a linenumber entry, output */
1023 struct internal_lineno out;
1024 memset( (PTR)&out, 0, sizeof(out));
1025 out.l_lnno = 0;
075caafd
ILT
1026 out.l_addr.l_symndx = l->u.offset;
1027 bfd_coff_swap_lineno_out(abfd, &out, buff);
c16313f0
ILT
1028 if (bfd_write(buff, 1, linesz, abfd) != linesz)
1029 return false;
075caafd 1030 l++;
27f524a3
ILT
1031 while (l->line_number) {
1032 out.l_lnno = l->line_number;
1033 out.l_addr.l_symndx = l->u.offset;
1034 bfd_coff_swap_lineno_out(abfd, &out, buff);
c16313f0
ILT
1035 if (bfd_write(buff, 1, linesz, abfd) != linesz)
1036 return false;
27f524a3
ILT
1037 l++;
1038 }
075caafd
ILT
1039 }
1040 }
1041 q++;
1042 }
1043 }
1044 }
1045 bfd_release (abfd, buff);
9783e04a 1046 return true;
075caafd
ILT
1047}
1048
330595d0 1049/*ARGSUSED*/
075caafd 1050alent *
25057836
JL
1051coff_get_lineno (ignore_abfd, symbol)
1052 bfd *ignore_abfd;
1053 asymbol *symbol;
075caafd
ILT
1054{
1055 return coffsymbol(symbol)->lineno;
1056}
1057
1058asymbol *
1059coff_section_symbol (abfd, name)
1060 bfd *abfd;
1061 char *name;
1062{
1063 asection *sec = bfd_make_section_old_way (abfd, name);
1064 asymbol *sym;
1065 combined_entry_type *csym;
1066
1067 sym = sec->symbol;
4c3721d5 1068 csym = coff_symbol_from (abfd, sym)->native;
075caafd
ILT
1069 /* Make sure back-end COFF stuff is there. */
1070 if (csym == 0)
1071 {
1072 struct foo {
1073 coff_symbol_type sym;
1074 /* @@FIXME This shouldn't use a fixed size!! */
1075 combined_entry_type e[10];
1076 };
1077 struct foo *f;
1078 f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
9783e04a
DM
1079 if (!f)
1080 {
d1ad85a6 1081 bfd_set_error (bfd_error_no_error);
9783e04a
DM
1082 return NULL;
1083 }
075caafd
ILT
1084 memset ((char *) f, 0, sizeof (*f));
1085 coff_symbol_from (abfd, sym)->native = csym = f->e;
1086 }
1087 csym[0].u.syment.n_sclass = C_STAT;
1088 csym[0].u.syment.n_numaux = 1;
1089/* SF_SET_STATICS (sym); @@ ??? */
4c3721d5
ILT
1090 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1091 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1092 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1093
1094 if (sec->output_section == NULL)
075caafd 1095 {
4c3721d5
ILT
1096 sec->output_section = sec;
1097 sec->output_offset = 0;
075caafd 1098 }
4c3721d5 1099
075caafd
ILT
1100 return sym;
1101}
1102
1103/* This function transforms the offsets into the symbol table into
1104 pointers to syments. */
1105
1106static void
25057836
JL
1107coff_pointerize_aux (abfd, table_base, type, class, auxent)
1108 bfd *abfd;
1109 combined_entry_type *table_base;
1110 int type;
1111 int class;
1112 combined_entry_type *auxent;
075caafd
ILT
1113{
1114 /* Don't bother if this is a file or a section */
1115 if (class == C_STAT && type == T_NULL) return;
1116 if (class == C_FILE) return;
1117
1118 /* Otherwise patch up */
1119#define N_TMASK coff_data (abfd)->local_n_tmask
1120#define N_BTSHFT coff_data (abfd)->local_n_btshft
1121 if (ISFCN(type) || ISTAG(class) || class == C_BLOCK) {
1122 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = table_base +
1123 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
1124 auxent->fix_end = 1;
1125 }
1126 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1127 generate one, so we must be careful to ignore it. */
1128 if (auxent->u.auxent.x_sym.x_tagndx.l > 0) {
1129 auxent->u.auxent.x_sym.x_tagndx.p =
1130 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
1131 auxent->fix_tag = 1;
1132 }
1133}
1134
1135static char *
25057836
JL
1136build_string_table (abfd)
1137 bfd *abfd;
075caafd
ILT
1138{
1139 char string_table_size_buffer[4];
1140 unsigned int string_table_size;
1141 char *string_table;
1142
1143 /* At this point we should be "seek"'d to the end of the
1144 symbols === the symbol table size. */
1145 if (bfd_read((char *) string_table_size_buffer,
1146 sizeof(string_table_size_buffer),
25057836 1147 1, abfd) != sizeof(string_table_size))
075caafd 1148 return (NULL);
075caafd
ILT
1149
1150 string_table_size = bfd_h_get_32(abfd, (bfd_byte *) string_table_size_buffer);
1151
1152 if ((string_table = (PTR) bfd_alloc(abfd, string_table_size -= 4)) == NULL) {
d1ad85a6 1153 bfd_set_error (bfd_error_no_memory);
075caafd
ILT
1154 return (NULL);
1155 } /* on mallocation error */
25057836 1156 if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size)
075caafd 1157 return (NULL);
075caafd
ILT
1158 return string_table;
1159}
1160
1161/* Allocate space for the ".debug" section, and read it.
1162 We did not read the debug section until now, because
1163 we didn't want to go to the trouble until someone needed it. */
1164
1165static char *
25057836
JL
1166build_debug_section (abfd)
1167 bfd *abfd;
075caafd
ILT
1168{
1169 char *debug_section;
1170 long position;
1171
1172 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1173
1174 if (!sect) {
d1ad85a6 1175 bfd_set_error (bfd_error_no_debug_section);
075caafd
ILT
1176 return NULL;
1177 }
1178
1179 debug_section = (PTR) bfd_alloc (abfd,
1180 bfd_get_section_size_before_reloc (sect));
1181 if (debug_section == NULL) {
d1ad85a6 1182 bfd_set_error (bfd_error_no_memory);
075caafd
ILT
1183 return NULL;
1184 }
1185
1186 /* Seek to the beginning of the `.debug' section and read it.
1187 Save the current position first; it is needed by our caller.
1188 Then read debug section and reset the file pointer. */
1189
1190 position = bfd_tell (abfd);
c16313f0
ILT
1191 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
1192 || (bfd_read (debug_section,
1193 bfd_get_section_size_before_reloc (sect), 1, abfd)
1194 != bfd_get_section_size_before_reloc(sect))
1195 || bfd_seek (abfd, position, SEEK_SET) != 0)
075caafd 1196 return NULL;
075caafd
ILT
1197 return debug_section;
1198}
1199
1200
1201/* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1202 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1203 be \0-terminated. */
1204static char *
25057836
JL
1205copy_name (abfd, name, maxlen)
1206 bfd *abfd;
1207 char *name;
1208 int maxlen;
075caafd
ILT
1209{
1210 int len;
1211 char *newname;
1212
1213 for (len = 0; len < maxlen; ++len) {
1214 if (name[len] == '\0') {
1215 break;
1216 }
1217 }
1218
1219 if ((newname = (PTR) bfd_alloc(abfd, len+1)) == NULL) {
d1ad85a6 1220 bfd_set_error (bfd_error_no_memory);
075caafd
ILT
1221 return (NULL);
1222 }
1223 strncpy(newname, name, len);
1224 newname[len] = '\0';
1225 return newname;
1226}
1227
1228/* Read a symbol table into freshly bfd_allocated memory, swap it, and
1229 knit the symbol names into a normalized form. By normalized here I
1230 mean that all symbols have an n_offset pointer that points to a null-
1231 terminated string. */
1232
1233combined_entry_type *
25057836
JL
1234coff_get_normalized_symtab (abfd)
1235 bfd *abfd;
075caafd
ILT
1236{
1237 combined_entry_type *internal;
1238 combined_entry_type *internal_ptr;
1239 combined_entry_type *symbol_ptr;
1240 combined_entry_type *internal_end;
1241 bfd_size_type symesz;
1242 PTR raw;
1243 char *raw_src;
1244 char *raw_end;
1245 char *string_table = NULL;
1246 char *debug_section = NULL;
1247 unsigned long size;
1248
1249 unsigned int raw_size;
1250 if (obj_raw_syments(abfd) != (combined_entry_type *)NULL) {
1251 return obj_raw_syments(abfd);
1252 }
1253 if ((size = bfd_get_symcount(abfd) * sizeof(combined_entry_type)) == 0) {
d1ad85a6 1254 bfd_set_error (bfd_error_no_symbols);
075caafd
ILT
1255 return (NULL);
1256 }
1257
1258 internal = (combined_entry_type *)bfd_alloc(abfd, size);
9783e04a
DM
1259 if (!internal)
1260 {
d1ad85a6 1261 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1262 return NULL;
1263 }
075caafd
ILT
1264 internal_end = internal + bfd_get_symcount(abfd);
1265
1266 symesz = bfd_coff_symesz (abfd);
1267 raw_size = bfd_get_symcount(abfd) * symesz;
1268 raw = bfd_alloc(abfd,raw_size);
9783e04a
DM
1269 if (!raw)
1270 {
d1ad85a6 1271 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1272 return NULL;
1273 }
075caafd
ILT
1274
1275 if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1
25057836 1276 || bfd_read(raw, raw_size, 1, abfd) != raw_size)
075caafd 1277 return (NULL);
075caafd
ILT
1278 /* mark the end of the symbols */
1279 raw_end = (char *) raw + bfd_get_symcount(abfd) * symesz;
1280 /*
1281 FIXME SOMEDAY. A string table size of zero is very weird, but
1282 probably possible. If one shows up, it will probably kill us.
1283 */
1284
1285 /* Swap all the raw entries */
1286 for (raw_src = (char *) raw, internal_ptr = internal;
1287 raw_src < raw_end;
1288 raw_src += symesz, internal_ptr++) {
1289
1290 unsigned int i;
1291 bfd_coff_swap_sym_in(abfd, (PTR)raw_src, (PTR)&internal_ptr->u.syment);
9783e04a 1292 internal_ptr->fix_value = 0;
075caafd
ILT
1293 internal_ptr->fix_tag = 0;
1294 internal_ptr->fix_end = 0;
9783e04a 1295 internal_ptr->fix_scnlen = 0;
075caafd 1296 symbol_ptr = internal_ptr;
85fe7cff 1297
075caafd
ILT
1298 for (i = 0;
1299 i < symbol_ptr->u.syment.n_numaux;
1300 i++)
1301 {
1302 internal_ptr++;
1303 raw_src += symesz;
1304
9783e04a 1305 internal_ptr->fix_value = 0;
075caafd
ILT
1306 internal_ptr->fix_tag = 0;
1307 internal_ptr->fix_end = 0;
9783e04a 1308 internal_ptr->fix_scnlen = 0;
075caafd
ILT
1309 bfd_coff_swap_aux_in(abfd, (PTR) raw_src,
1310 symbol_ptr->u.syment.n_type,
1311 symbol_ptr->u.syment.n_sclass,
330595d0 1312 i, symbol_ptr->u.syment.n_numaux,
075caafd
ILT
1313 &(internal_ptr->u.auxent));
1314 /* Remember that bal entries arn't pointerized */
1315 if (i != 1 || symbol_ptr->u.syment.n_sclass != C_LEAFPROC)
1316 {
1317
1318 coff_pointerize_aux(abfd,
1319 internal,
1320 symbol_ptr->u.syment.n_type,
1321 symbol_ptr->u.syment.n_sclass,
1322 internal_ptr);
1323 }
1324
1325 }
1326 }
1327
1328 /* Free all the raw stuff */
1329 bfd_release(abfd, raw);
1330
1331 for (internal_ptr = internal; internal_ptr < internal_end;
1332 internal_ptr ++)
1333 {
1334 if (internal_ptr->u.syment.n_sclass == C_FILE) {
1335 /* make a file symbol point to the name in the auxent, since
1336 the text ".file" is redundant */
1337 if ((internal_ptr+1)->u.auxent.x_file.x_n.x_zeroes == 0) {
1338 /* the filename is a long one, point into the string table */
1339 if (string_table == NULL) {
1340 string_table = build_string_table(abfd);
1341 }
1342
1343 internal_ptr->u.syment._n._n_n._n_offset =
e4b6b3e7 1344 (long) (string_table - 4 +
075caafd
ILT
1345 (internal_ptr+1)->u.auxent.x_file.x_n.x_offset);
1346 }
1347 else {
1348 /* ordinary short filename, put into memory anyway */
e4b6b3e7 1349 internal_ptr->u.syment._n._n_n._n_offset = (long)
075caafd
ILT
1350 copy_name(abfd, (internal_ptr+1)->u.auxent.x_file.x_fname,
1351 FILNMLEN);
1352 }
1353 }
1354 else {
1355 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) {
1356 /* This is a "short" name. Make it long. */
1357 unsigned long i = 0;
1358 char *newstring = NULL;
1359
1360 /* find the length of this string without walking into memory
1361 that isn't ours. */
1362 for (i = 0; i < 8; ++i) {
1363 if (internal_ptr->u.syment._n._n_name[i] == '\0') {
1364 break;
1365 } /* if end of string */
1366 } /* possible lengths of this string. */
1367
1368 if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) {
d1ad85a6 1369 bfd_set_error (bfd_error_no_memory);
075caafd
ILT
1370 return (NULL);
1371 } /* on error */
1372 memset(newstring, 0, i);
1373 strncpy(newstring, internal_ptr->u.syment._n._n_name, i-1);
e4b6b3e7 1374 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
075caafd
ILT
1375 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1376 }
9783e04a
DM
1377 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1378 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
075caafd
ILT
1379 else if (!bfd_coff_symname_in_debug(abfd, &internal_ptr->u.syment)) {
1380 /* Long name already. Point symbol at the string in the table. */
1381 if (string_table == NULL) {
1382 string_table = build_string_table(abfd);
1383 }
e4b6b3e7 1384 internal_ptr->u.syment._n._n_n._n_offset = (long int)
075caafd
ILT
1385 (string_table - 4 + internal_ptr->u.syment._n._n_n._n_offset);
1386 }
1387 else {
1388 /* Long name in debug section. Very similar. */
1389 if (debug_section == NULL) {
1390 debug_section = build_debug_section(abfd);
1391 }
e4b6b3e7 1392 internal_ptr->u.syment._n._n_n._n_offset = (long int)
075caafd
ILT
1393 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1394 }
1395 }
1396 internal_ptr += internal_ptr->u.syment.n_numaux;
1397 }
1398
1399 obj_raw_syments(abfd) = internal;
85fe7cff 1400 obj_raw_syment_count(abfd) = internal_ptr - internal;
075caafd
ILT
1401
1402 return (internal);
1403} /* coff_get_normalized_symtab() */
1404
326e32d7 1405long
25057836
JL
1406coff_get_reloc_upper_bound (abfd, asect)
1407 bfd *abfd;
1408 sec_ptr asect;
075caafd
ILT
1409{
1410 if (bfd_get_format(abfd) != bfd_object) {
d1ad85a6 1411 bfd_set_error (bfd_error_invalid_operation);
326e32d7 1412 return -1;
075caafd
ILT
1413 }
1414 return (asect->reloc_count + 1) * sizeof(arelent *);
1415}
1416
1417asymbol *
25057836
JL
1418coff_make_empty_symbol (abfd)
1419 bfd *abfd;
075caafd
ILT
1420{
1421 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1422 if (new == NULL) {
d1ad85a6 1423 bfd_set_error (bfd_error_no_memory);
075caafd
ILT
1424 return (NULL);
1425 } /* on error */
9783e04a 1426 memset (new, 0, sizeof *new);
075caafd
ILT
1427 new->symbol.section = 0;
1428 new->native = 0;
1429 new->lineno = (alent *) NULL;
1430 new->done_lineno = false;
1431 new->symbol.the_bfd = abfd;
1432 return &new->symbol;
1433}
1434
2d1e6c9c
KR
1435/* Make a debugging symbol. */
1436
075caafd 1437asymbol *
2d1e6c9c
KR
1438coff_bfd_make_debug_symbol (abfd, ptr, sz)
1439 bfd *abfd;
1440 PTR ptr;
1441 unsigned long sz;
075caafd
ILT
1442{
1443 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc(abfd, sizeof(coff_symbol_type));
1444 if (new == NULL) {
d1ad85a6 1445 bfd_set_error (bfd_error_no_memory);
075caafd
ILT
1446 return (NULL);
1447 } /* on error */
1448 /* @@ This shouldn't be using a constant multiplier. */
1449 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
9783e04a
DM
1450 if (!new->native)
1451 {
d1ad85a6 1452 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1453 return (NULL);
1454 } /* on error */
075caafd
ILT
1455 new->symbol.section = &bfd_debug_section;
1456 new->lineno = (alent *) NULL;
1457 new->done_lineno = false;
1458 new->symbol.the_bfd = abfd;
1459 return &new->symbol;
1460}
1461
330595d0 1462/*ARGSUSED*/
2d1e6c9c
KR
1463void
1464coff_get_symbol_info (abfd, symbol, ret)
1465 bfd *abfd;
1466 asymbol *symbol;
1467 symbol_info *ret;
1468{
1469 bfd_symbol_info (symbol, ret);
1470}
1471
e61cfdf8
ILT
1472/* Print out information about COFF symbol. */
1473
075caafd 1474void
e61cfdf8
ILT
1475coff_print_symbol (abfd, filep, symbol, how)
1476 bfd *abfd;
1477 PTR filep;
1478 asymbol *symbol;
1479 bfd_print_symbol_type how;
075caafd 1480{
e61cfdf8
ILT
1481 FILE *file = (FILE *) filep;
1482
1483 switch (how)
1484 {
075caafd 1485 case bfd_print_symbol_name:
e61cfdf8 1486 fprintf (file, "%s", symbol->name);
075caafd 1487 break;
e61cfdf8 1488
075caafd 1489 case bfd_print_symbol_more:
e61cfdf8
ILT
1490 fprintf (file, "coff %s %s",
1491 coffsymbol(symbol)->native ? "n" : "g",
1492 coffsymbol(symbol)->lineno ? "l" : " ");
075caafd 1493 break;
075caafd 1494
e61cfdf8 1495 case bfd_print_symbol_all:
075caafd 1496 if (coffsymbol(symbol)->native)
075caafd 1497 {
e61cfdf8
ILT
1498 unsigned int aux;
1499 combined_entry_type *combined = coffsymbol (symbol)->native;
1500 combined_entry_type *root = obj_raw_syments (abfd);
1501 struct lineno_cache_entry *l = coffsymbol(symbol)->lineno;
1502
1503 fprintf (file,"[%3d]", combined - root);
1504
1505 fprintf (file,
4c3721d5 1506 "(sc %2d)(fl 0x%02x)(ty %3x)(sc %3d) (nx %d) 0x%08lx %s",
e61cfdf8
ILT
1507 combined->u.syment.n_scnum,
1508 combined->u.syment.n_flags,
1509 combined->u.syment.n_type,
1510 combined->u.syment.n_sclass,
1511 combined->u.syment.n_numaux,
4c3721d5 1512 (unsigned long) combined->u.syment.n_value,
e61cfdf8
ILT
1513 symbol->name);
1514
1515 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
1516 {
1517 combined_entry_type *auxp = combined + aux + 1;
1518 long tagndx;
1519
1520 if (auxp->fix_tag)
1521 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
1522 else
1523 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
1524
1525 fprintf (file, "\n");
1526 switch (combined->u.syment.n_sclass)
1527 {
1528 case C_FILE:
1529 fprintf (file, "File ");
1530 break;
1531 default:
1532
4c3721d5 1533 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
e61cfdf8
ILT
1534 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
1535 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
1536 tagndx);
1537 break;
1538 }
075caafd 1539 }
075caafd 1540
e61cfdf8
ILT
1541 if (l)
1542 {
e4b6b3e7 1543 fprintf (file, "\n%s :", l->u.sym->name);
e61cfdf8
ILT
1544 l++;
1545 while (l->line_number)
1546 {
4c3721d5
ILT
1547 fprintf (file, "\n%4d : 0x%lx",
1548 l->line_number,
1549 ((unsigned long)
1550 (l->u.offset + symbol->section->vma)));
e61cfdf8
ILT
1551 l++;
1552 }
1553 }
1554 }
1555 else
075caafd 1556 {
e61cfdf8
ILT
1557 bfd_print_symbol_vandf ((PTR) file, symbol);
1558 fprintf (file, " %-5s %s %s %s",
1559 symbol->section->name,
1560 coffsymbol(symbol)->native ? "n" : "g",
1561 coffsymbol(symbol)->lineno ? "l" : " ",
1562 symbol->name);
075caafd 1563 }
075caafd
ILT
1564 }
1565}
1566
1567/* Provided a BFD, a section and an offset into the section, calculate
1568 and return the name of the source file and the line nearest to the
1569 wanted location. */
1570
330595d0 1571/*ARGSUSED*/
075caafd 1572boolean
25057836
JL
1573coff_find_nearest_line (abfd, section, ignore_symbols, offset, filename_ptr,
1574 functionname_ptr, line_ptr)
1575 bfd *abfd;
1576 asection *section;
1577 asymbol **ignore_symbols;
1578 bfd_vma offset;
1579 CONST char **filename_ptr;
1580 CONST char **functionname_ptr;
1581 unsigned int *line_ptr;
075caafd
ILT
1582{
1583 static bfd *cache_abfd;
1584 static asection *cache_section;
1585 static bfd_vma cache_offset;
1586 static unsigned int cache_i;
85fe7cff
PB
1587 static CONST char *cache_function;
1588 static unsigned int line_base = 0;
075caafd
ILT
1589
1590 unsigned int i = 0;
1591 coff_data_type *cof = coff_data(abfd);
1592 /* Run through the raw syments if available */
1593 combined_entry_type *p;
1594 alent *l;
075caafd
ILT
1595
1596
1597 *filename_ptr = 0;
1598 *functionname_ptr = 0;
1599 *line_ptr = 0;
1600
1601 /* Don't try and find line numbers in a non coff file */
1602 if (abfd->xvec->flavour != bfd_target_coff_flavour)
1603 return false;
1604
1605 if (cof == NULL)
1606 return false;
1607
1608 p = cof->raw_syments;
1609
1610 for (i = 0; i < cof->raw_syment_count; i++) {
1611 if (p->u.syment.n_sclass == C_FILE) {
1612 /* File name has been moved into symbol */
1613 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
1614 break;
1615 }
1616 p += 1 + p->u.syment.n_numaux;
1617 }
1618 /* Now wander though the raw linenumbers of the section */
1619 /*
1620 If this is the same BFD as we were previously called with and this is
1621 the same section, and the offset we want is further down then we can
1622 prime the lookup loop
1623 */
1624 if (abfd == cache_abfd &&
1625 section == cache_section &&
1626 offset >= cache_offset) {
1627 i = cache_i;
85fe7cff 1628 *functionname_ptr = cache_function;
075caafd
ILT
1629 }
1630 else {
1631 i = 0;
075caafd 1632 }
85fe7cff 1633 l = &section->lineno[i];
075caafd
ILT
1634
1635 for (; i < section->lineno_count; i++) {
1636 if (l->line_number == 0) {
1637 /* Get the symbol this line number points at */
1638 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
85fe7cff
PB
1639 if (coff->symbol.value > offset)
1640 break;
075caafd
ILT
1641 *functionname_ptr = coff->symbol.name;
1642 if (coff->native) {
1643 combined_entry_type *s = coff->native;
1644 s = s + 1 + s->u.syment.n_numaux;
1645 /*
1646 S should now point to the .bf of the function
1647 */
1648 if (s->u.syment.n_numaux) {
1649 /*
1650 The linenumber is stored in the auxent
1651 */
1652 union internal_auxent *a = &((s + 1)->u.auxent);
1653 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
85fe7cff 1654 *line_ptr = line_base;
075caafd
ILT
1655 }
1656 }
1657 }
1658 else {
1659 if (l->u.offset > offset)
1660 break;
85fe7cff 1661 *line_ptr = l->line_number + line_base - 1;
075caafd
ILT
1662 }
1663 l++;
1664 }
1665
1666 cache_abfd = abfd;
1667 cache_section = section;
1668 cache_offset = offset;
1669 cache_i = i;
85fe7cff 1670 cache_function = *functionname_ptr;
075caafd
ILT
1671
1672 return true;
1673}
1674
1675int
25057836
JL
1676coff_sizeof_headers (abfd, reloc)
1677 bfd *abfd;
1678 boolean reloc;
075caafd
ILT
1679{
1680 size_t size;
1681
1682 if (reloc == false) {
1683 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
1684 }
1685 else {
1686 size = bfd_coff_filhsz (abfd);
1687 }
1688
1689 size += abfd->section_count * bfd_coff_scnhsz (abfd);
1690 return size;
1691}
This page took 0.404671 seconds and 4 git commands to generate.