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