Fixed all the places where there were problems with the size and
[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 static boolean
1626 coff_close_and_cleanup(abfd)
1627 bfd *abfd;
1628 {
1629 if (!bfd_read_p(abfd))
1630 switch (abfd->format) {
1631 case bfd_archive:
1632 if (!_bfd_write_archive_contents(abfd))
1633 return false;
1634 break;
1635 case bfd_object:
1636 if (!coff_write_object_contents(abfd))
1637 return false;
1638 break;
1639 default:
1640 bfd_error = invalid_operation;
1641 return false;
1642 }
1643
1644 /* We depend on bfd_close to free all the memory on the obstack. */
1645 /* FIXME if bfd_release is not using obstacks! */
1646 return true;
1647 }
1648
1649
1650 static PTR
1651 buy_and_read(abfd, where, seek_direction, size)
1652 bfd *abfd;
1653 file_ptr where;
1654 int seek_direction;
1655 size_t size;
1656 {
1657 PTR area = (PTR) bfd_alloc(abfd, size);
1658 if (!area) {
1659 bfd_error = no_memory;
1660 return (NULL);
1661 }
1662 bfd_seek(abfd, where, seek_direction);
1663 if (bfd_read(area, 1, size, abfd) != size) {
1664 bfd_error = system_call_error;
1665 return (NULL);
1666 } /* on error */
1667 return (area);
1668 } /* buy_and_read() */
1669
1670 static void
1671 DEFUN(offset_symbol_indices,(abfd, symtab, count, offset),
1672 bfd *abfd AND
1673 struct internal_syment *symtab AND
1674 unsigned long count AND
1675 long offset)
1676 {
1677 struct internal_syment *end = symtab + count;
1678 for (; symtab < end; ++symtab) {
1679 if (bfd_h_get_x(abfd, &symtab->n_sclass) == C_FILE) {
1680 bfd_h_put_x(abfd, 0, & symtab->n_value);
1681 }
1682 else if (bfd_h_get_x(abfd, &symtab->n_sclass) == C_ALIAS) {
1683 /*
1684 These guys have indices in their values.
1685 */
1686 bfd_h_put_x(abfd, bfd_h_get_x(abfd, & symtab->n_value) + offset, & symtab->n_value);
1687
1688 }
1689 else if (symtab->n_numaux) {
1690 /*
1691 anybody else without an aux, has no indices.
1692 */
1693
1694 if (symtab->n_sclass == C_EOS
1695 || (BTYPE(symtab->n_type) == T_STRUCT
1696 && symtab->n_sclass != C_STRTAG)
1697 || BTYPE(symtab->n_type) == T_UNION
1698 || BTYPE(symtab->n_type) == T_ENUM) {
1699 /* If the tagndx is 0 then the struct hasn't really been
1700 defined, so leave it alone */
1701
1702 if(((union internal_auxent *) (symtab + 1))->x_sym.x_tagndx != 0) {
1703 ((union internal_auxent *) (symtab + 1))->x_sym.x_tagndx += offset;
1704 }
1705
1706 } /* These guys have a tagndx */
1707 if (symtab->n_sclass == C_STRTAG
1708 || symtab->n_sclass == C_UNTAG
1709 || symtab->n_sclass == C_ENTAG
1710 || symtab->n_sclass == C_BLOCK
1711 || symtab->n_sclass == C_FCN
1712 || ISFCN(bfd_h_get_x(abfd, &symtab->n_type))) {
1713
1714 ((union internal_auxent *) (symtab +
1715 1))->x_sym.x_fcnary.x_fcn.x_endndx
1716 += offset;
1717
1718 } /* These guys have an endndx */
1719 #ifndef I960
1720 if (ISFCN(bfd_h_get_x(abfd,& symtab->n_type))) {
1721 ((union internal_auxent *) (symtab + 1))->x_sym.x_tvndx += offset;
1722 } /* These guys have a tvndx. I think...
1723 (FIXME) */
1724 #endif /* Not I960 */
1725
1726 } /* if value, else if aux */
1727 symtab += bfd_h_get_x(abfd, &symtab->n_numaux);
1728 } /* walk the symtab */
1729
1730 return;
1731 } /* offset_symbol_indices() */
1732
1733 #if 0
1734 /* swap the entire symbol table - we c*/
1735 static void
1736 swap_raw_symtab(abfd, raw_symtab)
1737 bfd *abfd;
1738 SYMENT *raw_symtab;
1739 {
1740 long i;
1741 SYMENT *end = raw_symtab + bfd_get_symcount(abfd);
1742 for (; raw_symtab < end; ++raw_symtab) {
1743 bfd_coff_swap_sym(abfd, raw_symtab);
1744
1745 for (i = raw_symtab->n_numaux; i; --i, ++raw_symtab) {
1746 bfd_coff_swap_aux(abfd,
1747 (AUXENT *)(raw_symtab + 1),
1748 raw_symtab->n_type,
1749 raw_symtab->n_sclass);
1750 } /* swap all the aux entries */
1751 } /* walk the symbol table */
1752
1753 return;
1754 } /* swap_raw_symtab() */
1755 #endif
1756 /*
1757 read a symbol table into freshly mallocated memory, swap it, and knit the
1758 symbol names into a normalized form. By normalized here I mean that all
1759 symbols have an n_offset pointer that points to a NULL terminated string.
1760 Oh, and the first symbol MUST be a C_FILE. If there wasn't one there
1761 before, put one there.
1762 */
1763
1764 static struct internal_syment *
1765 DEFUN(get_normalized_symtab,(abfd),
1766 bfd *abfd)
1767 {
1768
1769 struct internal_syment *internal;
1770 struct internal_syment *internal_ptr;
1771 struct internal_syment *internal_end;
1772 SYMENT *raw;
1773 SYMENT *raw_src;
1774 SYMENT *raw_end;
1775 char *string_table = NULL;
1776 unsigned long size;
1777 char string_table_size_buffer[4];
1778 unsigned long string_table_size = 0;
1779 unsigned int raw_size;
1780 if (obj_raw_syments(abfd) != (struct internal_syment *)NULL) {
1781 return obj_raw_syments(abfd);
1782 }
1783 if ((size = bfd_get_symcount(abfd) * sizeof(struct internal_syment)) == 0) {
1784 bfd_error = no_symbols;
1785 return (NULL);
1786 }
1787
1788 internal = (struct internal_syment *)bfd_alloc(abfd, size);
1789 internal_end = internal + bfd_get_symcount(abfd);
1790
1791 raw_size = bfd_get_symcount(abfd) * SYMESZ;
1792 raw = (SYMENT *)bfd_alloc(abfd,raw_size);
1793
1794 if (bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET) == -1
1795 || bfd_read((PTR)raw, raw_size, 1, abfd) != raw_size) {
1796 bfd_error = system_call_error;
1797 return (NULL);
1798 }
1799 /* mark the end of the symbols */
1800 raw_end = raw + bfd_get_symcount(abfd);
1801 /*
1802 FIXME SOMEDAY. A string table size of zero is very weird, but
1803 probably possible. If one shows up, it will probably kill us.
1804 */
1805
1806 /* Swap all the raw entries */
1807 for (raw_src = raw, internal_ptr = internal; raw_src < raw_end; raw_src++, internal_ptr++) {
1808 unsigned int i;
1809 bfd_coff_swap_sym_in(abfd, raw_src,internal_ptr);
1810 for (i = internal_ptr->n_numaux; i; --i, raw_src++, internal_ptr++) {
1811 bfd_coff_swap_aux_in(abfd, (AUXENT *)(raw_src +1), internal_ptr->n_type,
1812 internal_ptr->n_sclass, (union
1813 internal_auxent *)(internal_ptr +1));
1814 }
1815 }
1816
1817 /* Free all the raw stuff */
1818 bfd_release(abfd, raw_src);
1819
1820 for (internal_ptr = internal; internal_ptr < internal_end; internal_ptr ++) {
1821
1822 if (internal_ptr->_n._n_n._n_zeroes != 0) {
1823 /*
1824 This is a "short" name. Make it long.
1825 */
1826 unsigned long i = 0;
1827 char *newstring = NULL;
1828 /*
1829 find the length of this string without walking into memory
1830 that isn't ours.
1831 */
1832
1833 for (i = 0; i < 8; ++i) {
1834 if (internal_ptr->_n._n_name[i] == '\0') {
1835 break;
1836 } /* if end of string */
1837 } /* possible lengths of this string. */
1838
1839 if ((newstring = (PTR) bfd_alloc(abfd, ++i)) == NULL) {
1840 bfd_error = no_memory;
1841 return (NULL);
1842 } /* on error */
1843 bzero(newstring, i);
1844 strncpy(newstring, internal_ptr->_n._n_name, i-1);
1845 internal_ptr->_n._n_n._n_offset = (int) newstring;
1846 internal_ptr->_n._n_n._n_zeroes = 0;
1847
1848 }
1849 else {
1850 if (string_table == NULL) {
1851 /*
1852 NOTE: we don't read the string table until now because we
1853 don't necessarily know that we have one until now.
1854 */
1855 /*
1856 At this point we should be "seek"'d to the end of the
1857 symbols === the symbol table size.
1858 */
1859
1860 if (bfd_read((char *) &string_table_size_buffer,
1861 sizeof(string_table_size_buffer),
1862 1, abfd) != sizeof(string_table_size)) {
1863 bfd_error = system_call_error;
1864 return (NULL);
1865 } /* on error */
1866
1867 string_table_size = bfd_h_getlong(abfd, &string_table_size_buffer);
1868
1869 if ((string_table = (PTR) bfd_alloc(abfd, string_table_size -= 4)) == NULL) {
1870 bfd_error = no_memory;
1871 return (NULL);
1872 } /* on mallocation error */
1873 if (bfd_read(string_table, string_table_size, 1, abfd) != string_table_size) {
1874 bfd_error = system_call_error;
1875 return (NULL);
1876 } /* on error */
1877 } /* have not yet read the string table. */
1878 /*
1879 This is a long name already. Just point it at the string in
1880 memory.
1881 */
1882 internal_ptr->_n._n_n._n_offset = (int) (string_table - 4 + internal_ptr->_n._n_n._n_offset);
1883
1884 } /* switch on type of symbol name */
1885
1886 internal_ptr += internal_ptr->n_numaux;
1887 } /* for each symbol */
1888 #if 0
1889 #ifndef GNU960
1890 /* I'm not sure of the repercussions of this, so the Intel
1891 folks will always do the force
1892 */
1893 if (obj_symbol_slew(abfd) > 0)
1894 force_indices_file_symbol_relative(abfd, internal);
1895 #else
1896 force_indices_file_symbol_relative(abfd, internal);
1897 #endif
1898 #endif
1899 obj_raw_syments(abfd) = internal;
1900 obj_string_table(abfd) = string_table;
1901
1902 return (internal);
1903 } /* get_normalized_symtab() */
1904
1905 static
1906 struct sec *
1907 DEFUN(section_from_bfd_index,(abfd, index),
1908 bfd *abfd AND
1909 int index)
1910 {
1911 if (index > 0) {
1912 struct sec *answer = abfd->sections;
1913 while (--index) {
1914 answer = answer->next;
1915 }
1916 return answer;
1917 }
1918 return 0;
1919 }
1920
1921
1922
1923
1924 static boolean
1925 coff_slurp_line_table(abfd, asect)
1926 bfd *abfd;
1927 asection *asect;
1928 {
1929 LINENO *native_lineno;
1930 alent *lineno_cache;
1931
1932 BFD_ASSERT(asect->lineno == (alent *) NULL);
1933
1934 native_lineno = (LINENO *) buy_and_read(abfd,
1935 asect->line_filepos,
1936 SEEK_SET,
1937 (size_t) (LINESZ *
1938 asect->lineno_count));
1939 lineno_cache =
1940 (alent *) bfd_alloc(abfd, (size_t) ((asect->lineno_count + 1) * sizeof(alent)));
1941 if (lineno_cache == NULL) {
1942 bfd_error = no_memory;
1943 return false;
1944 } else {
1945 unsigned int counter = 0;
1946 alent *cache_ptr = lineno_cache;
1947 LINENO *src = native_lineno;
1948
1949 while (counter < asect->lineno_count) {
1950 struct internal_lineno dst;
1951 bfd_coff_swap_lineno_in(abfd, src, &dst);
1952 cache_ptr->line_number = dst.l_lnno;
1953
1954 if (cache_ptr->line_number == 0) {
1955 coff_symbol_type *sym =
1956 (coff_symbol_type *) (dst.l_addr.l_symndx
1957 + obj_symbol_slew(abfd)
1958 + obj_raw_syments(abfd))->_n._n_n._n_zeroes;
1959 cache_ptr->u.sym = (asymbol *) sym;
1960 sym->lineno = cache_ptr;
1961 }
1962 else {
1963 cache_ptr->u.offset = dst.l_addr.l_paddr
1964 - bfd_section_vma(abfd, asect);
1965 } /* If no linenumber expect a symbol index */
1966
1967 cache_ptr++;
1968 src++;
1969 counter++;
1970 }
1971 cache_ptr->line_number = 0;
1972
1973 }
1974 asect->lineno = lineno_cache;
1975 /* FIXME, free native_lineno here, or use alloca or something. */
1976 return true;
1977 } /* coff_slurp_line_table() */
1978
1979 static struct internal_syment *
1980 DEFUN(find_next_file_symbol,(abfd, current, end),
1981 bfd *abfd AND
1982 struct internal_syment *current AND
1983 struct internal_syment *end)
1984 {
1985 current += current->n_numaux + 1;
1986
1987 while (current < end) {
1988 if (current->n_sclass== C_FILE) {
1989 return (current);
1990 }
1991 current += current->n_numaux + 1;
1992 }
1993 return end;
1994 }
1995
1996
1997 /*
1998 Note that C_FILE symbols can, and some do, have more than 1 aux entry.
1999 */
2000
2001 static void
2002 DEFUN(force_indices_file_symbol_relative,(abfd, symtab),
2003 bfd *abfd AND
2004 struct internal_syment *symtab)
2005 {
2006 struct internal_syment *end = symtab + bfd_get_symcount(abfd);
2007 struct internal_syment *current;
2008 struct internal_syment *next;
2009 /* the first symbol had damn well better be a C_FILE. */
2010 BFD_ASSERT(symtab->n_sclass == C_FILE);
2011
2012 for (current = find_next_file_symbol(abfd, symtab, end);
2013 current < end;
2014 current = next) {
2015 offset_symbol_indices(abfd, current,
2016 ((next =
2017 find_next_file_symbol(abfd, current,
2018 end)) - current),
2019 symtab - current);
2020 }
2021 return;
2022 }
2023
2024 static boolean
2025 DEFUN(coff_slurp_symbol_table,(abfd),
2026 bfd *abfd)
2027 {
2028 struct internal_syment *native_symbols;
2029 coff_symbol_type *cached_area;
2030 unsigned int *table_ptr;
2031
2032 unsigned int number_of_symbols = 0;
2033 if (obj_symbols(abfd))
2034 return true;
2035 bfd_seek(abfd, obj_sym_filepos(abfd), SEEK_SET);
2036
2037 /* Read in the symbol table */
2038 if ((native_symbols = get_normalized_symtab(abfd)) == NULL) {
2039 return (false);
2040 } /* on error */
2041
2042
2043 /* Allocate enough room for all the symbols in cached form */
2044 cached_area =
2045 (coff_symbol_type *)
2046 bfd_alloc(abfd, (size_t) (bfd_get_symcount(abfd) * sizeof(coff_symbol_type)));
2047
2048 if (cached_area == NULL) {
2049 bfd_error = no_memory;
2050 return false;
2051 } /* on error */
2052 table_ptr =
2053 (unsigned int *)
2054 bfd_alloc(abfd, (size_t) (bfd_get_symcount(abfd) * sizeof(unsigned int)));
2055
2056 if (table_ptr == NULL) {
2057 bfd_error = no_memory;
2058 return false;
2059 } else {
2060 coff_symbol_type *dst = cached_area;
2061 unsigned int last_native_index = bfd_get_symcount(abfd);
2062 unsigned int this_index = 0;
2063 while (this_index < last_native_index) {
2064 struct internal_syment *src = native_symbols + this_index;
2065 table_ptr[this_index] = number_of_symbols;
2066 dst->symbol.the_bfd = abfd;
2067
2068 dst->symbol.name = (char *)(src->_n._n_n._n_offset);
2069 /*
2070 We use the native name field to point to the cached field
2071 */
2072 src->_n._n_n._n_zeroes = (int) dst;
2073 dst->symbol.section = section_from_bfd_index(abfd,
2074 src->n_scnum);
2075 switch (src->n_sclass) {
2076 #ifdef I960
2077 case C_LEAFEXT:
2078 #if 0
2079 dst->symbol.value = src->n_value - dst->symbol.section->vma;
2080 dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
2081 dst->symbol.flags |= BSF_NOT_AT_END;
2082 #endif
2083 /* Fall through to next case */
2084
2085 #endif
2086
2087 case C_EXT:
2088 if ((src->n_scnum) == 0) {
2089 if ((src->n_value) == 0) {
2090 dst->symbol.flags = BSF_UNDEFINED;
2091 dst->symbol.value= 0;
2092 }
2093 else {
2094 dst->symbol.flags = BSF_FORT_COMM;
2095 dst->symbol.value = (src->n_value);
2096 }
2097 }
2098 else {
2099 /*
2100 Base the value as an index from the base of the
2101 section
2102 */
2103 if (dst->symbol.section == (asection *) NULL) {
2104 dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL | BSF_ABSOLUTE;
2105 dst->symbol.value = src->n_value;
2106 }
2107 else {
2108 dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
2109 dst->symbol.value = src->n_value - dst->symbol.section->vma;
2110 }
2111 if (ISFCN((src->n_type))) {
2112 /*
2113 A function ext does not go at the end of a file
2114 */
2115 dst->symbol.flags |= BSF_NOT_AT_END;
2116 }
2117 }
2118
2119 break;
2120 case C_STAT: /* static */
2121 #ifdef I960
2122 case C_LEAFSTAT: /* static leaf procedure */
2123 #endif
2124 case C_LABEL: /* label */
2125 dst->symbol.flags = BSF_LOCAL;
2126 /*
2127 Base the value as an index from the base of the section
2128 */
2129 dst->symbol.value = (src->n_value) - dst->symbol.section->vma;
2130 break;
2131
2132 case C_MOS: /* member of structure */
2133 case C_EOS: /* end of structure */
2134 case C_REGPARM: /* register parameter */
2135 case C_REG: /* register variable */
2136 #ifdef C_AUTOARG
2137 case C_AUTOARG: /* 960-specific storage class */
2138 #endif
2139 case C_TPDEF: /* type definition */
2140
2141 case C_ARG:
2142 case C_AUTO: /* automatic variable */
2143 case C_FIELD: /* bit field */
2144 case C_ENTAG: /* enumeration tag */
2145 case C_MOE: /* member of enumeration */
2146 case C_MOU: /* member of union */
2147 case C_UNTAG: /* union tag */
2148
2149 dst->symbol.flags = BSF_DEBUGGING;
2150 dst->symbol.value = (src->n_value);
2151 break;
2152
2153 case C_FILE: /* file name */
2154 case C_STRTAG: /* structure tag */
2155 dst->symbol.flags = BSF_DEBUGGING;
2156 dst->symbol.value = (src->n_value);
2157
2158 break;
2159 case C_BLOCK: /* ".bb" or ".eb" */
2160 case C_FCN: /* ".bf" or ".ef" */
2161 dst->symbol.flags = BSF_LOCAL;
2162 /*
2163 Base the value as an index from the base of the section
2164 */
2165 dst->symbol.value = (src->n_value) - dst->symbol.section->vma;
2166
2167 break;
2168 case C_EFCN: /* physical end of function */
2169 case C_NULL:
2170 case C_EXTDEF: /* external definition */
2171 case C_ULABEL: /* undefined label */
2172 case C_USTATIC: /* undefined static */
2173 case C_LINE: /* line # reformatted as symbol table entry */
2174 case C_ALIAS: /* duplicate tag */
2175 case C_HIDDEN: /* ext symbol in dmert public lib */
2176
2177 default:
2178
2179 abort();
2180 dst->symbol.flags = BSF_DEBUGGING;
2181 dst->symbol.value = (src->n_value);
2182
2183 break;
2184 }
2185
2186 BFD_ASSERT(dst->symbol.flags != 0);
2187
2188 dst->native = src;
2189
2190 dst->symbol.udata = 0;
2191 dst->lineno = (alent *) NULL;
2192 this_index += (src->n_numaux) + 1;
2193 dst++;
2194 number_of_symbols++;
2195 } /* walk the native symtab */
2196 } /* bfdize the native symtab */
2197
2198 obj_symbols(abfd) = cached_area;
2199 obj_raw_syments(abfd) = native_symbols;
2200
2201 bfd_get_symcount(abfd) = number_of_symbols;
2202 obj_convert(abfd) = table_ptr;
2203 /* Slurp the line tables for each section too */
2204 {
2205 asection *p;
2206 p = abfd->sections;
2207 while (p) {
2208 coff_slurp_line_table(abfd, p);
2209 p = p->next;
2210 }
2211 }
2212 return true;
2213 } /* coff_slurp_symbol_table() */
2214
2215 static unsigned int
2216 coff_get_symtab_upper_bound(abfd)
2217 bfd *abfd;
2218 {
2219 if (!coff_slurp_symbol_table(abfd))
2220 return 0;
2221
2222 return (bfd_get_symcount(abfd) + 1) * (sizeof(coff_symbol_type *));
2223 }
2224
2225
2226 static unsigned int
2227 coff_get_symtab(abfd, alocation)
2228 bfd *abfd;
2229 asymbol **alocation;
2230 {
2231 unsigned int counter = 0;
2232 coff_symbol_type *symbase;
2233 coff_symbol_type **location = (coff_symbol_type **) (alocation);
2234 if (!coff_slurp_symbol_table(abfd))
2235 return 0;
2236
2237 for (symbase = obj_symbols(abfd); counter++ < bfd_get_symcount(abfd);)
2238 *(location++) = symbase++;
2239 *location++ = 0;
2240 return bfd_get_symcount(abfd);
2241 }
2242
2243 static unsigned int
2244 coff_get_reloc_upper_bound(abfd, asect)
2245 bfd *abfd;
2246 sec_ptr asect;
2247 {
2248 if (bfd_get_format(abfd) != bfd_object) {
2249 bfd_error = invalid_operation;
2250 return 0;
2251 }
2252 return (asect->reloc_count + 1) * sizeof(arelent *);
2253 }
2254
2255 static boolean
2256 DEFUN(coff_slurp_reloc_table,(abfd, asect, symbols),
2257 bfd *abfd AND
2258 sec_ptr asect AND
2259 asymbol **symbols)
2260 {
2261 RELOC *native_relocs;
2262 arelent *reloc_cache;
2263 if (asect->relocation)
2264 return true;
2265 if (asect->reloc_count == 0)
2266 return true;
2267 if (!coff_slurp_symbol_table(abfd))
2268 return false;
2269 native_relocs =
2270 (RELOC *) buy_and_read(abfd,
2271 asect->rel_filepos,
2272 SEEK_SET,
2273 (size_t) (RELSZ *
2274 asect->reloc_count));
2275 reloc_cache = (arelent *)
2276 bfd_alloc(abfd, (size_t) (asect->reloc_count * sizeof(arelent)));
2277
2278 if (reloc_cache == NULL) {
2279 bfd_error = no_memory;
2280 return false;
2281 } { /* on error */
2282 arelent *cache_ptr;
2283 RELOC *src;
2284 for (cache_ptr = reloc_cache,
2285 src = native_relocs;
2286 cache_ptr < reloc_cache + asect->reloc_count;
2287 cache_ptr++,
2288 src++) {
2289 struct internal_reloc dst;
2290 asymbol *ptr;
2291 swap_reloc_in(abfd, src, &dst);
2292 dst.r_symndx += obj_symbol_slew(abfd);
2293 cache_ptr->sym_ptr_ptr = symbols + obj_convert(abfd)[dst.r_symndx];
2294
2295 ptr = *(cache_ptr->sym_ptr_ptr);
2296 cache_ptr->address = dst.r_vaddr;
2297 /*
2298 The symbols definitions that we have read in have been
2299 relocated as if their sections started at 0. But the offsets
2300 refering to the symbols in the raw data have not been
2301 modified, so we have to have a negative addend to compensate.
2302
2303 Note that symbols which used to be common must be left alone
2304 */
2305
2306 if (ptr->the_bfd == abfd
2307 && ptr->section != (asection *) NULL
2308 && ((ptr->flags & BSF_OLD_COMMON)== 0))
2309 {
2310 cache_ptr->addend = -(ptr->section->vma + ptr->value);
2311 }
2312 else {
2313 cache_ptr->addend = 0;
2314 }
2315
2316 cache_ptr->address -= asect->vma;
2317
2318 cache_ptr->section = (asection *) NULL;
2319
2320 #if I960
2321 cache_ptr->howto = howto_table + dst.r_type;
2322 #endif
2323 #if M68
2324 cache_ptr->howto = howto_table + dst.r_type - R_RELBYTE;
2325 #endif
2326 #if M88
2327 if (dst.r_type >= R_PCR16L && dst.r_type <= R_VRT32) {
2328 cache_ptr->howto = howto_table + dst.r_type - R_PCR16L;
2329 cache_ptr->addend += dst.r_offset << 16;
2330 }
2331 else {
2332 BFD_ASSERT(0);
2333 }
2334 #endif
2335
2336 }
2337
2338 }
2339
2340 asect->relocation = reloc_cache;
2341 return true;
2342 }
2343
2344
2345 /* This is stupid. This function should be a boolean predicate */
2346 static unsigned int
2347 coff_canonicalize_reloc(abfd, section, relptr, symbols)
2348 bfd *abfd;
2349 sec_ptr section;
2350 arelent **relptr;
2351 asymbol **symbols;
2352 {
2353 arelent *tblptr = section->relocation;
2354 unsigned int count = 0;
2355 if (!(tblptr || coff_slurp_reloc_table(abfd, section, symbols)))
2356 return 0;
2357 tblptr = section->relocation;
2358 if (!tblptr)
2359 return 0;
2360
2361 for (; count++ < section->reloc_count;)
2362 *relptr++ = tblptr++;
2363
2364 *relptr = 0;
2365
2366 return section->reloc_count;
2367 }
2368
2369
2370 /*
2371 provided a bfd, a section and an offset into the section, calculate and
2372 return the name of the source file and the line nearest to the wanted
2373 location.
2374 */
2375
2376 static boolean
2377 DEFUN(coff_find_nearest_line,(abfd,
2378 section,
2379 symbols,
2380 offset,
2381 filename_ptr,
2382 functionname_ptr,
2383 line_ptr),
2384 bfd *abfd AND
2385 asection *section AND
2386 asymbol **symbols AND
2387 bfd_vma offset AND
2388 CONST char **filename_ptr AND
2389 CONST char **functionname_ptr AND
2390 unsigned int *line_ptr)
2391 {
2392 static bfd *cache_abfd;
2393 static asection *cache_section;
2394 static bfd_vma cache_offset;
2395 static unsigned int cache_i;
2396 static alent *cache_l;
2397
2398 unsigned int i = 0;
2399 struct icofdata *cof = obj_icof(abfd);
2400 /* Run through the raw syments if available */
2401 struct internal_syment *p;
2402 alent *l;
2403 unsigned int line_base = 0;
2404
2405
2406 *filename_ptr = 0;
2407 *functionname_ptr = 0;
2408 *line_ptr = 0;
2409
2410 /* Don't try and find line numbers in a non coff file */
2411 if (abfd->xvec->flavour != bfd_target_coff_flavour_enum)
2412 return false;
2413
2414 if (cof == (struct icofdata *)NULL)
2415 return false;
2416
2417 p = cof->raw_syments;
2418 /*
2419 I don't know for sure what's right, but this isn't it. First off, an
2420 object file may not have any C_FILE's in it. After
2421 get_normalized_symtab(), it should have at least 1, the one I put
2422 there, but otherwise, all bets are off. Point #2, the first C_FILE
2423 isn't necessarily the right C_FILE because any given object may have
2424 many. I think you'll have to track sections as they coelesce in order
2425 to find the C_STAT symbol for this section. Then you'll have to work
2426 backwards to find the previous C_FILE, or choke if you get to a C_STAT
2427 for the same kind of section. That will mean that the original object
2428 file didn't have a C_FILE. xoxorich.
2429 */
2430
2431 #ifdef WEREBEINGPEDANTIC
2432 return false;
2433 #endif
2434
2435 for (i = 0; i < cof->raw_syment_count; i++) {
2436 if (bfd_h_get_x(abfd, &p->n_sclass) == C_FILE) {
2437 /* File name is embeded in auxent */
2438 /*
2439 This isn't right. The fname should probably be normalized
2440 during get_normalized_symtab(). In any case, what was here
2441 wasn't right because a SYMENT.n_name isn't an
2442 AUXENT.x_file.x_fname. xoxorich.
2443 */
2444
2445 *filename_ptr = ((AUXENT *) (p + 1))->x_file.x_fname;
2446 break;
2447 }
2448 p += 1 + bfd_h_get_x(abfd, & p->n_numaux);
2449 }
2450 /* Now wander though the raw linenumbers of the section */
2451 /*
2452 If this is the same bfd as we were previously called with and this is
2453 the same section, and the offset we want is further down then we can
2454 prime the lookup loop
2455 */
2456 if (abfd == cache_abfd &&
2457 section == cache_section &&
2458 offset >= cache_offset) {
2459 i = cache_i;
2460 l = cache_l;
2461 }
2462 else {
2463 i = 0;
2464 l = section->lineno;
2465 }
2466
2467 for (; i < section->lineno_count; i++) {
2468 if (l->line_number == 0) {
2469 /* Get the symbol this line number points at */
2470 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2471 *functionname_ptr = coff->symbol.name;
2472 if (coff->native) {
2473 struct internal_syment *s = coff->native;
2474 s = s + 1 + s->n_numaux;
2475 /*
2476 S should now point to the .bf of the function
2477 */
2478 if (s->n_numaux) {
2479 /*
2480 The linenumber is stored in the auxent
2481 */
2482 union internal_auxent *a = (union internal_auxent *) (s + 1);
2483 line_base = bfd_h_get_x(abfd, &a->x_sym.x_misc.x_lnsz.x_lnno);
2484 }
2485 }
2486 }
2487 else {
2488 if (l->u.offset > offset)
2489 break;
2490 *line_ptr = l->line_number + line_base + 1;
2491 }
2492 l++;
2493 }
2494
2495 cache_abfd = abfd;
2496 cache_section = section;
2497 cache_offset = offset;
2498 cache_i = i;
2499 cache_l = l;
2500 return true;
2501 }
2502
2503 #ifdef GNU960
2504 file_ptr
2505 coff_sym_filepos(abfd)
2506 bfd *abfd;
2507 {
2508 return obj_sym_filepos(abfd);
2509 }
2510 #endif
2511
2512
2513 static int
2514 DEFUN(coff_sizeof_headers,(abfd, reloc),
2515 bfd *abfd AND
2516 boolean reloc)
2517 {
2518 size_t size;
2519
2520 if (reloc == false) {
2521 size = sizeof(struct filehdr) + sizeof(AOUTHDR);
2522 }
2523 else {
2524 size = sizeof(struct filehdr);
2525 }
2526
2527 size += abfd->section_count * SCNHSZ;
2528 return size;
2529 }
2530
2531
2532 #define coff_core_file_failing_command _bfd_dummy_core_file_failing_command
2533 #define coff_core_file_failing_signal _bfd_dummy_core_file_failing_signal
2534 #define coff_core_file_matches_executable_p _bfd_dummy_core_file_matches_executable_p
2535 #define coff_slurp_armap bfd_slurp_coff_armap
2536 #define coff_slurp_extended_name_table _bfd_slurp_extended_name_table
2537 #define coff_truncate_arname bfd_dont_truncate_arname
2538 #define coff_openr_next_archived_file bfd_generic_openr_next_archived_file
2539 #define coff_generic_stat_arch_elt bfd_generic_stat_arch_elt
2540 #define coff_get_section_contents bfd_generic_get_section_contents
This page took 0.084235 seconds and 4 git commands to generate.