3a69b3ac |
1 | /* b.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 1, |
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 | /* $Id$ */ |
21 | |
22 | #include "as.h" |
23 | #include "obstack.h" |
24 | |
25 | const short /* in: segT out: N_TYPE bits */ |
26 | seg_N_TYPE[] = { |
27 | N_ABS, |
28 | N_TEXT, |
29 | N_DATA, |
30 | N_BSS, |
31 | N_UNDF, /* unknown */ |
32 | N_UNDF, /* absent */ |
33 | N_UNDF, /* pass1 */ |
34 | N_UNDF, /* error */ |
35 | N_UNDF, /* bignum/flonum */ |
36 | N_UNDF, /* difference */ |
37 | N_REGISTER, /* register */ |
38 | }; |
39 | |
40 | const segT N_TYPE_seg [N_TYPE+2] = { /* N_TYPE == 0x1E = 32-2 */ |
41 | SEG_UNKNOWN, /* N_UNDF == 0 */ |
42 | SEG_GOOF, |
43 | SEG_ABSOLUTE, /* N_ABS == 2 */ |
44 | SEG_GOOF, |
45 | SEG_TEXT, /* N_TEXT == 4 */ |
46 | SEG_GOOF, |
47 | SEG_DATA, /* N_DATA == 6 */ |
48 | SEG_GOOF, |
49 | SEG_BSS, /* N_BSS == 8 */ |
50 | SEG_GOOF, |
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 */ |
55 | SEG_GOOF, |
56 | }; |
57 | |
58 | #ifdef __STDC__ |
59 | static void obj_bout_stab(int what); |
60 | static void obj_bout_line(void); |
61 | static void obj_bout_desc(void); |
62 | #else /* __STDC__ */ |
63 | static void obj_bout_desc(); |
64 | static void obj_bout_stab(); |
65 | static void obj_bout_line(); |
66 | #endif /* __STDC__ */ |
67 | |
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 */ |
75 | |
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 }, |
86 | |
87 | /* other stuff we don't handle */ |
88 | { "ABORT", s_ignore, 0 }, |
89 | { "ident", s_ignore, 0 }, |
90 | |
91 | { NULL} /* end sentinel */ |
92 | }; /* obj_pseudo_table */ |
93 | |
94 | /* Relocation. */ |
95 | |
96 | /* |
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. |
99 | */ |
100 | |
101 | static unsigned char |
102 | nbytes_r_length [] = { |
103 | 42, 0, 1, 42, 2 |
104 | }; |
105 | |
106 | /* |
107 | * emit_relocations() |
108 | * |
109 | * Crawl along a fixS chain. Emit the segment's relocations. |
110 | */ |
111 | void obj_emit_relocations(where, fixP, segment_address_in_file) |
112 | char **where; |
113 | fixS *fixP; /* Fixup chain for this segment. */ |
114 | relax_addressT segment_address_in_file; |
115 | { |
116 | struct reloc_info_generic ri; |
117 | register symbolS * symbolP; |
118 | |
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); |
122 | return; |
123 | } |
124 | |
125 | |
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; |
136 | |
137 | if (S_GET_TYPE(symbolP) == N_UNDF) { |
138 | ri . r_extern = 1; |
139 | ri . r_symbolnum = symbolP->sy_number; |
140 | } else { |
141 | ri . r_extern = 0; |
142 | ri . r_symbolnum = S_GET_TYPE(symbolP); |
143 | } |
144 | |
145 | /* Output the relocation information in machine-dependent form. */ |
146 | md_ri_to_chars(*where, &ri); |
147 | *where += md_reloc_size; |
148 | } |
149 | } |
150 | } /* emit_relocations() */ |
151 | |
152 | /* Aout file generation & utilities */ |
153 | |
154 | /* Convert a lvalue to machine dependent data */ |
155 | void obj_header_append(where, headers) |
156 | char **where; |
157 | object_headers *headers; |
158 | { |
159 | /* Always leave in host byte order */ |
160 | |
161 | headers->header.a_talign = section_alignment[SEG_TEXT]; |
162 | |
163 | if (headers->header.a_talign < 2){ |
164 | headers->header.a_talign = 2; |
165 | } /* force to at least 2 */ |
166 | |
167 | headers->header.a_dalign = section_alignment[SEG_DATA]; |
168 | headers->header.a_balign = section_alignment[SEG_BSS]; |
169 | |
170 | headers->header.a_tload = 0; |
171 | headers->header.a_dload = md_section_align(SEG_DATA, headers->header.a_text); |
172 | |
173 | append(where, (char *) &headers->header, sizeof(headers->header)); |
174 | } /* a_header_append() */ |
175 | |
176 | void obj_symbol_to_chars(where, symbolP) |
177 | char **where; |
178 | symbolS *symbolP; |
179 | { |
180 | /* leave in host byte order */ |
181 | append(where, (char *)&symbolP->sy_symbol, sizeof(obj_symbol_type)); |
182 | } /* obj_symbol_to_chars() */ |
183 | |
184 | void obj_emit_symbols(where, symbol_rootP) |
185 | char **where; |
186 | symbolS *symbol_rootP; |
187 | { |
188 | symbolS * symbolP; |
189 | |
190 | /* |
191 | * Emit all symbols left in the symbol chain. |
192 | */ |
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. */ |
196 | char *temp; |
197 | |
198 | temp = S_GET_NAME(symbolP); |
199 | S_SET_OFFSET(symbolP, symbolP->sy_name_offset); |
200 | |
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); |
203 | |
204 | obj_symbol_to_chars(where, symbolP); |
205 | S_SET_NAME(symbolP,temp); |
206 | } |
207 | } /* emit_symbols() */ |
208 | |
209 | void obj_symbol_new_hook(symbolP) |
210 | symbolS *symbolP; |
211 | { |
212 | S_SET_OTHER(symbolP, 0); |
213 | S_SET_DESC(symbolP, 0); |
214 | return; |
215 | } /* obj_symbol_new_hook() */ |
216 | |
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() */ |
224 | |
225 | /* |
226 | * stab() |
227 | * |
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. |
231 | * |
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.) |
236 | * |
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. |
240 | * |
241 | * .stabX directives always make a symbol table entry. It may be junk if |
242 | * the rest of your .stabX directive is malformed. |
243 | */ |
244 | static void obj_bout_stab(what) |
245 | int what; |
246 | { |
247 | register symbolS * symbolP = 0; |
248 | register char * string; |
249 | int saved_type = 0; |
250 | int length; |
251 | int goof; /* TRUE if we have aborted. */ |
252 | long longint; |
253 | |
254 | /* |
255 | * Enter with input_line_pointer pointing past .stabX and any following |
256 | * whitespace. |
257 | */ |
258 | goof = 0; /* JF who forgot this?? */ |
259 | if (what == 's') { |
260 | string = demand_copy_C_string(& length); |
261 | SKIP_WHITESPACE(); |
262 | if (*input_line_pointer == ',') |
263 | input_line_pointer ++; |
264 | else { |
265 | as_bad("I need a comma after symbol's name"); |
266 | goof = 1; |
267 | } |
268 | } else |
269 | string = ""; |
270 | |
271 | /* |
272 | * Input_line_pointer->after ','. String->symbol name. |
273 | */ |
274 | if (!goof) { |
275 | symbolP = symbol_new(string, |
276 | SEG_UNKNOWN, |
277 | 0, |
278 | (struct frag *)0); |
279 | switch (what) { |
280 | case 'd': |
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; |
285 | break; |
286 | |
287 | case 'n': |
288 | symbolP->sy_frag = &zero_address_frag; |
289 | break; |
290 | |
291 | case 's': |
292 | symbolP->sy_frag = & zero_address_frag; |
293 | break; |
294 | |
295 | default: |
296 | BAD_CASE(what); |
297 | break; |
298 | } |
299 | if (get_absolute_expression_and_terminator(& longint) == ',') |
300 | symbolP->sy_symbol.n_type = saved_type = longint; |
301 | else { |
302 | as_bad("I want a comma after the n_type expression"); |
303 | goof = 1; |
304 | input_line_pointer--; /* Backup over a non-',' char. */ |
305 | } |
306 | } |
307 | if (! goof) { |
308 | if (get_absolute_expression_and_terminator (& longint) == ',') |
309 | S_SET_OTHER(symbolP,longint); |
310 | else { |
311 | as_bad("I want a comma after the n_other expression"); |
312 | goof = 1; |
313 | input_line_pointer--; /* Backup over a non-',' char. */ |
314 | } |
315 | } |
316 | if (! goof) { |
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"); |
321 | goof = 1; |
322 | } else { |
323 | input_line_pointer ++; |
324 | } |
325 | } |
326 | } |
327 | if ((! goof) && (what=='s' || what=='n')) { |
328 | pseudo_set(symbolP); |
329 | symbolP->sy_symbol.n_type = saved_type; |
330 | } |
331 | if (goof) |
332 | ignore_rest_of_line (); |
333 | else |
334 | demand_empty_rest_of_line (); |
335 | } /* obj_bout_stab() */ |
336 | |
337 | static void obj_bout_desc() { |
338 | register char *name; |
339 | register char c; |
340 | register char *p; |
341 | register symbolS * symbolP; |
342 | register int temp; |
343 | |
344 | /* |
345 | * Frob invented at RMS' request. Set the n_desc of a symbol. |
346 | */ |
347 | name = input_line_pointer; |
348 | c = get_symbol_end(); |
349 | p = input_line_pointer; |
350 | * p = c; |
351 | SKIP_WHITESPACE(); |
352 | if (*input_line_pointer != ',') { |
353 | *p = 0; |
354 | as_bad("Expected comma after name \"%s\"", name); |
355 | *p = c; |
356 | ignore_rest_of_line(); |
357 | } else { |
358 | input_line_pointer ++; |
359 | temp = get_absolute_expression (); |
360 | *p = 0; |
361 | symbolP = symbol_find_or_make(name); |
362 | *p = c; |
363 | S_SET_DESC(symbolP,temp); |
364 | } |
365 | demand_empty_rest_of_line(); |
366 | } /* obj_bout_desc() */ |
367 | |
368 | void obj_read_begin_hook() { |
369 | return; |
370 | } /* obj_read_begin_hook() */ |
371 | |
372 | void obj_crawl_symbol_chain(headers) |
373 | object_headers *headers; |
374 | { |
375 | symbolS **symbolPP; |
376 | symbolS *symbolP; |
377 | int symbol_number = 0; |
378 | |
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); |
385 | |
386 | symbolP->sy_forward=0; |
387 | } /* if it has a forward reference */ |
388 | } /* walk the symbol chain */ |
389 | |
390 | tc_crawl_symbol_chain(headers); |
391 | |
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 */ |
397 | |
398 | S_SET_VALUE(symbolP, S_GET_VALUE(symbolP) + symbolP->sy_frag->fr_address); |
399 | |
400 | /* OK, here is how we decide which symbols go out into the |
401 | brave new symtab. Symbols that do are: |
402 | |
403 | * symbols with no name (stabd's?) |
404 | * symbols with debug info in their N_TYPE |
405 | |
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. |
411 | |
412 | All other symbols are output. We complain if a deleted |
413 | symbol was marked external. */ |
414 | |
415 | |
416 | if (1 |
417 | && !S_IS_REGISTER(symbolP) |
418 | && (!S_GET_NAME(symbolP) |
419 | || S_IS_DEBUG(symbolP) |
420 | #ifdef TC_I960 |
421 | /* FIXME-SOON this ifdef seems highly dubious to me. xoxorich. */ |
422 | || !S_IS_DEFINED(symbolP) |
423 | || S_IS_EXTERNAL(symbolP) |
424 | #endif /* TC_I960 */ |
425 | || (S_GET_NAME(symbolP)[0] != '\001' && (flagseen ['L'] || ! S_LOCAL_NAME(symbolP))))) { |
426 | symbolP->sy_number = symbol_number++; |
427 | |
428 | /* The + 1 after strlen account for the \0 at the |
429 | end of each string */ |
430 | if (!S_IS_STABD(symbolP)) { |
431 | /* Ordinary case. */ |
432 | symbolP->sy_name_offset = string_byte_count; |
433 | string_byte_count += strlen(S_GET_NAME(symbolP)) + 1; |
434 | } |
435 | else /* .Stabd case. */ |
436 | symbolP->sy_name_offset = 0; |
437 | symbolPP = &(symbol_next(symbolP)); |
438 | } else { |
439 | if (S_IS_EXTERNAL(symbolP) || !S_IS_DEFINED(symbolP)) { |
440 | as_bad("Local symbol %s never defined", S_GET_NAME(symbolP)); |
441 | } /* oops. */ |
442 | |
443 | /* Unhook it from the chain */ |
444 | *symbolPP = symbol_next(symbolP); |
445 | } /* if this symbol should be in the output */ |
446 | } /* for each symbol */ |
447 | |
448 | H_SET_SYMBOL_TABLE_SIZE(headers, symbol_number); |
449 | |
450 | return; |
451 | } /* obj_crawl_symbol_chain() */ |
452 | |
453 | /* |
454 | * Find strings by crawling along symbol table chain. |
455 | */ |
456 | |
457 | void obj_emit_strings(where) |
458 | char **where; |
459 | { |
460 | symbolS *symbolP; |
461 | |
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 */ |
469 | |
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 */ |
474 | |
475 | return; |
476 | } /* obj_emit_strings() */ |
477 | |
478 | /* |
479 | * Local Variables: |
480 | * comment-column: 0 |
481 | * fill-column: 131 |
482 | * End: |
483 | */ |
484 | |
485 | /* end of obj-bout.c */ |