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