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