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