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.
5 This file is part of GAS, the GNU Assembler.
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.
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.
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
25 #include "aout/aout64.h"
30 /* in: segT out: N_TYPE bits */
31 const short seg_N_TYPE[] =
39 N_UNDF, /* expression */
43 N_REGISTER, /* register */
46 const segT N_TYPE_seg[N_TYPE + 2] =
47 { /* N_TYPE == 0x1E = 32-2 */
48 SEG_UNKNOWN, /* N_UNDF == 0 */
50 SEG_ABSOLUTE, /* N_ABS == 2 */
52 SEG_TEXT, /* N_TEXT == 4 */
54 SEG_DATA, /* N_DATA == 6 */
56 SEG_BSS, /* N_BSS == 8 */
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 */
66 static void obj_aout_line PARAMS ((int));
67 static void obj_aout_weak PARAMS ((int));
68 static void obj_aout_type PARAMS ((int));
70 const pseudo_typeS obj_pseudo_table[] =
72 {"line", obj_aout_line, 0}, /* source code line number */
73 {"ln", obj_aout_line, 0}, /* coff line number that we use anyway */
75 {"weak", obj_aout_weak, 0}, /* mark symbol as weak. */
77 {"type", obj_aout_type, 0},
79 /* coff debug pseudos (ignored) */
82 {"endef", s_ignore, 0},
83 {"ident", s_ignore, 0},
84 {"line", s_ignore, 0},
87 {"size", s_ignore, 0},
90 {"version", s_ignore, 0},
92 {"optim", s_ignore, 0}, /* For sun386i cc (?) */
95 {"ABORT", s_abort, 0},
97 {NULL} /* end sentinel */
98 }; /* obj_pseudo_table */
104 obj_aout_frob_symbol (sym, punt)
110 int desc, type, other;
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);
118 /* Only frob simple symbols this way right now. */
119 if (! (type & ~ (N_TYPE | N_EXT)))
121 if (type == (N_UNDF | N_EXT)
122 && sec == &bfd_abs_section)
124 sec = bfd_und_section_ptr;
125 S_SET_SEGMENT (sym, sec);
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
134 && (sec == &bfd_abs_section
135 || sec == &bfd_und_section))
137 if (flags & BSF_EXPORT)
140 switch (type & N_TYPE)
146 /* Set the debugging flag for constructor symbols so that
147 BFD leaves them alone. */
148 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
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"),
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"),
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;
175 symbol_get_bfdsym (sym)->flags |= BSF_EXPORT;
176 symbol_get_bfdsym (sym)->flags &=~ BSF_LOCAL;
180 /* Mark warning symbols. */
181 symbol_get_bfdsym (sym)->flags |= BSF_WARNING;
187 symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
190 S_SET_TYPE (sym, type);
192 /* Double check weak symbols. */
195 if (S_IS_COMMON (sym))
196 as_bad (_("Symbol `%s' can not be both weak and common"),
202 obj_aout_frob_file ()
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.... */
209 if (bfd_section_size (stdoutput, text_section) != 0)
211 x = bfd_set_section_contents (stdoutput, text_section, &b, (file_ptr) 0,
214 else if (bfd_section_size (stdoutput, data_section) != 0)
216 x = bfd_set_section_contents (stdoutput, data_section, &b, (file_ptr) 0,
222 #else /* ! BFD_ASSEMBLER */
229 * Crawl along a fixS chain. Emit the segment's relocations.
232 obj_emit_relocations (where, fixP, segment_address_in_file)
234 fixS *fixP; /* Fixup chain for this segment. */
235 relax_addressT segment_address_in_file;
237 for (; fixP; fixP = fixP->fx_next)
238 if (fixP->fx_done == 0)
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;
248 if (! sym->sy_resolved && ! S_IS_DEFINED (sym))
253 if (expr_symbol_where (sym, &file, &line))
254 as_bad_where (file, line, _("unresolved relocation"));
256 as_bad (_("bad relocation: symbol `%s' not in symbol table"),
260 tc_aout_fix_to_chars (*where, fixP, segment_address_in_file);
261 *where += md_reloc_size;
265 #ifndef obj_header_append
266 /* Aout file generation & utilities */
268 obj_header_append (where, headers)
270 object_headers *headers;
272 tc_headers_hook (headers);
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);
292 #else /* CROSS_COMPILE */
294 append (where, (char *) &headers->header, sizeof (headers->header));
295 #endif /* CROSS_COMPILE */
298 #endif /* ! defined (obj_header_append) */
301 obj_symbol_to_chars (where, symbolP)
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));
309 append (where, (char *) &symbolP->sy_symbol, sizeof (obj_symbol_type));
313 obj_emit_symbols (where, symbol_rootP)
315 symbolS *symbol_rootP;
319 /* Emit all symbols left in the symbol chain. */
320 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
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. */
326 temp = S_GET_NAME (symbolP);
327 S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
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);
333 /* Adjust the type of a weak symbol. */
334 if (S_GET_WEAK (symbolP))
336 switch (S_GET_TYPE (symbolP))
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;
347 obj_symbol_to_chars (where, symbolP);
348 S_SET_NAME (symbolP, temp);
352 #endif /* ! BFD_ASSEMBLER */
355 obj_aout_line (ignore)
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() */
365 /* Handle .weak. This is a GNU extension. */
368 obj_aout_weak (ignore)
377 name = input_line_pointer;
378 c = get_symbol_end ();
379 symbolP = symbol_find_or_make (name);
380 *input_line_pointer = c;
382 S_SET_WEAK (symbolP);
385 input_line_pointer++;
387 if (*input_line_pointer == '\n')
392 demand_empty_rest_of_line ();
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. */
401 obj_aout_type (ignore)
408 name = input_line_pointer;
409 c = get_symbol_end ();
410 sym = symbol_find (name);
411 *input_line_pointer = c;
415 if (*input_line_pointer == ',')
417 ++input_line_pointer;
419 if (*input_line_pointer == '@')
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);
430 /* Ignore everything else on the line. */
435 obj_read_begin_hook ()
439 #ifndef BFD_ASSEMBLER
442 obj_crawl_symbol_chain (headers)
443 object_headers *headers;
447 int symbol_number = 0;
449 tc_crawl_symbol_chain (headers);
451 symbolPP = &symbol_rootP; /*->last symbol chain link. */
452 while ((symbolP = *symbolPP) != NULL)
454 if (symbolP->sy_mri_common)
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);
463 if (flag_readonly_data_in_text && (S_GET_SEGMENT (symbolP) == SEG_DATA))
465 S_SET_SEGMENT (symbolP, SEG_TEXT);
466 } /* if pusing data into text */
468 resolve_symbol_value (symbolP, 1);
470 /* Skip symbols which were equated to undefined or common
472 if (symbolP->sy_value.X_op == O_symbol
473 && (! S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP)))
475 *symbolPP = symbol_next (symbolP);
479 /* OK, here is how we decide which symbols go out into the brave
480 new symtab. Symbols that do are:
482 * symbols with no name (stabd's?)
483 * symbols with debug info in their N_TYPE
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.
491 All other symbols are output. We complain if a deleted
492 symbol was marked external. */
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)))))
503 symbolP->sy_number = symbol_number++;
505 /* The + 1 after strlen account for the \0 at the
506 end of each string */
507 if (!S_IS_STABD (symbolP))
510 symbolP->sy_name_offset = string_byte_count;
511 string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
513 else /* .Stabd case. */
514 symbolP->sy_name_offset = 0;
515 symbolPP = &symbolP->sy_next;
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
524 as_bad (_("Local symbol %s never defined."), decode_local_label_name (S_GET_NAME (symbolP)));
527 /* Unhook it from the chain */
528 *symbolPP = symbol_next (symbolP);
529 } /* if this symbol should be in the output */
530 } /* for each symbol */
532 H_SET_SYMBOL_TABLE_SIZE (headers, symbol_number);
536 * Find strings by crawling along symbol table chain.
540 obj_emit_strings (where)
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 */
553 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
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 */
562 #define AOUT_VERSION 0
566 obj_pre_write_hook (headers)
567 object_headers *headers;
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);
578 /* Strip out the section name */
580 char *section_name_end;
587 section_name = input_line_pointer;
588 c = get_symbol_end ();
589 section_name_end = input_line_pointer;
591 len = section_name_end - section_name;
592 input_line_pointer++;
593 save = input_line_pointer;
598 exp = get_absolute_expression ();
600 else if (*input_line_pointer == ',')
602 input_line_pointer++;
603 exp = get_absolute_expression ();
607 input_line_pointer = save;
612 as_bad (_("subsegment index too high"));
615 if (strcmp (section_name, ".text") == 0)
617 subseg_set (SEG_TEXT, (subsegT) exp);
620 if (strcmp (section_name, ".data") == 0)
622 if (flag_readonly_data_in_text)
623 subseg_set (SEG_TEXT, (subsegT) exp + 1000);
625 subseg_set (SEG_DATA, (subsegT) exp);
628 *section_name_end = c;
631 #endif /* ! BFD_ASSEMBLER */
633 /* end of obj-aout.c */