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