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