1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
|
/* ELF object file format
Copyright (C) 1992, 1993 Free Software Foundation, Inc.
This file is part of GAS, the GNU Assembler.
GAS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2,
or (at your option) any later version.
GAS is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with GAS; see the file COPYING. If not, write
to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "as.h"
#include "subsegs.h"
#include "aout/stab_gnu.h"
#include "obstack.h"
static void obj_elf_stab PARAMS ((int what));
static void obj_elf_xstab PARAMS ((int what));
static void obj_elf_line PARAMS ((void));
void obj_elf_desc PARAMS ((void));
void obj_elf_version PARAMS ((void));
static void obj_elf_section PARAMS ((int));
static void obj_elf_size PARAMS ((void));
static void obj_elf_type PARAMS ((void));
static void obj_elf_ident PARAMS ((void));
const pseudo_typeS obj_pseudo_table[] =
{
{"ident", obj_elf_ident, 0},
{"section", obj_elf_section, 0},
{"size", obj_elf_size, 0},
{"type", obj_elf_type, 0},
{"version", obj_elf_version, 0},
/* These are used for stabs-in-elf configurations. */
{"desc", obj_elf_desc, 0},
{"line", obj_elf_line, 0},
{"stabd", obj_elf_stab, 'd'},
{"stabn", obj_elf_stab, 'n'},
{"stabs", obj_elf_stab, 's'},
/* This is used on Solaris 2.x on SPARC, but not supported yet. */
{"xstabs", obj_elf_xstab, 's'},
{NULL} /* end sentinel */
};
#include "aout/aout64.h"
void
elf_file_symbol (s)
char *s;
{
symbolS *sym;
sym = symbol_new (s, absolute_section, (valueT) 0, (struct frag *) 0);
sym->sy_frag = &zero_address_frag;
sym->bsym->flags |= BSF_FILE;
if (symbol_rootP != sym)
{
symbol_remove (sym, &symbol_rootP, &symbol_lastP);
symbol_insert (sym, symbol_rootP, &symbol_rootP, &symbol_lastP);
#ifdef DEBUG
verify_symbol_chain (symbol_rootP, symbol_lastP);
#endif
}
}
static void
obj_elf_section (xxx)
int xxx;
{
char *string;
asection *sec;
/* Initialize this with inclusive-or of all flags that can be cleared
by attributes, but not set by them. Also include flags that won't
get set properly in the assembler, but which the user/compiler
shouldn't be expected to set. */
flagword flags = SEC_READONLY | SEC_ALLOC | SEC_RELOC;
/* Initialize this with the default flags to be used if none are
specified. */
flagword default_flags = SEC_ALLOC | SEC_RELOC;
string = demand_copy_C_string (&xxx);
SKIP_WHITESPACE ();
if (*input_line_pointer != ',')
flags = default_flags;
while (*input_line_pointer == ',')
{
flagword bit;
int len, inv;
char *p, oldp;
input_line_pointer++;
if (*input_line_pointer != '#' && *input_line_pointer != '@')
{
as_bad ("unrecognized syntax in .section command");
ignore_rest_of_line ();
break;
}
input_line_pointer++;
#define CHECK(X,BIT,NEG) \
if (!strncmp(X,input_line_pointer,len = sizeof(X) - 1)) { \
bit = BIT; inv = NEG; goto match; }
CHECK ("write", SEC_READONLY, 1);
CHECK ("alloc", SEC_ALLOC, 0);
CHECK ("execinstr", SEC_CODE, 1);
#undef CHECK
p = input_line_pointer;
while (!is_end_of_line[*p] && *p != 0 && *p != ',')
p++;
*p = 0;
oldp = *p;
as_bad ("unrecognized section attribute `%s' ignored",
input_line_pointer);
*p = oldp;
continue;
match:
if (inv)
flags &= ~bit;
else
flags |= bit;
input_line_pointer += len;
}
demand_empty_rest_of_line ();
sec = bfd_get_section_by_name (stdoutput, string);
if (sec == 0)
{
sec = subseg_new (string, 0);
bfd_set_section_flags (stdoutput, sec, flags);
sec->output_section = sec;
}
subseg_set (sec, 0);
}
int
obj_elf_write_symbol_p (sym)
symbolS *sym;
{
/* If this is a local symbol, are there any relocations for which
need this symbol? */
/* To find this out, we examine all relocations in all bfd sections
that have relocations. If there is one that references this
symbol, we need to keep this symbol. In this case, we return a
true status. In all other cases, we return a false status. */
if (S_IS_LOCAL (sym))
{
asymbol *bsym = sym->bsym;
bfd *abfd = bsym->the_bfd;
asection *bsec;
for (bsec = abfd->sections; bsec; bsec = bsec->next)
{
struct reloc_cache_entry **rlocs = bsec->orelocation;
int rcnt = bsec->reloc_count;
if (rlocs)
{
int i;
for (i = 0; i < rcnt; i++)
if (rlocs[i]->sym_ptr_ptr
&& rlocs[i]->sym_ptr_ptr[0] == bsym)
return 1;
}
else
{
/* No relocations for this section. Check the seg_info
structure to see if there are any fixups for this
section. */
segment_info_type *seginfo = seg_info (bsec);
fixS *fixp;
for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
if ((fixp->fx_addsy && fixp->fx_addsy->bsym == bsym)
|| (fixp->fx_subsy && fixp->fx_subsy->bsym == bsym))
return 1;
}
}
}
return 0;
}
int
obj_elf_write_symbol (sym)
symbolS *sym;
{
return /* obj_elf_write_symbol_p (sym) || */ !S_IS_LOCAL (sym);
}
int
obj_elf_frob_symbol (sym, punt)
symbolS *sym;
int *punt;
{
return obj_elf_write_symbol_p (sym);
}
static void
obj_elf_line ()
{
/* Assume delimiter is part of expression. BSD4.2 as fails with
delightful bug, so we are not being incompatible here. */
new_logical_line ((char *) NULL, (int) (get_absolute_expression ()));
demand_empty_rest_of_line ();
}
/*
* stab()
*
* Handle .stabX directives, which used to be open-coded.
* So much creeping featurism overloaded the semantics that we decided
* to put all .stabX thinking in one place. Here.
*
* We try to make any .stabX directive legal. Other people's AS will often
* do assembly-time consistency checks: eg assigning meaning to n_type bits
* and "protecting" you from setting them to certain values. (They also zero
* certain bits before emitting symbols. Tut tut.)
*
* If an expression is not absolute we either gripe or use the relocation
* information. Other people's assemblers silently forget information they
* don't need and invent information they need that you didn't supply.
*
* .stabX directives always make a symbol table entry. It may be junk if
* the rest of your .stabX directive is malformed.
*/
/*
* elf_stab_symbol_string()
*
* Build a string dictionary entry for a .stabX symbol.
* The symbol is added to the .stabstr section.
*
*/
static unsigned int
elf_stab_symbol_string (string, secname)
char *string, *secname;
{
asection *save_seg;
asection *seg;
subsegT save_subseg;
unsigned int length;
unsigned int old_gdb_string_index;
char *clengthP;
int i;
char c;
/* @@FIXME -- there should be no static data here!
This also has the effect of making all stab string tables large enough
to contain all the contents written to any of them. This only matters
with the Solaris native compiler for the moment, but it should be fixed
anyways. */
static unsigned int gdb_string_index = 0;
old_gdb_string_index = 0;
length = strlen (string);
clengthP = (char *) &length;
if (length > 0)
{ /* Ordinary case. */
save_seg = now_seg;
save_subseg = now_subseg;
/* Create the stab sections, if they are not already created. */
{
char *newsecname = xmalloc (strlen (secname) + 4);
strcpy (newsecname, secname);
strcat (newsecname, "str");
seg = bfd_get_section_by_name (stdoutput, newsecname);
if (seg == 0)
{
seg = bfd_make_section_old_way (stdoutput, newsecname);
bfd_set_section_flags (stdoutput, seg, SEC_READONLY | SEC_ALLOC);
}
/* free (newsecname);*/
}
subseg_new ((char *) seg->name, save_subseg);
old_gdb_string_index = gdb_string_index;
i = 0;
while ((c = *string++))
{
i++;
gdb_string_index++;
FRAG_APPEND_1_CHAR (c);
}
{
FRAG_APPEND_1_CHAR ((char) 0);
i++;
gdb_string_index++;
}
while (i % 4 != 0)
{
FRAG_APPEND_1_CHAR ((char) 0);
i++;
gdb_string_index++;
}
subseg_new ((char *) save_seg->name, save_subseg);
}
return old_gdb_string_index;
}
static void
DEFUN (elf_stab_symbol, (symbolP, stab_type),
symbolS *symbolP AND
int stab_type)
{
char *toP;
toP = frag_more (8);
/* the string index portion of the stab */
md_number_to_chars (toP, (valueT) symbolP->sy_name_offset, 4);
md_number_to_chars (toP + 4, (valueT) S_GET_TYPE (symbolP), 1);
md_number_to_chars (toP + 5, (valueT) S_GET_OTHER (symbolP), 1);
md_number_to_chars (toP + 6, (valueT) S_GET_DESC (symbolP), 2);
/* The n_value field doesn't get written here, it gets done below. It
may be an expression needing relocating. */
}
static void
obj_elf_stab_generic (what, secname)
int what;
char *secname;
{
extern int listing;
symbolS *symbolP = 0;
char *string;
int saved_type = 0;
int length;
int goof = 0;
long longint;
asection *saved_seg = now_seg;
asection *seg;
subsegT subseg = now_subseg;
#if 1
/* This function doesn't work yet.
Actually, this function is okay, but some finalizations are needed
before writing the object file; that's not done yet, and the Solaris
linker chokes without it.
In any case, this should effectively disable it for now. */
if (what == 's')
demand_copy_C_string (&length);
s_ignore (69);
return;
#endif
seg = bfd_get_section_by_name (stdoutput, secname);
if (seg == 0)
{
seg = subseg_new (secname, 0);
bfd_set_section_flags (stdoutput, seg,
SEC_READONLY | SEC_ALLOC | SEC_RELOC);
subseg_set (saved_seg, subseg);
}
/*
* Enter with input_line_pointer pointing past .stabX and any following
* whitespace.
*/
if (what == 's')
{
string = demand_copy_C_string (&length);
SKIP_WHITESPACE ();
if (*input_line_pointer == ',')
input_line_pointer++;
else
{
as_bad ("I need a comma after symbol's name");
goof = 1;
}
}
else
string = "";
/*
* Input_line_pointer->after ','. String->symbol name.
*/
if (!goof)
{
symbolP = symbol_new (string, &bfd_und_section, (valueT) 0, (struct frag *) 0);
/* enter the string in the .stab string table (section .stabstr) */
symbolP->sy_name_offset = elf_stab_symbol_string (string, secname);
switch (what)
{
case 'd':
S_SET_NAME (symbolP, NULL); /* .stabd feature. */
S_SET_VALUE (symbolP,
(valueT) ((char*) obstack_next_free (&frags) - frag_now->fr_literal));
S_SET_SEGMENT (symbolP, now_seg);
symbolP->sy_frag = frag_now;
break;
case 'n':
symbolP->sy_frag = &zero_address_frag;
break;
case 's':
symbolP->sy_frag = &zero_address_frag;
break;
default:
BAD_CASE (what);
break;
}
if (get_absolute_expression_and_terminator (&longint) == ',')
{
saved_type = longint;
S_SET_TYPE (symbolP, saved_type);
}
else
{
as_bad ("I want a comma after the n_type expression");
goof = 1;
input_line_pointer--; /* Backup over a non-',' char. */
}
}
if (!goof)
{
if (get_absolute_expression_and_terminator (&longint) == ',')
S_SET_OTHER (symbolP, longint);
else
{
as_bad ("I want a comma after the n_other expression");
goof = 1;
input_line_pointer--; /* Backup over a non-',' char. */
}
}
if (!goof)
{
S_SET_DESC (symbolP, get_absolute_expression ());
if (what == 's' || what == 'n')
{
if (*input_line_pointer != ',')
{
as_bad ("I want a comma after the n_desc expression");
goof = 1;
}
else
{
input_line_pointer++;
}
}
}
if (!goof)
{
subseg_new ((char *) seg->name, subseg);
/* Emit the stab symbol. */
elf_stab_symbol (symbolP, what);
if (what == 's' || what == 'n')
{
cons (4);
input_line_pointer--;
}
else
{
char *p = frag_more (4);
md_number_to_chars (p, 0, 0);
}
subseg_new ((char *) saved_seg->name, subseg);
if ((what == 's' || what == 'n')
&& symbolP->sy_forward == NULL)
{
/* symbol is not needed in the regular symbol table */
symbol_remove (symbolP, &symbol_rootP, &symbol_lastP);
}
}
#ifndef NO_LISTING
if (listing && !goof)
switch (S_GET_TYPE (symbolP))
{
case N_SLINE:
listing_source_line (S_GET_DESC (symbolP));
break;
case N_SO:
case N_SOL:
listing_source_file (string);
break;
}
#endif
if (goof)
ignore_rest_of_line ();
else
demand_empty_rest_of_line ();
}
static void
obj_elf_stab (what)
int what;
{
obj_elf_stab_generic (what, ".stab");
}
static void
obj_elf_xstab (what)
int what;
{
int length;
char *secname;
secname = demand_copy_C_string (&length);
SKIP_WHITESPACE ();
if (*input_line_pointer == ',')
input_line_pointer++;
else
{
as_bad ("comma missing in .xstabs");
ignore_rest_of_line ();
return;
}
obj_elf_stab_generic (what, secname);
}
void
obj_elf_desc ()
{
char *name;
char c;
char *p;
symbolS *symbolP;
int temp;
/* Frob invented at RMS' request. Set the n_desc of a symbol. */
name = input_line_pointer;
c = get_symbol_end ();
p = input_line_pointer;
*p = c;
SKIP_WHITESPACE ();
if (*input_line_pointer != ',')
{
*p = 0;
as_bad ("Expected comma after name \"%s\"", name);
*p = c;
ignore_rest_of_line ();
}
else
{
input_line_pointer++;
temp = get_absolute_expression ();
*p = 0;
symbolP = symbol_find_or_make (name);
*p = c;
S_SET_DESC (symbolP, temp);
}
demand_empty_rest_of_line ();
} /* obj_elf_desc() */
void
obj_read_begin_hook ()
{
}
void
obj_symbol_new_hook (symbolP)
symbolS *symbolP;
{
#if 0 /* BFD already takes care of this */
elf32_symbol_type *esym = (elf32_symbol_type *) symbolP;
/* There is an Elf_Internal_Sym and an Elf_External_Sym. For now,
just zero them out. */
bzero ((char *) &esym->internal_elf_sym, sizeof (esym->internal_elf_sym));
bzero ((char *) &esym->native_elf_sym, sizeof (esym->native_elf_sym));
bzero ((char *) &esym->tc_data, sizeof (esym->tc_data));
#endif
}
void
obj_elf_version ()
{
char *name;
unsigned int c;
char ch;
char *p;
asection *seg = now_seg;
subsegT subseg = now_subseg;
Elf_Internal_Note i_note;
Elf_External_Note e_note;
asection *note_secp = (asection *) NULL;
int i, len;
SKIP_WHITESPACE ();
if (*input_line_pointer == '\"')
{
++input_line_pointer; /* -> 1st char of string. */
name = input_line_pointer;
while (is_a_char (c = next_char_of_string ()))
;
c = *input_line_pointer;
*input_line_pointer = '\0';
*(input_line_pointer - 1) = '\0';
*input_line_pointer = c;
/* create the .note section if this is the first version string */
note_secp = bfd_get_section_by_name (stdoutput, ".note");
if (note_secp == (asection *) NULL)
{
note_secp = bfd_make_section_old_way (stdoutput, ".note");
bfd_set_section_flags (stdoutput,
note_secp,
SEC_LOAD | SEC_ALLOC | SEC_HAS_CONTENTS);
}
/* process the version string */
subseg_new ((char *) note_secp->name, 0);
len = strlen (name);
i_note.namesz = ((len + 1) + 3) & ~3; /* round this to word boundary */
i_note.descsz = 0; /* no description */
i_note.type = NT_VERSION;
p = frag_more (sizeof (e_note.namesz));
md_number_to_chars (p, (valueT) i_note.namesz, 4);
p = frag_more (sizeof (e_note.descsz));
md_number_to_chars (p, (valueT) i_note.descsz, 4);
p = frag_more (sizeof (e_note.type));
md_number_to_chars (p, (valueT) i_note.type, 4);
for (i = 0; i < len; i++)
{
ch = *(name + i);
{
FRAG_APPEND_1_CHAR (ch);
}
}
frag_align (2, 0);
subseg_new ((char *) seg->name, subseg);
}
else
{
as_bad ("Expected \"-ed string");
}
demand_empty_rest_of_line ();
}
static void
obj_elf_size ()
{
char *name = input_line_pointer;
char c = get_symbol_end ();
char *p;
expressionS exp;
segT sec;
symbolS *sym;
p = input_line_pointer;
*p = c;
SKIP_WHITESPACE ();
if (*input_line_pointer != ',')
{
*p = 0;
as_bad ("expected comma after name `%s' in .size directive", name);
*p = c;
ignore_rest_of_line ();
return;
}
input_line_pointer++;
sec = expression (&exp);
if (sec == absent_section)
{
as_bad ("missing expression in .size directive");
exp.X_seg = absolute_section;
exp.X_add_number = 0;
}
*p = 0;
sym = symbol_find_or_make (name);
*p = c;
if (sec == absolute_section)
S_SET_SIZE (sym, exp.X_add_number);
else
{
#if 0
static int warned;
if (!warned)
{
as_tsktsk (".size expressions not yet supported, ignored");
warned++;
}
#endif
}
demand_empty_rest_of_line ();
}
static void
obj_elf_type ()
{
char *name = input_line_pointer;
char c = get_symbol_end ();
char *p;
int type = 0;
symbolS *sym;
p = input_line_pointer;
*p = c;
SKIP_WHITESPACE ();
if (*input_line_pointer != ',')
{
as_bad ("expected comma after name in .type directive");
egress:
ignore_rest_of_line ();
return;
}
input_line_pointer++;
SKIP_WHITESPACE ();
if (*input_line_pointer != '#')
{
as_bad ("expected `#' after comma in .type directive");
goto egress;
}
input_line_pointer++;
if (!strncmp ("function", input_line_pointer, sizeof ("function") - 1))
{
type = BSF_FUNCTION;
input_line_pointer += sizeof ("function") - 1;
}
else if (!strncmp ("object", input_line_pointer, sizeof ("object") - 1))
{
input_line_pointer += sizeof ("object") - 1;
}
else
{
as_bad ("unrecognized symbol type, ignored");
goto egress;
}
demand_empty_rest_of_line ();
*p = 0;
sym = symbol_find_or_make (name);
sym->bsym->flags |= type;
}
static void
obj_elf_ident ()
{
static segT comment_section;
segT old_section = now_seg;
int old_subsection = now_subseg;
if (!comment_section)
{
char *p;
comment_section = subseg_new (".comment", 0);
bfd_set_section_flags (stdoutput, comment_section, SEC_HAS_CONTENTS);
p = frag_more (1);
*p = 0;
}
else
subseg_set (comment_section, 0);
stringer (1);
subseg_set (old_section, old_subsection);
}
void
elf_frob_file ()
{
#ifdef elf_tc_symbol
int i;
for (i = 0; i < stdoutput->symcount; i++)
elf_tc_symbol (stdoutput, (elf_symbol_type *) (stdoutput->outsymbols[i]), i + 1);
#endif
#ifdef elf_tc_final_processing
elf_tc_final_processing_hook ();
#endif
/* Finally, we must make any target-specific sections. */
#ifdef elf_tc_make_sections
elf_tc_make_sections (stdoutput, NULL);
#endif
}
|