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