This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gas / config / obj-aout.c
1 /* a.out object file format
2    Copyright (C) 1989, 90, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3    Free Software Foundation, Inc.
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2,
10 or (at your option) any later version.
11
12 GAS is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
15 the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GAS; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 #include "as.h"
23 #ifdef BFD_ASSEMBLER
24 #undef NO_RELOC
25 #include "aout/aout64.h"
26 #endif
27 #include "obstack.h"
28
29 #ifndef BFD_ASSEMBLER
30 /* in: segT   out: N_TYPE bits */
31 const short seg_N_TYPE[] =
32 {
33   N_ABS,
34   N_TEXT,
35   N_DATA,
36   N_BSS,
37   N_UNDF,                       /* unknown */
38   N_UNDF,                       /* error */
39   N_UNDF,                       /* expression */
40   N_UNDF,                       /* debug */
41   N_UNDF,                       /* ntv */
42   N_UNDF,                       /* ptv */
43   N_REGISTER,                   /* register */
44 };
45
46 const segT N_TYPE_seg[N_TYPE + 2] =
47 {                               /* N_TYPE == 0x1E = 32-2 */
48   SEG_UNKNOWN,                  /* N_UNDF == 0 */
49   SEG_GOOF,
50   SEG_ABSOLUTE,                 /* N_ABS == 2 */
51   SEG_GOOF,
52   SEG_TEXT,                     /* N_TEXT == 4 */
53   SEG_GOOF,
54   SEG_DATA,                     /* N_DATA == 6 */
55   SEG_GOOF,
56   SEG_BSS,                      /* N_BSS == 8 */
57   SEG_GOOF,
58   SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
59   SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
60   SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
61   SEG_REGISTER,                 /* dummy N_REGISTER for regs = 30 */
62   SEG_GOOF,
63 };
64 #endif
65
66 static void obj_aout_line PARAMS ((int));
67 static void obj_aout_weak PARAMS ((int));
68 static void obj_aout_type PARAMS ((int));
69
70 const pseudo_typeS obj_pseudo_table[] =
71 {
72   {"line", obj_aout_line, 0},   /* source code line number */
73   {"ln", obj_aout_line, 0},     /* coff line number that we use anyway */
74
75   {"weak", obj_aout_weak, 0},   /* mark symbol as weak.  */
76
77   {"type", obj_aout_type, 0},
78
79   /* coff debug pseudos (ignored) */
80   {"def", s_ignore, 0},
81   {"dim", s_ignore, 0},
82   {"endef", s_ignore, 0},
83   {"ident", s_ignore, 0},
84   {"line", s_ignore, 0},
85   {"ln", s_ignore, 0},
86   {"scl", s_ignore, 0},
87   {"size", s_ignore, 0},
88   {"tag", s_ignore, 0},
89   {"val", s_ignore, 0},
90   {"version", s_ignore, 0},
91
92   {"optim", s_ignore, 0},       /* For sun386i cc (?) */
93
94   /* other stuff */
95   {"ABORT", s_abort, 0},
96
97   {NULL}                        /* end sentinel */
98 };                              /* obj_pseudo_table */
99
100
101 #ifdef BFD_ASSEMBLER
102
103 void
104 obj_aout_frob_symbol (sym, punt)
105      symbolS *sym;
106      int *punt;
107 {
108   flagword flags;
109   asection *sec;
110   int desc, type, other;
111
112   flags = symbol_get_bfdsym (sym)->flags;
113   desc = S_GET_DESC (sym);
114   type = S_GET_TYPE (sym);
115   other = S_GET_OTHER (sym);
116   sec = S_GET_SEGMENT (sym);
117
118   /* Only frob simple symbols this way right now.  */
119   if (! (type & ~ (N_TYPE | N_EXT)))
120     {
121       if (type == (N_UNDF | N_EXT)
122           && sec == &bfd_abs_section)
123         {
124           sec = bfd_und_section_ptr;
125           S_SET_SEGMENT (sym, sec);
126         }
127
128       if ((type & N_TYPE) != N_INDR
129           && (type & N_TYPE) != N_SETA
130           && (type & N_TYPE) != N_SETT
131           && (type & N_TYPE) != N_SETD
132           && (type & N_TYPE) != N_SETB
133           && type != N_WARNING
134           && (sec == &bfd_abs_section
135               || sec == &bfd_und_section))
136         return;
137       if (flags & BSF_EXPORT)
138         type |= N_EXT;
139
140       switch (type & N_TYPE)
141         {
142         case N_SETA:
143         case N_SETT:
144         case N_SETD:
145         case N_SETB:
146           /* Set the debugging flag for constructor symbols so that
147              BFD leaves them alone.  */
148           symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
149
150           /* You can't put a common symbol in a set.  The way a set
151              element works is that the symbol has a definition and a
152              name, and the linker adds the definition to the set of
153              that name.  That does not work for a common symbol,
154              because the linker can't tell which common symbol the
155              user means.  FIXME: Using as_bad here may be
156              inappropriate, since the user may want to force a
157              particular type without regard to the semantics of sets;
158              on the other hand, we certainly don't want anybody to be
159              mislead into thinking that their code will work.  */
160           if (S_IS_COMMON (sym))
161             as_bad (_("Attempt to put a common symbol into set %s"),
162                     S_GET_NAME (sym));
163           /* Similarly, you can't put an undefined symbol in a set.  */
164           else if (! S_IS_DEFINED (sym))
165             as_bad (_("Attempt to put an undefined symbol into set %s"),
166                     S_GET_NAME (sym));
167
168           break;
169         case N_INDR:
170           /* Put indirect symbols in the indirect section.  */
171           S_SET_SEGMENT (sym, bfd_ind_section_ptr);
172           symbol_get_bfdsym (sym)->flags |= BSF_INDIRECT;
173           if (type & N_EXT)
174             {
175               symbol_get_bfdsym (sym)->flags |= BSF_EXPORT;
176               symbol_get_bfdsym (sym)->flags &=~ BSF_LOCAL;
177             }
178           break;
179         case N_WARNING:
180           /* Mark warning symbols.  */
181           symbol_get_bfdsym (sym)->flags |= BSF_WARNING;
182           break;
183         }
184     }
185   else
186     {
187       symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
188     }
189
190   S_SET_TYPE (sym, type);
191
192   /* Double check weak symbols.  */
193   if (S_IS_WEAK (sym))
194     {
195       if (S_IS_COMMON (sym))
196         as_bad (_("Symbol `%s' can not be both weak and common"),
197                 S_GET_NAME (sym));
198     }
199 }
200
201 void
202 obj_aout_frob_file ()
203 {
204   /* Relocation processing may require knowing the VMAs of the sections.
205      Since writing to a section will cause the BFD back end to compute the
206      VMAs, fake it out here....  */
207   bfd_byte b = 0;
208   boolean x = true;
209   if (bfd_section_size (stdoutput, text_section) != 0)
210     {
211       x = bfd_set_section_contents (stdoutput, text_section, &b, (file_ptr) 0,
212                                     (bfd_size_type) 1);
213     }
214   else if (bfd_section_size (stdoutput, data_section) != 0)
215     {
216       x = bfd_set_section_contents (stdoutput, data_section, &b, (file_ptr) 0,
217                                     (bfd_size_type) 1);
218     }
219   assert (x == true);
220 }
221
222 #else /* ! BFD_ASSEMBLER */
223
224 /* Relocation. */
225
226 /*
227  *              emit_relocations()
228  *
229  * Crawl along a fixS chain. Emit the segment's relocations.
230  */
231 void
232 obj_emit_relocations (where, fixP, segment_address_in_file)
233      char **where;
234      fixS *fixP;                /* Fixup chain for this segment. */
235      relax_addressT segment_address_in_file;
236 {
237   for (; fixP; fixP = fixP->fx_next)
238     if (fixP->fx_done == 0)
239       {
240         symbolS *sym;
241
242         sym = fixP->fx_addsy;
243         while (sym->sy_value.X_op == O_symbol
244                && (! S_IS_DEFINED (sym) || S_IS_COMMON (sym)))
245           sym = sym->sy_value.X_add_symbol;
246         fixP->fx_addsy = sym;
247
248         if (! sym->sy_resolved && ! S_IS_DEFINED (sym))
249           {
250             char *file;
251             unsigned int line;
252
253             if (expr_symbol_where (sym, &file, &line))
254               as_bad_where (file, line, _("unresolved relocation"));
255             else
256               as_bad (_("bad relocation: symbol `%s' not in symbol table"),
257                       S_GET_NAME (sym));
258           }
259
260         tc_aout_fix_to_chars (*where, fixP, segment_address_in_file);
261         *where += md_reloc_size;
262       }
263 }
264
265 #ifndef obj_header_append
266 /* Aout file generation & utilities */
267 void
268 obj_header_append (where, headers)
269      char **where;
270      object_headers *headers;
271 {
272   tc_headers_hook (headers);
273
274 #ifdef CROSS_COMPILE
275   md_number_to_chars (*where, headers->header.a_info, sizeof (headers->header.a_info));
276   *where += sizeof (headers->header.a_info);
277   md_number_to_chars (*where, headers->header.a_text, sizeof (headers->header.a_text));
278   *where += sizeof (headers->header.a_text);
279   md_number_to_chars (*where, headers->header.a_data, sizeof (headers->header.a_data));
280   *where += sizeof (headers->header.a_data);
281   md_number_to_chars (*where, headers->header.a_bss, sizeof (headers->header.a_bss));
282   *where += sizeof (headers->header.a_bss);
283   md_number_to_chars (*where, headers->header.a_syms, sizeof (headers->header.a_syms));
284   *where += sizeof (headers->header.a_syms);
285   md_number_to_chars (*where, headers->header.a_entry, sizeof (headers->header.a_entry));
286   *where += sizeof (headers->header.a_entry);
287   md_number_to_chars (*where, headers->header.a_trsize, sizeof (headers->header.a_trsize));
288   *where += sizeof (headers->header.a_trsize);
289   md_number_to_chars (*where, headers->header.a_drsize, sizeof (headers->header.a_drsize));
290   *where += sizeof (headers->header.a_drsize);
291
292 #else /* CROSS_COMPILE */
293
294   append (where, (char *) &headers->header, sizeof (headers->header));
295 #endif /* CROSS_COMPILE */
296
297 }
298 #endif /* ! defined (obj_header_append) */
299
300 void
301 obj_symbol_to_chars (where, symbolP)
302      char **where;
303      symbolS *symbolP;
304 {
305   md_number_to_chars ((char *) &(S_GET_OFFSET (symbolP)), S_GET_OFFSET (symbolP), sizeof (S_GET_OFFSET (symbolP)));
306   md_number_to_chars ((char *) &(S_GET_DESC (symbolP)), S_GET_DESC (symbolP), sizeof (S_GET_DESC (symbolP)));
307   md_number_to_chars ((char *) &(symbolP->sy_symbol.n_value), S_GET_VALUE (symbolP), sizeof (symbolP->sy_symbol.n_value));
308
309   append (where, (char *) &symbolP->sy_symbol, sizeof (obj_symbol_type));
310 }
311
312 void
313 obj_emit_symbols (where, symbol_rootP)
314      char **where;
315      symbolS *symbol_rootP;
316 {
317   symbolS *symbolP;
318
319   /* Emit all symbols left in the symbol chain.  */
320   for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
321     {
322       /* Used to save the offset of the name. It is used to point
323          to the string in memory but must be a file offset. */
324       register char *temp;
325
326       temp = S_GET_NAME (symbolP);
327       S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
328
329       /* Any symbol still undefined and is not a dbg symbol is made N_EXT. */
330       if (!S_IS_DEBUG (symbolP) && !S_IS_DEFINED (symbolP))
331         S_SET_EXTERNAL (symbolP);
332
333       /* Adjust the type of a weak symbol.  */
334       if (S_GET_WEAK (symbolP))
335         {
336           switch (S_GET_TYPE (symbolP))
337             {
338             case N_UNDF: S_SET_TYPE (symbolP, N_WEAKU); break;
339             case N_ABS:  S_SET_TYPE (symbolP, N_WEAKA); break;
340             case N_TEXT: S_SET_TYPE (symbolP, N_WEAKT); break;
341             case N_DATA: S_SET_TYPE (symbolP, N_WEAKD); break;
342             case N_BSS:  S_SET_TYPE (symbolP, N_WEAKB); break;
343             default: as_bad (_("%s: bad type for weak symbol"), temp); break;
344             }
345         }
346
347       obj_symbol_to_chars (where, symbolP);
348       S_SET_NAME (symbolP, temp);
349     }
350 }
351
352 #endif /* ! BFD_ASSEMBLER */
353
354 static void
355 obj_aout_line (ignore)
356      int ignore;
357 {
358   /* Assume delimiter is part of expression.
359      BSD4.2 as fails with delightful bug, so we
360      are not being incompatible here. */
361   new_logical_line ((char *) NULL, (int) (get_absolute_expression ()));
362   demand_empty_rest_of_line ();
363 }                               /* obj_aout_line() */
364
365 /* Handle .weak.  This is a GNU extension.  */
366
367 static void
368 obj_aout_weak (ignore)
369      int ignore;
370 {
371   char *name;
372   int c;
373   symbolS *symbolP;
374
375   do
376     {
377       name = input_line_pointer;
378       c = get_symbol_end ();
379       symbolP = symbol_find_or_make (name);
380       *input_line_pointer = c;
381       SKIP_WHITESPACE ();
382       S_SET_WEAK (symbolP);
383       if (c == ',')
384         {
385           input_line_pointer++;
386           SKIP_WHITESPACE ();
387           if (*input_line_pointer == '\n')
388             c = '\n';
389         }
390     }
391   while (c == ',');
392   demand_empty_rest_of_line ();
393 }
394
395 /* Handle .type.  On {Net,Open}BSD, this is used to set the n_other field,
396    which is then apparently used when doing dynamic linking.  Older
397    versions ogas ignored the .type pseudo-op, so we also ignore it if
398    we can't parse it.  */
399
400 static void
401 obj_aout_type (ignore)
402      int ignore;
403 {
404   char *name;
405   int c;
406   symbolS *sym;
407
408   name = input_line_pointer;
409   c = get_symbol_end ();
410   sym = symbol_find (name);
411   *input_line_pointer = c;
412   if (sym != NULL)
413     {
414       SKIP_WHITESPACE ();
415       if (*input_line_pointer == ',')
416         {
417           ++input_line_pointer;
418           SKIP_WHITESPACE ();
419           if (*input_line_pointer == '@')
420             {
421               ++input_line_pointer;
422               if (strncmp (input_line_pointer, "object", 6) == 0)
423                 S_SET_OTHER (sym, 1);
424               else if (strncmp (input_line_pointer, "function", 8) == 0)
425                 S_SET_OTHER (sym, 2);
426             }
427         }
428     }
429
430   /* Ignore everything else on the line.  */
431   s_ignore (0);
432 }
433
434 void
435 obj_read_begin_hook ()
436 {
437 }
438
439 #ifndef BFD_ASSEMBLER
440
441 void
442 obj_crawl_symbol_chain (headers)
443      object_headers *headers;
444 {
445   symbolS *symbolP;
446   symbolS **symbolPP;
447   int symbol_number = 0;
448
449   tc_crawl_symbol_chain (headers);
450
451   symbolPP = &symbol_rootP;     /*->last symbol chain link. */
452   while ((symbolP = *symbolPP) != NULL)
453     {
454       if (symbolP->sy_mri_common)
455         {
456           if (S_IS_EXTERNAL (symbolP))
457             as_bad (_("%s: global symbols not supported in common sections"),
458                     S_GET_NAME (symbolP));
459           *symbolPP = symbol_next (symbolP);
460           continue;
461         }
462
463       if (flag_readonly_data_in_text && (S_GET_SEGMENT (symbolP) == SEG_DATA))
464         {
465           S_SET_SEGMENT (symbolP, SEG_TEXT);
466         }                       /* if pusing data into text */
467
468       resolve_symbol_value (symbolP, 1);
469
470       /* Skip symbols which were equated to undefined or common
471          symbols.  */
472       if (symbolP->sy_value.X_op == O_symbol
473           && (! S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP)))
474         {
475           *symbolPP = symbol_next (symbolP);
476           continue;
477         }
478
479       /* OK, here is how we decide which symbols go out into the brave
480          new symtab.  Symbols that do are:
481
482          * symbols with no name (stabd's?)
483          * symbols with debug info in their N_TYPE
484
485          Symbols that don't are:
486          * symbols that are registers
487          * symbols with \1 as their 3rd character (numeric labels)
488          * "local labels" as defined by S_LOCAL_NAME(name) if the -L
489          switch was passed to gas.
490
491          All other symbols are output.  We complain if a deleted
492          symbol was marked external. */
493
494
495       if (!S_IS_REGISTER (symbolP)
496           && (!S_GET_NAME (symbolP)
497               || S_IS_DEBUG (symbolP)
498               || !S_IS_DEFINED (symbolP)
499               || S_IS_EXTERNAL (symbolP)
500               || (S_GET_NAME (symbolP)[0] != '\001'
501                   && (flag_keep_locals || !S_LOCAL_NAME (symbolP)))))
502         {
503           symbolP->sy_number = symbol_number++;
504
505           /* The + 1 after strlen account for the \0 at the
506                            end of each string */
507           if (!S_IS_STABD (symbolP))
508             {
509               /* Ordinary case. */
510               symbolP->sy_name_offset = string_byte_count;
511               string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
512             }
513           else                  /* .Stabd case. */
514             symbolP->sy_name_offset = 0;
515           symbolPP = &symbolP->sy_next;
516         }
517       else
518         {
519           if (S_IS_EXTERNAL (symbolP) || !S_IS_DEFINED (symbolP))
520             /* This warning should never get triggered any more.
521                Well, maybe if you're doing twisted things with
522                register names...  */
523             {
524               as_bad (_("Local symbol %s never defined."), decode_local_label_name (S_GET_NAME (symbolP)));
525             }                   /* oops. */
526
527           /* Unhook it from the chain */
528           *symbolPP = symbol_next (symbolP);
529         }                       /* if this symbol should be in the output */
530     }                           /* for each symbol */
531
532   H_SET_SYMBOL_TABLE_SIZE (headers, symbol_number);
533 }
534
535 /*
536  * Find strings by crawling along symbol table chain.
537  */
538
539 void
540 obj_emit_strings (where)
541      char **where;
542 {
543   symbolS *symbolP;
544
545 #ifdef CROSS_COMPILE
546   /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
547   md_number_to_chars (*where, string_byte_count, sizeof (string_byte_count));
548   *where += sizeof (string_byte_count);
549 #else /* CROSS_COMPILE */
550   append (where, (char *) &string_byte_count, (unsigned long) sizeof (string_byte_count));
551 #endif /* CROSS_COMPILE */
552
553   for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
554     {
555       if (S_GET_NAME (symbolP))
556         append (&next_object_file_charP, S_GET_NAME (symbolP),
557                 (unsigned long) (strlen (S_GET_NAME (symbolP)) + 1));
558     }                           /* walk symbol chain */
559 }
560
561 #ifndef AOUT_VERSION
562 #define AOUT_VERSION 0
563 #endif
564
565 void
566 obj_pre_write_hook (headers)
567      object_headers *headers;
568 {
569   H_SET_DYNAMIC (headers, 0);
570   H_SET_VERSION (headers, AOUT_VERSION);
571   H_SET_MACHTYPE (headers, AOUT_MACHTYPE);
572   tc_aout_pre_write_hook (headers);
573 }
574
575 void
576 DEFUN_VOID (s_sect)
577 {
578   /* Strip out the section name */
579   char *section_name;
580   char *section_name_end;
581   char c;
582
583   unsigned int len;
584   unsigned int exp;
585   char *save;
586
587   section_name = input_line_pointer;
588   c = get_symbol_end ();
589   section_name_end = input_line_pointer;
590
591   len = section_name_end - section_name;
592   input_line_pointer++;
593   save = input_line_pointer;
594
595   SKIP_WHITESPACE ();
596   if (c == ',')
597     {
598       exp = get_absolute_expression ();
599     }
600   else if (*input_line_pointer == ',')
601     {
602       input_line_pointer++;
603       exp = get_absolute_expression ();
604     }
605   else
606     {
607       input_line_pointer = save;
608       exp = 0;
609     }
610   if (exp >= 1000)
611     {
612       as_bad (_("subsegment index too high"));
613     }
614
615   if (strcmp (section_name, ".text") == 0)
616     {
617       subseg_set (SEG_TEXT, (subsegT) exp);
618     }
619
620   if (strcmp (section_name, ".data") == 0)
621     {
622       if (flag_readonly_data_in_text)
623         subseg_set (SEG_TEXT, (subsegT) exp + 1000);
624       else
625         subseg_set (SEG_DATA, (subsegT) exp);
626     }
627
628   *section_name_end = c;
629 }
630
631 #endif /* ! BFD_ASSEMBLER */
632
633 /* end of obj-aout.c */