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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
|
/* Describe symbol table of a rel file.
Copyright (C) 1986, 1988, 1990 Free Software Foundation, Inc.
This program 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 1, or (at your option)
any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <stdio.h>
#include <ar.h>
#include <sys/types.h>
#include <sys/file.h>
#include "getopt.h"
#if !defined(A_OUT) && !defined(MACH_O)
#define A_OUT
#endif
#ifdef A_OUT
#ifdef COFF_ENCAPSULATE
#include "a.out.encap.h"
#else
/* On native BSD systems, use the system's own a.out.h. */
#include <a.out.h>
#endif
#endif
#ifdef MACH_O
#ifndef A_OUT
#include <nlist.h>
#ifndef N_TEXT
#define N_TEXT 4
#define N_DATA 6
#define N_BSS 8
#endif
#ifndef N_FN
#define N_FN 15
#endif
#endif
#include <sys/loader.h>
#endif
/* Always use the GNU version of debugging symbol type codes, if possible. */
#include "stab.h"
/* Struct or union for header of object file. */
#ifdef USG
#include <string.h>
/* You might need to compile with -I/usr/include/sys if your fcntl.h
isn't in /usr/include (which is where it should be according to POSIX). */
#include <fcntl.h>
#else
#include <strings.h>
#endif
/* Alloca definitions and includes... */
/* If compiled with GNU C, use the built-in alloca */
#ifdef __GNUC__
#define alloca __builtin_alloca
#else
/* If on a sun 4, make sure to include the right definition. */
#if defined(sun) && defined(sparc)
#include "alloca.h"
#else
char *alloca ();
#endif
#endif
char *malloc (), *realloc ();
char *xmalloc (), *xrealloc ();
/* C++ demangler stuff. */
char *cplus_demangle ();
/* Print NAME on STREAM, demangling if necessary. */
void
fprint_name (stream, name)
FILE *stream;
char *name;
{
char *demangled = cplus_demangle (name);
if (demangled == NULL)
fputs (name, stream);
else
{
fputs (demangled, stream);
free (demangled);
}
}
/* Special global symbol types understood by GNU LD. */
/* The following type indicates the definition of a symbol as being
an indirect reference to another symbol. The other symbol
appears as an undefined reference, immediately following this symbol.
Indirection is asymmetrical. The other symbol's value will be used
to satisfy requests for the indirect symbol, but not vice versa.
If the other symbol does not have a definition, libraries will
be searched to find a definition. */
#ifndef N_INDR
#define N_INDR 0xa
#endif
/* Warning message symbol. The name of this symbol will be printed if
any symbols from its file are required by the linker. */
#ifndef N_WARNING
#define N_WARNING 0x1e
#endif
/* The following symbols refer to set elements.
All the N_SET[ATDB] symbols with the same name form one set.
Space is allocated for the set in the text section, and each set
element's value is stored into one word of the space.
The first word of the space is the length of the set (number of elements).
The address of the set is made into an N_SETV symbol
whose name is the same as the name of the set.
This symbol acts like a N_DATA global symbol
in that it can satisfy undefined external references. */
#ifndef N_SETA
#define N_SETA 0x14 /* Absolute set element symbol */
#endif /* This is input to LD, in a .o file. */
#ifndef N_SETT
#define N_SETT 0x16 /* Text set element symbol */
#endif /* This is input to LD, in a .o file. */
#ifndef N_SETD
#define N_SETD 0x18 /* Data set element symbol */
#endif /* This is input to LD, in a .o file. */
#ifndef N_SETB
#define N_SETB 0x1A /* Bss set element symbol */
#endif /* This is input to LD, in a .o file. */
/* Macros dealing with the set element symbols defined in a.out.h */
#define SET_ELEMENT_P(x) ((x)>=N_SETA&&(x)<=(N_SETB|N_EXT))
#define TYPE_OF_SET_ELEMENT(x) ((x)-N_SETA+N_ABS)
#ifndef N_SETV
#define N_SETV 0x1C /* Pointer to set vector in text area. */
#endif /* This is output from LD. */
#ifndef __GNU_STAB__
/* Line number for the data section. This is to be used to describe
the source location of a variable declaration. */
#ifndef N_DSLINE
#define N_DSLINE (N_SLINE+N_DATA-N_TEXT)
#endif
/* Line number for the bss section. This is to be used to describe
the source location of a variable declaration. */
#ifndef N_BSLINE
#define N_BSLINE (N_SLINE+N_BSS-N_TEXT)
#endif
#endif /* not __GNU_STAB__ */
/* Name of this program. */
char *program_name;
/* Number of input files specified. */
int number_of_files;
/* Current file's name. */
char *input_name;
/* Current member's name, or 0 if processing a non-library file. */
char *input_member;
#ifdef MACH_O
/* Section ordinals for N_SECT references. */
int text_section;
int data_section;
int bss_section;
#endif
/* Offset within archive of the current member,
if we are processing an archive. */
int member_offset;
/* Command options. */
int external_only; /* nonzero means print external symbols only. */
int sort_numerically; /* sort in numerical order rather than alphabetic */
int reverse_sort; /* sort in downward (alphabetic or numeric) order. */
int no_sort; /* don't sort; print symbols in order in the file. */
int undefined_only; /* print undefined symbols only. */
int file_on_each_line; /* print file name on each line. */
int debugger_syms; /* print the debugger-only symbols. */
int print_symdefs; /* describe the __.SYMDEF data in any archive file specified. */
/* The __.SYMDEF member of an archive has the following format:
1) A longword saying the size of the symdef data that follows
2) Zero or more struct symdef filling that many bytes
3) A longword saying how many bytes of strings follow
4) That many bytes of string data.
*/
struct symdef
{
long stringoffset; /* Offset of this symbol's name in the string data */
long offset; /* Offset in the archive of the header-data for the member that
defines this symbol. */
};
/* Create a table of debugging stab-codes and corresponding names. */
#ifdef __GNU_STAB__
#define __define_stab(NAME, CODE, STRING) {NAME, STRING},
struct {enum __stab_debug_code code; char *string;} stab_names[]
= {
#include "stab.def"
};
#undef __define_stab
#endif
char *concat ();
int decode_arg ();
void decode_switch ();
void do_one_file ();
void do_one_rel_file ();
void do_symdef_member ();
void error ();
void error_with_file ();
void fatal ();
void perror_name ();
void print_file_name ();
void
main (argc, argv)
char **argv;
int argc;
{
int i;
int c;
int ind;
static struct option long_options[] =
{
{"all", 0, &debugger_syms, 1},
{"extern-only", 0, &external_only, 1},
{"numeric-sort", 0, &sort_numerically, 1},
{"print-file-name", 0, &file_on_each_line,1},
{"no-sort", 0, &no_sort, 1},
{"reverse", 0, &reverse_sort, 1},
{"print-symdefs", 0, &print_symdefs, 1},
{"undefined-only",0, &undefined_only, 1},
{NULL, 0, NULL, 0}
};
program_name = argv[0];
number_of_files = 0;
external_only = 0;
sort_numerically = 0;
reverse_sort = 0;
no_sort = 0;
undefined_only = 0;
file_on_each_line = 0;
debugger_syms = 0;
print_symdefs = 0;
while ((c = getopt_long (argc, argv, "agnoprsu", long_options, &ind)) != EOF)
switch (c)
{
case 0:
break;
case 'a':
debugger_syms = 1;
break;
case 'g':
external_only = 1;
break;
case 'n':
sort_numerically = 1;
break;
case 'o':
file_on_each_line = 1;
break;
case 'p':
no_sort = 1;
break;
case 'r':
reverse_sort = 1;
break;
case 's':
print_symdefs = 1;
break;
case 'u':
undefined_only = 1;
break;
default:
fprintf (stderr, "\
Usage: %s [-agnoprsu] [+all] [+extern-only] [+numeric-sort]\n\
[+print-file-name] [+no-sort] [+reverse] [+print-symdefs]\n\
[+undefined-only] [file...]\n", program_name);
exit (1);
break;
}
number_of_files = argc - optind;
/* Now scan again and print the files. */
if (argc == optind)
do_one_file ("a.out");
else
for (i = optind; i < argc; i++)
do_one_file (argv[i]);
exit (0);
}
/* Print the filename of the current file on 'outfile' (a stdio stream). */
void
print_file_name (outfile)
FILE *outfile;
{
fprintf (outfile, "%s", input_name);
if (input_member)
fprintf (outfile, "(%s)", input_member);
}
/* process one input file */
void scan_library ();
void
do_one_file (name)
char *name;
{
int desc, nchars;
char armag[SARMAG];
desc = open (name, O_RDONLY, 0);
if (desc < 0)
{
perror_name (name);
return;
}
input_name = name;
input_member = 0;
nchars = read (desc, armag, SARMAG);
if (nchars == SARMAG && !strncmp(armag, ARMAG, SARMAG))
scan_library (desc);
else
do_one_rel_file (desc, 0);
close (desc);
}
/* Read in the archive data about one member.
SUBFILE_OFFSET is the address within the archive of the start of that data.
Return the length of the member's contents, which does
not include the archive data about the member.
A pointer to the member's name is stored into *MEMBER_NAME_PTR.
If there are no more valid members, return zero. */
int
decode_library_subfile (desc, subfile_offset, member_name_ptr)
int desc;
int subfile_offset;
char **member_name_ptr;
{
int bytes_read;
int namelen;
int member_length;
char *name;
struct ar_hdr hdr1;
lseek (desc, subfile_offset, 0);
bytes_read = read (desc, &hdr1, sizeof hdr1);
if (!bytes_read)
; /* end of archive */
else if (sizeof hdr1 != bytes_read)
error_with_file ("malformed library archive ");
else if (sscanf (hdr1.ar_size, "%d", &member_length) != 1)
error_with_file ("malformatted header of archive member in ");
else
{
for (namelen = 0; ; namelen++)
if (hdr1.ar_name[namelen] == 0 || hdr1.ar_name[namelen] == ' '
/* Some systems use a slash? Strange. */
|| hdr1.ar_name[namelen] == '/')
break;
name = (char *) xmalloc (namelen+1);
strncpy (name, hdr1.ar_name, namelen);
name[namelen] = 0;
*member_name_ptr = name;
return member_length;
}
return 0; /* tell caller to exit loop */
}
/* Scan a library and describe each member. */
void
scan_library (desc)
int desc;
{
int this_subfile_offset = SARMAG;
int member_length;
if (!file_on_each_line)
printf ("\n%s:\n", input_name);
while (1)
{
member_length
= decode_library_subfile (desc, this_subfile_offset, &input_member);
if (member_length == 0)
break;
/* Describe every member except the ranlib data if any. */
if (strcmp (input_member, "__.SYMDEF"))
do_one_rel_file (desc, this_subfile_offset + sizeof (struct ar_hdr));
else if (print_symdefs)
do_symdef_member (desc, this_subfile_offset + sizeof (struct ar_hdr), member_length);
this_subfile_offset += ((member_length + sizeof (struct ar_hdr)) + 1) & -2;
}
}
/* Read a file's header and fill in various pieces of information.
Return 0 on failure. */
int
read_header_info (desc, offset, syms_offset, syms_size, strs_offset, strs_size)
int desc;
long int offset;
long int *syms_offset;
unsigned int *syms_size;
long int *strs_offset;
unsigned int *strs_size;
{
int len;
#ifdef A_OUT
{
struct exec hdr;
lseek (desc, offset, 0);
#ifdef HEADER_SEEK_FD
/* Skip the headers that encapsulate our data in some other format
such as COFF. */
HEADER_SEEK_FD (desc);
#endif
len = read (desc, (char *) &hdr, sizeof (struct exec));
if (len == sizeof (struct exec) && !N_BADMAG (hdr))
{
*syms_offset = N_SYMOFF(hdr);
*syms_size = hdr.a_syms;
*strs_offset = N_STROFF(hdr);
lseek(desc, N_STROFF(hdr) + offset, 0);
if (read (desc, (char *) strs_size, sizeof *strs_size) != sizeof *strs_size)
{
error_with_file ("cannot read string table size in ");
return 0;
}
return 1;
}
}
#endif
#ifdef MACH_O
{
struct mach_header mach_header;
char *hdrbuf;
struct load_command *load_command;
struct segment_command *segment_command;
struct section *section;
struct symtab_command *symtab_command;
int symtab_seen;
int len, cmd, seg, ordinal;
symtab_seen = 0;
lseek (desc, offset, 0);
len = read (desc, (char *) &mach_header, sizeof (struct mach_header));
if (len == sizeof (struct mach_header) && mach_header.magic == MH_MAGIC)
{
hdrbuf = xmalloc (mach_header.sizeofcmds);
len = read (desc, hdrbuf, mach_header.sizeofcmds);
if (len != mach_header.sizeofcmds)
{
error_with_file ("failure reading Mach-O load commands in ");
return 0;
}
load_command = (struct load_command *) hdrbuf;
ordinal = 1;
for (cmd = 0; cmd < mach_header.ncmds; ++cmd)
{
switch (load_command->cmd)
{
case LC_SEGMENT:
segment_command = (struct segment_command *) load_command;
section = (struct section *) ((char *) (segment_command + 1));
for (seg = 0; seg < segment_command->nsects; ++seg, ++section, ++ordinal)
{
if (!strncmp(SECT_TEXT, section->sectname, sizeof section->sectname))
text_section = ordinal;
else if (!strncmp(SECT_DATA, section->sectname, sizeof section->sectname))
data_section = ordinal;
else if (!strncmp(SECT_BSS, section->sectname, sizeof section->sectname))
bss_section = ordinal;
}
break;
case LC_SYMTAB:
if (symtab_seen)
error_with_file ("more than one LC_SYMTAB in ");
else
{
symtab_seen = 1;
symtab_command = (struct symtab_command *) load_command;
*syms_offset = symtab_command->symoff;
*syms_size = symtab_command->nsyms * sizeof (struct nlist);
*strs_offset = symtab_command->stroff;
*strs_size = symtab_command->strsize;
}
break;
}
load_command = (struct load_command *)
((char *) load_command + load_command->cmdsize);
}
free (hdrbuf);
return 1;
}
}
#endif
return 0;
}
void print_symbols ();
void print_one_symbol ();
void read_header ();
int alphacompare ();
int valuecompare ();
int filter_symbols ();
void
do_one_rel_file (desc, offset)
int desc;
int offset;
{
struct nlist *symbols_and_strings;
int symcount;
int totalsize;
char *strings;
long int syms_offset, strs_offset;
unsigned int syms_size, strs_size;
if (!read_header_info (desc, offset, &syms_offset, &syms_size, &strs_offset, &strs_size))
{
error_with_file ("malformed input (not a rel file or archive) in ");
return;
}
/* Number of symbol entries in the file. */
symcount = syms_size / sizeof (struct nlist);
totalsize = strs_size + syms_size;
/* Allocate space for symbol entries and string table. */
symbols_and_strings = (struct nlist *) xmalloc (totalsize);
strings = (char *) symbols_and_strings + syms_size;
/* Read them both in. */
lseek (desc, syms_offset + offset, 0);
if (syms_size != read (desc, (char *) symbols_and_strings, syms_size))
{
error_with_file ("premature end of file in symbols of ");
return;
}
lseek (desc, strs_offset + offset, 0);
if (strs_size != read (desc, (char *) strings, strs_size))
{
error_with_file ("premature end of file in strings of ");
return;
}
/* Identify this file, if desired. */
if (!file_on_each_line && (number_of_files > 1 || input_member))
printf ("\n%s:\n", input_member ? input_member : input_name);
/* Discard the symbols we don't want to print; compact the rest down. */
symcount = filter_symbols (symbols_and_strings, symcount, strings);
/* Modify each symbol entry to point directly at the symbol name.
This is so the sort routine does not need to be passed
the value of `strings' separately. */
{
struct nlist *p = symbols_and_strings;
struct nlist *end = symbols_and_strings + symcount;
for (; p < end; p++)
{
/* A zero index means there is no string. */
if (p->n_un.n_strx != 0)
{
if (p->n_un.n_strx > 0 && p->n_un.n_strx < strs_size)
p->n_un.n_name = strings + p->n_un.n_strx;
else
{
error_with_file ("invalid string table offset in ");
return;
}
}
}
}
/* Sort the symbols if desired. */
if (!no_sort)
qsort (symbols_and_strings, symcount, sizeof (struct nlist),
sort_numerically ? valuecompare : alphacompare);
/* Print the symbols in the order they are now in. */
print_symbols (symbols_and_strings, symcount);
free (symbols_and_strings);
}
/* Choose which symbol entries to print;
compact them downward to get rid of the rest.
Return the number of symbols to be printed. */
int
filter_symbols (syms, symcount, strings)
struct nlist *syms;
int symcount;
char *strings;
{
struct nlist *from = syms, *to = syms;
struct nlist *end = syms + symcount;
while (from < end)
{
int keep = 0;
/* undefined sym or common sym */
if (from->n_type == N_EXT) keep = !undefined_only || !from->n_value;
/* global defined sym */
else if (from->n_type & N_EXT) keep = !undefined_only;
/* debugger sym: normally don't print */
else if (from->n_type & ~(N_TYPE | N_EXT)) keep = debugger_syms;
/* local sym */
else keep = !external_only && !undefined_only;
if (keep)
*to++ = *from;
from++;
}
return to - syms;
}
/* Comparison functions for sorting symbols. */
int
alphacompare (sym1, sym2)
struct nlist *sym1, *sym2;
{
if (reverse_sort)
{
if (!sym2->n_un.n_name)
{
if (sym1->n_un.n_name) return -1;
else return 0;
}
if (!sym1->n_un.n_name) return 1;
return strcmp (sym2->n_un.n_name, sym1->n_un.n_name);
}
else
{
if (!sym1->n_un.n_name)
{
if (sym2->n_un.n_name) return -1;
else return 0;
}
if (!sym2->n_un.n_name) return 1;
return strcmp (sym1->n_un.n_name, sym2->n_un.n_name);
}
}
int
valuecompare (sym1, sym2)
struct nlist *sym1, *sym2;
{
if (reverse_sort)
return sym2->n_value - sym1->n_value;
else
return sym1->n_value - sym2->n_value;
}
void
print_symbols (syms, symcount)
struct nlist *syms;
int symcount;
{
int i;
for (i = 0; i < symcount; i++)
print_one_symbol (&syms[i]);
}
void
print_one_symbol (sym)
struct nlist *sym;
{
if (file_on_each_line)
{
print_file_name (stdout);
printf (":");
}
if (undefined_only)
{
if (sym->n_type == N_EXT && !sym->n_value)
{
fprint_name (stdout, sym->n_un.n_name);
printf ("\n");
}
return;
}
if (sym->n_type & ~N_EXT || sym->n_value)
printf ("%08x ", sym->n_value);
else printf (" ");
switch (sym->n_type)
{
case N_EXT:
if (sym->n_value) printf ("C");
else printf ("U");
break;
case 0:
if (sym->n_value) printf ("c");
else printf ("u");
break;
case N_ABS | N_EXT:
printf ("A");
break;
case N_ABS:
printf ("a");
break;
case N_TEXT | N_EXT:
printf ("T");
break;
case N_TEXT:
printf ("t");
break;
case N_DATA | N_EXT:
printf ("D");
break;
case N_DATA:
printf ("d");
break;
case N_BSS | N_EXT:
printf ("B");
break;
case N_BSS:
printf ("b");
break;
case N_SETV | N_EXT:
printf ("V");
break;
case N_SETV:
printf ("v");
break;
case N_SETA | N_EXT:
printf ("L");
break;
case N_SETA:
printf ("l");
break;
case N_SETT | N_EXT:
printf ("X");
break;
case N_SETT:
printf ("x");
break;
case N_SETD | N_EXT:
printf ("Z");
break;
case N_SETD:
printf ("z");
break;
case N_SETB | N_EXT:
printf ("S");
break;
case N_SETB:
printf ("s");
break;
case N_INDR | N_EXT:
printf ("I");
break;
case N_INDR:
printf ("i");
break;
case N_WARNING | N_EXT:
printf ("W");
break;
case N_WARNING:
printf ("w");
break;
#ifdef N_SECT
case N_SECT:
if (sym->n_sect == text_section)
printf ("t");
else if (sym->n_sect == data_section)
printf ("d");
else if (sym->n_sect == bss_section)
printf ("b");
else
printf ("%d", sym->n_sect);
break;
case N_SECT | N_EXT:
if (sym->n_sect == text_section)
printf ("T");
else if (sym->n_sect == data_section)
printf ("D");
else if (sym->n_sect == bss_section)
printf ("B");
else
printf ("%d", sym->n_sect);
break;
#endif
default:
{
char *s;
int i;
#ifdef __GNU_STAB__
s = "";
for (i = sizeof (stab_names) / sizeof (stab_names[0]) - 1;
i >= 0; i--)
{
if (stab_names[i].code
== (enum __stab_debug_code) sym->n_type)
{
s = stab_names[i].string;
break;
}
}
#else /* not __GNU_STAB__ */
switch (sym->n_type)
{
case N_GSYM:
s = "GSYM";
break;
case N_FNAME:
s = "FNAME";
break;
case N_FUN:
s = "FUN";
break;
case N_STSYM:
s = "STSYM";
break;
case N_LCSYM:
s = "LCSYM";
break;
case N_RSYM:
s = "RSYM";
break;
case N_SLINE:
s = "SLINE";
break;
case N_DSLINE:
s = "DSLINE";
break;
case N_BSLINE:
s = "BSLINE";
break;
case N_SSYM:
s = "SSYM";
break;
case N_SO:
s = "SO";
break;
case N_LSYM:
s = "LSYM";
break;
case N_SOL:
s = "SOL";
break;
case N_PSYM:
s = "PSYM";
break;
case N_ENTRY:
s = "ENTRY";
break;
case N_LBRAC:
s = "LBRAC";
break;
case N_RBRAC:
s = "RBRAC";
break;
case N_BCOMM:
s = "BCOMM";
break;
case N_ECOMM:
s = "ECOMM";
break;
case N_ECOML:
s = "ECOML";
break;
case N_LENG:
s = "LENG";
break;
default:
s = "";
}
#endif /* not __GNU_STAB__ */
/* %x treats them as unsigned anyway, so if we didn't cast
them we'd get 0xffffffff for all 1's instead of 0xff
or 0xffff. */
printf ("- %02x %04x %5s",
#ifdef N_SECT
(unsigned char) sym->n_sect,
#else
(unsigned char) sym->n_other,
#endif
(unsigned short) sym->n_desc, s);
}
}
printf (" ");
if (sym->n_un.n_name)
fprint_name (stdout, sym->n_un.n_name);
printf ("\n");
}
void
do_symdef_member (desc, offset, member_length)
int desc;
int offset;
int member_length;
{
int symdef_size;
int nsymdefs;
struct symdef *symdefs;
int stringsize;
char *strings;
int i;
char *member_name;
int member_offset;
/* read the string-table-length out of the file */
lseek (desc, offset, 0);
if (sizeof symdef_size != read (desc, &symdef_size, sizeof symdef_size))
{
error_with_file ("premature eof in ");
return;
}
if (symdef_size < 0)
{
error_with_file ("invalid size value in ");
return;
}
nsymdefs = symdef_size / sizeof (struct symdef);
symdefs = (struct symdef *) alloca (symdef_size);
if (symdef_size != read (desc, symdefs, symdef_size))
{
error_with_file ("premature eof in ");
return;
}
if (stringsize < 0)
{
error_with_file ("invalid size value in ");
return;
}
if (sizeof stringsize != read (desc, &stringsize, sizeof stringsize))
{
error_with_file ("premature eof in ");
return;
}
strings = (char *) alloca (stringsize);
if (stringsize != read (desc, strings, stringsize))
{
error_with_file ("premature eof in ");
return;
}
if (stringsize + symdef_size + sizeof stringsize + sizeof symdef_size != member_length)
{
error_with_file ("size of data isn't what the data calls for in ");
return;
}
if (!file_on_each_line && (number_of_files > 1 || input_member))
printf ("\n%s:\n", input_member ? input_member : input_name);
member_offset = -1;
for (i = 0; i < nsymdefs; i++)
{
if (symdefs[i].stringoffset < 0 || symdefs[i].stringoffset >= stringsize)
{
error_with_file ("invalid entry in ");
return;
}
if (member_offset != symdefs[i].offset)
{
member_offset = symdefs[i].offset;
decode_library_subfile (desc, member_offset, &member_name);
}
if (file_on_each_line)
{
print_file_name (stdout);
printf (":");
}
printf ("%s in %s\n", symdefs[i].stringoffset + strings, member_name);
}
}
/* Report a fatal error.
STRING is a printf format string and ARG is one arg for it. */
void
fatal (string, arg)
char *string, *arg;
{
fprintf (stderr, "%s: ", program_name);
fprintf (stderr, string, arg);
fprintf (stderr, "\n");
exit (1);
}
/* Report a nonfatal error.
STRING is a printf format string and ARG is one arg for it. */
void
error (string, arg)
char *string, *arg;
{
fprintf (stderr, "%s: ", program_name);
fprintf (stderr, string, arg);
fprintf (stderr, "\n");
}
/* Report a nonfatal error.
STRING is printed, followed by the current file name. */
void
error_with_file (string)
char *string;
{
fprintf (stderr, "%s: ", program_name);
fprintf (stderr, string);
print_file_name (stderr);
fprintf (stderr, "\n");
}
/* Report a fatal error using the message for the last failed system call,
followed by the string NAME. */
void
perror_name (name)
char *name;
{
extern int errno, sys_nerr;
extern char *sys_errlist[];
char *s;
if (errno < sys_nerr)
s = concat ("", sys_errlist[errno], " for %s");
else
s = "cannot open %s";
error (s, name);
}
/* Like malloc but get fatal error if memory is exhausted. */
char *
xmalloc (size)
unsigned size;
{
char *result = malloc (size);
if (!result)
fatal ("virtual memory exhausted", 0);
return result;
}
/* Like realloc but get fatal error if out of memory. */
char *
xrealloc (p, size)
char *p;
unsigned size;
{
char *result = realloc(p, size);
if (!result)
fatal ("virtual memory exhausted", 0);
return result;
}
/* Return a newly-allocated string
whose contents concatenate those of S1, S2, S3. */
char *
concat (s1, s2, s3)
char *s1, *s2, *s3;
{
int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
strcpy (result, s1);
strcpy (result + len1, s2);
strcpy (result + len1 + len2, s3);
result[len1 + len2 + len3] = 0;
return result;
}
#ifdef USG
getpagesize ()
{
return (4096);
}
#endif
|