* as.h: Replace flagseen with separate variables.
[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 #ifdef BFD_ASSEMBLER
22 #undef NO_RELOC
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_line PARAMS ((int));
65
66 const pseudo_typeS obj_pseudo_table[] =
67 {
68   {"line", obj_aout_line, 0},   /* source code line number */
69   {"ln", obj_aout_line, 0},     /* coff line number that we use anyway */
70
71   /* coff debug pseudos (ignored) */
72   {"def", s_ignore, 0},
73   {"dim", s_ignore, 0},
74   {"endef", s_ignore, 0},
75   {"ident", s_ignore, 0},
76   {"line", s_ignore, 0},
77   {"ln", s_ignore, 0},
78   {"scl", s_ignore, 0},
79   {"size", s_ignore, 0},
80   {"tag", s_ignore, 0},
81   {"type", s_ignore, 0},
82   {"val", s_ignore, 0},
83   {"version", s_ignore, 0},
84
85   {"optim", s_ignore, 0},       /* For sun386i cc (?) */
86
87   /* other stuff */
88   {"ABORT", s_abort, 0},
89
90   {NULL}                        /* end sentinel */
91 };                              /* obj_pseudo_table */
92
93
94 #ifdef BFD_ASSEMBLER
95
96 void
97 obj_aout_frob_symbol (sym, punt)
98      symbolS *sym;
99      int *punt;
100 {
101   flagword flags;
102   asection *sec;
103   int desc, type, other;
104
105   flags = sym->bsym->flags;
106   desc = S_GET_DESC (sym);
107   type = S_GET_TYPE (sym);
108   other = S_GET_OTHER (sym);
109   sec = sym->bsym->section;
110
111   /* Only frob simple symbols this way right now.  */
112   if (! (type & ~ (N_TYPE | N_EXT)))
113     {
114       if (type == (N_UNDF | N_EXT)
115           && sec == &bfd_abs_section)
116         sym->bsym->section = sec = &bfd_und_section;
117
118       if ((type & N_TYPE) != N_INDR
119           && (type & N_TYPE) != N_SETA
120           && (type & N_TYPE) != N_SETT
121           && (type & N_TYPE) != N_SETD
122           && (type & N_TYPE) != N_SETB
123           && type != N_WARNING
124           && (sec == &bfd_abs_section
125               || sec == &bfd_und_section))
126         return;
127       if (flags & BSF_EXPORT)
128         type |= N_EXT;
129
130       switch (type & N_TYPE)
131         {
132         case N_SETA:
133         case N_SETT:
134         case N_SETD:
135         case N_SETB:
136           /* Set the debugging flag for constructor symbols so that
137              BFD leaves them alone.  */
138           sym->bsym->flags |= BSF_DEBUGGING;
139           break;
140         case N_INDR:
141           /* Put indirect symbols in the indirect section.  */
142           sym->bsym->section = &bfd_ind_section;
143           sym->bsym->flags |= BSF_INDIRECT;
144           if (type & N_EXT)
145             {
146               sym->bsym->flags |= BSF_EXPORT;
147               sym->bsym->flags &=~ BSF_LOCAL;
148             }
149           break;
150         case N_WARNING:
151           /* Mark warning symbols.  */
152           sym->bsym->flags |= BSF_WARNING;
153           break;
154         }
155     }
156   else
157     {
158       sym->bsym->flags |= BSF_DEBUGGING;
159     }
160
161   S_SET_TYPE (sym, type);
162 }
163
164 void
165 obj_aout_frob_file ()
166 {
167   /* Relocation processing may require knowing the VMAs of the sections.
168      Since writing to a section will cause the BFD back end to compute the
169      VMAs, fake it out here....  */
170   bfd_byte b = 0;
171   boolean x = true;
172   if (bfd_section_size (stdoutput, text_section) != 0)
173     {
174       x = bfd_set_section_contents (stdoutput, text_section, &b, (file_ptr) 0,
175                                     (bfd_size_type) 1);
176     }
177   else if (bfd_section_size (stdoutput, data_section) != 0)
178     {
179       x = bfd_set_section_contents (stdoutput, data_section, &b, (file_ptr) 0,
180                                     (bfd_size_type) 1);
181     }
182   assert (x == true);
183 }
184
185 #else
186
187 /* Relocation. */
188
189 /*
190  *              emit_relocations()
191  *
192  * Crawl along a fixS chain. Emit the segment's relocations.
193  */
194 void
195 obj_emit_relocations (where, fixP, segment_address_in_file)
196      char **where;
197      fixS *fixP;                /* Fixup chain for this segment. */
198      relax_addressT segment_address_in_file;
199 {
200   for (; fixP; fixP = fixP->fx_next)
201     if (fixP->fx_done == 0)
202       {
203         tc_aout_fix_to_chars (*where, fixP, segment_address_in_file);
204         *where += md_reloc_size;
205       }
206 }
207
208 #ifndef obj_header_append
209 /* Aout file generation & utilities */
210 void
211 obj_header_append (where, headers)
212      char **where;
213      object_headers *headers;
214 {
215   tc_headers_hook (headers);
216
217 #ifdef CROSS_COMPILE
218   md_number_to_chars (*where, headers->header.a_info, sizeof (headers->header.a_info));
219   *where += sizeof (headers->header.a_info);
220   md_number_to_chars (*where, headers->header.a_text, sizeof (headers->header.a_text));
221   *where += sizeof (headers->header.a_text);
222   md_number_to_chars (*where, headers->header.a_data, sizeof (headers->header.a_data));
223   *where += sizeof (headers->header.a_data);
224   md_number_to_chars (*where, headers->header.a_bss, sizeof (headers->header.a_bss));
225   *where += sizeof (headers->header.a_bss);
226   md_number_to_chars (*where, headers->header.a_syms, sizeof (headers->header.a_syms));
227   *where += sizeof (headers->header.a_syms);
228   md_number_to_chars (*where, headers->header.a_entry, sizeof (headers->header.a_entry));
229   *where += sizeof (headers->header.a_entry);
230   md_number_to_chars (*where, headers->header.a_trsize, sizeof (headers->header.a_trsize));
231   *where += sizeof (headers->header.a_trsize);
232   md_number_to_chars (*where, headers->header.a_drsize, sizeof (headers->header.a_drsize));
233   *where += sizeof (headers->header.a_drsize);
234
235 #else /* CROSS_COMPILE */
236
237   append (where, (char *) &headers->header, sizeof (headers->header));
238 #endif /* CROSS_COMPILE */
239
240 }
241 #endif
242
243 void
244 obj_symbol_to_chars (where, symbolP)
245      char **where;
246      symbolS *symbolP;
247 {
248   md_number_to_chars ((char *) &(S_GET_OFFSET (symbolP)), S_GET_OFFSET (symbolP), sizeof (S_GET_OFFSET (symbolP)));
249   md_number_to_chars ((char *) &(S_GET_DESC (symbolP)), S_GET_DESC (symbolP), sizeof (S_GET_DESC (symbolP)));
250   md_number_to_chars ((char *) &(symbolP->sy_symbol.n_value), S_GET_VALUE (symbolP), sizeof (symbolP->sy_symbol.n_value));
251
252   append (where, (char *) &symbolP->sy_symbol, sizeof (obj_symbol_type));
253 }
254
255 void
256 obj_emit_symbols (where, symbol_rootP)
257      char **where;
258      symbolS *symbol_rootP;
259 {
260   symbolS *symbolP;
261
262   /* Emit all symbols left in the symbol chain.  */
263   for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
264     {
265       /* Used to save the offset of the name. It is used to point
266          to the string in memory but must be a file offset. */
267       register char *temp;
268
269       temp = S_GET_NAME (symbolP);
270       S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
271
272       /* Any symbol still undefined and is not a dbg symbol is made N_EXT. */
273       if (!S_IS_DEBUG (symbolP) && !S_IS_DEFINED (symbolP))
274         S_SET_EXTERNAL (symbolP);
275
276       obj_symbol_to_chars (where, symbolP);
277       S_SET_NAME (symbolP, temp);
278     }
279 }
280
281 #endif /* ! BFD_ASSEMBLER */
282
283 static void
284 obj_aout_line (ignore)
285      int ignore;
286 {
287   /* Assume delimiter is part of expression.
288      BSD4.2 as fails with delightful bug, so we
289      are not being incompatible here. */
290   new_logical_line ((char *) NULL, (int) (get_absolute_expression ()));
291   demand_empty_rest_of_line ();
292 }                               /* obj_aout_line() */
293
294 void
295 obj_read_begin_hook ()
296 {
297 }
298
299 #ifndef BFD_ASSEMBLER
300
301 void
302 obj_crawl_symbol_chain (headers)
303      object_headers *headers;
304 {
305   symbolS *symbolP;
306   symbolS **symbolPP;
307   int symbol_number = 0;
308
309   tc_crawl_symbol_chain (headers);
310
311   symbolPP = &symbol_rootP;     /*->last symbol chain link. */
312   while ((symbolP = *symbolPP) != NULL)
313     {
314       if (flag_readonly_data_in_text && (S_GET_SEGMENT (symbolP) == SEG_DATA))
315         {
316           S_SET_SEGMENT (symbolP, SEG_TEXT);
317         }                       /* if pusing data into text */
318
319       resolve_symbol_value (symbolP);
320
321       /* OK, here is how we decide which symbols go out into the brave
322          new symtab.  Symbols that do are:
323
324          * symbols with no name (stabd's?)
325          * symbols with debug info in their N_TYPE
326
327          Symbols that don't are:
328          * symbols that are registers
329          * symbols with \1 as their 3rd character (numeric labels)
330          * "local labels" as defined by S_LOCAL_NAME(name) if the -L
331          switch was passed to gas.
332
333          All other symbols are output.  We complain if a deleted
334          symbol was marked external. */
335
336
337       if (!S_IS_REGISTER (symbolP)
338           && (!S_GET_NAME (symbolP)
339               || S_IS_DEBUG (symbolP)
340               || !S_IS_DEFINED (symbolP)
341               || S_IS_EXTERNAL (symbolP)
342               || (S_GET_NAME (symbolP)[0] != '\001'
343                   && (flag_keep_locals || !S_LOCAL_NAME (symbolP)))))
344         {
345           symbolP->sy_number = symbol_number++;
346
347           /* The + 1 after strlen account for the \0 at the
348                            end of each string */
349           if (!S_IS_STABD (symbolP))
350             {
351               /* Ordinary case. */
352               symbolP->sy_name_offset = string_byte_count;
353               string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
354             }
355           else                  /* .Stabd case. */
356             symbolP->sy_name_offset = 0;
357           symbolPP = &(symbol_next (symbolP));
358         }
359       else
360         {
361           if (S_IS_EXTERNAL (symbolP) || !S_IS_DEFINED (symbolP))
362             /* This warning should never get triggered any more.
363                Well, maybe if you're doing twisted things with
364                register names...  */
365             {
366               as_bad ("Local symbol %s never defined.", decode_local_label_name (S_GET_NAME (symbolP)));
367             }                   /* oops. */
368
369           /* Unhook it from the chain */
370           *symbolPP = symbol_next (symbolP);
371         }                       /* if this symbol should be in the output */
372     }                           /* for each symbol */
373
374   H_SET_SYMBOL_TABLE_SIZE (headers, symbol_number);
375 }
376
377 /*
378  * Find strings by crawling along symbol table chain.
379  */
380
381 void
382 obj_emit_strings (where)
383      char **where;
384 {
385   symbolS *symbolP;
386
387 #ifdef CROSS_COMPILE
388   /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
389   md_number_to_chars (*where, string_byte_count, sizeof (string_byte_count));
390   *where += sizeof (string_byte_count);
391 #else /* CROSS_COMPILE */
392   append (where, (char *) &string_byte_count, (unsigned long) sizeof (string_byte_count));
393 #endif /* CROSS_COMPILE */
394
395   for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
396     {
397       if (S_GET_NAME (symbolP))
398         append (&next_object_file_charP, S_GET_NAME (symbolP),
399                 (unsigned long) (strlen (S_GET_NAME (symbolP)) + 1));
400     }                           /* walk symbol chain */
401 }
402
403 #ifndef AOUT_VERSION
404 #define AOUT_VERSION 0
405 #endif
406
407 void
408 obj_pre_write_hook (headers)
409      object_headers *headers;
410 {
411   H_SET_DYNAMIC (headers, 0);
412   H_SET_VERSION (headers, AOUT_VERSION);
413   H_SET_MACHTYPE (headers, AOUT_MACHTYPE);
414   tc_aout_pre_write_hook (headers);
415 }
416
417 void
418 DEFUN_VOID (s_sect)
419 {
420   /* Strip out the section name */
421   char *section_name;
422   char *section_name_end;
423   char c;
424
425   unsigned int len;
426   unsigned int exp;
427   char *save;
428
429   section_name = input_line_pointer;
430   c = get_symbol_end ();
431   section_name_end = input_line_pointer;
432
433   len = section_name_end - section_name;
434   input_line_pointer++;
435   save = input_line_pointer;
436
437   SKIP_WHITESPACE ();
438   if (c == ',')
439     {
440       exp = get_absolute_expression ();
441     }
442   else if (*input_line_pointer == ',')
443     {
444       input_line_pointer++;
445       exp = get_absolute_expression ();
446     }
447   else
448     {
449       input_line_pointer = save;
450       exp = 0;
451     }
452   if (exp >= 1000)
453     {
454       as_bad ("subsegment index too high");
455     }
456
457   if (strcmp (section_name, ".text") == 0)
458     {
459       subseg_set (SEG_TEXT, (subsegT) exp);
460     }
461
462   if (strcmp (section_name, ".data") == 0)
463     {
464       if (flag_readonly_data_in_text)
465         subseg_set (SEG_TEXT, (subsegT) exp + 1000);
466       else
467         subseg_set (SEG_DATA, (subsegT) exp);
468     }
469
470   *section_name_end = c;
471 }
472
473 #endif /* ! BFD_ASSEMBLER */
474
475 /* end of obj-aout.c */