Do not crash if path is NULL in DEBUG_ProcessElfFileFromPath.
[wine] / debugger / stabs.c
1 /* -*- tab-width: 8; c-basic-offset: 2 -*- */
2
3 /*
4  * File stabs.c - read stabs information from the wine executable itself.
5  *
6  * Copyright (C) 1996, Eric Youngdale.
7  *               1999, 2000 Eric Pouech
8  */
9
10 #include "config.h"
11
12 #include <sys/types.h>
13 #include <fcntl.h>
14 #include <sys/stat.h>
15 #ifdef HAVE_SYS_MMAN_H
16 #include <sys/mman.h>
17 #endif
18 #include <limits.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #ifndef PATH_MAX
23 #define PATH_MAX _MAX_PATH
24 #endif
25
26 #include "debugger.h"
27
28 #if defined(__svr4__) || defined(__sun)
29 #define __ELF__
30 #endif
31
32 #ifdef __ELF__
33 #ifdef HAVE_ELF_H
34 # include <elf.h>
35 #endif
36 #ifdef HAVE_LINK_H
37 # include <link.h>
38 #endif
39 #elif defined(__EMX__)
40 #ifdef HAVE_A_OUT_H
41 # include <a_out.h>
42 #endif
43 #else
44 #ifdef HAVE_A_OUT_H
45 # include <a.out.h>
46 #endif
47 #endif
48
49 #ifndef N_UNDF
50 #define N_UNDF          0x00
51 #endif
52
53 #ifndef STN_UNDEF
54 # define STN_UNDEF      0
55 #endif
56
57 #define N_GSYM          0x20
58 #define N_FUN           0x24
59 #define N_STSYM         0x26
60 #define N_LCSYM         0x28
61 #define N_MAIN          0x2a
62 #define N_ROSYM         0x2c
63 #define N_OPT           0x3c
64 #define N_RSYM          0x40
65 #define N_SLINE         0x44
66 #define N_SO            0x64
67 #define N_LSYM          0x80
68 #define N_BINCL         0x82
69 #define N_SOL           0x84
70 #define N_PSYM          0xa0
71 #define N_EINCL         0xa2
72 #define N_LBRAC         0xc0
73 #define N_EXCL          0xc2
74 #define N_RBRAC         0xe0
75
76
77 struct stab_nlist {
78   union {
79     char *n_name;
80     struct stab_nlist *n_next;
81     long n_strx;
82   } n_un;
83   unsigned char n_type;
84   char n_other;
85   short n_desc;
86   unsigned long n_value;
87 };
88
89 /*
90  * This is used to keep track of known datatypes so that we don't redefine
91  * them over and over again.  It sucks up lots of memory otherwise.
92  */
93 struct known_typedef
94 {
95   struct known_typedef * next;
96   char                 * name;
97   int                    ndefs;
98   struct datatype      * types[1];
99 };
100
101 #define NR_STAB_HASH 521
102
103 static struct known_typedef * ktd_head[NR_STAB_HASH] = {NULL,};
104 static struct datatype **     curr_types = NULL;
105 static int                    allocated_types = 0;
106
107 static unsigned int stab_hash( const char * name )
108 {
109     unsigned int hash = 0;
110     unsigned int tmp;
111     const char * p;
112
113     p = name;
114
115     while (*p) 
116       {
117         hash = (hash << 4) + *p++;
118
119         if( (tmp = (hash & 0xf0000000)) )
120           {
121             hash ^= tmp >> 24;
122           }
123         hash &= ~tmp;
124       }
125     return hash % NR_STAB_HASH;
126 }
127
128
129 static void stab_strcpy(char * dest, int sz, const char * source)
130 {
131   /*
132    * A strcpy routine that stops when we hit the ':' character.
133    * Faster than copying the whole thing, and then nuking the
134    * ':'.
135    */
136   while(*source != '\0' && *source != ':' && sz-- > 0)
137       *dest++ = *source++;
138   *dest = '\0';
139   assert(sz > 0);
140 }
141
142 typedef struct {
143    char*                name;
144    unsigned long        value;
145    int                  idx;
146    struct datatype**    vector;
147    int                  nrofentries;
148 } include_def;
149
150 #define MAX_INCLUDES    256
151
152 static  include_def*    include_defs = NULL;
153 static  int             num_include_def = 0;
154 static  int             num_alloc_include_def = 0;
155 static  int             cu_include_stack[MAX_INCLUDES];
156 static  int             cu_include_stk_idx = 0;
157 static  struct datatype**       cu_vector = NULL;
158 static  int             cu_nrofentries = 0;
159
160 static 
161 int     
162 DEBUG_CreateInclude(const char* file, unsigned long val)
163 {
164   if (num_include_def == num_alloc_include_def) 
165     {
166       num_alloc_include_def += 256;
167       include_defs = DBG_realloc(include_defs, sizeof(include_defs[0])*num_alloc_include_def);
168       memset(include_defs+num_include_def, 0, sizeof(include_defs[0])*256);
169     }
170   include_defs[num_include_def].name = DBG_strdup(file);
171   include_defs[num_include_def].value = val;
172   include_defs[num_include_def].vector = NULL;
173   include_defs[num_include_def].nrofentries = 0;
174   
175   return num_include_def++;
176 }
177
178 static 
179 int     
180 DEBUG_FindInclude(const char* file, unsigned long val)
181 {
182   int           i;
183   
184   for (i = 0; i < num_include_def; i++) 
185     {
186       if (val == include_defs[i].value && 
187           strcmp(file, include_defs[i].name) == 0)
188         return i;
189     }
190   return -1;
191 }
192
193 static 
194 int
195 DEBUG_AddInclude(int idx)
196 {
197   ++cu_include_stk_idx;
198   
199   /* is this happen, just bump MAX_INCLUDES */
200   /* we could also handle this as another dynarray */
201   assert(cu_include_stk_idx < MAX_INCLUDES);
202   
203   cu_include_stack[cu_include_stk_idx] = idx;
204   return cu_include_stk_idx;
205 }
206
207 static
208 void
209 DEBUG_ResetIncludes(void)
210 {
211   /*
212    * The datatypes that we would need to use are reset when
213    * we start a new file. (at least the ones in filenr == 0
214    */
215   cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
216   memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
217 }
218
219 static
220 void
221 DEBUG_FreeIncludes(void)
222 {
223   int   i;
224   
225   DEBUG_ResetIncludes();
226   
227   for (i = 0; i < num_include_def; i++) 
228     {
229       DBG_free(include_defs[i].name);
230       DBG_free(include_defs[i].vector);
231     }
232   DBG_free(include_defs);
233   include_defs = NULL;
234   num_include_def = 0;
235   num_alloc_include_def = 0;
236   DBG_free(cu_vector);
237   cu_vector = NULL;
238   cu_nrofentries = 0;
239 }
240
241 static
242 struct datatype**
243 DEBUG_FileSubNr2StabEnum(int filenr, int subnr) 
244 {
245   struct datatype** ret;
246   
247   /* DEBUG_Printf(DBG_CHN_MESG, "creating type id for (%d,%d)\n", filenr, subnr); */
248   
249   /* FIXME: I could perhaps create a dummy include_def for each compilation
250    * unit which would allow not to handle those two cases separately
251    */
252   if (filenr == 0) 
253     {
254       if (cu_nrofentries <= subnr) 
255         {
256           cu_vector = DBG_realloc(cu_vector, sizeof(cu_vector[0])*(subnr+1));
257           memset(cu_vector+cu_nrofentries, 0, sizeof(cu_vector[0])*(subnr+1-cu_nrofentries));
258           cu_nrofentries = subnr + 1;
259         }
260       ret = &cu_vector[subnr];
261     }
262   else
263     {
264       include_def*      idef;
265       
266       assert(filenr <= cu_include_stk_idx);
267       
268       idef = &include_defs[cu_include_stack[filenr]];
269       
270       if (idef->nrofentries <= subnr)
271         {
272           idef->vector = DBG_realloc(idef->vector, sizeof(idef->vector[0])*(subnr+1));
273           memset(idef->vector + idef->nrofentries, 0, sizeof(idef->vector[0])*(subnr+1-idef->nrofentries));
274           idef->nrofentries = subnr + 1;
275         }
276       ret = &idef->vector[subnr];
277     }
278   /* DEBUG_Printf(DBG_CHN_MESG,"(%d,%d) is %d\n",filenr,subnr,ret); */
279   return ret;
280 }
281
282 static
283 struct datatype**
284 DEBUG_ReadTypeEnumBackwards(char*x) {
285     int filenr,subnr;
286
287     if (*x==')') {
288         while (*x!='(')
289             x--;
290         x++;                            /* '(' */
291         filenr=strtol(x,&x,10);         /* <int> */
292         x++;                            /* ',' */
293         subnr=strtol(x,&x,10);          /* <int> */
294         x++;                            /* ')' */
295     } else {
296         while ((*x>='0') && (*x<='9'))
297             x--;
298         filenr = 0;
299         subnr = atol(x+1);
300     }
301     return DEBUG_FileSubNr2StabEnum(filenr,subnr);
302 }
303
304 static 
305 struct datatype**
306 DEBUG_ReadTypeEnum(char **x) {
307     int filenr,subnr;
308
309     if (**x=='(') {
310         (*x)++;                                 /* '(' */
311         filenr=strtol(*x,x,10);                 /* <int> */
312         (*x)++;                                 /* ',' */
313         subnr=strtol(*x,x,10);                  /* <int> */
314         (*x)++;                                 /* ')' */
315     } else {
316         filenr = 0;
317         subnr = strtol(*x,x,10);                /* <int> */
318     }
319     return DEBUG_FileSubNr2StabEnum(filenr,subnr);
320 }
321
322 static
323 int
324 DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef)
325 {
326   int                    hash;
327   struct known_typedef * ktd;
328
329   if( ndef == 1 )
330       return TRUE;
331
332   ktd = (struct known_typedef *) DBG_alloc(sizeof(struct known_typedef) 
333                                          + (ndef - 1) * sizeof(struct datatype *));
334   
335   hash = stab_hash(name);
336
337   ktd->name = DBG_strdup(name);
338   ktd->ndefs = ndef;
339   memcpy(&ktd->types[0], types, ndef * sizeof(struct datatype *));
340   ktd->next = ktd_head[hash];
341   ktd_head[hash] = ktd;
342
343   return TRUE;
344 }
345
346 static
347 int
348 DEBUG_HandlePreviousTypedef(const char * name, const char * stab)
349 {
350   int                    count;
351   enum debug_type        expect;
352   int                    hash;
353   struct known_typedef * ktd;
354   char                 * ptr;
355
356   hash = stab_hash(name);
357
358   for(ktd = ktd_head[hash]; ktd; ktd = ktd->next)
359       if ((ktd->name[0] == name[0]) && (strcmp(name, ktd->name) == 0) )
360           break;
361
362   /*
363    * Didn't find it.  This must be a new one.
364    */
365   if( ktd == NULL )
366       return FALSE;
367
368   /*
369    * Examine the stab to make sure it has the same number of definitions.
370    */
371   count = 0;
372   for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
373     {
374       if( count >= ktd->ndefs )
375           return FALSE;
376
377       /*
378        * Make sure the types of all of the objects is consistent with
379        * what we have already parsed.
380        */
381       switch(ptr[1])
382         {
383         case '*':
384           expect = DT_POINTER;
385           break;
386         case 's':
387         case 'u':
388           expect = DT_STRUCT;
389           break;
390         case 'a':
391           expect = DT_ARRAY;
392           break;
393         case '(':       /* it's mainly a ref to another typedef, skip it */
394           expect = -1;
395           break;
396         case '1':
397         case 'r':
398           expect = DT_BASIC;
399           break;
400         case 'x':
401           expect = DT_STRUCT;
402           break;
403         case 'e':
404           expect = DT_ENUM;
405           break;
406         case 'f':
407           expect = DT_FUNC;
408           break;
409         default:
410           DEBUG_Printf(DBG_CHN_FIXME, "Unknown type (%c).\n",ptr[1]);
411           return FALSE;
412         }
413       if( expect != -1 && expect != DEBUG_GetType(ktd->types[count]) )
414           return FALSE;
415       count++;
416     }
417
418   if( ktd->ndefs != count )
419       return FALSE;
420
421   /*
422    * Go through, dig out all of the type numbers, and substitute the
423    * appropriate things.
424    */
425   count = 0;
426   for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
427       *DEBUG_ReadTypeEnumBackwards(ptr-1) = ktd->types[count++];
428
429   return TRUE;
430 }
431
432 static int DEBUG_FreeRegisteredTypedefs(void)
433 {
434   int                    count;
435   int                    j;
436   struct known_typedef * ktd;
437   struct known_typedef * next;
438
439   count = 0;
440   for(j=0; j < NR_STAB_HASH; j++ )
441     {
442       for(ktd = ktd_head[j]; ktd; ktd = next)
443         {
444           count++;
445           next = ktd->next;
446           DBG_free(ktd->name);
447           DBG_free(ktd);
448         }  
449       ktd_head[j] = NULL;
450     }
451
452   return TRUE;
453
454 }
455
456 static 
457 int
458 DEBUG_ParseTypedefStab(char * ptr, const char * typename)
459 {
460   int               arrmax;
461   int               arrmin;
462   char            * c;
463   struct datatype * curr_type;
464   struct datatype * datatype;
465   char              element_name[1024];
466   int               ntypes = 0, ntp;
467   int               offset;
468   const char      * orig_typename;
469   int               size;
470   char            * tc;
471   char            * tc2;
472   int               failure;
473   
474   orig_typename = typename;
475
476   if( DEBUG_HandlePreviousTypedef(typename, ptr) )
477     return TRUE;
478
479   /* 
480    * Go from back to front.  First we go through and figure out what
481    * type numbers we need, and register those types.  Then we go in
482    * and fill the details.  
483    */
484
485   for( c = strchr(ptr, '='); c != NULL; c = strchr(c + 1, '=') )
486     {
487       /*
488        * Back up until we get to a non-numeric character, to get datatype
489        */
490       struct datatype** dt = DEBUG_ReadTypeEnumBackwards(c-1);
491       
492       if( ntypes >= allocated_types )
493         {
494           allocated_types += 64;
495           curr_types = DBG_realloc(curr_types, sizeof(struct datatype*) * allocated_types);
496           if (!curr_types) return FALSE;
497         }
498       
499       switch(c[1])
500         {
501         case '*':
502           *dt = DEBUG_NewDataType(DT_POINTER, NULL);
503           curr_types[ntypes++] = *dt;
504           break;
505         case 's':
506         case 'u':
507           *dt = DEBUG_NewDataType(DT_STRUCT, typename);
508           curr_types[ntypes++] = *dt;
509           break;
510         case 'a':
511           *dt = DEBUG_NewDataType(DT_ARRAY, NULL);
512           curr_types[ntypes++] = *dt;
513           break;
514         case '(':
515           /* will be handled in next loop, 
516            * just a ref to another type 
517            */
518           curr_types[ntypes++] = NULL;
519           break;
520         case '1':
521         case 'r':
522           *dt = DEBUG_NewDataType(DT_BASIC, typename);
523           curr_types[ntypes++] = *dt;
524           break;
525         case 'x':
526           stab_strcpy(element_name, sizeof(element_name), c + 3);
527           *dt = DEBUG_NewDataType(DT_STRUCT, element_name);
528           curr_types[ntypes++] = *dt;
529           break;
530         case 'e':
531           *dt = DEBUG_NewDataType(DT_ENUM, NULL);
532           curr_types[ntypes++] = *dt;
533           break;
534         case 'f':
535           *dt = DEBUG_NewDataType(DT_FUNC, NULL);
536           curr_types[ntypes++] = *dt;
537           break;
538         default:
539           DEBUG_Printf(DBG_CHN_FIXME, "Unknown type (%c).\n",c[1]);
540           return FALSE;
541         }
542       typename = NULL;
543       
544     }
545
546   ntp = ntypes - 1;
547   /* 
548    * OK, now take a second sweep through.  Now we will be digging
549    * out the definitions of the various components, and storing
550    * them in the skeletons that we have already allocated.  We take
551    * a right-to left search as this is much easier to parse.  
552    */
553   for( c = strrchr(ptr, '='); c != NULL; c = strrchr(ptr, '=') )
554     {
555       struct datatype** dt = DEBUG_ReadTypeEnumBackwards(c-1);
556       struct datatype** dt2;
557         
558       curr_type = *dt;
559       
560       switch(c[1])
561         {
562         case 'x':
563           ntp--;
564           tc = c + 3;
565           while( *tc != ':' )
566             tc++;
567           tc++;
568           if( *tc == '\0' )
569             *c = '\0';
570           else
571             strcpy(c, tc);
572           break;
573         case '*':
574         case 'f':
575           ntp--;
576           tc = c + 2;
577           datatype = *DEBUG_ReadTypeEnum(&tc);
578           DEBUG_SetPointerType(curr_type, datatype);
579           if( *tc == '\0' )
580             *c = '\0';
581           else
582             strcpy(c, tc);
583           break;
584         case '(':
585           tc = c + 1;
586           dt2 = DEBUG_ReadTypeEnum(&tc);
587           
588           if (!*dt && *dt2) 
589             {
590               *dt = *dt2;
591             } 
592           else if (!*dt && !*dt2) 
593             {
594               /* this should be a basic type, define it */
595               *dt2 = *dt = DEBUG_NewDataType(DT_BASIC, typename);
596             } 
597           else 
598             {
599               DEBUG_Printf(DBG_CHN_MESG, "Unknown condition %08lx %08lx (%s)\n", 
600                            (unsigned long)*dt, (unsigned long)*dt2, ptr);
601             }
602           if( *tc == '\0' )
603             *c = '\0';
604           else
605             strcpy(c, tc);
606           curr_types[ntp--] = *dt;
607           break;
608         case '1':
609         case 'r':
610           ntp--;
611           /*
612            * We have already handled these above.
613            */
614           *c = '\0';
615           break;
616         case 'a':
617           ntp--;
618           /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */
619           
620           tc  = c + 3;
621           /* 'r' */
622           DEBUG_ReadTypeEnum(&tc);
623           tc++;                                 /* ';' */
624           arrmin = strtol(tc, &tc, 10);         /* <int> */
625           tc++;                                 /* ';' */
626           arrmax = strtol(tc, &tc, 10);         /* <int> */
627           tc++;                                 /* ';' */
628           datatype = *DEBUG_ReadTypeEnum(&tc);  /* <typeinfo> */
629           if( *tc == '\0' )
630             *c = '\0';
631           else
632             strcpy(c, tc);
633           DEBUG_SetArrayParams(curr_type, arrmin, arrmax, datatype);
634           break;
635         case 's':
636         case 'u':
637           ntp--;
638           failure = 0;
639           
640           tc = c + 2;
641           if( DEBUG_SetStructSize(curr_type, strtol(tc, &tc, 10)) == FALSE )
642             {
643               /*
644                * We have already filled out this structure.  Nothing to do,
645                * so just skip forward to the end of the definition.
646                */
647               while( tc[0] != ';' && tc[1] != ';' )
648                 tc++;
649               
650               tc += 2;
651               
652               if( *tc == '\0' )
653                 *c = '\0';
654               else
655                 strcpy(c, tc + 1);
656               continue;
657             }
658           
659           /*
660            * Now parse the individual elements of the structure/union.
661            */
662           while(*tc != ';')
663             {
664               char *ti;
665               tc2 = element_name;
666               while(*tc != ':')
667                 *tc2++ = *tc++;
668               tc++;
669               *tc2++ = '\0';
670               ti=tc;
671               datatype = *DEBUG_ReadTypeEnum(&tc);
672               *tc='\0';
673               tc++;
674               offset  = strtol(tc, &tc, 10);
675               tc++;
676               size  = strtol(tc, &tc, 10);
677               tc++;
678               if (datatype)
679                 DEBUG_AddStructElement(curr_type, element_name, datatype, 
680                                        offset, size);
681               else 
682                 {
683                   failure = 1;
684                   /* ... but proceed parsing to the end of the stab */
685                   DEBUG_Printf(DBG_CHN_MESG, "failure on %s %s\n", ptr, ti);
686                 }
687             }
688           
689           if (failure) 
690             {
691               
692               /* if we had a undeclared value this one is undeclared too.
693                * remove it from the stab_types. 
694                * I just set it to NULL to detect bugs in my thoughtprocess.
695                * FIXME: leaks the memory for the structure elements.
696                * FIXME: such structures should have been optimized away
697                *        by ld.
698                */
699               *dt = NULL;
700             }
701           if( *tc == '\0' )
702             *c = '\0';
703           else
704             strcpy(c, tc + 1);
705           break;
706         case 'e':
707           ntp--;
708           tc = c + 2;
709           /*
710            * Now parse the individual elements of the structure/union.
711            */
712           while(*tc != ';')
713             {
714               tc2 = element_name;
715               while(*tc != ':')
716                 *tc2++ = *tc++;
717               tc++;
718               *tc2++ = '\0';
719               offset  = strtol(tc, &tc, 10);
720               tc++;
721               DEBUG_AddStructElement(curr_type, element_name, NULL, offset, 0);
722             }
723           if( *tc == '\0' )
724             *c = '\0';
725           else
726             strcpy(c, tc + 1);
727           break;
728         default:
729           DEBUG_Printf(DBG_CHN_FIXME, "Unknown type (%c).\n",c[1]);
730           return FALSE;
731         }
732     }
733   /*
734    * Now register the type so that if we encounter it again, we will know
735    * what to do.
736    */
737   DEBUG_RegisterTypedef(orig_typename, curr_types, ntypes);
738     
739   return TRUE;
740 }
741
742 static struct datatype *
743 DEBUG_ParseStabType(const char * stab)
744 {
745   char * c;
746
747   /*
748    * Look through the stab definition, and figure out what datatype
749    * this represents.  If we have something we know about, assign the
750    * type.
751    */
752   c = strchr(stab, ':');
753   if( c == NULL )
754       return NULL;
755
756   c++;
757   /*
758    * The next character says more about the type (i.e. data, function, etc)
759    * of symbol.  Skip it.
760    */
761   if (*c != '(')
762     c++;
763   /* 
764    * The next is either an integer or a (integer,integer).
765    * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
766    */
767   return *DEBUG_ReadTypeEnum(&c);
768 }
769
770 int
771 DEBUG_ParseStabs(char * addr, unsigned int load_offset,
772                  unsigned int staboff, int stablen, 
773                  unsigned int strtaboff, int strtablen)
774 {
775   struct name_hash    * curr_func = NULL;
776   struct wine_locals  * curr_loc = NULL;
777   struct name_hash    * curr_sym = NULL;
778   char                  currpath[PATH_MAX];
779   int                   i;
780   int                   in_external_file = FALSE;
781   int                   last_nso = -1;
782   int                   len;
783   DBG_VALUE             new_value;
784   int                   nstab;
785   char                * ptr;
786   char                * stabbuff;
787   int                   stabbufflen;
788   struct stab_nlist   * stab_ptr;
789   char                * strs;
790   int                   strtabinc;
791   char                * subpath = NULL;
792   char                  symname[4096];
793
794   nstab = stablen / sizeof(struct stab_nlist);
795   stab_ptr = (struct stab_nlist *) (addr + staboff);
796   strs  = (char *) (addr + strtaboff);
797
798   memset(currpath, 0, sizeof(currpath));
799
800   /*
801    * Allocate a buffer into which we can build stab strings for cases
802    * where the stab is continued over multiple lines.
803    */
804   stabbufflen = 65536;
805   stabbuff = (char *) DBG_alloc(stabbufflen);
806
807   strtabinc = 0;
808   stabbuff[0] = '\0';
809   for(i=0; i < nstab; i++, stab_ptr++ )
810     {
811       ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
812       if( ptr[strlen(ptr) - 1] == '\\' )
813         {
814           /*
815            * Indicates continuation.  Append this to the buffer, and go onto the
816            * next record.  Repeat the process until we find a stab without the
817            * '/' character, as this indicates we have the whole thing.
818            */
819           len = strlen(ptr);
820           if( strlen(stabbuff) + len > stabbufflen )
821             {
822               stabbufflen += 65536;
823               stabbuff = (char *) DBG_realloc(stabbuff, stabbufflen);
824             }
825           strncat(stabbuff, ptr, len - 1);
826           continue;
827         }
828       else if( stabbuff[0] != '\0' )
829         {
830           strcat( stabbuff, ptr);
831           ptr = stabbuff;
832         }
833
834       if( strchr(ptr, '=') != NULL )
835         {
836           /*
837            * The stabs aren't in writable memory, so copy it over so we are
838            * sure we can scribble on it.
839            */
840           if( ptr != stabbuff )
841             {
842               strcpy(stabbuff, ptr);
843               ptr = stabbuff;
844             }
845           stab_strcpy(symname, sizeof(symname), ptr);
846           if (!DEBUG_ParseTypedefStab(ptr, symname)) {
847             /* skip this definition */
848             stabbuff[0] = '\0';
849             continue;
850           }
851         }
852
853       switch(stab_ptr->n_type)
854         {
855         case N_GSYM:
856           /*
857            * These are useless with ELF.  They have no value, and you have to
858            * read the normal symbol table to get the address.  Thus we
859            * ignore them, and when we process the normal symbol table
860            * we should do the right thing.
861            *
862            * With a.out or mingw, they actually do make some amount of sense.
863            */
864           new_value.addr.seg = 0;
865           new_value.type = DEBUG_ParseStabType(ptr);
866           new_value.addr.off = load_offset + stab_ptr->n_value;
867           new_value.cookie = DV_TARGET;
868
869           stab_strcpy(symname, sizeof(symname), ptr);
870 #ifdef __ELF__
871           curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
872                                       SYM_WINE | SYM_DATA | SYM_INVALID );
873 #else
874           curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
875                                       SYM_WINE | SYM_DATA );
876 #endif
877           break;
878         case N_RBRAC:
879         case N_LBRAC:
880           /*
881            * We need to keep track of these so we get symbol scoping
882            * right for local variables.  For now, we just ignore them.
883            * The hooks are already there for dealing with this however,
884            * so all we need to do is to keep count of the nesting level,
885            * and find the RBRAC for each matching LBRAC.
886            */
887           break;
888         case N_LCSYM:
889         case N_STSYM:
890           /*
891            * These are static symbols and BSS symbols.
892            */
893           new_value.addr.seg = 0;
894           new_value.type = DEBUG_ParseStabType(ptr);
895           new_value.addr.off = load_offset + stab_ptr->n_value;
896           new_value.cookie = DV_TARGET;
897
898           stab_strcpy(symname, sizeof(symname), ptr);
899           curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath, 
900                                       SYM_WINE | SYM_DATA );
901           break;
902         case N_PSYM:
903           /*
904            * These are function parameters.
905            */
906           if( curr_func != NULL && !in_external_file )
907             {
908               stab_strcpy(symname, sizeof(symname), ptr);
909               curr_loc = DEBUG_AddLocal( curr_func, 0, 
910                                          stab_ptr->n_value, 0, 0, symname );
911               DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
912             }
913           break;
914         case N_RSYM:
915           if( curr_func != NULL && !in_external_file )
916             {
917               stab_strcpy(symname, sizeof(symname), ptr);
918               curr_loc = DEBUG_AddLocal( curr_func, stab_ptr->n_value + 1, 
919                                          0, 0, 0, symname );
920               DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
921             }
922           break;
923         case N_LSYM:
924           if( curr_func != NULL && !in_external_file )
925             {
926               stab_strcpy(symname, sizeof(symname), ptr);
927               curr_loc = DEBUG_AddLocal( curr_func, 0, 
928                                          stab_ptr->n_value, 0, 0, symname );
929               DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
930             }
931           break;
932         case N_SLINE:
933           /*
934            * This is a line number.  These are always relative to the start
935            * of the function (N_FUN), and this makes the lookup easier.
936            */
937           if( curr_func != NULL && !in_external_file )
938             {
939 #ifdef __ELF__
940               DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc, 
941                                   stab_ptr->n_value);
942 #else
943 #if 0
944               /*
945                * This isn't right.  The order of the stabs is different under
946                * a.out, and as a result we would end up attaching the line
947                * number to the wrong function.
948                */
949               DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc, 
950                                   stab_ptr->n_value - curr_func->addr.off);
951 #endif
952 #endif
953             }
954           break;
955         case N_FUN:
956           /*
957            * First, clean up the previous function we were working on.
958            */
959           DEBUG_Normalize(curr_func);
960
961           /*
962            * For now, just declare the various functions.  Later
963            * on, we will add the line number information and the
964            * local symbols.
965            */
966           if( !in_external_file)
967             {
968               stab_strcpy(symname, sizeof(symname), ptr);
969               if (*symname)
970                 {
971                   new_value.addr.seg = 0;
972                   new_value.type = DEBUG_ParseStabType(ptr);
973                   new_value.addr.off = load_offset + stab_ptr->n_value;
974                   new_value.cookie = DV_TARGET;
975                   /*
976                    * Copy the string to a temp buffer so we
977                    * can kill everything after the ':'.  We do
978                    * it this way because otherwise we end up dirtying
979                    * all of the pages related to the stabs, and that
980                    * sucks up swap space like crazy.
981                    */
982                   curr_func = DEBUG_AddSymbol( symname, &new_value, currpath,
983                                                SYM_WINE | SYM_FUNC);
984                 } 
985               else
986                 {
987                   /* some GCC seem to use a N_FUN "" to mark the end of a function */
988                   curr_func = NULL;
989                 }
990             }
991           else
992             {
993               /*
994                * Don't add line number information for this function
995                * any more.
996                */
997               curr_func = NULL;
998             }
999           break;
1000         case N_SO:
1001           /*
1002            * This indicates a new source file.  Append the records
1003            * together, to build the correct path name.
1004            */
1005 #ifndef __ELF__
1006           /*
1007            * With a.out, there is no NULL string N_SO entry at the end of
1008            * the file.  Thus when we find non-consecutive entries,
1009            * we consider that a new file is started.
1010            */
1011           if( last_nso < i-1 )
1012             {
1013               currpath[0] = '\0';
1014               DEBUG_Normalize(curr_func);
1015               curr_func = NULL;
1016             }
1017 #endif
1018
1019           if( *ptr == '\0' )
1020             {
1021               /*
1022                * Nuke old path.
1023                */
1024               currpath[0] = '\0';
1025               DEBUG_Normalize(curr_func);
1026               curr_func = NULL;
1027             }
1028           else
1029             {
1030               if (*ptr != '/')
1031                 strcat(currpath, ptr);
1032               else
1033                 strcpy(currpath, ptr);
1034               subpath = ptr;
1035               DEBUG_ResetIncludes();
1036             }
1037           last_nso = i;
1038           break;
1039         case N_SOL:
1040           /*
1041            * This indicates we are including stuff from an include file.
1042            * If this is the main source, enable the debug stuff, otherwise
1043            * ignore it.
1044            */
1045           in_external_file = !(subpath == NULL || strcmp(ptr, subpath) == 0);
1046           break;
1047         case N_UNDF:
1048           strs += strtabinc;
1049           strtabinc = stab_ptr->n_value;
1050           DEBUG_Normalize(curr_func);
1051           curr_func = NULL;
1052           break;
1053         case N_OPT:
1054           /*
1055            * Ignore this.  We don't care what it points to.
1056            */
1057           break;
1058         case N_BINCL:
1059            DEBUG_AddInclude(DEBUG_CreateInclude(ptr, stab_ptr->n_value));
1060            break;
1061         case N_EINCL:
1062            break;
1063         case N_EXCL:
1064            DEBUG_AddInclude(DEBUG_FindInclude(ptr, stab_ptr->n_value));
1065            break;
1066         case N_MAIN:
1067           /*
1068            * Always ignore these.  GCC doesn't even generate them.
1069            */
1070           break;
1071         default:
1072           DEBUG_Printf(DBG_CHN_MESG, "Unkown stab type 0x%02x\n", stab_ptr->n_type);
1073           break;
1074         }
1075
1076       stabbuff[0] = '\0';
1077
1078 #if 0
1079       DEBUG_Printf(DBG_CHN_MESG, "%d %x %s\n", stab_ptr->n_type, 
1080                    (unsigned int) stab_ptr->n_value,
1081                    strs + (unsigned int) stab_ptr->n_un.n_name);
1082 #endif
1083     }
1084
1085   DEBUG_FreeRegisteredTypedefs();
1086   DEBUG_FreeIncludes();
1087   DBG_free(curr_types);
1088   curr_types = NULL;
1089   allocated_types = 0;
1090
1091   return TRUE;
1092 }
1093
1094 #ifdef __ELF__
1095
1096 /*
1097  * Walk through the entire symbol table and add any symbols we find there.
1098  * This can be used in cases where we have stripped ELF shared libraries,
1099  * or it can be used in cases where we have data symbols for which the address
1100  * isn't encoded in the stabs.
1101  *
1102  * This is all really quite easy, since we don't have to worry about line
1103  * numbers or local data variables.
1104  */
1105 static
1106 int
1107 DEBUG_ProcessElfSymtab(char * addr, unsigned int load_offset,
1108                        Elf32_Shdr * symtab, Elf32_Shdr * strtab)
1109 {
1110   char          * curfile = NULL;
1111   struct name_hash * curr_sym = NULL;
1112   int             flags;
1113   int             i;
1114   DBG_VALUE       new_value;
1115   int             nsym;
1116   char          * strp;
1117   char          * symname;
1118   Elf32_Sym     * symp;
1119
1120
1121   symp = (Elf32_Sym *) (addr + symtab->sh_offset);
1122   nsym = symtab->sh_size / sizeof(*symp);
1123   strp = (char *) (addr + strtab->sh_offset);
1124
1125   for(i=0; i < nsym; i++, symp++)
1126     {
1127       /*
1128        * Ignore certain types of entries which really aren't of that much
1129        * interest.
1130        */
1131       if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION ||
1132           symp->st_shndx == STN_UNDEF )
1133         {
1134           continue;
1135         }
1136
1137       symname = strp + symp->st_name;
1138
1139       /*
1140        * Save the name of the current file, so we have a way of tracking
1141        * static functions/data.
1142        */
1143       if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
1144         {
1145           curfile = symname;
1146           continue;
1147         }
1148
1149       /*
1150        * See if we already have something for this symbol.
1151        * If so, ignore this entry, because it would have come from the
1152        * stabs or from a previous symbol.  If the value is different,
1153        * we will have to keep the darned thing, because there can be
1154        * multiple local symbols by the same name.
1155        */
1156       if(    (DEBUG_GetSymbolValue(symname, -1, &new_value, FALSE ) == TRUE)
1157           && (new_value.addr.off == (load_offset + symp->st_value)) )
1158           continue;
1159
1160       new_value.addr.seg = 0;
1161       new_value.type = NULL;
1162       new_value.addr.off = load_offset + symp->st_value;
1163       new_value.cookie = DV_TARGET;
1164       flags = SYM_WINE | (ELF32_ST_BIND(symp->st_info) == STT_FUNC 
1165                           ? SYM_FUNC : SYM_DATA);
1166       if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
1167           curr_sym = DEBUG_AddSymbol( symname, &new_value, NULL, flags );
1168       else
1169           curr_sym = DEBUG_AddSymbol( symname, &new_value, curfile, flags );
1170
1171       /*
1172        * Record the size of the symbol.  This can come in handy in
1173        * some cases.  Not really used yet, however.
1174        */
1175       if( symp->st_size != 0 )
1176           DEBUG_SetSymbolSize(curr_sym, symp->st_size);
1177     }
1178
1179   return TRUE;
1180 }
1181
1182 /*
1183  * Loads the symbolic information from ELF module stored in 'filename'
1184  * the module has been loaded at 'load_offset' address, so symbols' address
1185  * relocation is performed
1186  * returns 
1187  *      -1 if the file cannot be found/opened
1188  *      0 if the file doesn't contain symbolic info (or this info cannot be 
1189  *      read or parsed)
1190  *      1 on success
1191  */
1192 static int DEBUG_ProcessElfFile(const char * filename, unsigned int load_offset)
1193 {
1194   int                   rtn = -1;
1195   char                * addr = (char*)0xffffffff;
1196   int                   fd = -1;
1197   struct stat           statbuf;
1198   Elf32_Ehdr          * ehptr;
1199   Elf32_Shdr          * spnt;
1200   char                * shstrtab;
1201   int                   nsect;
1202   int                   i;
1203   int                   stabsect;
1204   int                   stabstrsect;
1205   
1206   /* check that the file exists, and that the module hasn't been loaded yet */
1207   if (stat(filename, &statbuf) == -1) goto leave;
1208   
1209   /*
1210    * Now open the file, so that we can mmap() it.
1211    */
1212   if ((fd = open(filename, O_RDONLY)) == -1) goto leave;
1213   
1214   rtn = 0;
1215   /*
1216    * Now mmap() the file.
1217    */
1218   addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1219   if (addr == (char*)0xffffffff) goto leave;
1220   
1221   /*
1222    * Next, we need to find a few of the internal ELF headers within
1223    * this thing.  We need the main executable header, and the section
1224    * table.
1225    */
1226   ehptr = (Elf32_Ehdr *) addr;
1227   
1228   DEBUG_RegisterELFModule((load_offset == 0) ? ehptr->e_entry : load_offset, filename);
1229   
1230   spnt = (Elf32_Shdr *) (addr + ehptr->e_shoff);
1231   nsect = ehptr->e_shnum;
1232   shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1233   
1234   stabsect = stabstrsect = -1;
1235   
1236   for (i = 0; i < nsect; i++) {
1237     if (strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0)
1238       stabsect = i;
1239
1240     if (strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0)
1241       stabstrsect = i;
1242   }
1243
1244   if (stabsect == -1 || stabstrsect == -1) {
1245     DEBUG_Printf(DBG_CHN_WARN, "no .stab section\n");
1246     goto leave;
1247   }
1248
1249   /*
1250    * OK, now just parse all of the stabs.
1251    */
1252   if (!(rtn = DEBUG_ParseStabs(addr, load_offset, 
1253                                spnt[stabsect].sh_offset,
1254                                spnt[stabsect].sh_size,
1255                                spnt[stabstrsect].sh_offset,
1256                                spnt[stabstrsect].sh_size))) {
1257     DEBUG_Printf(DBG_CHN_WARN, "bad stabs\n");
1258     goto leave;
1259   }
1260
1261   for (i = 0; i < nsect; i++) {
1262     if (   (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1263         && (spnt[i].sh_type == SHT_SYMTAB))
1264       DEBUG_ProcessElfSymtab(addr, load_offset, 
1265                              spnt + i, spnt + spnt[i].sh_link);
1266
1267     if (   (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1268         && (spnt[i].sh_type == SHT_DYNSYM))
1269       DEBUG_ProcessElfSymtab(addr, load_offset, 
1270                              spnt + i, spnt + spnt[i].sh_link);
1271   }
1272
1273 leave:
1274   if (addr != (char*)0xffffffff) munmap(addr, statbuf.st_size);
1275   if (fd != -1) close(fd);
1276
1277   return rtn;
1278 }
1279
1280 static int DEBUG_ProcessElfFileFromPath(const char * filename, 
1281                                         unsigned int load_offset, const char* path)
1282 {
1283    int          rtn = -1;
1284    char         *s, *t, *fn;
1285    char*        paths = NULL;
1286
1287    if (!path) return -1;
1288
1289    for (s = paths = DBG_strdup(path); s && *s; s = (t) ? (t+1) : NULL) {
1290       t = strchr(s, ':');
1291       if (t) *t = '\0';
1292       fn = (char*)DBG_alloc(strlen(filename) + 1 + strlen(s) + 1);
1293       if (!fn) break;
1294       strcpy(fn, s );
1295       strcat(fn, "/");
1296       strcat(fn, filename);
1297       rtn = DEBUG_ProcessElfFile(fn, load_offset);
1298       DBG_free(fn);
1299       if (rtn >= 0) break;
1300       s = (t) ? (t+1) : NULL;
1301    }
1302
1303    DBG_free(paths);
1304    return rtn;
1305 }
1306
1307 static int DEBUG_ProcessElfObject(const char * filename, unsigned int load_offset)
1308 {
1309    int          rtn = -1;
1310    const char*  fmt;
1311
1312    DEBUG_Printf(DBG_CHN_TRACE, "Processing elf file '%s'\n", filename);
1313
1314    if (filename == NULL) return FALSE;
1315    if (DEBUG_FindModuleByName(filename, DM_TYPE_ELF)) return TRUE;
1316
1317    rtn = DEBUG_ProcessElfFile(filename, load_offset);
1318
1319    /* if relative pathname, try some absolute base dirs */
1320    if (rtn < 0 && !strchr(filename, '/')) {
1321       rtn = DEBUG_ProcessElfFileFromPath(filename, load_offset, getenv("PATH"));
1322       if (rtn < 0)
1323         rtn = DEBUG_ProcessElfFileFromPath(filename, load_offset, getenv("LD_LIBRARY_PATH"));
1324    }
1325
1326    switch (rtn) {
1327    case 1: fmt = "Loaded stabs debug symbols from ELF '%s' (0x%08x)\n"; break;
1328    case 0: fmt = "No stabs debug symbols in ELF '%s' (0x%08x)\n";       break;
1329    case -1:fmt = "Can't find file for ELF '%s' (0x%08x)\n";             break;
1330    default: DEBUG_Printf(DBG_CHN_ERR, "Oooocch (%d)\n", rtn); return FALSE;
1331    }
1332    
1333    DEBUG_Printf(DBG_CHN_MESG, fmt, filename, load_offset);
1334
1335    return rtn >= 0;
1336 }
1337    
1338 static  BOOL    DEBUG_WalkList(struct r_debug* dbg_hdr)
1339 {
1340   u_long                lm_addr;
1341   struct link_map       lm;
1342   Elf32_Ehdr            ehdr;
1343   char                  bufstr[256];
1344
1345   /*
1346    * Now walk the linked list.  In all known ELF implementations,
1347    * the dynamic loader maintains this linked list for us.  In some
1348    * cases the first entry doesn't appear with a name, in other cases it
1349    * does.
1350    */
1351   for (lm_addr = (u_long)dbg_hdr->r_map; lm_addr; lm_addr = (u_long)lm.l_next) {
1352     if (!DEBUG_READ_MEM_VERBOSE((void*)lm_addr, &lm, sizeof(lm)))
1353       return FALSE;
1354     if (lm.l_addr != 0 &&
1355         DEBUG_READ_MEM_VERBOSE((void*)lm.l_addr, &ehdr, sizeof(ehdr)) &&
1356         ehdr.e_type == ET_DYN && /* only look at dynamic modules */
1357         lm.l_name != NULL &&
1358         DEBUG_READ_MEM_VERBOSE(lm.l_name, bufstr, sizeof(bufstr))) {
1359       bufstr[sizeof(bufstr) - 1] = '\0';
1360       DEBUG_ProcessElfObject(bufstr, lm.l_addr);
1361     }
1362   }
1363   
1364   return TRUE;
1365 }
1366
1367 static BOOL DEBUG_RescanElf(void)
1368 {
1369     struct r_debug        dbg_hdr;
1370
1371     if (!DEBUG_CurrProcess || 
1372         !DEBUG_READ_MEM_VERBOSE((void*)DEBUG_CurrProcess->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)))
1373        return FALSE;
1374
1375     switch (dbg_hdr.r_state) {
1376     case RT_CONSISTENT: 
1377        DEBUG_WalkList(&dbg_hdr);
1378        break;
1379     case RT_ADD:
1380        break;
1381     case RT_DELETE:
1382        /*FIXME: this is not currently handled, would need some kind of mark&sweep algo */
1383       break;
1384     }
1385     return FALSE;
1386 }
1387
1388 int
1389 DEBUG_ReadExecutableDbgInfo(const char* exe_name)
1390 {
1391   Elf32_Dyn             dyn;
1392   struct r_debug        dbg_hdr;
1393   int                   rtn = FALSE;
1394   DBG_VALUE             val;
1395
1396   /*
1397    * Make sure we can stat and open this file.
1398    */
1399   if (exe_name == NULL) goto leave;
1400   DEBUG_ProcessElfObject(exe_name, 0);
1401
1402   /* previous step should have loaded symbol _DYNAMIC if it exists inside 
1403    * the main executable
1404    */
1405   if (!DEBUG_GetSymbolValue("_DYNAMIC", -1, &val, FALSE)) {
1406     DEBUG_Printf(DBG_CHN_WARN, "Can't find symbol _DYNAMIC\n");
1407     goto leave;
1408   }
1409
1410   do {
1411     if (!DEBUG_READ_MEM_VERBOSE((void*)val.addr.off, &dyn, sizeof(dyn)))
1412       goto leave;
1413     val.addr.off += sizeof(dyn);
1414   } while (dyn.d_tag != DT_DEBUG && dyn.d_tag != DT_NULL);
1415   if (dyn.d_tag == DT_NULL) goto leave;
1416
1417   /*
1418    * OK, now dig into the actual tables themselves.
1419    */
1420   if (!DEBUG_READ_MEM_VERBOSE((void*)dyn.d_un.d_ptr, &dbg_hdr, sizeof(dbg_hdr)))
1421     goto leave;
1422
1423   assert(!DEBUG_CurrProcess->dbg_hdr_addr);
1424   DEBUG_CurrProcess->dbg_hdr_addr = (u_long)dyn.d_un.d_ptr;
1425
1426   if (dbg_hdr.r_brk) {
1427     DBG_VALUE   value;
1428
1429     DEBUG_Printf(DBG_CHN_TRACE, "Setting up a breakpoint on r_brk(%lx)\n",
1430                  (unsigned long)dbg_hdr.r_brk);
1431
1432     DEBUG_SetBreakpoints(FALSE);
1433     value.type = NULL;
1434     value.cookie = DV_TARGET;
1435     value.addr.seg = 0;
1436     value.addr.off = dbg_hdr.r_brk;
1437     DEBUG_AddBreakpoint(&value, DEBUG_RescanElf);
1438     DEBUG_SetBreakpoints(TRUE);
1439   }
1440
1441   rtn = DEBUG_WalkList(&dbg_hdr);
1442
1443 leave:
1444   return rtn;
1445 }
1446
1447 #else   /* !__ELF__ */
1448
1449 int
1450 DEBUG_ReadExecutableDbgInfo(const char* exe_name)
1451 {
1452   return FALSE;
1453 }
1454
1455 #endif  /* __ELF__ */