* Removed sy_forward and replaced it with an undefined expression
[external/binutils.git] / gas / config / obj-aout.c
1 /* a.out object file format
2    Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
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
8 published by the Free Software Foundation; either version 2,
9 or (at your option) any later version.
10
11 GAS is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14 the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public
17 License along with GAS; see the file COPYING.  If not, write
18 to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "as.h"
21 #include "aout/stab_gnu.h"
22 #include "obstack.h"
23
24 #ifndef BFD_ASSEMBLER
25 /* in: segT   out: N_TYPE bits */
26 const short seg_N_TYPE[] =
27 {
28   N_ABS,
29   N_TEXT,
30   N_DATA,
31   N_BSS,
32   N_UNDF,                       /* unknown */
33   N_UNDF,                       /* absent */
34   N_UNDF,                       /* pass1 */
35   N_UNDF,                       /* error */
36   N_UNDF,                       /* bignum/flonum */
37   N_UNDF,                       /* difference */
38   N_UNDF,                       /* debug */
39   N_UNDF,                       /* ntv */
40   N_UNDF,                       /* ptv */
41   N_REGISTER,                   /* register */
42 };
43
44 const segT N_TYPE_seg[N_TYPE + 2] =
45 {                               /* N_TYPE == 0x1E = 32-2 */
46   SEG_UNKNOWN,                  /* N_UNDF == 0 */
47   SEG_GOOF,
48   SEG_ABSOLUTE,                 /* N_ABS == 2 */
49   SEG_GOOF,
50   SEG_TEXT,                     /* N_TEXT == 4 */
51   SEG_GOOF,
52   SEG_DATA,                     /* N_DATA == 6 */
53   SEG_GOOF,
54   SEG_BSS,                      /* N_BSS == 8 */
55   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, SEG_GOOF,
58   SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
59   SEG_REGISTER,                 /* dummy N_REGISTER for regs = 30 */
60   SEG_GOOF,
61 };
62 #endif
63
64 static void obj_aout_stab PARAMS ((int what));
65 static void obj_aout_line PARAMS ((void));
66 static void obj_aout_desc PARAMS ((void));
67
68 const pseudo_typeS obj_pseudo_table[] =
69 {
70   /* stabs debug info */
71   {"line", obj_aout_line, 0},   /* source code line number */
72   {"ln", obj_aout_line, 0},     /* coff line number that we use anyway */
73   {"desc", obj_aout_desc, 0},   /* desc */
74   {"stabd", obj_aout_stab, 'd'},/* stabs */
75   {"stabn", obj_aout_stab, 'n'},/* stabs */
76   {"stabs", obj_aout_stab, 's'},/* stabs */
77
78   /* coff debug pseudos (ignored) */
79   {"def", s_ignore, 0},
80   {"dim", s_ignore, 0},
81   {"endef", s_ignore, 0},
82   {"ident", s_ignore, 0},
83   {"line", s_ignore, 0},
84   {"ln", s_ignore, 0},
85   {"scl", s_ignore, 0},
86   {"size", s_ignore, 0},
87   {"tag", s_ignore, 0},
88   {"type", s_ignore, 0},
89   {"val", s_ignore, 0},
90   {"version", s_ignore, 0},
91
92   /* stabs-in-coff (?) debug pseudos (ignored) */
93   {"optim", s_ignore, 0},       /* For sun386i cc (?) */
94
95   /* other stuff */
96   {"ABORT", s_abort, 0},
97
98   {NULL}                        /* end sentinel */
99 };                              /* obj_pseudo_table */
100
101
102 #ifdef BFD_ASSEMBLER
103
104 void
105 obj_aout_frob_symbol (sym, punt)
106      symbolS *sym;
107      int *punt;
108 {
109   flagword flags;
110   asection *sec;
111   int desc, type, other;
112
113   flags = sym->bsym->flags;
114   desc = S_GET_DESC (sym);
115   type = S_GET_TYPE (sym);
116   other = S_GET_OTHER (sym);
117   sec = sym->bsym->section;
118
119   /* Only frob simple symbols this way right now.  */
120   if (! (type & ~0x1f))
121     {
122       if (sec == &bfd_abs_section
123           || sec == &bfd_und_section)
124         return;
125       if (flags & BSF_EXPORT)
126         type |= 1;
127     }
128   else
129     {
130       sym->bsym->flags |= BSF_DEBUGGING;
131     }
132
133   S_SET_TYPE (sym, type);
134 }
135
136 #else
137
138 /* Relocation. */
139
140 /*
141  *              emit_relocations()
142  *
143  * Crawl along a fixS chain. Emit the segment's relocations.
144  */
145 void
146 obj_emit_relocations (where, fixP, segment_address_in_file)
147      char **where;
148      fixS *fixP;                /* Fixup chain for this segment. */
149      relax_addressT segment_address_in_file;
150 {
151   for (; fixP; fixP = fixP->fx_next)
152     if (fixP->fx_addsy != NULL)
153       {
154         tc_aout_fix_to_chars (*where, fixP, segment_address_in_file);
155         *where += md_reloc_size;
156       }
157 }
158
159 #ifndef obj_header_append
160 /* Aout file generation & utilities */
161 void
162 obj_header_append (where, headers)
163      char **where;
164      object_headers *headers;
165 {
166   tc_headers_hook (headers);
167
168 #ifdef CROSS_COMPILE
169   md_number_to_chars (*where, headers->header.a_info, sizeof (headers->header.a_info));
170   *where += sizeof (headers->header.a_info);
171   md_number_to_chars (*where, headers->header.a_text, sizeof (headers->header.a_text));
172   *where += sizeof (headers->header.a_text);
173   md_number_to_chars (*where, headers->header.a_data, sizeof (headers->header.a_data));
174   *where += sizeof (headers->header.a_data);
175   md_number_to_chars (*where, headers->header.a_bss, sizeof (headers->header.a_bss));
176   *where += sizeof (headers->header.a_bss);
177   md_number_to_chars (*where, headers->header.a_syms, sizeof (headers->header.a_syms));
178   *where += sizeof (headers->header.a_syms);
179   md_number_to_chars (*where, headers->header.a_entry, sizeof (headers->header.a_entry));
180   *where += sizeof (headers->header.a_entry);
181   md_number_to_chars (*where, headers->header.a_trsize, sizeof (headers->header.a_trsize));
182   *where += sizeof (headers->header.a_trsize);
183   md_number_to_chars (*where, headers->header.a_drsize, sizeof (headers->header.a_drsize));
184   *where += sizeof (headers->header.a_drsize);
185
186 #else /* CROSS_COMPILE */
187
188   append (where, (char *) &headers->header, sizeof (headers->header));
189 #endif /* CROSS_COMPILE */
190
191 }
192 #endif
193
194 void
195 obj_symbol_to_chars (where, symbolP)
196      char **where;
197      symbolS *symbolP;
198 {
199   md_number_to_chars ((char *) &(S_GET_OFFSET (symbolP)), S_GET_OFFSET (symbolP), sizeof (S_GET_OFFSET (symbolP)));
200   md_number_to_chars ((char *) &(S_GET_DESC (symbolP)), S_GET_DESC (symbolP), sizeof (S_GET_DESC (symbolP)));
201   md_number_to_chars ((char *) &(symbolP->sy_symbol.n_value), S_GET_VALUE (symbolP), sizeof (symbolP->sy_symbol.n_value));
202
203   append (where, (char *) &symbolP->sy_symbol, sizeof (obj_symbol_type));
204 }
205
206 void
207 obj_emit_symbols (where, symbol_rootP)
208      char **where;
209      symbolS *symbol_rootP;
210 {
211   symbolS *symbolP;
212
213   /* Emit all symbols left in the symbol chain.  */
214   for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
215     {
216       /* Used to save the offset of the name. It is used to point
217          to the string in memory but must be a file offset. */
218       register char *temp;
219
220       temp = S_GET_NAME (symbolP);
221       S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
222
223       /* Any symbol still undefined and is not a dbg symbol is made N_EXT. */
224       if (!S_IS_DEBUG (symbolP) && !S_IS_DEFINED (symbolP))
225         S_SET_EXTERNAL (symbolP);
226
227       obj_symbol_to_chars (where, symbolP);
228       S_SET_NAME (symbolP, temp);
229     }
230 }
231
232 #endif /* ! BFD_ASSEMBLER */
233
234 static void
235 obj_aout_line ()
236 {
237   /* Assume delimiter is part of expression.
238      BSD4.2 as fails with delightful bug, so we
239      are not being incompatible here. */
240   new_logical_line ((char *) NULL, (int) (get_absolute_expression ()));
241   demand_empty_rest_of_line ();
242 }                               /* obj_aout_line() */
243
244 /*
245  *                      stab()
246  *
247  * Handle .stabX directives, which used to be open-coded.
248  * So much creeping featurism overloaded the semantics that we decided
249  * to put all .stabX thinking in one place. Here.
250  *
251  * We try to make any .stabX directive legal. Other people's AS will often
252  * do assembly-time consistency checks: eg assigning meaning to n_type bits
253  * and "protecting" you from setting them to certain values. (They also zero
254  * certain bits before emitting symbols. Tut tut.)
255  *
256  * If an expression is not absolute we either gripe or use the relocation
257  * information. Other people's assemblers silently forget information they
258  * don't need and invent information they need that you didn't supply.
259  *
260  * .stabX directives always make a symbol table entry. It may be junk if
261  * the rest of your .stabX directive is malformed.
262  */
263 static void
264 obj_aout_stab (what)
265      int what;
266 {
267   extern int listing;
268
269   register symbolS *symbolP = 0;
270   register char *string;
271   int saved_type = 0;
272   int length;
273   int goof;                     /* TRUE if we have aborted. */
274   long longint;
275
276   /*
277    * Enter with input_line_pointer pointing past .stabX and any following
278    * whitespace.
279    */
280   goof = 0;                     /* JF who forgot this?? */
281   if (what == 's')
282     {
283       string = demand_copy_C_string (&length);
284       SKIP_WHITESPACE ();
285       if (*input_line_pointer == ',')
286         input_line_pointer++;
287       else
288         {
289           as_bad ("I need a comma after symbol's name");
290           goof = 1;
291         }
292     }
293   else
294     string = "";
295
296   /*
297    * Input_line_pointer->after ','.  String->symbol name.
298    */
299   if (!goof)
300     {
301       symbolP = symbol_new (string, undefined_section, 0, (struct frag *) 0);
302       switch (what)
303         {
304         case 'd':
305           S_SET_NAME (symbolP, NULL);   /* .stabd feature. */
306           S_SET_VALUE (symbolP, (char*) obstack_next_free (&frags) - frag_now->fr_literal);
307           symbolP->sy_frag = frag_now;
308           break;
309
310         case 'n':
311           symbolP->sy_frag = &zero_address_frag;
312           break;
313
314         case 's':
315           symbolP->sy_frag = &zero_address_frag;
316           break;
317
318         default:
319           BAD_CASE (what);
320           break;
321         }
322
323       if (get_absolute_expression_and_terminator (&longint) == ',')
324         {
325           saved_type = longint;
326           S_SET_TYPE (symbolP, saved_type);
327         }
328       else
329         {
330           as_bad ("I want a comma after the n_type expression");
331           goof = 1;
332           input_line_pointer--; /* Backup over a non-',' char. */
333         }
334     }
335
336   if (!goof)
337     {
338       if (get_absolute_expression_and_terminator (&longint) == ',')
339         S_SET_OTHER (symbolP, longint);
340       else
341         {
342           as_bad ("I want a comma after the n_other expression");
343           goof = 1;
344           input_line_pointer--; /* Backup over a non-',' char. */
345         }
346     }
347
348   if (!goof)
349     {
350       S_SET_DESC (symbolP, get_absolute_expression ());
351       if (what == 's' || what == 'n')
352         {
353           if (*input_line_pointer != ',')
354             {
355               as_bad ("I want a comma after the n_desc expression");
356               goof = 1;
357             }
358           else
359             {
360               input_line_pointer++;
361             }
362         }
363     }
364
365   if ((!goof) && (what == 's' || what == 'n'))
366     {
367       pseudo_set (symbolP);
368       S_SET_TYPE (symbolP, saved_type);
369     }
370 #ifndef NO_LISTING
371   if (listing && !goof)
372     {
373       if (S_GET_TYPE (symbolP) == N_SLINE)
374         {
375           listing_source_line (S_GET_DESC (symbolP));
376         }
377       else if (S_GET_TYPE (symbolP) == N_SO || S_GET_TYPE (symbolP) == N_SOL)
378         {
379           listing_source_file (string);
380         }
381     }
382 #endif
383
384   if (goof)
385     ignore_rest_of_line ();
386   else
387     demand_empty_rest_of_line ();
388 }                               /* obj_aout_stab() */
389
390 static void
391 obj_aout_desc ()
392 {
393   register char *name;
394   register char c;
395   register char *p;
396   register symbolS *symbolP;
397   register int temp;
398
399   /*
400          * Frob invented at RMS' request. Set the n_desc of a symbol.
401  */
402   name = input_line_pointer;
403   c = get_symbol_end ();
404   p = input_line_pointer;
405   *p = c;
406   SKIP_WHITESPACE ();
407   if (*input_line_pointer != ',')
408     {
409       *p = 0;
410       as_bad ("Expected comma after name \"%s\"", name);
411       *p = c;
412       ignore_rest_of_line ();
413     }
414   else
415     {
416       input_line_pointer++;
417       temp = get_absolute_expression ();
418       *p = 0;
419       symbolP = symbol_find_or_make (name);
420       *p = c;
421       S_SET_DESC (symbolP, temp);
422     }
423   demand_empty_rest_of_line ();
424 }                               /* obj_aout_desc() */
425
426 void
427 obj_read_begin_hook ()
428 {
429   return;
430 }                               /* obj_read_begin_hook() */
431
432 #ifndef BFD_ASSEMBLER
433
434 void
435 obj_crawl_symbol_chain (headers)
436      object_headers *headers;
437 {
438   symbolS *symbolP;
439   symbolS **symbolPP;
440   int symbol_number = 0;
441
442   tc_crawl_symbol_chain (headers);
443
444   symbolPP = &symbol_rootP;     /*->last symbol chain link. */
445   while ((symbolP = *symbolPP) != NULL)
446     {
447       if (flagseen['R'] && (S_GET_SEGMENT (symbolP) == SEG_DATA))
448         {
449           S_SET_SEGMENT (symbolP, SEG_TEXT);
450         }                       /* if pusing data into text */
451
452       resolve_symbol_value (symbolP);
453
454       /* OK, here is how we decide which symbols go out into the brave
455          new symtab.  Symbols that do are:
456
457          * symbols with no name (stabd's?)
458          * symbols with debug info in their N_TYPE
459
460          Symbols that don't are:
461          * symbols that are registers
462          * symbols with \1 as their 3rd character (numeric labels)
463          * "local labels" as defined by S_LOCAL_NAME(name) if the -L
464          switch was passed to gas.
465
466          All other symbols are output.  We complain if a deleted
467          symbol was marked external. */
468
469
470       if (!S_IS_REGISTER (symbolP)
471           && (!S_GET_NAME (symbolP)
472               || S_IS_DEBUG (symbolP)
473 #ifdef TC_I960
474       /* FIXME-SOON this ifdef seems highly dubious to me.  xoxorich. */
475               || !S_IS_DEFINED (symbolP)
476               || S_IS_EXTERNAL (symbolP)
477 #endif /* TC_I960 */
478               || (S_GET_NAME (symbolP)[0] != '\001' && (flagseen['L'] || !S_LOCAL_NAME (symbolP)))))
479         {
480           symbolP->sy_number = symbol_number++;
481
482           /* The + 1 after strlen account for the \0 at the
483                            end of each string */
484           if (!S_IS_STABD (symbolP))
485             {
486               /* Ordinary case. */
487               symbolP->sy_name_offset = string_byte_count;
488               string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
489             }
490           else                  /* .Stabd case. */
491             symbolP->sy_name_offset = 0;
492           symbolPP = &(symbol_next (symbolP));
493         }
494       else
495         {
496           if (S_IS_EXTERNAL (symbolP) || !S_IS_DEFINED (symbolP))
497             {
498               as_bad ("Local symbol %s never defined.", decode_local_label_name (S_GET_NAME (symbolP)));
499             }                   /* oops. */
500
501           /* Unhook it from the chain */
502           *symbolPP = symbol_next (symbolP);
503         }                       /* if this symbol should be in the output */
504     }                           /* for each symbol */
505
506   H_SET_SYMBOL_TABLE_SIZE (headers, symbol_number);
507
508   return;
509 }                               /* obj_crawl_symbol_chain() */
510
511 /*
512  * Find strings by crawling along symbol table chain.
513  */
514
515 void
516 obj_emit_strings (where)
517      char **where;
518 {
519   symbolS *symbolP;
520
521 #ifdef CROSS_COMPILE
522   /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
523   md_number_to_chars (*where, string_byte_count, sizeof (string_byte_count));
524   *where += sizeof (string_byte_count);
525 #else /* CROSS_COMPILE */
526   append (where, (char *) &string_byte_count, (unsigned long) sizeof (string_byte_count));
527 #endif /* CROSS_COMPILE */
528
529   for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
530     {
531       if (S_GET_NAME (symbolP))
532         append (&next_object_file_charP, S_GET_NAME (symbolP),
533                 (unsigned long) (strlen (S_GET_NAME (symbolP)) + 1));
534     }                           /* walk symbol chain */
535
536   return;
537 }                               /* obj_emit_strings() */
538
539 #ifndef AOUT_VERSION
540 #define AOUT_VERSION 0
541 #endif
542
543 void
544 obj_pre_write_hook (headers)
545      object_headers *headers;
546 {
547   H_SET_DYNAMIC (headers, 0);
548   H_SET_VERSION (headers, AOUT_VERSION);
549   H_SET_MACHTYPE (headers, AOUT_MACHTYPE);
550   tc_aout_pre_write_hook (headers);
551   return;
552 }                               /* obj_pre_write_hook() */
553
554 void
555 DEFUN_VOID (s_sect)
556 {
557   /* Strip out the section name */
558   char *section_name;
559   char *section_name_end;
560   char c;
561
562   unsigned int len;
563   unsigned int exp;
564   char *save;
565
566   section_name = input_line_pointer;
567   c = get_symbol_end ();
568   section_name_end = input_line_pointer;
569
570   len = section_name_end - section_name;
571   input_line_pointer++;
572   save = input_line_pointer;
573
574   SKIP_WHITESPACE ();
575   if (c == ',')
576     {
577       exp = get_absolute_expression ();
578     }
579   else if (*input_line_pointer == ',')
580     {
581       input_line_pointer++;
582       exp = get_absolute_expression ();
583     }
584   else
585     {
586       input_line_pointer = save;
587       exp = 0;
588     }
589   if (exp >= 1000)
590     {
591       as_bad ("subsegment index too high");
592     }
593
594   if (strcmp (section_name, ".text") == 0)
595     {
596       subseg_new (SEG_TEXT, (subsegT) exp);
597     }
598
599   if (strcmp (section_name, ".data") == 0)
600     {
601       if (flagseen['R'])
602         subseg_new (SEG_TEXT, (subsegT) exp + 1000);
603       else
604         subseg_new (SEG_DATA, (subsegT) exp);
605     }
606
607   *section_name_end = c;
608 }
609
610 #endif /* ! BFD_ASSEMBLER */
611
612 /* end of obj-aout.c */