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