1 /* a.out object file format
2 Copyright (C) 1989, 90, 91, 92, 93, 94, 95, 1996
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
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. */
24 #include "aout/aout64.h"
29 /* in: segT out: N_TYPE bits */
30 const short seg_N_TYPE[] =
38 N_UNDF, /* expression */
42 N_REGISTER, /* register */
45 const segT N_TYPE_seg[N_TYPE + 2] =
46 { /* N_TYPE == 0x1E = 32-2 */
47 SEG_UNKNOWN, /* N_UNDF == 0 */
49 SEG_ABSOLUTE, /* N_ABS == 2 */
51 SEG_TEXT, /* N_TEXT == 4 */
53 SEG_DATA, /* N_DATA == 6 */
55 SEG_BSS, /* N_BSS == 8 */
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 */
65 static void obj_aout_line PARAMS ((int));
66 static void obj_aout_weak PARAMS ((int));
67 static void obj_aout_type PARAMS ((int));
69 const pseudo_typeS obj_pseudo_table[] =
71 {"line", obj_aout_line, 0}, /* source code line number */
72 {"ln", obj_aout_line, 0}, /* coff line number that we use anyway */
74 {"weak", obj_aout_weak, 0}, /* mark symbol as weak. */
76 {"type", obj_aout_type, 0},
78 /* coff debug pseudos (ignored) */
81 {"endef", s_ignore, 0},
82 {"ident", s_ignore, 0},
83 {"line", s_ignore, 0},
86 {"size", s_ignore, 0},
89 {"version", s_ignore, 0},
91 {"optim", s_ignore, 0}, /* For sun386i cc (?) */
94 {"ABORT", s_abort, 0},
96 {NULL} /* end sentinel */
97 }; /* obj_pseudo_table */
103 obj_aout_frob_symbol (sym, punt)
109 int desc, type, other;
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;
117 /* Only frob simple symbols this way right now. */
118 if (! (type & ~ (N_TYPE | N_EXT)))
120 if (type == (N_UNDF | N_EXT)
121 && sec == &bfd_abs_section)
122 sym->bsym->section = sec = bfd_und_section_ptr;
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
130 && (sec == &bfd_abs_section
131 || sec == &bfd_und_section))
133 if (flags & BSF_EXPORT)
136 switch (type & N_TYPE)
142 /* Set the debugging flag for constructor symbols so that
143 BFD leaves them alone. */
144 sym->bsym->flags |= BSF_DEBUGGING;
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"),
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"),
166 /* Put indirect symbols in the indirect section. */
167 sym->bsym->section = bfd_ind_section_ptr;
168 sym->bsym->flags |= BSF_INDIRECT;
171 sym->bsym->flags |= BSF_EXPORT;
172 sym->bsym->flags &=~ BSF_LOCAL;
176 /* Mark warning symbols. */
177 sym->bsym->flags |= BSF_WARNING;
183 sym->bsym->flags |= BSF_DEBUGGING;
186 S_SET_TYPE (sym, type);
188 /* Double check weak symbols. */
189 if (sym->bsym->flags & BSF_WEAK)
191 if (S_IS_COMMON (sym))
192 as_bad (_("Symbol `%s' can not be both weak and common"),
198 obj_aout_frob_file ()
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.... */
205 if (bfd_section_size (stdoutput, text_section) != 0)
207 x = bfd_set_section_contents (stdoutput, text_section, &b, (file_ptr) 0,
210 else if (bfd_section_size (stdoutput, data_section) != 0)
212 x = bfd_set_section_contents (stdoutput, data_section, &b, (file_ptr) 0,
225 * Crawl along a fixS chain. Emit the segment's relocations.
228 obj_emit_relocations (where, fixP, segment_address_in_file)
230 fixS *fixP; /* Fixup chain for this segment. */
231 relax_addressT segment_address_in_file;
233 for (; fixP; fixP = fixP->fx_next)
234 if (fixP->fx_done == 0)
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;
244 if (! sym->sy_resolved && ! S_IS_DEFINED (sym))
249 if (expr_symbol_where (sym, &file, &line))
250 as_bad_where (file, line, _("unresolved relocation"));
252 as_bad (_("bad relocation: symbol `%s' not in symbol table"),
256 tc_aout_fix_to_chars (*where, fixP, segment_address_in_file);
257 *where += md_reloc_size;
261 #ifndef obj_header_append
262 /* Aout file generation & utilities */
264 obj_header_append (where, headers)
266 object_headers *headers;
268 tc_headers_hook (headers);
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);
288 #else /* CROSS_COMPILE */
290 append (where, (char *) &headers->header, sizeof (headers->header));
291 #endif /* CROSS_COMPILE */
297 obj_symbol_to_chars (where, symbolP)
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));
305 append (where, (char *) &symbolP->sy_symbol, sizeof (obj_symbol_type));
309 obj_emit_symbols (where, symbol_rootP)
311 symbolS *symbol_rootP;
315 /* Emit all symbols left in the symbol chain. */
316 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
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. */
322 temp = S_GET_NAME (symbolP);
323 S_SET_OFFSET (symbolP, symbolP->sy_name_offset);
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);
329 /* Adjust the type of a weak symbol. */
330 if (S_GET_WEAK (symbolP))
332 switch (S_GET_TYPE (symbolP))
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;
343 obj_symbol_to_chars (where, symbolP);
344 S_SET_NAME (symbolP, temp);
348 #endif /* ! BFD_ASSEMBLER */
351 obj_aout_line (ignore)
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() */
361 /* Handle .weak. This is a GNU extension. */
364 obj_aout_weak (ignore)
373 name = input_line_pointer;
374 c = get_symbol_end ();
375 symbolP = symbol_find_or_make (name);
376 *input_line_pointer = c;
378 S_SET_WEAK (symbolP);
381 input_line_pointer++;
383 if (*input_line_pointer == '\n')
388 demand_empty_rest_of_line ();
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. */
397 obj_aout_type (ignore)
404 name = input_line_pointer;
405 c = get_symbol_end ();
406 sym = symbol_find (name);
407 *input_line_pointer = c;
411 if (*input_line_pointer == ',')
413 ++input_line_pointer;
415 if (*input_line_pointer == '@')
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);
426 /* Ignore everything else on the line. */
431 obj_read_begin_hook ()
435 #ifndef BFD_ASSEMBLER
438 obj_crawl_symbol_chain (headers)
439 object_headers *headers;
443 int symbol_number = 0;
445 tc_crawl_symbol_chain (headers);
447 symbolPP = &symbol_rootP; /*->last symbol chain link. */
448 while ((symbolP = *symbolPP) != NULL)
450 if (symbolP->sy_mri_common)
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);
459 if (flag_readonly_data_in_text && (S_GET_SEGMENT (symbolP) == SEG_DATA))
461 S_SET_SEGMENT (symbolP, SEG_TEXT);
462 } /* if pusing data into text */
464 resolve_symbol_value (symbolP, 1);
466 /* Skip symbols which were equated to undefined or common
468 if (symbolP->sy_value.X_op == O_symbol
469 && (! S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP)))
471 *symbolPP = symbol_next (symbolP);
475 /* OK, here is how we decide which symbols go out into the brave
476 new symtab. Symbols that do are:
478 * symbols with no name (stabd's?)
479 * symbols with debug info in their N_TYPE
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.
487 All other symbols are output. We complain if a deleted
488 symbol was marked external. */
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)))))
499 symbolP->sy_number = symbol_number++;
501 /* The + 1 after strlen account for the \0 at the
502 end of each string */
503 if (!S_IS_STABD (symbolP))
506 symbolP->sy_name_offset = string_byte_count;
507 string_byte_count += strlen (S_GET_NAME (symbolP)) + 1;
509 else /* .Stabd case. */
510 symbolP->sy_name_offset = 0;
511 symbolPP = &(symbol_next (symbolP));
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
520 as_bad (_("Local symbol %s never defined."), decode_local_label_name (S_GET_NAME (symbolP)));
523 /* Unhook it from the chain */
524 *symbolPP = symbol_next (symbolP);
525 } /* if this symbol should be in the output */
526 } /* for each symbol */
528 H_SET_SYMBOL_TABLE_SIZE (headers, symbol_number);
532 * Find strings by crawling along symbol table chain.
536 obj_emit_strings (where)
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 */
549 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
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 */
558 #define AOUT_VERSION 0
562 obj_pre_write_hook (headers)
563 object_headers *headers;
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);
574 /* Strip out the section name */
576 char *section_name_end;
583 section_name = input_line_pointer;
584 c = get_symbol_end ();
585 section_name_end = input_line_pointer;
587 len = section_name_end - section_name;
588 input_line_pointer++;
589 save = input_line_pointer;
594 exp = get_absolute_expression ();
596 else if (*input_line_pointer == ',')
598 input_line_pointer++;
599 exp = get_absolute_expression ();
603 input_line_pointer = save;
608 as_bad (_("subsegment index too high"));
611 if (strcmp (section_name, ".text") == 0)
613 subseg_set (SEG_TEXT, (subsegT) exp);
616 if (strcmp (section_name, ".data") == 0)
618 if (flag_readonly_data_in_text)
619 subseg_set (SEG_TEXT, (subsegT) exp + 1000);
621 subseg_set (SEG_DATA, (subsegT) exp);
624 *section_name_end = c;
627 #endif /* ! BFD_ASSEMBLER */
629 /* end of obj-aout.c */