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