1 /* b.out object file format
2 Copyright (C) 1989, 1990, 1991 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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 1,
9 or (at your option) any later version.
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.
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. */
25 const short /* in: segT out: N_TYPE bits */
35 N_UNDF
, /* bignum/flonum */
36 N_UNDF
, /* difference */
37 N_REGISTER
, /* register */
40 const segT N_TYPE_seg
[N_TYPE
+2] = { /* N_TYPE == 0x1E = 32-2 */
41 SEG_UNKNOWN
, /* N_UNDF == 0 */
43 SEG_ABSOLUTE
, /* N_ABS == 2 */
45 SEG_TEXT
, /* N_TEXT == 4 */
47 SEG_DATA
, /* N_DATA == 6 */
49 SEG_BSS
, /* N_BSS == 8 */
51 SEG_GOOF
, SEG_GOOF
, SEG_GOOF
, SEG_GOOF
, SEG_GOOF
, SEG_GOOF
, SEG_GOOF
, SEG_GOOF
,
52 SEG_GOOF
, SEG_GOOF
, SEG_GOOF
, SEG_GOOF
, SEG_GOOF
, SEG_GOOF
, SEG_GOOF
, SEG_GOOF
,
53 SEG_GOOF
, SEG_GOOF
, SEG_GOOF
, SEG_GOOF
,
54 SEG_REGISTER
, /* dummy N_REGISTER for regs = 30 */
59 static void obj_bout_stab(int what
);
60 static void obj_bout_line(void);
61 static void obj_bout_desc(void);
63 static void obj_bout_desc();
64 static void obj_bout_stab();
65 static void obj_bout_line();
68 const pseudo_typeS obj_pseudo_table
[] = {
69 /* stabs (aka a.out aka b.out directives for debug symbols) */
70 { "desc", obj_bout_desc
, 0 }, /* def */
71 { "line", obj_bout_line
, 0 }, /* source code line number */
72 { "stabd", obj_bout_stab
, 'd' }, /* stabs */
73 { "stabn", obj_bout_stab
, 'n' }, /* stabs */
74 { "stabs", obj_bout_stab
, 's' }, /* stabs */
76 /* coff debugging directives. Currently ignored silently */
77 { "def", s_ignore
, 0 },
78 { "dim", s_ignore
, 0 },
79 { "endef", s_ignore
, 0 },
80 { "ln", s_ignore
, 0 },
81 { "scl", s_ignore
, 0 },
82 { "size", s_ignore
, 0 },
83 { "tag", s_ignore
, 0 },
84 { "type", s_ignore
, 0 },
85 { "val", s_ignore
, 0 },
87 /* other stuff we don't handle */
88 { "ABORT", s_ignore
, 0 },
89 { "ident", s_ignore
, 0 },
91 { NULL
} /* end sentinel */
92 }; /* obj_pseudo_table */
97 * In: length of relocation (or of address) in chars: 1, 2 or 4.
98 * Out: GNU LD relocation length code: 0, 1, or 2.
102 nbytes_r_length
[] = {
109 * Crawl along a fixS chain. Emit the segment's relocations.
111 void obj_emit_relocations(where
, fixP
, segment_address_in_file
)
113 fixS
*fixP
; /* Fixup chain for this segment. */
114 relax_addressT segment_address_in_file
;
116 struct reloc_info_generic ri
;
117 register symbolS
* symbolP
;
119 /* If a machine dependent emitter is needed, call it instead. */
120 if (md_emit_relocations
) {
121 (*md_emit_relocations
) (fixP
, segment_address_in_file
);
126 /* JF this is for paranoia */
127 bzero((char *)&ri
,sizeof(ri
));
128 for (; fixP
; fixP
= fixP
->fx_next
) {
129 if ((symbolP
= fixP
->fx_addsy
) != 0) {
130 ri
. r_bsr
= fixP
->fx_bsr
;
131 ri
. r_disp
= fixP
->fx_im_disp
;
132 ri
. r_callj
= fixP
->fx_callj
;
133 ri
. r_length
= nbytes_r_length
[fixP
->fx_size
];
134 ri
. r_pcrel
= fixP
->fx_pcrel
;
135 ri
. r_address
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
- segment_address_in_file
;
137 if (S_GET_TYPE(symbolP
) == N_UNDF
) {
139 ri
. r_symbolnum
= symbolP
->sy_number
;
142 ri
. r_symbolnum
= S_GET_TYPE(symbolP
);
145 /* Output the relocation information in machine-dependent form. */
146 md_ri_to_chars(*where
, &ri
);
147 *where
+= md_reloc_size
;
150 } /* emit_relocations() */
152 /* Aout file generation & utilities */
154 /* Convert a lvalue to machine dependent data */
155 void obj_header_append(where
, headers
)
157 object_headers
*headers
;
159 /* Always leave in host byte order */
161 headers
->header
.a_talign
= section_alignment
[SEG_TEXT
];
163 if (headers
->header
.a_talign
< 2){
164 headers
->header
.a_talign
= 2;
165 } /* force to at least 2 */
167 headers
->header
.a_dalign
= section_alignment
[SEG_DATA
];
168 headers
->header
.a_balign
= section_alignment
[SEG_BSS
];
170 headers
->header
.a_tload
= 0;
171 headers
->header
.a_dload
= md_section_align(SEG_DATA
, headers
->header
.a_text
);
173 append(where
, (char *) &headers
->header
, sizeof(headers
->header
));
174 } /* a_header_append() */
176 void obj_symbol_to_chars(where
, symbolP
)
180 /* leave in host byte order */
181 append(where
, (char *)&symbolP
->sy_symbol
, sizeof(obj_symbol_type
));
182 } /* obj_symbol_to_chars() */
184 void obj_emit_symbols(where
, symbol_rootP
)
186 symbolS
*symbol_rootP
;
191 * Emit all symbols left in the symbol chain.
193 for(symbolP
= symbol_rootP
; symbolP
; symbolP
= symbol_next(symbolP
)) {
194 /* Used to save the offset of the name. It is used to point
195 to the string in memory but must be a file offset. */
198 temp
= S_GET_NAME(symbolP
);
199 S_SET_OFFSET(symbolP
, symbolP
->sy_name_offset
);
201 /* Any symbol still undefined and is not a dbg symbol is made N_EXT. */
202 if (!S_IS_DEBUG(symbolP
) && !S_IS_DEFINED(symbolP
)) S_SET_EXTERNAL(symbolP
);
204 obj_symbol_to_chars(where
, symbolP
);
205 S_SET_NAME(symbolP
,temp
);
207 } /* emit_symbols() */
209 void obj_symbol_new_hook(symbolP
)
212 S_SET_OTHER(symbolP
, 0);
213 S_SET_DESC(symbolP
, 0);
215 } /* obj_symbol_new_hook() */
217 static void obj_bout_line() {
218 /* Assume delimiter is part of expression. */
219 /* BSD4.2 as fails with delightful bug, so we */
220 /* are not being incompatible here. */
221 new_logical_line ((char *)NULL
, (int)(get_absolute_expression ()));
222 demand_empty_rest_of_line();
223 } /* obj_bout_line() */
228 * Handle .stabX directives, which used to be open-coded.
229 * So much creeping featurism overloaded the semantics that we decided
230 * to put all .stabX thinking in one place. Here.
232 * We try to make any .stabX directive legal. Other people's AS will often
233 * do assembly-time consistency checks: eg assigning meaning to n_type bits
234 * and "protecting" you from setting them to certain values. (They also zero
235 * certain bits before emitting symbols. Tut tut.)
237 * If an expression is not absolute we either gripe or use the relocation
238 * information. Other people's assemblers silently forget information they
239 * don't need and invent information they need that you didn't supply.
241 * .stabX directives always make a symbol table entry. It may be junk if
242 * the rest of your .stabX directive is malformed.
244 static void obj_bout_stab(what
)
247 register symbolS
* symbolP
= 0;
248 register char * string
;
251 int goof
; /* TRUE if we have aborted. */
255 * Enter with input_line_pointer pointing past .stabX and any following
258 goof
= 0; /* JF who forgot this?? */
260 string
= demand_copy_C_string(& length
);
262 if (*input_line_pointer
== ',')
263 input_line_pointer
++;
265 as_bad("I need a comma after symbol's name");
272 * Input_line_pointer->after ','. String->symbol name.
275 symbolP
= symbol_new(string
,
281 S_SET_NAME(symbolP
,NULL
); /* .stabd feature. */
282 S_SET_VALUE(symbolP
,obstack_next_free(&frags
) -
283 frag_now
->fr_literal
);
284 symbolP
->sy_frag
= frag_now
;
288 symbolP
->sy_frag
= &zero_address_frag
;
292 symbolP
->sy_frag
= & zero_address_frag
;
299 if (get_absolute_expression_and_terminator(& longint
) == ',')
300 symbolP
->sy_symbol
.n_type
= saved_type
= longint
;
302 as_bad("I want a comma after the n_type expression");
304 input_line_pointer
--; /* Backup over a non-',' char. */
308 if (get_absolute_expression_and_terminator (& longint
) == ',')
309 S_SET_OTHER(symbolP
,longint
);
311 as_bad("I want a comma after the n_other expression");
313 input_line_pointer
--; /* Backup over a non-',' char. */
317 S_SET_DESC(symbolP
, get_absolute_expression ());
318 if (what
== 's' || what
== 'n') {
319 if (* input_line_pointer
!= ',') {
320 as_bad("I want a comma after the n_desc expression");
323 input_line_pointer
++;
327 if ((! goof
) && (what
=='s' || what
=='n')) {
329 symbolP
->sy_symbol
.n_type
= saved_type
;
332 ignore_rest_of_line ();
334 demand_empty_rest_of_line ();
335 } /* obj_bout_stab() */
337 static void obj_bout_desc() {
341 register symbolS
* symbolP
;
345 * Frob invented at RMS' request. Set the n_desc of a symbol.
347 name
= input_line_pointer
;
348 c
= get_symbol_end();
349 p
= input_line_pointer
;
352 if (*input_line_pointer
!= ',') {
354 as_bad("Expected comma after name \"%s\"", name
);
356 ignore_rest_of_line();
358 input_line_pointer
++;
359 temp
= get_absolute_expression ();
361 symbolP
= symbol_find_or_make(name
);
363 S_SET_DESC(symbolP
,temp
);
365 demand_empty_rest_of_line();
366 } /* obj_bout_desc() */
368 void obj_read_begin_hook() {
370 } /* obj_read_begin_hook() */
372 void obj_crawl_symbol_chain(headers
)
373 object_headers
*headers
;
377 int symbol_number
= 0;
379 /* JF deal with forward references first... */
380 for (symbolP
= symbol_rootP
; symbolP
; symbolP
= symbol_next(symbolP
)) {
381 if (symbolP
->sy_forward
) {
382 S_SET_VALUE(symbolP
, S_GET_VALUE(symbolP
)
383 + S_GET_VALUE(symbolP
->sy_forward
)
384 + symbolP
->sy_forward
->sy_frag
->fr_address
);
386 symbolP
->sy_forward
=0;
387 } /* if it has a forward reference */
388 } /* walk the symbol chain */
390 tc_crawl_symbol_chain(headers
);
392 symbolPP
= & symbol_rootP
; /*->last symbol chain link. */
393 while ((symbolP
= *symbolPP
) != NULL
) {
394 if (flagseen
['R'] && (S_GET_SEGMENT(symbolP
) == SEG_DATA
)) {
395 S_SET_SEGMENT(symbolP
, SEG_TEXT
);
396 } /* if pusing data into text */
398 S_SET_VALUE(symbolP
, S_GET_VALUE(symbolP
) + symbolP
->sy_frag
->fr_address
);
400 /* OK, here is how we decide which symbols go out into the
401 brave new symtab. Symbols that do are:
403 * symbols with no name (stabd's?)
404 * symbols with debug info in their N_TYPE
406 Symbols that don't are:
407 * symbols that are registers
408 * symbols with \1 as their 3rd character (numeric labels)
409 * "local labels" as defined by S_LOCAL_NAME(name)
410 if the -L switch was passed to gas.
412 All other symbols are output. We complain if a deleted
413 symbol was marked external. */
417 && !S_IS_REGISTER(symbolP
)
418 && (!S_GET_NAME(symbolP
)
419 || S_IS_DEBUG(symbolP
)
421 /* FIXME-SOON this ifdef seems highly dubious to me. xoxorich. */
422 || !S_IS_DEFINED(symbolP
)
423 || S_IS_EXTERNAL(symbolP
)
425 || (S_GET_NAME(symbolP
)[0] != '\001' && (flagseen
['L'] || ! S_LOCAL_NAME(symbolP
))))) {
426 symbolP
->sy_number
= symbol_number
++;
428 /* The + 1 after strlen account for the \0 at the
429 end of each string */
430 if (!S_IS_STABD(symbolP
)) {
432 symbolP
->sy_name_offset
= string_byte_count
;
433 string_byte_count
+= strlen(S_GET_NAME(symbolP
)) + 1;
435 else /* .Stabd case. */
436 symbolP
->sy_name_offset
= 0;
437 symbolPP
= &(symbol_next(symbolP
));
439 if (S_IS_EXTERNAL(symbolP
) || !S_IS_DEFINED(symbolP
)) {
440 as_bad("Local symbol %s never defined", S_GET_NAME(symbolP
));
443 /* Unhook it from the chain */
444 *symbolPP
= symbol_next(symbolP
);
445 } /* if this symbol should be in the output */
446 } /* for each symbol */
448 H_SET_SYMBOL_TABLE_SIZE(headers
, symbol_number
);
451 } /* obj_crawl_symbol_chain() */
454 * Find strings by crawling along symbol table chain.
457 void obj_emit_strings(where
)
462 #ifdef CROSS_ASSEMBLE
463 /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
464 md_number_to_chars(*where
, string_byte_count
, sizeof(string_byte_count
));
465 *where
+= sizeof(string_byte_count
);
466 #else /* CROSS_ASSEMBLE */
467 append(where
, (char *) &string_byte_count
, (unsigned long) sizeof(string_byte_count
));
468 #endif /* CROSS_ASSEMBLE */
470 for(symbolP
= symbol_rootP
; symbolP
; symbolP
= symbol_next(symbolP
)) {
471 if(S_GET_NAME(symbolP
))
472 append(where
, S_GET_NAME(symbolP
), (unsigned long)(strlen (S_GET_NAME(symbolP
)) + 1));
473 } /* walk symbol chain */
476 } /* obj_emit_strings() */
485 /* end of obj-bout.c */