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