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