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