Fri Aug 28 16:25:22 1992 Ian Lance Taylor (ian@cygnus.com)
[deliverable/binutils-gdb.git] / gas / config / obj-bout.c
1 /* b.out object file format
2 Copyright (C) 1989, 1990, 1991, 1992 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 "obstack.h"
22 #include "aout/stab_gnu.h"
23 const short /* in: segT out: N_TYPE bits */
24 seg_N_TYPE[] = {
25 N_ABS,
26 N_TEXT,
27 N_DATA,
28 N_BSS,
29 N_UNDF, /* unknown */
30 N_UNDF, /* absent */
31 N_UNDF, /* pass1 */
32 N_UNDF, /* error */
33 N_UNDF, /* bignum/flonum */
34 N_UNDF, /* difference */
35 N_REGISTER, /* register */
36 };
37
38 const segT N_TYPE_seg [N_TYPE+2] = { /* N_TYPE == 0x1E = 32-2 */
39 SEG_UNKNOWN, /* N_UNDF == 0 */
40 SEG_GOOF,
41 SEG_ABSOLUTE, /* N_ABS == 2 */
42 SEG_GOOF,
43 SEG_TEXT, /* N_TEXT == 4 */
44 SEG_GOOF,
45 SEG_DATA, /* N_DATA == 6 */
46 SEG_GOOF,
47 SEG_BSS, /* N_BSS == 8 */
48 SEG_GOOF,
49 SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
50 SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
51 SEG_GOOF, SEG_GOOF, SEG_GOOF, SEG_GOOF,
52 SEG_REGISTER, /* dummy N_REGISTER for regs = 30 */
53 SEG_GOOF,
54 };
55
56 #if __STDC__ == 1
57 static void obj_bout_stab(int what);
58 static void obj_bout_line(void);
59 static void obj_bout_desc(void);
60 #else /* not __STDC__ */
61 static void obj_bout_desc();
62 static void obj_bout_stab();
63 static void obj_bout_line();
64 #endif /* not __STDC__ */
65
66 const pseudo_typeS obj_pseudo_table[] = {
67 /* stabs (aka a.out aka b.out directives for debug symbols) */
68 { "desc", obj_bout_desc, 0 }, /* def */
69 { "line", obj_bout_line, 0 }, /* source code line number */
70 { "stabd", obj_bout_stab, 'd' }, /* stabs */
71 { "stabn", obj_bout_stab, 'n' }, /* stabs */
72 { "stabs", obj_bout_stab, 's' }, /* stabs */
73
74 /* coff debugging directives. Currently ignored silently */
75 { "def", s_ignore, 0 },
76 { "dim", s_ignore, 0 },
77 { "endef", s_ignore, 0 },
78 { "ln", s_ignore, 0 },
79 { "scl", s_ignore, 0 },
80 { "size", s_ignore, 0 },
81 { "tag", s_ignore, 0 },
82 { "type", s_ignore, 0 },
83 { "val", s_ignore, 0 },
84
85 /* other stuff we don't handle */
86 { "ABORT", s_ignore, 0 },
87 { "ident", s_ignore, 0 },
88
89 { NULL} /* end sentinel */
90 }; /* obj_pseudo_table */
91
92 /* Relocation. */
93
94 /*
95 * emit_relocations()
96 *
97 * Crawl along a fixS chain. Emit the segment's relocations.
98 */
99 void obj_emit_relocations(where, fixP, segment_address_in_file)
100 char **where;
101 fixS *fixP; /* Fixup chain for this segment. */
102 relax_addressT segment_address_in_file;
103 {
104 for (; fixP; fixP = fixP->fx_next) {
105 if (fixP->fx_addsy != NULL
106 || fixP->fx_r_type != NO_RELOC) {
107 tc_bout_fix_to_chars(*where, fixP, segment_address_in_file);
108 *where += sizeof(struct relocation_info);
109 } /* if there's a symbol */
110 } /* for each fixup */
111
112 } /* emit_relocations() */
113
114 /* Aout file generation & utilities */
115
116 /* Convert a lvalue to machine dependent data */
117 void obj_header_append(where, headers)
118 char **where;
119 object_headers *headers;
120 {
121 /* Always leave in host byte order */
122
123 headers->header.a_talign = section_alignment[SEG_TEXT];
124
125 if (headers->header.a_talign < 2){
126 headers->header.a_talign = 2;
127 } /* force to at least 2 */
128
129 headers->header.a_dalign = section_alignment[SEG_DATA];
130 headers->header.a_balign = section_alignment[SEG_BSS];
131
132 headers->header.a_tload = 0;
133 headers->header.a_dload = md_section_align(SEG_DATA, H_GET_TEXT_SIZE(headers));
134
135 headers->header.a_relaxable = linkrelax;
136
137 #ifdef CROSS_COMPILE
138 md_number_to_chars(*where, headers->header.a_magic, sizeof(headers->header.a_magic));
139 *where += sizeof(headers->header.a_magic);
140 md_number_to_chars(*where, headers->header.a_text, sizeof(headers->header.a_text));
141 *where += sizeof(headers->header.a_text);
142 md_number_to_chars(*where, headers->header.a_data, sizeof(headers->header.a_data));
143 *where += sizeof(headers->header.a_data);
144 md_number_to_chars(*where, headers->header.a_bss, sizeof(headers->header.a_bss));
145 *where += sizeof(headers->header.a_bss);
146 md_number_to_chars(*where, headers->header.a_syms, sizeof(headers->header.a_syms));
147 *where += sizeof(headers->header.a_syms);
148 md_number_to_chars(*where, headers->header.a_entry, sizeof(headers->header.a_entry));
149 *where += sizeof(headers->header.a_entry);
150 md_number_to_chars(*where, headers->header.a_trsize, sizeof(headers->header.a_trsize));
151 *where += sizeof(headers->header.a_trsize);
152 md_number_to_chars(*where, headers->header.a_drsize, sizeof(headers->header.a_drsize));
153 *where += sizeof(headers->header.a_drsize);
154 md_number_to_chars(*where, headers->header.a_tload, sizeof(headers->header.a_tload));
155 *where += sizeof(headers->header.a_tload);
156 md_number_to_chars(*where, headers->header.a_dload, sizeof(headers->header.a_dload));
157 *where += sizeof(headers->header.a_dload);
158 md_number_to_chars(*where, headers->header.a_talign, sizeof(headers->header.a_talign));
159 *where += sizeof(headers->header.a_talign);
160 md_number_to_chars(*where, headers->header.a_dalign, sizeof(headers->header.a_dalign));
161 *where += sizeof(headers->header.a_dalign);
162 md_number_to_chars(*where, headers->header.a_balign, sizeof(headers->header.a_balign));
163 *where += sizeof(headers->header.a_balign);
164 md_number_to_chars(*where, headers->header.a_relaxable, sizeof(headers->header.a_relaxable));
165 *where += sizeof(headers->header.a_relaxable);
166 #else /* ! CROSS_COMPILE */
167 append(where, (char *) &headers->header, sizeof(headers->header));
168 #endif /* ! CROSS_COMPILE */
169 } /* a_header_append() */
170
171 void obj_symbol_to_chars(where, symbolP)
172 char **where;
173 symbolS *symbolP;
174 {
175 md_number_to_chars((char *)&(S_GET_OFFSET(symbolP)), S_GET_OFFSET(symbolP), sizeof(S_GET_OFFSET(symbolP)));
176 md_number_to_chars((char *)&(S_GET_DESC(symbolP)), S_GET_DESC(symbolP), sizeof(S_GET_DESC(symbolP)));
177 md_number_to_chars((char *)&(S_GET_VALUE(symbolP)), S_GET_VALUE(symbolP), sizeof(S_GET_VALUE(symbolP)));
178
179 append(where, (char *)&symbolP->sy_symbol, sizeof(obj_symbol_type));
180 } /* obj_symbol_to_chars() */
181
182 void obj_emit_symbols(where, symbol_rootP)
183 char **where;
184 symbolS *symbol_rootP;
185 {
186 symbolS * symbolP;
187
188 /*
189 * Emit all symbols left in the symbol chain.
190 */
191 for(symbolP = symbol_rootP; symbolP; symbolP = symbol_next(symbolP)) {
192 /* Used to save the offset of the name. It is used to point
193 to the string in memory but must be a file offset. */
194 char *temp;
195
196 temp = S_GET_NAME(symbolP);
197 S_SET_OFFSET(symbolP, symbolP->sy_name_offset);
198
199 /* Any symbol still undefined and is not a dbg symbol is made N_EXT. */
200 if (!S_IS_DEBUG(symbolP) && !S_IS_DEFINED(symbolP)) S_SET_EXTERNAL(symbolP);
201
202 obj_symbol_to_chars(where, symbolP);
203 S_SET_NAME(symbolP,temp);
204 }
205 } /* emit_symbols() */
206
207 void obj_symbol_new_hook(symbolP)
208 symbolS *symbolP;
209 {
210 S_SET_OTHER(symbolP, 0);
211 S_SET_DESC(symbolP, 0);
212 return;
213 } /* obj_symbol_new_hook() */
214
215 static void obj_bout_line() {
216 /* Assume delimiter is part of expression. */
217 /* BSD4.2 as fails with delightful bug, so we */
218 /* are not being incompatible here. */
219 new_logical_line ((char *)NULL, (int)(get_absolute_expression ()));
220 demand_empty_rest_of_line();
221 } /* obj_bout_line() */
222
223 /*
224 * stab()
225 *
226 * Handle .stabX directives, which used to be open-coded.
227 * So much creeping featurism overloaded the semantics that we decided
228 * to put all .stabX thinking in one place. Here.
229 *
230 * We try to make any .stabX directive legal. Other people's AS will often
231 * do assembly-time consistency checks: eg assigning meaning to n_type bits
232 * and "protecting" you from setting them to certain values. (They also zero
233 * certain bits before emitting symbols. Tut tut.)
234 *
235 * If an expression is not absolute we either gripe or use the relocation
236 * information. Other people's assemblers silently forget information they
237 * don't need and invent information they need that you didn't supply.
238 *
239 * .stabX directives always make a symbol table entry. It may be junk if
240 * the rest of your .stabX directive is malformed.
241 */
242 static void obj_bout_stab(what)
243 int what;
244 {
245 register symbolS * symbolP = 0;
246 register char * string;
247 int saved_type = 0;
248 int length;
249 int goof; /* TRUE if we have aborted. */
250 long longint;
251
252 /*
253 * Enter with input_line_pointer pointing past .stabX and any following
254 * whitespace.
255 */
256 goof = 0; /* JF who forgot this?? */
257 if (what == 's') {
258 string = demand_copy_C_string(& length);
259 SKIP_WHITESPACE();
260 if (*input_line_pointer == ',')
261 input_line_pointer ++;
262 else {
263 as_bad("I need a comma after symbol's name");
264 goof = 1;
265 }
266 } else
267 string = "";
268
269 /*
270 * Input_line_pointer->after ','. String->symbol name.
271 */
272 if (!goof) {
273 symbolP = symbol_new(string,
274 SEG_UNKNOWN,
275 0,
276 (struct frag *)0);
277 switch (what) {
278 case 'd':
279 S_SET_NAME(symbolP,NULL); /* .stabd feature. */
280 S_SET_VALUE(symbolP,obstack_next_free(&frags) -
281 frag_now->fr_literal);
282 symbolP->sy_frag = frag_now;
283 break;
284
285 case 'n':
286 symbolP->sy_frag = &zero_address_frag;
287 break;
288
289 case 's':
290 symbolP->sy_frag = & zero_address_frag;
291 break;
292
293 default:
294 BAD_CASE(what);
295 break;
296 }
297 if (get_absolute_expression_and_terminator(& longint) == ',')
298 symbolP->sy_symbol.n_type = saved_type = longint;
299 else {
300 as_bad("I want a comma after the n_type expression");
301 goof = 1;
302 input_line_pointer--; /* Backup over a non-',' char. */
303 }
304 }
305 if (! goof) {
306 if (get_absolute_expression_and_terminator (& longint) == ',')
307 S_SET_OTHER(symbolP,longint);
308 else {
309 as_bad("I want a comma after the n_other expression");
310 goof = 1;
311 input_line_pointer--; /* Backup over a non-',' char. */
312 }
313 }
314 if (! goof) {
315 S_SET_DESC(symbolP, get_absolute_expression ());
316 if (what == 's' || what == 'n') {
317 if (* input_line_pointer != ',') {
318 as_bad("I want a comma after the n_desc expression");
319 goof = 1;
320 } else {
321 input_line_pointer ++;
322 }
323 }
324 }
325 if ((! goof) && (what=='s' || what=='n')) {
326 pseudo_set(symbolP);
327 symbolP->sy_symbol.n_type = saved_type;
328 }
329 #ifndef NO_LISTING
330 {
331 extern int listing;
332
333 if (listing && !goof)
334 {
335 if (symbolP->sy_symbol.n_type == N_SLINE)
336 {
337
338 listing_source_line(symbolP->sy_symbol.n_desc);
339 }
340 else if (symbolP->sy_symbol.n_type == N_SO
341 || symbolP->sy_symbol.n_type == N_SOL)
342 {
343 listing_source_file(string);
344 }
345 }
346 }
347
348 #endif
349
350 if (goof)
351 ignore_rest_of_line ();
352 else
353 demand_empty_rest_of_line ();
354 } /* obj_bout_stab() */
355
356 static void obj_bout_desc() {
357 register char *name;
358 register char c;
359 register char *p;
360 register symbolS * symbolP;
361 register int temp;
362
363 /*
364 * Frob invented at RMS' request. Set the n_desc of a symbol.
365 */
366 name = input_line_pointer;
367 c = get_symbol_end();
368 p = input_line_pointer;
369 * p = c;
370 SKIP_WHITESPACE();
371 if (*input_line_pointer != ',') {
372 *p = 0;
373 as_bad("Expected comma after name \"%s\"", name);
374 *p = c;
375 ignore_rest_of_line();
376 } else {
377 input_line_pointer ++;
378 temp = get_absolute_expression ();
379 *p = 0;
380 symbolP = symbol_find_or_make(name);
381 *p = c;
382 S_SET_DESC(symbolP,temp);
383 }
384 demand_empty_rest_of_line();
385 } /* obj_bout_desc() */
386
387 void obj_read_begin_hook() {
388 return;
389 } /* obj_read_begin_hook() */
390
391 void obj_crawl_symbol_chain(headers)
392 object_headers *headers;
393 {
394 symbolS **symbolPP;
395 symbolS *symbolP;
396 int symbol_number = 0;
397
398 /* JF deal with forward references first... */
399 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next(symbolP)) {
400 if (symbolP->sy_forward) {
401 S_SET_VALUE(symbolP, S_GET_VALUE(symbolP)
402 + S_GET_VALUE(symbolP->sy_forward)
403 + symbolP->sy_forward->sy_frag->fr_address);
404
405 symbolP->sy_forward=0;
406 } /* if it has a forward reference */
407 } /* walk the symbol chain */
408
409 tc_crawl_symbol_chain(headers);
410
411 symbolPP = & symbol_rootP; /*->last symbol chain link. */
412 while ((symbolP = *symbolPP) != NULL) {
413 if (flagseen['R'] && (S_GET_SEGMENT(symbolP) == SEG_DATA)) {
414 S_SET_SEGMENT(symbolP, SEG_TEXT);
415 } /* if pusing data into text */
416
417 S_SET_VALUE(symbolP, S_GET_VALUE(symbolP) + symbolP->sy_frag->fr_address);
418
419 /* OK, here is how we decide which symbols go out into the
420 brave new symtab. Symbols that do are:
421
422 * symbols with no name (stabd's?)
423 * symbols with debug info in their N_TYPE
424
425 Symbols that don't are:
426 * symbols that are registers
427 * symbols with \1 as their 3rd character (numeric labels)
428 * "local labels" as defined by S_LOCAL_NAME(name)
429 if the -L switch was passed to gas.
430
431 All other symbols are output. We complain if a deleted
432 symbol was marked external. */
433
434
435 if (1
436 && !S_IS_REGISTER(symbolP)
437 && (!S_GET_NAME(symbolP)
438 || S_IS_DEBUG(symbolP)
439 #ifdef TC_I960
440 /* FIXME-SOON this ifdef seems highly dubious to me. xoxorich. */
441 || !S_IS_DEFINED(symbolP)
442 || S_IS_EXTERNAL(symbolP)
443 #endif /* TC_I960 */
444 || (S_GET_NAME(symbolP)[0] != '\001' && (flagseen ['L'] || ! S_LOCAL_NAME(symbolP))))) {
445 symbolP->sy_number = symbol_number++;
446
447 /* The + 1 after strlen account for the \0 at the
448 end of each string */
449 if (!S_IS_STABD(symbolP)) {
450 /* Ordinary case. */
451 symbolP->sy_name_offset = string_byte_count;
452 string_byte_count += strlen(S_GET_NAME(symbolP)) + 1;
453 }
454 else /* .Stabd case. */
455 symbolP->sy_name_offset = 0;
456 symbolPP = &(symbol_next(symbolP));
457 } else {
458 if (S_IS_EXTERNAL(symbolP) || !S_IS_DEFINED(symbolP)) {
459 as_bad("Local symbol %s never defined", S_GET_NAME(symbolP));
460 } /* oops. */
461
462 /* Unhook it from the chain */
463 *symbolPP = symbol_next(symbolP);
464 } /* if this symbol should be in the output */
465 } /* for each symbol */
466
467 H_SET_SYMBOL_TABLE_SIZE(headers, symbol_number);
468
469 return;
470 } /* obj_crawl_symbol_chain() */
471
472 /*
473 * Find strings by crawling along symbol table chain.
474 */
475
476 void obj_emit_strings(where)
477 char **where;
478 {
479 symbolS *symbolP;
480
481 #ifdef CROSS_COMPILE
482 /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */
483 md_number_to_chars(*where, string_byte_count, sizeof(string_byte_count));
484 *where += sizeof(string_byte_count);
485 #else /* CROSS_COMPILE */
486 append(where, (char *) &string_byte_count, (unsigned long) sizeof(string_byte_count));
487 #endif /* CROSS_COMPILE */
488
489 for(symbolP = symbol_rootP; symbolP; symbolP = symbol_next(symbolP)) {
490 if(S_GET_NAME(symbolP))
491 append(where, S_GET_NAME(symbolP), (unsigned long)(strlen (S_GET_NAME(symbolP)) + 1));
492 } /* walk symbol chain */
493
494 return;
495 } /* obj_emit_strings() */
496
497 /*
498 * Local Variables:
499 * comment-column: 0
500 * fill-column: 131
501 * End:
502 */
503
504 /* end of obj-bout.c */
This page took 0.04036 seconds and 5 git commands to generate.