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