Don't add underscore prefix to amd64 symbols
[reactos.git] / reactos / tools / stubgen.c
1 #include <stdio.h>
2 #include <malloc.h>
3 #include <string.h>
4 #include <ctype.h>
5
6 #ifdef _WIN32
7 #define popen _popen
8 #define snprintf _snprintf
9 #endif
10
11 typedef struct _stub {
12 char *name;
13 char *origin;
14 struct _stub *next;
15 } stub;
16
17 void usage( char *name ) {
18 fprintf( stderr,
19 "Usage: %s [-n nm] [-m make] libs...\n"
20 "nm -- The command used to run nm on reactos objects\n"
21 "make -- The command used to build reactos\n\n"
22 "libs are import libraries (.a files) typically from\n"
23 "dk/lib/nkm and dk/lib/w32\n",
24 name );
25 }
26
27 int main( int argc, char **argv ) {
28 char line[1024];
29 char *make = "make";
30 char *nm = "nm";
31 char *origin = "unknown.a";
32 stub *functions = NULL, *new_f, *imports = NULL, *search;
33 FILE *make_f, *nm_f;
34 int i, libstart = argc;
35 FILE *out = fopen("tests/stubs.tst","w");
36
37 if( argc == 1 ) { usage(argv[0]); return 1; }
38
39 if( !out ) {
40 fprintf( stderr, "Could not write file tests/stubs.tst\n" );
41 return 1;
42 }
43
44 fprintf( out, "# Automatically generated by stubgen\n" );
45
46 for( i = 1; i < argc; i++ ) {
47 if( !strcmp( argv[i], "-m" ) ) {
48 make = argv[i+1];
49 i++;
50 } else if( !strcmp( argv[i], "-n" ) ) {
51 nm = argv[i+1];
52 i++;
53 } else { libstart = i; break; }
54 }
55
56 snprintf( line, sizeof(line), "%s test 2>&1", make );
57 make_f = popen( line, "r" );
58
59 if( !make_f ) {
60 fclose( out );
61 fprintf( stderr, "Could not run %s test\n", make );
62 return 1;
63 }
64
65 while( fgets( line, sizeof(line), make_f ) ) {
66 char *end_of_location;
67 char *begin_q, *end_q;
68
69 if( !strstr( line, "undefined reference to" ) ) continue;
70
71 end_of_location = strrchr( line, ':' );
72
73 if( !end_of_location ) continue;
74
75 begin_q = strchr( end_of_location, '`' );
76 end_q = strchr( end_of_location, '\'' );
77
78 if( !begin_q || !end_q ) continue;
79
80 begin_q += 2; /* skip `_ */
81
82 memmove( line, begin_q, end_q - begin_q );
83 line[end_q - begin_q] = 0;
84
85 for( new_f = functions; new_f; new_f = new_f->next )
86 if( !strcmp( new_f->name, line ) ) break;
87
88 if( new_f ) continue;
89
90 new_f = (stub *)malloc( sizeof(stub) );
91 if( !new_f )
92 {
93 fprintf( stderr, "Out of memory\n" );
94 fclose( out );
95 pclose( make_f );
96 return 1;
97 }
98
99 new_f->name = strdup( line );
100 new_f->next = functions;
101 functions = new_f;
102 }
103
104 /* Scan libraries and collect available import sections */
105 for( i = libstart; i < argc; i++ ) {
106 snprintf( line, sizeof(line), "%s %s", nm, argv[i] );
107 nm_f = popen( line, "r" );
108
109 for( origin = argv[i]; *argv[i]; argv[i]++ )
110 if( *argv[i] == '/' || *argv[i] == '\\' )
111 origin = argv[i] + 1;
112
113
114 if( !nm_f ) {
115 fprintf( stderr, "Could not run %s\n", line );
116 continue;
117 }
118
119 while( fgets( line, sizeof(line), nm_f ) ) {
120 char *import_sign, *eol;
121
122 if( !(import_sign = strstr( line, " I " )) ) continue;
123
124 import_sign += 3;
125 while( *import_sign && isspace(*import_sign) ) import_sign++;
126
127 /* Strip ws after name */
128 for( eol = import_sign; *eol && !isspace(*eol); eol++ );
129
130 *eol = 0;
131
132 for( new_f = imports; new_f; new_f = new_f->next )
133 if( !strcmp( new_f->name, import_sign ) ) break;
134
135 if( new_f ) continue;
136
137 new_f = (stub *)malloc( sizeof(stub) );
138 if( !new_f )
139 {
140 fprintf( stderr, "Out of memory\n" );
141 fclose( out );
142 pclose( make_f );
143 pclose( nm_f );
144 return 1;
145 }
146
147 new_f->name = strdup( import_sign + 1 );
148 new_f->origin = origin;
149 new_f->next = imports;
150 imports = new_f;
151 }
152
153 pclose( nm_f );
154 }
155
156 /* Now we have a list of unique functions and a list of imports,
157 lookup each function and output the entry from the import list. */
158 for( new_f = functions; new_f; new_f = new_f->next ) {
159 for( search = imports; search; search = search->next ) {
160 if( !strcmp( new_f->name, search->name ) ) {
161 fprintf( out, "%s %s\n", search->origin, search->name );
162 continue;
163 }
164 }
165 }
166
167 fclose( out );
168 pclose( make_f );
169 return 0;
170 }