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