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