blob: a72f8e678f56c8cfb352d37a05566b1e01daf5b0 [file] [log] [blame]
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001/*
2 * File stabs.c - read stabs information from the wine executable itself.
3 *
4 * Copyright (C) 1996, Eric Youngdale.
5 */
6
Marcus Meissner592ba101999-01-20 14:18:55 +00007#include "config.h"
8
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +00009#include <sys/types.h>
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +000010#include <fcntl.h>
11#include <sys/stat.h>
Alexandre Julliardf0cbfa01997-02-15 14:29:56 +000012#include <sys/mman.h>
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +000013#include <limits.h>
14#include <stdio.h>
Alexandre Julliardc6c09441997-01-12 18:32:19 +000015#include <stdlib.h>
16#include <string.h>
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +000017#include <unistd.h>
Alexandre Julliard349a9531997-02-02 19:01:52 +000018#ifndef PATH_MAX
19#define PATH_MAX _MAX_PATH
20#endif
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +000021
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +000022#include "debugger.h"
Alexandre Julliardc6c09441997-01-12 18:32:19 +000023
Patrik Stridvalla9a671d1999-04-25 19:01:52 +000024#if defined(__svr4__) || defined(__sun)
Alexandre Julliardc6c09441997-01-12 18:32:19 +000025#define __ELF__
26#endif
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +000027
28#ifdef __ELF__
Marcus Meissner592ba101999-01-20 14:18:55 +000029#ifdef HAVE_ELF_H
30# include <elf.h>
31#endif
Alexandre Julliardc6c09441997-01-12 18:32:19 +000032#include <link.h>
Alexandre Julliard349a9531997-02-02 19:01:52 +000033#include <sys/mman.h>
34#elif defined(__EMX__)
35#include <a_out.h>
Alexandre Julliardc6c09441997-01-12 18:32:19 +000036#else
37#include <a.out.h>
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +000038#endif
39
Alexandre Julliardc6c09441997-01-12 18:32:19 +000040#ifndef N_UNDF
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +000041#define N_UNDF 0x00
Alexandre Julliardc6c09441997-01-12 18:32:19 +000042#endif
43
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +000044#define N_GSYM 0x20
45#define N_FUN 0x24
46#define N_STSYM 0x26
47#define N_LCSYM 0x28
48#define N_MAIN 0x2a
49#define N_ROSYM 0x2c
50#define N_OPT 0x3c
51#define N_RSYM 0x40
52#define N_SLINE 0x44
53#define N_SO 0x64
54#define N_LSYM 0x80
55#define N_BINCL 0x82
56#define N_SOL 0x84
57#define N_PSYM 0xa0
58#define N_EINCL 0xa2
59#define N_LBRAC 0xc0
60#define N_RBRAC 0xe0
61
62
63/*
Alexandre Julliardc6c09441997-01-12 18:32:19 +000064 * This is how we translate stab types into our internal representations
65 * of datatypes.
66 */
67static struct datatype ** stab_types = NULL;
68static int num_stab_types = 0;
69
70/*
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +000071 * Set so that we know the main executable name and path.
72 */
73char * DEBUG_argv0;
74
75struct stab_nlist {
76 union {
77 char *n_name;
78 struct stab_nlist *n_next;
79 long n_strx;
80 } n_un;
81 unsigned char n_type;
82 char n_other;
83 short n_desc;
84 unsigned long n_value;
85};
86
Alexandre Julliard01d63461997-01-20 19:43:45 +000087/*
88 * This is used to keep track of known datatypes so that we don't redefine
89 * them over and over again. It sucks up lots of memory otherwise.
90 */
91struct known_typedef
92{
93 struct known_typedef * next;
94 char * name;
95 int ndefs;
96 struct datatype * types[0];
97};
98
99#define NR_STAB_HASH 521
100
Alexandre Julliard60ce85c1998-02-01 18:33:27 +0000101struct known_typedef * ktd_head[NR_STAB_HASH] = {NULL,};
Alexandre Julliard01d63461997-01-20 19:43:45 +0000102
103static unsigned int stab_hash( const char * name )
104{
105 unsigned int hash = 0;
106 unsigned int tmp;
107 const char * p;
108
109 p = name;
110
111 while (*p)
112 {
113 hash = (hash << 4) + *p++;
114
115 if( (tmp = (hash & 0xf0000000)) )
116 {
117 hash ^= tmp >> 24;
118 }
119 hash &= ~tmp;
120 }
121 return hash % NR_STAB_HASH;
122}
123
124
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000125static void stab_strcpy(char * dest, const char * source)
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000126{
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000127 /*
128 * A strcpy routine that stops when we hit the ':' character.
129 * Faster than copying the whole thing, and then nuking the
130 * ':'.
131 */
132 while(*source != '\0' && *source != ':')
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000133 *dest++ = *source++;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000134 *dest++ = '\0';
135}
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000136
Alexandre Julliard01d63461997-01-20 19:43:45 +0000137#define MAX_TD_NESTING 128
138
Alexandre Julliarda845b881998-06-01 10:44:35 +0000139static int **typenums;
140static int *nroftypenums=NULL;
141static int nrofnroftypenums=0;
142static int curtypenum = 0;
143
144static
145int
146DEBUG_FileSubNr2StabEnum(int filenr,int subnr) {
147 if (nrofnroftypenums<=filenr) {
Ove Kaavendda17c61999-04-25 12:24:42 +0000148 nroftypenums = DBG_realloc(nroftypenums,sizeof(nroftypenums[0])*(filenr+1));
Alexandre Julliarda845b881998-06-01 10:44:35 +0000149 memset(nroftypenums+nrofnroftypenums,0,(filenr+1-nrofnroftypenums)*sizeof(nroftypenums[0]));
Ove Kaavendda17c61999-04-25 12:24:42 +0000150 typenums = DBG_realloc(typenums,sizeof(typenums[0])*(filenr+1));
Alexandre Julliarda845b881998-06-01 10:44:35 +0000151 memset(typenums+nrofnroftypenums,0,sizeof(typenums[0])*(filenr+1-nrofnroftypenums));
152 nrofnroftypenums=filenr+1;
153 }
154 if (nroftypenums[filenr]<=subnr) {
Ove Kaavendda17c61999-04-25 12:24:42 +0000155 typenums[filenr] = DBG_realloc(typenums[filenr],sizeof(typenums[0][0])*(subnr+1));
Alexandre Julliarda845b881998-06-01 10:44:35 +0000156 memset(typenums[filenr]+nroftypenums[filenr],0,sizeof(typenums[0][0])*(subnr+1-nroftypenums[filenr]));
157 nroftypenums[filenr] = subnr+1;
158 }
159 if (!typenums[filenr][subnr])
160 typenums[filenr][subnr]=++curtypenum;
161
162 if( num_stab_types <= curtypenum ) {
163 num_stab_types = curtypenum + 256;
Ove Kaavendda17c61999-04-25 12:24:42 +0000164 stab_types = (struct datatype **) DBG_realloc(stab_types,
Alexandre Julliarda845b881998-06-01 10:44:35 +0000165 num_stab_types * sizeof(struct datatype *)
166 );
Ove Kaaven7e5b3b31998-10-11 12:13:42 +0000167 memset( stab_types + curtypenum, 0, sizeof(struct datatype *) * (num_stab_types - curtypenum) );
Alexandre Julliarda845b881998-06-01 10:44:35 +0000168 }
Marcus Meissner73458b01998-12-26 12:54:29 +0000169 /*fprintf(stderr,"(%d,%d) is %d\n",filenr,subnr,typenums[filenr][subnr]); */
Alexandre Julliarda845b881998-06-01 10:44:35 +0000170 return typenums[filenr][subnr];
171}
172
173static
174int
175DEBUG_ReadTypeEnumBackwards(char*x) {
176 int filenr,subnr;
177
178 if (*x==')') {
179 while (*x!='(')
180 x--;
181 x++; /* '(' */
182 filenr=strtol(x,&x,10); /* <int> */
Alexandre Julliardf90efa91998-06-14 15:24:15 +0000183 x++; /* ',' */
Alexandre Julliarda845b881998-06-01 10:44:35 +0000184 subnr=strtol(x,&x,10); /* <int> */
Alexandre Julliardf90efa91998-06-14 15:24:15 +0000185 x++; /* ')' */
Alexandre Julliarda845b881998-06-01 10:44:35 +0000186 } else {
187 while ((*x>='0') && (*x<='9'))
188 x--;
189 filenr = 0;
190 subnr = atol(x+1);
191 }
192 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
193}
194
195static
196int
197DEBUG_ReadTypeEnum(char **x) {
198 int filenr,subnr;
199
200 if (**x=='(') {
201 (*x)++; /* '(' */
202 filenr=strtol(*x,x,10); /* <int> */
203 (*x)++; /* ',' */
204 subnr=strtol(*x,x,10); /* <int> */
205 (*x)++; /* ')' */
206 } else {
207 filenr = 0;
208 subnr = strtol(*x,x,10); /* <int> */
209 }
210 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
211}
212
Alexandre Julliard01d63461997-01-20 19:43:45 +0000213static
214int
215DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef)
216{
217 int hash;
218 struct known_typedef * ktd;
219
220 if( ndef == 1 )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000221 return TRUE;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000222
Ove Kaavendda17c61999-04-25 12:24:42 +0000223 ktd = (struct known_typedef *) DBG_alloc(sizeof(struct known_typedef)
Alexandre Julliarda845b881998-06-01 10:44:35 +0000224 + ndef * sizeof(struct datatype *));
Alexandre Julliard01d63461997-01-20 19:43:45 +0000225
226 hash = stab_hash(name);
227
Ove Kaavendda17c61999-04-25 12:24:42 +0000228 ktd->name = DBG_strdup(name);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000229 ktd->ndefs = ndef;
230 memcpy(&ktd->types[0], types, ndef * sizeof(struct datatype *));
231 ktd->next = ktd_head[hash];
232 ktd_head[hash] = ktd;
233
234 return TRUE;
235}
236
237static
238int
239DEBUG_HandlePreviousTypedef(const char * name, const char * stab)
240{
241 int count;
242 enum debug_type expect;
243 int hash;
244 struct known_typedef * ktd;
245 char * ptr;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000246
247 hash = stab_hash(name);
248
249 for(ktd = ktd_head[hash]; ktd; ktd = ktd->next)
Alexandre Julliarda845b881998-06-01 10:44:35 +0000250 if ((ktd->name[0] == name[0]) && (strcmp(name, ktd->name) == 0) )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000251 break;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000252
253 /*
254 * Didn't find it. This must be a new one.
255 */
256 if( ktd == NULL )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000257 return FALSE;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000258
259 /*
260 * Examine the stab to make sure it has the same number of definitions.
261 */
262 count = 0;
263 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
264 {
265 if( count >= ktd->ndefs )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000266 return FALSE;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000267
268 /*
269 * Make sure the types of all of the objects is consistent with
270 * what we have already parsed.
271 */
272 switch(ptr[1])
273 {
274 case '*':
François Gouget241c7301998-10-28 10:47:09 +0000275 expect = DT_POINTER;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000276 break;
277 case 's':
278 case 'u':
François Gouget241c7301998-10-28 10:47:09 +0000279 expect = DT_STRUCT;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000280 break;
281 case 'a':
François Gouget241c7301998-10-28 10:47:09 +0000282 expect = DT_ARRAY;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000283 break;
284 case '1':
Alexandre Julliarda845b881998-06-01 10:44:35 +0000285 case '(':
Alexandre Julliard01d63461997-01-20 19:43:45 +0000286 case 'r':
François Gouget241c7301998-10-28 10:47:09 +0000287 expect = DT_BASIC;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000288 break;
289 case 'x':
François Gouget241c7301998-10-28 10:47:09 +0000290 expect = DT_STRUCT;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000291 break;
292 case 'e':
François Gouget241c7301998-10-28 10:47:09 +0000293 expect = DT_ENUM;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000294 break;
295 case 'f':
François Gouget241c7301998-10-28 10:47:09 +0000296 expect = DT_FUNC;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000297 break;
298 default:
Alexandre Julliarda845b881998-06-01 10:44:35 +0000299 fprintf(stderr, "Unknown type (%c).\n",ptr[1]);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000300 return FALSE;
301 }
302 if( expect != DEBUG_GetType(ktd->types[count]) )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000303 return FALSE;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000304 count++;
305 }
306
307 if( ktd->ndefs != count )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000308 return FALSE;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000309
310 /*
Alexandre Julliarda845b881998-06-01 10:44:35 +0000311 * Go through, dig out all of the type numbers, and substitute the
312 * appropriate things.
Alexandre Julliard01d63461997-01-20 19:43:45 +0000313 */
314 count = 0;
315 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
Alexandre Julliarda845b881998-06-01 10:44:35 +0000316 stab_types[DEBUG_ReadTypeEnumBackwards(ptr-1)] = ktd->types[count++];
Alexandre Julliard01d63461997-01-20 19:43:45 +0000317
318 return TRUE;
319}
320
321static int DEBUG_FreeRegisteredTypedefs()
322{
323 int count;
324 int j;
325 struct known_typedef * ktd;
326 struct known_typedef * next;
327
328 count = 0;
329 for(j=0; j < NR_STAB_HASH; j++ )
330 {
331 for(ktd = ktd_head[j]; ktd; ktd = next)
332 {
333 count++;
334 next = ktd->next;
Ove Kaavendda17c61999-04-25 12:24:42 +0000335 DBG_free(ktd->name);
336 DBG_free(ktd);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000337 }
338 ktd_head[j] = NULL;
339 }
340
341 return TRUE;
342
343}
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000344
345static
346int
347DEBUG_ParseTypedefStab(char * ptr, const char * typename)
348{
349 int arrmax;
350 int arrmin;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000351 char * c;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000352 struct datatype * curr_type;
353 struct datatype * datatype;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000354 struct datatype * curr_types[MAX_TD_NESTING];
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000355 char element_name[1024];
Alexandre Julliard01d63461997-01-20 19:43:45 +0000356 int ntypes = 0;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000357 int offset;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000358 const char * orig_typename;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000359 int size;
360 char * tc;
361 char * tc2;
362 int typenum;
363
Alexandre Julliard01d63461997-01-20 19:43:45 +0000364 orig_typename = typename;
365
Alexandre Julliarda845b881998-06-01 10:44:35 +0000366 if( DEBUG_HandlePreviousTypedef(typename, ptr) )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000367 return TRUE;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000368
369 /*
370 * Go from back to front. First we go through and figure out what
371 * type numbers we need, and register those types. Then we go in
372 * and fill the details.
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000373 */
Alexandre Julliard01d63461997-01-20 19:43:45 +0000374
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000375 for( c = strchr(ptr, '='); c != NULL; c = strchr(c + 1, '=') )
376 {
377 /*
378 * Back up until we get to a non-numeric character. This is the type
379 * number.
380 */
Alexandre Julliarda845b881998-06-01 10:44:35 +0000381 typenum = DEBUG_ReadTypeEnumBackwards(c-1);
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000382
Alexandre Julliard01d63461997-01-20 19:43:45 +0000383 if( ntypes >= MAX_TD_NESTING )
384 {
385 /*
386 * If this ever happens, just bump the counter.
387 */
388 fprintf(stderr, "Typedef nesting overflow\n");
389 return FALSE;
390 }
391
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000392 switch(c[1])
393 {
394 case '*':
François Gouget241c7301998-10-28 10:47:09 +0000395 stab_types[typenum] = DEBUG_NewDataType(DT_POINTER, NULL);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000396 curr_types[ntypes++] = stab_types[typenum];
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000397 break;
398 case 's':
399 case 'u':
François Gouget241c7301998-10-28 10:47:09 +0000400 stab_types[typenum] = DEBUG_NewDataType(DT_STRUCT, typename);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000401 curr_types[ntypes++] = stab_types[typenum];
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000402 break;
403 case 'a':
François Gouget241c7301998-10-28 10:47:09 +0000404 stab_types[typenum] = DEBUG_NewDataType(DT_ARRAY, NULL);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000405 curr_types[ntypes++] = stab_types[typenum];
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000406 break;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000407 case '(':
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000408 case '1':
409 case 'r':
François Gouget241c7301998-10-28 10:47:09 +0000410 stab_types[typenum] = DEBUG_NewDataType(DT_BASIC, typename);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000411 curr_types[ntypes++] = stab_types[typenum];
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000412 break;
413 case 'x':
414 stab_strcpy(element_name, c + 3);
François Gouget241c7301998-10-28 10:47:09 +0000415 stab_types[typenum] = DEBUG_NewDataType(DT_STRUCT, element_name);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000416 curr_types[ntypes++] = stab_types[typenum];
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000417 break;
418 case 'e':
François Gouget241c7301998-10-28 10:47:09 +0000419 stab_types[typenum] = DEBUG_NewDataType(DT_ENUM, NULL);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000420 curr_types[ntypes++] = stab_types[typenum];
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000421 break;
422 case 'f':
François Gouget241c7301998-10-28 10:47:09 +0000423 stab_types[typenum] = DEBUG_NewDataType(DT_FUNC, NULL);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000424 curr_types[ntypes++] = stab_types[typenum];
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000425 break;
426 default:
Alexandre Julliarda845b881998-06-01 10:44:35 +0000427 fprintf(stderr, "Unknown type (%c).\n",c[1]);
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000428 }
429 typename = NULL;
430 }
431
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000432 /*
Alexandre Julliard01d63461997-01-20 19:43:45 +0000433 * Now register the type so that if we encounter it again, we will know
434 * what to do.
435 */
436 DEBUG_RegisterTypedef(orig_typename, curr_types, ntypes);
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000437
Alexandre Julliard01d63461997-01-20 19:43:45 +0000438 /*
439 * OK, now take a second sweep through. Now we will be digging
440 * out the definitions of the various components, and storing
441 * them in the skeletons that we have already allocated. We take
442 * a right-to left search as this is much easier to parse.
443 */
444 for( c = strrchr(ptr, '='); c != NULL; c = strrchr(ptr, '=') )
445 {
Alexandre Julliarda845b881998-06-01 10:44:35 +0000446 int typenum = DEBUG_ReadTypeEnumBackwards(c-1);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000447 curr_type = stab_types[typenum];
448
449 switch(c[1])
450 {
451 case 'x':
452 tc = c + 3;
453 while( *tc != ':' )
Alexandre Julliarda845b881998-06-01 10:44:35 +0000454 tc++;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000455 tc++;
456 if( *tc == '\0' )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000457 *c = '\0';
Alexandre Julliard01d63461997-01-20 19:43:45 +0000458 else
Alexandre Julliard01d63461997-01-20 19:43:45 +0000459 strcpy(c, tc);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000460 break;
461 case '*':
462 case 'f':
463 tc = c + 2;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000464 datatype = stab_types[DEBUG_ReadTypeEnum(&tc)];
Alexandre Julliard01d63461997-01-20 19:43:45 +0000465 DEBUG_SetPointerType(curr_type, datatype);
466 if( *tc == '\0' )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000467 *c = '\0';
Alexandre Julliard01d63461997-01-20 19:43:45 +0000468 else
Alexandre Julliard01d63461997-01-20 19:43:45 +0000469 strcpy(c, tc);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000470 break;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000471 case '(':
Alexandre Julliard01d63461997-01-20 19:43:45 +0000472 case '1':
473 case 'r':
474 /*
475 * We have already handled these above.
476 */
477 *c = '\0';
478 break;
479 case 'a':
Alexandre Julliarda845b881998-06-01 10:44:35 +0000480 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */
481
482 tc = c + 3;
Alexandre Julliardf90efa91998-06-14 15:24:15 +0000483 /* 'r' */
Alexandre Julliarda845b881998-06-01 10:44:35 +0000484 DEBUG_ReadTypeEnum(&tc);
Alexandre Julliardf90efa91998-06-14 15:24:15 +0000485 tc++; /* ';' */
Alexandre Julliarda845b881998-06-01 10:44:35 +0000486 arrmin = strtol(tc, &tc, 10); /* <int> */
Alexandre Julliardf90efa91998-06-14 15:24:15 +0000487 tc++; /* ';' */
Alexandre Julliarda845b881998-06-01 10:44:35 +0000488 arrmax = strtol(tc, &tc, 10); /* <int> */
Alexandre Julliardf90efa91998-06-14 15:24:15 +0000489 tc++; /* ';' */
Alexandre Julliarda845b881998-06-01 10:44:35 +0000490 datatype = stab_types[DEBUG_ReadTypeEnum(&tc)]; /* <typeinfo> */
Alexandre Julliard01d63461997-01-20 19:43:45 +0000491 if( *tc == '\0' )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000492 *c = '\0';
Alexandre Julliard01d63461997-01-20 19:43:45 +0000493 else
Alexandre Julliard01d63461997-01-20 19:43:45 +0000494 strcpy(c, tc);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000495 DEBUG_SetArrayParams(curr_type, arrmin, arrmax, datatype);
496 break;
497 case 's':
Alexandre Julliarda845b881998-06-01 10:44:35 +0000498 case 'u': {
499 int failure = 0;
500
Alexandre Julliard01d63461997-01-20 19:43:45 +0000501 tc = c + 2;
502 if( DEBUG_SetStructSize(curr_type, strtol(tc, &tc, 10)) == FALSE )
503 {
504 /*
505 * We have already filled out this structure. Nothing to do,
506 * so just skip forward to the end of the definition.
507 */
508 while( tc[0] != ';' && tc[1] != ';' )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000509 tc++;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000510
511 tc += 2;
512
513 if( *tc == '\0' )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000514 *c = '\0';
Alexandre Julliard01d63461997-01-20 19:43:45 +0000515 else
Alexandre Julliard01d63461997-01-20 19:43:45 +0000516 strcpy(c, tc + 1);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000517 continue;
518 }
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000519
Alexandre Julliard01d63461997-01-20 19:43:45 +0000520 /*
521 * Now parse the individual elements of the structure/union.
522 */
523 while(*tc != ';')
524 {
Alexandre Julliarda845b881998-06-01 10:44:35 +0000525 char *ti;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000526 tc2 = element_name;
527 while(*tc != ':')
Alexandre Julliard01d63461997-01-20 19:43:45 +0000528 *tc2++ = *tc++;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000529 tc++;
530 *tc2++ = '\0';
Alexandre Julliarda845b881998-06-01 10:44:35 +0000531 ti=tc;
532 datatype = stab_types[DEBUG_ReadTypeEnum(&tc)];
533 *tc='\0';
Alexandre Julliard01d63461997-01-20 19:43:45 +0000534 tc++;
535 offset = strtol(tc, &tc, 10);
536 tc++;
537 size = strtol(tc, &tc, 10);
538 tc++;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000539 if (datatype)
540 DEBUG_AddStructElement(curr_type, element_name, datatype, offset, size);
541 else {
542 failure = 1;
543 /* ... but proceed parsing to the end of the stab */
544 }
Alexandre Julliard01d63461997-01-20 19:43:45 +0000545 }
Alexandre Julliarda845b881998-06-01 10:44:35 +0000546
547 if (failure) {
548 /* if we had a undeclared value this one is undeclared too.
549 * remove it from the stab_types.
550 * I just set it to NULL to detect bugs in my thoughtprocess.
551 * FIXME: leaks the memory for the structure elements.
552 * FIXME: such structures should have been optimized away
553 * by ld.
554 */
555 stab_types[typenum] = NULL;
556 }
Alexandre Julliard01d63461997-01-20 19:43:45 +0000557 if( *tc == '\0' )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000558 *c = '\0';
Alexandre Julliard01d63461997-01-20 19:43:45 +0000559 else
Alexandre Julliard01d63461997-01-20 19:43:45 +0000560 strcpy(c, tc + 1);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000561 break;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000562 }
Alexandre Julliard01d63461997-01-20 19:43:45 +0000563 case 'e':
564 tc = c + 2;
565 /*
566 * Now parse the individual elements of the structure/union.
567 */
568 while(*tc != ';')
569 {
570 tc2 = element_name;
571 while(*tc != ':')
Alexandre Julliard01d63461997-01-20 19:43:45 +0000572 *tc2++ = *tc++;
Alexandre Julliard01d63461997-01-20 19:43:45 +0000573 tc++;
574 *tc2++ = '\0';
575 offset = strtol(tc, &tc, 10);
576 tc++;
577 DEBUG_AddStructElement(curr_type, element_name, NULL, offset, 0);
578 }
579 if( *tc == '\0' )
Alexandre Julliard01d63461997-01-20 19:43:45 +0000580 *c = '\0';
Alexandre Julliard01d63461997-01-20 19:43:45 +0000581 else
Alexandre Julliard01d63461997-01-20 19:43:45 +0000582 strcpy(c, tc + 1);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000583 break;
584 default:
Alexandre Julliarda845b881998-06-01 10:44:35 +0000585 fprintf(stderr, "Unknown type (%c).\n",c[1]);
Alexandre Julliard01d63461997-01-20 19:43:45 +0000586 break;
587 }
588 }
589
Alexandre Julliarda845b881998-06-01 10:44:35 +0000590 return TRUE;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000591
592}
593
594static struct datatype *
595DEBUG_ParseStabType(const char * stab)
596{
597 char * c;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000598
599 /*
600 * Look through the stab definition, and figure out what datatype
601 * this represents. If we have something we know about, assign the
602 * type.
603 */
604 c = strchr(stab, ':');
605 if( c == NULL )
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000606 return NULL;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000607
608 c++;
609 /*
610 * The next character says more about the type (i.e. data, function, etc)
611 * of symbol. Skip it.
612 */
613 c++;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000614 /*
615 * The next is either an integer or a (integer,integer).
616 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
617 */
618 return stab_types[DEBUG_ReadTypeEnum(&c)];
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000619}
620
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000621int
622DEBUG_ParseStabs(char * addr, unsigned int load_offset,
623 unsigned int staboff, int stablen,
624 unsigned int strtaboff, int strtablen)
625{
626 struct name_hash * curr_func = NULL;
627 struct wine_locals * curr_loc = NULL;
628 struct name_hash * curr_sym = NULL;
629 char currpath[PATH_MAX];
630 int i;
631 int ignore = FALSE;
632 int last_nso = -1;
633 int len;
634 DBG_ADDR new_addr;
635 int nstab;
636 char * ptr;
637 char * stabbuff;
638 int stabbufflen;
639 struct stab_nlist * stab_ptr;
640 char * strs;
641 int strtabinc;
642 char * subpath = NULL;
643 char symname[4096];
644
645 nstab = stablen / sizeof(struct stab_nlist);
646 stab_ptr = (struct stab_nlist *) (addr + staboff);
647 strs = (char *) (addr + strtaboff);
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000648
649 memset(currpath, 0, sizeof(currpath));
650
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000651 /*
652 * Allocate a buffer into which we can build stab strings for cases
653 * where the stab is continued over multiple lines.
654 */
655 stabbufflen = 65536;
Ove Kaavendda17c61999-04-25 12:24:42 +0000656 stabbuff = (char *) DBG_alloc(stabbufflen);
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000657
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000658 strtabinc = 0;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000659 stabbuff[0] = '\0';
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000660 for(i=0; i < nstab; i++, stab_ptr++ )
661 {
662 ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000663 if( ptr[strlen(ptr) - 1] == '\\' )
664 {
665 /*
666 * Indicates continuation. Append this to the buffer, and go onto the
667 * next record. Repeat the process until we find a stab without the
668 * '/' character, as this indicates we have the whole thing.
669 */
670 len = strlen(ptr);
671 if( strlen(stabbuff) + len > stabbufflen )
672 {
673 stabbufflen += 65536;
Ove Kaavendda17c61999-04-25 12:24:42 +0000674 stabbuff = (char *) DBG_realloc(stabbuff, stabbufflen);
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000675 }
676 strncat(stabbuff, ptr, len - 1);
677 continue;
678 }
679 else if( stabbuff[0] != '\0' )
680 {
681 strcat( stabbuff, ptr);
682 ptr = stabbuff;
683 }
684
685 if( strchr(ptr, '=') != NULL )
686 {
687 /*
688 * The stabs aren't in writable memory, so copy it over so we are
689 * sure we can scribble on it.
690 */
691 if( ptr != stabbuff )
692 {
693 strcpy(stabbuff, ptr);
694 ptr = stabbuff;
695 }
696 stab_strcpy(symname, ptr);
697 DEBUG_ParseTypedefStab(ptr, symname);
698 }
699
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000700 switch(stab_ptr->n_type)
701 {
702 case N_GSYM:
703 /*
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000704 * These are useless with ELF. They have no value, and you have to
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000705 * read the normal symbol table to get the address. Thus we
706 * ignore them, and when we process the normal symbol table
707 * we should do the right thing.
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000708 *
709 * With a.out, they actually do make some amount of sense.
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000710 */
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000711 new_addr.seg = 0;
712 new_addr.type = DEBUG_ParseStabType(ptr);
713 new_addr.off = load_offset + stab_ptr->n_value;
714
715 stab_strcpy(symname, ptr);
716#ifdef __ELF__
717 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
718 SYM_WINE | SYM_DATA | SYM_INVALID);
719#else
720 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
721 SYM_WINE | SYM_DATA );
722#endif
723 break;
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000724 case N_RBRAC:
725 case N_LBRAC:
726 /*
727 * We need to keep track of these so we get symbol scoping
728 * right for local variables. For now, we just ignore them.
729 * The hooks are already there for dealing with this however,
730 * so all we need to do is to keep count of the nesting level,
731 * and find the RBRAC for each matching LBRAC.
732 */
733 break;
734 case N_LCSYM:
735 case N_STSYM:
736 /*
737 * These are static symbols and BSS symbols.
738 */
739 new_addr.seg = 0;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000740 new_addr.type = DEBUG_ParseStabType(ptr);
741 new_addr.off = load_offset + stab_ptr->n_value;
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000742
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000743 stab_strcpy(symname, ptr);
744 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
745 SYM_WINE | SYM_DATA );
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000746 break;
747 case N_PSYM:
748 /*
749 * These are function parameters.
750 */
751 if( (curr_func != NULL)
752 && (stab_ptr->n_value != 0) )
753 {
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000754 stab_strcpy(symname, ptr);
755 curr_loc = DEBUG_AddLocal(curr_func, 0,
756 stab_ptr->n_value, 0, 0, symname);
757 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr));
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000758 }
759 break;
760 case N_RSYM:
761 if( curr_func != NULL )
762 {
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000763 stab_strcpy(symname, ptr);
764 curr_loc = DEBUG_AddLocal(curr_func, stab_ptr->n_value, 0, 0, 0, symname);
765 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr));
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000766 }
767 break;
768 case N_LSYM:
769 if( (curr_func != NULL)
770 && (stab_ptr->n_value != 0) )
771 {
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000772 stab_strcpy(symname, ptr);
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000773 DEBUG_AddLocal(curr_func, 0,
774 stab_ptr->n_value, 0, 0, symname);
775 }
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000776 else if (curr_func == NULL)
777 {
778 stab_strcpy(symname, ptr);
779 }
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000780 break;
781 case N_SLINE:
782 /*
783 * This is a line number. These are always relative to the start
784 * of the function (N_FUN), and this makes the lookup easier.
785 */
786 if( curr_func != NULL )
787 {
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000788#ifdef __ELF__
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000789 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
790 stab_ptr->n_value);
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000791#else
792#if 0
793 /*
794 * This isn't right. The order of the stabs is different under
795 * a.out, and as a result we would end up attaching the line
796 * number to the wrong function.
797 */
798 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
799 stab_ptr->n_value - curr_func->addr.off);
800#endif
801#endif
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000802 }
803 break;
804 case N_FUN:
805 /*
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000806 * First, clean up the previous function we were working on.
807 */
808 DEBUG_Normalize(curr_func);
809
810 /*
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000811 * For now, just declare the various functions. Later
812 * on, we will add the line number information and the
813 * local symbols.
814 */
815 if( !ignore )
816 {
817 new_addr.seg = 0;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000818 new_addr.type = DEBUG_ParseStabType(ptr);
819 new_addr.off = load_offset + stab_ptr->n_value;
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000820 /*
821 * Copy the string to a temp buffer so we
822 * can kill everything after the ':'. We do
823 * it this way because otherwise we end up dirtying
824 * all of the pages related to the stabs, and that
825 * sucks up swap space like crazy.
826 */
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000827 stab_strcpy(symname, ptr);
828 curr_func = DEBUG_AddSymbol( symname, &new_addr, currpath,
829 SYM_WINE | SYM_FUNC);
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000830 }
831 else
832 {
833 /*
834 * Don't add line number information for this function
835 * any more.
836 */
837 curr_func = NULL;
838 }
839 break;
840 case N_SO:
841 /*
842 * This indicates a new source file. Append the records
843 * together, to build the correct path name.
844 */
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000845#ifndef __ELF__
846 /*
847 * With a.out, there is no NULL string N_SO entry at the end of
848 * the file. Thus when we find non-consecutive entries,
849 * we consider that a new file is started.
850 */
851 if( last_nso < i-1 )
852 {
853 currpath[0] = '\0';
854 DEBUG_Normalize(curr_func);
855 curr_func = NULL;
856 }
857#endif
858
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000859 if( *ptr == '\0' )
860 {
861 /*
862 * Nuke old path.
863 */
864 currpath[0] = '\0';
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000865 DEBUG_Normalize(curr_func);
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000866 curr_func = NULL;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000867 /*
868 * The datatypes that we would need to use are reset when
869 * we start a new file.
870 */
Alexandre Julliarda845b881998-06-01 10:44:35 +0000871 memset(stab_types, 0, num_stab_types * sizeof(stab_types[0]));
872 /*
873 for (i=0;i<nrofnroftypenums;i++)
874 memset(typenums[i],0,sizeof(typenums[i][0])*nroftypenums[i]);
875 */
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000876 }
877 else
878 {
Alexandre Julliard349a9531997-02-02 19:01:52 +0000879 if (*ptr != '/')
880 strcat(currpath, ptr);
881 else
882 strcpy(currpath, ptr);
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000883 subpath = ptr;
884 }
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000885 last_nso = i;
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000886 break;
887 case N_SOL:
888 /*
889 * This indicates we are including stuff from an include file.
890 * If this is the main source, enable the debug stuff, otherwise
891 * ignore it.
892 */
893 if( subpath == NULL || strcmp(ptr, subpath) == 0 )
894 {
895 ignore = FALSE;
896 }
897 else
898 {
899 ignore = TRUE;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000900 DEBUG_Normalize(curr_func);
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000901 curr_func = NULL;
902 }
903 break;
904 case N_UNDF:
905 strs += strtabinc;
906 strtabinc = stab_ptr->n_value;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000907 DEBUG_Normalize(curr_func);
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000908 curr_func = NULL;
909 break;
910 case N_OPT:
911 /*
912 * Ignore this. We don't care what it points to.
913 */
914 break;
915 case N_BINCL:
916 case N_EINCL:
917 case N_MAIN:
918 /*
919 * Always ignore these. GCC doesn't even generate them.
920 */
921 break;
922 default:
923 break;
924 }
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000925
926 stabbuff[0] = '\0';
927
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000928#if 0
929 fprintf(stderr, "%d %x %s\n", stab_ptr->n_type,
930 (unsigned int) stab_ptr->n_value,
931 strs + (unsigned int) stab_ptr->n_un.n_name);
932#endif
933 }
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000934
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000935 if( stab_types != NULL )
936 {
Ove Kaavendda17c61999-04-25 12:24:42 +0000937 DBG_free(stab_types);
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000938 stab_types = NULL;
939 num_stab_types = 0;
940 }
941
Alexandre Julliard01d63461997-01-20 19:43:45 +0000942
943 DEBUG_FreeRegisteredTypedefs();
944
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000945 return TRUE;
946}
947
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000948#ifdef __ELF__
949
950/*
951 * Walk through the entire symbol table and add any symbols we find there.
952 * This can be used in cases where we have stripped ELF shared libraries,
953 * or it can be used in cases where we have data symbols for which the address
954 * isn't encoded in the stabs.
955 *
956 * This is all really quite easy, since we don't have to worry about line
957 * numbers or local data variables.
958 */
959static
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000960int
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000961DEBUG_ProcessElfSymtab(char * addr, unsigned int load_offset,
962 Elf32_Shdr * symtab, Elf32_Shdr * strtab)
963{
964 char * curfile = NULL;
965 struct name_hash * curr_sym = NULL;
966 int flags;
967 int i;
968 DBG_ADDR new_addr;
969 int nsym;
970 char * strp;
971 char * symname;
972 Elf32_Sym * symp;
973
974
975 symp = (Elf32_Sym *) (addr + symtab->sh_offset);
976 nsym = symtab->sh_size / sizeof(*symp);
977 strp = (char *) (addr + strtab->sh_offset);
978
979 for(i=0; i < nsym; i++, symp++)
980 {
981 /*
982 * Ignore certain types of entries which really aren't of that much
983 * interest.
984 */
985 if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION )
986 {
987 continue;
988 }
989
990 symname = strp + symp->st_name;
991
992 /*
993 * Save the name of the current file, so we have a way of tracking
994 * static functions/data.
995 */
996 if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
997 {
998 curfile = symname;
999 continue;
1000 }
1001
1002
1003 /*
1004 * See if we already have something for this symbol.
1005 * If so, ignore this entry, because it would have come from the
1006 * stabs or from a previous symbol. If the value is different,
1007 * we will have to keep the darned thing, because there can be
1008 * multiple local symbols by the same name.
1009 */
1010 if( (DEBUG_GetSymbolValue(symname, -1, &new_addr, FALSE ) == TRUE)
1011 && (new_addr.off == (load_offset + symp->st_value)) )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001012 continue;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001013
1014 new_addr.seg = 0;
1015 new_addr.type = NULL;
1016 new_addr.off = load_offset + symp->st_value;
1017 flags = SYM_WINE | (ELF32_ST_BIND(symp->st_info) == STT_FUNC
1018 ? SYM_FUNC : SYM_DATA);
1019 if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001020 curr_sym = DEBUG_AddSymbol( symname, &new_addr, NULL, flags );
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001021 else
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001022 curr_sym = DEBUG_AddSymbol( symname, &new_addr, curfile, flags );
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001023
1024 /*
1025 * Record the size of the symbol. This can come in handy in
1026 * some cases. Not really used yet, however.
1027 */
1028 if( symp->st_size != 0 )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001029 DEBUG_SetSymbolSize(curr_sym, symp->st_size);
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001030 }
1031
1032 return TRUE;
1033}
1034
1035static
1036int
1037DEBUG_ProcessElfObject(char * filename, unsigned int load_offset)
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001038{
1039 int rtn = FALSE;
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001040 struct stat statbuf;
1041 int fd = -1;
1042 int status;
1043 char * addr = (char *) 0xffffffff;
1044 Elf32_Ehdr * ehptr;
1045 Elf32_Shdr * spnt;
1046 char * shstrtab;
1047 int nsect;
1048 int i;
1049 int stabsect;
1050 int stabstrsect;
1051
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001052
1053 /*
1054 * Make sure we can stat and open this file.
1055 */
1056 if( filename == NULL )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001057 goto leave;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001058
1059 status = stat(filename, &statbuf);
1060 if( status == -1 )
1061 {
Alexandre Julliard77b99181997-09-14 17:17:23 +00001062 char *s,*t,*fn,*paths;
1063 if (strchr(filename,'/'))
1064 goto leave;
Ove Kaavendda17c61999-04-25 12:24:42 +00001065 paths = DBG_strdup(getenv("PATH"));
Alexandre Julliard77b99181997-09-14 17:17:23 +00001066 s = paths;
1067 while (s && *s) {
1068 t = strchr(s,':');
1069 if (t) *t='\0';
Ove Kaavendda17c61999-04-25 12:24:42 +00001070 fn = (char*)DBG_alloc(strlen(filename)+1+strlen(s)+1);
Alexandre Julliard77b99181997-09-14 17:17:23 +00001071 strcpy(fn,s);
1072 strcat(fn,"/");
1073 strcat(fn,filename);
1074 if ((rtn = DEBUG_ProcessElfObject(fn,load_offset))) {
Ove Kaavendda17c61999-04-25 12:24:42 +00001075 DBG_free(fn);
1076 DBG_free(paths);
Alexandre Julliard77b99181997-09-14 17:17:23 +00001077 goto leave;
1078 }
Ove Kaavendda17c61999-04-25 12:24:42 +00001079 DBG_free(fn);
Alexandre Julliard85ed45e1998-08-22 19:03:56 +00001080 if (t) s = t+1; else break;
Alexandre Julliard77b99181997-09-14 17:17:23 +00001081 }
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001082 if (!s || !*s) fprintf(stderr," not found");
Ove Kaavendda17c61999-04-25 12:24:42 +00001083 DBG_free(paths);
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001084 goto leave;
1085 }
1086
1087 /*
1088 * Now open the file, so that we can mmap() it.
1089 */
1090 fd = open(filename, O_RDONLY);
1091 if( fd == -1 )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001092 goto leave;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001093
1094
1095 /*
1096 * Now mmap() the file.
1097 */
1098 addr = mmap(0, statbuf.st_size, PROT_READ,
1099 MAP_PRIVATE, fd, 0);
Alexandre Julliarddf2673b1997-03-29 17:20:20 +00001100 if( addr == (char *) 0xffffffff )
Alexandre Julliarddf2673b1997-03-29 17:20:20 +00001101 goto leave;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001102
1103 /*
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001104 * Next, we need to find a few of the internal ELF headers within
1105 * this thing. We need the main executable header, and the section
1106 * table.
1107 */
1108 ehptr = (Elf32_Ehdr *) addr;
1109
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001110 if( load_offset == 0 )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001111 DEBUG_RegisterELFDebugInfo(ehptr->e_entry, statbuf.st_size, filename);
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001112 else
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001113 DEBUG_RegisterELFDebugInfo(load_offset, statbuf.st_size, filename);
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001114
1115 spnt = (Elf32_Shdr *) (addr + ehptr->e_shoff);
1116 nsect = ehptr->e_shnum;
1117 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1118
1119 stabsect = stabstrsect = -1;
1120
1121 for(i=0; i < nsect; i++)
1122 {
1123 if( strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0 )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001124 stabsect = i;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001125
1126 if( strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0 )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001127 stabstrsect = i;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001128 }
1129
1130 if( stabsect == -1 || stabstrsect == -1 )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001131 goto leave;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001132
1133 /*
1134 * OK, now just parse all of the stabs.
1135 */
1136 rtn = DEBUG_ParseStabs(addr, load_offset,
1137 spnt[stabsect].sh_offset,
1138 spnt[stabsect].sh_size,
1139 spnt[stabstrsect].sh_offset,
1140 spnt[stabstrsect].sh_size);
1141
1142 if( rtn != TRUE )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001143 goto leave;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001144
1145 for(i=0; i < nsect; i++)
1146 {
1147 if( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1148 && (spnt[i].sh_type == SHT_SYMTAB) )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001149 DEBUG_ProcessElfSymtab(addr, load_offset,
1150 spnt + i, spnt + spnt[i].sh_link);
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001151
1152 if( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1153 && (spnt[i].sh_type == SHT_DYNSYM) )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001154 DEBUG_ProcessElfSymtab(addr, load_offset,
1155 spnt + i, spnt + spnt[i].sh_link);
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001156 }
1157
1158leave:
1159
1160 if( addr != (char *) 0xffffffff )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001161 munmap(addr, statbuf.st_size);
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001162
1163 if( fd != -1 )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001164 close(fd);
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001165
1166 return (rtn);
1167
1168}
1169
1170int
1171DEBUG_ReadExecutableDbgInfo(void)
1172{
1173 Elf32_Ehdr * ehdr;
1174 char * exe_name;
1175 Elf32_Dyn * dynpnt;
1176 struct r_debug * dbg_hdr;
1177 struct link_map * lpnt = NULL;
1178 extern Elf32_Dyn _DYNAMIC[];
1179 int rtn = FALSE;
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001180 int rowcount;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001181
1182 exe_name = DEBUG_argv0;
1183
1184 /*
1185 * Make sure we can stat and open this file.
1186 */
1187 if( exe_name == NULL )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001188 goto leave;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001189
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001190 fprintf( stderr, "Loading symbols: %s", exe_name );
1191 rowcount = 17 + strlen(exe_name);
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001192 DEBUG_ProcessElfObject(exe_name, 0);
1193
1194 /*
1195 * Finally walk the tables that the dynamic loader maintains to find all
1196 * of the other shared libraries which might be loaded. Perform the
1197 * same step for all of these.
1198 */
1199 dynpnt = _DYNAMIC;
1200 if( dynpnt == NULL )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001201 goto leave;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001202
1203 /*
1204 * Now walk the dynamic section (of the executable, looking for a DT_DEBUG
1205 * entry.
1206 */
1207 for(; dynpnt->d_tag != DT_NULL; dynpnt++)
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001208 if( dynpnt->d_tag == DT_DEBUG )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001209 break;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001210
1211 if( (dynpnt->d_tag != DT_DEBUG)
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001212 || (dynpnt->d_un.d_ptr == 0) )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001213 goto leave;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001214
1215 /*
1216 * OK, now dig into the actual tables themselves.
1217 */
1218 dbg_hdr = (struct r_debug *) dynpnt->d_un.d_ptr;
1219 lpnt = dbg_hdr->r_map;
1220
1221 /*
1222 * Now walk the linked list. In all known ELF implementations,
1223 * the dynamic loader maintains this linked list for us. In some
1224 * cases the first entry doesn't appear with a name, in other cases it
1225 * does.
1226 */
1227 for(; lpnt; lpnt = lpnt->l_next )
1228 {
1229 /*
1230 * We already got the stuff for the executable using the
1231 * argv[0] entry above. Here we only need to concentrate on any
1232 * shared libraries which may be loaded.
1233 */
1234 ehdr = (Elf32_Ehdr *) lpnt->l_addr;
Alexandre Julliarddadf78f1998-05-17 17:13:43 +00001235 if( (lpnt->l_addr == 0) || (ehdr->e_type != ET_DYN) )
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001236 continue;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001237
1238 if( lpnt->l_name != NULL )
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001239 {
1240 if (rowcount + strlen(lpnt->l_name) > 76)
1241 {
1242 fprintf( stderr, "\n " );
1243 rowcount = 3;
1244 }
1245 fprintf( stderr, " %s", lpnt->l_name );
1246 rowcount += strlen(lpnt->l_name) + 1;
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001247 DEBUG_ProcessElfObject(lpnt->l_name, lpnt->l_addr);
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001248 }
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001249 }
1250
1251 rtn = TRUE;
1252
1253leave:
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001254 fprintf( stderr, "\n" );
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001255 return (rtn);
1256
1257}
1258
1259#else /* !__ELF__ */
1260
1261#ifdef linux
1262/*
1263 * a.out linux.
1264 */
1265int
1266DEBUG_ReadExecutableDbgInfo(void)
1267{
1268 char * addr = (char *) 0xffffffff;
1269 char * exe_name;
1270 struct exec * ahdr;
1271 int fd = -1;
1272 int rtn = FALSE;
1273 unsigned int staboff;
1274 struct stat statbuf;
1275 int status;
1276 unsigned int stroff;
1277
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001278 exe_name = DEBUG_argv0;
1279
1280 /*
1281 * Make sure we can stat and open this file.
1282 */
1283 if( exe_name == NULL )
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001284 goto leave;
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001285
1286 status = stat(exe_name, &statbuf);
1287 if( status == -1 )
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001288 goto leave;
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001289
1290 /*
1291 * Now open the file, so that we can mmap() it.
1292 */
1293 fd = open(exe_name, O_RDONLY);
1294 if( fd == -1 )
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001295 goto leave;
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001296
1297
1298 /*
1299 * Now mmap() the file.
1300 */
1301 addr = mmap(0, statbuf.st_size, PROT_READ,
1302 MAP_PRIVATE, fd, 0);
Alexandre Julliarddf2673b1997-03-29 17:20:20 +00001303 if( addr == (char *) 0xffffffff )
Alexandre Julliarddf2673b1997-03-29 17:20:20 +00001304 goto leave;
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001305
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001306 ahdr = (struct exec *) addr;
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001307
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001308 staboff = N_SYMOFF(*ahdr);
1309 stroff = N_STROFF(*ahdr);
1310 rtn = DEBUG_ParseStabs(addr, 0,
1311 staboff,
1312 ahdr->a_syms,
1313 stroff,
1314 statbuf.st_size - stroff);
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001315
1316 /*
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001317 * Give a nice status message here...
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001318 */
Alexandre Julliard767e6f61998-08-09 12:47:43 +00001319 fprintf( stderr, "Loading symbols: %s", exe_name );
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001320
1321 rtn = TRUE;
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001322
1323leave:
1324
1325 if( addr != (char *) 0xffffffff )
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001326 munmap(addr, statbuf.st_size);
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001327
1328 if( fd != -1 )
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001329 close(fd);
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001330
1331 return (rtn);
1332
1333}
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001334#else
1335/*
1336 * Non-linux, non-ELF platforms.
1337 */
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001338int
1339DEBUG_ReadExecutableDbgInfo(void)
1340{
1341return FALSE;
1342}
Alexandre Julliardc6c09441997-01-12 18:32:19 +00001343#endif
Alexandre Julliard9ea19e51997-01-01 17:29:55 +00001344
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +00001345#endif /* __ELF__ */