*** empty log message ***
[binutils-gdb.git] / bfd / coff-code.h
1 /*
2 Copyright (C) 1990, 1991 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Diddler.
5
6 BFD is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 1, or (at your option) any later version.
9
10 BFD is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 details.
14
15 You should have received a copy of the GNU General Public License along with
16 BFD; see the file COPYING. If not, write to the Free Software Foundation,
17 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 /* $Id$ */
21 /*
22 Most of this hacked by Steve Chamberlain, steve@cygnus.com
23 */
24
25 #include "archures.h" /* Machine architectures and types */
26
27 /* SUPPRESS 558 */
28 /* SUPPRESS 590 */
29 /* SUPPRESS 529 */
30 /* SUPPRESS 530 */
31
32 #define ALIGN(this, boundary) \
33 ((( (this) + ((boundary) -1)) & (~((boundary)-1))))
34
35 #define sp(x) bfd_h_put_x(abfd, x, &x)
36
37
38
39 /*
40 Align an address by rounding it up to a power of two. It leaves the
41 address unchanged if align == 0 (2^0 = alignment of 1 byte)
42 */
43 #define i960_align(addr, align) \
44 ( ((addr) + ((1<<(align))-1)) & (-1 << (align)))
45
46
47 /* All the swapping routines */
48
49 static void
50 swap_reloc(abfd, reloc)
51 bfd *abfd;
52 RELOC *reloc;
53 {
54 sp(reloc->r_vaddr);
55 sp(reloc->r_symndx);
56 sp(reloc->r_type);
57 }
58
59
60 static void
61 swap_filehdr(abfd, filehdr)
62 bfd *abfd;
63 FILHDR *filehdr;
64 {
65 sp(filehdr->f_magic);
66 sp(filehdr->f_nscns);
67 sp(filehdr->f_timdat);
68 sp(filehdr->f_symptr);
69 sp(filehdr->f_nsyms);
70 sp(filehdr->f_opthdr);
71 sp(filehdr->f_flags);
72
73
74 }
75
76 static void
77 swap_aouthdr(abfd, aouthdr)
78 bfd *abfd;
79 AOUTHDR *aouthdr;
80 {
81
82 sp(aouthdr->magic);
83 sp(aouthdr->vstamp);
84 sp(aouthdr->tsize);
85 sp(aouthdr->dsize);
86 sp(aouthdr->bsize);
87 sp(aouthdr->entry);
88 sp(aouthdr->text_start);
89 sp(aouthdr->data_start);
90 #ifdef I960
91 sp(aouthdr->tagentries);
92 #endif
93 }
94
95 static void
96 swap_scnhdr(abfd, scnhdr)
97 bfd *abfd;
98 SCNHDR *scnhdr;
99 {
100 sp(scnhdr->s_vaddr);
101 sp(scnhdr->s_paddr);
102 sp(scnhdr->s_size);
103 sp(scnhdr->s_scnptr);
104 sp(scnhdr->s_relptr);
105 sp(scnhdr->s_lnnoptr);
106 sp(scnhdr->s_nreloc);
107 sp(scnhdr->s_nlnno);
108 sp(scnhdr->s_flags);
109 #ifdef I960
110 sp(scnhdr->s_align);
111 #endif
112 }
113
114 static void
115 swap_name(abfd, ptr)
116 bfd *abfd;
117 long *ptr;
118 {
119 if (ptr[0] == 0) {
120 /* There is an index which needs to be swapped */
121 bfd_h_put_x(abfd, ptr[1], (ptr + 1));
122 }
123 else {
124 /* This is a string .. leave it alone */
125 }
126 }
127
128 static void
129 bfd_coff_swap_sym(abfd, se)
130 bfd *abfd;
131 SYMENT *se;
132 {
133 swap_name(abfd, se->n_name);
134 bfd_h_put_x(abfd, se->n_value, &se->n_value);
135 bfd_h_put_x(abfd, se->n_scnum, &se->n_scnum);
136 bfd_h_put_x(abfd, se->n_type, &se->n_type);
137 bfd_h_put_x(abfd, se->n_sclass, &se->n_sclass);
138 bfd_h_put_x(abfd, se->n_numaux, &se->n_numaux);
139 }
140
141 static void
142 bfd_coff_swap_aux(abfd, au, type, class)
143 bfd *abfd;
144 AUXENT *au;
145 int type;
146 int class;
147 {
148 switch (class) {
149 case C_FILE:
150 swap_name(abfd, &au->x_file.x_n);
151 break;
152 case C_STAT:
153 #ifdef C_LEAFSTAT
154 case C_LEAFSTAT:
155 #endif
156 case C_HIDDEN:
157 if (type == T_NULL) {
158 sp(au->x_scn.x_scnlen);
159 sp(au->x_scn.x_nreloc);
160 sp(au->x_scn.x_nlinno);
161 break;
162 }
163 default:
164 sp(au->x_sym.x_tagndx);
165 sp(au->x_sym.x_tvndx);
166
167 if (ISARY(type)) {
168 sp(au->x_sym.x_fcnary.x_ary.x_dimen[0]);
169 sp(au->x_sym.x_fcnary.x_ary.x_dimen[1]);
170 sp(au->x_sym.x_fcnary.x_ary.x_dimen[2]);
171 sp(au->x_sym.x_fcnary.x_ary.x_dimen[3]);
172 }
173 else {
174 sp(au->x_sym.x_fcnary.x_fcn.x_lnnoptr);
175 sp(au->x_sym.x_fcnary.x_fcn.x_endndx);
176 }
177 if (ISFCN(type)) {
178 sp(au->x_sym.x_misc.x_fsize);
179 }
180 else {
181 sp(au->x_sym.x_misc.x_lnsz.x_lnno);
182 sp(au->x_sym.x_misc.x_lnsz.x_size);
183 }
184 }
185 }
186
187 static void
188 bfd_coff_swap_lineno(abfd, lineno)
189 bfd *abfd;
190 LINENO *lineno;
191 {
192 sp(lineno->l_addr.l_symndx);
193 sp(lineno->l_lnno);
194 }
195
196
197 /* void warning(); */
198 extern asection abs_section;
199
200
201
202 static int
203 get_index(symbol)
204 asymbol *symbol;
205 {
206 return (int) symbol->value;
207 }
208
209 static void
210 set_index(symbol, idx)
211 asymbol *symbol;
212 unsigned int idx;
213 {
214 symbol->value = idx;
215 }
216
217 /*
218 initialize a section structure with information peculiar to this
219 particular implementation of coff
220 */
221
222 static boolean
223 coff_new_section_hook(abfd_ignore, section_ignore)
224 bfd *abfd_ignore;
225 asection *section_ignore;
226 {
227 return true;
228 }
229 /* actually it makes itself and its children from the file headers */
230 static boolean
231 make_a_section_from_file(abfd, hdr)
232 bfd *abfd;
233 struct scnhdr *hdr;
234
235 {
236 asection *return_section;
237 {
238 char *name = malloc(9);
239 if (name == NULL) {
240 bfd_error = no_memory;
241 return (BFD_FAILURE);
242 } /* on error */
243 strncpy(name, (char *) &hdr->s_name[0], 8);
244
245 return_section = bfd_make_section(abfd, name);
246 (return_section->name)[8] = 0;
247 }
248
249 /* s_paddr is presumed to be = to s_vaddr */
250 #define assign(to, from) return_section->to = hdr->from
251 assign(vma, s_vaddr);
252 /* assign (vma, s_vaddr); */
253 assign(size, s_size);
254 assign(filepos, s_scnptr);
255 assign(rel_filepos, s_relptr);
256 assign(reloc_count, s_nreloc);
257 #ifdef I960
258 {
259
260 assign(alignment_power, s_align);
261 {
262 unsigned int i;
263 for (i = 0; i < 32; i++) {
264 if ((1 << i) >= (int) (return_section->alignment_power)) {
265 return_section->alignment_power = i;
266 break;
267 }
268 }
269 }
270 }
271 #endif
272 assign(line_filepos, s_lnnoptr);
273 /*
274 return_section->linesize = hdr->s_nlnno * sizeof (struct lineno);
275 */
276
277 #undef assign
278 return_section->lineno_count = hdr->s_nlnno;
279 return_section->userdata = NULL;
280 return_section->next = (asection *) NULL;
281 if ((hdr->s_flags & STYP_TEXT) || (hdr->s_flags & STYP_DATA))
282 return_section->flags = (SEC_LOAD | SEC_ALLOC);
283 else if (hdr->s_flags & STYP_BSS)
284 return_section->flags = SEC_ALLOC;
285
286 if (hdr->s_nreloc != 0)
287 return_section->flags |= SEC_RELOC;
288 if (hdr->s_scnptr != 0)
289 return_section->flags |= SEC_HAS_CONTENTS;
290 return true;
291 }
292
293
294 static
295 bfd_target *
296 coff_real_object_p(abfd, nscns, opthdr)
297 bfd *abfd;
298 unsigned nscns,
299 opthdr;
300 {
301 struct icofdata *tdata;
302 char *file_info; /* buffer for all the headers */
303 size_t readsize; /* length of file_info */
304 struct filehdr *filehdr; /* points into file_info */
305 struct scnhdr *sections; /* points into file_info */
306 /*
307 OK, now we know the format, read in the filehdr, soi-disant "optional
308 header", and all the sections.
309 */
310 readsize = sizeof(struct filehdr)
311 + opthdr
312 + (nscns * sizeof(struct scnhdr));
313
314 file_info = malloc(readsize);
315 if (file_info == NULL) {
316 bfd_error = no_memory;
317 return 0;
318 }
319 if (bfd_seek(abfd, 0L, SEEK_SET) < 0)
320 return 0;
321 if (bfd_read((void *) file_info, 1, readsize, abfd) != readsize)
322 return 0;
323 filehdr = (struct filehdr *) file_info;
324 sections = (struct scnhdr *) (file_info + sizeof(struct filehdr) + opthdr);
325
326
327 swap_filehdr(abfd, filehdr);
328
329 /* Now copy data as required; construct all asections etc */
330 tdata = (struct icofdata *) malloc(sizeof(struct icofdata) +
331 sizeof(AOUTHDR));
332 if (tdata == NULL) {
333 bfd_error = no_memory;
334 return 0;
335 }
336 tdata->symbol_index_slew = 0;
337 tdata->relocbase =0;
338 tdata->raw_syment_count = 0;
339 tdata->raw_linenos = 0;
340 tdata->raw_syments = 0;
341 tdata->sym_filepos =0;
342 tdata->flags = filehdr->f_flags;
343 if (nscns != 0) {
344 unsigned int i;
345 for (i = 0; i < nscns; i++) {
346 swap_scnhdr(abfd, sections + i);
347 make_a_section_from_file(abfd, sections + i);
348 }
349 }
350 /* Determine the machine architecture and type. */
351 abfd->obj_machine = 0;
352 switch (filehdr->f_magic) {
353 #ifdef MC68MAGIC
354 case MC68MAGIC:
355 case MC68DMAGIC:
356 abfd->obj_arch = bfd_arch_m68k;
357 abfd->obj_machine = 68020;
358 break;
359 #endif
360 #ifdef MC88MAGIC
361 case MC88MAGIC:
362 case MC88DMAGIC:
363 abfd->obj_arch = bfd_arch_m88k;
364 abfd->obj_machine = 88100;
365 break;
366 #endif
367 #ifdef I960ROMAGIC
368 case I960ROMAGIC:
369 case I960RWMAGIC:
370 abfd->obj_arch = bfd_arch_i960;
371 switch (F_I960TYPE & filehdr->f_flags)
372 {
373 default:
374 case F_I960CORE:
375 abfd->obj_machine = bfd_mach_i960_core;
376 break;
377 case F_I960KB:
378 abfd->obj_machine = bfd_mach_i960_kb_sb;
379 break;
380 case F_I960MC:
381 abfd->obj_machine = bfd_mach_i960_mc;
382 break;
383 case F_I960XA:
384 abfd->obj_machine = bfd_mach_i960_xa;
385 break;
386 case F_I960CA:
387 abfd->obj_machine = bfd_mach_i960_ca;
388 break;
389 case F_I960KA:
390 abfd->obj_machine = bfd_mach_i960_ka_sa;
391 break;
392
393 }
394 break;
395 #endif
396
397 default: /* Unreadable input file type */
398 abfd->obj_arch = bfd_arch_obscure;
399 break;
400 }
401
402 if (!(filehdr->f_flags & F_RELFLG))
403 abfd->flags |= HAS_RELOC;
404 if ((filehdr->f_flags & F_EXEC))
405 abfd->flags |= EXEC_P;
406 if (!(filehdr->f_flags & F_LNNO))
407 abfd->flags |= HAS_LINENO;
408 if (!(filehdr->f_flags & F_LSYMS))
409 abfd->flags |= HAS_LOCALS;
410
411 abfd->tdata = (void *) tdata;
412 bfd_get_symcount(abfd) = filehdr->f_nsyms;
413 if (filehdr->f_nsyms)
414 abfd->flags |= HAS_SYMS;
415
416 tdata->sym_filepos = filehdr->f_symptr;
417 tdata->hdr = (struct aouthdr *) (file_info + sizeof(struct filehdr));
418
419 swap_aouthdr(abfd, tdata->hdr);
420
421 tdata->symbols = (coff_symbol_type *) NULL;
422 bfd_get_start_address(abfd) = opthdr ? exec_hdr(abfd)->entry : 0;
423
424 return abfd->xvec;
425 }
426
427
428 /*
429 Takes a bfd and a symbol, returns a pointer to the coff specific area
430 of the symbol if there is one.
431 */
432 static coff_symbol_type *
433 coff_symbol_from(abfd, symbol)
434 bfd *abfd;
435 asymbol *symbol;
436 {
437 if (symbol->the_bfd->xvec->flavour != bfd_target_coff_flavour_enum)
438 return (coff_symbol_type *)NULL;
439
440 if (symbol->the_bfd->tdata == (void *)NULL)
441 return (coff_symbol_type *)NULL;
442
443 return (coff_symbol_type *) symbol;
444 }
445
446
447
448 static bfd_target *
449 coff_object_p(abfd)
450 bfd *abfd;
451 {
452 unsigned short magic,
453 nscns,
454 opthdr;
455 bfd_error = system_call_error;
456
457 /* figure out how much to read */
458 if (bfd_read((void *) &magic, 1, sizeof(magic), abfd) != sizeof(magic))
459 return 0;
460
461 magic = bfd_h_getshort(abfd, (bfd_byte *) (&magic));
462
463 if (BADMAG(*((struct filehdr *) & magic))) {
464 bfd_error = wrong_format;
465 return 0;
466 }
467 if (bfd_read((void *) &nscns, 1, sizeof(nscns), abfd) != sizeof(nscns))
468 return 0;
469 nscns = bfd_h_getshort(abfd, (unsigned char *) &nscns);
470 if (bfd_seek(abfd, (file_ptr) & (((struct filehdr *) NULL)->f_opthdr), SEEK_SET)
471 < 0)
472 return (bfd_target *) NULL;
473 if (bfd_read((void *) &opthdr, 1, sizeof(opthdr), abfd) != sizeof(opthdr))
474 return (bfd_target *) NULL;
475 opthdr = bfd_h_getshort(abfd, (unsigned char *) &opthdr);
476
477 return coff_real_object_p(abfd, nscns, opthdr);
478 }
479
480 static boolean
481 coff_mkobject(abfd)
482 bfd *abfd;
483 {
484 char *rawptr;
485
486 bfd_error = system_call_error;
487
488 /* Use an intermediate variable for clarity */
489 rawptr = zalloc(sizeof(struct icofdata) + sizeof(AOUTHDR));
490 if (rawptr == NULL) {
491 bfd_error = no_memory;
492 return false;
493 }
494 abfd->tdata = (void *) ((struct icofdata *) rawptr);
495 exec_hdr(abfd) = (AOUTHDR *) (rawptr + sizeof(struct icofdata));
496 obj_relocbase(abfd) =0;
497 return true;
498 }
499
500
501
502 static void
503 coff_count_linenumbers(abfd)
504 bfd *abfd;
505 {
506 unsigned int limit = bfd_get_symcount(abfd);
507 unsigned int i;
508 asymbol **p;
509 {
510 asection *s = abfd->sections->output_section;
511 while (s) {
512 BFD_ASSERT(s->lineno_count == 0);
513 s = s->next;
514 }
515 }
516
517
518 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) {
519 asymbol *q_maybe = *p;
520 if (q_maybe->the_bfd->xvec->flavour == bfd_target_coff_flavour_enum) {
521 coff_symbol_type *q = coffsymbol(q_maybe);
522 if (q->lineno) {
523 /*
524 This symbol has a linenumber, increment the owning
525 section's linenumber count
526 */
527 alent *l = q->lineno;
528 q->symbol.section->output_section->lineno_count++;
529 l++;
530 while (l->line_number) {
531 q->symbol.section->output_section->lineno_count++;
532 l++;
533 }
534 }
535 }
536 }
537 }
538
539 /*
540 This function returns true if the supplied SYMENT has an AUXENT with
541 a tagndx field which should be relocated.
542
543 The coff book says that all auxents have this and should be moved,
544 but all the actual implementations I've looked at do this ..
545 (sac@cygnus.com)
546
547 */
548 static boolean
549 uses_x_sym_x_tagndx_p(native)
550 SYMENT *native;
551 {
552 if (BTYPE(native->n_type) == T_STRUCT) return true;
553 if (BTYPE(native->n_type) == T_UNION) return true;
554 if (BTYPE(native->n_type) == T_ENUM)return true;
555 return false;
556 }
557
558
559
560
561
562
563 /*
564 This procedure runs through the native entries in a coff symbol table
565 and links up all the elements which should point to one another, in
566 particular these are:
567
568 strtag, entag and untags have an auxent endindex which points to the
569 first syment after the .eos. This is simple to do, we just keep a
570 pointer to the symbol with the most recent pending strtag and patch it
571 when we see the eos. This works since coff structs are never nested.
572
573 ISFCN type entries have an endindex which points to the next static or
574 extern in the table, thereby skipping the function contents.
575 The coff book says that an ISFCN's tagindex
576 points to the first .bf for the function, so far I havn't seen it
577 used. We do this using the same mechanism as strtags.
578
579 Each file entry has a value which points to the next file entry,
580 the last file entry points to the first extern symbol in the table
581 which is not an ISFCN.
582
583 Each .bb entry points to the matching .eb entry, but these are nested
584 so we keep a stack of them.
585
586 The tagndx of .eos items points to the strtag attached to them, this
587 is simply the last_tagndx again.
588
589 The tagndx of items with type strtag point to the defining struct.
590 This bit is complicated; We know that a struct ref and def must be
591 within the same file, so all the natives will be in the same vector.
592 This means that we can subtracts two pointers and get the index
593 differences between to items, used to work out the true index of the
594 target.
595
596 We store in the name field of each syment the actual native index
597 applied so we can dig it out through a pointer.
598
599
600 */
601 static void
602 coff_mangle_symbols(bfd_ptr)
603 bfd *bfd_ptr;
604 {
605 unsigned int symbol_count = bfd_get_symcount(bfd_ptr);
606 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
607 SYMENT *last_tagndx = (SYMENT *)NULL;
608 SYMENT *last_file = (SYMENT *)NULL;
609 SYMENT *last_fcn = (SYMENT *)NULL;
610 SYMENT *block_stack[50];
611 SYMENT **last_block = &block_stack[0];
612 boolean first_time = true;
613 unsigned int symbol_index;
614 unsigned int native_index = 0;
615
616 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) {
617 coff_symbol_type *coff_symbol_ptr =
618 coff_symbol_from(bfd_ptr, symbol_ptr_ptr[symbol_index]);
619 if (coff_symbol_ptr == (coff_symbol_type *)NULL) {
620 /*
621 This symbol has no coff information in it, it will take up
622 only one slot in the output symbol table
623 */
624 native_index++;
625 }
626 else {
627 SYMENT *syment = coff_symbol_ptr->native;
628 if (syment == (SYMENT *)NULL) {
629 native_index++;
630 }
631 else {
632 /* Normalize the symbol flags */
633 if (coff_symbol_ptr->symbol.flags & BSF_FORT_COMM) {
634 /* a common symbol is undefined with a value */
635 syment->n_scnum = N_UNDEF;
636 syment->n_value = coff_symbol_ptr->symbol.value;
637 }
638 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) {
639 syment->n_value = coff_symbol_ptr->symbol.value;
640 }
641 else if (coff_symbol_ptr->symbol.flags & BSF_UNDEFINED) {
642 syment->n_scnum = N_UNDEF;
643 syment->n_value = 0;
644 }
645 else if (coff_symbol_ptr->symbol.flags & BSF_ABSOLUTE) {
646 syment->n_scnum = N_ABS;
647 syment->n_value = coff_symbol_ptr->symbol.value;
648 }
649 else {
650 syment->n_scnum =
651 coff_symbol_ptr->symbol.section->output_section->index+1;
652 syment->n_value =
653 coff_symbol_ptr->symbol.value +
654 coff_symbol_ptr->symbol.section->output_offset +
655 coff_symbol_ptr->symbol.section->output_section->vma;
656 }
657
658
659 /* If this symbol ties up something then do it */
660
661 if (syment->n_sclass == C_FILE && last_file != (SYMENT *)NULL)
662 {
663 last_file->n_value = native_index;
664 }
665 else if ((syment->n_sclass == C_EXT
666 || syment->n_sclass == C_STAT
667 #ifdef C_LEAFEXT
668 || syment->n_sclass == C_LEAFEXT
669 || syment->n_sclass == C_LEAFSTAT
670 #endif
671 )
672 && last_fcn != (SYMENT *)NULL)
673 {
674 AUXENT *auxent = (AUXENT *)(last_fcn+1);
675 auxent->x_sym.x_fcnary.x_fcn.x_endndx = native_index;
676 last_fcn = (SYMENT *)NULL;
677
678 }
679 else if (syment->n_sclass == C_EOS && last_tagndx != (SYMENT*)NULL)
680 {
681 AUXENT *auxent = (AUXENT *)(last_tagndx+1);
682 /* Remember that we keep the native index in the offset
683 so patch the beginning of the struct to point to this
684 */
685 auxent->x_sym.x_tagndx = last_tagndx->n_offset;
686 auxent->x_sym.x_fcnary.x_fcn.x_endndx =
687 native_index + syment->n_numaux + 1 ;
688 /* Now point the eos to the structure */
689 auxent = (AUXENT *)(syment+1);
690 auxent->x_sym.x_tagndx = last_tagndx->n_offset;
691
692
693 }
694 else if (syment->n_sclass == C_BLOCK
695 && coff_symbol_ptr->symbol.name[1] == 'e')
696 {
697 AUXENT *auxent = (AUXENT *)((*(--last_block))+1);
698 auxent->x_sym.x_fcnary.x_fcn.x_endndx =
699 native_index + syment->n_numaux + 1;
700 }
701 if (syment->n_sclass == C_EXT
702 && !ISFCN(syment->n_type)
703 && first_time == true
704 && last_file != (SYMENT *)NULL) {
705 /* This is the first external symbol seen which isn't a
706 function place it in the last .file entry */
707 last_file->n_value = native_index;
708 first_time = false;
709 }
710 #ifdef C_LEAFPROC
711 if (syment->n_sclass == C_LEAFPROC && syment->n_numaux == 2) {
712 AUXENT *auxent = (AUXENT *)(syment+2);
713 /* This is the definition of a leaf proc, we'll relocate the
714 address */
715
716 auxent->x_bal.x_balntry +=
717 coff_symbol_ptr->symbol.section->output_offset +
718 coff_symbol_ptr->symbol.section->output_section->vma ;
719 }
720 #endif
721 /* If this symbol needs to be tied up then remember some facts */
722 if (syment->n_sclass == C_FILE)
723 {
724 last_file = syment;
725 }
726 if (syment->n_numaux != 0) {
727 /*
728 If this symbol would like to point to something in the
729 future then remember where it is
730 */
731 if (uses_x_sym_x_tagndx_p(syment)) {
732 /*
733 If this is a ref to a structure then we'll tie it up
734 now - there are never any forward refs for one
735 */
736 if (syment->n_sclass == C_STRTAG ||
737 syment->n_sclass == C_ENTAG ||
738 syment->n_sclass == C_UNTAG) {
739 last_tagndx = syment;
740 }
741 else {
742 /*
743 This is a ref to a structure - the structure must
744 have been defined within the same file, and previous
745 to this point, so we can deduce the new tagndx
746 directly.
747 */
748 AUXENT *auxent = (AUXENT *)(syment+1);
749 bfd *bfd_ptr = coff_symbol_ptr->symbol.the_bfd;
750 SYMENT *base = obj_raw_syments(bfd_ptr);
751 auxent->x_sym.x_tagndx = base[auxent->x_sym.x_tagndx].n_offset;
752
753 }
754 }
755 if (ISFCN(syment->n_type)) {
756 last_fcn = syment;
757 }
758 if (syment->n_sclass == C_BLOCK
759 && coff_symbol_ptr->symbol.name[1] == 'b')
760 {
761 *last_block++ = syment;
762 }
763 }
764 syment->n_offset = native_index;
765 native_index = native_index + 1 + syment->n_numaux;
766 }
767 }
768 }
769 }
770
771
772
773
774
775
776
777 static void
778 coff_write_symbols(abfd)
779 bfd *abfd;
780 {
781 unsigned int i;
782 unsigned int limit = bfd_get_symcount(abfd);
783 unsigned int written = 0;
784 SYMENT dummy;
785 asymbol **p;
786 unsigned int string_size = 0;
787
788
789 /* Seek to the right place */
790 bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET);
791
792 /* Output all the symbols we have */
793
794 written = 0;
795 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) {
796 asymbol *symbol = *p;
797 coff_symbol_type *c_symbol = coff_symbol_from(abfd, symbol);
798
799 unsigned int j;
800 SYMENT *native;
801 if (c_symbol == (coff_symbol_type *) NULL ||
802 c_symbol->native == (SYMENT *) NULL) {
803 /*
804 This symbol has been created by the loader, or come from a non
805 coff format. It has no native element to inherit, make our
806 own
807 */
808
809 native = &dummy;
810 native->n_type = T_NULL;
811 #ifdef I960
812 native->n_flags = 0;
813 #endif
814 if (symbol->flags & BSF_ABSOLUTE) {
815 native->n_scnum = N_ABS;
816 native->n_value = symbol->value;
817 }
818 else if (symbol->flags & (BSF_UNDEFINED | BSF_FORT_COMM)) {
819 native->n_scnum = N_UNDEF;
820 native->n_value = symbol->value;
821 }
822 else if (symbol->flags & BSF_DEBUGGING) {
823 /*
824 remove name so it doesn't take up any space
825 */
826 symbol->name = "";
827 #if 0 /* FIXME -- Steve hasn't decided what to do
828 with these */
829 /*
830 Don't do anything with debugs from the loader
831 */
832 native->n_scnum = N_DEBUG;
833 #endif
834 continue;
835 }
836 else {
837 native->n_scnum = symbol->section->output_section->index + 1;
838 native->n_value = symbol->value +
839 symbol->section->output_section->vma +
840 symbol->section->output_offset;
841 #ifdef I960
842 /* Copy the any flags from the the file hdr into the symbol */
843 {
844 coff_symbol_type *c = coff_symbol_from(abfd, symbol);
845 if (c != (coff_symbol_type *)NULL) {
846 native->n_flags = c->symbol.the_bfd->flags;
847 }
848 }
849 #endif
850 }
851
852
853
854 #ifdef HASPAD1
855 native->pad1[0] = 0;
856 native->pad1[0] = 0;
857 #endif
858
859 native->pad2[0] = 0;
860 native->pad2[1] = 0;
861
862 native->n_type = 0;
863 native->n_sclass = C_EXT;
864 native->n_numaux = 0;
865 }
866 else
867 /*
868 Does this symbol have an ascociated line number - if so then
869 make it remember this symbol index. Also tag the auxent of
870 this symbol to point to the right place in the lineno table
871 */
872 {
873 alent *lineno = c_symbol->lineno;
874 native = c_symbol->native;
875 if (lineno) {
876 unsigned int count = 0;
877 lineno[count].u.offset = written;
878 if (native->n_numaux) {
879 union auxent *a = (union auxent *) (native + 1);
880 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
881 c_symbol->symbol.section->output_section->moving_line_filepos;
882 }
883 /*
884 And count and relocate all other linenumbers
885 */
886 count++;
887 while (lineno[count].line_number) {
888 lineno[count].u.offset +=
889 c_symbol->symbol.section->output_section->vma +
890 c_symbol->symbol.section->output_offset;
891 count++;
892 }
893 c_symbol->symbol.section->output_section->moving_line_filepos +=
894 count * sizeof(struct lineno);
895
896 }
897 } /* if symbol new to coff */
898
899 /* Fix the symbol names */
900 {
901 unsigned int name_length;
902 if (symbol->name == (char *) NULL) {
903 /*
904 coff symbols always have names, so we'll make one up
905 */
906 symbol->name = "strange";
907 }
908 name_length = strlen(symbol->name);
909 if (name_length <= SYMNMLEN) {
910 /* This name will fit into the symbol neatly */
911 strncpy(native->n_name, symbol->name, SYMNMLEN);
912 }
913 else {
914 native->n_offset = string_size + 4;
915 native->n_zeroes = 0;
916 string_size += name_length + 1;
917 }
918 {
919 unsigned int numaux = native->n_numaux;
920 int type = native->n_type;
921 int class = native->n_sclass;
922 bfd_coff_swap_sym(abfd, native);
923 bfd_write((void *) native, 1, SYMESZ, abfd);
924 for (j = 0; j != native->n_numaux; j++) {
925 bfd_coff_swap_aux(abfd, native + j + 1, type, class);
926 bfd_write((void *) (native + j + 1), 1, AUXESZ, abfd);
927
928 }
929 /*
930 Reuse somewhere in the symbol to keep the index
931 */
932 set_index(symbol, written);
933 written += 1 + numaux;
934 }
935 }
936 } /* for each out symbol */
937
938 bfd_get_symcount(abfd) = written;
939 /* Now write out strings */
940
941 if (string_size) {
942 unsigned int size = string_size + 4;
943 bfd_h_put_x(abfd, size, &size);
944 bfd_write((void *) &size, 1, sizeof(size), abfd);
945 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) {
946 asymbol *q = *p;
947 size_t name_length = strlen(q->name);
948 if (name_length > SYMNMLEN) {
949 bfd_write((void *) (q->name), 1, name_length + 1, abfd);
950 }
951 }
952 }
953 else {
954 /* We would normally not write anything here, but we'll write
955 out 4 so that any stupid coff reader which tries to read
956 the string table even when there isn't one won't croak.
957 */
958
959 uint32e_type size = 4;
960 bfd_h_put_x(abfd, size, &size);
961 bfd_write((void *)&size, 1, sizeof(size), abfd);
962
963 }
964
965 }
966
967 static void
968 coff_write_relocs(abfd)
969 bfd *abfd;
970 {
971 asection *s;
972 for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
973 unsigned int i;
974 arelent **p = s->orelocation;
975 bfd_seek(abfd, s->rel_filepos, SEEK_SET);
976 for (i = 0; i < s->reloc_count; i++) {
977 struct reloc n;
978 arelent *q = p[i];
979 memset((PTR)&n, 0, sizeof(n));
980 n.r_vaddr = q->address + s->vma;
981 n.r_symndx = get_index((*(q->sym_ptr_ptr)));
982 n.r_type = q->howto->type;
983 swap_reloc(abfd, &n);
984 bfd_write((void *) &n, 1, RELSZ, abfd);
985 }
986 }
987 }
988 static void
989 coff_write_linenumbers(abfd)
990 bfd *abfd;
991 {
992 asection *s;
993 for (s = abfd->sections; s != (asection *) NULL; s = s->next) {
994 if (s->lineno_count) {
995 asymbol **q = abfd->outsymbols;
996 bfd_seek(abfd, s->line_filepos, SEEK_SET);
997 /* Find all the linenumbers in this section */
998 while (*q) {
999 asymbol *p = *q;
1000 alent *l = BFD_SEND(p->the_bfd, _get_lineno, (p->the_bfd, p));
1001 if (l) {
1002 /* Found a linenumber entry, output */
1003 struct lineno out;
1004 bzero( &out, sizeof(out));
1005 out.l_lnno = 0;
1006 out.l_addr.l_symndx = l->u.offset;
1007 bfd_coff_swap_lineno(abfd, &out);
1008 bfd_write((void *) &out, 1, LINESZ, abfd);
1009 l++;
1010 while (l->line_number) {
1011 out.l_lnno = l->line_number;
1012 out.l_addr.l_symndx = l->u.offset;
1013 bfd_coff_swap_lineno(abfd, &out);
1014 bfd_write((void *) &out, 1, LINESZ, abfd);
1015 l++;
1016 }
1017 }
1018 q++;
1019 }
1020 }
1021 }
1022 }
1023
1024
1025 static asymbol *
1026 coff_make_empty_symbol(abfd)
1027 bfd *abfd;
1028 {
1029 coff_symbol_type *new = (coff_symbol_type *) malloc(sizeof(coff_symbol_type));
1030 if (new == NULL) {
1031 bfd_error = no_memory;
1032 return (NULL);
1033 } /* on error */
1034 new->native = 0;
1035 new->lineno = (alent *) NULL;
1036 new->symbol.the_bfd = abfd;
1037 return &new->symbol;
1038 }
1039
1040 static void
1041 coff_print_symbol(ignore_abfd, file, symbol, how)
1042 bfd *ignore_abfd;
1043 FILE *file;
1044 asymbol *symbol;
1045 bfd_print_symbol_enum_type how;
1046 {
1047 switch (how) {
1048 case bfd_print_symbol_name_enum:
1049 fprintf(file, "%s", symbol->name);
1050 break;
1051 case bfd_print_symbol_type_enum:
1052 fprintf(file, "coff %lx %lx", (unsigned long) coffsymbol(symbol)->native,
1053 (unsigned long) coffsymbol(symbol)->lineno);
1054 break;
1055 case bfd_print_symbol_all_enum:
1056 {
1057 char *section_name = symbol->section == (asection *) NULL ?
1058 "*abs" : symbol->section->name;
1059 bfd_print_symbol_vandf((void *) file, symbol);
1060
1061 fprintf(file, " %-5s %s %s %s",
1062 section_name,
1063 coffsymbol(symbol)->native ? "n" : "g",
1064 coffsymbol(symbol)->lineno ? "l" : " ",
1065 symbol->name);
1066 }
1067
1068
1069 break;
1070 }
1071 }
1072 static alent *
1073 coff_get_lineno(ignore_abfd, symbol)
1074 bfd *ignore_abfd;
1075 asymbol *symbol;
1076 {
1077 return coffsymbol(symbol)->lineno;
1078 }
1079 /*
1080 Set flags and magic number of a coff file from architecture and machine
1081 type. Result is true if we can represent the arch&type, false if not.
1082 */
1083 static boolean
1084 coff_set_flags(abfd, magicp, flagsp)
1085 bfd *abfd;
1086 unsigned *magicp,
1087 *flagsp;
1088 {
1089
1090
1091 switch (abfd->obj_arch) {
1092
1093 #ifdef I960ROMAGIC
1094
1095 case bfd_arch_i960:
1096
1097 {
1098 unsigned flags;
1099 *magicp = I960ROMAGIC;
1100 /*
1101 ((bfd_get_file_flags(abfd) & WP_TEXT) ? I960ROMAGIC :
1102 I960RWMAGIC); FIXME???
1103 */
1104 switch (abfd->obj_machine) {
1105 case bfd_mach_i960_core:
1106 flags = F_I960CORE;
1107 break;
1108 case bfd_mach_i960_kb_sb:
1109 flags = F_I960KB;
1110 break;
1111 case bfd_mach_i960_mc:
1112 flags = F_I960MC;
1113 break;
1114 case bfd_mach_i960_xa:
1115 flags = F_I960XA;
1116 break;
1117 case bfd_mach_i960_ca:
1118 flags = F_I960CA;
1119 break;
1120 case bfd_mach_i960_ka_sa:
1121 flags = F_I960KA;
1122 break;
1123 default:
1124 return false;
1125 }
1126 *flagsp = flags;
1127 return true;
1128 }
1129 break;
1130 #endif
1131 #ifdef MC68MAGIC
1132 case bfd_arch_m68k:
1133 *magicp = MC68MAGIC;
1134 return true;
1135 #endif
1136 #ifdef M88MAGIC
1137 case bfd_arch_m88k:
1138 *magicp = MC88MAGIC;
1139 return true;
1140 break;
1141 #endif
1142
1143 default: /* Unknown architecture */
1144 return false;
1145 }
1146
1147 return false;
1148 }
1149
1150
1151 static boolean
1152 coff_set_arch_mach(abfd, arch, machine)
1153 bfd *abfd;
1154 enum bfd_architecture arch;
1155 unsigned long machine;
1156 {
1157 unsigned dummy1,
1158 dummy2;
1159 abfd->obj_arch = arch;
1160 abfd->obj_machine = machine;
1161 if (arch != bfd_arch_unknown &&
1162 coff_set_flags(abfd, &dummy1, &dummy2) != true)
1163 return false; /* We can't represent this type */
1164 return true; /* We're easy ... */
1165 }
1166
1167
1168 /* Calculate the file position for each section. */
1169
1170 static void
1171 coff_compute_section_file_positions(abfd)
1172 bfd *abfd;
1173 {
1174 asection *current;
1175 file_ptr sofar = FILHSZ;
1176 if (bfd_get_start_address(abfd)) {
1177 /*
1178 A start address may have been added to the original file. In this
1179 case it will need an optional header to record it.
1180 */
1181 abfd->flags |= EXEC_P;
1182 }
1183 if (abfd->flags & EXEC_P)
1184 sofar += AOUTSZ;
1185
1186
1187 sofar += abfd->section_count * SCNHSZ;
1188
1189 for (current = abfd->sections; current != NULL; current = current->next) {
1190 current->filepos = sofar;
1191 /* Only add sections which have contents */
1192 if (current->flags & SEC_HAS_CONTENTS)
1193 sofar += current->size;
1194 }
1195 obj_relocbase(abfd) = sofar;
1196 }
1197
1198
1199
1200
1201 /* SUPPRESS 558 */
1202 /* SUPPRESS 529 */
1203 static boolean
1204 coff_write_object_contents(abfd)
1205 bfd *abfd;
1206 {
1207 struct filehdr file_header;
1208 asection *current;
1209 boolean hasrelocs = false;
1210 boolean haslinno = false;
1211 file_ptr reloc_base;
1212 file_ptr lineno_base;
1213 file_ptr sym_base;
1214 file_ptr scn_base;
1215 file_ptr data_base;
1216 unsigned long reloc_size = 0;
1217 unsigned long lnno_size = 0;
1218 asection *text_sec = NULL;
1219 asection *data_sec = NULL;
1220 asection *bss_sec = NULL;
1221 unsigned magic,
1222 flags;
1223 bfd_error = system_call_error;
1224
1225
1226 if(abfd->output_has_begun == false) {
1227 coff_compute_section_file_positions(abfd);
1228 }
1229 scn_base = (file_ptr) (sizeof(struct filehdr)
1230 + ((abfd->flags & EXEC_P) ? sizeof(AOUTHDR) : 0));
1231
1232 if (bfd_seek(abfd, scn_base, SEEK_SET) != 0)
1233 return false;
1234 reloc_base = obj_relocbase(abfd);
1235
1236
1237
1238
1239
1240 /*
1241 Make a pass through the symbol table to count line number entries and
1242 put them into the correct asections
1243 */
1244 coff_count_linenumbers(abfd);
1245 data_base = scn_base;
1246 /* Work out the size of the reloc and linno areas */
1247
1248 for (current = abfd->sections; current != NULL; current = current->next) {
1249 reloc_size += current->reloc_count * sizeof(struct reloc);
1250 lnno_size += current->lineno_count * sizeof(struct lineno);
1251 data_base += sizeof(struct scnhdr);
1252 }
1253
1254
1255 lineno_base = reloc_base + reloc_size;
1256 sym_base = lineno_base + lnno_size;
1257
1258 /* Indicate in each section->line_filepos its actual file address */
1259 for (current = abfd->sections; current != NULL; current = current->next) {
1260 if (current->lineno_count) {
1261 current->line_filepos = lineno_base;
1262 current->moving_line_filepos = lineno_base;
1263 lineno_base += current->lineno_count * sizeof(struct lineno);
1264
1265 }
1266 else {
1267 current->line_filepos = 0;
1268 }
1269 if (current->reloc_count) {
1270 current->rel_filepos = reloc_base;
1271 reloc_base += current->reloc_count * sizeof(struct reloc);
1272 }
1273 else {
1274 current->rel_filepos = 0;
1275 }
1276 }
1277
1278
1279
1280 bfd_seek(abfd,
1281 (file_ptr) ((abfd->flags & EXEC_P) ?
1282 (FILHSZ + AOUTSZ) : FILHSZ),
1283 SEEK_SET);
1284 {
1285 #if 0
1286 unsigned int pad = abfd->flags & D_PAGED ? data_base : 0;
1287 #endif
1288 unsigned int pad = 0;
1289 for (current = abfd->sections; current != NULL; current = current->next) {
1290 SCNHDR section;
1291 strncpy(&(section.s_name[0]), current->name, 8);
1292 section.s_vaddr = current->vma + pad;
1293 section.s_paddr = current->vma + pad;
1294 section.s_size = current->size - pad;
1295 /*
1296 If this section has no size or is unloadable then the scnptr
1297 will be 0 too
1298 */
1299 if (current->size - pad == 0 ||
1300 (current->flags & SEC_LOAD) == 0) {
1301 section.s_scnptr = 0;
1302
1303 }
1304 else {
1305 section.s_scnptr = current->filepos;
1306 }
1307 section.s_relptr = current->rel_filepos;
1308 section.s_lnnoptr = current->line_filepos;
1309 section.s_nreloc = current->reloc_count;
1310 section.s_nlnno = current->lineno_count;
1311 if (current->reloc_count != 0)
1312 hasrelocs = true;
1313 if (current->lineno_count != 0)
1314 haslinno = true;
1315
1316 if (!strcmp(current->name, _TEXT)) {
1317 text_sec = current;
1318 section.s_flags = STYP_TEXT; /* kind stupid optimisation */
1319 }
1320 else {
1321
1322 if (!strcmp(current->name, _DATA)) {
1323 data_sec = current;
1324 section.s_flags = STYP_DATA; /* kind stupid
1325 optimisation */
1326 }
1327 else if (!strcmp(current->name, _BSS)) {
1328 bss_sec = current;
1329 section.s_flags = STYP_BSS; /* kind stupid optimisation */
1330 }
1331 }
1332
1333
1334 #ifdef I960
1335 section.s_align = (current->alignment_power
1336 ? 1 << current->alignment_power
1337 : 0);
1338
1339 #endif
1340 swap_scnhdr(abfd, &section);
1341 bfd_write((void *) (&section), 1, SCNHSZ, abfd);
1342 pad = 0;
1343 }
1344
1345 }
1346 /* OK, now set up the filehdr... */
1347
1348 bfd_h_put_x(abfd, abfd->section_count, &file_header.f_nscns);
1349 /*
1350 We will NOT put a fucking timestamp in the header here. Every time you
1351 put it back, I will come in and take it out again. I'm sorry. This
1352 field does not belong here. We fill it with a 0 so it compares the
1353 same but is not a reasonable time. -- gnu@cygnus.com
1354 */
1355 /*
1356 Well, I like it, so I'm conditionally compiling it in.
1357 steve@cygnus.com
1358 */
1359 #ifdef COFF_TIMESTAMP
1360 bfd_h_put_x(abfd, time(0), &file_header.f_timdat);
1361 #else
1362 bfd_h_put_x(abfd, 0, &file_header.f_timdat);
1363 #endif
1364
1365 if (bfd_get_symcount(abfd) != 0)
1366 bfd_h_put_x(abfd, sym_base, &file_header.f_symptr);
1367 else
1368 bfd_h_put_x(abfd, 0, &file_header.f_symptr);
1369
1370 file_header.f_flags = 0;
1371
1372 if (abfd->flags & EXEC_P)
1373 bfd_h_put_x(abfd, sizeof(AOUTHDR), &file_header.f_opthdr);
1374 else
1375 bfd_h_put_x(abfd, 0, &file_header.f_opthdr);
1376
1377 if (!hasrelocs)
1378 file_header.f_flags |= F_RELFLG;
1379 if (!haslinno)
1380 file_header.f_flags |= F_LNNO;
1381 if (0 == bfd_get_symcount(abfd))
1382 file_header.f_flags |= F_LSYMS;
1383 if (abfd->flags & EXEC_P)
1384 file_header.f_flags |= F_EXEC;
1385 if (!abfd->xvec->byteorder_big_p)
1386 file_header.f_flags |= F_AR32WR;
1387 /*
1388 FIXME, should do something about the other byte orders and
1389 architectures.
1390 */
1391
1392 /* Set up architecture-dependent stuff */
1393
1394 magic = 0;
1395 flags = 0;
1396 coff_set_flags(abfd, &magic, &flags);
1397 file_header.f_flags |= flags;
1398
1399 bfd_h_put_x(abfd, magic, &file_header.f_magic);
1400 bfd_h_put_x(abfd, file_header.f_flags, &file_header.f_flags);
1401
1402 /* ...and the "opt"hdr... */
1403 #ifdef I960
1404 bfd_h_put_x(abfd, (magic == I960ROMAGIC ? NMAGIC : OMAGIC),
1405 &(exec_hdr(abfd)->magic));
1406 #endif
1407 #if M88
1408 exec_hdr(abfd)->magic = PAGEMAGIC3;
1409 #endif
1410
1411 /* Now should write relocs, strings, syms */
1412 obj_sym_filepos(abfd) = sym_base;
1413
1414 if (bfd_get_symcount(abfd) != 0) {
1415 coff_mangle_symbols(abfd);
1416 coff_write_symbols(abfd);
1417 coff_write_linenumbers(abfd);
1418 coff_write_relocs(abfd);
1419 }
1420 if (text_sec) {
1421 bfd_h_put_x(abfd, text_sec->size, &exec_hdr(abfd)->tsize);
1422 bfd_h_put_x(abfd,
1423 text_sec->size ? text_sec->vma : 0,
1424 &exec_hdr(abfd)->text_start);
1425 }
1426 if (data_sec) {
1427 bfd_h_put_x(abfd, data_sec->size, &exec_hdr(abfd)->dsize);
1428 bfd_h_put_x(abfd,
1429 data_sec->size ? data_sec->vma : 0,
1430 &exec_hdr(abfd)->data_start);
1431 }
1432 if (bss_sec) {
1433 bfd_h_put_x(abfd, bss_sec->size, &exec_hdr(abfd)->bsize);
1434 }
1435 bfd_h_put_x(abfd, bfd_get_start_address(abfd), &exec_hdr(abfd)->entry);
1436
1437
1438 /* now write them */
1439 bfd_h_put_x(abfd, bfd_get_symcount(abfd), &file_header.f_nsyms);
1440 if (bfd_seek(abfd, 0L, SEEK_SET) != 0)
1441 return false;
1442
1443 bfd_write((void *) &file_header, 1, FILHSZ, abfd);
1444
1445 if (abfd->flags & EXEC_P) {
1446
1447 bfd_write((void *) exec_hdr(abfd), 1, AOUTSZ, abfd);
1448 }
1449 return true;
1450 }
1451
1452
1453
1454 static boolean
1455 coff_set_section_contents(abfd, section, location, offset, count)
1456 bfd *abfd;
1457 sec_ptr section;
1458 void *location;
1459 file_ptr offset;
1460 size_t count;
1461 {
1462 if (abfd->output_has_begun == false) /* set by bfd.c handler */
1463 coff_compute_section_file_positions(abfd);
1464
1465 bfd_seek(abfd, (file_ptr) (section->filepos + offset), SEEK_SET);
1466
1467 if (count != 0) {
1468 return (bfd_write(location, 1, count, abfd) == count) ? true : false;
1469 }
1470 return true;
1471 }
1472 static boolean
1473 coff_get_section_contents(abfd, section, location, offset, count)
1474 bfd *abfd;
1475 sec_ptr section;
1476 void *location;
1477 file_ptr offset;
1478 int count;
1479 {
1480 if (count == 0
1481 || offset >= section->size
1482 || bfd_seek(abfd, section->filepos + offset, SEEK_SET) == -1
1483 || bfd_read(location, 1, count, abfd) != count) {
1484 return (false);
1485 } /* on error */
1486 return (true);
1487 } /* coff_get_section_contents() */
1488
1489
1490 static boolean
1491 coff_close_and_cleanup(abfd)
1492 bfd *abfd;
1493 {
1494 if (!bfd_read_p(abfd))
1495 switch (abfd->format) {
1496 case bfd_archive:
1497 if (!_bfd_write_archive_contents(abfd))
1498 return false;
1499 break;
1500 case bfd_object:
1501 if (!coff_write_object_contents(abfd))
1502 return false;
1503 break;
1504 default:
1505 bfd_error = invalid_operation;
1506 return false;
1507 }
1508
1509 #define cleaner(ptr) if (abfd->ptr) free (abfd->ptr)
1510 cleaner(tdata);
1511
1512 if (abfd->my_archive)
1513 cleaner(filename);
1514
1515 #undef cleaner
1516 return true;
1517 }
1518
1519
1520
1521
1522
1523
1524 static void *
1525 buy_and_read(abfd, where, seek_direction, size)
1526 bfd *abfd;
1527 file_ptr where;
1528 int seek_direction;
1529 size_t size;
1530 {
1531 void *area = (void *) malloc(size);
1532 if (!area) {
1533 bfd_error = no_memory;
1534 return (NULL);
1535 }
1536 bfd_seek(abfd, where, seek_direction);
1537 if (bfd_read(area, 1, size, abfd) != size) {
1538 bfd_error = system_call_error;
1539 free(area);
1540 return (NULL);
1541 } /* on error */
1542 return (area);
1543 } /* buy_and_read() */
1544
1545 static void
1546 offset_symbol_indices(symtab, count, offset)
1547 SYMENT *symtab;
1548 unsigned long count;
1549 long offset;
1550 {
1551 SYMENT *end = symtab + count;
1552 for (; symtab < end; ++symtab) {
1553 if (symtab->n_sclass == C_FILE) {
1554 symtab->n_value = 0;
1555 }
1556 else if (symtab->n_sclass == C_ALIAS) {
1557 /*
1558 These guys have indices in their values.
1559 */
1560 symtab->n_value += offset;
1561
1562 }
1563 else if (symtab->n_numaux) {
1564 /*
1565 anybody else without an aux, has no indices.
1566 */
1567
1568 if (symtab->n_sclass == C_EOS
1569 || (BTYPE(symtab->n_type) == T_STRUCT
1570 && symtab->n_sclass != C_STRTAG)
1571 || BTYPE(symtab->n_type) == T_UNION
1572 || BTYPE(symtab->n_type) == T_ENUM) {
1573
1574 ((AUXENT *) (symtab + 1))->x_sym.x_tagndx += offset;
1575
1576 } /* These guys have a tagndx */
1577 if (symtab->n_sclass == C_STRTAG
1578 || symtab->n_sclass == C_UNTAG
1579 || symtab->n_sclass == C_ENTAG
1580 || symtab->n_sclass == C_BLOCK
1581 || symtab->n_sclass == C_FCN
1582 || ISFCN(symtab->n_type)) {
1583
1584 ((AUXENT *) (symtab + 1))->x_sym.x_fcnary.x_fcn.x_endndx += offset;
1585
1586 } /* These guys have an endndx */
1587 #ifndef I960
1588 if (ISFCN(symtab->n_type)) {
1589 ((AUXENT *) (symtab + 1))->x_sym.x_tvndx += offset;
1590 } /* These guys have a tvndx. I think...
1591 (FIXME) */
1592 #endif /* Not I960 */
1593
1594 } /* if value, else if aux */
1595 symtab += symtab->n_numaux;
1596 } /* walk the symtab */
1597
1598 return;
1599 } /* offset_symbol_indices() */
1600 /* swap the entire symbol table */
1601 static void
1602 swap_raw_symtab(abfd, raw_symtab)
1603 bfd *abfd;
1604 SYMENT *raw_symtab;
1605 {
1606 long i;
1607 SYMENT *end = raw_symtab + bfd_get_symcount(abfd);
1608 for (; raw_symtab < end; ++raw_symtab) {
1609 bfd_coff_swap_sym(abfd, raw_symtab);
1610
1611 for (i = raw_symtab->n_numaux; i; --i, ++raw_symtab) {
1612 bfd_coff_swap_aux(abfd,
1613 raw_symtab + 1,
1614 raw_symtab->n_type,
1615 raw_symtab->n_sclass);
1616 } /* swap all the aux entries */
1617 } /* walk the symbol table */
1618
1619 return;
1620 } /* swap_raw_symtab() */
1621 /*
1622 read a symbol table into freshly mallocated memory, swap it, and knit the
1623 symbol names into a normalized form. By normalized here I mean that all
1624 symbols have an n_offset pointer that points to a NULL terminated string.
1625 Oh, and the first symbol MUST be a C_FILE. If there wasn't one there
1626 before, put one there.
1627 */
1628
1629 static SYMENT *
1630 get_normalized_symtab(abfd)
1631 bfd *abfd;
1632 {
1633 SYMENT *end;
1634 SYMENT *retval;
1635 SYMENT *s;
1636 char *string_table = NULL;
1637 unsigned long size;
1638 unsigned long string_table_size = 0;
1639 /*
1640 I used to SEEK_END here to read the symtab and string table all at
1641 once. This fails if this bfd is really an archive element. Thus, the
1642 somewhat convoluted approach to reading in the string table. xoxorich.
1643 */
1644 /*
1645 $if ((bfd_seek(abfd, 0, SEEK_END) == -1) || ((end_of_file =
1646 bfd_tell(abfd)) == -1) || (bfd_seek(abfd, obj_sym_filepos(abfd),
1647 SEEK_SET) == -1) || ((beginning_of_symtab = bfd_tell(abfd)) == -1)) {
1648
1649 bfd_error = system_call_error; return(NULL); }$
1650 *//* on error */
1651
1652 /* $if ((size = end_of_file - beginning_of_symtab) == 0) {$ */
1653
1654 if ((size = bfd_get_symcount(abfd) * sizeof(SYMENT)) == 0) {
1655 bfd_error = no_symbols;
1656 return (NULL);
1657 } /* no symbols */
1658 /*
1659 This is a hack. Some tool chains fail to put a C_FILE symbol at the
1660 beginning of the symbol table. To make life simpler for our users, we
1661 inject one if it wasn't there originally.
1662
1663 We'd like to keep all of this bfd's native symbols in one block to keep
1664 table traversals simple. To do that, we need to know whether we will
1665 be prepending the C_FILE symbol before we read the rest of the table.
1666 */
1667 if ((s = (SYMENT *) malloc(sizeof(SYMENT) * 2)) == NULL) {
1668 bfd_error = no_memory;
1669 return (NULL);
1670 } /* on error */
1671 if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1
1672 || bfd_read(s, sizeof(SYMENT), 1, abfd) != sizeof(SYMENT)) {
1673 bfd_error = system_call_error;
1674 return (NULL);
1675 } /* on error */
1676 bfd_coff_swap_sym(abfd, s);
1677
1678 if (s->n_sclass == C_FILE) {
1679 obj_symbol_slew(abfd) = 0;
1680
1681 if ((retval = (SYMENT *) malloc(size)) == NULL) {
1682 bfd_error = no_memory;
1683 return (NULL);
1684 } /* on malloc error */
1685 }
1686 else {
1687 unsigned long namelength = 0;
1688 char *filename;
1689 obj_symbol_slew(abfd) = 2;
1690
1691 if ((retval = (SYMENT *) malloc(size
1692 + (obj_symbol_slew(abfd)
1693 * sizeof(SYMENT)))) == NULL) {
1694 bfd_error = no_memory;
1695 return (NULL);
1696 } /* on malloc error */
1697 bzero((char *) retval, size + (obj_symbol_slew(abfd) * sizeof(SYMENT)));
1698
1699 #define FILE_ENTRY_NAME ".file"
1700
1701 if ((retval->n_offset = (int) malloc(strlen(FILE_ENTRY_NAME)
1702 + 1)) == NULL) {
1703 bfd_error = no_memory;
1704 return (NULL);
1705 } /* on malloc error */
1706 strcpy((char *) retval->n_offset, FILE_ENTRY_NAME);
1707 retval->n_sclass = C_FILE;
1708 retval->n_scnum = N_DEBUG;
1709 retval->n_numaux = 1;
1710
1711 #undef FILE_ENTRY_NAME
1712
1713 if ((filename = bfd_get_filename(abfd)) == NULL) {
1714 filename = "fake";
1715 } /* if we know it's name */
1716 if ((namelength = strlen(filename)) <= FILNMLEN) {
1717 strncpy(((AUXENT *) (retval + 1))->x_file.x_fname, filename, FILNMLEN);
1718 }
1719 else {
1720 if ((((AUXENT *) (retval + 1))->x_file.x_n.x_offset
1721 = (int) malloc(namelength+1)) == NULL) {
1722 bfd_error = no_memory;
1723 return (NULL);
1724 } /* on error */
1725 strcpy((char *) (((AUXENT *) (retval + 1))->x_file.x_n.x_offset),
1726 filename);
1727
1728 } /* if "short" name */
1729 } /* missing file entry. */
1730
1731 free(s);
1732
1733 if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1
1734 || bfd_read(retval + obj_symbol_slew(abfd), size, 1, abfd) != size) {
1735 bfd_error = system_call_error;
1736 return (NULL);
1737 } /* on read error */
1738 /* mark the end of the symbols */
1739 end = retval + obj_symbol_slew(abfd) + bfd_get_symcount(abfd);
1740 /*
1741 FIXME SOMEDAY. A string table size of zero is very weird, but
1742 probably possible. If one shows up, it will probably kill us.
1743 */
1744
1745 swap_raw_symtab(abfd, retval + obj_symbol_slew(abfd));
1746
1747 /* ...and normalize symbol names. */
1748
1749 for (s = retval + obj_symbol_slew(abfd); s < end; ++s) {
1750 if (s->n_zeroes != 0) {
1751 /*
1752 This is a "short" name. Make it long.
1753 */
1754 unsigned long i = 0;
1755 char *newstring = NULL;
1756 /*
1757 find the length of this string without walking into memory
1758 that isn't ours.
1759 */
1760
1761 for (i = 0; i <= 8; ++i) {
1762 if (s->n_name[i] == '\0') {
1763 break;
1764 } /* if end of string */
1765 } /* possible lengths of this string. */
1766
1767 if ((newstring = malloc(++i)) == NULL) {
1768 bfd_error = no_memory;
1769 return (NULL);
1770 } /* on error */
1771 bzero(newstring, i);
1772 strncpy(newstring, s->n_name, i -1 );
1773 s->n_offset = (int) newstring;
1774 s->n_zeroes = 0;
1775
1776 }
1777 else {
1778 if (string_table == NULL) {
1779 /*
1780 NOTE: we don't read the string table until now because we
1781 don't necessarily know that we have one until now.
1782 */
1783 /*
1784 At this point we should be "seek"'d to the end of the
1785 symbols === the symbol table size.
1786 */
1787
1788 if (bfd_read((char *) &string_table_size, sizeof(string_table_size), 1, abfd) != sizeof(string_table_size)) {
1789 bfd_error = system_call_error;
1790 return (NULL);
1791 } /* on error */
1792 sp(string_table_size);
1793
1794 if ((string_table = malloc(string_table_size -= 4)) == NULL) {
1795 bfd_error = no_memory;
1796 return (NULL);
1797 } /* on mallocation error */
1798 if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size) {
1799 bfd_error = system_call_error;
1800 return (NULL);
1801 } /* on error */
1802 } /* have not yet read the string table. */
1803 /*
1804 This is a long name already. Just point it at the string in
1805 memory.
1806 */
1807 s->n_offset = (int) (string_table - 4 + s->n_offset);
1808 } /* switch on type of symbol name */
1809
1810 s += s->n_numaux;
1811 } /* for each symbol */
1812 /*
1813 If we had to insert a C_FILE symbol, then everybody's indices are off
1814 by 2, so correct them.
1815 */
1816
1817 if (obj_symbol_slew(abfd) > 0) {
1818 offset_symbol_indices(retval + 2, bfd_get_symcount(abfd), 2);
1819
1820 /* and let the world know there are two more of them. */
1821 bfd_get_symcount(abfd) += 2;
1822 } /* if we added a C_FILE */
1823 obj_raw_syments(abfd) = retval;
1824 obj_string_table(abfd) = string_table;
1825
1826 return (retval);
1827 } /* get_normalized_symtab() */
1828
1829 static
1830 struct sec_struct *
1831 section_from_bfd_index(abfd, index)
1832 bfd *abfd;
1833 int index;
1834 {
1835 if (index > 0) {
1836 struct sec_struct *answer = abfd->sections;
1837 while (--index) {
1838 answer = answer->next;
1839 }
1840 return answer;
1841 }
1842 return 0;
1843 }
1844
1845 static int
1846 coff_get_symcount_upper_bound(ignore_abfd)
1847 bfd *ignore_abfd;
1848 {
1849 BFD_ASSERT(0);
1850 return 0;
1851 }
1852
1853 static symindex
1854 coff_get_first_symbol(ignore_abfd)
1855 bfd *ignore_abfd;
1856 {
1857 return 0;
1858 }
1859
1860 static symindex
1861 coff_get_next_symbol(abfd, oidx)
1862 bfd *abfd;
1863 symindex oidx;
1864 {
1865 if (oidx == BFD_NO_MORE_SYMBOLS)
1866 return BFD_NO_MORE_SYMBOLS;
1867 return ++oidx >= bfd_get_symcount(abfd) ? BFD_NO_MORE_SYMBOLS : oidx;
1868 }
1869
1870 static char *
1871 coff_symbol_name(abfd, idx)
1872 bfd *abfd;
1873 symindex idx;
1874 {
1875 return (obj_symbols(abfd) + idx)->symbol.name;
1876 }
1877
1878 static long
1879 coff_symbol_value(abfd, idx)
1880 bfd *abfd;
1881 symindex idx;
1882 {
1883 return (obj_symbols(abfd) + idx)->symbol.value;
1884 }
1885
1886 static symclass
1887 coff_classify_symbol(abfd, idx)
1888 bfd *abfd;
1889 symindex idx;
1890 {
1891 coff_symbol_type *sym = obj_symbols(abfd) + idx;
1892 if ((sym->symbol.flags & BSF_FORT_COMM) != 0)
1893 return bfd_symclass_fcommon;
1894 if ((sym->symbol.flags & BSF_GLOBAL) != 0)
1895 return bfd_symclass_global;
1896 if ((sym->symbol.flags & BSF_DEBUGGING) != 0)
1897 return bfd_symclass_debugger;
1898 if ((sym->symbol.flags & BSF_UNDEFINED) != 0)
1899 return bfd_symclass_undefined;
1900
1901 return bfd_symclass_unknown;
1902 }
1903
1904 static boolean
1905 coff_symbol_hasclass(abfd, idx, class)
1906 bfd *abfd;
1907 symindex idx;
1908 symclass class;
1909 {
1910
1911 coff_symbol_type *sym = obj_symbols(abfd) + idx;
1912 switch (class) {
1913 case bfd_symclass_fcommon:
1914 return (sym->symbol.flags & BSF_FORT_COMM) ? true : false;
1915 case bfd_symclass_global:
1916 return (sym->symbol.flags & BSF_GLOBAL) ? true : false;
1917 case bfd_symclass_debugger:
1918 return (sym->symbol.flags & BSF_DEBUGGING) ? true : false;;
1919 case bfd_symclass_undefined:
1920 return (sym->symbol.flags & BSF_UNDEFINED) ? true : false;;
1921 default:
1922 return false;
1923 }
1924
1925 }
1926
1927
1928
1929
1930 static
1931 boolean
1932 coff_slurp_line_table(abfd, asect)
1933 bfd *abfd;
1934 asection *asect;
1935 {
1936 struct lineno *native_lineno;
1937 alent *lineno_cache;
1938 BFD_ASSERT(asect->lineno == (alent *) NULL);
1939
1940
1941 native_lineno = (struct lineno *) buy_and_read(abfd,
1942 asect->line_filepos,
1943 SEEK_SET,
1944 (size_t) (sizeof(struct lineno) *
1945 asect->lineno_count));
1946 lineno_cache =
1947 (alent *) malloc((size_t) ((asect->lineno_count + 1) * sizeof(alent)));
1948 if (lineno_cache == NULL) {
1949 bfd_error = no_memory;
1950 return (BFD_FAILURE);
1951 } { /* on error */
1952 unsigned int counter = 0;
1953 alent *cache_ptr = lineno_cache;
1954 struct lineno *src = native_lineno;
1955 while (counter < asect->lineno_count) {
1956 bfd_coff_swap_lineno(abfd, src);
1957 cache_ptr->line_number = src->l_lnno;
1958
1959 if (cache_ptr->line_number == 0) {
1960 coff_symbol_type *sym =
1961 (coff_symbol_type *) (src->l_addr.l_symndx
1962 + obj_symbol_slew(abfd)
1963 + obj_raw_syments(abfd))->n_zeroes;
1964 cache_ptr->u.sym = (asymbol *) sym;
1965 sym->lineno = cache_ptr;
1966 }
1967 else {
1968 cache_ptr->u.offset = src->l_addr.l_paddr
1969 - bfd_section_vma(abfd, asect);
1970 } /* If no linenumber expect a symbol index */
1971
1972 cache_ptr++;
1973 src++;
1974 counter++;
1975 }
1976 cache_ptr->line_number = 0;
1977
1978 }
1979 free(native_lineno);
1980 asect->lineno = lineno_cache;
1981 return true;
1982 } /* coff_slurp_line_table() */
1983
1984 static SYMENT *
1985 find_next_file_symbol(current, end)
1986 SYMENT *current;
1987 SYMENT *end;
1988 {
1989 /* ignore the first symbol which is probably a C_FILE. */
1990
1991 current += current->n_numaux + 1;
1992
1993 for (; current < end; ++current) {
1994 if (current->n_sclass == C_FILE) {
1995 return (current);
1996 } /* found one */
1997 current += current->n_numaux;
1998 } /* walk the remaining table */
1999
2000 /* not found */
2001 return (end);
2002 } /* find_next_file_symbol() */
2003 /*
2004 Note that C_FILE symbols can, and some do, have more than 1 aux entry.
2005 */
2006
2007 static void
2008 force_indices_file_symbol_relative(abfd, symtab)
2009 bfd *abfd;
2010 SYMENT *symtab;
2011 {
2012 SYMENT *end = symtab + bfd_get_symcount(abfd);
2013 SYMENT *current;
2014 SYMENT *next;
2015 /* the first symbol had damn well better be a C_FILE. */
2016 BFD_ASSERT(symtab->n_sclass == C_FILE);
2017 symtab->n_value = 0;
2018
2019 for (current = find_next_file_symbol(symtab, end);
2020 current < end;
2021 current = next) {
2022 offset_symbol_indices(current,
2023 ((next =
2024 find_next_file_symbol(current,
2025 end)) - current),
2026 symtab - current);
2027 } /* walk the table */
2028
2029 return;
2030 } /* force_indices_file_symbol_relative() */
2031
2032 static boolean
2033 coff_slurp_symbol_table(abfd)
2034 bfd *abfd;
2035 {
2036 SYMENT *native_symbols;
2037 coff_symbol_type *cached_area;
2038 unsigned int *table_ptr;
2039 char *string_table = (char *) NULL;
2040 unsigned int number_of_symbols = 0;
2041 if (obj_symbols(abfd))
2042 return true;
2043 bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET);
2044
2045 /* Read in the symbol table */
2046 if ((native_symbols = get_normalized_symtab(abfd)) == NULL) {
2047 return (false);
2048 } /* on error */
2049 force_indices_file_symbol_relative(abfd, native_symbols);
2050
2051 /* Allocate enough room for all the symbols in cached form */
2052 cached_area =
2053 (coff_symbol_type *)
2054 malloc((size_t) (bfd_get_symcount(abfd) * sizeof(coff_symbol_type)));
2055
2056 if (cached_area == NULL) {
2057 bfd_error = no_memory;
2058 return (BFD_FAILURE);
2059 } /* on error */
2060 table_ptr =
2061 (unsigned int *)
2062 malloc((size_t) (bfd_get_symcount(abfd) * sizeof(unsigned int)));
2063
2064 if (table_ptr == NULL) {
2065 bfd_error = no_memory;
2066 return (BFD_FAILURE);
2067 } { /* on error */
2068 coff_symbol_type *dst = cached_area;
2069 unsigned int last_native_index = bfd_get_symcount(abfd);
2070 unsigned int this_index = 0;
2071 while (this_index < last_native_index) {
2072 SYMENT *src = native_symbols + this_index;
2073 table_ptr[this_index] = number_of_symbols;
2074 dst->symbol.the_bfd = abfd;
2075
2076 dst->symbol.name = (char *) src->n_offset; /* which was normalized
2077 to point to a null
2078 terminated string. */
2079 /*
2080 We use the native name field to point to the cached field
2081 */
2082 src->n_zeroes = (int) dst;
2083 dst->symbol.section = section_from_bfd_index(abfd, src->n_scnum);
2084
2085 switch (src->n_sclass) {
2086 #ifdef I960
2087 case C_LEAFEXT:
2088 #if 0
2089 dst->symbol.value = src->n_value - dst->symbol.section->vma;
2090 dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
2091 dst->symbol.flags |= BSF_NOT_AT_END;
2092 #endif
2093 /* Fall through to next case */
2094
2095 #endif
2096
2097 case C_EXT:
2098 if (src->n_scnum == 0) {
2099 if (src->n_value == 0) {
2100 dst->symbol.flags = BSF_UNDEFINED;
2101 }
2102 else {
2103 dst->symbol.flags = BSF_FORT_COMM;
2104 dst->symbol.value = src->n_value;
2105 }
2106 }
2107 else {
2108 /*
2109 Base the value as an index from the base of the
2110 section
2111 */
2112 if (dst->symbol.section == (asection *) NULL) {
2113 dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL | BSF_ABSOLUTE;
2114 dst->symbol.value = src->n_value;
2115 }
2116 else {
2117 dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
2118 dst->symbol.value = src->n_value - dst->symbol.section->vma;
2119 }
2120 if (ISFCN(src->n_type)) {
2121 /*
2122 A function ext does not go at the end of a file
2123 */
2124 dst->symbol.flags |= BSF_NOT_AT_END;
2125 }
2126 }
2127
2128 break;
2129 case C_STAT: /* static */
2130 #ifdef I960
2131 case C_LEAFSTAT: /* static leaf procedure */
2132 #endif
2133 case C_LABEL: /* label */
2134 dst->symbol.flags = BSF_LOCAL;
2135 /*
2136 Base the value as an index from the base of the section
2137 */
2138 dst->symbol.value = src->n_value - dst->symbol.section->vma;
2139 break;
2140
2141 case C_MOS: /* member of structure */
2142 case C_EOS: /* end of structure */
2143 case C_REGPARM: /* register parameter */
2144 case C_REG: /* register variable */
2145 #ifdef C_AUTOARG
2146 case C_AUTOARG: /* 960-specific storage class */
2147 #endif
2148 case C_TPDEF: /* type definition */
2149
2150 case C_ARG:
2151 case C_AUTO: /* automatic variable */
2152 case C_FIELD: /* bit field */
2153 case C_ENTAG: /* enumeration tag */
2154 case C_MOE: /* member of enumeration */
2155 case C_MOU: /* member of union */
2156 case C_UNTAG: /* union tag */
2157
2158 dst->symbol.flags = BSF_DEBUGGING;
2159 dst->symbol.value = src->n_value;
2160 break;
2161
2162 case C_FILE: /* file name */
2163 case C_STRTAG: /* structure tag */
2164 dst->symbol.flags = BSF_DEBUGGING;
2165 dst->symbol.value = src->n_value;
2166
2167 break;
2168 case C_BLOCK: /* ".bb" or ".eb" */
2169 case C_FCN: /* ".bf" or ".ef" */
2170 dst->symbol.flags = BSF_LOCAL;
2171 /*
2172 Base the value as an index from the base of the section
2173 */
2174 dst->symbol.value = src->n_value - dst->symbol.section->vma;
2175
2176 break;
2177 case C_EFCN: /* physical end of function */
2178 case C_NULL:
2179 case C_EXTDEF: /* external definition */
2180 case C_ULABEL: /* undefined label */
2181 case C_USTATIC: /* undefined static */
2182 case C_LINE: /* line # reformatted as symbol table entry */
2183 case C_ALIAS: /* duplicate tag */
2184 case C_HIDDEN: /* ext symbol in dmert public lib */
2185
2186 default:
2187
2188 printf("SICK%d\n", src->n_sclass);
2189 abort();
2190 dst->symbol.flags = BSF_DEBUGGING;
2191 dst->symbol.value = src->n_value;
2192
2193 break;
2194 }
2195
2196
2197
2198
2199 BFD_ASSERT(dst->symbol.flags != 0);
2200
2201 dst->native = src;
2202
2203 dst->symbol.udata = 0;
2204 dst->lineno = (alent *) NULL;
2205 this_index += src->n_numaux + 1;
2206 dst++;
2207 number_of_symbols++;
2208 } /* walk the native symtab */
2209
2210 } /* bfdize the native symtab */
2211
2212 obj_symbols(abfd) = cached_area;
2213 obj_raw_syments(abfd) = native_symbols;
2214
2215 bfd_get_symcount(abfd) = number_of_symbols;
2216 obj_convert(abfd) = table_ptr;
2217 /* Slurp the line tables for each section too */
2218 {
2219 asection *p;
2220 p = abfd->sections;
2221 while (p) {
2222 coff_slurp_line_table(abfd, p);
2223 p = p->next;
2224 }
2225 }
2226 return true;
2227 } /* coff_slurp_symbol_table() */
2228
2229 static unsigned int
2230 coff_get_symtab_upper_bound(abfd)
2231 bfd *abfd;
2232 {
2233 if (!coff_slurp_symbol_table(abfd))
2234 return 0;
2235
2236 return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *));
2237 }
2238
2239
2240 static unsigned int
2241 coff_get_symtab(abfd, alocation)
2242 bfd *abfd;
2243 asymbol **alocation;
2244 {
2245 unsigned int counter = 0;
2246 coff_symbol_type *symbase;
2247 coff_symbol_type **location = (coff_symbol_type **) (alocation);
2248 if (!coff_slurp_symbol_table(abfd))
2249 return 0;
2250
2251 for (symbase = obj_symbols(abfd); counter++ < bfd_get_symcount(abfd);)
2252 *(location++) = symbase++;
2253 *location++ = 0;
2254 return bfd_get_symcount(abfd);
2255 }
2256
2257 static unsigned int
2258 coff_get_reloc_upper_bound(abfd, asect)
2259 bfd *abfd;
2260 sec_ptr asect;
2261 {
2262 if (bfd_get_format(abfd) != bfd_object) {
2263 bfd_error = invalid_operation;
2264 return 0;
2265 }
2266 return (asect->reloc_count + 1) * sizeof(arelent *);
2267 }
2268
2269
2270 static boolean
2271 coff_slurp_reloc_table(abfd, asect, symbols)
2272 bfd *abfd;
2273 sec_ptr asect;
2274 asymbol **symbols;
2275 {
2276 struct reloc *native_relocs;
2277 arelent *reloc_cache;
2278 if (asect->relocation)
2279 return true;
2280 if (asect->reloc_count == 0)
2281 return true;
2282 if (!coff_slurp_symbol_table(abfd))
2283 return false;
2284 native_relocs =
2285 (struct reloc *) buy_and_read(abfd,
2286 asect->rel_filepos,
2287 SEEK_SET,
2288 (size_t) (sizeof(struct reloc) *
2289 asect->reloc_count));
2290 reloc_cache = (arelent *)
2291 malloc((size_t) (asect->reloc_count * sizeof(arelent)));
2292
2293 if (reloc_cache == NULL) {
2294 bfd_error = no_memory;
2295 return (BFD_FAILURE);
2296 } { /* on error */
2297 arelent *cache_ptr;
2298 struct reloc *src;
2299 for (cache_ptr = reloc_cache,
2300 src = native_relocs;
2301 cache_ptr < reloc_cache + asect->reloc_count;
2302 cache_ptr++,
2303 src++) {
2304 asymbol *ptr;
2305 swap_reloc(abfd, src);
2306 src->r_symndx += obj_symbol_slew(abfd);
2307 cache_ptr->sym_ptr_ptr = symbols + obj_convert(abfd)[src->r_symndx];
2308
2309 ptr = *(cache_ptr->sym_ptr_ptr);
2310 cache_ptr->address = src->r_vaddr;
2311 /*
2312 The symbols definitions that we have read in have been
2313 relocated as if their sections started at 0. But the offsets
2314 refering to the symbols in the raw data have not been
2315 modified, so we have to have a negative addend to compensate.
2316 */
2317
2318 if (ptr->the_bfd == abfd && ptr->section != (asection *) NULL) {
2319 cache_ptr->addend = -ptr->section->vma;
2320 }
2321 else {
2322 cache_ptr->addend = 0;
2323 }
2324
2325 cache_ptr->address -= asect->vma;
2326
2327 cache_ptr->section = (asection *) NULL;
2328
2329 #if I960
2330 cache_ptr->howto = howto_table + src->r_type;
2331 #endif
2332 #if M88
2333 if (src->r_type >= R_PCR16L && src->r_type <= R_VRT32) {
2334 cache_ptr->howto = howto_table + src->r_type - R_PCR16L;
2335 }
2336 else {
2337 BFD_ASSERT(0);
2338 }
2339 #endif
2340 #if M68
2341 cache_ptr->howto = howto_table + src->r_type;
2342 #endif
2343
2344 }
2345
2346 }
2347
2348 free(native_relocs);
2349 asect->relocation = reloc_cache;
2350 return true;
2351 }
2352
2353
2354 /* This is stupid. This function should be a boolean predicate */
2355 static unsigned int
2356 coff_canonicalize_reloc(abfd, section, relptr, symbols)
2357 bfd *abfd;
2358 sec_ptr section;
2359 arelent **relptr;
2360 asymbol **symbols;
2361 {
2362 arelent *tblptr = section->relocation;
2363 unsigned int count = 0;
2364 if (!(tblptr || coff_slurp_reloc_table(abfd, section, symbols)))
2365 return 0;
2366 tblptr = section->relocation;
2367 if (!tblptr)
2368 return 0;
2369
2370 for (; count++ < section->reloc_count;)
2371 *relptr++ = tblptr++;
2372
2373 *relptr = 0;
2374
2375 return section->reloc_count;
2376 }
2377
2378
2379
2380
2381
2382 /*
2383 provided a bfd, a section and an offset into the section, calculate and
2384 return the name of the source file and the line nearest to the wanted
2385 location.
2386 */
2387
2388 static boolean
2389 coff_find_nearest_line(abfd,
2390 section,
2391 symbols,
2392 offset,
2393 filename_ptr,
2394 functionname_ptr,
2395 line_ptr)
2396 bfd *abfd;
2397 asection *section;
2398 asymbol **symbols;
2399 bfd_vma offset;
2400 char **filename_ptr;
2401 char **functionname_ptr;
2402 unsigned int *line_ptr;
2403 {
2404 static bfd *cache_abfd;
2405 static asection *cache_section;
2406 static bfd_vma cache_offset;
2407 static unsigned int cache_i;
2408 static alent *cache_l;
2409
2410 unsigned int i = 0;
2411 struct icofdata *cof = obj_icof(abfd);
2412 /* Run through the raw syments if available */
2413 SYMENT *p;
2414 alent *l;
2415 unsigned int line_base = 0;
2416
2417
2418 *filename_ptr = 0;
2419 *functionname_ptr = 0;
2420 *line_ptr = 0;
2421
2422 /* Don't try and find line numbers in a non coff file */
2423 if (abfd->xvec->flavour != bfd_target_coff_flavour_enum)
2424 return false;
2425
2426 if (cof == (struct icofdata *)NULL)
2427 return false;
2428
2429
2430 p = cof->raw_syments;
2431 /*
2432 I don't know for sure what's right, but this isn't it. First off, an
2433 object file may not have any C_FILE's in it. After
2434 get_normalized_symtab(), it should have at least 1, the one I put
2435 there, but otherwise, all bets are off. Point #2, the first C_FILE
2436 isn't necessarily the right C_FILE because any given object may have
2437 many. I think you'll have to track sections as they coelesce in order
2438 to find the C_STAT symbol for this section. Then you'll have to work
2439 backwards to find the previous C_FILE, or choke if you get to a C_STAT
2440 for the same kind of section. That will mean that the original object
2441 file didn't have a C_FILE. xoxorich.
2442 */
2443
2444 #ifdef WEREBEINGPEDANTIC
2445 return false;
2446 #endif
2447
2448
2449
2450 for (i = 0; i < cof->raw_syment_count; i++) {
2451 if (p->n_sclass == C_FILE) {
2452 /* File name is embeded in auxent */
2453 /*
2454 This isn't right. The fname should probably be normalized
2455 during get_normalized_symtab(). In any case, what was here
2456 wasn't right because a SYMENT.n_name isn't an
2457 AUXENT.x_file.x_fname. xoxorich.
2458 */
2459
2460 *filename_ptr = ((AUXENT *) (p + 1))->x_file.x_fname;
2461 break;
2462 }
2463 p += 1 + p->n_numaux;
2464 }
2465 /* Now wander though the raw linenumbers of the section */
2466
2467
2468
2469
2470 /*
2471 If this is the same bfd as we were previously called with and this is
2472 the same section, and the offset we want is further down then we can
2473 prime the lookup loop
2474 */
2475 if (abfd == cache_abfd &&
2476 section == cache_section &&
2477 offset >= cache_offset) {
2478 i = cache_i;
2479 l = cache_l;
2480 }
2481 else {
2482 i = 0;
2483 l = section->lineno;
2484 }
2485
2486 for (; i < section->lineno_count; i++) {
2487 if (l->line_number == 0) {
2488 /* Get the symbol this line number points at */
2489 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2490 *functionname_ptr = coff->symbol.name;
2491 if (coff->native) {
2492 struct syment *s = coff->native;
2493 s = s + 1 + s->n_numaux;
2494 /*
2495 S should now point to the .bf of the function
2496 */
2497 if (s->n_numaux) {
2498 /*
2499 The linenumber is stored in the auxent
2500 */
2501 union auxent *a = (union auxent *) (s + 1);
2502 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2503 }
2504 }
2505 }
2506 else {
2507 if (l->u.offset > offset)
2508 break;
2509 *line_ptr = l->line_number + line_base + 1;
2510 }
2511 l++;
2512 }
2513
2514 cache_abfd = abfd;
2515 cache_section = section;
2516 cache_offset = offset;
2517 cache_i = i;
2518 cache_l = l;
2519 return true;
2520 }