* config/obj-coff.c: Minor formatting/stylistic changes, plus:
[binutils-gdb.git] / gas / config / obj-coff.c
1 /* coff object file format
2 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
4
5 This file is part of GAS.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GAS is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21 #include "as.h"
22
23 #include "obstack.h"
24
25 #ifndef BFD_ASSEMBLER
26
27 const short seg_N_TYPE[] =
28 { /* in: segT out: N_TYPE bits */
29 C_ABS_SECTION,
30 C_TEXT_SECTION,
31 C_DATA_SECTION,
32 C_BSS_SECTION,
33 C_UNDEF_SECTION, /* SEG_UNKNOWN */
34 C_UNDEF_SECTION, /* SEG_GOOF */
35 C_UNDEF_SECTION, /* SEG_EXPR */
36 C_DEBUG_SECTION, /* SEG_DEBUG */
37 C_NTV_SECTION, /* SEG_NTV */
38 C_PTV_SECTION, /* SEG_PTV */
39 C_REGISTER_SECTION, /* SEG_REGISTER */
40 };
41
42
43 /* Add 4 to the real value to get the index and compensate the negatives */
44
45 const segT N_TYPE_seg[32] =
46 {
47 SEG_PTV, /* C_PTV_SECTION == -4 */
48 SEG_NTV, /* C_NTV_SECTION == -3 */
49 SEG_DEBUG, /* C_DEBUG_SECTION == -2 */
50 SEG_ABSOLUTE, /* C_ABS_SECTION == -1 */
51 SEG_UNKNOWN, /* C_UNDEF_SECTION == 0 */
52 SEG_TEXT, /* C_TEXT_SECTION == 1 */
53 SEG_DATA, /* C_DATA_SECTION == 2 */
54 SEG_BSS, /* C_BSS_SECTION == 3 */
55 SEG_REGISTER, /* C_REGISTER_SECTION == 4 */
56 SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
57 SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
58 SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF
59 };
60 #endif
61
62 const char *s_get_name PARAMS ((symbolS * s));
63 static symbolS *tag_find_or_make PARAMS ((char *name));
64 static symbolS *tag_find PARAMS ((char *name));
65
66 #ifndef BFD_ASSEMBLER
67 #ifdef BFD_HEADERS
68 static void obj_coff_section_header_append PARAMS ((char **where, struct internal_scnhdr * header));
69 #else
70 static void obj_coff_section_header_append PARAMS ((char **where, SCNHDR * header));
71 #endif
72 #endif
73
74 static void obj_coff_def PARAMS ((int what));
75 static void obj_coff_dim PARAMS ((int));
76 static void obj_coff_endef PARAMS ((int));
77 static void obj_coff_line PARAMS ((int));
78 static void obj_coff_ln PARAMS ((int));
79 static void obj_coff_scl PARAMS ((int));
80 static void obj_coff_section PARAMS ((int));
81 static void obj_coff_size PARAMS ((int));
82 static void obj_coff_tag PARAMS ((int));
83 static void obj_coff_type PARAMS ((int));
84 static void obj_coff_val PARAMS ((int));
85 static void tag_init PARAMS ((void));
86 static void tag_insert PARAMS ((const char *name, symbolS * symbolP));
87
88 #ifdef BFD_ASSEMBLER
89 static void SA_SET_SYM_TAGNDX PARAMS ((symbolS *, symbolS *));
90 #endif
91
92 static struct hash_control *tag_hash;
93 static symbolS *def_symbol_in_progress;
94
95 #ifndef BFD_ASSEMBLER
96 static symbolS *dot_text_symbol;
97 static symbolS *dot_data_symbol;
98 static symbolS *dot_bss_symbol;
99 #endif
100
101 const pseudo_typeS obj_pseudo_table[] =
102 {
103 #ifndef IGNORE_DEBUG
104 {"def", obj_coff_def, 0},
105 {"dim", obj_coff_dim, 0},
106 {"endef", obj_coff_endef, 0},
107 {"line", obj_coff_line, 0},
108 {"ln", obj_coff_ln, 0},
109 {"appline", obj_coff_ln, 1},
110 {"scl", obj_coff_scl, 0},
111 {"size", obj_coff_size, 0},
112 {"tag", obj_coff_tag, 0},
113 {"type", obj_coff_type, 0},
114 {"val", obj_coff_val, 0},
115 #else
116 {"def", s_ignore, 0},
117 {"dim", s_ignore, 0},
118 {"endef", s_ignore, 0},
119 {"line", s_ignore, 0},
120 {"ln", s_ignore, 0},
121 {"scl", s_ignore, 0},
122 {"size", s_ignore, 0},
123 {"tag", s_ignore, 0},
124 {"type", s_ignore, 0},
125 {"val", s_ignore, 0},
126 #endif /* ignore debug */
127
128 #ifdef MANY_SECTIONS
129 { "section", obj_coff_section, 0 },
130 #endif
131
132 {"ident", s_ignore, 0}, /* we don't yet handle this. */
133
134 {"optim", s_ignore, 0}, /* For sun386i cc (?) */
135 /* other stuff */
136 {"ABORT", s_abort, 0},
137
138 {NULL} /* end sentinel */
139 }; /* obj_pseudo_table */
140
141 #ifdef BFD_ASSEMBLER
142 struct line_no {
143 struct line_no *next;
144 fragS *frag;
145 alent l;
146 };
147 #endif
148
149 #define GET_FILENAME_STRING(X) \
150 ((char*)(&((X)->sy_symbol.ost_auxent->x_file.x_n.x_offset))[1])
151
152 /* obj dependant output values */
153 #ifndef BFD_ASSEMBLER
154 #ifdef BFD_HEADERS
155 static struct internal_scnhdr bss_section_header;
156 struct internal_scnhdr data_section_header;
157 struct internal_scnhdr text_section_header;
158 #else
159 static SCNHDR bss_section_header;
160 SCNHDR data_section_header;
161 SCNHDR text_section_header;
162 #endif
163 #endif
164
165 #ifdef BFD_ASSEMBLER
166
167 /* @@ Ick. */
168 static segT
169 fetch_coff_debug_section ()
170 {
171 static segT debug_section;
172 if (!debug_section)
173 {
174 CONST asymbol *s;
175 s = bfd_make_debug_symbol (stdoutput, (char *) 0, 0);
176 assert (s != 0);
177 debug_section = s->section;
178 }
179 return debug_section;
180 }
181
182 void
183 SA_SET_SYM_ENDNDX (sym, val)
184 symbolS *sym;
185 symbolS *val;
186 {
187 combined_entry_type *entry, *p;
188
189 entry = &coffsymbol (sym->bsym)->native[1];
190 p = coffsymbol (val->bsym)->native;
191 entry->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = p;
192 entry->fix_end = 1;
193 }
194
195 static void
196 SA_SET_SYM_TAGNDX (sym, val)
197 symbolS *sym;
198 symbolS *val;
199 {
200 combined_entry_type *entry, *p;
201
202 entry = &coffsymbol (sym->bsym)->native[1];
203 p = coffsymbol (val->bsym)->native;
204 entry->u.auxent.x_sym.x_tagndx.p = p;
205 entry->fix_tag = 1;
206 }
207
208 static int
209 S_GET_DATA_TYPE (sym)
210 symbolS *sym;
211 {
212 return coffsymbol (sym->bsym)->native->u.syment.n_type;
213 }
214
215 int
216 S_SET_DATA_TYPE (sym, val)
217 symbolS *sym;
218 int val;
219 {
220 coffsymbol (sym->bsym)->native->u.syment.n_type = val;
221 return val;
222 }
223
224 int
225 S_GET_STORAGE_CLASS (sym)
226 symbolS *sym;
227 {
228 return coffsymbol (sym->bsym)->native->u.syment.n_sclass;
229 }
230
231 int
232 S_SET_STORAGE_CLASS (sym, val)
233 symbolS *sym;
234 int val;
235 {
236 coffsymbol (sym->bsym)->native->u.syment.n_sclass = val;
237 return val;
238 }
239
240 #else /* ! BFD_ASSEMBLER */
241
242 /* Relocation. */
243
244 static int
245 reloc_compare (p1, p2)
246 #ifdef BFD_HEADERS
247 struct internal_reloc *p1, *p2;
248 #else
249 RELOC *p1, *p2;
250 #endif
251 {
252 return (int) (p1->r_vaddr - p2->r_vaddr);
253 }
254
255 /*
256 * emit_relocations()
257 *
258 * Crawl along a fixS chain. Emit the segment's relocations.
259 */
260
261 void
262 obj_emit_relocations (where, fixP, segment_address_in_file)
263 char **where;
264 fixS *fixP; /* Fixup chain for this segment. */
265 relax_addressT segment_address_in_file;
266 {
267 #ifdef BFD_HEADERS
268 struct internal_reloc *ri_table;
269 #else
270 RELOC *ri_table;
271 #endif
272 #ifdef TC_I960
273 char *callj_table;
274 #endif
275 symbolS *symbolP;
276 int i, count;
277 fixS *p;
278
279 for (count = 0, p = fixP; p; p = p->fx_next)
280 if (!p->fx_done)
281 count++;
282 if (!count)
283 return;
284
285 #ifdef BFD_HEADERS
286 ri_table = (struct internal_reloc *) calloc (sizeof (*ri_table), count);
287 #else
288 ri_table = (RELOC *) calloc (sizeof (*ri_table), count);
289 #endif
290 if (!ri_table)
291 as_fatal ("obj_emit_relocations: Could not malloc relocation table");
292
293 #ifdef TC_I960
294 callj_table = (char *) malloc (sizeof (char) * count);
295 if (!callj_table)
296 as_fatal ("obj_emit_relocations: Could not malloc callj table");
297 #endif
298
299 for (i = 0; fixP; fixP = fixP->fx_next)
300 {
301 symbolP = fixP->fx_addsy;
302 if (!fixP->fx_done)
303 {
304 int rtype_ok = 0;
305 #if defined(TC_M68K)
306 ri_table[i].r_type = (fixP->fx_pcrel ?
307 (fixP->fx_size == 1 ? R_PCRBYTE :
308 fixP->fx_size == 2 ? R_PCRWORD :
309 R_PCRLONG) :
310 (fixP->fx_size == 1 ? R_RELBYTE :
311 fixP->fx_size == 2 ? R_RELWORD :
312 R_RELLONG));
313 rtype_ok = 1;
314 #endif
315 #if defined(TC_I386)
316 /* FIXME-SOON R_OFF8 & R_DIR16 are a vague guess, completly
317 untested. */
318 ri_table[i].r_type = (fixP->fx_pcrel ?
319 (fixP->fx_size == 1 ? R_PCRBYTE :
320 fixP->fx_size == 2 ? R_PCRWORD :
321 R_PCRLONG) :
322 (fixP->fx_size == 1 ? R_OFF8 :
323 fixP->fx_size == 2 ? R_DIR16 :
324 R_DIR32));
325 rtype_ok = 1;
326 #endif
327 #if defined(TC_I960)
328 ri_table[i].r_type = (fixP->fx_pcrel
329 ? R_IPRMED
330 : R_RELLONG);
331 callj_table[i] = fixP->fx_tcbit ? 1 : 0;
332 rtype_ok = 1;
333 #endif
334 #if defined(TC_A29K)
335 ri_table[i].r_type = tc_coff_fix2rtype (fixP);
336 rtype_ok = 1;
337 #endif
338 if (!rtype_ok)
339 abort ();
340 ri_table[i].r_vaddr = (fixP->fx_frag->fr_address
341 + fixP->fx_where);
342 /* If symbol associated to relocation entry is a bss symbol
343 or undefined symbol just remember the index of the symbol.
344 Otherwise store the index of the symbol describing the
345 section the symbol belong to. This heuristic speeds up ld.
346 */
347 /* Local symbols can generate relocation information. In case
348 of structure return for instance. But they have no symbol
349 number because they won't be emitted in the final object.
350 In the case where they are in the BSS section, this leads
351 to an incorrect r_symndx.
352 Under bsd the loader do not care if the symbol reference
353 is incorrect. But the SYS V ld complains about this. To
354 avoid this we associate the symbol to the associated
355 section, *even* if it is the BSS section. */
356 /* If someone can tell me why the other symbols of the bss
357 section are not associated with the .bss section entry,
358 I'd be gratefull. I guess that it has to do with the special
359 nature of the .bss section. Or maybe this is because the
360 bss symbols are declared in the common section and can
361 be resized later. Can it break code some where ? */
362 ri_table[i].r_symndx = (S_GET_SEGMENT (symbolP) == SEG_TEXT
363 ? dot_text_symbol->sy_number
364 : (S_GET_SEGMENT (symbolP) == SEG_DATA
365 ? dot_data_symbol->sy_number
366 : ((SF_GET_LOCAL (symbolP)
367 ? dot_bss_symbol->sy_number
368 : symbolP->sy_number)))); /* bss or undefined */
369
370 /* md_ri_to_chars((char *) &ri, ri); *//* Last step : write md f */
371
372 i++;
373 } /* if there's a symbol */
374 } /* for each fixP */
375
376 /* AIX ld prefer to have the reloc table with r_vaddr sorted.
377 But sorting it should not hurt any other ld. */
378 qsort (ri_table, count, sizeof (*ri_table), reloc_compare);
379
380 for (i = 0; i < count; i++)
381 {
382 #ifdef BFD_HEADERS
383 *where += bfd_coff_swap_reloc_out (stdoutput, &ri_table[i], *where);
384 # ifdef TC_A29K
385 /* The 29k has a special kludge for the high 16 bit reloc.
386 Two relocations are emmited, R_IHIHALF, and R_IHCONST.
387 The second one doesn't contain a symbol, but uses the
388 value for offset */
389 if (ri_table[i].r_type == R_IHIHALF)
390 {
391 /* now emit the second bit */
392 ri_table[i].r_type = R_IHCONST;
393 ri_table[i].r_symndx = fixP->fx_addnumber;
394 *where += bfd_coff_swap_reloc_out (stdoutput, &ri_table[i],
395 *where);
396 }
397 # endif /* TC_A29K */
398
399 #else /* not BFD_HEADERS */
400 append (where, (char *) &ri_table[i], RELSZ);
401 #endif /* not BFD_HEADERS */
402
403 #ifdef TC_I960
404 if (callj_table[i])
405 {
406 ri_table[i].r_type = R_OPTCALL;
407 # ifdef BFD_HEADERS
408 *where += bfd_coff_swap_reloc_out (stdoutput, &ri_table[i],
409 *where);
410 # else
411 append (where, (char *) &ri_table[i], (unsigned long) RELSZ);
412 # endif /* BFD_HEADERS */
413 } /* if it's a callj, do it again for the opcode */
414 #endif /* TC_I960 */
415 }
416
417 free (ri_table);
418 #ifdef TC_I960
419 free (callj_table);
420 #endif
421 }
422
423 /* Coff file generation & utilities */
424
425 #ifdef BFD_HEADERS
426 void
427 obj_header_append (where, headers)
428 char **where;
429 object_headers *headers;
430 {
431 tc_headers_hook (headers);
432 *where += bfd_coff_swap_filehdr_out (stdoutput, &(headers->filehdr), *where);
433 #ifndef OBJ_COFF_OMIT_OPTIONAL_HEADER
434 *where += bfd_coff_swap_aouthdr_out (stdoutput, &(headers->aouthdr), *where);
435 #endif
436 obj_coff_section_header_append (where, &text_section_header);
437 obj_coff_section_header_append (where, &data_section_header);
438 obj_coff_section_header_append (where, &bss_section_header);
439 }
440
441 #else /* ! BFD_HEADERS */
442
443 void
444 obj_header_append (where, headers)
445 char **where;
446 object_headers *headers;
447 {
448 tc_headers_hook (headers);
449
450 #ifdef CROSS_COMPILE
451 /* Eventually swap bytes for cross compilation for file header */
452 md_number_to_chars (*where, headers->filehdr.f_magic, sizeof (headers->filehdr.f_magic));
453 *where += sizeof (headers->filehdr.f_magic);
454 md_number_to_chars (*where, headers->filehdr.f_nscns, sizeof (headers->filehdr.f_nscns));
455 *where += sizeof (headers->filehdr.f_nscns);
456 md_number_to_chars (*where, headers->filehdr.f_timdat, sizeof (headers->filehdr.f_timdat));
457 *where += sizeof (headers->filehdr.f_timdat);
458 md_number_to_chars (*where, headers->filehdr.f_symptr, sizeof (headers->filehdr.f_symptr));
459 *where += sizeof (headers->filehdr.f_symptr);
460 md_number_to_chars (*where, headers->filehdr.f_nsyms, sizeof (headers->filehdr.f_nsyms));
461 *where += sizeof (headers->filehdr.f_nsyms);
462 md_number_to_chars (*where, headers->filehdr.f_opthdr, sizeof (headers->filehdr.f_opthdr));
463 *where += sizeof (headers->filehdr.f_opthdr);
464 md_number_to_chars (*where, headers->filehdr.f_flags, sizeof (headers->filehdr.f_flags));
465 *where += sizeof (headers->filehdr.f_flags);
466
467 #ifndef OBJ_COFF_OMIT_OPTIONAL_HEADER
468 /* Eventually swap bytes for cross compilation for a.out header */
469 md_number_to_chars (*where, headers->aouthdr.magic, sizeof (headers->aouthdr.magic));
470 *where += sizeof (headers->aouthdr.magic);
471 md_number_to_chars (*where, headers->aouthdr.vstamp, sizeof (headers->aouthdr.vstamp));
472 *where += sizeof (headers->aouthdr.vstamp);
473 md_number_to_chars (*where, headers->aouthdr.tsize, sizeof (headers->aouthdr.tsize));
474 *where += sizeof (headers->aouthdr.tsize);
475 md_number_to_chars (*where, headers->aouthdr.dsize, sizeof (headers->aouthdr.dsize));
476 *where += sizeof (headers->aouthdr.dsize);
477 md_number_to_chars (*where, headers->aouthdr.bsize, sizeof (headers->aouthdr.bsize));
478 *where += sizeof (headers->aouthdr.bsize);
479 md_number_to_chars (*where, headers->aouthdr.entry, sizeof (headers->aouthdr.entry));
480 *where += sizeof (headers->aouthdr.entry);
481 md_number_to_chars (*where, headers->aouthdr.text_start, sizeof (headers->aouthdr.text_start));
482 *where += sizeof (headers->aouthdr.text_start);
483 md_number_to_chars (*where, headers->aouthdr.data_start, sizeof (headers->aouthdr.data_start));
484 *where += sizeof (headers->aouthdr.data_start);
485 md_number_to_chars (*where, headers->aouthdr.tagentries, sizeof (headers->aouthdr.tagentries));
486 *where += sizeof (headers->aouthdr.tagentries);
487 #endif /* OBJ_COFF_OMIT_OPTIONAL_HEADER */
488
489 #else /* CROSS_COMPILE */
490
491 append (where, (char *) &headers->filehdr, sizeof (headers->filehdr));
492 #ifndef OBJ_COFF_OMIT_OPTIONAL_HEADER
493 append (where, (char *) &headers->aouthdr, sizeof (headers->aouthdr));
494 #endif /* OBJ_COFF_OMIT_OPTIONAL_HEADER */
495
496 #endif /* CROSS_COMPILE */
497
498 /* Output the section headers */
499 obj_coff_section_header_append (where, &text_section_header);
500 obj_coff_section_header_append (where, &data_section_header);
501 obj_coff_section_header_append (where, &bss_section_header);
502 }
503
504 #endif /* ! BFD_HEADERS */
505
506 void
507 obj_symbol_to_chars (where, symbolP)
508 char **where;
509 symbolS *symbolP;
510 {
511 /* Move the value into the COFF symbol itself. */
512 symbolP->sy_symbol.ost_entry.n_value = S_GET_VALUE (symbolP);
513
514 #ifdef BFD_HEADERS
515 unsigned int numaux = symbolP->sy_symbol.ost_entry.n_numaux;
516 unsigned int i;
517
518 if (S_GET_SEGMENT (symbolP) == SEG_REGISTER)
519 {
520 S_SET_SEGMENT (symbolP, SEG_ABSOLUTE);
521 }
522 *where += bfd_coff_swap_sym_out (stdoutput, &symbolP->sy_symbol.ost_entry,
523 *where);
524
525 for (i = 0; i < numaux; i++)
526 {
527 *where += bfd_coff_swap_aux_out (stdoutput,
528 &symbolP->sy_symbol.ost_auxent[i],
529 S_GET_DATA_TYPE (symbolP),
530 S_GET_STORAGE_CLASS (symbolP),
531 i, numaux, *where);
532 }
533
534 #else /* BFD_HEADERS */
535 {
536 SYMENT *syment = &symbolP->sy_symbol.ost_entry;
537 int i;
538 char numaux = syment->n_numaux;
539 unsigned short type = S_GET_DATA_TYPE (symbolP);
540
541 #ifdef CROSS_COMPILE
542 md_number_to_chars (*where, syment->n_value, sizeof (syment->n_value));
543 *where += sizeof (syment->n_value);
544 md_number_to_chars (*where, 0xffff & syment->n_scnum,
545 sizeof (syment->n_scnum));
546 *where += sizeof (syment->n_scnum);
547 md_number_to_chars (*where, 0, sizeof (short)); /* pad n_flags */
548 *where += sizeof (short);
549 md_number_to_chars (*where, syment->n_type, sizeof (syment->n_type));
550 *where += sizeof (syment->n_type);
551 md_number_to_chars (*where, syment->n_sclass, sizeof (syment->n_sclass));
552 *where += sizeof (syment->n_sclass);
553 md_number_to_chars (*where, syment->n_numaux, sizeof (syment->n_numaux));
554 *where += sizeof (syment->n_numaux);
555 #else /* CROSS_COMPILE */
556 append (where, (char *) syment, sizeof (*syment));
557 #endif /* CROSS_COMPILE */
558
559 /* Should do the following:
560 if (.file entry) MD(..)... else if (static entry) MD(..) */
561 if (numaux > OBJ_COFF_MAX_AUXENTRIES)
562 as_bad ("Internal error? too many auxents for symbol");
563
564 for (i = 0; i < numaux; ++i)
565 {
566 #ifdef CROSS_COMPILE
567 this code does not work;
568
569 foo *auxP = SYM_AUXENT (symbolP);
570 /* The most common case, x_sym entry. */
571 if ((SF_GET (symbolP) & SF_STATICS) == 0)
572 {
573 md_number_to_chars (*where, auxP->x_sym.x_tagndx,
574 sizeof (auxP->x_sym.x_tagndx));
575 *where += sizeof (auxP->x_sym.x_tagndx);
576 if (ISFCN (type))
577 {
578 md_number_to_chars (*where, auxP->x_sym.x_misc.x_fsize,
579 sizeof (auxP->x_sym.x_misc.x_fsize));
580 *where += sizeof (auxP->x_sym.x_misc.x_fsize);
581 }
582 else
583 {
584 md_number_to_chars (*where, auxP->x_sym.x_misc.x_lnno,
585 sizeof (auxP->x_sym.x_misc.x_lnno));
586 *where += sizeof (auxP->x_sym.x_misc.x_lnno);
587 md_number_to_chars (*where, auxP->x_sym.x_misc.x_size,
588 sizeof (auxP->x_sym.x_misc.x_size));
589 *where += sizeof (auxP->x_sym.x_misc.x_size);
590 }
591 if (ISARY (type))
592 {
593 int index;
594 for (index = 0; index < DIMNUM; index++)
595 md_number_to_chars (*where, auxP->x_sym.x_fcnary.x_ary.x_dimen[index],
596 sizeof (auxP->x_sym.x_fcnary.x_ary.x_dimen[index]));
597 *where += sizeof (auxP->x_sym.x_fcnary.x_ary.x_dimen[index]);
598 }
599 else
600 {
601 md_number_to_chars (*where,
602 auxP->x_sym.x_fcnary.x_fcn.x_lnnoptr,
603 sizeof (auxP->x_sym.x_fcnary.x_fcn.x_lnnoptr));
604 *where += sizeof (auxP->x_sym.x_fcnary.x_fcn.x_lnnoptr);
605 md_number_to_chars (*where,
606 auxP->x_sym.x_fcnary.x_fcn.x_endndx,
607 sizeof (auxP->x_sym.x_fcnary.x_fcn.x_endndx));
608 *where += sizeof (auxP->x_sym.x_fcnary.x_fcn.x_endndx);
609 }
610 md_number_to_chars (*where, auxP->x_sym.x_tvndx,
611 sizeof (auxP->x_sym.x_tvndx));
612 *where += sizeof (auxP->x_sym.x_tvndx);
613 }
614 else if (S_GET_STORAGE_CLASS (symbolP) == C_FILE)
615 /* .file */
616 ;
617 else if (SF_GET_STATICS (symbolP))
618 { /* .text, .data, .bss symbols */
619 md_number_to_chars (*where, auxP->x_scn.x_scnlen,
620 sizeof (auxP->x_scn.x_scnlen));
621 *where += sizeof (auxP->x_scn.x_scnlen);
622 md_number_to_chars (*where, auxP->x_scn.x_nreloc,
623 sizeof (auxP->x_scn.x_nreloc));
624 *where += sizeof (auxP->x_scn.x_nreloc);
625 md_number_to_chars (*where, auxP->x_scn.x_nlinno,
626 sizeof (auxP->x_scn.x_nlinno));
627 *where += sizeof (auxP->x_scn.x_nlinno);
628 }
629 #else /* CROSS_COMPILE */
630 append (where, (char *) &symbolP->sy_symbol.ost_auxent[i],
631 sizeof (symbolP->sy_symbol.ost_auxent[i]));
632 #endif /* CROSS_COMPILE */
633 }
634 }
635 #endif /* BFD_HEADERS */
636 }
637
638 #ifdef BFD_HEADERS
639 static void
640 obj_coff_section_header_append (where, header)
641 char **where;
642 struct internal_scnhdr *header;
643 {
644 *where += bfd_coff_swap_scnhdr_out (stdoutput, header, *where);
645 }
646
647 #else
648 static void
649 obj_coff_section_header_append (where, header)
650 char **where;
651 SCNHDR *header;
652 {
653 #ifdef CROSS_COMPILE
654 memcpy (*where, header->s_name, sizeof (header->s_name));
655 *where += sizeof (header->s_name);
656
657 md_number_to_chars (*where, header->s_paddr, sizeof (header->s_paddr));
658 *where += sizeof (header->s_paddr);
659
660 md_number_to_chars (*where, header->s_vaddr, sizeof (header->s_vaddr));
661 *where += sizeof (header->s_vaddr);
662
663 md_number_to_chars (*where, header->s_size, sizeof (header->s_size));
664 *where += sizeof (header->s_size);
665
666 md_number_to_chars (*where, header->s_scnptr, sizeof (header->s_scnptr));
667 *where += sizeof (header->s_scnptr);
668
669 md_number_to_chars (*where, header->s_relptr, sizeof (header->s_relptr));
670 *where += sizeof (header->s_relptr);
671
672 md_number_to_chars (*where, header->s_lnnoptr, sizeof (header->s_lnnoptr));
673 *where += sizeof (header->s_lnnoptr);
674
675 md_number_to_chars (*where, header->s_nreloc, sizeof (header->s_nreloc));
676 *where += sizeof (header->s_nreloc);
677
678 md_number_to_chars (*where, header->s_nlnno, sizeof (header->s_nlnno));
679 *where += sizeof (header->s_nlnno);
680
681 md_number_to_chars (*where, header->s_flags, sizeof (header->s_flags));
682 *where += sizeof (header->s_flags);
683
684 #ifdef TC_I960
685 md_number_to_chars (*where, header->s_align, sizeof (header->s_align));
686 *where += sizeof (header->s_align);
687 #endif /* TC_I960 */
688
689 #else /* CROSS_COMPILE */
690
691 append (where, (char *) header, sizeof (*header));
692
693 #endif /* CROSS_COMPILE */
694 }
695
696 #endif
697 void
698 obj_emit_symbols (where, symbol_rootP)
699 char **where;
700 symbolS *symbol_rootP;
701 {
702 symbolS *symbolP;
703 /*
704 * Emit all symbols left in the symbol chain.
705 */
706 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
707 {
708 /* Used to save the offset of the name. It is used to point
709 to the string in memory but must be a file offset. */
710 register char *temp;
711
712 tc_coff_symbol_emit_hook (symbolP);
713
714 temp = S_GET_NAME (symbolP);
715 if (SF_GET_STRING (symbolP))
716 {
717 S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
718 S_SET_ZEROES (symbolP, 0);
719 }
720 else
721 {
722 memset (symbolP->sy_symbol.ost_entry.n_name, '\0', SYMNMLEN);
723 strncpy (symbolP->sy_symbol.ost_entry.n_name, temp, SYMNMLEN);
724 }
725 obj_symbol_to_chars (where, symbolP);
726 S_SET_NAME (symbolP, temp);
727 }
728 } /* obj_emit_symbols() */
729
730 #endif /* ! BFD_ASSEMBLER */
731
732 /* Merge a debug symbol containing debug information into a normal symbol. */
733
734 void
735 c_symbol_merge (debug, normal)
736 symbolS *debug;
737 symbolS *normal;
738 {
739 S_SET_DATA_TYPE (normal, S_GET_DATA_TYPE (debug));
740 S_SET_STORAGE_CLASS (normal, S_GET_STORAGE_CLASS (debug));
741
742 if (S_GET_NUMBER_AUXILIARY (debug) > S_GET_NUMBER_AUXILIARY (normal))
743 /* take the most we have */
744 S_SET_NUMBER_AUXILIARY (normal, S_GET_NUMBER_AUXILIARY (debug));
745
746 if (S_GET_NUMBER_AUXILIARY (debug) > 0)
747 {
748 /* Move all the auxiliary information. */
749 #ifdef BFD_ASSEMBLER
750 /* @@ How many fields do we want to preserve? Would it make more
751 sense to pick and choose those we want to copy? Should look
752 into this further.... [raeburn:19920512.2209EST] */
753 alent *linenos;
754 linenos = coffsymbol (normal->bsym)->lineno;
755 memcpy ((char *) &coffsymbol (normal->bsym)->native,
756 (char *) &coffsymbol (debug->bsym)->native,
757 S_GET_NUMBER_AUXILIARY(debug) * AUXESZ);
758 coffsymbol (normal->bsym)->lineno = linenos;
759 #else
760 memcpy ((char *) &normal->sy_symbol.ost_auxent[0],
761 (char *) &debug->sy_symbol.ost_auxent[0],
762 S_GET_NUMBER_AUXILIARY (debug) * AUXESZ);
763 #endif
764 }
765
766 /* Move the debug flags. */
767 SF_SET_DEBUG_FIELD (normal, SF_GET_DEBUG_FIELD (debug));
768 }
769
770 static symbolS *previous_file_symbol;
771 void
772 c_dot_file_symbol (filename)
773 char *filename;
774 {
775 symbolS *symbolP;
776
777 #ifdef BFD_ASSEMBLER
778 symbolP = symbol_new (filename, &bfd_abs_section, 0,
779 &zero_address_frag);
780 #else
781 symbolP = symbol_new (".file",
782 SEG_DEBUG,
783 0,
784 &zero_address_frag);
785 #endif
786
787 S_SET_STORAGE_CLASS (symbolP, C_FILE);
788 S_SET_NUMBER_AUXILIARY (symbolP, 1);
789
790 #ifdef BFD_ASSEMBLER
791 symbolP->bsym->flags = BSF_DEBUGGING;
792 #else
793 if (strlen(filename) > 14)
794 {
795 /* This won't fit into a 14 char space, it will go into the string
796 table. */
797 symbolP->sy_symbol.ost_auxent->x_file.x_n.x_zeroes = 0;
798 (&(symbolP->sy_symbol.ost_auxent->x_file.x_n.x_offset))[0] = string_byte_count;
799 (&(symbolP->sy_symbol.ost_auxent->x_file.x_n.x_offset))[1] = (int)filename;
800 }
801 else
802 {
803 SA_SET_FILE_FNAME (symbolP, filename);
804 }
805 SF_SET_DEBUG (symbolP);
806 #endif
807
808 #ifndef NO_LISTING
809 {
810 extern int listing;
811 if (listing)
812 {
813 listing_source_file (filename);
814 }
815 }
816 #endif
817
818 S_SET_VALUE (symbolP, (long) previous_file_symbol);
819
820 previous_file_symbol = symbolP;
821
822 /* Make sure that the symbol is first on the symbol chain */
823 if (symbol_rootP != symbolP)
824 {
825 if (symbolP == symbol_lastP)
826 {
827 symbol_lastP = symbol_lastP->sy_previous;
828 } /* if it was the last thing on the list */
829
830 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
831 symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP);
832 symbol_rootP = symbolP;
833 } /* if not first on the list */
834 }
835
836 /*
837 * Build a 'section static' symbol.
838 */
839
840 char *
841 c_section_symbol (name, value, length, nreloc, nlnno)
842 char *name;
843 long value;
844 long length;
845 unsigned short nreloc;
846 unsigned short nlnno;
847 {
848 symbolS *symbolP;
849
850 symbolP = symbol_new (name,
851 (name[1] == 't'
852 ? text_section
853 : name[1] == 'd'
854 ? data_section
855 : bss_section),
856 value,
857 &zero_address_frag);
858
859 S_SET_STORAGE_CLASS (symbolP, C_STAT);
860 S_SET_NUMBER_AUXILIARY (symbolP, 1);
861
862 SA_SET_SCN_SCNLEN (symbolP, length);
863 SA_SET_SCN_NRELOC (symbolP, nreloc);
864 SA_SET_SCN_NLINNO (symbolP, nlnno);
865
866 SF_SET_STATICS (symbolP);
867
868 return (char *) symbolP;
869 }
870
871 #ifndef BFD_ASSEMBLER
872 void
873 c_section_header (header,
874 name,
875 core_address,
876 size,
877 data_ptr,
878 reloc_ptr,
879 lineno_ptr,
880 reloc_number,
881 lineno_number,
882 alignment)
883 #ifdef BFD_HEADERS
884 struct internal_scnhdr *header;
885 #else
886 SCNHDR *header;
887 #endif
888 char *name;
889 long core_address;
890 long size;
891 long data_ptr;
892 long reloc_ptr;
893 long lineno_ptr;
894 long reloc_number;
895 long lineno_number;
896 long alignment;
897 {
898 strncpy (header->s_name, name, 8);
899 header->s_paddr = header->s_vaddr = core_address;
900 header->s_scnptr = ((header->s_size = size) != 0) ? data_ptr : 0;
901 header->s_relptr = reloc_ptr;
902 header->s_lnnoptr = lineno_ptr;
903 header->s_nreloc = reloc_number;
904 header->s_nlnno = lineno_number;
905
906 #ifdef OBJ_COFF_SECTION_HEADER_HAS_ALIGNMENT
907 #ifdef OBJ_COFF_BROKEN_ALIGNMENT
908 header->s_align = ((name[1] == 'b' || (size > 0)) ? 16 : 0);
909 #else
910 header->s_align = ((alignment == 0)
911 ? 0
912 : (1 << alignment));
913 #endif /* OBJ_COFF_BROKEN_ALIGNMENT */
914 #endif /* OBJ_COFF_SECTION_HEADER_HAS_ALIGNMENT */
915
916 header->s_flags = STYP_REG | (name[1] == 't'
917 ? STYP_TEXT
918 : name[1] == 'd'
919 ? STYP_DATA
920 : name[1] == 'b'
921 ? STYP_BSS
922 : STYP_INFO);
923 }
924 #endif
925
926 /* Line number handling */
927
928 int coff_line_base;
929
930 #ifdef BFD_ASSEMBLER
931
932 /* Symbol of last function, which we should hang line#s off of. */
933 static symbolS *line_fsym;
934
935 #define in_function() (line_fsym != 0)
936 #define clear_function() (line_fsym = 0)
937 #define set_function(F) (line_fsym = (F), coff_add_linesym (F))
938
939 #else
940
941 /* Offset in line#s where the last function started (the odd entry for
942 line #0). */
943 static int function_lineoff = -1;
944
945 #define in_function() (function_lineoff >= 0)
946 #define clear_function() (function_lineoff = -1)
947 #define set_function(F) (function_lineoff = c_line_new ((long) (F), 0, &zero_address_frag))
948
949 int text_lineno_number;
950
951 /* We use this to build pointers from .bf's into the linetable. It
952 should match exactly the values that are later assigned in
953 text_lineno_number by write.c. */
954 int our_lineno_number;
955
956 lineno *lineno_rootP;
957 lineno *lineno_lastP;
958
959 int
960 c_line_new (paddr, line_number, frag)
961 long paddr;
962 unsigned short line_number;
963 fragS *frag;
964 {
965 lineno *new_line = (lineno *) xmalloc (sizeof (lineno));
966
967 new_line->line.l_addr.l_paddr = paddr;
968 new_line->line.l_lnno = line_number;
969 new_line->frag = (char *) frag;
970 new_line->next = (lineno *) 0;
971
972 if (lineno_rootP == (lineno *) 0)
973 lineno_rootP = new_line;
974 else
975 lineno_lastP->next = new_line;
976 lineno_lastP = new_line;
977 return LINESZ * our_lineno_number++;
978 }
979
980 void
981 obj_emit_lineno (where, line, file_start)
982 char **where;
983 lineno *line;
984 char *file_start;
985 {
986 #ifdef BFD_HEADERS
987 struct bfd_internal_lineno *line_entry;
988 #else
989 LINENO *line_entry;
990 #endif
991 char *where2 = *where;
992
993 for (; line; line = line->next)
994 {
995 line_entry = &line->line;
996
997 /* FIXME-SOMEDAY Resolving the sy_number of function linno's used to be
998 done in write_object_file() but their symbols need a fileptr to the
999 lnno, so I moved this resolution check here. xoxorich. */
1000
1001 if (line_entry->l_lnno == 0)
1002 {
1003 /* There is a good chance that the symbol pointed to
1004 is not the one that will be emitted and that the
1005 sy_number is not accurate. */
1006 symbolS *symbolP;
1007
1008 symbolP = (symbolS *) line_entry->l_addr.l_symndx;
1009
1010 line_entry->l_addr.l_symndx = symbolP->sy_number;
1011 symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_fcn.x_lnnoptr = where2 - file_start;
1012 }
1013 #ifdef BFD_HEADERS
1014 where2 += bfd_coff_swap_lineno_out (stdoutput, line_entry, where2);
1015 #else
1016 /* No matter which member of the union we process, they are
1017 both long. */
1018 md_number_to_chars (where2, line_entry->l_addr.l_paddr, sizeof (line_entry->l_addr.l_paddr));
1019 where2 += sizeof (line_entry->l_addr.l_paddr);
1020
1021 md_number_to_chars (where2, line_entry->l_lnno, sizeof (line_entry->l_lnno));
1022 where2 += sizeof (line_entry->l_lnno);
1023
1024 #ifdef TC_I960
1025 *where2++ = '0';
1026 *where2++ = '0';
1027 #endif /* TC_I960 */
1028
1029 #endif /* BFD_HEADERS */
1030 }
1031 *where = where2;
1032 }
1033
1034 #endif /* ! BFD_ASSEMBLER */
1035
1036 \f
1037 void
1038 obj_symbol_new_hook (symbolP)
1039 symbolS *symbolP;
1040 {
1041 char underscore = 0; /* Symbol has leading _ */
1042
1043 #ifdef BFD_ASSEMBLER
1044 {
1045 long sz = (OBJ_COFF_MAX_AUXENTRIES + 1) * sizeof (combined_entry_type);
1046 char *s = (char *) bfd_alloc_by_size_t (stdoutput, sz);
1047 memset (s, 0, sz);
1048 coffsymbol (symbolP->bsym)->native = (combined_entry_type *) s;
1049 }
1050 #else
1051 /* Effective symbol */
1052 /* Store the pointer in the offset. */
1053 S_SET_ZEROES (symbolP, 0L);
1054 /* Additional information */
1055 symbolP->sy_symbol.ost_flags = 0;
1056 /* Auxiliary entries */
1057 memset ((char *) &symbolP->sy_symbol.ost_auxent[0], '\0', AUXESZ);
1058 #endif
1059 S_SET_DATA_TYPE (symbolP, T_NULL);
1060 S_SET_STORAGE_CLASS (symbolP, 0);
1061 S_SET_NUMBER_AUXILIARY (symbolP, 0);
1062
1063 #ifdef STRIP_UNDERSCORE
1064 /* Remove leading underscore at the beginning of the symbol.
1065 This is to be compatible with the standard librairies. */
1066 if (*S_GET_NAME (symbolP) == '_')
1067 {
1068 underscore = 1;
1069 S_SET_NAME (symbolP, S_GET_NAME (symbolP) + 1);
1070 }
1071 #endif /* STRIP_UNDERSCORE */
1072
1073 if (S_IS_STRING (symbolP))
1074 SF_SET_STRING (symbolP);
1075 if (!underscore && S_IS_LOCAL (symbolP))
1076 SF_SET_LOCAL (symbolP);
1077 }
1078
1079 \f
1080 /* stack stuff */
1081 stack *
1082 stack_init (chunk_size, element_size)
1083 unsigned long chunk_size;
1084 unsigned long element_size;
1085 {
1086 stack *st;
1087
1088 st = (stack *) malloc (sizeof (stack));
1089 if (!st)
1090 return 0;
1091 st->data = malloc (chunk_size);
1092 if (!st->data)
1093 {
1094 free (st);
1095 return 0;
1096 }
1097 st->pointer = 0;
1098 st->size = chunk_size;
1099 st->chunk_size = chunk_size;
1100 st->element_size = element_size;
1101 return st;
1102 }
1103
1104 void
1105 stack_delete (st)
1106 stack *st;
1107 {
1108 free (st->data);
1109 free (st);
1110 }
1111
1112 char *
1113 stack_push (st, element)
1114 stack *st;
1115 char *element;
1116 {
1117 if (st->pointer + st->element_size >= st->size)
1118 {
1119 st->size += st->chunk_size;
1120 if ((st->data = xrealloc (st->data, st->size)) == (char *) 0)
1121 return (char *) 0;
1122 }
1123 memcpy (st->data + st->pointer, element, st->element_size);
1124 st->pointer += st->element_size;
1125 return st->data + st->pointer;
1126 }
1127
1128 char *
1129 stack_pop (st)
1130 stack *st;
1131 {
1132 if (st->pointer < st->element_size)
1133 {
1134 st->pointer = 0;
1135 return (char *) 0;
1136 }
1137 st->pointer -= st->element_size;
1138 return st->data + st->pointer;
1139 }
1140
1141 char *
1142 stack_top (st)
1143 stack *st;
1144 {
1145 return st->data + st->pointer - st->element_size;
1146 }
1147
1148
1149 /*
1150 * Handle .ln directives.
1151 */
1152
1153 #ifdef BFD_ASSEMBLER
1154 static symbolS *current_lineno_sym;
1155 static struct line_no *line_nos;
1156
1157 static void
1158 add_lineno (frag, offset, num)
1159 fragS *frag;
1160 int offset;
1161 int num;
1162 {
1163 struct line_no *new_line = (struct line_no *) bfd_alloc_by_size_t (stdoutput,
1164 sizeof (struct line_no));
1165 if (!current_lineno_sym)
1166 {
1167 abort ();
1168 }
1169 new_line->next = line_nos;
1170 new_line->frag = frag;
1171 new_line->l.line_number = num;
1172 new_line->l.u.offset = offset;
1173 line_nos = new_line;
1174 }
1175
1176 void
1177 coff_add_linesym (sym)
1178 symbolS *sym;
1179 {
1180 if (line_nos)
1181 {
1182 add_lineno (0, 0, 0);
1183 coffsymbol (current_lineno_sym->bsym)->lineno = (alent *) line_nos;
1184 line_nos = 0;
1185 }
1186 current_lineno_sym = sym;
1187 }
1188 #endif
1189
1190 static void
1191 obj_coff_ln (appline)
1192 int appline;
1193 {
1194 int l;
1195
1196 if (! appline && def_symbol_in_progress != NULL)
1197 {
1198 as_warn (".ln pseudo-op inside .def/.endef: ignored.");
1199 demand_empty_rest_of_line ();
1200 return;
1201 }
1202
1203 l = get_absolute_expression ();
1204 if (!appline)
1205 {
1206 #ifdef BFD_ASSEMBLER
1207 add_lineno (frag_now, frag_now_fix (), l);
1208 #else
1209 c_line_new (frag_now_fix (), l, frag_now);
1210 #endif
1211 }
1212
1213 #ifndef NO_LISTING
1214 {
1215 extern int listing;
1216
1217 if (listing)
1218 {
1219 if (! appline)
1220 l += coff_line_base - 1;
1221 listing_source_line (l);
1222 }
1223 }
1224 #endif
1225
1226 demand_empty_rest_of_line ();
1227 }
1228
1229 /*
1230 * def()
1231 *
1232 * Handle .def directives.
1233 *
1234 * One might ask : why can't we symbol_new if the symbol does not
1235 * already exist and fill it with debug information. Because of
1236 * the C_EFCN special symbol. It would clobber the value of the
1237 * function symbol before we have a chance to notice that it is
1238 * a C_EFCN. And a second reason is that the code is more clear this
1239 * way. (at least I think it is :-).
1240 *
1241 */
1242
1243 #define SKIP_SEMI_COLON() while (*input_line_pointer++ != ';')
1244 #define SKIP_WHITESPACES() while (*input_line_pointer == ' ' || \
1245 *input_line_pointer == '\t') \
1246 input_line_pointer++;
1247
1248 static void
1249 obj_coff_def (what)
1250 int what;
1251 {
1252 char name_end; /* Char after the end of name */
1253 char *symbol_name; /* Name of the debug symbol */
1254 char *symbol_name_copy; /* Temporary copy of the name */
1255 unsigned int symbol_name_length;
1256
1257 if (def_symbol_in_progress != NULL)
1258 {
1259 as_warn (".def pseudo-op used inside of .def/.endef: ignored.");
1260 demand_empty_rest_of_line ();
1261 return;
1262 } /* if not inside .def/.endef */
1263
1264 SKIP_WHITESPACES ();
1265
1266 symbol_name = input_line_pointer;
1267 #ifdef STRIP_UNDERSCORE
1268 if (symbol_name[0] == '_' && symbol_name[1] != 0)
1269 symbol_name++;
1270 #endif /* STRIP_UNDERSCORE */
1271
1272 name_end = get_symbol_end ();
1273 symbol_name_length = strlen (symbol_name);
1274 symbol_name_copy = xmalloc (symbol_name_length + 1);
1275 strcpy (symbol_name_copy, symbol_name);
1276
1277 /* Initialize the new symbol */
1278 #ifdef BFD_ASSEMBLER
1279 def_symbol_in_progress = symbol_make (symbol_name_copy);
1280 #else
1281 def_symbol_in_progress = (symbolS *) obstack_alloc (&notes, sizeof (*def_symbol_in_progress));
1282 memset (def_symbol_in_progress, '\0', sizeof (*def_symbol_in_progress));
1283
1284 S_SET_NAME (def_symbol_in_progress, symbol_name_copy);
1285 def_symbol_in_progress->sy_name_offset = ~0;
1286 def_symbol_in_progress->sy_number = ~0;
1287 #endif
1288
1289 def_symbol_in_progress->sy_frag = &zero_address_frag;
1290 S_SET_VALUE (def_symbol_in_progress, 0);
1291
1292 if (S_IS_STRING (def_symbol_in_progress))
1293 SF_SET_STRING (def_symbol_in_progress);
1294
1295 *input_line_pointer = name_end;
1296
1297 demand_empty_rest_of_line ();
1298 }
1299
1300 unsigned int dim_index;
1301
1302 static void
1303 obj_coff_endef (ignored)
1304 int ignored;
1305 {
1306 symbolS *symbolP;
1307 /* DIM BUG FIX sac@cygnus.com */
1308 dim_index = 0;
1309 if (def_symbol_in_progress == NULL)
1310 {
1311 as_warn (".endef pseudo-op used outside of .def/.endef: ignored.");
1312 demand_empty_rest_of_line ();
1313 return;
1314 } /* if not inside .def/.endef */
1315
1316 /* Set the section number according to storage class. */
1317 switch (S_GET_STORAGE_CLASS (def_symbol_in_progress))
1318 {
1319 case C_STRTAG:
1320 case C_ENTAG:
1321 case C_UNTAG:
1322 SF_SET_TAG (def_symbol_in_progress);
1323 /* intentional fallthrough */
1324 case C_FILE:
1325 case C_TPDEF:
1326 SF_SET_DEBUG (def_symbol_in_progress);
1327 #ifdef BFD_ASSEMBLER
1328 S_SET_SEGMENT (def_symbol_in_progress, fetch_coff_debug_section ());
1329 #else
1330 S_SET_SEGMENT (def_symbol_in_progress, SEG_DEBUG);
1331 #endif
1332 break;
1333
1334 case C_EFCN:
1335 SF_SET_LOCAL (def_symbol_in_progress); /* Do not emit this symbol. */
1336 /* intentional fallthrough */
1337 case C_BLOCK:
1338 SF_SET_PROCESS (def_symbol_in_progress); /* Will need processing before writing */
1339 /* intentional fallthrough */
1340 case C_FCN:
1341 {
1342 CONST char *name;
1343 S_SET_SEGMENT (def_symbol_in_progress, text_section);
1344
1345 #ifdef BFD_ASSEMBLER
1346 name = bfd_asymbol_name (def_symbol_in_progress->bsym);
1347 #else
1348 name = def_symbol_in_progress->sy_symbol.ost_entry._n._n_nptr[1];
1349 #endif
1350 if (name[1] == 'b' && name[2] == 'f')
1351 {
1352 if (! in_function ())
1353 as_warn ("`%s' symbol without preceding function", name);
1354 #ifdef BFD_ASSEMBLER
1355 /* SA_SET_SYM_LNNO (def_symbol_in_progress, 12345);*/
1356 #else
1357 SA_GET_SYM_LNNOPTR (def_symbol_in_progress) = function_lineoff;
1358 #endif
1359 /* Will need relocating */
1360 SF_SET_PROCESS (def_symbol_in_progress);
1361 clear_function ();
1362 }
1363 }
1364 break;
1365
1366 #ifdef C_AUTOARG
1367 case C_AUTOARG:
1368 #endif /* C_AUTOARG */
1369 case C_AUTO:
1370 case C_REG:
1371 case C_MOS:
1372 case C_MOE:
1373 case C_MOU:
1374 case C_ARG:
1375 case C_REGPARM:
1376 case C_FIELD:
1377 case C_EOS:
1378 SF_SET_DEBUG (def_symbol_in_progress);
1379 S_SET_SEGMENT (def_symbol_in_progress, absolute_section);
1380 break;
1381
1382 case C_EXT:
1383 case C_STAT:
1384 case C_LABEL:
1385 /* Valid but set somewhere else (s_comm, s_lcomm, colon) */
1386 break;
1387
1388 case C_USTATIC:
1389 case C_EXTDEF:
1390 case C_ULABEL:
1391 as_warn ("unexpected storage class %d",
1392 S_GET_STORAGE_CLASS (def_symbol_in_progress));
1393 break;
1394 } /* switch on storage class */
1395
1396 /* Now that we have built a debug symbol, try to find if we should
1397 merge with an existing symbol or not. If a symbol is C_EFCN or
1398 SEG_ABSOLUTE or untagged SEG_DEBUG it never merges. */
1399
1400 /* Two cases for functions. Either debug followed by definition or
1401 definition followed by debug. For definition first, we will
1402 merge the debug symbol into the definition. For debug first, the
1403 lineno entry MUST point to the definition function or else it
1404 will point off into space when obj_crawl_symbol_chain() merges
1405 the debug symbol into the real symbol. Therefor, let's presume
1406 the debug symbol is a real function reference. */
1407
1408 /* FIXME-SOON If for some reason the definition label/symbol is
1409 never seen, this will probably leave an undefined symbol at link
1410 time. */
1411
1412 if (S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_EFCN
1413 #ifdef BFD_ASSEMBLER
1414 || (!strcmp (bfd_get_section_name (stdoutput,
1415 S_GET_SEGMENT (def_symbol_in_progress)),
1416 "*DEBUG*")
1417 && !SF_GET_TAG (def_symbol_in_progress))
1418 #else
1419 || (S_GET_SEGMENT (def_symbol_in_progress) == SEG_DEBUG
1420 && !SF_GET_TAG (def_symbol_in_progress))
1421 #endif
1422 || S_GET_SEGMENT (def_symbol_in_progress) == absolute_section
1423 || (symbolP = symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP)) == NULL)
1424 {
1425 #ifdef BFD_ASSEMBLER
1426 if (def_symbol_in_progress != symbol_lastP)
1427 symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP,
1428 &symbol_lastP);
1429 #else
1430 symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP,
1431 &symbol_lastP);
1432 #endif
1433 }
1434 else
1435 {
1436 /* This symbol already exists, merge the newly created symbol
1437 into the old one. This is not mandatory. The linker can
1438 handle duplicate symbols correctly. But I guess that it save
1439 a *lot* of space if the assembly file defines a lot of
1440 symbols. [loic] */
1441
1442 /* The debug entry (def_symbol_in_progress) is merged into the
1443 previous definition. */
1444
1445 c_symbol_merge (def_symbol_in_progress, symbolP);
1446 /* FIXME-SOON Should *def_symbol_in_progress be free'd? xoxorich. */
1447 def_symbol_in_progress = symbolP;
1448
1449 if (SF_GET_FUNCTION (def_symbol_in_progress)
1450 || SF_GET_TAG (def_symbol_in_progress))
1451 {
1452 /* For functions, and tags, the symbol *must* be where the
1453 debug symbol appears. Move the existing symbol to the
1454 current place. */
1455 /* If it already is at the end of the symbol list, do nothing */
1456 if (def_symbol_in_progress != symbol_lastP)
1457 {
1458 symbol_remove (def_symbol_in_progress, &symbol_rootP, &symbol_lastP);
1459 symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP, &symbol_lastP);
1460 }
1461 }
1462 }
1463
1464 if (SF_GET_TAG (def_symbol_in_progress)
1465 && symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP) == NULL)
1466 {
1467 tag_insert (S_GET_NAME (def_symbol_in_progress), def_symbol_in_progress);
1468 }
1469
1470 if (SF_GET_FUNCTION (def_symbol_in_progress))
1471 {
1472 know (sizeof (def_symbol_in_progress) <= sizeof (long));
1473 set_function (def_symbol_in_progress);
1474 SF_SET_PROCESS (def_symbol_in_progress);
1475
1476 if (symbolP == NULL)
1477 {
1478 /* That is, if this is the first time we've seen the
1479 function... */
1480 symbol_table_insert (def_symbol_in_progress);
1481 } /* definition follows debug */
1482 } /* Create the line number entry pointing to the function being defined */
1483
1484 def_symbol_in_progress = NULL;
1485 demand_empty_rest_of_line ();
1486 }
1487
1488 static void
1489 obj_coff_dim (ignored)
1490 int ignored;
1491 {
1492 int dim_index;
1493
1494 if (def_symbol_in_progress == NULL)
1495 {
1496 as_warn (".dim pseudo-op used outside of .def/.endef: ignored.");
1497 demand_empty_rest_of_line ();
1498 return;
1499 } /* if not inside .def/.endef */
1500
1501 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
1502
1503 for (dim_index = 0; dim_index < DIMNUM; dim_index++)
1504 {
1505 SKIP_WHITESPACES ();
1506 SA_SET_SYM_DIMEN (def_symbol_in_progress, dim_index,
1507 get_absolute_expression ());
1508
1509 switch (*input_line_pointer)
1510 {
1511 case ',':
1512 input_line_pointer++;
1513 break;
1514
1515 default:
1516 as_warn ("badly formed .dim directive ignored");
1517 /* intentional fallthrough */
1518 case '\n':
1519 case ';':
1520 dim_index = DIMNUM;
1521 break;
1522 }
1523 }
1524
1525 demand_empty_rest_of_line ();
1526 }
1527
1528 static void
1529 obj_coff_line (ignored)
1530 int ignored;
1531 {
1532 int this_base;
1533
1534 if (def_symbol_in_progress == NULL)
1535 {
1536 /* Probably stabs-style line? */
1537 obj_coff_ln (0);
1538 return;
1539 }
1540
1541 this_base = get_absolute_expression ();
1542 if (this_base > coff_line_base)
1543 coff_line_base = this_base;
1544
1545 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
1546 SA_SET_SYM_LNNO (def_symbol_in_progress, coff_line_base);
1547
1548 demand_empty_rest_of_line ();
1549 }
1550
1551 static void
1552 obj_coff_size (ignored)
1553 int ignored;
1554 {
1555 if (def_symbol_in_progress == NULL)
1556 {
1557 as_warn (".size pseudo-op used outside of .def/.endef ignored.");
1558 demand_empty_rest_of_line ();
1559 return;
1560 } /* if not inside .def/.endef */
1561
1562 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
1563 SA_SET_SYM_SIZE (def_symbol_in_progress, get_absolute_expression ());
1564 demand_empty_rest_of_line ();
1565 }
1566
1567 static void
1568 obj_coff_scl (ignored)
1569 int ignored;
1570 {
1571 if (def_symbol_in_progress == NULL)
1572 {
1573 as_warn (".scl pseudo-op used outside of .def/.endef ignored.");
1574 demand_empty_rest_of_line ();
1575 return;
1576 } /* if not inside .def/.endef */
1577
1578 S_SET_STORAGE_CLASS (def_symbol_in_progress, get_absolute_expression ());
1579 demand_empty_rest_of_line ();
1580 }
1581
1582 static void
1583 obj_coff_tag (ignored)
1584 int ignored;
1585 {
1586 char *symbol_name;
1587 char name_end;
1588
1589 if (def_symbol_in_progress == NULL)
1590 {
1591 as_warn (".tag pseudo-op used outside of .def/.endef ignored.");
1592 demand_empty_rest_of_line ();
1593 return;
1594 }
1595
1596 S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1);
1597 symbol_name = input_line_pointer;
1598 name_end = get_symbol_end ();
1599
1600 /* Assume that the symbol referred to by .tag is always defined.
1601 This was a bad assumption. I've added find_or_make. xoxorich. */
1602 SA_SET_SYM_TAGNDX (def_symbol_in_progress,
1603 tag_find_or_make (symbol_name));
1604 if (SA_GET_SYM_TAGNDX (def_symbol_in_progress) == 0L)
1605 {
1606 as_warn ("tag not found for .tag %s", symbol_name);
1607 } /* not defined */
1608
1609 SF_SET_TAGGED (def_symbol_in_progress);
1610 *input_line_pointer = name_end;
1611
1612 demand_empty_rest_of_line ();
1613 }
1614
1615 static void
1616 obj_coff_type (ignored)
1617 int ignored;
1618 {
1619 if (def_symbol_in_progress == NULL)
1620 {
1621 as_warn (".type pseudo-op used outside of .def/.endef ignored.");
1622 demand_empty_rest_of_line ();
1623 return;
1624 } /* if not inside .def/.endef */
1625
1626 S_SET_DATA_TYPE (def_symbol_in_progress, get_absolute_expression ());
1627
1628 if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress)) &&
1629 S_GET_STORAGE_CLASS (def_symbol_in_progress) != C_TPDEF)
1630 {
1631 SF_SET_FUNCTION (def_symbol_in_progress);
1632 } /* is a function */
1633
1634 demand_empty_rest_of_line ();
1635 }
1636
1637 static void
1638 obj_coff_val (ignored)
1639 int ignored;
1640 {
1641 if (def_symbol_in_progress == NULL)
1642 {
1643 as_warn (".val pseudo-op used outside of .def/.endef ignored.");
1644 demand_empty_rest_of_line ();
1645 return;
1646 } /* if not inside .def/.endef */
1647
1648 if (is_name_beginner (*input_line_pointer))
1649 {
1650 char *symbol_name = input_line_pointer;
1651 char name_end = get_symbol_end ();
1652
1653 if (!strcmp (symbol_name, "."))
1654 {
1655 def_symbol_in_progress->sy_frag = frag_now;
1656 S_SET_VALUE (def_symbol_in_progress, obstack_next_free (&frags) - frag_now->fr_literal);
1657 /* If the .val is != from the .def (e.g. statics) */
1658 }
1659 else if (strcmp (S_GET_NAME (def_symbol_in_progress), symbol_name))
1660 {
1661 def_symbol_in_progress->sy_value.X_op = O_symbol;
1662 def_symbol_in_progress->sy_value.X_add_symbol =
1663 symbol_find_or_make (symbol_name);
1664 def_symbol_in_progress->sy_value.X_op_symbol = NULL;
1665 def_symbol_in_progress->sy_value.X_add_number = 0;
1666
1667 /* If the segment is undefined when the forward reference is
1668 resolved, then copy the segment id from the forward
1669 symbol. */
1670 SF_SET_GET_SEGMENT (def_symbol_in_progress);
1671 }
1672 /* Otherwise, it is the name of a non debug symbol and its value will be calculated later. */
1673 *input_line_pointer = name_end;
1674 }
1675 else
1676 {
1677 S_SET_VALUE (def_symbol_in_progress, get_absolute_expression ());
1678 } /* if symbol based */
1679
1680 demand_empty_rest_of_line ();
1681 }
1682
1683 /*
1684 * Maintain a list of the tagnames of the structres.
1685 */
1686
1687 static void
1688 tag_init ()
1689 {
1690 tag_hash = hash_new ();
1691 }
1692
1693 static void
1694 tag_insert (name, symbolP)
1695 const char *name;
1696 symbolS *symbolP;
1697 {
1698 const char *error_string;
1699
1700 if ((error_string = hash_jam (tag_hash, name, (char *) symbolP)))
1701 {
1702 as_fatal ("Inserting \"%s\" into structure table failed: %s",
1703 name, error_string);
1704 }
1705 }
1706
1707 static symbolS *
1708 tag_find_or_make (name)
1709 char *name;
1710 {
1711 symbolS *symbolP;
1712
1713 if ((symbolP = tag_find (name)) == NULL)
1714 {
1715 symbolP = symbol_new (name, undefined_section,
1716 0, &zero_address_frag);
1717
1718 tag_insert (S_GET_NAME (symbolP), symbolP);
1719 symbol_table_insert (symbolP);
1720 } /* not found */
1721
1722 return symbolP;
1723 }
1724
1725 static symbolS *
1726 tag_find (name)
1727 char *name;
1728 {
1729 #ifdef STRIP_UNDERSCORE
1730 if (*name == '_')
1731 name++;
1732 #endif /* STRIP_UNDERSCORE */
1733 return (symbolS *) hash_find (tag_hash, name);
1734 }
1735
1736 void
1737 obj_read_begin_hook ()
1738 {
1739 /* These had better be the same. Usually 18 bytes. */
1740 #ifndef BFD_HEADERS
1741 know (sizeof (SYMENT) == sizeof (AUXENT));
1742 know (SYMESZ == AUXESZ);
1743 #endif
1744 tag_init ();
1745 }
1746
1747 #ifndef BFD_ASSEMBLER
1748 void
1749 obj_crawl_symbol_chain (headers)
1750 object_headers *headers;
1751 {
1752 int symbol_number = 0;
1753 lineno *lineP;
1754 symbolS *last_functionP = NULL;
1755 symbolS *last_tagP;
1756 symbolS *symbolP;
1757 symbolS *symbol_externP = NULL;
1758 symbolS *symbol_extern_lastP = NULL;
1759
1760 /* Initialize the stack used to keep track of the matching .bb .be */
1761 stack *block_stack = stack_init (512, sizeof (symbolS *));
1762
1763 tc_crawl_symbol_chain (headers);
1764
1765 /* The symbol list should be ordered according to the following sequence
1766 * order :
1767 * . .file symbol
1768 * . debug entries for functions
1769 * . fake symbols for .text .data and .bss
1770 * . defined symbols
1771 * . undefined symbols
1772 * But this is not mandatory. The only important point is to put the
1773 * undefined symbols at the end of the list.
1774 */
1775
1776 if (symbol_rootP == NULL
1777 || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE)
1778 {
1779 know (!previous_file_symbol);
1780 c_dot_file_symbol ("fake");
1781 } /* Is there a .file symbol? If not, insert one at the beginning. */
1782
1783 /*
1784 * Build up static symbols for .text, .data and .bss
1785 */
1786 dot_text_symbol = (symbolS *) c_section_symbol (".text",
1787 0,
1788 H_GET_TEXT_SIZE (headers),
1789 0 /*text_relocation_number*/,
1790 0 /*text_lineno_number */);
1791 #ifdef TE_I386AIX
1792 symbol_remove (dot_text_symbol, &symbol_rootP, &symbol_lastP);
1793 symbol_append (dot_text_symbol, previous_file_symbol,
1794 &symbol_rootP, &symbol_lastP);
1795 #endif /* TE_I386AIX */
1796
1797 dot_data_symbol = (symbolS *)
1798 c_section_symbol (".data",
1799 H_GET_TEXT_SIZE (headers),
1800 H_GET_DATA_SIZE (headers),
1801 0 /*data_relocation_number */ ,
1802 0); /* There are no data lineno entries */
1803 #ifdef TE_I386AIX
1804 symbol_remove (dot_data_symbol, &symbol_rootP, &symbol_lastP);
1805 symbol_append (dot_data_symbol, dot_text_symbol,
1806 &symbol_rootP, &symbol_lastP);
1807 #endif /* TE_I386AIX */
1808
1809 dot_bss_symbol = (symbolS *)
1810 c_section_symbol (".bss",
1811 H_GET_TEXT_SIZE (headers) + H_GET_DATA_SIZE (headers),
1812 H_GET_BSS_SIZE (headers),
1813 0, /* No relocation for a bss section. */
1814 0); /* There are no bss lineno entries */
1815 #ifdef TE_I386AIX
1816 symbol_remove (dot_bss_symbol, &symbol_rootP, &symbol_lastP);
1817 symbol_append (dot_bss_symbol, dot_data_symbol,
1818 &symbol_rootP, &symbol_lastP);
1819 #endif /* TE_I386AIX */
1820
1821 #if defined(DEBUG)
1822 verify_symbol_chain (symbol_rootP, symbol_lastP);
1823 #endif /* DEBUG */
1824
1825 /* Three traversals of symbol chains here. The
1826 first traversal yanks externals into a temporary
1827 chain, removing the externals from the global
1828 chain, numbers symbols, and does some other guck.
1829 The second traversal is on the temporary chain of
1830 externals and just appends them to the global
1831 chain again, numbering them as we go. The third
1832 traversal patches pointers to symbols (using sym
1833 indexes). The last traversal was once done as
1834 part of the first pass, but that fails when a
1835 reference preceeds a definition as the definition
1836 has no number at the time we process the
1837 reference. */
1838
1839 /* Note that symbolP will be NULL at the end of a loop
1840 if an external was at the beginning of the list (it
1841 gets moved off the list). Hence the weird check in
1842 the loop control.
1843 */
1844 for (symbolP = symbol_rootP;
1845 symbolP;
1846 symbolP = symbolP ? symbol_next (symbolP) : symbol_rootP)
1847 {
1848 if (!SF_GET_DEBUG (symbolP))
1849 {
1850 /* Debug symbols do not need all this rubbish */
1851 symbolS *real_symbolP;
1852
1853 /* L* and C_EFCN symbols never merge. */
1854 if (!SF_GET_LOCAL (symbolP)
1855 && (real_symbolP = symbol_find_base (S_GET_NAME (symbolP), DO_NOT_STRIP))
1856 && real_symbolP != symbolP)
1857 {
1858 /* FIXME-SOON: where do dups come from? Maybe tag references before definitions? xoxorich. */
1859 /* Move the debug data from the debug symbol to the
1860 real symbol. Do NOT do the oposite (i.e. move from
1861 real symbol to debug symbol and remove real symbol from the
1862 list.) Because some pointers refer to the real symbol
1863 whereas no pointers refer to the debug symbol. */
1864 c_symbol_merge (symbolP, real_symbolP);
1865 /* Replace the current symbol by the real one */
1866 /* The symbols will never be the last or the first
1867 because : 1st symbol is .file and 3 last symbols are
1868 .text, .data, .bss */
1869 symbol_remove (real_symbolP, &symbol_rootP, &symbol_lastP);
1870 symbol_insert (real_symbolP, symbolP, &symbol_rootP, &symbol_lastP);
1871 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
1872 symbolP = real_symbolP;
1873 } /* if not local but dup'd */
1874
1875 if (flagseen['R'] && (S_GET_SEGMENT (symbolP) == SEG_DATA))
1876 {
1877 S_SET_SEGMENT (symbolP, SEG_TEXT);
1878 } /* push data into text */
1879
1880 resolve_symbol_value (symbolP);
1881
1882 if (!S_IS_DEFINED (symbolP) && !SF_GET_LOCAL (symbolP))
1883 {
1884 S_SET_EXTERNAL (symbolP);
1885 }
1886 else if (S_GET_STORAGE_CLASS (symbolP) == C_NULL)
1887 {
1888 if (S_GET_SEGMENT (symbolP) == SEG_TEXT)
1889 {
1890 S_SET_STORAGE_CLASS (symbolP, C_LABEL);
1891 }
1892 else
1893 {
1894 S_SET_STORAGE_CLASS (symbolP, C_STAT);
1895 }
1896 } /* no storage class yet */
1897
1898 /* Mainly to speed up if not -g */
1899 if (SF_GET_PROCESS (symbolP))
1900 {
1901 /* Handle the nested blocks auxiliary info. */
1902 if (S_GET_STORAGE_CLASS (symbolP) == C_BLOCK)
1903 {
1904 if (!strcmp (S_GET_NAME (symbolP), ".bb"))
1905 stack_push (block_stack, (char *) &symbolP);
1906 else
1907 { /* .eb */
1908 register symbolS *begin_symbolP;
1909 begin_symbolP = *(symbolS **) stack_pop (block_stack);
1910 if (begin_symbolP == (symbolS *) 0)
1911 as_warn ("mismatched .eb");
1912 else
1913 SA_SET_SYM_ENDNDX (begin_symbolP, symbol_number + 2);
1914 }
1915 }
1916 /* If we are able to identify the type of a function, and we
1917 are out of a function (last_functionP == 0) then, the
1918 function symbol will be associated with an auxiliary
1919 entry. */
1920 if (last_functionP == (symbolS *) 0 &&
1921 SF_GET_FUNCTION (symbolP))
1922 {
1923 last_functionP = symbolP;
1924
1925 if (S_GET_NUMBER_AUXILIARY (symbolP) < 1)
1926 {
1927 S_SET_NUMBER_AUXILIARY (symbolP, 1);
1928 } /* make it at least 1 */
1929
1930 /* Clobber possible stale .dim information. */
1931 memset (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen,
1932 '\0', sizeof (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen));
1933 }
1934 /* The C_FCN doesn't need any additional information.
1935 I don't even know if this is needed for sdb. But the
1936 standard assembler generates it, so...
1937 */
1938 if (S_GET_STORAGE_CLASS (symbolP) == C_EFCN)
1939 {
1940 if (last_functionP == (symbolS *) 0)
1941 as_fatal ("C_EFCN symbol out of scope");
1942 SA_SET_SYM_FSIZE (last_functionP,
1943 (long) (S_GET_VALUE (symbolP) -
1944 S_GET_VALUE (last_functionP)));
1945 SA_SET_SYM_ENDNDX (last_functionP, symbol_number);
1946 last_functionP = (symbolS *) 0;
1947 }
1948 }
1949 }
1950 else if (SF_GET_TAG (symbolP))
1951 {
1952 /* First descriptor of a structure must point to
1953 the first slot after the structure description. */
1954 last_tagP = symbolP;
1955
1956 }
1957 else if (S_GET_STORAGE_CLASS (symbolP) == C_EOS)
1958 {
1959 /* +2 take in account the current symbol */
1960 SA_SET_SYM_ENDNDX (last_tagP, symbol_number + 2);
1961 }
1962 else if (S_GET_STORAGE_CLASS (symbolP) == C_FILE)
1963 {
1964 if (symbolP->sy_symbol.ost_auxent->x_file.x_n.x_zeroes == 0)
1965 {
1966 symbolP->sy_symbol.ost_auxent->x_file.x_n.x_offset = string_byte_count;
1967 string_byte_count +=
1968 strlen(GET_FILENAME_STRING(symbolP)) + 1;
1969
1970
1971 }
1972
1973 if (S_GET_VALUE (symbolP))
1974 {
1975 S_SET_VALUE ((symbolS *) S_GET_VALUE (symbolP), symbol_number);
1976 S_SET_VALUE (symbolP, 0);
1977 } /* no one points at the first .file symbol */
1978 } /* if debug or tag or eos or file */
1979
1980 /* We must put the external symbols apart. The loader
1981 does not bomb if we do not. But the references in
1982 the endndx field for a .bb symbol are not corrected
1983 if an external symbol is removed between .bb and .be.
1984 I.e in the following case :
1985 [20] .bb endndx = 22
1986 [21] foo external
1987 [22] .be
1988 ld will move the symbol 21 to the end of the list but
1989 endndx will still be 22 instead of 21. */
1990
1991 if (SF_GET_LOCAL (symbolP))
1992 {
1993 /* remove C_EFCN and LOCAL (L...) symbols */
1994 /* next pointer remains valid */
1995 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
1996
1997 }
1998 else if (
1999 #ifdef TE_I386AIX
2000 S_GET_STORAGE_CLASS (symbolP) == C_EXT
2001 && !SF_GET_FUNCTION (symbolP)
2002 #else /* not TE_I386AIX */
2003 !S_IS_DEFINED (symbolP)
2004 && !S_IS_DEBUG (symbolP)
2005 && !SF_GET_STATICS (symbolP)
2006 #endif /* not TE_I386AIX */
2007 )
2008 {
2009 /* if external, Remove from the list */
2010 symbolS *hold = symbol_previous (symbolP);
2011
2012 symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
2013 symbol_clear_list_pointers (symbolP);
2014 symbol_append (symbolP, symbol_extern_lastP, &symbol_externP, &symbol_extern_lastP);
2015 symbolP = hold;
2016 }
2017 else
2018 {
2019 if (SF_GET_STRING (symbolP))
2020 {
2021 symbolP->sy_name_offset = string_byte_count;
2022 string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
2023 }
2024 else
2025 {
2026 symbolP->sy_name_offset = 0;
2027 } /* fix "long" names */
2028
2029 symbolP->sy_number = symbol_number;
2030 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP);
2031 } /* if local symbol */
2032 } /* traverse the symbol list */
2033
2034 for (symbolP = symbol_externP; symbol_externP;)
2035 {
2036 symbolS *tmp = symbol_externP;
2037
2038 /* append */
2039 symbol_remove (tmp, &symbol_externP, &symbol_extern_lastP);
2040 symbol_append (tmp, symbol_lastP, &symbol_rootP, &symbol_lastP);
2041
2042 /* and process */
2043 if (SF_GET_STRING (tmp))
2044 {
2045 tmp->sy_name_offset = string_byte_count;
2046 string_byte_count += strlen (S_GET_NAME (tmp)) + 1;
2047 }
2048 else
2049 {
2050 tmp->sy_name_offset = 0;
2051 } /* fix "long" names */
2052
2053 tmp->sy_number = symbol_number;
2054 symbol_number += 1 + S_GET_NUMBER_AUXILIARY (tmp);
2055 } /* append the entire extern chain */
2056
2057 /* When a tag reference preceeds the tag definition, the definition
2058 will not have a number at the time we process the reference
2059 during the first traversal. Thus, a second traversal. */
2060
2061 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
2062 {
2063 if (SF_GET_TAGGED (symbolP))
2064 {
2065 SA_SET_SYM_TAGNDX (symbolP, ((symbolS *) SA_GET_SYM_TAGNDX (symbolP))->sy_number);
2066 }
2067 }
2068
2069 know (symbol_externP == NULL);
2070 know (symbol_extern_lastP == NULL);
2071
2072 /* FIXME-SOMEDAY I'm counting line no's here so we know what to put
2073 in the section headers, and I'm resolving the addresses since I'm
2074 not sure how to do it later. I am NOT resolving the linno's
2075 representing functions. Their symbols need a fileptr pointing to
2076 this linno when emitted. Thus, I resolve them on emit.
2077 xoxorich. */
2078
2079 for (lineP = lineno_rootP; lineP; lineP = lineP->next)
2080 {
2081 if (lineP->line.l_lnno > 0)
2082 {
2083 lineP->line.l_addr.l_paddr += ((fragS *) lineP->frag)->fr_address;
2084 }
2085 else
2086 {
2087 ;
2088 }
2089 text_lineno_number++;
2090 } /* for each line number */
2091
2092 H_SET_SYMBOL_TABLE_SIZE (headers, symbol_number);
2093 }
2094
2095 /*
2096 * Find strings by crawling along symbol table chain.
2097 */
2098
2099 void
2100 obj_emit_strings (where)
2101 char **where;
2102 {
2103 symbolS *symbolP;
2104
2105 #ifdef CROSS_COMPILE
2106 /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
2107 md_number_to_chars (*where, string_byte_count, sizeof (string_byte_count));
2108 *where += sizeof (string_byte_count);
2109 #else /* CROSS_COMPILE */
2110 append (where, (char *) &string_byte_count, (unsigned long) sizeof (string_byte_count));
2111 #endif /* CROSS_COMPILE */
2112
2113 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
2114 {
2115 if (S_GET_STORAGE_CLASS(symbolP) == C_FILE)
2116 {
2117 /* May need special treatment for this auxent */
2118 if (symbolP->sy_symbol.ost_auxent->x_file.x_n.x_zeroes == 0)
2119 {
2120 char *p = GET_FILENAME_STRING(symbolP);
2121 append
2122 (where,p, strlen(p)+1);
2123 }
2124 }
2125 if (SF_GET_STRING (symbolP))
2126 {
2127 append (where, S_GET_NAME (symbolP),
2128 (unsigned long) (strlen (S_GET_NAME (symbolP)) + 1));
2129 } /* if it has a string */
2130 } /* walk the symbol chain */
2131 }
2132
2133 void
2134 obj_pre_write_hook (headers)
2135 object_headers *headers;
2136 {
2137 register int text_relocation_number = 0;
2138 register int data_relocation_number = 0;
2139 register fixS *fixP;
2140
2141 /* FIXME-SOMEDAY this should be done at fixup_segment time but I'm
2142 going to wait until I do multiple segments. xoxorich. */
2143 /* Count the number of relocation entries for text and data */
2144 for (fixP = text_fix_root; fixP; fixP = fixP->fx_next)
2145 {
2146 if (!fixP->fx_done)
2147 {
2148 ++text_relocation_number;
2149 #ifdef TC_I960
2150 /* two relocs per callj under coff. */
2151 if (fixP->fx_tcbit)
2152 {
2153 ++text_relocation_number;
2154 } /* if callj and not already fixed. */
2155 #endif /* TC_I960 */
2156 #ifdef TC_A29K
2157 /* Count 2 for a constH */
2158 if (fixP->fx_r_type == RELOC_CONSTH)
2159 {
2160 ++text_relocation_number;
2161 }
2162 #endif
2163 } /* if not yet fixed */
2164 } /* for each fix */
2165
2166 SA_SET_SCN_NRELOC (dot_text_symbol, text_relocation_number);
2167 /* Assign the number of line number entries for the text section */
2168 SA_SET_SCN_NLINNO (dot_text_symbol, text_lineno_number);
2169 /* Assign the size of the section */
2170 SA_SET_SCN_SCNLEN (dot_text_symbol, H_GET_TEXT_SIZE (headers));
2171
2172 for (fixP = data_fix_root; fixP; fixP = fixP->fx_next)
2173 {
2174 if (!fixP->fx_done)
2175 {
2176 ++data_relocation_number;
2177 } /* if still relocatable */
2178 #ifdef TC_A29K
2179 /* Count 2 for a constH */
2180 if (fixP->fx_r_type == RELOC_CONSTH)
2181 {
2182 ++data_relocation_number;
2183 }
2184 #endif
2185 }
2186
2187 SA_SET_SCN_NRELOC (dot_data_symbol, data_relocation_number);
2188 /* Assign the size of the section */
2189 SA_SET_SCN_SCNLEN (dot_data_symbol, H_GET_DATA_SIZE (headers));
2190
2191 /* Assign the size of the section */
2192 SA_SET_SCN_SCNLEN (dot_bss_symbol, H_GET_BSS_SIZE (headers));
2193
2194 /* pre write hook can add relocs (for 960 and 29k coff) so */
2195 headers->relocation_size = text_relocation_number * RELSZ +
2196 data_relocation_number * RELSZ;
2197
2198
2199
2200 /* Fill in extra coff fields */
2201
2202 /* Initialize general line number information. */
2203 H_SET_LINENO_SIZE (headers, text_lineno_number * LINESZ);
2204
2205 /* filehdr */
2206 H_SET_FILE_MAGIC_NUMBER (headers, FILE_HEADER_MAGIC);
2207 H_SET_NUMBER_OF_SECTIONS (headers, 3); /* text+data+bss */
2208 #ifndef OBJ_COFF_OMIT_TIMESTAMP
2209 H_SET_TIME_STAMP (headers, (long) time ((long *) 0));
2210 #else /* OBJ_COFF_OMIT_TIMESTAMP */
2211 H_SET_TIME_STAMP (headers, 0);
2212 #endif /* OBJ_COFF_OMIT_TIMESTAMP */
2213 H_SET_SYMBOL_TABLE_POINTER (headers, H_GET_SYMBOL_TABLE_FILE_OFFSET (headers));
2214 #if 0
2215 printf ("FILHSZ %x\n", FILHSZ);
2216 printf ("OBJ_COFF_AOUTHDRSZ %x\n", OBJ_COFF_AOUTHDRSZ);
2217 printf ("section headers %x\n", H_GET_NUMBER_OF_SECTIONS (headers) * SCNHSZ);
2218 printf ("get text size %x\n", H_GET_TEXT_SIZE (headers));
2219 printf ("get data size %x\n", H_GET_DATA_SIZE (headers));
2220 printf ("get relocation size %x\n", H_GET_RELOCATION_SIZE (headers));
2221 printf ("get lineno size %x\n", H_GET_LINENO_SIZE (headers));
2222 #endif
2223 /* symbol table size allready set */
2224 H_SET_SIZEOF_OPTIONAL_HEADER (headers, OBJ_COFF_AOUTHDRSZ);
2225
2226 /* Do not added the F_RELFLG for the standard COFF. The AIX linker
2227 complain on file with relocation info striped flag. */
2228 #ifdef KEEP_RELOC_INFO
2229 H_SET_FLAGS (headers, (text_lineno_number == 0 ? F_LNNO : 0)
2230 | BYTE_ORDERING);
2231 #else
2232 H_SET_FLAGS (headers, (text_lineno_number == 0 ? F_LNNO : 0)
2233 | ((text_relocation_number + data_relocation_number) ? 0 : F_RELFLG)
2234 | BYTE_ORDERING);
2235 #endif
2236 /* aouthdr */
2237 /* magic number allready set */
2238 H_SET_VERSION_STAMP (headers, 0);
2239 /* Text, data, bss size; entry point; text_start and data_start are already set */
2240
2241 /* Build section headers */
2242
2243 c_section_header (&text_section_header,
2244 ".text",
2245 0,
2246 H_GET_TEXT_SIZE (headers),
2247 H_GET_TEXT_FILE_OFFSET (headers),
2248 (SA_GET_SCN_NRELOC (dot_text_symbol)
2249 ? H_GET_RELOCATION_FILE_OFFSET (headers)
2250 : 0),
2251 (text_lineno_number
2252 ? H_GET_LINENO_FILE_OFFSET (headers)
2253 : 0),
2254 SA_GET_SCN_NRELOC (dot_text_symbol),
2255 text_lineno_number,
2256 section_alignment[(int) SEG_TEXT]);
2257
2258 c_section_header (&data_section_header,
2259 ".data",
2260 H_GET_TEXT_SIZE (headers),
2261 H_GET_DATA_SIZE (headers),
2262 (H_GET_DATA_SIZE (headers)
2263 ? H_GET_DATA_FILE_OFFSET (headers)
2264 : 0),
2265 (SA_GET_SCN_NRELOC (dot_data_symbol)
2266 ? (H_GET_RELOCATION_FILE_OFFSET (headers)
2267 + text_section_header.s_nreloc * RELSZ)
2268 : 0),
2269 0, /* No line number information */
2270 SA_GET_SCN_NRELOC (dot_data_symbol),
2271 0, /* No line number information */
2272 section_alignment[(int) SEG_DATA]);
2273
2274 c_section_header (&bss_section_header,
2275 ".bss",
2276 H_GET_TEXT_SIZE (headers) + H_GET_DATA_SIZE (headers),
2277 H_GET_BSS_SIZE (headers),
2278 0, /* No file offset */
2279 0, /* No relocation information */
2280 0, /* No line number information */
2281 0, /* No relocation information */
2282 0, /* No line number information */
2283 section_alignment[(int) SEG_BSS]);
2284 }
2285 #endif
2286
2287 #ifdef BFD_ASSEMBLER
2288
2289 symbolS *coff_last_function;
2290
2291 void
2292 coff_frob_symbol (symp, punt)
2293 symbolS *symp;
2294 int *punt;
2295 {
2296 static symbolS *last_tagP;
2297 static stack *block_stack;
2298 static symbolS *set_end;
2299
2300 if (symp == &abs_symbol)
2301 {
2302 *punt = 1;
2303 return;
2304 }
2305
2306 if (current_lineno_sym)
2307 coff_add_linesym ((symbolS *) 0);
2308
2309 if (!block_stack)
2310 block_stack = stack_init (512, sizeof (symbolS*));
2311
2312 if (!S_IS_DEFINED (symp) && S_GET_STORAGE_CLASS (symp) != C_STAT)
2313 S_SET_STORAGE_CLASS (symp, C_EXT);
2314
2315 if (!SF_GET_DEBUG (symp))
2316 {
2317 symbolS *real;
2318 if (!SF_GET_LOCAL (symp)
2319 && (real = symbol_find_base (S_GET_NAME (symp), DO_NOT_STRIP))
2320 && real != symp)
2321 {
2322 c_symbol_merge (symp, real);
2323 *punt = 1;
2324 }
2325 if (!S_IS_DEFINED (symp) && !SF_GET_LOCAL (symp))
2326 {
2327 assert (S_GET_VALUE (symp) == 0);
2328 S_SET_EXTERNAL (symp);
2329 }
2330 else if (S_GET_STORAGE_CLASS (symp) == C_NULL)
2331 {
2332 if (S_GET_SEGMENT (symp) == text_section)
2333 S_SET_STORAGE_CLASS (symp, C_LABEL);
2334 else
2335 S_SET_STORAGE_CLASS (symp, C_STAT);
2336 }
2337 if (SF_GET_PROCESS (symp))
2338 {
2339 if (S_GET_STORAGE_CLASS (symp) == C_BLOCK)
2340 {
2341 if (!strcmp (S_GET_NAME (symp), ".bb"))
2342 stack_push (block_stack, (char *) &symp);
2343 else
2344 {
2345 symbolS *begin;
2346 begin = *(symbolS **) stack_pop (block_stack);
2347 if (begin == 0)
2348 as_warn ("mismatched .eb");
2349 else
2350 set_end = begin;
2351 }
2352 }
2353 if (coff_last_function == 0 && SF_GET_FUNCTION (symp))
2354 {
2355 union internal_auxent *auxp;
2356 coff_last_function = symp;
2357 if (S_GET_NUMBER_AUXILIARY (symp) < 1)
2358 S_SET_NUMBER_AUXILIARY (symp, 1);
2359 auxp = &coffsymbol (symp->bsym)->native[1].u.auxent;
2360 memset (auxp->x_sym.x_fcnary.x_ary.x_dimen, 0,
2361 sizeof (auxp->x_sym.x_fcnary.x_ary.x_dimen));
2362 }
2363 if (S_GET_STORAGE_CLASS (symp) == C_EFCN)
2364 {
2365 if (coff_last_function == 0)
2366 as_fatal ("C_EFCN symbol out of scope");
2367 SA_SET_SYM_FSIZE (coff_last_function,
2368 (long) (S_GET_VALUE (symp)
2369 - S_GET_VALUE (coff_last_function)));
2370 set_end = coff_last_function;
2371 coff_last_function = 0;
2372 }
2373 }
2374 else if (SF_GET_TAG (symp))
2375 last_tagP = symp;
2376 else if (S_GET_STORAGE_CLASS (symp) == C_EOS)
2377 set_end = last_tagP;
2378 else if (S_GET_STORAGE_CLASS (symp) == C_FILE)
2379 {
2380 if (S_GET_VALUE (symp))
2381 {
2382 S_SET_VALUE ((symbolS *) S_GET_VALUE (symp), 0xdeadbeef);
2383 S_SET_VALUE (symp, 0);
2384 }
2385 }
2386 if (S_IS_EXTERNAL (symp))
2387 S_SET_STORAGE_CLASS (symp, C_EXT);
2388 else if (SF_GET_LOCAL (symp))
2389 *punt = 1;
2390 /* more ... */
2391 }
2392
2393 if (set_end != (symbolS *) NULL
2394 && ! *punt)
2395 {
2396 SA_SET_SYM_ENDNDX (set_end, symp);
2397 set_end = NULL;
2398 }
2399
2400 if (coffsymbol (symp->bsym)->lineno)
2401 {
2402 int i, n;
2403 struct line_no *lptr;
2404 alent *l;
2405
2406 lptr = (struct line_no *) coffsymbol (symp->bsym)->lineno;
2407 for (i = 0; lptr; lptr = lptr->next)
2408 i++;
2409 n = i + 1;
2410 lptr = (struct line_no *) coffsymbol (symp->bsym)->lineno;
2411 l = (alent *) bfd_alloc_by_size_t (stdoutput, n * sizeof (alent));
2412 coffsymbol (symp->bsym)->lineno = l;
2413 for (i = n - 1; i > 0; i--)
2414 {
2415 if (lptr->frag)
2416 lptr->l.u.offset += lptr->frag->fr_address;
2417 l[i] = lptr->l;
2418 lptr = lptr->next;
2419 }
2420 }
2421 }
2422
2423 /*
2424 * implement the .section pseudo op:
2425 * .section name {, "flags"}
2426 * ^ ^
2427 * | +--- optional flags: 'b' for bss
2428 * | 'i' for info
2429 * +-- section name 'l' for lib
2430 * 'n' for noload
2431 * 'o' for over
2432 * 'w' for data
2433 * 'd' (apparently m88k for data)
2434 * 'x' for text
2435 * But if the argument is not a quoted string, treat it as a
2436 * subsegment number.
2437 */
2438
2439 void
2440 obj_coff_section (ignore)
2441 int ignore;
2442 {
2443 /* Strip out the section name */
2444 char *section_name;
2445 char c;
2446 char *name;
2447 unsigned int exp;
2448 flagword flags;
2449 asection *sec;
2450
2451 section_name = input_line_pointer;
2452 c = get_symbol_end ();
2453
2454 name = xmalloc (input_line_pointer - section_name + 1);
2455 strcpy (name, section_name);
2456
2457 *input_line_pointer = c;
2458
2459 SKIP_WHITESPACE ();
2460
2461 exp = 0;
2462 flags = SEC_NO_FLAGS;
2463
2464 if (*input_line_pointer == ',')
2465 {
2466 ++input_line_pointer;
2467 SKIP_WHITESPACE ();
2468 if (*input_line_pointer != '"')
2469 exp = get_absolute_expression ();
2470 else
2471 {
2472 ++input_line_pointer;
2473 while (*input_line_pointer != '"'
2474 && ! is_end_of_line[(unsigned char) *input_line_pointer])
2475 {
2476 switch (*input_line_pointer)
2477 {
2478 case 'b': flags |= SEC_ALLOC; flags &=~ SEC_LOAD; break;
2479 case 'n': flags &=~ SEC_LOAD; break;
2480 case 'd':
2481 case 'w': flags &=~ SEC_READONLY; break;
2482 case 'x': flags |= SEC_CODE; break;
2483
2484 case 'i': /* STYP_INFO */
2485 case 'l': /* STYP_LIB */
2486 case 'o': /* STYP_OVER */
2487 as_warn ("unsupported section attribute '%c'",
2488 *input_line_pointer);
2489 break;
2490
2491 default:
2492 as_warn("unknown section attribute '%c'",
2493 *input_line_pointer);
2494 break;
2495 }
2496 ++input_line_pointer;
2497 }
2498 if (*input_line_pointer == '"')
2499 ++input_line_pointer;
2500 }
2501 }
2502
2503 sec = subseg_new (name, (subsegT) exp);
2504
2505 if (flags != SEC_NO_FLAGS)
2506 {
2507 if (! bfd_set_section_flags (stdoutput, sec, flags))
2508 as_warn ("error setting flags for \"%s\": %s",
2509 bfd_section_name (stdoutput, sec),
2510 bfd_errmsg (bfd_get_error ()));
2511 }
2512 }
2513
2514 void
2515 coff_frob_file ()
2516 {
2517 if (symbol_rootP == NULL
2518 || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE)
2519 {
2520 assert (previous_file_symbol == 0);
2521 c_dot_file_symbol ("fake");
2522 }
2523 }
2524 #endif /* BFD_ASSEMBLER */
2525
2526 #ifdef DEBUG
2527 /* for debugging */
2528 const char *
2529 s_get_name (s)
2530 symbolS *s;
2531 {
2532 return ((s == NULL) ? "(NULL)" : S_GET_NAME (s));
2533 } /* s_get_name() */
2534
2535 void
2536 symbol_dump ()
2537 {
2538 symbolS *symbolP;
2539
2540 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
2541 {
2542 #ifdef BFD_ASSEMBLER
2543 printf("0x%lx: \"%s\" type = %ld, class = %d, segment = %d\n",
2544 (unsigned long) symbolP,
2545 S_GET_NAME(symbolP),
2546 (long) S_GET_DATA_TYPE(symbolP),
2547 S_GET_STORAGE_CLASS(symbolP),
2548 (int) S_GET_SEGMENT(symbolP));
2549 #else
2550 printf ("%3ld: 0x%lx \"%s\" type = %ld, class = %d, segment = %d\n",
2551 symbolP->sy_number,
2552 (unsigned long) symbolP,
2553 S_GET_NAME (symbolP),
2554 (long) S_GET_DATA_TYPE (symbolP),
2555 S_GET_STORAGE_CLASS (symbolP),
2556 (int) S_GET_SEGMENT (symbolP));
2557 #endif
2558 }
2559 }
2560
2561 #endif /* DEBUG */
2562
2563 /* end of obj-coff.c */