Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* coff object file format |
f7e42eb4 NC |
2 | Copyright 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
3 | 1999, 2000, 2001 | |
252b5132 RH |
4 | Free Software Foundation, Inc. |
5 | ||
6 | This file is part of GAS. | |
7 | ||
8 | GAS is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GAS is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GAS; see the file COPYING. If not, write to the Free | |
20 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
21 | 02111-1307, USA. */ | |
22 | ||
23 | #define OBJ_HEADER "obj-coff.h" | |
24 | ||
25 | #include "as.h" | |
26 | #include "obstack.h" | |
27 | #include "subsegs.h" | |
28 | ||
29 | /* I think this is probably always correct. */ | |
30 | #ifndef KEEP_RELOC_INFO | |
31 | #define KEEP_RELOC_INFO | |
32 | #endif | |
33 | ||
b8a9dcab NC |
34 | /* The BFD_ASSEMBLER version of obj_coff_section will use this macro to set |
35 | a new section's attributes when a directive has no valid flags or the | |
36 | "w" flag is used. This default should be appropriate for most. */ | |
37 | #ifndef TC_COFF_SECTION_DEFAULT_ATTRIBUTES | |
38 | #define TC_COFF_SECTION_DEFAULT_ATTRIBUTES (SEC_LOAD | SEC_DATA) | |
39 | #endif | |
40 | ||
8d28c9d7 AM |
41 | /* This is used to hold the symbol built by a sequence of pseudo-ops |
42 | from .def and .endef. */ | |
43 | static symbolS *def_symbol_in_progress; | |
44 | ||
45 | typedef struct | |
46 | { | |
47 | unsigned long chunk_size; | |
48 | unsigned long element_size; | |
49 | unsigned long size; | |
50 | char *data; | |
51 | unsigned long pointer; | |
52 | } | |
53 | stack; | |
54 | ||
55 | static stack *stack_init PARAMS ((unsigned long, unsigned long)); | |
56 | static char *stack_push PARAMS ((stack *, char *)); | |
57 | static char *stack_pop PARAMS ((stack *)); | |
58 | static void tag_init PARAMS ((void)); | |
59 | static void tag_insert PARAMS ((const char *, symbolS *)); | |
60 | static symbolS *tag_find PARAMS ((char *)); | |
61 | static symbolS *tag_find_or_make PARAMS ((char *)); | |
252b5132 | 62 | static void obj_coff_bss PARAMS ((int)); |
8d28c9d7 | 63 | static void obj_coff_weak PARAMS ((int)); |
252b5132 | 64 | const char *s_get_name PARAMS ((symbolS * s)); |
0561a208 ILT |
65 | static void obj_coff_ln PARAMS ((int)); |
66 | static void obj_coff_def PARAMS ((int)); | |
67 | static void obj_coff_endef PARAMS ((int)); | |
68 | static void obj_coff_dim PARAMS ((int)); | |
69 | static void obj_coff_line PARAMS ((int)); | |
70 | static void obj_coff_size PARAMS ((int)); | |
71 | static void obj_coff_scl PARAMS ((int)); | |
72 | static void obj_coff_tag PARAMS ((int)); | |
73 | static void obj_coff_val PARAMS ((int)); | |
74 | static void obj_coff_type PARAMS ((int)); | |
7a6284c4 | 75 | static void obj_coff_ident PARAMS ((int)); |
28428223 ILT |
76 | #ifdef BFD_ASSEMBLER |
77 | static void obj_coff_loc PARAMS((int)); | |
78 | #endif | |
252b5132 RH |
79 | \f |
80 | /* stack stuff */ | |
252b5132 RH |
81 | |
82 | static stack * | |
83 | stack_init (chunk_size, element_size) | |
84 | unsigned long chunk_size; | |
85 | unsigned long element_size; | |
86 | { | |
87 | stack *st; | |
88 | ||
89 | st = (stack *) malloc (sizeof (stack)); | |
90 | if (!st) | |
91 | return 0; | |
92 | st->data = malloc (chunk_size); | |
93 | if (!st->data) | |
94 | { | |
95 | free (st); | |
96 | return 0; | |
97 | } | |
98 | st->pointer = 0; | |
99 | st->size = chunk_size; | |
100 | st->chunk_size = chunk_size; | |
101 | st->element_size = element_size; | |
102 | return st; | |
103 | } | |
104 | ||
105 | #if 0 | |
106 | /* Not currently used. */ | |
107 | static void | |
108 | stack_delete (st) | |
109 | stack *st; | |
110 | { | |
111 | free (st->data); | |
112 | free (st); | |
113 | } | |
114 | #endif | |
115 | ||
116 | static char * | |
117 | stack_push (st, element) | |
118 | stack *st; | |
119 | char *element; | |
120 | { | |
121 | if (st->pointer + st->element_size >= st->size) | |
122 | { | |
123 | st->size += st->chunk_size; | |
124 | if ((st->data = xrealloc (st->data, st->size)) == (char *) 0) | |
125 | return (char *) 0; | |
126 | } | |
127 | memcpy (st->data + st->pointer, element, st->element_size); | |
128 | st->pointer += st->element_size; | |
129 | return st->data + st->pointer; | |
130 | } | |
131 | ||
132 | static char * | |
133 | stack_pop (st) | |
134 | stack *st; | |
135 | { | |
136 | if (st->pointer < st->element_size) | |
137 | { | |
138 | st->pointer = 0; | |
139 | return (char *) 0; | |
140 | } | |
141 | st->pointer -= st->element_size; | |
142 | return st->data + st->pointer; | |
143 | } | |
144 | \f | |
145 | /* | |
146 | * Maintain a list of the tagnames of the structres. | |
147 | */ | |
148 | ||
149 | static struct hash_control *tag_hash; | |
150 | ||
151 | static void | |
152 | tag_init () | |
153 | { | |
154 | tag_hash = hash_new (); | |
155 | } | |
156 | ||
157 | static void | |
158 | tag_insert (name, symbolP) | |
159 | const char *name; | |
160 | symbolS *symbolP; | |
161 | { | |
162 | const char *error_string; | |
163 | ||
164 | if ((error_string = hash_jam (tag_hash, name, (char *) symbolP))) | |
165 | { | |
166 | as_fatal (_("Inserting \"%s\" into structure table failed: %s"), | |
167 | name, error_string); | |
168 | } | |
169 | } | |
170 | ||
171 | static symbolS * | |
172 | tag_find (name) | |
173 | char *name; | |
174 | { | |
175 | #ifdef STRIP_UNDERSCORE | |
176 | if (*name == '_') | |
177 | name++; | |
178 | #endif /* STRIP_UNDERSCORE */ | |
179 | return (symbolS *) hash_find (tag_hash, name); | |
180 | } | |
181 | ||
182 | static symbolS * | |
183 | tag_find_or_make (name) | |
184 | char *name; | |
185 | { | |
186 | symbolS *symbolP; | |
187 | ||
188 | if ((symbolP = tag_find (name)) == NULL) | |
189 | { | |
190 | symbolP = symbol_new (name, undefined_section, | |
191 | 0, &zero_address_frag); | |
192 | ||
193 | tag_insert (S_GET_NAME (symbolP), symbolP); | |
194 | #ifdef BFD_ASSEMBLER | |
195 | symbol_table_insert (symbolP); | |
196 | #endif | |
197 | } /* not found */ | |
198 | ||
199 | return symbolP; | |
200 | } | |
201 | ||
202 | /* We accept the .bss directive to set the section for backward | |
203 | compatibility with earlier versions of gas. */ | |
204 | ||
205 | static void | |
206 | obj_coff_bss (ignore) | |
a04b544b | 207 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
208 | { |
209 | if (*input_line_pointer == '\n') | |
210 | subseg_new (".bss", get_absolute_expression ()); | |
211 | else | |
212 | s_lcomm (0); | |
213 | } | |
214 | ||
215 | /* Handle .weak. This is a GNU extension. */ | |
216 | ||
217 | static void | |
218 | obj_coff_weak (ignore) | |
a04b544b | 219 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
220 | { |
221 | char *name; | |
222 | int c; | |
223 | symbolS *symbolP; | |
dcd619be | 224 | |
252b5132 RH |
225 | do |
226 | { | |
227 | name = input_line_pointer; | |
228 | c = get_symbol_end (); | |
229 | symbolP = symbol_find_or_make (name); | |
230 | *input_line_pointer = c; | |
231 | SKIP_WHITESPACE (); | |
232 | ||
4ce3447c | 233 | #if defined BFD_ASSEMBLER || defined S_SET_WEAK |
252b5132 RH |
234 | S_SET_WEAK (symbolP); |
235 | #endif | |
236 | ||
237 | #ifdef TE_PE | |
238 | S_SET_STORAGE_CLASS (symbolP, C_NT_WEAK); | |
239 | #else | |
240 | S_SET_STORAGE_CLASS (symbolP, C_WEAKEXT); | |
241 | #endif | |
242 | ||
243 | if (c == ',') | |
244 | { | |
245 | input_line_pointer++; | |
246 | SKIP_WHITESPACE (); | |
247 | if (*input_line_pointer == '\n') | |
248 | c = '\n'; | |
249 | } | |
250 | } | |
251 | while (c == ','); | |
252 | ||
253 | demand_empty_rest_of_line (); | |
254 | } | |
255 | ||
256 | #ifdef BFD_ASSEMBLER | |
257 | ||
8d28c9d7 | 258 | static segT fetch_coff_debug_section PARAMS ((void)); |
252b5132 | 259 | static void SA_SET_SYM_TAGNDX PARAMS ((symbolS *, symbolS *)); |
8d28c9d7 AM |
260 | static int S_GET_DATA_TYPE PARAMS ((symbolS *)); |
261 | void c_symbol_merge PARAMS ((symbolS *, symbolS *)); | |
262 | static void add_lineno PARAMS ((fragS *, addressT, int)); | |
252b5132 RH |
263 | |
264 | #define GET_FILENAME_STRING(X) \ | |
814f6641 | 265 | ((char*) (&((X)->sy_symbol.ost_auxent->x_file.x_n.x_offset))[1]) |
252b5132 RH |
266 | |
267 | /* @@ Ick. */ | |
268 | static segT | |
269 | fetch_coff_debug_section () | |
270 | { | |
271 | static segT debug_section; | |
272 | if (!debug_section) | |
273 | { | |
274 | CONST asymbol *s; | |
275 | s = bfd_make_debug_symbol (stdoutput, (char *) 0, 0); | |
276 | assert (s != 0); | |
277 | debug_section = s->section; | |
278 | } | |
279 | return debug_section; | |
280 | } | |
281 | ||
282 | void | |
283 | SA_SET_SYM_ENDNDX (sym, val) | |
284 | symbolS *sym; | |
285 | symbolS *val; | |
286 | { | |
287 | combined_entry_type *entry, *p; | |
288 | ||
49309057 ILT |
289 | entry = &coffsymbol (symbol_get_bfdsym (sym))->native[1]; |
290 | p = coffsymbol (symbol_get_bfdsym (val))->native; | |
252b5132 RH |
291 | entry->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = p; |
292 | entry->fix_end = 1; | |
293 | } | |
294 | ||
295 | static void | |
296 | SA_SET_SYM_TAGNDX (sym, val) | |
297 | symbolS *sym; | |
298 | symbolS *val; | |
299 | { | |
300 | combined_entry_type *entry, *p; | |
301 | ||
49309057 ILT |
302 | entry = &coffsymbol (symbol_get_bfdsym (sym))->native[1]; |
303 | p = coffsymbol (symbol_get_bfdsym (val))->native; | |
252b5132 RH |
304 | entry->u.auxent.x_sym.x_tagndx.p = p; |
305 | entry->fix_tag = 1; | |
306 | } | |
307 | ||
308 | static int | |
309 | S_GET_DATA_TYPE (sym) | |
310 | symbolS *sym; | |
311 | { | |
49309057 | 312 | return coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_type; |
252b5132 RH |
313 | } |
314 | ||
315 | int | |
316 | S_SET_DATA_TYPE (sym, val) | |
317 | symbolS *sym; | |
318 | int val; | |
319 | { | |
49309057 | 320 | coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_type = val; |
252b5132 RH |
321 | return val; |
322 | } | |
323 | ||
324 | int | |
325 | S_GET_STORAGE_CLASS (sym) | |
326 | symbolS *sym; | |
327 | { | |
49309057 | 328 | return coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_sclass; |
252b5132 RH |
329 | } |
330 | ||
331 | int | |
332 | S_SET_STORAGE_CLASS (sym, val) | |
333 | symbolS *sym; | |
334 | int val; | |
335 | { | |
49309057 | 336 | coffsymbol (symbol_get_bfdsym (sym))->native->u.syment.n_sclass = val; |
252b5132 RH |
337 | return val; |
338 | } | |
339 | ||
dcd619be | 340 | /* Merge a debug symbol containing debug information into a normal symbol. */ |
252b5132 RH |
341 | |
342 | void | |
343 | c_symbol_merge (debug, normal) | |
344 | symbolS *debug; | |
345 | symbolS *normal; | |
346 | { | |
347 | S_SET_DATA_TYPE (normal, S_GET_DATA_TYPE (debug)); | |
348 | S_SET_STORAGE_CLASS (normal, S_GET_STORAGE_CLASS (debug)); | |
349 | ||
350 | if (S_GET_NUMBER_AUXILIARY (debug) > S_GET_NUMBER_AUXILIARY (normal)) | |
351 | { | |
352 | /* take the most we have */ | |
353 | S_SET_NUMBER_AUXILIARY (normal, S_GET_NUMBER_AUXILIARY (debug)); | |
354 | } | |
355 | ||
356 | if (S_GET_NUMBER_AUXILIARY (debug) > 0) | |
357 | { | |
358 | /* Move all the auxiliary information. */ | |
359 | memcpy (SYM_AUXINFO (normal), SYM_AUXINFO (debug), | |
360 | (S_GET_NUMBER_AUXILIARY (debug) | |
361 | * sizeof (*SYM_AUXINFO (debug)))); | |
362 | } | |
363 | ||
dcd619be | 364 | /* Move the debug flags. */ |
252b5132 RH |
365 | SF_SET_DEBUG_FIELD (normal, SF_GET_DEBUG_FIELD (debug)); |
366 | } | |
367 | ||
368 | void | |
369 | c_dot_file_symbol (filename) | |
5110c57e | 370 | const char *filename; |
252b5132 RH |
371 | { |
372 | symbolS *symbolP; | |
373 | ||
0561a208 ILT |
374 | /* BFD converts filename to a .file symbol with an aux entry. It |
375 | also handles chaining. */ | |
252b5132 RH |
376 | symbolP = symbol_new (filename, bfd_abs_section_ptr, 0, &zero_address_frag); |
377 | ||
378 | S_SET_STORAGE_CLASS (symbolP, C_FILE); | |
379 | S_SET_NUMBER_AUXILIARY (symbolP, 1); | |
380 | ||
49309057 | 381 | symbol_get_bfdsym (symbolP)->flags = BSF_DEBUGGING; |
252b5132 RH |
382 | |
383 | #ifndef NO_LISTING | |
384 | { | |
385 | extern int listing; | |
386 | if (listing) | |
387 | { | |
388 | listing_source_file (filename); | |
389 | } | |
390 | } | |
391 | #endif | |
392 | ||
393 | /* Make sure that the symbol is first on the symbol chain */ | |
394 | if (symbol_rootP != symbolP) | |
395 | { | |
396 | symbol_remove (symbolP, &symbol_rootP, &symbol_lastP); | |
397 | symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP); | |
398 | } /* if not first on the list */ | |
399 | } | |
400 | ||
401 | /* Line number handling */ | |
402 | ||
403 | struct line_no { | |
404 | struct line_no *next; | |
405 | fragS *frag; | |
406 | alent l; | |
407 | }; | |
408 | ||
409 | int coff_line_base; | |
410 | ||
411 | /* Symbol of last function, which we should hang line#s off of. */ | |
412 | static symbolS *line_fsym; | |
413 | ||
414 | #define in_function() (line_fsym != 0) | |
415 | #define clear_function() (line_fsym = 0) | |
416 | #define set_function(F) (line_fsym = (F), coff_add_linesym (F)) | |
417 | ||
418 | \f | |
419 | void | |
420 | coff_obj_symbol_new_hook (symbolP) | |
421 | symbolS *symbolP; | |
422 | { | |
423 | long sz = (OBJ_COFF_MAX_AUXENTRIES + 1) * sizeof (combined_entry_type); | |
424 | char * s = (char *) xmalloc (sz); | |
dcd619be | 425 | |
252b5132 | 426 | memset (s, 0, sz); |
49309057 | 427 | coffsymbol (symbol_get_bfdsym (symbolP))->native = (combined_entry_type *) s; |
252b5132 RH |
428 | |
429 | S_SET_DATA_TYPE (symbolP, T_NULL); | |
430 | S_SET_STORAGE_CLASS (symbolP, 0); | |
431 | S_SET_NUMBER_AUXILIARY (symbolP, 0); | |
432 | ||
433 | if (S_IS_STRING (symbolP)) | |
434 | SF_SET_STRING (symbolP); | |
dcd619be | 435 | |
252b5132 RH |
436 | if (S_IS_LOCAL (symbolP)) |
437 | SF_SET_LOCAL (symbolP); | |
438 | } | |
439 | ||
440 | \f | |
441 | /* | |
442 | * Handle .ln directives. | |
443 | */ | |
444 | ||
445 | static symbolS *current_lineno_sym; | |
446 | static struct line_no *line_nos; | |
447 | /* @@ Blindly assume all .ln directives will be in the .text section... */ | |
448 | int coff_n_line_nos; | |
449 | ||
450 | static void | |
451 | add_lineno (frag, offset, num) | |
452 | fragS *frag; | |
7f6d05e8 | 453 | addressT offset; |
252b5132 RH |
454 | int num; |
455 | { | |
456 | struct line_no *new_line = | |
457 | (struct line_no *) xmalloc (sizeof (struct line_no)); | |
458 | if (!current_lineno_sym) | |
459 | { | |
460 | abort (); | |
461 | } | |
6877bb43 TR |
462 | |
463 | #ifndef OBJ_XCOFF | |
464 | /* The native aix assembler accepts negative line number */ | |
465 | ||
dcd619be | 466 | if (num <= 0) |
e8a3ab75 ILT |
467 | { |
468 | /* Zero is used as an end marker in the file. */ | |
b985eaa8 ILT |
469 | as_warn (_("Line numbers must be positive integers\n")); |
470 | num = 1; | |
e8a3ab75 | 471 | } |
6877bb43 | 472 | #endif /* OBJ_XCOFF */ |
252b5132 RH |
473 | new_line->next = line_nos; |
474 | new_line->frag = frag; | |
475 | new_line->l.line_number = num; | |
476 | new_line->l.u.offset = offset; | |
477 | line_nos = new_line; | |
478 | coff_n_line_nos++; | |
479 | } | |
480 | ||
481 | void | |
482 | coff_add_linesym (sym) | |
483 | symbolS *sym; | |
484 | { | |
485 | if (line_nos) | |
486 | { | |
49309057 ILT |
487 | coffsymbol (symbol_get_bfdsym (current_lineno_sym))->lineno = |
488 | (alent *) line_nos; | |
252b5132 RH |
489 | coff_n_line_nos++; |
490 | line_nos = 0; | |
491 | } | |
492 | current_lineno_sym = sym; | |
493 | } | |
494 | ||
495 | static void | |
496 | obj_coff_ln (appline) | |
497 | int appline; | |
498 | { | |
499 | int l; | |
500 | ||
501 | if (! appline && def_symbol_in_progress != NULL) | |
502 | { | |
503 | as_warn (_(".ln pseudo-op inside .def/.endef: ignored.")); | |
504 | demand_empty_rest_of_line (); | |
505 | return; | |
506 | } | |
507 | ||
508 | l = get_absolute_expression (); | |
252b5132 | 509 | |
e237d851 NC |
510 | /* If there is no lineno symbol, treat a .ln |
511 | directive as if it were a .appline directive. */ | |
512 | if (appline || current_lineno_sym == NULL) | |
252b5132 | 513 | new_logical_line ((char *) NULL, l - 1); |
e237d851 NC |
514 | else |
515 | add_lineno (frag_now, frag_now_fix (), l); | |
252b5132 RH |
516 | |
517 | #ifndef NO_LISTING | |
518 | { | |
519 | extern int listing; | |
520 | ||
521 | if (listing) | |
522 | { | |
523 | if (! appline) | |
524 | l += coff_line_base - 1; | |
525 | listing_source_line (l); | |
526 | } | |
527 | } | |
528 | #endif | |
529 | ||
530 | demand_empty_rest_of_line (); | |
531 | } | |
532 | ||
28428223 ILT |
533 | /* .loc is essentially the same as .ln; parse it for assembler |
534 | compatibility. */ | |
535 | ||
536 | static void | |
537 | obj_coff_loc (ignore) | |
538 | int ignore ATTRIBUTE_UNUSED; | |
539 | { | |
540 | int lineno; | |
541 | ||
542 | /* FIXME: Why do we need this check? We need it for ECOFF, but why | |
543 | do we need it for COFF? */ | |
544 | if (now_seg != text_section) | |
545 | { | |
546 | as_warn (_(".loc outside of .text")); | |
547 | demand_empty_rest_of_line (); | |
548 | return; | |
549 | } | |
550 | ||
551 | if (def_symbol_in_progress != NULL) | |
552 | { | |
553 | as_warn (_(".loc pseudo-op inside .def/.endef: ignored.")); | |
554 | demand_empty_rest_of_line (); | |
555 | return; | |
556 | } | |
557 | ||
558 | /* Skip the file number. */ | |
559 | SKIP_WHITESPACE (); | |
560 | get_absolute_expression (); | |
561 | SKIP_WHITESPACE (); | |
562 | ||
563 | lineno = get_absolute_expression (); | |
564 | ||
565 | #ifndef NO_LISTING | |
566 | { | |
567 | extern int listing; | |
568 | ||
569 | if (listing) | |
570 | { | |
571 | lineno += coff_line_base - 1; | |
572 | listing_source_line (lineno); | |
573 | } | |
574 | } | |
575 | #endif | |
576 | ||
577 | demand_empty_rest_of_line (); | |
578 | ||
579 | add_lineno (frag_now, frag_now_fix (), lineno); | |
580 | } | |
581 | ||
7a6284c4 ILT |
582 | /* Handle the .ident pseudo-op. */ |
583 | ||
584 | static void | |
585 | obj_coff_ident (ignore) | |
586 | int ignore ATTRIBUTE_UNUSED; | |
587 | { | |
588 | segT current_seg = now_seg; | |
589 | subsegT current_subseg = now_subseg; | |
590 | ||
591 | #ifdef TE_PE | |
592 | { | |
593 | segT sec; | |
594 | ||
595 | /* We could put it in .comment, but that creates an extra section | |
596 | that shouldn't be loaded into memory, which requires linker | |
597 | changes... For now, until proven otherwise, use .rdata. */ | |
598 | sec = subseg_new (".rdata$zzz", 0); | |
599 | bfd_set_section_flags (stdoutput, sec, | |
600 | ((SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA) | |
601 | & bfd_applicable_section_flags (stdoutput))); | |
602 | } | |
603 | #else | |
604 | subseg_new (".comment", 0); | |
605 | #endif | |
606 | ||
607 | stringer (1); | |
608 | subseg_set (current_seg, current_subseg); | |
609 | } | |
610 | ||
252b5132 RH |
611 | /* |
612 | * def() | |
613 | * | |
614 | * Handle .def directives. | |
615 | * | |
616 | * One might ask : why can't we symbol_new if the symbol does not | |
617 | * already exist and fill it with debug information. Because of | |
618 | * the C_EFCN special symbol. It would clobber the value of the | |
619 | * function symbol before we have a chance to notice that it is | |
620 | * a C_EFCN. And a second reason is that the code is more clear this | |
621 | * way. (at least I think it is :-). | |
622 | * | |
623 | */ | |
624 | ||
625 | #define SKIP_SEMI_COLON() while (*input_line_pointer++ != ';') | |
626 | #define SKIP_WHITESPACES() while (*input_line_pointer == ' ' || \ | |
627 | *input_line_pointer == '\t') \ | |
628 | input_line_pointer++; | |
629 | ||
630 | static void | |
631 | obj_coff_def (what) | |
c4bf532f | 632 | int what ATTRIBUTE_UNUSED; |
252b5132 RH |
633 | { |
634 | char name_end; /* Char after the end of name */ | |
635 | char *symbol_name; /* Name of the debug symbol */ | |
636 | char *symbol_name_copy; /* Temporary copy of the name */ | |
637 | unsigned int symbol_name_length; | |
638 | ||
639 | if (def_symbol_in_progress != NULL) | |
640 | { | |
641 | as_warn (_(".def pseudo-op used inside of .def/.endef: ignored.")); | |
642 | demand_empty_rest_of_line (); | |
643 | return; | |
644 | } /* if not inside .def/.endef */ | |
645 | ||
646 | SKIP_WHITESPACES (); | |
647 | ||
648 | symbol_name = input_line_pointer; | |
649 | #ifdef STRIP_UNDERSCORE | |
650 | if (symbol_name[0] == '_' && symbol_name[1] != 0) | |
651 | symbol_name++; | |
652 | #endif /* STRIP_UNDERSCORE */ | |
653 | ||
654 | name_end = get_symbol_end (); | |
655 | symbol_name_length = strlen (symbol_name); | |
656 | symbol_name_copy = xmalloc (symbol_name_length + 1); | |
657 | strcpy (symbol_name_copy, symbol_name); | |
658 | #ifdef tc_canonicalize_symbol_name | |
659 | symbol_name_copy = tc_canonicalize_symbol_name (symbol_name_copy); | |
660 | #endif | |
661 | ||
662 | /* Initialize the new symbol */ | |
663 | def_symbol_in_progress = symbol_make (symbol_name_copy); | |
49309057 | 664 | symbol_set_frag (def_symbol_in_progress, &zero_address_frag); |
252b5132 RH |
665 | S_SET_VALUE (def_symbol_in_progress, 0); |
666 | ||
667 | if (S_IS_STRING (def_symbol_in_progress)) | |
668 | SF_SET_STRING (def_symbol_in_progress); | |
669 | ||
670 | *input_line_pointer = name_end; | |
671 | ||
672 | demand_empty_rest_of_line (); | |
673 | } | |
674 | ||
675 | unsigned int dim_index; | |
676 | ||
677 | static void | |
678 | obj_coff_endef (ignore) | |
c4bf532f | 679 | int ignore ATTRIBUTE_UNUSED; |
252b5132 | 680 | { |
c9900432 | 681 | symbolS *symbolP = NULL; |
252b5132 RH |
682 | |
683 | /* DIM BUG FIX sac@cygnus.com */ | |
684 | dim_index = 0; | |
685 | if (def_symbol_in_progress == NULL) | |
686 | { | |
687 | as_warn (_(".endef pseudo-op used outside of .def/.endef: ignored.")); | |
688 | demand_empty_rest_of_line (); | |
689 | return; | |
690 | } /* if not inside .def/.endef */ | |
691 | ||
dcd619be | 692 | /* Set the section number according to storage class. */ |
252b5132 RH |
693 | switch (S_GET_STORAGE_CLASS (def_symbol_in_progress)) |
694 | { | |
695 | case C_STRTAG: | |
696 | case C_ENTAG: | |
697 | case C_UNTAG: | |
698 | SF_SET_TAG (def_symbol_in_progress); | |
699 | /* intentional fallthrough */ | |
700 | case C_FILE: | |
701 | case C_TPDEF: | |
702 | SF_SET_DEBUG (def_symbol_in_progress); | |
703 | S_SET_SEGMENT (def_symbol_in_progress, fetch_coff_debug_section ()); | |
704 | break; | |
705 | ||
706 | case C_EFCN: | |
dcd619be | 707 | SF_SET_LOCAL (def_symbol_in_progress); /* Do not emit this symbol. */ |
252b5132 RH |
708 | /* intentional fallthrough */ |
709 | case C_BLOCK: | |
710 | SF_SET_PROCESS (def_symbol_in_progress); /* Will need processing before writing */ | |
711 | /* intentional fallthrough */ | |
712 | case C_FCN: | |
713 | { | |
714 | CONST char *name; | |
715 | S_SET_SEGMENT (def_symbol_in_progress, text_section); | |
716 | ||
49309057 | 717 | name = S_GET_NAME (def_symbol_in_progress); |
23dab925 ILT |
718 | if (name[0] == '.' && name[2] == 'f' && name[3] == '\0') |
719 | { | |
720 | switch (name[1]) | |
721 | { | |
dcd619be | 722 | case 'b': |
23dab925 ILT |
723 | /* .bf */ |
724 | if (! in_function ()) | |
725 | as_warn (_("`%s' symbol without preceding function"), name); | |
726 | /* Will need relocating. */ | |
727 | SF_SET_PROCESS (def_symbol_in_progress); | |
728 | clear_function (); | |
729 | break; | |
730 | #ifdef TE_PE | |
dcd619be | 731 | case 'e': |
23dab925 ILT |
732 | /* .ef */ |
733 | /* The MS compilers output the actual endline, not the | |
734 | function-relative one... we want to match without | |
735 | changing the assembler input. */ | |
dcd619be | 736 | SA_SET_SYM_LNNO (def_symbol_in_progress, |
23dab925 ILT |
737 | (SA_GET_SYM_LNNO (def_symbol_in_progress) |
738 | + coff_line_base)); | |
739 | break; | |
740 | #endif | |
741 | } | |
252b5132 RH |
742 | } |
743 | } | |
744 | break; | |
745 | ||
746 | #ifdef C_AUTOARG | |
747 | case C_AUTOARG: | |
748 | #endif /* C_AUTOARG */ | |
749 | case C_AUTO: | |
750 | case C_REG: | |
751 | case C_ARG: | |
752 | case C_REGPARM: | |
753 | case C_FIELD: | |
56385375 L |
754 | |
755 | /* According to the COFF documentation: | |
756 | ||
757 | http://osr5doc.sco.com:1996/topics/COFF_SectNumFld.html | |
758 | ||
759 | A special section number (-2) marks symbolic debugging symbols, | |
760 | including structure/union/enumeration tag names, typedefs, and | |
dcd619be | 761 | the name of the file. A section number of -1 indicates that the |
56385375 | 762 | symbol has a value but is not relocatable. Examples of |
dcd619be KH |
763 | absolute-valued symbols include automatic and register variables, |
764 | function arguments, and .eos symbols. | |
56385375 L |
765 | |
766 | But from Ian Lance Taylor: | |
767 | ||
768 | http://sources.redhat.com/ml/binutils/2000-08/msg00202.html | |
769 | ||
770 | the actual tools all marked them as section -1. So the GNU COFF | |
771 | assembler follows historical COFF assemblers. | |
772 | ||
773 | However, it causes problems for djgpp | |
774 | ||
775 | http://sources.redhat.com/ml/binutils/2000-08/msg00210.html | |
776 | ||
777 | By defining STRICTCOFF, a COFF port can make the assembler to | |
dcd619be | 778 | follow the documented behavior. */ |
56385375 | 779 | #ifdef STRICTCOFF |
252b5132 RH |
780 | case C_MOS: |
781 | case C_MOE: | |
782 | case C_MOU: | |
783 | case C_EOS: | |
56385375 | 784 | #endif |
d1d8ba22 | 785 | SF_SET_DEBUG (def_symbol_in_progress); |
252b5132 RH |
786 | S_SET_SEGMENT (def_symbol_in_progress, absolute_section); |
787 | break; | |
788 | ||
56385375 L |
789 | #ifndef STRICTCOFF |
790 | case C_MOS: | |
791 | case C_MOE: | |
792 | case C_MOU: | |
793 | case C_EOS: | |
794 | S_SET_SEGMENT (def_symbol_in_progress, absolute_section); | |
795 | break; | |
796 | #endif | |
797 | ||
252b5132 RH |
798 | case C_EXT: |
799 | case C_WEAKEXT: | |
800 | #ifdef TE_PE | |
801 | case C_NT_WEAK: | |
802 | #endif | |
803 | case C_STAT: | |
804 | case C_LABEL: | |
805 | /* Valid but set somewhere else (s_comm, s_lcomm, colon) */ | |
806 | break; | |
807 | ||
808 | default: | |
809 | case C_USTATIC: | |
810 | case C_EXTDEF: | |
811 | case C_ULABEL: | |
812 | as_warn (_("unexpected storage class %d"), | |
813 | S_GET_STORAGE_CLASS (def_symbol_in_progress)); | |
814 | break; | |
815 | } /* switch on storage class */ | |
816 | ||
817 | /* Now that we have built a debug symbol, try to find if we should | |
818 | merge with an existing symbol or not. If a symbol is C_EFCN or | |
9690c54d ILT |
819 | absolute_section or untagged SEG_DEBUG it never merges. We also |
820 | don't merge labels, which are in a different namespace, nor | |
821 | symbols which have not yet been defined since they are typically | |
822 | unique, nor do we merge tags with non-tags. */ | |
252b5132 RH |
823 | |
824 | /* Two cases for functions. Either debug followed by definition or | |
825 | definition followed by debug. For definition first, we will | |
826 | merge the debug symbol into the definition. For debug first, the | |
827 | lineno entry MUST point to the definition function or else it | |
828 | will point off into space when obj_crawl_symbol_chain() merges | |
829 | the debug symbol into the real symbol. Therefor, let's presume | |
dcd619be | 830 | the debug symbol is a real function reference. */ |
252b5132 RH |
831 | |
832 | /* FIXME-SOON If for some reason the definition label/symbol is | |
833 | never seen, this will probably leave an undefined symbol at link | |
dcd619be | 834 | time. */ |
252b5132 RH |
835 | |
836 | if (S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_EFCN | |
9690c54d | 837 | || S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_LABEL |
252b5132 RH |
838 | || (!strcmp (bfd_get_section_name (stdoutput, |
839 | S_GET_SEGMENT (def_symbol_in_progress)), | |
840 | "*DEBUG*") | |
841 | && !SF_GET_TAG (def_symbol_in_progress)) | |
842 | || S_GET_SEGMENT (def_symbol_in_progress) == absolute_section | |
9690c54d ILT |
843 | || ! symbol_constant_p (def_symbol_in_progress) |
844 | || (symbolP = symbol_find_base (S_GET_NAME (def_symbol_in_progress), | |
845 | DO_NOT_STRIP)) == NULL | |
846 | || SF_GET_TAG (def_symbol_in_progress) != SF_GET_TAG (symbolP)) | |
252b5132 | 847 | { |
9690c54d | 848 | /* If it already is at the end of the symbol list, do nothing */ |
252b5132 | 849 | if (def_symbol_in_progress != symbol_lastP) |
9690c54d ILT |
850 | { |
851 | symbol_remove (def_symbol_in_progress, &symbol_rootP, &symbol_lastP); | |
852 | symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP, | |
853 | &symbol_lastP); | |
854 | } | |
252b5132 RH |
855 | } |
856 | else | |
857 | { | |
858 | /* This symbol already exists, merge the newly created symbol | |
859 | into the old one. This is not mandatory. The linker can | |
860 | handle duplicate symbols correctly. But I guess that it save | |
861 | a *lot* of space if the assembly file defines a lot of | |
862 | symbols. [loic] */ | |
863 | ||
864 | /* The debug entry (def_symbol_in_progress) is merged into the | |
dcd619be | 865 | previous definition. */ |
252b5132 RH |
866 | |
867 | c_symbol_merge (def_symbol_in_progress, symbolP); | |
868 | symbol_remove (def_symbol_in_progress, &symbol_rootP, &symbol_lastP); | |
869 | ||
870 | def_symbol_in_progress = symbolP; | |
871 | ||
872 | if (SF_GET_FUNCTION (def_symbol_in_progress) | |
873 | || SF_GET_TAG (def_symbol_in_progress) | |
874 | || S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_STAT) | |
875 | { | |
876 | /* For functions, and tags, and static symbols, the symbol | |
877 | *must* be where the debug symbol appears. Move the | |
dcd619be | 878 | existing symbol to the current place. */ |
252b5132 RH |
879 | /* If it already is at the end of the symbol list, do nothing */ |
880 | if (def_symbol_in_progress != symbol_lastP) | |
881 | { | |
882 | symbol_remove (def_symbol_in_progress, &symbol_rootP, &symbol_lastP); | |
883 | symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP, &symbol_lastP); | |
884 | } | |
885 | } | |
886 | } | |
887 | ||
888 | if (SF_GET_TAG (def_symbol_in_progress)) | |
889 | { | |
890 | symbolS *oldtag; | |
891 | ||
892 | oldtag = symbol_find_base (S_GET_NAME (def_symbol_in_progress), | |
893 | DO_NOT_STRIP); | |
894 | if (oldtag == NULL || ! SF_GET_TAG (oldtag)) | |
895 | tag_insert (S_GET_NAME (def_symbol_in_progress), | |
896 | def_symbol_in_progress); | |
897 | } | |
898 | ||
899 | if (SF_GET_FUNCTION (def_symbol_in_progress)) | |
900 | { | |
901 | know (sizeof (def_symbol_in_progress) <= sizeof (long)); | |
902 | set_function (def_symbol_in_progress); | |
903 | SF_SET_PROCESS (def_symbol_in_progress); | |
904 | ||
905 | if (symbolP == NULL) | |
906 | { | |
907 | /* That is, if this is the first time we've seen the | |
dcd619be | 908 | function... */ |
252b5132 RH |
909 | symbol_table_insert (def_symbol_in_progress); |
910 | } /* definition follows debug */ | |
911 | } /* Create the line number entry pointing to the function being defined */ | |
912 | ||
913 | def_symbol_in_progress = NULL; | |
914 | demand_empty_rest_of_line (); | |
915 | } | |
916 | ||
917 | static void | |
918 | obj_coff_dim (ignore) | |
c4bf532f | 919 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
920 | { |
921 | int dim_index; | |
922 | ||
923 | if (def_symbol_in_progress == NULL) | |
924 | { | |
925 | as_warn (_(".dim pseudo-op used outside of .def/.endef: ignored.")); | |
926 | demand_empty_rest_of_line (); | |
927 | return; | |
928 | } /* if not inside .def/.endef */ | |
929 | ||
930 | S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1); | |
931 | ||
932 | for (dim_index = 0; dim_index < DIMNUM; dim_index++) | |
933 | { | |
934 | SKIP_WHITESPACES (); | |
935 | SA_SET_SYM_DIMEN (def_symbol_in_progress, dim_index, | |
936 | get_absolute_expression ()); | |
937 | ||
938 | switch (*input_line_pointer) | |
939 | { | |
940 | case ',': | |
941 | input_line_pointer++; | |
942 | break; | |
943 | ||
944 | default: | |
945 | as_warn (_("badly formed .dim directive ignored")); | |
946 | /* intentional fallthrough */ | |
947 | case '\n': | |
948 | case ';': | |
949 | dim_index = DIMNUM; | |
950 | break; | |
951 | } | |
952 | } | |
953 | ||
954 | demand_empty_rest_of_line (); | |
955 | } | |
956 | ||
957 | static void | |
958 | obj_coff_line (ignore) | |
c4bf532f | 959 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
960 | { |
961 | int this_base; | |
962 | ||
963 | if (def_symbol_in_progress == NULL) | |
964 | { | |
965 | /* Probably stabs-style line? */ | |
966 | obj_coff_ln (0); | |
967 | return; | |
968 | } | |
969 | ||
970 | this_base = get_absolute_expression (); | |
971 | if (!strcmp (".bf", S_GET_NAME (def_symbol_in_progress))) | |
972 | coff_line_base = this_base; | |
973 | ||
974 | S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1); | |
23dab925 | 975 | SA_SET_SYM_LNNO (def_symbol_in_progress, this_base); |
252b5132 RH |
976 | |
977 | demand_empty_rest_of_line (); | |
978 | ||
979 | #ifndef NO_LISTING | |
980 | if (strcmp (".bf", S_GET_NAME (def_symbol_in_progress)) == 0) | |
981 | { | |
982 | extern int listing; | |
983 | ||
984 | if (listing) | |
23dab925 | 985 | listing_source_line ((unsigned int) this_base); |
252b5132 RH |
986 | } |
987 | #endif | |
988 | } | |
989 | ||
990 | static void | |
991 | obj_coff_size (ignore) | |
c4bf532f | 992 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
993 | { |
994 | if (def_symbol_in_progress == NULL) | |
995 | { | |
996 | as_warn (_(".size pseudo-op used outside of .def/.endef ignored.")); | |
997 | demand_empty_rest_of_line (); | |
998 | return; | |
999 | } /* if not inside .def/.endef */ | |
1000 | ||
1001 | S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1); | |
1002 | SA_SET_SYM_SIZE (def_symbol_in_progress, get_absolute_expression ()); | |
1003 | demand_empty_rest_of_line (); | |
1004 | } | |
1005 | ||
1006 | static void | |
1007 | obj_coff_scl (ignore) | |
c4bf532f | 1008 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
1009 | { |
1010 | if (def_symbol_in_progress == NULL) | |
1011 | { | |
1012 | as_warn (_(".scl pseudo-op used outside of .def/.endef ignored.")); | |
1013 | demand_empty_rest_of_line (); | |
1014 | return; | |
1015 | } /* if not inside .def/.endef */ | |
1016 | ||
1017 | S_SET_STORAGE_CLASS (def_symbol_in_progress, get_absolute_expression ()); | |
1018 | demand_empty_rest_of_line (); | |
1019 | } | |
1020 | ||
1021 | static void | |
1022 | obj_coff_tag (ignore) | |
c4bf532f | 1023 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
1024 | { |
1025 | char *symbol_name; | |
1026 | char name_end; | |
1027 | ||
1028 | if (def_symbol_in_progress == NULL) | |
1029 | { | |
1030 | as_warn (_(".tag pseudo-op used outside of .def/.endef ignored.")); | |
1031 | demand_empty_rest_of_line (); | |
1032 | return; | |
1033 | } | |
1034 | ||
1035 | S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1); | |
1036 | symbol_name = input_line_pointer; | |
1037 | name_end = get_symbol_end (); | |
1038 | ||
1039 | #ifdef tc_canonicalize_symbol_name | |
1040 | symbol_name = tc_canonicalize_symbol_name (symbol_name); | |
1041 | #endif | |
1042 | ||
1043 | /* Assume that the symbol referred to by .tag is always defined. | |
dcd619be | 1044 | This was a bad assumption. I've added find_or_make. xoxorich. */ |
252b5132 RH |
1045 | SA_SET_SYM_TAGNDX (def_symbol_in_progress, |
1046 | tag_find_or_make (symbol_name)); | |
1047 | if (SA_GET_SYM_TAGNDX (def_symbol_in_progress) == 0L) | |
1048 | { | |
1049 | as_warn (_("tag not found for .tag %s"), symbol_name); | |
1050 | } /* not defined */ | |
1051 | ||
1052 | SF_SET_TAGGED (def_symbol_in_progress); | |
1053 | *input_line_pointer = name_end; | |
1054 | ||
1055 | demand_empty_rest_of_line (); | |
1056 | } | |
1057 | ||
1058 | static void | |
1059 | obj_coff_type (ignore) | |
c4bf532f | 1060 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
1061 | { |
1062 | if (def_symbol_in_progress == NULL) | |
1063 | { | |
1064 | as_warn (_(".type pseudo-op used outside of .def/.endef ignored.")); | |
1065 | demand_empty_rest_of_line (); | |
1066 | return; | |
1067 | } /* if not inside .def/.endef */ | |
1068 | ||
1069 | S_SET_DATA_TYPE (def_symbol_in_progress, get_absolute_expression ()); | |
1070 | ||
1071 | if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress)) && | |
1072 | S_GET_STORAGE_CLASS (def_symbol_in_progress) != C_TPDEF) | |
1073 | { | |
1074 | SF_SET_FUNCTION (def_symbol_in_progress); | |
1075 | } /* is a function */ | |
1076 | ||
1077 | demand_empty_rest_of_line (); | |
1078 | } | |
1079 | ||
1080 | static void | |
1081 | obj_coff_val (ignore) | |
c4bf532f | 1082 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
1083 | { |
1084 | if (def_symbol_in_progress == NULL) | |
1085 | { | |
1086 | as_warn (_(".val pseudo-op used outside of .def/.endef ignored.")); | |
1087 | demand_empty_rest_of_line (); | |
1088 | return; | |
1089 | } /* if not inside .def/.endef */ | |
1090 | ||
1091 | if (is_name_beginner (*input_line_pointer)) | |
1092 | { | |
1093 | char *symbol_name = input_line_pointer; | |
1094 | char name_end = get_symbol_end (); | |
1095 | ||
1096 | #ifdef tc_canonicalize_symbol_name | |
1097 | symbol_name = tc_canonicalize_symbol_name (symbol_name); | |
1098 | #endif | |
1099 | if (!strcmp (symbol_name, ".")) | |
1100 | { | |
49309057 | 1101 | symbol_set_frag (def_symbol_in_progress, frag_now); |
252b5132 RH |
1102 | S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ()); |
1103 | /* If the .val is != from the .def (e.g. statics) */ | |
1104 | } | |
1105 | else if (strcmp (S_GET_NAME (def_symbol_in_progress), symbol_name)) | |
1106 | { | |
49309057 ILT |
1107 | expressionS exp; |
1108 | ||
1109 | exp.X_op = O_symbol; | |
1110 | exp.X_add_symbol = symbol_find_or_make (symbol_name); | |
1111 | exp.X_op_symbol = NULL; | |
1112 | exp.X_add_number = 0; | |
1113 | symbol_set_value_expression (def_symbol_in_progress, &exp); | |
252b5132 RH |
1114 | |
1115 | /* If the segment is undefined when the forward reference is | |
1116 | resolved, then copy the segment id from the forward | |
1117 | symbol. */ | |
1118 | SF_SET_GET_SEGMENT (def_symbol_in_progress); | |
0561a208 ILT |
1119 | |
1120 | /* FIXME: gcc can generate address expressions here in | |
1121 | unusual cases (search for "obscure" in sdbout.c). We | |
1122 | just ignore the offset here, thus generating incorrect | |
1123 | debugging information. We ignore the rest of the line | |
1124 | just below. */ | |
252b5132 | 1125 | } |
0561a208 | 1126 | /* Otherwise, it is the name of a non debug symbol and its value |
dcd619be | 1127 | will be calculated later. */ |
252b5132 RH |
1128 | *input_line_pointer = name_end; |
1129 | } | |
1130 | else | |
1131 | { | |
1132 | S_SET_VALUE (def_symbol_in_progress, get_absolute_expression ()); | |
1133 | } /* if symbol based */ | |
1134 | ||
1135 | demand_empty_rest_of_line (); | |
1136 | } | |
1137 | ||
1138 | void | |
1139 | coff_obj_read_begin_hook () | |
1140 | { | |
dcd619be | 1141 | /* These had better be the same. Usually 18 bytes. */ |
252b5132 RH |
1142 | #ifndef BFD_HEADERS |
1143 | know (sizeof (SYMENT) == sizeof (AUXENT)); | |
1144 | know (SYMESZ == AUXESZ); | |
1145 | #endif | |
1146 | tag_init (); | |
1147 | } | |
1148 | ||
252b5132 RH |
1149 | symbolS *coff_last_function; |
1150 | static symbolS *coff_last_bf; | |
1151 | ||
1152 | void | |
1153 | coff_frob_symbol (symp, punt) | |
1154 | symbolS *symp; | |
1155 | int *punt; | |
1156 | { | |
1157 | static symbolS *last_tagP; | |
1158 | static stack *block_stack; | |
1159 | static symbolS *set_end; | |
1160 | symbolS *next_set_end = NULL; | |
1161 | ||
1162 | if (symp == &abs_symbol) | |
1163 | { | |
1164 | *punt = 1; | |
1165 | return; | |
1166 | } | |
1167 | ||
1168 | if (current_lineno_sym) | |
1169 | coff_add_linesym ((symbolS *) 0); | |
1170 | ||
1171 | if (!block_stack) | |
1172 | block_stack = stack_init (512, sizeof (symbolS*)); | |
1173 | ||
1174 | if (S_IS_WEAK (symp)) | |
1175 | { | |
1176 | #ifdef TE_PE | |
1177 | S_SET_STORAGE_CLASS (symp, C_NT_WEAK); | |
1178 | #else | |
1179 | S_SET_STORAGE_CLASS (symp, C_WEAKEXT); | |
1180 | #endif | |
1181 | } | |
1182 | ||
1183 | if (!S_IS_DEFINED (symp) | |
1184 | && !S_IS_WEAK (symp) | |
1185 | && S_GET_STORAGE_CLASS (symp) != C_STAT) | |
1186 | S_SET_STORAGE_CLASS (symp, C_EXT); | |
1187 | ||
1188 | if (!SF_GET_DEBUG (symp)) | |
1189 | { | |
bdbe95c8 NC |
1190 | symbolS * real; |
1191 | ||
252b5132 RH |
1192 | if (!SF_GET_LOCAL (symp) |
1193 | && !SF_GET_STATICS (symp) | |
39da8128 DD |
1194 | && S_GET_STORAGE_CLASS (symp) != C_LABEL |
1195 | && symbol_constant_p(symp) | |
252b5132 | 1196 | && (real = symbol_find_base (S_GET_NAME (symp), DO_NOT_STRIP)) |
bdbe95c8 | 1197 | && S_GET_STORAGE_CLASS (real) == C_NULL |
252b5132 RH |
1198 | && real != symp) |
1199 | { | |
1200 | c_symbol_merge (symp, real); | |
1201 | *punt = 1; | |
39da8128 | 1202 | return; |
252b5132 | 1203 | } |
bdbe95c8 | 1204 | |
5e0d736c | 1205 | if (!S_IS_DEFINED (symp) && !SF_GET_LOCAL (symp)) |
252b5132 | 1206 | { |
5e0d736c DD |
1207 | assert (S_GET_VALUE (symp) == 0); |
1208 | S_SET_EXTERNAL (symp); | |
1209 | } | |
1210 | else if (S_GET_STORAGE_CLASS (symp) == C_NULL) | |
1211 | { | |
1212 | if (S_GET_SEGMENT (symp) == text_section | |
1213 | && symp != seg_info (text_section)->sym) | |
1214 | S_SET_STORAGE_CLASS (symp, C_LABEL); | |
252b5132 | 1215 | else |
5e0d736c | 1216 | S_SET_STORAGE_CLASS (symp, C_STAT); |
252b5132 | 1217 | } |
bdbe95c8 | 1218 | |
252b5132 RH |
1219 | if (SF_GET_PROCESS (symp)) |
1220 | { | |
1221 | if (S_GET_STORAGE_CLASS (symp) == C_BLOCK) | |
1222 | { | |
1223 | if (!strcmp (S_GET_NAME (symp), ".bb")) | |
1224 | stack_push (block_stack, (char *) &symp); | |
1225 | else | |
1226 | { | |
1227 | symbolS *begin; | |
bdbe95c8 | 1228 | |
252b5132 RH |
1229 | begin = *(symbolS **) stack_pop (block_stack); |
1230 | if (begin == 0) | |
1231 | as_warn (_("mismatched .eb")); | |
1232 | else | |
1233 | next_set_end = begin; | |
1234 | } | |
1235 | } | |
bdbe95c8 | 1236 | |
252b5132 RH |
1237 | if (coff_last_function == 0 && SF_GET_FUNCTION (symp)) |
1238 | { | |
1239 | union internal_auxent *auxp; | |
bdbe95c8 | 1240 | |
252b5132 RH |
1241 | coff_last_function = symp; |
1242 | if (S_GET_NUMBER_AUXILIARY (symp) < 1) | |
1243 | S_SET_NUMBER_AUXILIARY (symp, 1); | |
0561a208 | 1244 | auxp = SYM_AUXENT (symp); |
252b5132 RH |
1245 | memset (auxp->x_sym.x_fcnary.x_ary.x_dimen, 0, |
1246 | sizeof (auxp->x_sym.x_fcnary.x_ary.x_dimen)); | |
1247 | } | |
bdbe95c8 | 1248 | |
252b5132 RH |
1249 | if (S_GET_STORAGE_CLASS (symp) == C_EFCN) |
1250 | { | |
1251 | if (coff_last_function == 0) | |
1252 | as_fatal (_("C_EFCN symbol out of scope")); | |
1253 | SA_SET_SYM_FSIZE (coff_last_function, | |
1254 | (long) (S_GET_VALUE (symp) | |
1255 | - S_GET_VALUE (coff_last_function))); | |
1256 | next_set_end = coff_last_function; | |
1257 | coff_last_function = 0; | |
1258 | } | |
1259 | } | |
bdbe95c8 | 1260 | |
252b5132 RH |
1261 | if (S_IS_EXTERNAL (symp)) |
1262 | S_SET_STORAGE_CLASS (symp, C_EXT); | |
1263 | else if (SF_GET_LOCAL (symp)) | |
1264 | *punt = 1; | |
1265 | ||
1266 | if (SF_GET_FUNCTION (symp)) | |
49309057 | 1267 | symbol_get_bfdsym (symp)->flags |= BSF_FUNCTION; |
252b5132 | 1268 | |
dcd619be | 1269 | /* more ... */ |
252b5132 RH |
1270 | } |
1271 | ||
8828d862 ILT |
1272 | /* Double check weak symbols. */ |
1273 | if (S_IS_WEAK (symp) && S_IS_COMMON (symp)) | |
1274 | as_bad (_("Symbol `%s' can not be both weak and common"), | |
1275 | S_GET_NAME (symp)); | |
1276 | ||
252b5132 RH |
1277 | if (SF_GET_TAG (symp)) |
1278 | last_tagP = symp; | |
1279 | else if (S_GET_STORAGE_CLASS (symp) == C_EOS) | |
1280 | next_set_end = last_tagP; | |
1281 | ||
1282 | #ifdef OBJ_XCOFF | |
1283 | /* This is pretty horrible, but we have to set *punt correctly in | |
1284 | order to call SA_SET_SYM_ENDNDX correctly. */ | |
809ffe0d | 1285 | if (! symbol_used_in_reloc_p (symp) |
49309057 | 1286 | && ((symbol_get_bfdsym (symp)->flags & BSF_SECTION_SYM) != 0 |
252b5132 | 1287 | || (! S_IS_EXTERNAL (symp) |
809ffe0d | 1288 | && ! symbol_get_tc (symp)->output |
252b5132 RH |
1289 | && S_GET_STORAGE_CLASS (symp) != C_FILE))) |
1290 | *punt = 1; | |
1291 | #endif | |
1292 | ||
1293 | if (set_end != (symbolS *) NULL | |
1294 | && ! *punt | |
49309057 | 1295 | && ((symbol_get_bfdsym (symp)->flags & BSF_NOT_AT_END) != 0 |
252b5132 RH |
1296 | || (S_IS_DEFINED (symp) |
1297 | && ! S_IS_COMMON (symp) | |
1298 | && (! S_IS_EXTERNAL (symp) || SF_GET_FUNCTION (symp))))) | |
1299 | { | |
1300 | SA_SET_SYM_ENDNDX (set_end, symp); | |
1301 | set_end = NULL; | |
1302 | } | |
1303 | ||
a04b544b ILT |
1304 | if (next_set_end != NULL) |
1305 | { | |
1306 | if (set_end != NULL) | |
1307 | as_warn ("Warning: internal error: forgetting to set endndx of %s", | |
1308 | S_GET_NAME (set_end)); | |
1309 | set_end = next_set_end; | |
1310 | } | |
252b5132 RH |
1311 | |
1312 | if (! *punt | |
1313 | && S_GET_STORAGE_CLASS (symp) == C_FCN | |
1314 | && strcmp (S_GET_NAME (symp), ".bf") == 0) | |
1315 | { | |
1316 | if (coff_last_bf != NULL) | |
1317 | SA_SET_SYM_ENDNDX (coff_last_bf, symp); | |
1318 | coff_last_bf = symp; | |
1319 | } | |
1320 | ||
49309057 | 1321 | if (coffsymbol (symbol_get_bfdsym (symp))->lineno) |
252b5132 RH |
1322 | { |
1323 | int i; | |
1324 | struct line_no *lptr; | |
1325 | alent *l; | |
1326 | ||
49309057 | 1327 | lptr = (struct line_no *) coffsymbol (symbol_get_bfdsym (symp))->lineno; |
252b5132 RH |
1328 | for (i = 0; lptr; lptr = lptr->next) |
1329 | i++; | |
49309057 | 1330 | lptr = (struct line_no *) coffsymbol (symbol_get_bfdsym (symp))->lineno; |
252b5132 RH |
1331 | |
1332 | /* We need i entries for line numbers, plus 1 for the first | |
1333 | entry which BFD will override, plus 1 for the last zero | |
1334 | entry (a marker for BFD). */ | |
1335 | l = (alent *) xmalloc ((i + 2) * sizeof (alent)); | |
49309057 | 1336 | coffsymbol (symbol_get_bfdsym (symp))->lineno = l; |
252b5132 RH |
1337 | l[i + 1].line_number = 0; |
1338 | l[i + 1].u.sym = NULL; | |
1339 | for (; i > 0; i--) | |
1340 | { | |
1341 | if (lptr->frag) | |
bea9907b | 1342 | lptr->l.u.offset += lptr->frag->fr_address / OCTETS_PER_BYTE; |
252b5132 RH |
1343 | l[i] = lptr->l; |
1344 | lptr = lptr->next; | |
1345 | } | |
1346 | } | |
1347 | } | |
1348 | ||
1349 | void | |
1350 | coff_adjust_section_syms (abfd, sec, x) | |
c4bf532f | 1351 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 | 1352 | asection *sec; |
c4bf532f | 1353 | PTR x ATTRIBUTE_UNUSED; |
252b5132 RH |
1354 | { |
1355 | symbolS *secsym; | |
1356 | segment_info_type *seginfo = seg_info (sec); | |
1357 | int nlnno, nrelocs = 0; | |
1358 | ||
1359 | /* RS/6000 gas creates a .debug section manually in ppc_frob_file in | |
1360 | tc-ppc.c. Do not get confused by it. */ | |
1361 | if (seginfo == NULL) | |
1362 | return; | |
1363 | ||
1364 | if (!strcmp (sec->name, ".text")) | |
1365 | nlnno = coff_n_line_nos; | |
1366 | else | |
1367 | nlnno = 0; | |
1368 | { | |
1369 | /* @@ Hope that none of the fixups expand to more than one reloc | |
1370 | entry... */ | |
1371 | fixS *fixp = seginfo->fix_root; | |
1372 | while (fixp) | |
1373 | { | |
1374 | if (! fixp->fx_done) | |
1375 | nrelocs++; | |
1376 | fixp = fixp->fx_next; | |
1377 | } | |
1378 | } | |
1379 | if (bfd_get_section_size_before_reloc (sec) == 0 | |
1380 | && nrelocs == 0 | |
1381 | && nlnno == 0 | |
1382 | && sec != text_section | |
1383 | && sec != data_section | |
1384 | && sec != bss_section) | |
1385 | return; | |
1386 | secsym = section_symbol (sec); | |
945a1a6b ILT |
1387 | /* This is an estimate; we'll plug in the real value using |
1388 | SET_SECTION_RELOCS later */ | |
252b5132 RH |
1389 | SA_SET_SCN_NRELOC (secsym, nrelocs); |
1390 | SA_SET_SCN_NLINNO (secsym, nlnno); | |
1391 | } | |
1392 | ||
1393 | void | |
1394 | coff_frob_file_after_relocs () | |
1395 | { | |
1396 | bfd_map_over_sections (stdoutput, coff_adjust_section_syms, (char*) 0); | |
1397 | } | |
1398 | ||
1399 | /* | |
1400 | * implement the .section pseudo op: | |
1401 | * .section name {, "flags"} | |
1402 | * ^ ^ | |
1403 | * | +--- optional flags: 'b' for bss | |
1404 | * | 'i' for info | |
1405 | * +-- section name 'l' for lib | |
1406 | * 'n' for noload | |
1407 | * 'o' for over | |
1408 | * 'w' for data | |
1409 | * 'd' (apparently m88k for data) | |
1410 | * 'x' for text | |
1411 | * 'r' for read-only data | |
2dcc60be | 1412 | * 's' for shared data (PE) |
252b5132 RH |
1413 | * But if the argument is not a quoted string, treat it as a |
1414 | * subsegment number. | |
1415 | */ | |
1416 | ||
1417 | void | |
1418 | obj_coff_section (ignore) | |
c4bf532f | 1419 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
1420 | { |
1421 | /* Strip out the section name */ | |
1422 | char *section_name; | |
1423 | char c; | |
1424 | char *name; | |
1425 | unsigned int exp; | |
c9900432 | 1426 | flagword flags, oldflags; |
252b5132 RH |
1427 | asection *sec; |
1428 | ||
1429 | if (flag_mri) | |
1430 | { | |
1431 | char type; | |
1432 | ||
1433 | s_mri_sect (&type); | |
1434 | return; | |
1435 | } | |
1436 | ||
1437 | section_name = input_line_pointer; | |
1438 | c = get_symbol_end (); | |
1439 | ||
1440 | name = xmalloc (input_line_pointer - section_name + 1); | |
1441 | strcpy (name, section_name); | |
1442 | ||
1443 | *input_line_pointer = c; | |
1444 | ||
1445 | SKIP_WHITESPACE (); | |
1446 | ||
1447 | exp = 0; | |
c9900432 | 1448 | flags = SEC_NO_FLAGS; |
252b5132 RH |
1449 | |
1450 | if (*input_line_pointer == ',') | |
1451 | { | |
1452 | ++input_line_pointer; | |
1453 | SKIP_WHITESPACE (); | |
1454 | if (*input_line_pointer != '"') | |
1455 | exp = get_absolute_expression (); | |
1456 | else | |
1457 | { | |
1458 | ++input_line_pointer; | |
1459 | while (*input_line_pointer != '"' | |
1460 | && ! is_end_of_line[(unsigned char) *input_line_pointer]) | |
1461 | { | |
1462 | switch (*input_line_pointer) | |
1463 | { | |
1464 | case 'b': flags |= SEC_ALLOC; flags &=~ SEC_LOAD; break; | |
1af96959 | 1465 | case 'n': flags &=~ SEC_LOAD; flags |= SEC_NEVER_LOAD; break; |
5881e4aa ILT |
1466 | case 'd': flags |= SEC_DATA | SEC_LOAD; /* fall through */ |
1467 | case 'w': flags &=~ SEC_READONLY; break; | |
1468 | case 'x': flags |= SEC_CODE | SEC_LOAD; break; | |
252b5132 | 1469 | case 'r': flags |= SEC_READONLY; break; |
2dcc60be | 1470 | case 's': flags |= SEC_SHARED; break; |
252b5132 RH |
1471 | |
1472 | case 'i': /* STYP_INFO */ | |
1473 | case 'l': /* STYP_LIB */ | |
1474 | case 'o': /* STYP_OVER */ | |
1475 | as_warn (_("unsupported section attribute '%c'"), | |
1476 | *input_line_pointer); | |
1477 | break; | |
1478 | ||
1479 | default: | |
1480 | as_warn(_("unknown section attribute '%c'"), | |
1481 | *input_line_pointer); | |
1482 | break; | |
1483 | } | |
1484 | ++input_line_pointer; | |
1485 | } | |
1486 | if (*input_line_pointer == '"') | |
1487 | ++input_line_pointer; | |
1488 | } | |
1489 | } | |
1490 | ||
1491 | sec = subseg_new (name, (subsegT) exp); | |
1492 | ||
c9900432 NC |
1493 | oldflags = bfd_get_section_flags (stdoutput, sec); |
1494 | if (oldflags == SEC_NO_FLAGS) | |
252b5132 | 1495 | { |
c9900432 NC |
1496 | /* Set section flags for a new section just created by subseg_new. |
1497 | Provide a default if no flags were parsed. */ | |
1498 | if (flags == SEC_NO_FLAGS) | |
1ad5eac0 | 1499 | flags = TC_COFF_SECTION_DEFAULT_ATTRIBUTES; |
dcd619be | 1500 | |
c9900432 NC |
1501 | #ifdef COFF_LONG_SECTION_NAMES |
1502 | /* Add SEC_LINK_ONCE and SEC_LINK_DUPLICATES_DISCARD to .gnu.linkonce | |
1503 | sections so adjust_reloc_syms in write.c will correctly handle | |
1504 | relocs which refer to non-local symbols in these sections. */ | |
814f6641 | 1505 | if (strncmp (name, ".gnu.linkonce", sizeof (".gnu.linkonce") - 1) == 0) |
c9900432 NC |
1506 | flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; |
1507 | #endif | |
252b5132 RH |
1508 | |
1509 | if (! bfd_set_section_flags (stdoutput, sec, flags)) | |
c9900432 NC |
1510 | as_warn (_("error setting flags for \"%s\": %s"), |
1511 | bfd_section_name (stdoutput, sec), | |
1512 | bfd_errmsg (bfd_get_error ())); | |
1513 | } | |
1514 | else if (flags != SEC_NO_FLAGS) | |
1515 | { | |
1516 | /* This section's attributes have already been set. Warn if the | |
1517 | attributes don't match. */ | |
5dd0794d AM |
1518 | flagword matchflags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE |
1519 | | SEC_DATA | SEC_SHARED | SEC_NEVER_LOAD); | |
c9900432 NC |
1520 | if ((flags ^ oldflags) & matchflags) |
1521 | as_warn (_("Ignoring changed section attributes for %s"), name); | |
252b5132 RH |
1522 | } |
1523 | ||
1524 | demand_empty_rest_of_line (); | |
1525 | } | |
1526 | ||
1527 | void | |
1528 | coff_adjust_symtab () | |
1529 | { | |
1530 | if (symbol_rootP == NULL | |
1531 | || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE) | |
1532 | c_dot_file_symbol ("fake"); | |
1533 | } | |
1534 | ||
1535 | void | |
1536 | coff_frob_section (sec) | |
1537 | segT sec; | |
1538 | { | |
1539 | segT strsec; | |
1540 | char *p; | |
1541 | fragS *fragp; | |
1542 | bfd_vma size, n_entries, mask; | |
bea9907b | 1543 | bfd_vma align_power = (bfd_vma)sec->alignment_power + OCTETS_PER_BYTE_POWER; |
252b5132 RH |
1544 | |
1545 | /* The COFF back end in BFD requires that all section sizes be | |
bea9907b TW |
1546 | rounded up to multiples of the corresponding section alignments, |
1547 | supposedly because standard COFF has no other way of encoding alignment | |
1548 | for sections. If your COFF flavor has a different way of encoding | |
dcd619be | 1549 | section alignment, then skip this step, as TICOFF does. */ |
252b5132 | 1550 | size = bfd_get_section_size_before_reloc (sec); |
bea9907b TW |
1551 | mask = ((bfd_vma) 1 << align_power) - 1; |
1552 | #if !defined(TICOFF) | |
252b5132 RH |
1553 | if (size & mask) |
1554 | { | |
7f788821 NC |
1555 | bfd_vma new_size; |
1556 | fragS *last; | |
dcd619be | 1557 | |
7f788821 NC |
1558 | new_size = (size + mask) & ~mask; |
1559 | bfd_set_section_size (stdoutput, sec, new_size); | |
1560 | ||
1561 | /* If the size had to be rounded up, add some padding in | |
1562 | the last non-empty frag. */ | |
1563 | fragp = seg_info (sec)->frchainP->frch_root; | |
1564 | last = seg_info (sec)->frchainP->frch_last; | |
1565 | while (fragp->fr_next != last) | |
1566 | fragp = fragp->fr_next; | |
1567 | last->fr_address = size; | |
1568 | fragp->fr_offset += new_size - size; | |
252b5132 | 1569 | } |
bea9907b | 1570 | #endif |
252b5132 RH |
1571 | |
1572 | /* If the section size is non-zero, the section symbol needs an aux | |
1573 | entry associated with it, indicating the size. We don't know | |
1574 | all the values yet; coff_frob_symbol will fill them in later. */ | |
bea9907b | 1575 | #ifndef TICOFF |
252b5132 RH |
1576 | if (size != 0 |
1577 | || sec == text_section | |
1578 | || sec == data_section | |
1579 | || sec == bss_section) | |
bea9907b | 1580 | #endif |
252b5132 RH |
1581 | { |
1582 | symbolS *secsym = section_symbol (sec); | |
1583 | ||
1584 | S_SET_STORAGE_CLASS (secsym, C_STAT); | |
1585 | S_SET_NUMBER_AUXILIARY (secsym, 1); | |
1586 | SF_SET_STATICS (secsym); | |
1587 | SA_SET_SCN_SCNLEN (secsym, size); | |
1588 | } | |
1589 | ||
1590 | /* @@ these should be in a "stabs.h" file, or maybe as.h */ | |
1591 | #ifndef STAB_SECTION_NAME | |
1592 | #define STAB_SECTION_NAME ".stab" | |
1593 | #endif | |
1594 | #ifndef STAB_STRING_SECTION_NAME | |
1595 | #define STAB_STRING_SECTION_NAME ".stabstr" | |
1596 | #endif | |
1597 | if (strcmp (STAB_STRING_SECTION_NAME, sec->name)) | |
1598 | return; | |
1599 | ||
1600 | strsec = sec; | |
1601 | sec = subseg_get (STAB_SECTION_NAME, 0); | |
1602 | /* size is already rounded up, since other section will be listed first */ | |
1603 | size = bfd_get_section_size_before_reloc (strsec); | |
1604 | ||
1605 | n_entries = bfd_get_section_size_before_reloc (sec) / 12 - 1; | |
1606 | ||
1607 | /* Find first non-empty frag. It should be large enough. */ | |
1608 | fragp = seg_info (sec)->frchainP->frch_root; | |
1609 | while (fragp && fragp->fr_fix == 0) | |
1610 | fragp = fragp->fr_next; | |
1611 | assert (fragp != 0 && fragp->fr_fix >= 12); | |
1612 | ||
1613 | /* Store the values. */ | |
1614 | p = fragp->fr_literal; | |
1615 | bfd_h_put_16 (stdoutput, n_entries, (bfd_byte *) p + 6); | |
1616 | bfd_h_put_32 (stdoutput, size, (bfd_byte *) p + 8); | |
1617 | } | |
1618 | ||
1619 | void | |
1620 | obj_coff_init_stab_section (seg) | |
1621 | segT seg; | |
1622 | { | |
1623 | char *file; | |
1624 | char *p; | |
1625 | char *stabstr_name; | |
1626 | unsigned int stroff; | |
1627 | ||
dcd619be | 1628 | /* Make space for this first symbol. */ |
252b5132 | 1629 | p = frag_more (12); |
dcd619be | 1630 | /* Zero it out. */ |
252b5132 RH |
1631 | memset (p, 0, 12); |
1632 | as_where (&file, (unsigned int *) NULL); | |
1633 | stabstr_name = (char *) alloca (strlen (seg->name) + 4); | |
1634 | strcpy (stabstr_name, seg->name); | |
1635 | strcat (stabstr_name, "str"); | |
1636 | stroff = get_stab_string_offset (file, stabstr_name); | |
1637 | know (stroff == 1); | |
1638 | md_number_to_chars (p, stroff, 4); | |
1639 | } | |
1640 | ||
1641 | #ifdef DEBUG | |
1642 | /* for debugging */ | |
1643 | const char * | |
1644 | s_get_name (s) | |
1645 | symbolS *s; | |
1646 | { | |
1647 | return ((s == NULL) ? "(NULL)" : S_GET_NAME (s)); | |
1648 | } | |
1649 | ||
1650 | void | |
1651 | symbol_dump () | |
1652 | { | |
1653 | symbolS *symbolP; | |
1654 | ||
1655 | for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP)) | |
1656 | { | |
814f6641 | 1657 | printf (_("0x%lx: \"%s\" type = %ld, class = %d, segment = %d\n"), |
252b5132 RH |
1658 | (unsigned long) symbolP, |
1659 | S_GET_NAME(symbolP), | |
1660 | (long) S_GET_DATA_TYPE(symbolP), | |
1661 | S_GET_STORAGE_CLASS(symbolP), | |
1662 | (int) S_GET_SEGMENT(symbolP)); | |
1663 | } | |
1664 | } | |
1665 | ||
1666 | #endif /* DEBUG */ | |
1667 | ||
1668 | #else /* not BFD_ASSEMBLER */ | |
1669 | ||
1670 | #include "frags.h" | |
dcd619be | 1671 | /* This is needed because we include internal bfd things. */ |
252b5132 RH |
1672 | #include <time.h> |
1673 | ||
1674 | #include "libbfd.h" | |
1675 | #include "libcoff.h" | |
1676 | ||
1677 | #ifdef TE_PE | |
1678 | #include "coff/pe.h" | |
1679 | #endif | |
1680 | ||
1681 | /* The NOP_OPCODE is for the alignment fill value. Fill with nop so | |
1682 | that we can stick sections together without causing trouble. */ | |
1683 | #ifndef NOP_OPCODE | |
1684 | #define NOP_OPCODE 0x00 | |
1685 | #endif | |
1686 | ||
1687 | /* The zeroes if symbol name is longer than 8 chars */ | |
1688 | #define S_SET_ZEROES(s,v) ((s)->sy_symbol.ost_entry.n_zeroes = (v)) | |
1689 | ||
1690 | #define MIN(a,b) ((a) < (b)? (a) : (b)) | |
a04b544b ILT |
1691 | |
1692 | /* This vector is used to turn a gas internal segment number into a | |
1693 | section number suitable for insertion into a coff symbol table. | |
1694 | This must correspond to seg_info_off_by_4. */ | |
252b5132 RH |
1695 | |
1696 | const short seg_N_TYPE[] = | |
1697 | { /* in: segT out: N_TYPE bits */ | |
1698 | C_ABS_SECTION, | |
1699 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | |
1700 | 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, | |
1701 | 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, | |
1702 | 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, | |
1703 | C_UNDEF_SECTION, /* SEG_UNKNOWN */ | |
1704 | C_UNDEF_SECTION, /* SEG_GOOF */ | |
1705 | C_UNDEF_SECTION, /* SEG_EXPR */ | |
1706 | C_DEBUG_SECTION, /* SEG_DEBUG */ | |
1707 | C_NTV_SECTION, /* SEG_NTV */ | |
1708 | C_PTV_SECTION, /* SEG_PTV */ | |
1709 | C_REGISTER_SECTION, /* SEG_REGISTER */ | |
1710 | }; | |
1711 | ||
1712 | int function_lineoff = -1; /* Offset in line#s where the last function | |
1713 | started (the odd entry for line #0) */ | |
1714 | ||
1715 | /* structure used to keep the filenames which | |
1716 | are too long around so that we can stick them | |
1717 | into the string table */ | |
dcd619be | 1718 | struct filename_list |
252b5132 RH |
1719 | { |
1720 | char *filename; | |
1721 | struct filename_list *next; | |
1722 | }; | |
1723 | ||
1724 | static struct filename_list *filename_list_head; | |
1725 | static struct filename_list *filename_list_tail; | |
1726 | ||
1727 | static symbolS *last_line_symbol; | |
1728 | ||
1729 | /* Add 4 to the real value to get the index and compensate the | |
1730 | negatives. This vector is used by S_GET_SEGMENT to turn a coff | |
1731 | section number into a segment number | |
1732 | */ | |
1733 | static symbolS *previous_file_symbol; | |
1734 | void c_symbol_merge (); | |
1735 | static int line_base; | |
1736 | ||
1737 | symbolS *c_section_symbol (); | |
1738 | bfd *abfd; | |
1739 | ||
1740 | static void fixup_segment PARAMS ((segment_info_type *segP, | |
1741 | segT this_segment_type)); | |
1742 | ||
252b5132 RH |
1743 | static void fixup_mdeps PARAMS ((fragS *, |
1744 | object_headers *, | |
1745 | segT)); | |
1746 | ||
252b5132 RH |
1747 | static void fill_section PARAMS ((bfd * abfd, |
1748 | object_headers *, | |
1749 | unsigned long *)); | |
1750 | ||
252b5132 RH |
1751 | static int c_line_new PARAMS ((symbolS * symbol, long paddr, |
1752 | int line_number, | |
1753 | fragS * frag)); | |
1754 | ||
252b5132 RH |
1755 | static void w_symbols PARAMS ((bfd * abfd, char *where, |
1756 | symbolS * symbol_rootP)); | |
1757 | ||
1758 | static void adjust_stab_section PARAMS ((bfd *abfd, segT seg)); | |
1759 | ||
1760 | static void obj_coff_lcomm PARAMS ((int)); | |
1761 | static void obj_coff_text PARAMS ((int)); | |
1762 | static void obj_coff_data PARAMS ((int)); | |
252b5132 RH |
1763 | void obj_coff_section PARAMS ((int)); |
1764 | ||
a04b544b | 1765 | /* When not using BFD_ASSEMBLER, we permit up to 40 sections. |
252b5132 | 1766 | |
a04b544b ILT |
1767 | This array maps a COFF section number into a gas section number. |
1768 | Because COFF uses negative section numbers, you must add 4 to the | |
1769 | COFF section number when indexing into this array; this is done via | |
1770 | the SEG_INFO_FROM_SECTION_NUMBER macro. This must correspond to | |
1771 | seg_N_TYPE. */ | |
252b5132 | 1772 | |
a04b544b | 1773 | static const segT seg_info_off_by_4[] = |
252b5132 | 1774 | { |
a04b544b ILT |
1775 | SEG_PTV, |
1776 | SEG_NTV, | |
1777 | SEG_DEBUG, | |
1778 | SEG_ABSOLUTE, | |
1779 | SEG_UNKNOWN, | |
1780 | SEG_E0, SEG_E1, SEG_E2, SEG_E3, SEG_E4, | |
1781 | SEG_E5, SEG_E6, SEG_E7, SEG_E8, SEG_E9, | |
1782 | SEG_E10, SEG_E11, SEG_E12, SEG_E13, SEG_E14, | |
1783 | SEG_E15, SEG_E16, SEG_E17, SEG_E18, SEG_E19, | |
1784 | SEG_E20, SEG_E21, SEG_E22, SEG_E23, SEG_E24, | |
1785 | SEG_E25, SEG_E26, SEG_E27, SEG_E28, SEG_E29, | |
1786 | SEG_E30, SEG_E31, SEG_E32, SEG_E33, SEG_E34, | |
1787 | SEG_E35, SEG_E36, SEG_E37, SEG_E38, SEG_E39, | |
1788 | (segT) 40, | |
1789 | (segT) 41, | |
1790 | (segT) 42, | |
1791 | (segT) 43, | |
1792 | (segT) 44, | |
1793 | (segT) 45, | |
1794 | (segT) 0, | |
1795 | (segT) 0, | |
1796 | (segT) 0, | |
1797 | SEG_REGISTER | |
252b5132 RH |
1798 | }; |
1799 | ||
252b5132 RH |
1800 | #define SEG_INFO_FROM_SECTION_NUMBER(x) (seg_info_off_by_4[(x)+4]) |
1801 | ||
1802 | static relax_addressT | |
1803 | relax_align (address, alignment) | |
1804 | relax_addressT address; | |
1805 | long alignment; | |
1806 | { | |
1807 | relax_addressT mask; | |
1808 | relax_addressT new_address; | |
1809 | ||
1810 | mask = ~((~0) << alignment); | |
1811 | new_address = (address + mask) & (~mask); | |
1812 | return (new_address - address); | |
1813 | } | |
1814 | ||
252b5132 RH |
1815 | segT |
1816 | s_get_segment (x) | |
1817 | symbolS * x; | |
1818 | { | |
a04b544b | 1819 | return SEG_INFO_FROM_SECTION_NUMBER (x->sy_symbol.ost_entry.n_scnum); |
252b5132 RH |
1820 | } |
1821 | ||
1822 | /* calculate the size of the frag chain and fill in the section header | |
1823 | to contain all of it, also fill in the addr of the sections */ | |
1824 | static unsigned int | |
1825 | size_section (abfd, idx) | |
a04b544b | 1826 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
1827 | unsigned int idx; |
1828 | { | |
1829 | ||
1830 | unsigned int size = 0; | |
1831 | fragS *frag = segment_info[idx].frchainP->frch_root; | |
1832 | while (frag) | |
1833 | { | |
1834 | size = frag->fr_address; | |
1835 | if (frag->fr_address != size) | |
1836 | { | |
1837 | fprintf (stderr, _("Out of step\n")); | |
1838 | size = frag->fr_address; | |
1839 | } | |
1840 | ||
1841 | switch (frag->fr_type) | |
1842 | { | |
1843 | #ifdef TC_COFF_SIZEMACHDEP | |
1844 | case rs_machine_dependent: | |
1845 | size += TC_COFF_SIZEMACHDEP (frag); | |
1846 | break; | |
1847 | #endif | |
1848 | case rs_space: | |
252b5132 RH |
1849 | case rs_fill: |
1850 | case rs_org: | |
1851 | size += frag->fr_fix; | |
1852 | size += frag->fr_offset * frag->fr_var; | |
1853 | break; | |
1854 | case rs_align: | |
1855 | case rs_align_code: | |
0a9ef439 | 1856 | case rs_align_test: |
252b5132 RH |
1857 | { |
1858 | addressT off; | |
1859 | ||
1860 | size += frag->fr_fix; | |
1861 | off = relax_align (size, frag->fr_offset); | |
1862 | if (frag->fr_subtype != 0 && off > frag->fr_subtype) | |
1863 | off = 0; | |
1864 | size += off; | |
1865 | } | |
1866 | break; | |
1867 | default: | |
1868 | BAD_CASE (frag->fr_type); | |
1869 | break; | |
1870 | } | |
1871 | frag = frag->fr_next; | |
1872 | } | |
1873 | segment_info[idx].scnhdr.s_size = size; | |
1874 | return size; | |
1875 | } | |
1876 | ||
252b5132 RH |
1877 | static unsigned int |
1878 | count_entries_in_chain (idx) | |
1879 | unsigned int idx; | |
1880 | { | |
1881 | unsigned int nrelocs; | |
1882 | fixS *fixup_ptr; | |
1883 | ||
1884 | /* Count the relocations */ | |
1885 | fixup_ptr = segment_info[idx].fix_root; | |
1886 | nrelocs = 0; | |
1887 | while (fixup_ptr != (fixS *) NULL) | |
1888 | { | |
1889 | if (fixup_ptr->fx_done == 0 && TC_COUNT_RELOC (fixup_ptr)) | |
1890 | { | |
1891 | #ifdef TC_A29K | |
1892 | if (fixup_ptr->fx_r_type == RELOC_CONSTH) | |
1893 | nrelocs += 2; | |
1894 | else | |
1895 | nrelocs++; | |
1896 | #else | |
1897 | nrelocs++; | |
1898 | #endif | |
1899 | } | |
1900 | ||
1901 | fixup_ptr = fixup_ptr->fx_next; | |
1902 | } | |
1903 | return nrelocs; | |
1904 | } | |
1905 | ||
1906 | #ifdef TE_AUX | |
1907 | ||
1908 | static int compare_external_relocs PARAMS ((const PTR, const PTR)); | |
1909 | ||
1910 | /* AUX's ld expects relocations to be sorted */ | |
1911 | static int | |
1912 | compare_external_relocs (x, y) | |
1913 | const PTR x; | |
1914 | const PTR y; | |
1915 | { | |
1916 | struct external_reloc *a = (struct external_reloc *) x; | |
1917 | struct external_reloc *b = (struct external_reloc *) y; | |
1918 | bfd_vma aadr = bfd_getb32 (a->r_vaddr); | |
1919 | bfd_vma badr = bfd_getb32 (b->r_vaddr); | |
1920 | return (aadr < badr ? -1 : badr < aadr ? 1 : 0); | |
1921 | } | |
1922 | ||
1923 | #endif | |
1924 | ||
1925 | /* output all the relocations for a section */ | |
1926 | void | |
1927 | do_relocs_for (abfd, h, file_cursor) | |
1928 | bfd * abfd; | |
1929 | object_headers * h; | |
1930 | unsigned long *file_cursor; | |
1931 | { | |
1932 | unsigned int nrelocs; | |
1933 | unsigned int idx; | |
1934 | unsigned long reloc_start = *file_cursor; | |
1935 | ||
1936 | for (idx = SEG_E0; idx < SEG_LAST; idx++) | |
1937 | { | |
1938 | if (segment_info[idx].scnhdr.s_name[0]) | |
1939 | { | |
1940 | struct external_reloc *ext_ptr; | |
1941 | struct external_reloc *external_reloc_vec; | |
1942 | unsigned int external_reloc_size; | |
1943 | unsigned int base = segment_info[idx].scnhdr.s_paddr; | |
1944 | fixS *fix_ptr = segment_info[idx].fix_root; | |
1945 | nrelocs = count_entries_in_chain (idx); | |
1946 | ||
1947 | if (nrelocs) | |
1948 | /* Bypass this stuff if no relocs. This also incidentally | |
1949 | avoids a SCO bug, where free(malloc(0)) tends to crash. */ | |
1950 | { | |
1951 | external_reloc_size = nrelocs * RELSZ; | |
1952 | external_reloc_vec = | |
1953 | (struct external_reloc *) malloc (external_reloc_size); | |
1954 | ||
1955 | ext_ptr = external_reloc_vec; | |
1956 | ||
1957 | /* Fill in the internal coff style reloc struct from the | |
1958 | internal fix list. */ | |
1959 | while (fix_ptr) | |
1960 | { | |
1961 | struct internal_reloc intr; | |
1962 | ||
1963 | /* Only output some of the relocations */ | |
1964 | if (fix_ptr->fx_done == 0 && TC_COUNT_RELOC (fix_ptr)) | |
1965 | { | |
1966 | #ifdef TC_RELOC_MANGLE | |
1967 | TC_RELOC_MANGLE (&segment_info[idx], fix_ptr, &intr, | |
1968 | base); | |
1969 | ||
1970 | #else | |
1971 | symbolS *dot; | |
1972 | symbolS *symbol_ptr = fix_ptr->fx_addsy; | |
1973 | ||
1974 | intr.r_type = TC_COFF_FIX2RTYPE (fix_ptr); | |
1975 | intr.r_vaddr = | |
1976 | base + fix_ptr->fx_frag->fr_address + fix_ptr->fx_where; | |
1977 | ||
1978 | #ifdef TC_KEEP_FX_OFFSET | |
1979 | intr.r_offset = fix_ptr->fx_offset; | |
1980 | #else | |
1981 | intr.r_offset = 0; | |
1982 | #endif | |
1983 | ||
1984 | while (symbol_ptr->sy_value.X_op == O_symbol | |
1985 | && (! S_IS_DEFINED (symbol_ptr) | |
1986 | || S_IS_COMMON (symbol_ptr))) | |
1987 | { | |
1988 | symbolS *n; | |
1989 | ||
1990 | /* We must avoid looping, as that can occur | |
1991 | with a badly written program. */ | |
1992 | n = symbol_ptr->sy_value.X_add_symbol; | |
1993 | if (n == symbol_ptr) | |
1994 | break; | |
1995 | symbol_ptr = n; | |
1996 | } | |
1997 | ||
1998 | /* Turn the segment of the symbol into an offset. */ | |
1999 | if (symbol_ptr) | |
2000 | { | |
6386f3a7 | 2001 | resolve_symbol_value (symbol_ptr); |
252b5132 RH |
2002 | if (! symbol_ptr->sy_resolved) |
2003 | { | |
2004 | char *file; | |
2005 | unsigned int line; | |
2006 | ||
2007 | if (expr_symbol_where (symbol_ptr, &file, &line)) | |
2008 | as_bad_where (file, line, | |
2009 | _("unresolved relocation")); | |
2010 | else | |
2011 | as_bad (_("bad relocation: symbol `%s' not in symbol table"), | |
2012 | S_GET_NAME (symbol_ptr)); | |
2013 | } | |
2014 | dot = segment_info[S_GET_SEGMENT (symbol_ptr)].dot; | |
2015 | if (dot) | |
2016 | { | |
2017 | intr.r_symndx = dot->sy_number; | |
2018 | } | |
2019 | else | |
2020 | { | |
2021 | intr.r_symndx = symbol_ptr->sy_number; | |
2022 | } | |
2023 | ||
2024 | } | |
2025 | else | |
2026 | { | |
2027 | intr.r_symndx = -1; | |
2028 | } | |
2029 | #endif | |
2030 | ||
2031 | (void) bfd_coff_swap_reloc_out (abfd, &intr, ext_ptr); | |
2032 | ext_ptr++; | |
2033 | ||
2034 | #if defined(TC_A29K) | |
2035 | ||
2036 | /* The 29k has a special kludge for the high 16 bit | |
2037 | reloc. Two relocations are emited, R_IHIHALF, | |
2038 | and R_IHCONST. The second one doesn't contain a | |
2039 | symbol, but uses the value for offset. */ | |
2040 | ||
2041 | if (intr.r_type == R_IHIHALF) | |
2042 | { | |
2043 | /* now emit the second bit */ | |
2044 | intr.r_type = R_IHCONST; | |
2045 | intr.r_symndx = fix_ptr->fx_addnumber; | |
2046 | (void) bfd_coff_swap_reloc_out (abfd, &intr, ext_ptr); | |
2047 | ext_ptr++; | |
2048 | } | |
2049 | #endif | |
2050 | } | |
2051 | ||
2052 | fix_ptr = fix_ptr->fx_next; | |
2053 | } | |
2054 | ||
2055 | #ifdef TE_AUX | |
2056 | /* Sort the reloc table */ | |
2057 | qsort ((PTR) external_reloc_vec, nrelocs, | |
2058 | sizeof (struct external_reloc), compare_external_relocs); | |
2059 | #endif | |
2060 | ||
2061 | /* Write out the reloc table */ | |
2062 | bfd_write ((PTR) external_reloc_vec, 1, external_reloc_size, | |
2063 | abfd); | |
2064 | free (external_reloc_vec); | |
2065 | ||
2066 | /* Fill in section header info. */ | |
2067 | segment_info[idx].scnhdr.s_relptr = *file_cursor; | |
2068 | *file_cursor += external_reloc_size; | |
2069 | segment_info[idx].scnhdr.s_nreloc = nrelocs; | |
2070 | } | |
2071 | else | |
2072 | { | |
2073 | /* No relocs */ | |
2074 | segment_info[idx].scnhdr.s_relptr = 0; | |
2075 | } | |
2076 | } | |
2077 | } | |
2078 | /* Set relocation_size field in file headers */ | |
2079 | H_SET_RELOCATION_SIZE (h, *file_cursor - reloc_start, 0); | |
2080 | } | |
2081 | ||
252b5132 RH |
2082 | /* run through a frag chain and write out the data to go with it, fill |
2083 | in the scnhdrs with the info on the file postions | |
2084 | */ | |
2085 | static void | |
2086 | fill_section (abfd, h, file_cursor) | |
2087 | bfd * abfd; | |
a04b544b | 2088 | object_headers *h ATTRIBUTE_UNUSED; |
252b5132 RH |
2089 | unsigned long *file_cursor; |
2090 | { | |
2091 | ||
2092 | unsigned int i; | |
2093 | unsigned int paddr = 0; | |
2094 | ||
2095 | for (i = SEG_E0; i < SEG_UNKNOWN; i++) | |
2096 | { | |
2097 | unsigned int offset = 0; | |
2098 | struct internal_scnhdr *s = &(segment_info[i].scnhdr); | |
2099 | ||
2100 | PROGRESS (1); | |
2101 | ||
2102 | if (s->s_name[0]) | |
2103 | { | |
2104 | fragS *frag = segment_info[i].frchainP->frch_root; | |
ea3b9044 | 2105 | char *buffer = NULL; |
252b5132 RH |
2106 | |
2107 | if (s->s_size == 0) | |
2108 | s->s_scnptr = 0; | |
2109 | else | |
2110 | { | |
2111 | buffer = xmalloc (s->s_size); | |
2112 | s->s_scnptr = *file_cursor; | |
2113 | } | |
2114 | know (s->s_paddr == paddr); | |
2115 | ||
2116 | if (strcmp (s->s_name, ".text") == 0) | |
2117 | s->s_flags |= STYP_TEXT; | |
2118 | else if (strcmp (s->s_name, ".data") == 0) | |
2119 | s->s_flags |= STYP_DATA; | |
2120 | else if (strcmp (s->s_name, ".bss") == 0) | |
2121 | { | |
2122 | s->s_scnptr = 0; | |
2123 | s->s_flags |= STYP_BSS; | |
2124 | ||
2125 | /* @@ Should make the i386 and a29k coff targets define | |
2126 | COFF_NOLOAD_PROBLEM, and have only one test here. */ | |
2127 | #ifndef TC_I386 | |
2128 | #ifndef TC_A29K | |
2129 | #ifndef COFF_NOLOAD_PROBLEM | |
2130 | /* Apparently the SVR3 linker (and exec syscall) and UDI | |
2131 | mondfe progrem are confused by noload sections. */ | |
2132 | s->s_flags |= STYP_NOLOAD; | |
2133 | #endif | |
2134 | #endif | |
2135 | #endif | |
2136 | } | |
2137 | else if (strcmp (s->s_name, ".lit") == 0) | |
2138 | s->s_flags = STYP_LIT | STYP_TEXT; | |
2139 | else if (strcmp (s->s_name, ".init") == 0) | |
2140 | s->s_flags |= STYP_TEXT; | |
2141 | else if (strcmp (s->s_name, ".fini") == 0) | |
2142 | s->s_flags |= STYP_TEXT; | |
2143 | else if (strncmp (s->s_name, ".comment", 8) == 0) | |
2144 | s->s_flags |= STYP_INFO; | |
2145 | ||
2146 | while (frag) | |
2147 | { | |
2148 | unsigned int fill_size; | |
2149 | switch (frag->fr_type) | |
2150 | { | |
2151 | case rs_machine_dependent: | |
2152 | if (frag->fr_fix) | |
2153 | { | |
2154 | memcpy (buffer + frag->fr_address, | |
2155 | frag->fr_literal, | |
2156 | (unsigned int) frag->fr_fix); | |
2157 | offset += frag->fr_fix; | |
2158 | } | |
2159 | ||
2160 | break; | |
2161 | case rs_space: | |
252b5132 RH |
2162 | case rs_fill: |
2163 | case rs_align: | |
2164 | case rs_align_code: | |
0a9ef439 | 2165 | case rs_align_test: |
252b5132 RH |
2166 | case rs_org: |
2167 | if (frag->fr_fix) | |
2168 | { | |
2169 | memcpy (buffer + frag->fr_address, | |
2170 | frag->fr_literal, | |
2171 | (unsigned int) frag->fr_fix); | |
2172 | offset += frag->fr_fix; | |
2173 | } | |
2174 | ||
2175 | fill_size = frag->fr_var; | |
2176 | if (fill_size && frag->fr_offset > 0) | |
2177 | { | |
2178 | unsigned int count; | |
2179 | unsigned int off = frag->fr_fix; | |
2180 | for (count = frag->fr_offset; count; count--) | |
2181 | { | |
2182 | if (fill_size + frag->fr_address + off <= s->s_size) | |
2183 | { | |
2184 | memcpy (buffer + frag->fr_address + off, | |
2185 | frag->fr_literal + frag->fr_fix, | |
2186 | fill_size); | |
2187 | off += fill_size; | |
2188 | offset += fill_size; | |
2189 | } | |
2190 | } | |
2191 | } | |
2192 | break; | |
2193 | case rs_broken_word: | |
2194 | break; | |
2195 | default: | |
2196 | abort (); | |
2197 | } | |
2198 | frag = frag->fr_next; | |
2199 | } | |
2200 | ||
2201 | if (s->s_size != 0) | |
2202 | { | |
2203 | if (s->s_scnptr != 0) | |
2204 | { | |
2205 | bfd_write (buffer, s->s_size, 1, abfd); | |
2206 | *file_cursor += s->s_size; | |
2207 | } | |
2208 | free (buffer); | |
2209 | } | |
2210 | paddr += s->s_size; | |
2211 | } | |
2212 | } | |
2213 | } | |
2214 | ||
2215 | /* Coff file generation & utilities */ | |
2216 | ||
2217 | static void | |
2218 | coff_header_append (abfd, h) | |
2219 | bfd * abfd; | |
2220 | object_headers * h; | |
2221 | { | |
2222 | unsigned int i; | |
2223 | char buffer[1000]; | |
2224 | char buffero[1000]; | |
2225 | #ifdef COFF_LONG_SECTION_NAMES | |
2226 | unsigned long string_size = 4; | |
2227 | #endif | |
2228 | ||
2229 | bfd_seek (abfd, 0, 0); | |
2230 | ||
2231 | #ifndef OBJ_COFF_OMIT_OPTIONAL_HEADER | |
2232 | H_SET_MAGIC_NUMBER (h, COFF_MAGIC); | |
2233 | H_SET_VERSION_STAMP (h, 0); | |
2234 | H_SET_ENTRY_POINT (h, 0); | |
2235 | H_SET_TEXT_START (h, segment_info[SEG_E0].frchainP->frch_root->fr_address); | |
2236 | H_SET_DATA_START (h, segment_info[SEG_E1].frchainP->frch_root->fr_address); | |
2237 | H_SET_SIZEOF_OPTIONAL_HEADER (h, bfd_coff_swap_aouthdr_out(abfd, &h->aouthdr, | |
2238 | buffero)); | |
2239 | #else /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */ | |
2240 | H_SET_SIZEOF_OPTIONAL_HEADER (h, 0); | |
2241 | #endif /* defined (OBJ_COFF_OMIT_OPTIONAL_HEADER) */ | |
2242 | ||
2243 | i = bfd_coff_swap_filehdr_out (abfd, &h->filehdr, buffer); | |
2244 | ||
2245 | bfd_write (buffer, i, 1, abfd); | |
2246 | bfd_write (buffero, H_GET_SIZEOF_OPTIONAL_HEADER (h), 1, abfd); | |
2247 | ||
2248 | for (i = SEG_E0; i < SEG_LAST; i++) | |
2249 | { | |
2250 | if (segment_info[i].scnhdr.s_name[0]) | |
2251 | { | |
2252 | unsigned int size; | |
2253 | ||
2254 | #ifdef COFF_LONG_SECTION_NAMES | |
2255 | /* Support long section names as found in PE. This code | |
2256 | must coordinate with that in write_object_file and | |
2257 | w_strings. */ | |
2258 | if (strlen (segment_info[i].name) > SCNNMLEN) | |
2259 | { | |
2260 | memset (segment_info[i].scnhdr.s_name, 0, SCNNMLEN); | |
2261 | sprintf (segment_info[i].scnhdr.s_name, "/%lu", string_size); | |
2262 | string_size += strlen (segment_info[i].name) + 1; | |
2263 | } | |
2264 | #endif | |
2265 | ||
2266 | size = bfd_coff_swap_scnhdr_out (abfd, | |
2267 | &(segment_info[i].scnhdr), | |
2268 | buffer); | |
2269 | if (size == 0) | |
2270 | as_bad (_("bfd_coff_swap_scnhdr_out failed")); | |
2271 | bfd_write (buffer, size, 1, abfd); | |
2272 | } | |
2273 | } | |
2274 | } | |
2275 | ||
252b5132 RH |
2276 | char * |
2277 | symbol_to_chars (abfd, where, symbolP) | |
2278 | bfd * abfd; | |
2279 | char *where; | |
2280 | symbolS * symbolP; | |
2281 | { | |
2282 | unsigned int numaux = symbolP->sy_symbol.ost_entry.n_numaux; | |
2283 | unsigned int i; | |
2284 | valueT val; | |
2285 | ||
2286 | /* Turn any symbols with register attributes into abs symbols */ | |
2287 | if (S_GET_SEGMENT (symbolP) == reg_section) | |
2288 | { | |
2289 | S_SET_SEGMENT (symbolP, absolute_section); | |
2290 | } | |
2291 | /* At the same time, relocate all symbols to their output value */ | |
2292 | ||
2293 | #ifndef TE_PE | |
2294 | val = (segment_info[S_GET_SEGMENT (symbolP)].scnhdr.s_paddr | |
2295 | + S_GET_VALUE (symbolP)); | |
2296 | #else | |
2297 | val = S_GET_VALUE (symbolP); | |
2298 | #endif | |
2299 | ||
2300 | S_SET_VALUE (symbolP, val); | |
2301 | ||
2302 | symbolP->sy_symbol.ost_entry.n_value = val; | |
2303 | ||
2304 | where += bfd_coff_swap_sym_out (abfd, &symbolP->sy_symbol.ost_entry, | |
2305 | where); | |
2306 | ||
2307 | for (i = 0; i < numaux; i++) | |
2308 | { | |
2309 | where += bfd_coff_swap_aux_out (abfd, | |
2310 | &symbolP->sy_symbol.ost_auxent[i], | |
2311 | S_GET_DATA_TYPE (symbolP), | |
2312 | S_GET_STORAGE_CLASS (symbolP), | |
2313 | i, numaux, where); | |
2314 | } | |
2315 | return where; | |
2316 | ||
2317 | } | |
2318 | ||
2319 | void | |
2320 | coff_obj_symbol_new_hook (symbolP) | |
2321 | symbolS *symbolP; | |
2322 | { | |
2323 | char underscore = 0; /* Symbol has leading _ */ | |
2324 | ||
2325 | /* Effective symbol */ | |
dcd619be | 2326 | /* Store the pointer in the offset. */ |
252b5132 RH |
2327 | S_SET_ZEROES (symbolP, 0L); |
2328 | S_SET_DATA_TYPE (symbolP, T_NULL); | |
2329 | S_SET_STORAGE_CLASS (symbolP, 0); | |
2330 | S_SET_NUMBER_AUXILIARY (symbolP, 0); | |
2331 | /* Additional information */ | |
2332 | symbolP->sy_symbol.ost_flags = 0; | |
2333 | /* Auxiliary entries */ | |
2334 | memset ((char *) &symbolP->sy_symbol.ost_auxent[0], 0, AUXESZ); | |
2335 | ||
2336 | if (S_IS_STRING (symbolP)) | |
2337 | SF_SET_STRING (symbolP); | |
2338 | if (!underscore && S_IS_LOCAL (symbolP)) | |
2339 | SF_SET_LOCAL (symbolP); | |
2340 | } | |
2341 | ||
2342 | /* | |
2343 | * Handle .ln directives. | |
2344 | */ | |
2345 | ||
2346 | static void | |
2347 | obj_coff_ln (appline) | |
2348 | int appline; | |
2349 | { | |
2350 | int l; | |
2351 | ||
2352 | if (! appline && def_symbol_in_progress != NULL) | |
2353 | { | |
2354 | as_warn (_(".ln pseudo-op inside .def/.endef: ignored.")); | |
2355 | demand_empty_rest_of_line (); | |
2356 | return; | |
2357 | } /* wrong context */ | |
2358 | ||
2359 | l = get_absolute_expression (); | |
2360 | c_line_new (0, frag_now_fix (), l, frag_now); | |
2361 | ||
2362 | if (appline) | |
2363 | new_logical_line ((char *) NULL, l - 1); | |
2364 | ||
2365 | #ifndef NO_LISTING | |
2366 | { | |
2367 | extern int listing; | |
2368 | ||
2369 | if (listing) | |
2370 | { | |
2371 | if (! appline) | |
2372 | l += line_base - 1; | |
2373 | listing_source_line ((unsigned int) l); | |
2374 | } | |
2375 | ||
2376 | } | |
2377 | #endif | |
2378 | demand_empty_rest_of_line (); | |
2379 | } | |
2380 | ||
2381 | /* | |
2382 | * def() | |
2383 | * | |
2384 | * Handle .def directives. | |
2385 | * | |
2386 | * One might ask : why can't we symbol_new if the symbol does not | |
2387 | * already exist and fill it with debug information. Because of | |
2388 | * the C_EFCN special symbol. It would clobber the value of the | |
2389 | * function symbol before we have a chance to notice that it is | |
2390 | * a C_EFCN. And a second reason is that the code is more clear this | |
2391 | * way. (at least I think it is :-). | |
2392 | * | |
2393 | */ | |
2394 | ||
2395 | #define SKIP_SEMI_COLON() while (*input_line_pointer++ != ';') | |
2396 | #define SKIP_WHITESPACES() while (*input_line_pointer == ' ' || \ | |
2397 | *input_line_pointer == '\t') \ | |
2398 | input_line_pointer++; | |
2399 | ||
2400 | static void | |
2401 | obj_coff_def (what) | |
a04b544b | 2402 | int what ATTRIBUTE_UNUSED; |
252b5132 RH |
2403 | { |
2404 | char name_end; /* Char after the end of name */ | |
2405 | char *symbol_name; /* Name of the debug symbol */ | |
2406 | char *symbol_name_copy; /* Temporary copy of the name */ | |
2407 | unsigned int symbol_name_length; | |
2408 | ||
2409 | if (def_symbol_in_progress != NULL) | |
2410 | { | |
2411 | as_warn (_(".def pseudo-op used inside of .def/.endef: ignored.")); | |
2412 | demand_empty_rest_of_line (); | |
2413 | return; | |
2414 | } /* if not inside .def/.endef */ | |
2415 | ||
2416 | SKIP_WHITESPACES (); | |
2417 | ||
2418 | def_symbol_in_progress = (symbolS *) obstack_alloc (¬es, sizeof (*def_symbol_in_progress)); | |
2419 | memset (def_symbol_in_progress, 0, sizeof (*def_symbol_in_progress)); | |
2420 | ||
2421 | symbol_name = input_line_pointer; | |
2422 | name_end = get_symbol_end (); | |
2423 | symbol_name_length = strlen (symbol_name); | |
2424 | symbol_name_copy = xmalloc (symbol_name_length + 1); | |
2425 | strcpy (symbol_name_copy, symbol_name); | |
2426 | #ifdef tc_canonicalize_symbol_name | |
2427 | symbol_name_copy = tc_canonicalize_symbol_name (symbol_name_copy); | |
2428 | #endif | |
2429 | ||
2430 | /* Initialize the new symbol */ | |
2431 | #ifdef STRIP_UNDERSCORE | |
2432 | S_SET_NAME (def_symbol_in_progress, (*symbol_name_copy == '_' | |
2433 | ? symbol_name_copy + 1 | |
2434 | : symbol_name_copy)); | |
2435 | #else /* STRIP_UNDERSCORE */ | |
2436 | S_SET_NAME (def_symbol_in_progress, symbol_name_copy); | |
2437 | #endif /* STRIP_UNDERSCORE */ | |
2438 | /* free(symbol_name_copy); */ | |
2439 | def_symbol_in_progress->sy_name_offset = (unsigned long) ~0; | |
2440 | def_symbol_in_progress->sy_number = ~0; | |
2441 | def_symbol_in_progress->sy_frag = &zero_address_frag; | |
2442 | S_SET_VALUE (def_symbol_in_progress, 0); | |
2443 | ||
2444 | if (S_IS_STRING (def_symbol_in_progress)) | |
2445 | SF_SET_STRING (def_symbol_in_progress); | |
2446 | ||
2447 | *input_line_pointer = name_end; | |
2448 | ||
2449 | demand_empty_rest_of_line (); | |
2450 | } | |
2451 | ||
2452 | unsigned int dim_index; | |
2453 | ||
252b5132 RH |
2454 | static void |
2455 | obj_coff_endef (ignore) | |
a04b544b | 2456 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
2457 | { |
2458 | symbolS *symbolP = 0; | |
2459 | /* DIM BUG FIX sac@cygnus.com */ | |
2460 | dim_index = 0; | |
2461 | if (def_symbol_in_progress == NULL) | |
2462 | { | |
2463 | as_warn (_(".endef pseudo-op used outside of .def/.endef: ignored.")); | |
2464 | demand_empty_rest_of_line (); | |
2465 | return; | |
2466 | } /* if not inside .def/.endef */ | |
2467 | ||
dcd619be | 2468 | /* Set the section number according to storage class. */ |
252b5132 RH |
2469 | switch (S_GET_STORAGE_CLASS (def_symbol_in_progress)) |
2470 | { | |
2471 | case C_STRTAG: | |
2472 | case C_ENTAG: | |
2473 | case C_UNTAG: | |
2474 | SF_SET_TAG (def_symbol_in_progress); | |
2475 | /* intentional fallthrough */ | |
2476 | case C_FILE: | |
2477 | case C_TPDEF: | |
2478 | SF_SET_DEBUG (def_symbol_in_progress); | |
2479 | S_SET_SEGMENT (def_symbol_in_progress, SEG_DEBUG); | |
2480 | break; | |
2481 | ||
2482 | case C_EFCN: | |
dcd619be | 2483 | SF_SET_LOCAL (def_symbol_in_progress); /* Do not emit this symbol. */ |
252b5132 RH |
2484 | /* intentional fallthrough */ |
2485 | case C_BLOCK: | |
2486 | SF_SET_PROCESS (def_symbol_in_progress); /* Will need processing before writing */ | |
2487 | /* intentional fallthrough */ | |
2488 | case C_FCN: | |
2489 | S_SET_SEGMENT (def_symbol_in_progress, SEG_E0); | |
2490 | ||
2491 | if (strcmp (S_GET_NAME (def_symbol_in_progress), ".bf") == 0) | |
2492 | { /* .bf */ | |
2493 | if (function_lineoff < 0) | |
2494 | { | |
2495 | fprintf (stderr, _("`.bf' symbol without preceding function\n")); | |
2496 | } /* missing function symbol */ | |
2497 | SA_GET_SYM_LNNOPTR (last_line_symbol) = function_lineoff; | |
2498 | ||
2499 | SF_SET_PROCESS (last_line_symbol); | |
2500 | SF_SET_ADJ_LNNOPTR (last_line_symbol); | |
2501 | SF_SET_PROCESS (def_symbol_in_progress); | |
2502 | function_lineoff = -1; | |
2503 | } | |
dcd619be | 2504 | /* Value is always set to . */ |
252b5132 RH |
2505 | def_symbol_in_progress->sy_frag = frag_now; |
2506 | S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ()); | |
2507 | break; | |
2508 | ||
2509 | #ifdef C_AUTOARG | |
2510 | case C_AUTOARG: | |
2511 | #endif /* C_AUTOARG */ | |
2512 | case C_AUTO: | |
2513 | case C_REG: | |
2514 | case C_MOS: | |
2515 | case C_MOE: | |
2516 | case C_MOU: | |
2517 | case C_ARG: | |
2518 | case C_REGPARM: | |
2519 | case C_FIELD: | |
2520 | case C_EOS: | |
2521 | SF_SET_DEBUG (def_symbol_in_progress); | |
2522 | S_SET_SEGMENT (def_symbol_in_progress, absolute_section); | |
2523 | break; | |
2524 | ||
2525 | case C_EXT: | |
2526 | case C_WEAKEXT: | |
2527 | #ifdef TE_PE | |
2528 | case C_NT_WEAK: | |
2529 | #endif | |
2530 | case C_STAT: | |
2531 | case C_LABEL: | |
2532 | /* Valid but set somewhere else (s_comm, s_lcomm, colon) */ | |
2533 | break; | |
2534 | ||
2535 | case C_USTATIC: | |
2536 | case C_EXTDEF: | |
2537 | case C_ULABEL: | |
2538 | as_warn (_("unexpected storage class %d"), S_GET_STORAGE_CLASS (def_symbol_in_progress)); | |
2539 | break; | |
2540 | } /* switch on storage class */ | |
2541 | ||
2542 | /* Now that we have built a debug symbol, try to find if we should | |
2543 | merge with an existing symbol or not. If a symbol is C_EFCN or | |
2544 | absolute_section or untagged SEG_DEBUG it never merges. We also | |
2545 | don't merge labels, which are in a different namespace, nor | |
2546 | symbols which have not yet been defined since they are typically | |
2547 | unique, nor do we merge tags with non-tags. */ | |
2548 | ||
2549 | /* Two cases for functions. Either debug followed by definition or | |
2550 | definition followed by debug. For definition first, we will | |
2551 | merge the debug symbol into the definition. For debug first, the | |
2552 | lineno entry MUST point to the definition function or else it | |
2553 | will point off into space when crawl_symbols() merges the debug | |
2554 | symbol into the real symbol. Therefor, let's presume the debug | |
dcd619be | 2555 | symbol is a real function reference. */ |
252b5132 RH |
2556 | |
2557 | /* FIXME-SOON If for some reason the definition label/symbol is | |
2558 | never seen, this will probably leave an undefined symbol at link | |
dcd619be | 2559 | time. */ |
252b5132 RH |
2560 | |
2561 | if (S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_EFCN | |
2562 | || S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_LABEL | |
2563 | || (S_GET_SEGMENT (def_symbol_in_progress) == SEG_DEBUG | |
2564 | && !SF_GET_TAG (def_symbol_in_progress)) | |
2565 | || S_GET_SEGMENT (def_symbol_in_progress) == absolute_section | |
2566 | || def_symbol_in_progress->sy_value.X_op != O_constant | |
2567 | || (symbolP = symbol_find_base (S_GET_NAME (def_symbol_in_progress), DO_NOT_STRIP)) == NULL | |
2568 | || (SF_GET_TAG (def_symbol_in_progress) != SF_GET_TAG (symbolP))) | |
2569 | { | |
2570 | symbol_append (def_symbol_in_progress, symbol_lastP, &symbol_rootP, | |
2571 | &symbol_lastP); | |
2572 | } | |
2573 | else | |
2574 | { | |
2575 | /* This symbol already exists, merge the newly created symbol | |
2576 | into the old one. This is not mandatory. The linker can | |
2577 | handle duplicate symbols correctly. But I guess that it save | |
2578 | a *lot* of space if the assembly file defines a lot of | |
2579 | symbols. [loic] */ | |
2580 | ||
2581 | /* The debug entry (def_symbol_in_progress) is merged into the | |
2582 | previous definition. */ | |
2583 | ||
2584 | c_symbol_merge (def_symbol_in_progress, symbolP); | |
dcd619be | 2585 | /* FIXME-SOON Should *def_symbol_in_progress be free'd? xoxorich. */ |
252b5132 RH |
2586 | def_symbol_in_progress = symbolP; |
2587 | ||
2588 | if (SF_GET_FUNCTION (def_symbol_in_progress) | |
2589 | || SF_GET_TAG (def_symbol_in_progress) | |
2590 | || S_GET_STORAGE_CLASS (def_symbol_in_progress) == C_STAT) | |
2591 | { | |
2592 | /* For functions, and tags, and static symbols, the symbol | |
2593 | *must* be where the debug symbol appears. Move the | |
dcd619be | 2594 | existing symbol to the current place. */ |
252b5132 RH |
2595 | /* If it already is at the end of the symbol list, do nothing */ |
2596 | if (def_symbol_in_progress != symbol_lastP) | |
2597 | { | |
2598 | symbol_remove (def_symbol_in_progress, &symbol_rootP, | |
2599 | &symbol_lastP); | |
2600 | symbol_append (def_symbol_in_progress, symbol_lastP, | |
2601 | &symbol_rootP, &symbol_lastP); | |
2602 | } /* if not already in place */ | |
2603 | } /* if function */ | |
2604 | } /* normal or mergable */ | |
2605 | ||
2606 | if (SF_GET_TAG (def_symbol_in_progress)) | |
2607 | { | |
2608 | symbolS *oldtag; | |
2609 | ||
2610 | oldtag = symbol_find_base (S_GET_NAME (def_symbol_in_progress), | |
2611 | DO_NOT_STRIP); | |
2612 | if (oldtag == NULL || ! SF_GET_TAG (oldtag)) | |
2613 | tag_insert (S_GET_NAME (def_symbol_in_progress), | |
2614 | def_symbol_in_progress); | |
2615 | } | |
2616 | ||
2617 | if (SF_GET_FUNCTION (def_symbol_in_progress)) | |
2618 | { | |
2619 | know (sizeof (def_symbol_in_progress) <= sizeof (long)); | |
2620 | function_lineoff | |
2621 | = c_line_new (def_symbol_in_progress, 0, 0, &zero_address_frag); | |
2622 | ||
2623 | SF_SET_PROCESS (def_symbol_in_progress); | |
2624 | ||
2625 | if (symbolP == NULL) | |
2626 | { | |
2627 | /* That is, if this is the first time we've seen the | |
dcd619be | 2628 | function... */ |
252b5132 RH |
2629 | symbol_table_insert (def_symbol_in_progress); |
2630 | } /* definition follows debug */ | |
2631 | } /* Create the line number entry pointing to the function being defined */ | |
2632 | ||
2633 | def_symbol_in_progress = NULL; | |
2634 | demand_empty_rest_of_line (); | |
2635 | } | |
2636 | ||
2637 | static void | |
2638 | obj_coff_dim (ignore) | |
a04b544b | 2639 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
2640 | { |
2641 | int dim_index; | |
2642 | ||
2643 | if (def_symbol_in_progress == NULL) | |
2644 | { | |
2645 | as_warn (_(".dim pseudo-op used outside of .def/.endef: ignored.")); | |
2646 | demand_empty_rest_of_line (); | |
2647 | return; | |
2648 | } /* if not inside .def/.endef */ | |
2649 | ||
2650 | S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1); | |
2651 | ||
2652 | for (dim_index = 0; dim_index < DIMNUM; dim_index++) | |
2653 | { | |
2654 | SKIP_WHITESPACES (); | |
2655 | SA_SET_SYM_DIMEN (def_symbol_in_progress, dim_index, | |
2656 | get_absolute_expression ()); | |
2657 | ||
2658 | switch (*input_line_pointer) | |
2659 | { | |
2660 | case ',': | |
2661 | input_line_pointer++; | |
2662 | break; | |
2663 | ||
2664 | default: | |
2665 | as_warn (_("badly formed .dim directive ignored")); | |
2666 | /* intentional fallthrough */ | |
2667 | case '\n': | |
2668 | case ';': | |
2669 | dim_index = DIMNUM; | |
2670 | break; | |
2671 | } | |
2672 | } | |
2673 | ||
2674 | demand_empty_rest_of_line (); | |
2675 | } | |
2676 | ||
2677 | static void | |
2678 | obj_coff_line (ignore) | |
a04b544b | 2679 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
2680 | { |
2681 | int this_base; | |
2682 | const char *name; | |
2683 | ||
2684 | if (def_symbol_in_progress == NULL) | |
2685 | { | |
2686 | obj_coff_ln (0); | |
2687 | return; | |
2688 | } | |
2689 | ||
2690 | name = S_GET_NAME (def_symbol_in_progress); | |
2691 | this_base = get_absolute_expression (); | |
2692 | ||
2693 | /* Only .bf symbols indicate the use of a new base line number; the | |
2694 | line numbers associated with .ef, .bb, .eb are relative to the | |
2695 | start of the containing function. */ | |
2696 | if (!strcmp (".bf", name)) | |
2697 | { | |
2698 | #if 0 /* XXX Can we ever have line numbers going backwards? */ | |
2699 | if (this_base > line_base) | |
2700 | #endif | |
2701 | { | |
2702 | line_base = this_base; | |
2703 | } | |
2704 | ||
2705 | #ifndef NO_LISTING | |
2706 | { | |
2707 | extern int listing; | |
2708 | if (listing) | |
2709 | { | |
2710 | listing_source_line ((unsigned int) line_base); | |
2711 | } | |
2712 | } | |
2713 | #endif | |
2714 | } | |
2715 | ||
2716 | S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1); | |
2717 | SA_SET_SYM_LNNO (def_symbol_in_progress, this_base); | |
2718 | ||
2719 | demand_empty_rest_of_line (); | |
2720 | } | |
2721 | ||
2722 | static void | |
2723 | obj_coff_size (ignore) | |
a04b544b | 2724 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
2725 | { |
2726 | if (def_symbol_in_progress == NULL) | |
2727 | { | |
2728 | as_warn (_(".size pseudo-op used outside of .def/.endef ignored.")); | |
2729 | demand_empty_rest_of_line (); | |
2730 | return; | |
2731 | } /* if not inside .def/.endef */ | |
2732 | ||
2733 | S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1); | |
2734 | SA_SET_SYM_SIZE (def_symbol_in_progress, get_absolute_expression ()); | |
2735 | demand_empty_rest_of_line (); | |
2736 | } | |
2737 | ||
2738 | static void | |
2739 | obj_coff_scl (ignore) | |
a04b544b | 2740 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
2741 | { |
2742 | if (def_symbol_in_progress == NULL) | |
2743 | { | |
2744 | as_warn (_(".scl pseudo-op used outside of .def/.endef ignored.")); | |
2745 | demand_empty_rest_of_line (); | |
2746 | return; | |
2747 | } /* if not inside .def/.endef */ | |
2748 | ||
2749 | S_SET_STORAGE_CLASS (def_symbol_in_progress, get_absolute_expression ()); | |
2750 | demand_empty_rest_of_line (); | |
2751 | } | |
2752 | ||
2753 | static void | |
2754 | obj_coff_tag (ignore) | |
a04b544b | 2755 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
2756 | { |
2757 | char *symbol_name; | |
2758 | char name_end; | |
2759 | ||
2760 | if (def_symbol_in_progress == NULL) | |
2761 | { | |
2762 | as_warn (_(".tag pseudo-op used outside of .def/.endef ignored.")); | |
2763 | demand_empty_rest_of_line (); | |
2764 | return; | |
2765 | } | |
2766 | ||
2767 | S_SET_NUMBER_AUXILIARY (def_symbol_in_progress, 1); | |
2768 | symbol_name = input_line_pointer; | |
2769 | name_end = get_symbol_end (); | |
2770 | #ifdef tc_canonicalize_symbol_name | |
2771 | symbol_name = tc_canonicalize_symbol_name (symbol_name); | |
2772 | #endif | |
2773 | ||
2774 | /* Assume that the symbol referred to by .tag is always defined. | |
dcd619be | 2775 | This was a bad assumption. I've added find_or_make. xoxorich. */ |
252b5132 RH |
2776 | SA_SET_SYM_TAGNDX (def_symbol_in_progress, |
2777 | (long) tag_find_or_make (symbol_name)); | |
2778 | if (SA_GET_SYM_TAGNDX (def_symbol_in_progress) == 0L) | |
2779 | { | |
2780 | as_warn (_("tag not found for .tag %s"), symbol_name); | |
2781 | } /* not defined */ | |
2782 | ||
2783 | SF_SET_TAGGED (def_symbol_in_progress); | |
2784 | *input_line_pointer = name_end; | |
2785 | ||
2786 | demand_empty_rest_of_line (); | |
2787 | } | |
2788 | ||
2789 | static void | |
2790 | obj_coff_type (ignore) | |
a04b544b | 2791 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
2792 | { |
2793 | if (def_symbol_in_progress == NULL) | |
2794 | { | |
2795 | as_warn (_(".type pseudo-op used outside of .def/.endef ignored.")); | |
2796 | demand_empty_rest_of_line (); | |
2797 | return; | |
2798 | } /* if not inside .def/.endef */ | |
2799 | ||
2800 | S_SET_DATA_TYPE (def_symbol_in_progress, get_absolute_expression ()); | |
2801 | ||
2802 | if (ISFCN (S_GET_DATA_TYPE (def_symbol_in_progress)) && | |
2803 | S_GET_STORAGE_CLASS (def_symbol_in_progress) != C_TPDEF) | |
2804 | { | |
2805 | SF_SET_FUNCTION (def_symbol_in_progress); | |
2806 | } /* is a function */ | |
2807 | ||
2808 | demand_empty_rest_of_line (); | |
2809 | } | |
2810 | ||
2811 | static void | |
2812 | obj_coff_val (ignore) | |
a04b544b | 2813 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
2814 | { |
2815 | if (def_symbol_in_progress == NULL) | |
2816 | { | |
2817 | as_warn (_(".val pseudo-op used outside of .def/.endef ignored.")); | |
2818 | demand_empty_rest_of_line (); | |
2819 | return; | |
2820 | } /* if not inside .def/.endef */ | |
2821 | ||
2822 | if (is_name_beginner (*input_line_pointer)) | |
2823 | { | |
2824 | char *symbol_name = input_line_pointer; | |
2825 | char name_end = get_symbol_end (); | |
2826 | ||
2827 | #ifdef tc_canonicalize_symbol_name | |
2828 | symbol_name = tc_canonicalize_symbol_name (symbol_name); | |
2829 | #endif | |
2830 | ||
2831 | if (!strcmp (symbol_name, ".")) | |
2832 | { | |
2833 | def_symbol_in_progress->sy_frag = frag_now; | |
2834 | S_SET_VALUE (def_symbol_in_progress, (valueT) frag_now_fix ()); | |
2835 | /* If the .val is != from the .def (e.g. statics) */ | |
2836 | } | |
2837 | else if (strcmp (S_GET_NAME (def_symbol_in_progress), symbol_name)) | |
2838 | { | |
2839 | def_symbol_in_progress->sy_value.X_op = O_symbol; | |
2840 | def_symbol_in_progress->sy_value.X_add_symbol = | |
2841 | symbol_find_or_make (symbol_name); | |
2842 | def_symbol_in_progress->sy_value.X_op_symbol = NULL; | |
2843 | def_symbol_in_progress->sy_value.X_add_number = 0; | |
2844 | ||
2845 | /* If the segment is undefined when the forward reference is | |
2846 | resolved, then copy the segment id from the forward | |
2847 | symbol. */ | |
2848 | SF_SET_GET_SEGMENT (def_symbol_in_progress); | |
2849 | ||
0561a208 ILT |
2850 | /* FIXME: gcc can generate address expressions here in |
2851 | unusual cases (search for "obscure" in sdbout.c). We | |
2852 | just ignore the offset here, thus generating incorrect | |
2853 | debugging information. We ignore the rest of the line | |
2854 | just below. */ | |
252b5132 RH |
2855 | } |
2856 | /* Otherwise, it is the name of a non debug symbol and | |
dcd619be | 2857 | its value will be calculated later. */ |
252b5132 RH |
2858 | *input_line_pointer = name_end; |
2859 | ||
2860 | /* FIXME: this is to avoid an error message in the | |
2861 | FIXME case mentioned just above. */ | |
2862 | while (! is_end_of_line[(unsigned char) *input_line_pointer]) | |
2863 | ++input_line_pointer; | |
2864 | } | |
2865 | else | |
2866 | { | |
2867 | S_SET_VALUE (def_symbol_in_progress, | |
2868 | (valueT) get_absolute_expression ()); | |
2869 | } /* if symbol based */ | |
2870 | ||
2871 | demand_empty_rest_of_line (); | |
2872 | } | |
2873 | ||
2874 | #ifdef TE_PE | |
2875 | ||
2876 | /* Handle the .linkonce pseudo-op. This is parsed by s_linkonce in | |
2877 | read.c, which then calls this object file format specific routine. */ | |
2878 | ||
2879 | void | |
2880 | obj_coff_pe_handle_link_once (type) | |
2881 | enum linkonce_type type; | |
2882 | { | |
2883 | seg_info (now_seg)->scnhdr.s_flags |= IMAGE_SCN_LNK_COMDAT; | |
2884 | ||
2885 | /* We store the type in the seg_info structure, and use it to set up | |
2886 | the auxiliary entry for the section symbol in c_section_symbol. */ | |
2887 | seg_info (now_seg)->linkonce = type; | |
2888 | } | |
2889 | ||
2890 | #endif /* TE_PE */ | |
2891 | ||
2892 | void | |
2893 | coff_obj_read_begin_hook () | |
2894 | { | |
dcd619be | 2895 | /* These had better be the same. Usually 18 bytes. */ |
252b5132 RH |
2896 | #ifndef BFD_HEADERS |
2897 | know (sizeof (SYMENT) == sizeof (AUXENT)); | |
2898 | know (SYMESZ == AUXESZ); | |
2899 | #endif | |
2900 | tag_init (); | |
2901 | } | |
2902 | ||
2903 | /* This function runs through the symbol table and puts all the | |
2904 | externals onto another chain */ | |
2905 | ||
2906 | /* The chain of globals. */ | |
2907 | symbolS *symbol_globalP; | |
2908 | symbolS *symbol_global_lastP; | |
2909 | ||
2910 | /* The chain of externals */ | |
2911 | symbolS *symbol_externP; | |
2912 | symbolS *symbol_extern_lastP; | |
2913 | ||
2914 | stack *block_stack; | |
2915 | symbolS *last_functionP; | |
2916 | static symbolS *last_bfP; | |
2917 | symbolS *last_tagP; | |
2918 | ||
2919 | static unsigned int | |
2920 | yank_symbols () | |
2921 | { | |
2922 | symbolS *symbolP; | |
2923 | unsigned int symbol_number = 0; | |
2924 | unsigned int last_file_symno = 0; | |
2925 | ||
2926 | struct filename_list *filename_list_scan = filename_list_head; | |
2927 | ||
2928 | for (symbolP = symbol_rootP; | |
2929 | symbolP; | |
2930 | symbolP = symbolP ? symbol_next (symbolP) : symbol_rootP) | |
2931 | { | |
2932 | if (symbolP->sy_mri_common) | |
2933 | { | |
2934 | if (S_GET_STORAGE_CLASS (symbolP) == C_EXT | |
2935 | #ifdef TE_PE | |
2936 | || S_GET_STORAGE_CLASS (symbolP) == C_NT_WEAK | |
2937 | #endif | |
2938 | || S_GET_STORAGE_CLASS (symbolP) == C_WEAKEXT) | |
2939 | as_bad (_("%s: global symbols not supported in common sections"), | |
2940 | S_GET_NAME (symbolP)); | |
2941 | symbol_remove (symbolP, &symbol_rootP, &symbol_lastP); | |
2942 | continue; | |
2943 | } | |
2944 | ||
2945 | if (!SF_GET_DEBUG (symbolP)) | |
2946 | { | |
2947 | /* Debug symbols do not need all this rubbish */ | |
2948 | symbolS *real_symbolP; | |
2949 | ||
dcd619be | 2950 | /* L* and C_EFCN symbols never merge. */ |
252b5132 RH |
2951 | if (!SF_GET_LOCAL (symbolP) |
2952 | && !SF_GET_STATICS (symbolP) | |
2953 | && S_GET_STORAGE_CLASS (symbolP) != C_LABEL | |
2954 | && symbolP->sy_value.X_op == O_constant | |
2955 | && (real_symbolP = symbol_find_base (S_GET_NAME (symbolP), DO_NOT_STRIP)) | |
2956 | && real_symbolP != symbolP) | |
2957 | { | |
2958 | /* FIXME-SOON: where do dups come from? | |
dcd619be | 2959 | Maybe tag references before definitions? xoxorich. */ |
252b5132 RH |
2960 | /* Move the debug data from the debug symbol to the |
2961 | real symbol. Do NOT do the oposite (i.e. move from | |
2962 | real symbol to debug symbol and remove real symbol from the | |
2963 | list.) Because some pointers refer to the real symbol | |
dcd619be | 2964 | whereas no pointers refer to the debug symbol. */ |
252b5132 RH |
2965 | c_symbol_merge (symbolP, real_symbolP); |
2966 | /* Replace the current symbol by the real one */ | |
2967 | /* The symbols will never be the last or the first | |
2968 | because : 1st symbol is .file and 3 last symbols are | |
2969 | .text, .data, .bss */ | |
2970 | symbol_remove (real_symbolP, &symbol_rootP, &symbol_lastP); | |
2971 | symbol_insert (real_symbolP, symbolP, &symbol_rootP, &symbol_lastP); | |
2972 | symbol_remove (symbolP, &symbol_rootP, &symbol_lastP); | |
2973 | symbolP = real_symbolP; | |
2974 | } /* if not local but dup'd */ | |
2975 | ||
2976 | if (flag_readonly_data_in_text && (S_GET_SEGMENT (symbolP) == SEG_E1)) | |
2977 | { | |
2978 | S_SET_SEGMENT (symbolP, SEG_E0); | |
2979 | } /* push data into text */ | |
2980 | ||
6386f3a7 | 2981 | resolve_symbol_value (symbolP); |
252b5132 RH |
2982 | |
2983 | if (S_GET_STORAGE_CLASS (symbolP) == C_NULL) | |
2984 | { | |
2985 | if (!S_IS_DEFINED (symbolP) && !SF_GET_LOCAL (symbolP)) | |
2986 | { | |
2987 | S_SET_EXTERNAL (symbolP); | |
2988 | } | |
2989 | else if (S_GET_SEGMENT (symbolP) == SEG_E0) | |
2990 | { | |
2991 | S_SET_STORAGE_CLASS (symbolP, C_LABEL); | |
2992 | } | |
2993 | else | |
2994 | { | |
2995 | S_SET_STORAGE_CLASS (symbolP, C_STAT); | |
2996 | } | |
2997 | } | |
2998 | ||
2999 | /* Mainly to speed up if not -g */ | |
3000 | if (SF_GET_PROCESS (symbolP)) | |
3001 | { | |
dcd619be | 3002 | /* Handle the nested blocks auxiliary info. */ |
252b5132 RH |
3003 | if (S_GET_STORAGE_CLASS (symbolP) == C_BLOCK) |
3004 | { | |
3005 | if (!strcmp (S_GET_NAME (symbolP), ".bb")) | |
3006 | stack_push (block_stack, (char *) &symbolP); | |
3007 | else | |
3008 | { /* .eb */ | |
3009 | register symbolS *begin_symbolP; | |
3010 | begin_symbolP = *(symbolS **) stack_pop (block_stack); | |
3011 | if (begin_symbolP == (symbolS *) 0) | |
3012 | as_warn (_("mismatched .eb")); | |
3013 | else | |
3014 | SA_SET_SYM_ENDNDX (begin_symbolP, symbol_number + 2); | |
3015 | } | |
3016 | } | |
3017 | /* If we are able to identify the type of a function, and we | |
3018 | are out of a function (last_functionP == 0) then, the | |
3019 | function symbol will be associated with an auxiliary | |
dcd619be | 3020 | entry. */ |
252b5132 RH |
3021 | if (last_functionP == (symbolS *) 0 && |
3022 | SF_GET_FUNCTION (symbolP)) | |
3023 | { | |
3024 | last_functionP = symbolP; | |
3025 | ||
3026 | if (S_GET_NUMBER_AUXILIARY (symbolP) < 1) | |
3027 | { | |
3028 | S_SET_NUMBER_AUXILIARY (symbolP, 1); | |
3029 | } /* make it at least 1 */ | |
3030 | ||
dcd619be | 3031 | /* Clobber possible stale .dim information. */ |
252b5132 RH |
3032 | #if 0 |
3033 | /* Iffed out by steve - this fries the lnnoptr info too */ | |
3034 | bzero (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen, | |
3035 | sizeof (symbolP->sy_symbol.ost_auxent[0].x_sym.x_fcnary.x_ary.x_dimen)); | |
3036 | #endif | |
3037 | } | |
3038 | if (S_GET_STORAGE_CLASS (symbolP) == C_FCN) | |
3039 | { | |
3040 | if (strcmp (S_GET_NAME (symbolP), ".bf") == 0) | |
3041 | { | |
3042 | if (last_bfP != NULL) | |
3043 | SA_SET_SYM_ENDNDX (last_bfP, symbol_number); | |
3044 | last_bfP = symbolP; | |
3045 | } | |
3046 | } | |
3047 | else if (S_GET_STORAGE_CLASS (symbolP) == C_EFCN) | |
3048 | { | |
3049 | /* I don't even know if this is needed for sdb. But | |
3050 | the standard assembler generates it, so... */ | |
3051 | if (last_functionP == (symbolS *) 0) | |
3052 | as_fatal (_("C_EFCN symbol out of scope")); | |
3053 | SA_SET_SYM_FSIZE (last_functionP, | |
3054 | (long) (S_GET_VALUE (symbolP) - | |
3055 | S_GET_VALUE (last_functionP))); | |
3056 | SA_SET_SYM_ENDNDX (last_functionP, symbol_number); | |
3057 | last_functionP = (symbolS *) 0; | |
3058 | } | |
3059 | } | |
3060 | } | |
3061 | else if (SF_GET_TAG (symbolP)) | |
3062 | { | |
3063 | /* First descriptor of a structure must point to | |
dcd619be | 3064 | the first slot after the structure description. */ |
252b5132 RH |
3065 | last_tagP = symbolP; |
3066 | ||
3067 | } | |
3068 | else if (S_GET_STORAGE_CLASS (symbolP) == C_EOS) | |
3069 | { | |
3070 | /* +2 take in account the current symbol */ | |
3071 | SA_SET_SYM_ENDNDX (last_tagP, symbol_number + 2); | |
3072 | } | |
3073 | else if (S_GET_STORAGE_CLASS (symbolP) == C_FILE) | |
3074 | { | |
3075 | /* If the filename was too long to fit in the | |
3076 | auxent, put it in the string table */ | |
3077 | if (SA_GET_FILE_FNAME_ZEROS (symbolP) == 0 | |
3078 | && SA_GET_FILE_FNAME_OFFSET (symbolP) != 0) | |
3079 | { | |
3080 | SA_SET_FILE_FNAME_OFFSET (symbolP, string_byte_count); | |
3081 | string_byte_count += strlen (filename_list_scan->filename) + 1; | |
3082 | filename_list_scan = filename_list_scan->next; | |
3083 | } | |
3084 | if (S_GET_VALUE (symbolP)) | |
3085 | { | |
3086 | S_SET_VALUE (symbolP, last_file_symno); | |
3087 | last_file_symno = symbol_number; | |
3088 | } /* no one points at the first .file symbol */ | |
3089 | } /* if debug or tag or eos or file */ | |
3090 | ||
3091 | #ifdef tc_frob_coff_symbol | |
3092 | tc_frob_coff_symbol (symbolP); | |
3093 | #endif | |
3094 | ||
3095 | /* We must put the external symbols apart. The loader | |
3096 | does not bomb if we do not. But the references in | |
3097 | the endndx field for a .bb symbol are not corrected | |
3098 | if an external symbol is removed between .bb and .be. | |
3099 | I.e in the following case : | |
3100 | [20] .bb endndx = 22 | |
3101 | [21] foo external | |
3102 | [22] .be | |
3103 | ld will move the symbol 21 to the end of the list but | |
dcd619be | 3104 | endndx will still be 22 instead of 21. */ |
252b5132 RH |
3105 | |
3106 | if (SF_GET_LOCAL (symbolP)) | |
3107 | { | |
3108 | /* remove C_EFCN and LOCAL (L...) symbols */ | |
3109 | /* next pointer remains valid */ | |
3110 | symbol_remove (symbolP, &symbol_rootP, &symbol_lastP); | |
3111 | ||
3112 | } | |
3113 | else if (symbolP->sy_value.X_op == O_symbol | |
3114 | && (! S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))) | |
3115 | { | |
3116 | /* Skip symbols which were equated to undefined or common | |
3117 | symbols. */ | |
3118 | symbol_remove (symbolP, &symbol_rootP, &symbol_lastP); | |
3119 | } | |
3120 | else if (!S_IS_DEFINED (symbolP) | |
3121 | && !S_IS_DEBUG (symbolP) | |
3122 | && !SF_GET_STATICS (symbolP) | |
3123 | && (S_GET_STORAGE_CLASS (symbolP) == C_EXT | |
3124 | #ifdef TE_PE | |
3125 | || S_GET_STORAGE_CLASS (symbolP) == C_NT_WEAK | |
3126 | #endif | |
3127 | || S_GET_STORAGE_CLASS (symbolP) == C_WEAKEXT)) | |
3128 | { | |
3129 | /* if external, Remove from the list */ | |
3130 | symbolS *hold = symbol_previous (symbolP); | |
3131 | ||
3132 | symbol_remove (symbolP, &symbol_rootP, &symbol_lastP); | |
3133 | symbol_clear_list_pointers (symbolP); | |
3134 | symbol_append (symbolP, symbol_extern_lastP, &symbol_externP, &symbol_extern_lastP); | |
3135 | symbolP = hold; | |
3136 | } | |
3137 | else if (! S_IS_DEBUG (symbolP) | |
3138 | && ! SF_GET_STATICS (symbolP) | |
3139 | && ! SF_GET_FUNCTION (symbolP) | |
3140 | && (S_GET_STORAGE_CLASS (symbolP) == C_EXT | |
3141 | #ifdef TE_PE | |
3142 | || S_GET_STORAGE_CLASS (symbolP) == C_NT_WEAK | |
3143 | #endif | |
3144 | || S_GET_STORAGE_CLASS (symbolP) == C_NT_WEAK)) | |
3145 | { | |
3146 | symbolS *hold = symbol_previous (symbolP); | |
3147 | ||
3148 | /* The O'Reilly COFF book says that defined global symbols | |
3149 | come at the end of the symbol table, just before | |
3150 | undefined global symbols. */ | |
3151 | ||
3152 | symbol_remove (symbolP, &symbol_rootP, &symbol_lastP); | |
3153 | symbol_clear_list_pointers (symbolP); | |
3154 | symbol_append (symbolP, symbol_global_lastP, &symbol_globalP, | |
3155 | &symbol_global_lastP); | |
3156 | symbolP = hold; | |
3157 | } | |
3158 | else | |
3159 | { | |
3160 | if (SF_GET_STRING (symbolP)) | |
3161 | { | |
3162 | symbolP->sy_name_offset = string_byte_count; | |
3163 | string_byte_count += strlen (S_GET_NAME (symbolP)) + 1; | |
3164 | } | |
3165 | else | |
3166 | { | |
3167 | symbolP->sy_name_offset = 0; | |
3168 | } /* fix "long" names */ | |
3169 | ||
3170 | symbolP->sy_number = symbol_number; | |
3171 | symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP); | |
3172 | } /* if local symbol */ | |
3173 | } /* traverse the symbol list */ | |
3174 | return symbol_number; | |
3175 | ||
3176 | } | |
3177 | ||
252b5132 RH |
3178 | static unsigned int |
3179 | glue_symbols (head, tail) | |
3180 | symbolS **head; | |
3181 | symbolS **tail; | |
3182 | { | |
3183 | unsigned int symbol_number = 0; | |
3184 | ||
3185 | while (*head != NULL) | |
3186 | { | |
3187 | symbolS *tmp = *head; | |
3188 | ||
3189 | /* append */ | |
3190 | symbol_remove (tmp, head, tail); | |
3191 | symbol_append (tmp, symbol_lastP, &symbol_rootP, &symbol_lastP); | |
3192 | ||
3193 | /* and process */ | |
3194 | if (SF_GET_STRING (tmp)) | |
3195 | { | |
3196 | tmp->sy_name_offset = string_byte_count; | |
3197 | string_byte_count += strlen (S_GET_NAME (tmp)) + 1; | |
3198 | } | |
3199 | else | |
3200 | { | |
3201 | tmp->sy_name_offset = 0; | |
3202 | } /* fix "long" names */ | |
3203 | ||
3204 | tmp->sy_number = symbol_number; | |
3205 | symbol_number += 1 + S_GET_NUMBER_AUXILIARY (tmp); | |
3206 | } /* append the entire extern chain */ | |
3207 | ||
3208 | return symbol_number; | |
3209 | } | |
3210 | ||
3211 | static unsigned int | |
3212 | tie_tags () | |
3213 | { | |
3214 | unsigned int symbol_number = 0; | |
3215 | symbolS *symbolP; | |
3216 | ||
3217 | for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP)) | |
3218 | { | |
3219 | symbolP->sy_number = symbol_number; | |
3220 | ||
3221 | if (SF_GET_TAGGED (symbolP)) | |
3222 | { | |
3223 | SA_SET_SYM_TAGNDX | |
3224 | (symbolP, | |
3225 | ((symbolS *) SA_GET_SYM_TAGNDX (symbolP))->sy_number); | |
3226 | } | |
3227 | ||
3228 | symbol_number += 1 + S_GET_NUMBER_AUXILIARY (symbolP); | |
3229 | } | |
3230 | ||
3231 | return symbol_number; | |
3232 | } | |
3233 | ||
3234 | static void | |
3235 | crawl_symbols (h, abfd) | |
3236 | object_headers *h; | |
a04b544b | 3237 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
3238 | { |
3239 | unsigned int i; | |
3240 | ||
3241 | /* Initialize the stack used to keep track of the matching .bb .be */ | |
3242 | ||
3243 | block_stack = stack_init (512, sizeof (symbolS *)); | |
3244 | ||
3245 | /* The symbol list should be ordered according to the following sequence | |
3246 | * order : | |
3247 | * . .file symbol | |
3248 | * . debug entries for functions | |
3249 | * . fake symbols for the sections, including .text .data and .bss | |
3250 | * . defined symbols | |
3251 | * . undefined symbols | |
3252 | * But this is not mandatory. The only important point is to put the | |
3253 | * undefined symbols at the end of the list. | |
3254 | */ | |
3255 | ||
dcd619be | 3256 | /* Is there a .file symbol ? If not insert one at the beginning. */ |
252b5132 RH |
3257 | if (symbol_rootP == NULL |
3258 | || S_GET_STORAGE_CLASS (symbol_rootP) != C_FILE) | |
3259 | { | |
3260 | c_dot_file_symbol ("fake"); | |
3261 | } | |
3262 | ||
3263 | /* | |
3264 | * Build up static symbols for the sections, they are filled in later | |
3265 | */ | |
3266 | ||
252b5132 RH |
3267 | for (i = SEG_E0; i < SEG_LAST; i++) |
3268 | if (segment_info[i].scnhdr.s_name[0]) | |
3269 | segment_info[i].dot = c_section_symbol (segment_info[i].name, | |
3270 | i - SEG_E0 + 1); | |
3271 | ||
3272 | /* Take all the externals out and put them into another chain */ | |
3273 | H_SET_SYMBOL_TABLE_SIZE (h, yank_symbols ()); | |
3274 | /* Take the externals and glue them onto the end.*/ | |
3275 | H_SET_SYMBOL_TABLE_SIZE (h, | |
3276 | (H_GET_SYMBOL_COUNT (h) | |
3277 | + glue_symbols (&symbol_globalP, | |
3278 | &symbol_global_lastP) | |
3279 | + glue_symbols (&symbol_externP, | |
3280 | &symbol_extern_lastP))); | |
3281 | ||
3282 | H_SET_SYMBOL_TABLE_SIZE (h, tie_tags ()); | |
3283 | know (symbol_globalP == NULL); | |
3284 | know (symbol_global_lastP == NULL); | |
3285 | know (symbol_externP == NULL); | |
3286 | know (symbol_extern_lastP == NULL); | |
3287 | } | |
3288 | ||
3289 | /* | |
3290 | * Find strings by crawling along symbol table chain. | |
3291 | */ | |
3292 | ||
3293 | void | |
3294 | w_strings (where) | |
3295 | char *where; | |
3296 | { | |
3297 | symbolS *symbolP; | |
3298 | struct filename_list *filename_list_scan = filename_list_head; | |
3299 | ||
3300 | /* Gotta do md_ byte-ordering stuff for string_byte_count first - KWK */ | |
3301 | md_number_to_chars (where, (valueT) string_byte_count, 4); | |
3302 | where += 4; | |
3303 | ||
3304 | #ifdef COFF_LONG_SECTION_NAMES | |
3305 | /* Support long section names as found in PE. This code must | |
3306 | coordinate with that in coff_header_append and write_object_file. */ | |
3307 | { | |
3308 | unsigned int i; | |
3309 | ||
3310 | for (i = SEG_E0; i < SEG_LAST; i++) | |
3311 | { | |
3312 | if (segment_info[i].scnhdr.s_name[0] | |
3313 | && strlen (segment_info[i].name) > SCNNMLEN) | |
3314 | { | |
3315 | unsigned int size; | |
3316 | ||
3317 | size = strlen (segment_info[i].name) + 1; | |
3318 | memcpy (where, segment_info[i].name, size); | |
3319 | where += size; | |
3320 | } | |
3321 | } | |
3322 | } | |
3323 | #endif /* COFF_LONG_SECTION_NAMES */ | |
3324 | ||
3325 | for (symbolP = symbol_rootP; | |
3326 | symbolP; | |
3327 | symbolP = symbol_next (symbolP)) | |
3328 | { | |
3329 | unsigned int size; | |
3330 | ||
3331 | if (SF_GET_STRING (symbolP)) | |
3332 | { | |
3333 | size = strlen (S_GET_NAME (symbolP)) + 1; | |
3334 | memcpy (where, S_GET_NAME (symbolP), size); | |
3335 | where += size; | |
3336 | } | |
3337 | if (S_GET_STORAGE_CLASS (symbolP) == C_FILE | |
3338 | && SA_GET_FILE_FNAME_ZEROS (symbolP) == 0 | |
3339 | && SA_GET_FILE_FNAME_OFFSET (symbolP) != 0) | |
3340 | { | |
3341 | size = strlen (filename_list_scan->filename) + 1; | |
3342 | memcpy (where, filename_list_scan->filename, size); | |
3343 | filename_list_scan = filename_list_scan ->next; | |
3344 | where += size; | |
3345 | } | |
3346 | } | |
3347 | } | |
3348 | ||
3349 | static void | |
3350 | do_linenos_for (abfd, h, file_cursor) | |
3351 | bfd * abfd; | |
3352 | object_headers * h; | |
3353 | unsigned long *file_cursor; | |
3354 | { | |
3355 | unsigned int idx; | |
3356 | unsigned long start = *file_cursor; | |
3357 | ||
3358 | for (idx = SEG_E0; idx < SEG_LAST; idx++) | |
3359 | { | |
3360 | segment_info_type *s = segment_info + idx; | |
3361 | ||
252b5132 RH |
3362 | if (s->scnhdr.s_nlnno != 0) |
3363 | { | |
3364 | struct lineno_list *line_ptr; | |
3365 | ||
3366 | struct external_lineno *buffer = | |
3367 | (struct external_lineno *) xmalloc (s->scnhdr.s_nlnno * LINESZ); | |
3368 | ||
3369 | struct external_lineno *dst = buffer; | |
3370 | ||
3371 | /* Run through the table we've built and turn it into its external | |
3372 | form, take this chance to remove duplicates */ | |
3373 | ||
3374 | for (line_ptr = s->lineno_list_head; | |
3375 | line_ptr != (struct lineno_list *) NULL; | |
3376 | line_ptr = line_ptr->next) | |
3377 | { | |
252b5132 RH |
3378 | if (line_ptr->line.l_lnno == 0) |
3379 | { | |
ea3b9044 NC |
3380 | /* Turn a pointer to a symbol into the symbols' index, |
3381 | provided that it has been initialised. */ | |
3382 | if (line_ptr->line.l_addr.l_symndx) | |
3383 | line_ptr->line.l_addr.l_symndx = | |
3384 | ((symbolS *) line_ptr->line.l_addr.l_symndx)->sy_number; | |
252b5132 RH |
3385 | } |
3386 | else | |
3387 | { | |
3388 | line_ptr->line.l_addr.l_paddr += ((struct frag *) (line_ptr->frag))->fr_address; | |
3389 | } | |
3390 | ||
252b5132 RH |
3391 | (void) bfd_coff_swap_lineno_out (abfd, &(line_ptr->line), dst); |
3392 | dst++; | |
3393 | ||
3394 | } | |
3395 | ||
3396 | s->scnhdr.s_lnnoptr = *file_cursor; | |
3397 | ||
3398 | bfd_write (buffer, 1, s->scnhdr.s_nlnno * LINESZ, abfd); | |
3399 | free (buffer); | |
3400 | ||
3401 | *file_cursor += s->scnhdr.s_nlnno * LINESZ; | |
3402 | } | |
3403 | } | |
3404 | H_SET_LINENO_SIZE (h, *file_cursor - start); | |
3405 | } | |
3406 | ||
252b5132 RH |
3407 | /* Now we run through the list of frag chains in a segment and |
3408 | make all the subsegment frags appear at the end of the | |
3409 | list, as if the seg 0 was extra long */ | |
3410 | ||
3411 | static void | |
3412 | remove_subsegs () | |
3413 | { | |
3414 | unsigned int i; | |
3415 | ||
3416 | for (i = SEG_E0; i < SEG_UNKNOWN; i++) | |
3417 | { | |
3418 | frchainS *head = segment_info[i].frchainP; | |
3419 | fragS dummy; | |
3420 | fragS *prev_frag = &dummy; | |
3421 | ||
3422 | while (head && head->frch_seg == i) | |
3423 | { | |
3424 | prev_frag->fr_next = head->frch_root; | |
3425 | prev_frag = head->frch_last; | |
3426 | head = head->frch_next; | |
3427 | } | |
3428 | prev_frag->fr_next = 0; | |
3429 | } | |
3430 | } | |
3431 | ||
3432 | unsigned long machine; | |
3433 | int coff_flags; | |
3434 | extern void | |
3435 | write_object_file () | |
3436 | { | |
3437 | int i; | |
3438 | const char *name; | |
3439 | struct frchain *frchain_ptr; | |
3440 | ||
3441 | object_headers headers; | |
3442 | unsigned long file_cursor; | |
3443 | bfd *abfd; | |
3444 | unsigned int addr; | |
3445 | abfd = bfd_openw (out_file_name, TARGET_FORMAT); | |
3446 | ||
252b5132 RH |
3447 | if (abfd == 0) |
3448 | { | |
3449 | as_perror (_("FATAL: Can't create %s"), out_file_name); | |
3450 | exit (EXIT_FAILURE); | |
3451 | } | |
3452 | bfd_set_format (abfd, bfd_object); | |
3453 | bfd_set_arch_mach (abfd, BFD_ARCH, machine); | |
3454 | ||
3455 | string_byte_count = 4; | |
3456 | ||
3457 | for (frchain_ptr = frchain_root; | |
3458 | frchain_ptr != (struct frchain *) NULL; | |
3459 | frchain_ptr = frchain_ptr->frch_next) | |
3460 | { | |
3461 | /* Run through all the sub-segments and align them up. Also | |
3462 | close any open frags. We tack a .fill onto the end of the | |
3463 | frag chain so that any .align's size can be worked by looking | |
3464 | at the next frag. */ | |
3465 | ||
3466 | subseg_set (frchain_ptr->frch_seg, frchain_ptr->frch_subseg); | |
b9e57a38 | 3467 | |
252b5132 RH |
3468 | #ifndef SUB_SEGMENT_ALIGN |
3469 | #define SUB_SEGMENT_ALIGN(SEG) 1 | |
3470 | #endif | |
3471 | #ifdef md_do_align | |
3472 | md_do_align (SUB_SEGMENT_ALIGN (now_seg), (char *) NULL, 0, 0, | |
3473 | alignment_done); | |
3474 | #endif | |
0a9ef439 RH |
3475 | if (subseg_text_p (now_seg)) |
3476 | frag_align_code (SUB_SEGMENT_ALIGN (now_seg), 0); | |
3477 | else | |
3478 | frag_align (SUB_SEGMENT_ALIGN (now_seg), 0, 0); | |
3479 | ||
252b5132 RH |
3480 | #ifdef md_do_align |
3481 | alignment_done: | |
3482 | #endif | |
0a9ef439 | 3483 | |
252b5132 RH |
3484 | frag_wane (frag_now); |
3485 | frag_now->fr_fix = 0; | |
3486 | know (frag_now->fr_next == NULL); | |
3487 | } | |
3488 | ||
252b5132 RH |
3489 | remove_subsegs (); |
3490 | ||
252b5132 RH |
3491 | for (i = SEG_E0; i < SEG_UNKNOWN; i++) |
3492 | { | |
3493 | relax_segment (segment_info[i].frchainP->frch_root, i); | |
3494 | } | |
3495 | ||
1cd55018 AM |
3496 | /* Relaxation has completed. Freeze all syms. */ |
3497 | finalize_syms = 1; | |
3498 | ||
252b5132 RH |
3499 | H_SET_NUMBER_OF_SECTIONS (&headers, 0); |
3500 | ||
3501 | /* Find out how big the sections are, and set the addresses. */ | |
3502 | addr = 0; | |
3503 | for (i = SEG_E0; i < SEG_UNKNOWN; i++) | |
3504 | { | |
3505 | long size; | |
3506 | ||
3507 | segment_info[i].scnhdr.s_paddr = addr; | |
3508 | segment_info[i].scnhdr.s_vaddr = addr; | |
3509 | ||
3510 | if (segment_info[i].scnhdr.s_name[0]) | |
3511 | { | |
3512 | H_SET_NUMBER_OF_SECTIONS (&headers, | |
3513 | H_GET_NUMBER_OF_SECTIONS (&headers) + 1); | |
3514 | ||
3515 | #ifdef COFF_LONG_SECTION_NAMES | |
3516 | /* Support long section names as found in PE. This code | |
3517 | must coordinate with that in coff_header_append and | |
3518 | w_strings. */ | |
3519 | { | |
3520 | unsigned int len; | |
3521 | ||
3522 | len = strlen (segment_info[i].name); | |
3523 | if (len > SCNNMLEN) | |
3524 | string_byte_count += len + 1; | |
3525 | } | |
3526 | #endif /* COFF_LONG_SECTION_NAMES */ | |
3527 | } | |
3528 | ||
3529 | size = size_section (abfd, (unsigned int) i); | |
3530 | addr += size; | |
3531 | ||
3532 | /* I think the section alignment is only used on the i960; the | |
3533 | i960 needs it, and it should do no harm on other targets. */ | |
3534 | #ifdef ALIGNMENT_IN_S_FLAGS | |
3535 | segment_info[i].scnhdr.s_flags |= (section_alignment[i] & 0xF) << 8; | |
3536 | #else | |
3537 | segment_info[i].scnhdr.s_align = 1 << section_alignment[i]; | |
3538 | #endif | |
3539 | ||
3540 | if (i == SEG_E0) | |
3541 | H_SET_TEXT_SIZE (&headers, size); | |
3542 | else if (i == SEG_E1) | |
3543 | H_SET_DATA_SIZE (&headers, size); | |
3544 | else if (i == SEG_E2) | |
3545 | H_SET_BSS_SIZE (&headers, size); | |
3546 | } | |
3547 | ||
3548 | /* Turn the gas native symbol table shape into a coff symbol table */ | |
3549 | crawl_symbols (&headers, abfd); | |
3550 | ||
3551 | if (string_byte_count == 4) | |
3552 | string_byte_count = 0; | |
3553 | ||
3554 | H_SET_STRING_SIZE (&headers, string_byte_count); | |
3555 | ||
3556 | #ifdef tc_frob_file | |
3557 | tc_frob_file (); | |
3558 | #endif | |
3559 | ||
3560 | for (i = SEG_E0; i < SEG_UNKNOWN; i++) | |
3561 | { | |
3562 | fixup_mdeps (segment_info[i].frchainP->frch_root, &headers, i); | |
3563 | fixup_segment (&segment_info[i], i); | |
3564 | } | |
3565 | ||
3566 | /* Look for ".stab" segments and fill in their initial symbols | |
dcd619be | 3567 | correctly. */ |
252b5132 RH |
3568 | for (i = SEG_E0; i < SEG_UNKNOWN; i++) |
3569 | { | |
3570 | name = segment_info[i].name; | |
3571 | ||
3572 | if (name != NULL | |
3573 | && strncmp (".stab", name, 5) == 0 | |
3574 | && strncmp (".stabstr", name, 8) != 0) | |
3575 | adjust_stab_section (abfd, i); | |
3576 | } | |
3577 | ||
3578 | file_cursor = H_GET_TEXT_FILE_OFFSET (&headers); | |
3579 | ||
3580 | bfd_seek (abfd, (file_ptr) file_cursor, 0); | |
3581 | ||
3582 | /* Plant the data */ | |
3583 | ||
3584 | fill_section (abfd, &headers, &file_cursor); | |
3585 | ||
3586 | do_relocs_for (abfd, &headers, &file_cursor); | |
3587 | ||
3588 | do_linenos_for (abfd, &headers, &file_cursor); | |
3589 | ||
3590 | H_SET_FILE_MAGIC_NUMBER (&headers, COFF_MAGIC); | |
3591 | #ifndef OBJ_COFF_OMIT_TIMESTAMP | |
3592 | H_SET_TIME_STAMP (&headers, (long)time((time_t *)0)); | |
3593 | #else | |
3594 | H_SET_TIME_STAMP (&headers, 0); | |
3595 | #endif | |
3596 | #ifdef TC_COFF_SET_MACHINE | |
3597 | TC_COFF_SET_MACHINE (&headers); | |
3598 | #endif | |
3599 | ||
3600 | #ifndef COFF_FLAGS | |
3601 | #define COFF_FLAGS 0 | |
3602 | #endif | |
3603 | ||
3604 | #ifdef KEEP_RELOC_INFO | |
3605 | H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers) ? 0 : F_LNNO) | | |
3606 | COFF_FLAGS | coff_flags)); | |
3607 | #else | |
3608 | H_SET_FLAGS (&headers, ((H_GET_LINENO_SIZE(&headers) ? 0 : F_LNNO) | | |
3609 | (H_GET_RELOCATION_SIZE(&headers) ? 0 : F_RELFLG) | | |
3610 | COFF_FLAGS | coff_flags)); | |
3611 | #endif | |
3612 | ||
3613 | { | |
3614 | unsigned int symtable_size = H_GET_SYMBOL_TABLE_SIZE (&headers); | |
3615 | char *buffer1 = xmalloc (symtable_size + string_byte_count + 1); | |
3616 | ||
3617 | H_SET_SYMBOL_TABLE_POINTER (&headers, bfd_tell (abfd)); | |
3618 | w_symbols (abfd, buffer1, symbol_rootP); | |
3619 | if (string_byte_count > 0) | |
3620 | w_strings (buffer1 + symtable_size); | |
3621 | bfd_write (buffer1, 1, symtable_size + string_byte_count, abfd); | |
3622 | free (buffer1); | |
3623 | } | |
3624 | ||
3625 | coff_header_append (abfd, &headers); | |
3626 | #if 0 | |
3627 | /* Recent changes to write need this, but where it should | |
dcd619be | 3628 | go is up to Ken.. */ |
252b5132 RH |
3629 | if (bfd_close_all_done (abfd) == false) |
3630 | as_fatal (_("Can't close %s: %s"), out_file_name, | |
3631 | bfd_errmsg (bfd_get_error ())); | |
3632 | #else | |
3633 | { | |
3634 | extern bfd *stdoutput; | |
3635 | stdoutput = abfd; | |
3636 | } | |
3637 | #endif | |
3638 | ||
3639 | } | |
3640 | ||
3641 | /* Add a new segment. This is called from subseg_new via the | |
3642 | obj_new_segment macro. */ | |
3643 | ||
3644 | segT | |
3645 | obj_coff_add_segment (name) | |
3646 | const char *name; | |
3647 | { | |
3648 | unsigned int i; | |
3649 | ||
3650 | #ifndef COFF_LONG_SECTION_NAMES | |
3651 | char buf[SCNNMLEN + 1]; | |
3652 | ||
3653 | strncpy (buf, name, SCNNMLEN); | |
3654 | buf[SCNNMLEN] = '\0'; | |
3655 | name = buf; | |
3656 | #endif | |
3657 | ||
3658 | for (i = SEG_E0; i < SEG_LAST && segment_info[i].scnhdr.s_name[0]; i++) | |
3659 | if (strcmp (name, segment_info[i].name) == 0) | |
3660 | return (segT) i; | |
3661 | ||
3662 | if (i == SEG_LAST) | |
3663 | { | |
3664 | as_bad (_("Too many new sections; can't add \"%s\""), name); | |
3665 | return now_seg; | |
3666 | } | |
3667 | ||
3668 | /* Add a new section. */ | |
3669 | strncpy (segment_info[i].scnhdr.s_name, name, | |
3670 | sizeof (segment_info[i].scnhdr.s_name)); | |
3671 | segment_info[i].scnhdr.s_flags = STYP_REG; | |
3672 | segment_info[i].name = xstrdup (name); | |
3673 | ||
3674 | return (segT) i; | |
3675 | } | |
3676 | ||
3677 | /* | |
3678 | * implement the .section pseudo op: | |
3679 | * .section name {, "flags"} | |
3680 | * ^ ^ | |
3681 | * | +--- optional flags: 'b' for bss | |
3682 | * | 'i' for info | |
3683 | * +-- section name 'l' for lib | |
3684 | * 'n' for noload | |
3685 | * 'o' for over | |
3686 | * 'w' for data | |
3687 | * 'd' (apparently m88k for data) | |
3688 | * 'x' for text | |
3689 | * 'r' for read-only data | |
3690 | * But if the argument is not a quoted string, treat it as a | |
3691 | * subsegment number. | |
3692 | */ | |
3693 | ||
3694 | void | |
3695 | obj_coff_section (ignore) | |
a04b544b | 3696 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
3697 | { |
3698 | /* Strip out the section name */ | |
3699 | char *section_name, *name; | |
3700 | char c; | |
3701 | unsigned int exp; | |
3702 | long flags; | |
3703 | ||
3704 | if (flag_mri) | |
3705 | { | |
3706 | char type; | |
3707 | ||
3708 | s_mri_sect (&type); | |
3709 | flags = 0; | |
3710 | if (type == 'C') | |
3711 | flags = STYP_TEXT; | |
3712 | else if (type == 'D') | |
3713 | flags = STYP_DATA; | |
3714 | segment_info[now_seg].scnhdr.s_flags |= flags; | |
3715 | ||
3716 | return; | |
3717 | } | |
3718 | ||
3719 | section_name = input_line_pointer; | |
3720 | c = get_symbol_end (); | |
3721 | ||
3722 | name = xmalloc (input_line_pointer - section_name + 1); | |
3723 | strcpy (name, section_name); | |
3724 | ||
3725 | *input_line_pointer = c; | |
3726 | ||
3727 | exp = 0; | |
3728 | flags = 0; | |
3729 | ||
3730 | SKIP_WHITESPACE (); | |
3731 | if (*input_line_pointer == ',') | |
3732 | { | |
3733 | ++input_line_pointer; | |
3734 | SKIP_WHITESPACE (); | |
3735 | ||
3736 | if (*input_line_pointer != '"') | |
3737 | exp = get_absolute_expression (); | |
3738 | else | |
3739 | { | |
3740 | ++input_line_pointer; | |
3741 | while (*input_line_pointer != '"' | |
3742 | && ! is_end_of_line[(unsigned char) *input_line_pointer]) | |
3743 | { | |
3744 | switch (*input_line_pointer) | |
3745 | { | |
3746 | case 'b': flags |= STYP_BSS; break; | |
3747 | case 'i': flags |= STYP_INFO; break; | |
3748 | case 'l': flags |= STYP_LIB; break; | |
3749 | case 'n': flags |= STYP_NOLOAD; break; | |
3750 | case 'o': flags |= STYP_OVER; break; | |
3751 | case 'd': | |
3752 | case 'w': flags |= STYP_DATA; break; | |
3753 | case 'x': flags |= STYP_TEXT; break; | |
3754 | case 'r': flags |= STYP_LIT; break; | |
3755 | default: | |
3756 | as_warn(_("unknown section attribute '%c'"), | |
3757 | *input_line_pointer); | |
3758 | break; | |
3759 | } | |
3760 | ++input_line_pointer; | |
3761 | } | |
3762 | if (*input_line_pointer == '"') | |
3763 | ++input_line_pointer; | |
3764 | } | |
3765 | } | |
3766 | ||
3767 | subseg_new (name, (subsegT) exp); | |
3768 | ||
3769 | segment_info[now_seg].scnhdr.s_flags |= flags; | |
3770 | ||
3771 | demand_empty_rest_of_line (); | |
3772 | } | |
3773 | ||
252b5132 RH |
3774 | static void |
3775 | obj_coff_text (ignore) | |
a04b544b | 3776 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
3777 | { |
3778 | subseg_new (".text", get_absolute_expression ()); | |
3779 | } | |
3780 | ||
252b5132 RH |
3781 | static void |
3782 | obj_coff_data (ignore) | |
a04b544b | 3783 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
3784 | { |
3785 | if (flag_readonly_data_in_text) | |
3786 | subseg_new (".text", get_absolute_expression () + 1000); | |
3787 | else | |
3788 | subseg_new (".data", get_absolute_expression ()); | |
3789 | } | |
3790 | ||
3791 | static void | |
3792 | obj_coff_ident (ignore) | |
a04b544b | 3793 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
3794 | { |
3795 | segT current_seg = now_seg; /* save current seg */ | |
3796 | subsegT current_subseg = now_subseg; | |
3797 | subseg_new (".comment", 0); /* .comment seg */ | |
3798 | stringer (1); /* read string */ | |
3799 | subseg_set (current_seg, current_subseg); /* restore current seg */ | |
3800 | } | |
3801 | ||
3802 | void | |
3803 | c_symbol_merge (debug, normal) | |
3804 | symbolS *debug; | |
3805 | symbolS *normal; | |
3806 | { | |
3807 | S_SET_DATA_TYPE (normal, S_GET_DATA_TYPE (debug)); | |
3808 | S_SET_STORAGE_CLASS (normal, S_GET_STORAGE_CLASS (debug)); | |
3809 | ||
3810 | if (S_GET_NUMBER_AUXILIARY (debug) > S_GET_NUMBER_AUXILIARY (normal)) | |
3811 | { | |
3812 | S_SET_NUMBER_AUXILIARY (normal, S_GET_NUMBER_AUXILIARY (debug)); | |
3813 | } /* take the most we have */ | |
3814 | ||
3815 | if (S_GET_NUMBER_AUXILIARY (debug) > 0) | |
3816 | { | |
3817 | memcpy ((char *) &normal->sy_symbol.ost_auxent[0], | |
3818 | (char *) &debug->sy_symbol.ost_auxent[0], | |
3819 | (unsigned int) (S_GET_NUMBER_AUXILIARY (debug) * AUXESZ)); | |
3820 | } /* Move all the auxiliary information */ | |
3821 | ||
dcd619be | 3822 | /* Move the debug flags. */ |
252b5132 RH |
3823 | SF_SET_DEBUG_FIELD (normal, SF_GET_DEBUG_FIELD (debug)); |
3824 | } /* c_symbol_merge() */ | |
3825 | ||
3826 | static int | |
3827 | c_line_new (symbol, paddr, line_number, frag) | |
3828 | symbolS * symbol; | |
3829 | long paddr; | |
3830 | int line_number; | |
3831 | fragS * frag; | |
3832 | { | |
3833 | struct lineno_list *new_line = | |
3834 | (struct lineno_list *) xmalloc (sizeof (struct lineno_list)); | |
3835 | ||
3836 | segment_info_type *s = segment_info + now_seg; | |
3837 | new_line->line.l_lnno = line_number; | |
3838 | ||
3839 | if (line_number == 0) | |
3840 | { | |
3841 | last_line_symbol = symbol; | |
3842 | new_line->line.l_addr.l_symndx = (long) symbol; | |
3843 | } | |
3844 | else | |
3845 | { | |
3846 | new_line->line.l_addr.l_paddr = paddr; | |
3847 | } | |
3848 | ||
3849 | new_line->frag = (char *) frag; | |
3850 | new_line->next = (struct lineno_list *) NULL; | |
3851 | ||
252b5132 RH |
3852 | if (s->lineno_list_head == (struct lineno_list *) NULL) |
3853 | { | |
3854 | s->lineno_list_head = new_line; | |
3855 | } | |
3856 | else | |
3857 | { | |
3858 | s->lineno_list_tail->next = new_line; | |
3859 | } | |
3860 | s->lineno_list_tail = new_line; | |
3861 | return LINESZ * s->scnhdr.s_nlnno++; | |
3862 | } | |
3863 | ||
3864 | void | |
3865 | c_dot_file_symbol (filename) | |
3866 | char *filename; | |
3867 | { | |
3868 | symbolS *symbolP; | |
3869 | ||
3870 | symbolP = symbol_new (".file", | |
3871 | SEG_DEBUG, | |
3872 | 0, | |
3873 | &zero_address_frag); | |
3874 | ||
3875 | S_SET_STORAGE_CLASS (symbolP, C_FILE); | |
3876 | S_SET_NUMBER_AUXILIARY (symbolP, 1); | |
3877 | ||
3878 | if (strlen (filename) > FILNMLEN) | |
3879 | { | |
3880 | /* Filename is too long to fit into an auxent, | |
3881 | we stick it into the string table instead. We keep | |
3882 | a linked list of the filenames we find so we can emit | |
3883 | them later.*/ | |
3884 | struct filename_list *f = ((struct filename_list *) | |
3885 | xmalloc (sizeof (struct filename_list))); | |
3886 | ||
3887 | f->filename = filename; | |
3888 | f->next = 0; | |
3889 | ||
3890 | SA_SET_FILE_FNAME_ZEROS (symbolP, 0); | |
3891 | SA_SET_FILE_FNAME_OFFSET (symbolP, 1); | |
3892 | ||
dcd619be | 3893 | if (filename_list_tail) |
252b5132 RH |
3894 | filename_list_tail->next = f; |
3895 | else | |
3896 | filename_list_head = f; | |
dcd619be | 3897 | filename_list_tail = f; |
252b5132 | 3898 | } |
dcd619be | 3899 | else |
252b5132 RH |
3900 | { |
3901 | SA_SET_FILE_FNAME (symbolP, filename); | |
3902 | } | |
3903 | #ifndef NO_LISTING | |
3904 | { | |
3905 | extern int listing; | |
3906 | if (listing) | |
3907 | { | |
3908 | listing_source_file (filename); | |
3909 | } | |
3910 | ||
3911 | } | |
3912 | ||
3913 | #endif | |
3914 | SF_SET_DEBUG (symbolP); | |
3915 | S_SET_VALUE (symbolP, (valueT) previous_file_symbol); | |
3916 | ||
3917 | previous_file_symbol = symbolP; | |
3918 | ||
3919 | /* Make sure that the symbol is first on the symbol chain */ | |
3920 | if (symbol_rootP != symbolP) | |
3921 | { | |
3922 | symbol_remove (symbolP, &symbol_rootP, &symbol_lastP); | |
3923 | symbol_insert (symbolP, symbol_rootP, &symbol_rootP, &symbol_lastP); | |
3924 | } | |
3925 | } /* c_dot_file_symbol() */ | |
3926 | ||
3927 | /* | |
3928 | * Build a 'section static' symbol. | |
3929 | */ | |
3930 | ||
3931 | symbolS * | |
3932 | c_section_symbol (name, idx) | |
3933 | char *name; | |
3934 | int idx; | |
3935 | { | |
3936 | symbolS *symbolP; | |
3937 | ||
3938 | symbolP = symbol_find_base (name, DO_NOT_STRIP); | |
3939 | if (symbolP == NULL) | |
3940 | symbolP = symbol_new (name, idx, 0, &zero_address_frag); | |
3941 | else | |
3942 | { | |
3943 | /* Mmmm. I just love violating interfaces. Makes me feel...dirty. */ | |
3944 | S_SET_SEGMENT (symbolP, idx); | |
3945 | symbolP->sy_frag = &zero_address_frag; | |
3946 | } | |
3947 | ||
3948 | S_SET_STORAGE_CLASS (symbolP, C_STAT); | |
3949 | S_SET_NUMBER_AUXILIARY (symbolP, 1); | |
3950 | ||
3951 | SF_SET_STATICS (symbolP); | |
3952 | ||
3953 | #ifdef TE_DELTA | |
3954 | /* manfred@s-direktnet.de: section symbols *must* have the LOCAL bit cleared, | |
3955 | which is set by the new definition of LOCAL_LABEL in tc-m68k.h. */ | |
3956 | SF_CLEAR_LOCAL (symbolP); | |
3957 | #endif | |
3958 | #ifdef TE_PE | |
3959 | /* If the .linkonce pseudo-op was used for this section, we must | |
3960 | store the information in the auxiliary entry for the section | |
3961 | symbol. */ | |
3962 | if (segment_info[idx].linkonce != LINKONCE_UNSET) | |
3963 | { | |
3964 | int type; | |
3965 | ||
3966 | switch (segment_info[idx].linkonce) | |
3967 | { | |
3968 | default: | |
3969 | abort (); | |
3970 | case LINKONCE_DISCARD: | |
3971 | type = IMAGE_COMDAT_SELECT_ANY; | |
3972 | break; | |
3973 | case LINKONCE_ONE_ONLY: | |
3974 | type = IMAGE_COMDAT_SELECT_NODUPLICATES; | |
3975 | break; | |
3976 | case LINKONCE_SAME_SIZE: | |
3977 | type = IMAGE_COMDAT_SELECT_SAME_SIZE; | |
3978 | break; | |
3979 | case LINKONCE_SAME_CONTENTS: | |
3980 | type = IMAGE_COMDAT_SELECT_EXACT_MATCH; | |
3981 | break; | |
3982 | } | |
3983 | ||
3984 | SYM_AUXENT (symbolP)->x_scn.x_comdat = type; | |
3985 | } | |
3986 | #endif /* TE_PE */ | |
3987 | ||
3988 | return symbolP; | |
3989 | } /* c_section_symbol() */ | |
3990 | ||
3991 | static void | |
3992 | w_symbols (abfd, where, symbol_rootP) | |
3993 | bfd * abfd; | |
3994 | char *where; | |
3995 | symbolS * symbol_rootP; | |
3996 | { | |
3997 | symbolS *symbolP; | |
3998 | unsigned int i; | |
3999 | ||
4000 | /* First fill in those values we have only just worked out */ | |
4001 | for (i = SEG_E0; i < SEG_LAST; i++) | |
4002 | { | |
4003 | symbolP = segment_info[i].dot; | |
4004 | if (symbolP) | |
4005 | { | |
4006 | SA_SET_SCN_SCNLEN (symbolP, segment_info[i].scnhdr.s_size); | |
4007 | SA_SET_SCN_NRELOC (symbolP, segment_info[i].scnhdr.s_nreloc); | |
4008 | SA_SET_SCN_NLINNO (symbolP, segment_info[i].scnhdr.s_nlnno); | |
4009 | } | |
4010 | } | |
4011 | ||
4012 | /* | |
4013 | * Emit all symbols left in the symbol chain. | |
4014 | */ | |
4015 | for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP)) | |
4016 | { | |
4017 | /* Used to save the offset of the name. It is used to point | |
dcd619be | 4018 | to the string in memory but must be a file offset. */ |
252b5132 RH |
4019 | register char *temp; |
4020 | ||
4021 | /* We can't fix the lnnoptr field in yank_symbols with the other | |
4022 | adjustments, because we have to wait until we know where they | |
4023 | go in the file. */ | |
4024 | if (SF_GET_ADJ_LNNOPTR (symbolP)) | |
4025 | { | |
4026 | SA_GET_SYM_LNNOPTR (symbolP) += | |
4027 | segment_info[S_GET_SEGMENT (symbolP)].scnhdr.s_lnnoptr; | |
4028 | } | |
4029 | ||
4030 | tc_coff_symbol_emit_hook (symbolP); | |
4031 | ||
4032 | temp = S_GET_NAME (symbolP); | |
4033 | if (SF_GET_STRING (symbolP)) | |
4034 | { | |
4035 | S_SET_OFFSET (symbolP, symbolP->sy_name_offset); | |
4036 | S_SET_ZEROES (symbolP, 0); | |
4037 | } | |
4038 | else | |
4039 | { | |
4040 | memset (symbolP->sy_symbol.ost_entry.n_name, 0, SYMNMLEN); | |
4041 | strncpy (symbolP->sy_symbol.ost_entry.n_name, temp, SYMNMLEN); | |
4042 | } | |
4043 | where = symbol_to_chars (abfd, where, symbolP); | |
4044 | S_SET_NAME (symbolP, temp); | |
4045 | } | |
4046 | ||
4047 | } /* w_symbols() */ | |
4048 | ||
4049 | static void | |
4050 | obj_coff_lcomm (ignore) | |
a04b544b | 4051 | int ignore ATTRIBUTE_UNUSED; |
252b5132 RH |
4052 | { |
4053 | s_lcomm(0); | |
4054 | return; | |
4055 | #if 0 | |
4056 | char *name; | |
4057 | char c; | |
4058 | int temp; | |
4059 | char *p; | |
4060 | ||
4061 | symbolS *symbolP; | |
4062 | ||
4063 | name = input_line_pointer; | |
4064 | ||
4065 | c = get_symbol_end (); | |
4066 | p = input_line_pointer; | |
4067 | *p = c; | |
4068 | SKIP_WHITESPACE (); | |
4069 | if (*input_line_pointer != ',') | |
4070 | { | |
4071 | as_bad (_("Expected comma after name")); | |
4072 | ignore_rest_of_line (); | |
4073 | return; | |
4074 | } | |
4075 | if (*input_line_pointer == '\n') | |
4076 | { | |
4077 | as_bad (_("Missing size expression")); | |
4078 | return; | |
4079 | } | |
4080 | input_line_pointer++; | |
4081 | if ((temp = get_absolute_expression ()) < 0) | |
4082 | { | |
4083 | as_warn (_("lcomm length (%d.) <0! Ignored."), temp); | |
4084 | ignore_rest_of_line (); | |
4085 | return; | |
4086 | } | |
4087 | *p = 0; | |
4088 | ||
708b82c7 | 4089 | symbolP = symbol_find_or_make (name); |
252b5132 | 4090 | |
708b82c7 NC |
4091 | if (S_GET_SEGMENT (symbolP) == SEG_UNKNOWN && |
4092 | S_GET_VALUE (symbolP) == 0) | |
252b5132 RH |
4093 | { |
4094 | if (! need_pass_2) | |
4095 | { | |
4096 | char *p; | |
4097 | segT current_seg = now_seg; /* save current seg */ | |
4098 | subsegT current_subseg = now_subseg; | |
4099 | ||
4100 | subseg_set (SEG_E2, 1); | |
4101 | symbolP->sy_frag = frag_now; | |
4102 | p = frag_var(rs_org, 1, 1, (relax_substateT)0, symbolP, | |
4103 | (offsetT) temp, (char *) 0); | |
4104 | *p = 0; | |
4105 | subseg_set (current_seg, current_subseg); /* restore current seg */ | |
708b82c7 NC |
4106 | S_SET_SEGMENT (symbolP, SEG_E2); |
4107 | S_SET_STORAGE_CLASS (symbolP, C_STAT); | |
252b5132 RH |
4108 | } |
4109 | } | |
4110 | else | |
708b82c7 | 4111 | as_bad (_("Symbol %s already defined"), name); |
252b5132 | 4112 | |
708b82c7 | 4113 | demand_empty_rest_of_line (); |
252b5132 RH |
4114 | #endif |
4115 | } | |
4116 | ||
4117 | static void | |
4118 | fixup_mdeps (frags, h, this_segment) | |
4119 | fragS * frags; | |
4120 | object_headers * h; | |
4121 | segT this_segment; | |
4122 | { | |
4123 | subseg_change (this_segment, 0); | |
4124 | while (frags) | |
4125 | { | |
4126 | switch (frags->fr_type) | |
4127 | { | |
4128 | case rs_align: | |
4129 | case rs_align_code: | |
0a9ef439 | 4130 | case rs_align_test: |
252b5132 RH |
4131 | case rs_org: |
4132 | #ifdef HANDLE_ALIGN | |
4133 | HANDLE_ALIGN (frags); | |
4134 | #endif | |
4135 | frags->fr_type = rs_fill; | |
4136 | frags->fr_offset = | |
4137 | ((frags->fr_next->fr_address - frags->fr_address - frags->fr_fix) | |
4138 | / frags->fr_var); | |
4139 | break; | |
4140 | case rs_machine_dependent: | |
4141 | md_convert_frag (h, this_segment, frags); | |
4142 | frag_wane (frags); | |
4143 | break; | |
4144 | default: | |
4145 | ; | |
4146 | } | |
4147 | frags = frags->fr_next; | |
4148 | } | |
4149 | } | |
4150 | ||
4151 | #if 1 | |
4152 | ||
4153 | #ifndef TC_FORCE_RELOCATION | |
4154 | #define TC_FORCE_RELOCATION(fix) 0 | |
4155 | #endif | |
4156 | ||
4157 | static void | |
4158 | fixup_segment (segP, this_segment_type) | |
4159 | segment_info_type * segP; | |
4160 | segT this_segment_type; | |
4161 | { | |
4162 | register fixS * fixP; | |
4163 | register symbolS *add_symbolP; | |
4164 | register symbolS *sub_symbolP; | |
4165 | long add_number; | |
4166 | register int size; | |
4167 | register char *place; | |
4168 | register long where; | |
4169 | register char pcrel; | |
4170 | register fragS *fragP; | |
4171 | register segT add_symbol_segment = absolute_section; | |
4172 | ||
4173 | for (fixP = segP->fix_root; fixP; fixP = fixP->fx_next) | |
4174 | { | |
4175 | fragP = fixP->fx_frag; | |
4176 | know (fragP); | |
4177 | where = fixP->fx_where; | |
4178 | place = fragP->fr_literal + where; | |
4179 | size = fixP->fx_size; | |
4180 | add_symbolP = fixP->fx_addsy; | |
4181 | sub_symbolP = fixP->fx_subsy; | |
4182 | add_number = fixP->fx_offset; | |
4183 | pcrel = fixP->fx_pcrel; | |
4184 | ||
4185 | /* We want function-relative stabs to work on systems which | |
4186 | may use a relaxing linker; thus we must handle the sym1-sym2 | |
4187 | fixups function-relative stabs generates. | |
4188 | ||
4189 | Of course, if you actually enable relaxing in the linker, the | |
4190 | line and block scoping information is going to be incorrect | |
4191 | in some cases. The only way to really fix this is to support | |
4192 | a reloc involving the difference of two symbols. */ | |
4193 | if (linkrelax | |
4194 | && (!sub_symbolP || pcrel)) | |
4195 | continue; | |
4196 | ||
4197 | #ifdef TC_I960 | |
4198 | if (fixP->fx_tcbit && SF_GET_CALLNAME (add_symbolP)) | |
4199 | { | |
4200 | /* Relocation should be done via the associated 'bal' entry | |
dcd619be | 4201 | point symbol. */ |
252b5132 RH |
4202 | |
4203 | if (!SF_GET_BALNAME (tc_get_bal_of_call (add_symbolP))) | |
4204 | { | |
4205 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
4206 | _("No 'bal' entry point for leafproc %s"), | |
4207 | S_GET_NAME (add_symbolP)); | |
4208 | continue; | |
4209 | } | |
4210 | fixP->fx_addsy = add_symbolP = tc_get_bal_of_call (add_symbolP); | |
4211 | } | |
4212 | #endif | |
4213 | ||
4214 | /* Make sure the symbols have been resolved; this may not have | |
4215 | happened if these are expression symbols. */ | |
4216 | if (add_symbolP != NULL && ! add_symbolP->sy_resolved) | |
6386f3a7 | 4217 | resolve_symbol_value (add_symbolP); |
252b5132 RH |
4218 | |
4219 | if (add_symbolP != NULL) | |
4220 | { | |
4221 | /* If this fixup is against a symbol which has been equated | |
4222 | to another symbol, convert it to the other symbol. */ | |
4223 | if (add_symbolP->sy_value.X_op == O_symbol | |
4224 | && (! S_IS_DEFINED (add_symbolP) | |
4225 | || S_IS_COMMON (add_symbolP))) | |
4226 | { | |
4227 | while (add_symbolP->sy_value.X_op == O_symbol | |
4228 | && (! S_IS_DEFINED (add_symbolP) | |
4229 | || S_IS_COMMON (add_symbolP))) | |
4230 | { | |
4231 | symbolS *n; | |
4232 | ||
4233 | /* We must avoid looping, as that can occur with a | |
4234 | badly written program. */ | |
4235 | n = add_symbolP->sy_value.X_add_symbol; | |
4236 | if (n == add_symbolP) | |
4237 | break; | |
4238 | add_number += add_symbolP->sy_value.X_add_number; | |
4239 | add_symbolP = n; | |
4240 | } | |
4241 | fixP->fx_addsy = add_symbolP; | |
4242 | fixP->fx_offset = add_number; | |
4243 | } | |
4244 | } | |
4245 | ||
4246 | if (sub_symbolP != NULL && ! sub_symbolP->sy_resolved) | |
6386f3a7 | 4247 | resolve_symbol_value (sub_symbolP); |
252b5132 RH |
4248 | |
4249 | if (add_symbolP != NULL | |
4250 | && add_symbolP->sy_mri_common) | |
4251 | { | |
4252 | know (add_symbolP->sy_value.X_op == O_symbol); | |
4253 | add_number += S_GET_VALUE (add_symbolP); | |
4254 | fixP->fx_offset = add_number; | |
4255 | add_symbolP = fixP->fx_addsy = add_symbolP->sy_value.X_add_symbol; | |
4256 | } | |
4257 | ||
4258 | if (add_symbolP) | |
4259 | { | |
4260 | add_symbol_segment = S_GET_SEGMENT (add_symbolP); | |
4261 | } /* if there is an addend */ | |
4262 | ||
4263 | if (sub_symbolP) | |
4264 | { | |
4265 | if (add_symbolP == NULL || add_symbol_segment == absolute_section) | |
4266 | { | |
4267 | if (add_symbolP != NULL) | |
4268 | { | |
4269 | add_number += S_GET_VALUE (add_symbolP); | |
4270 | add_symbolP = NULL; | |
4271 | fixP->fx_addsy = NULL; | |
4272 | } | |
4273 | ||
4274 | /* It's just -sym. */ | |
4275 | if (S_GET_SEGMENT (sub_symbolP) == absolute_section) | |
4276 | { | |
4277 | add_number -= S_GET_VALUE (sub_symbolP); | |
4278 | fixP->fx_subsy = 0; | |
4279 | fixP->fx_done = 1; | |
4280 | } | |
4281 | else | |
4282 | { | |
4283 | #ifndef TC_M68K | |
4284 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
4285 | _("Negative of non-absolute symbol %s"), | |
4286 | S_GET_NAME (sub_symbolP)); | |
4287 | #endif | |
4288 | add_number -= S_GET_VALUE (sub_symbolP); | |
4289 | } /* not absolute */ | |
4290 | ||
4291 | /* if sub_symbol is in the same segment that add_symbol | |
4292 | and add_symbol is either in DATA, TEXT, BSS or ABSOLUTE */ | |
4293 | } | |
4294 | else if (S_GET_SEGMENT (sub_symbolP) == add_symbol_segment | |
4295 | && SEG_NORMAL (add_symbol_segment)) | |
4296 | { | |
4297 | /* Difference of 2 symbols from same segment. Can't | |
4298 | make difference of 2 undefineds: 'value' means | |
dcd619be | 4299 | something different for N_UNDF. */ |
252b5132 RH |
4300 | #ifdef TC_I960 |
4301 | /* Makes no sense to use the difference of 2 arbitrary symbols | |
4302 | as the target of a call instruction. */ | |
4303 | if (fixP->fx_tcbit) | |
4304 | { | |
4305 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
4306 | _("callj to difference of 2 symbols")); | |
4307 | } | |
4308 | #endif /* TC_I960 */ | |
4309 | add_number += S_GET_VALUE (add_symbolP) - | |
4310 | S_GET_VALUE (sub_symbolP); | |
4311 | add_symbolP = NULL; | |
4312 | ||
4313 | if (!TC_FORCE_RELOCATION (fixP)) | |
4314 | { | |
4315 | fixP->fx_addsy = NULL; | |
4316 | fixP->fx_subsy = NULL; | |
4317 | fixP->fx_done = 1; | |
4318 | #ifdef TC_M68K /* is this right? */ | |
4319 | pcrel = 0; | |
4320 | fixP->fx_pcrel = 0; | |
4321 | #endif | |
4322 | } | |
4323 | } | |
4324 | else | |
4325 | { | |
dcd619be | 4326 | /* Different segments in subtraction. */ |
252b5132 RH |
4327 | know (!(S_IS_EXTERNAL (sub_symbolP) && (S_GET_SEGMENT (sub_symbolP) == absolute_section))); |
4328 | ||
4329 | if ((S_GET_SEGMENT (sub_symbolP) == absolute_section)) | |
4330 | { | |
4331 | add_number -= S_GET_VALUE (sub_symbolP); | |
4332 | } | |
4333 | #ifdef DIFF_EXPR_OK | |
4334 | else if (S_GET_SEGMENT (sub_symbolP) == this_segment_type | |
dcd619be | 4335 | #if 0 /* Okay for 68k, at least... */ |
252b5132 RH |
4336 | && !pcrel |
4337 | #endif | |
4338 | ) | |
4339 | { | |
4340 | /* Make it pc-relative. */ | |
4341 | add_number += (md_pcrel_from (fixP) | |
4342 | - S_GET_VALUE (sub_symbolP)); | |
4343 | pcrel = 1; | |
4344 | fixP->fx_pcrel = 1; | |
4345 | sub_symbolP = 0; | |
4346 | fixP->fx_subsy = 0; | |
4347 | } | |
4348 | #endif | |
4349 | else | |
4350 | { | |
4351 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
4352 | _("Can't emit reloc {- %s-seg symbol \"%s\"} @ file address %ld."), | |
4353 | segment_name (S_GET_SEGMENT (sub_symbolP)), | |
4354 | S_GET_NAME (sub_symbolP), | |
4355 | (long) (fragP->fr_address + where)); | |
4356 | } /* if absolute */ | |
4357 | } | |
4358 | } /* if sub_symbolP */ | |
4359 | ||
4360 | if (add_symbolP) | |
4361 | { | |
4362 | if (add_symbol_segment == this_segment_type && pcrel) | |
4363 | { | |
4364 | /* | |
4365 | * This fixup was made when the symbol's segment was | |
4366 | * SEG_UNKNOWN, but it is now in the local segment. | |
4367 | * So we know how to do the address without relocation. | |
4368 | */ | |
4369 | #ifdef TC_I960 | |
4370 | /* reloc_callj() may replace a 'call' with a 'calls' or a 'bal', | |
4371 | * in which cases it modifies *fixP as appropriate. In the case | |
4372 | * of a 'calls', no further work is required, and *fixP has been | |
4373 | * set up to make the rest of the code below a no-op. | |
4374 | */ | |
4375 | reloc_callj (fixP); | |
4376 | #endif /* TC_I960 */ | |
4377 | ||
4378 | add_number += S_GET_VALUE (add_symbolP); | |
4379 | add_number -= md_pcrel_from (fixP); | |
4380 | ||
4381 | /* We used to do | |
4382 | add_number -= segP->scnhdr.s_vaddr; | |
4383 | if defined (TC_I386) || defined (TE_LYNX). I now | |
4384 | think that was an error propagated from the case when | |
4385 | we are going to emit the relocation. If we are not | |
4386 | going to emit the relocation, then we just want to | |
4387 | set add_number to the difference between the symbols. | |
4388 | This is a case that would only arise when there is a | |
4389 | PC relative reference from a section other than .text | |
4390 | to a symbol defined in the same section, and the | |
4391 | reference is not relaxed. Since jump instructions on | |
4392 | the i386 are relaxed, this could only arise with a | |
4393 | call instruction. */ | |
4394 | ||
dcd619be | 4395 | pcrel = 0; /* Lie. Don't want further pcrel processing. */ |
252b5132 RH |
4396 | if (!TC_FORCE_RELOCATION (fixP)) |
4397 | { | |
4398 | fixP->fx_addsy = NULL; | |
4399 | fixP->fx_done = 1; | |
4400 | } | |
4401 | } | |
4402 | else | |
4403 | { | |
4404 | switch (add_symbol_segment) | |
4405 | { | |
4406 | case absolute_section: | |
4407 | #ifdef TC_I960 | |
4408 | reloc_callj (fixP); /* See comment about reloc_callj() above*/ | |
4409 | #endif /* TC_I960 */ | |
4410 | add_number += S_GET_VALUE (add_symbolP); | |
4411 | add_symbolP = NULL; | |
4412 | ||
4413 | if (!TC_FORCE_RELOCATION (fixP)) | |
4414 | { | |
4415 | fixP->fx_addsy = NULL; | |
4416 | fixP->fx_done = 1; | |
4417 | } | |
4418 | break; | |
4419 | default: | |
4420 | ||
252b5132 RH |
4421 | #if defined(TC_A29K) || (defined(TE_PE) && defined(TC_I386)) || defined(TC_M88K) |
4422 | /* This really should be handled in the linker, but | |
4423 | backward compatibility forbids. */ | |
4424 | add_number += S_GET_VALUE (add_symbolP); | |
4425 | #else | |
4426 | add_number += S_GET_VALUE (add_symbolP) + | |
4427 | segment_info[S_GET_SEGMENT (add_symbolP)].scnhdr.s_paddr; | |
4428 | #endif | |
4429 | break; | |
4430 | ||
4431 | case SEG_UNKNOWN: | |
4432 | #ifdef TC_I960 | |
4433 | if ((int) fixP->fx_bit_fixP == 13) | |
4434 | { | |
4435 | /* This is a COBR instruction. They have only a | |
4436 | * 13-bit displacement and are only to be used | |
4437 | * for local branches: flag as error, don't generate | |
4438 | * relocation. | |
4439 | */ | |
4440 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
4441 | _("can't use COBR format with external label")); | |
4442 | fixP->fx_addsy = NULL; | |
4443 | fixP->fx_done = 1; | |
4444 | continue; | |
4445 | } /* COBR */ | |
4446 | #endif /* TC_I960 */ | |
4447 | #if ((defined (TC_I386) || defined (TE_LYNX) || defined (TE_AUX)) && !defined(TE_PE)) || defined (COFF_COMMON_ADDEND) | |
4448 | /* 386 COFF uses a peculiar format in which the | |
4449 | value of a common symbol is stored in the .text | |
4450 | segment (I've checked this on SVR3.2 and SCO | |
4451 | 3.2.2) Ian Taylor <ian@cygnus.com>. */ | |
4452 | /* This is also true for 68k COFF on sysv machines | |
4453 | (Checked on Motorola sysv68 R3V6 and R3V7.1, and also on | |
4454 | UNIX System V/M68000, Release 1.0 from ATT/Bell Labs) | |
dcd619be | 4455 | Philippe De Muyter <phdm@info.ucl.ac.be>. */ |
252b5132 RH |
4456 | if (S_IS_COMMON (add_symbolP)) |
4457 | add_number += S_GET_VALUE (add_symbolP); | |
4458 | #endif | |
4459 | break; | |
4460 | ||
252b5132 RH |
4461 | } /* switch on symbol seg */ |
4462 | } /* if not in local seg */ | |
4463 | } /* if there was a + symbol */ | |
4464 | ||
4465 | if (pcrel) | |
4466 | { | |
4467 | #if !defined(TC_M88K) && !(defined(TE_PE) && defined(TC_I386)) && !defined(TC_A29K) | |
4468 | /* This adjustment is not correct on the m88k, for which the | |
4469 | linker does all the computation. */ | |
4470 | add_number -= md_pcrel_from (fixP); | |
4471 | #endif | |
4472 | if (add_symbolP == 0) | |
4473 | { | |
4474 | fixP->fx_addsy = &abs_symbol; | |
4475 | } /* if there's an add_symbol */ | |
4476 | #if defined (TC_I386) || defined (TE_LYNX) || defined (TC_I960) || defined (TC_M68K) | |
4477 | /* On the 386 we must adjust by the segment vaddr as well. | |
4478 | Ian Taylor. | |
4479 | ||
4480 | I changed the i960 to work this way as well. This is | |
4481 | compatible with the current GNU linker behaviour. I do | |
4482 | not know what other i960 COFF assemblers do. This is not | |
4483 | a common case: normally, only assembler code will contain | |
4484 | a PC relative reloc, and only branches which do not | |
4485 | originate in the .text section will have a non-zero | |
4486 | address. | |
4487 | ||
4488 | I changed the m68k to work this way as well. This will | |
4489 | break existing PC relative relocs from sections which do | |
4490 | not start at address 0, but it will make ld -r work. | |
4491 | Ian Taylor, 4 Oct 96. */ | |
4492 | ||
4493 | add_number -= segP->scnhdr.s_vaddr; | |
4494 | #endif | |
4495 | } /* if pcrel */ | |
4496 | ||
ec0f0840 AM |
4497 | #ifdef MD_APPLY_FIX3 |
4498 | md_apply_fix3 (fixP, (valueT *) &add_number, this_segment_type); | |
4499 | #else | |
4500 | md_apply_fix (fixP, add_number); | |
4501 | #endif | |
4502 | ||
252b5132 RH |
4503 | if (!fixP->fx_bit_fixP && ! fixP->fx_no_overflow) |
4504 | { | |
4505 | #ifndef TC_M88K | |
4506 | /* The m88k uses the offset field of the reloc to get around | |
4507 | this problem. */ | |
4508 | if ((size == 1 | |
4509 | && ((add_number & ~0xFF) | |
4510 | || (fixP->fx_signed && (add_number & 0x80))) | |
4511 | && ((add_number & ~0xFF) != (-1 & ~0xFF) | |
4512 | || (add_number & 0x80) == 0)) | |
4513 | || (size == 2 | |
4514 | && ((add_number & ~0xFFFF) | |
4515 | || (fixP->fx_signed && (add_number & 0x8000))) | |
4516 | && ((add_number & ~0xFFFF) != (-1 & ~0xFFFF) | |
4517 | || (add_number & 0x8000) == 0))) | |
4518 | { | |
4519 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
4520 | _("Value of %ld too large for field of %d bytes at 0x%lx"), | |
4521 | (long) add_number, size, | |
4522 | (unsigned long) (fragP->fr_address + where)); | |
4523 | } | |
4524 | #endif | |
4525 | #ifdef WARN_SIGNED_OVERFLOW_WORD | |
4526 | /* Warn if a .word value is too large when treated as a | |
4527 | signed number. We already know it is not too negative. | |
4528 | This is to catch over-large switches generated by gcc on | |
4529 | the 68k. */ | |
4530 | if (!flag_signed_overflow_ok | |
4531 | && size == 2 | |
4532 | && add_number > 0x7fff) | |
4533 | as_bad_where (fixP->fx_file, fixP->fx_line, | |
4534 | _("Signed .word overflow; switch may be too large; %ld at 0x%lx"), | |
4535 | (long) add_number, | |
4536 | (unsigned long) (fragP->fr_address + where)); | |
4537 | #endif | |
4538 | } /* not a bit fix */ | |
dcd619be | 4539 | } /* For each fixS in this segment. */ |
252b5132 RH |
4540 | } /* fixup_segment() */ |
4541 | ||
4542 | #endif | |
4543 | ||
4544 | /* The first entry in a .stab section is special. */ | |
4545 | ||
4546 | void | |
4547 | obj_coff_init_stab_section (seg) | |
4548 | segT seg; | |
4549 | { | |
4550 | char *file; | |
4551 | char *p; | |
4552 | char *stabstr_name; | |
4553 | unsigned int stroff; | |
4554 | ||
dcd619be | 4555 | /* Make space for this first symbol. */ |
252b5132 | 4556 | p = frag_more (12); |
dcd619be | 4557 | /* Zero it out. */ |
252b5132 RH |
4558 | memset (p, 0, 12); |
4559 | as_where (&file, (unsigned int *) NULL); | |
4560 | stabstr_name = (char *) alloca (strlen (segment_info[seg].name) + 4); | |
4561 | strcpy (stabstr_name, segment_info[seg].name); | |
4562 | strcat (stabstr_name, "str"); | |
4563 | stroff = get_stab_string_offset (file, stabstr_name); | |
4564 | know (stroff == 1); | |
4565 | md_number_to_chars (p, stroff, 4); | |
4566 | } | |
4567 | ||
4568 | /* Fill in the counts in the first entry in a .stab section. */ | |
4569 | ||
4570 | static void | |
4571 | adjust_stab_section(abfd, seg) | |
4572 | bfd *abfd; | |
4573 | segT seg; | |
4574 | { | |
4575 | segT stabstrseg = SEG_UNKNOWN; | |
4576 | const char *secname, *name2; | |
4577 | char *name; | |
4578 | char *p = NULL; | |
4579 | int i, strsz = 0, nsyms; | |
4580 | fragS *frag = segment_info[seg].frchainP->frch_root; | |
4581 | ||
dcd619be | 4582 | /* Look for the associated string table section. */ |
252b5132 RH |
4583 | |
4584 | secname = segment_info[seg].name; | |
4585 | name = (char *) alloca (strlen (secname) + 4); | |
4586 | strcpy (name, secname); | |
4587 | strcat (name, "str"); | |
4588 | ||
4589 | for (i = SEG_E0; i < SEG_UNKNOWN; i++) | |
4590 | { | |
4591 | name2 = segment_info[i].name; | |
4592 | if (name2 != NULL && strncmp(name2, name, 8) == 0) | |
4593 | { | |
4594 | stabstrseg = i; | |
4595 | break; | |
4596 | } | |
4597 | } | |
4598 | ||
dcd619be | 4599 | /* If we found the section, get its size. */ |
252b5132 RH |
4600 | if (stabstrseg != SEG_UNKNOWN) |
4601 | strsz = size_section (abfd, stabstrseg); | |
4602 | ||
4603 | nsyms = size_section (abfd, seg) / 12 - 1; | |
4604 | ||
4605 | /* Look for the first frag of sufficient size for the initial stab | |
dcd619be | 4606 | symbol, and collect a pointer to it. */ |
252b5132 RH |
4607 | while (frag && frag->fr_fix < 12) |
4608 | frag = frag->fr_next; | |
4609 | assert (frag != 0); | |
4610 | p = frag->fr_literal; | |
4611 | assert (p != 0); | |
4612 | ||
4613 | /* Write in the number of stab symbols and the size of the string | |
dcd619be | 4614 | table. */ |
252b5132 RH |
4615 | bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6); |
4616 | bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8); | |
4617 | } | |
4618 | ||
4619 | #endif /* not BFD_ASSEMBLER */ | |
4620 | ||
4c63da97 | 4621 | const pseudo_typeS coff_pseudo_table[] = |
252b5132 RH |
4622 | { |
4623 | {"def", obj_coff_def, 0}, | |
4624 | {"dim", obj_coff_dim, 0}, | |
4625 | {"endef", obj_coff_endef, 0}, | |
4626 | {"line", obj_coff_line, 0}, | |
4627 | {"ln", obj_coff_ln, 0}, | |
28428223 ILT |
4628 | #ifdef BFD_ASSEMBLER |
4629 | {"loc", obj_coff_loc, 0}, | |
4630 | #endif | |
252b5132 RH |
4631 | {"appline", obj_coff_ln, 1}, |
4632 | {"scl", obj_coff_scl, 0}, | |
4633 | {"size", obj_coff_size, 0}, | |
4634 | {"tag", obj_coff_tag, 0}, | |
4635 | {"type", obj_coff_type, 0}, | |
4636 | {"val", obj_coff_val, 0}, | |
4637 | {"section", obj_coff_section, 0}, | |
4638 | {"sect", obj_coff_section, 0}, | |
4639 | /* FIXME: We ignore the MRI short attribute. */ | |
4640 | {"section.s", obj_coff_section, 0}, | |
4641 | {"sect.s", obj_coff_section, 0}, | |
4642 | /* We accept the .bss directive for backward compatibility with | |
4643 | earlier versions of gas. */ | |
4644 | {"bss", obj_coff_bss, 0}, | |
4645 | {"weak", obj_coff_weak, 0}, | |
7a6284c4 | 4646 | {"ident", obj_coff_ident, 0}, |
252b5132 RH |
4647 | #ifndef BFD_ASSEMBLER |
4648 | {"use", obj_coff_section, 0}, | |
4649 | {"text", obj_coff_text, 0}, | |
4650 | {"data", obj_coff_data, 0}, | |
4651 | {"lcomm", obj_coff_lcomm, 0}, | |
252b5132 RH |
4652 | #else |
4653 | {"optim", s_ignore, 0}, /* For sun386i cc (?) */ | |
252b5132 RH |
4654 | #endif |
4655 | {"version", s_ignore, 0}, | |
4656 | {"ABORT", s_abort, 0}, | |
4657 | #ifdef TC_M88K | |
4658 | /* The m88k uses sdef instead of def. */ | |
4659 | {"sdef", obj_coff_def, 0}, | |
4660 | #endif | |
a04b544b | 4661 | {NULL, NULL, 0} /* end sentinel */ |
4c63da97 | 4662 | }; /* coff_pseudo_table */ |
252b5132 RH |
4663 | \f |
4664 | #ifdef BFD_ASSEMBLER | |
4665 | ||
4666 | /* Support for a COFF emulation. */ | |
4667 | ||
4c63da97 | 4668 | static void coff_pop_insert PARAMS ((void)); |
5110c57e | 4669 | static int coff_separate_stab_sections PARAMS ((void)); |
4c63da97 | 4670 | |
252b5132 RH |
4671 | static void |
4672 | coff_pop_insert () | |
4673 | { | |
4c63da97 | 4674 | pop_insert (coff_pseudo_table); |
252b5132 RH |
4675 | } |
4676 | ||
5110c57e HPN |
4677 | static int |
4678 | coff_separate_stab_sections () | |
4679 | { | |
4680 | return 1; | |
4681 | } | |
4682 | ||
252b5132 RH |
4683 | const struct format_ops coff_format_ops = |
4684 | { | |
4685 | bfd_target_coff_flavour, | |
4c63da97 AM |
4686 | 0, /* dfl_leading_underscore */ |
4687 | 1, /* emit_section_symbols */ | |
5110c57e HPN |
4688 | 0, /* begin */ |
4689 | c_dot_file_symbol, | |
252b5132 | 4690 | coff_frob_symbol, |
4c63da97 | 4691 | 0, /* frob_file */ |
339681c0 | 4692 | 0, /* frob_file_before_adjust */ |
252b5132 | 4693 | coff_frob_file_after_relocs, |
4c63da97 AM |
4694 | 0, /* s_get_size */ |
4695 | 0, /* s_set_size */ | |
4696 | 0, /* s_get_align */ | |
4697 | 0, /* s_set_align */ | |
4698 | 0, /* s_get_other */ | |
5110c57e | 4699 | 0, /* s_set_other */ |
4c63da97 | 4700 | 0, /* s_get_desc */ |
5110c57e HPN |
4701 | 0, /* s_set_desc */ |
4702 | 0, /* s_get_type */ | |
4703 | 0, /* s_set_type */ | |
4c63da97 AM |
4704 | 0, /* copy_symbol_attributes */ |
4705 | 0, /* generate_asm_lineno */ | |
4706 | 0, /* process_stab */ | |
5110c57e HPN |
4707 | coff_separate_stab_sections, |
4708 | obj_coff_init_stab_section, | |
4c63da97 | 4709 | 0, /* sec_sym_ok_for_reloc */ |
252b5132 | 4710 | coff_pop_insert, |
4c63da97 | 4711 | 0, /* ecoff_set_ext */ |
252b5132 | 4712 | coff_obj_read_begin_hook, |
4c63da97 | 4713 | coff_obj_symbol_new_hook |
252b5132 RH |
4714 | }; |
4715 | ||
4716 | #endif |