Corrected arguments to VirtualFree().
[wine/testsucceed.git] / debugger / stabs.c
blobcb5f7f0bb9dc866d2b9a732e141b4f2f7769b26e
1 /* -*- tab-width: 8; c-basic-offset: 2 -*- */
3 /*
4 * File stabs.c - read stabs information from the wine executable itself.
6 * Copyright (C) 1996, Eric Youngdale.
7 */
9 #include "config.h"
11 #include <assert.h>
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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #ifndef PATH_MAX
24 #define PATH_MAX _MAX_PATH
25 #endif
27 #include "debugger.h"
29 #if defined(__svr4__) || defined(__sun)
30 #define __ELF__
31 #endif
33 #ifdef __ELF__
34 #ifdef HAVE_ELF_H
35 # include <elf.h>
36 #endif
37 #ifdef HAVE_LINK_H
38 # include <link.h>
39 #endif
40 #elif defined(__EMX__)
41 #ifdef HAVE_A_OUT_H
42 # include <a_out.h>
43 #endif
44 #else
45 #ifdef HAVE_A_OUT_H
46 # include <a.out.h>
47 #endif
48 #endif
50 #ifndef N_UNDF
51 #define N_UNDF 0x00
52 #endif
54 #define N_GSYM 0x20
55 #define N_FUN 0x24
56 #define N_STSYM 0x26
57 #define N_LCSYM 0x28
58 #define N_MAIN 0x2a
59 #define N_ROSYM 0x2c
60 #define N_OPT 0x3c
61 #define N_RSYM 0x40
62 #define N_SLINE 0x44
63 #define N_SO 0x64
64 #define N_LSYM 0x80
65 #define N_BINCL 0x82
66 #define N_SOL 0x84
67 #define N_PSYM 0xa0
68 #define N_EINCL 0xa2
69 #define N_LBRAC 0xc0
70 #define N_EXCL 0xc2
71 #define N_RBRAC 0xe0
74 * Set so that we know the main executable name and path.
76 char * DEBUG_argv0;
78 struct stab_nlist {
79 union {
80 char *n_name;
81 struct stab_nlist *n_next;
82 long n_strx;
83 } n_un;
84 unsigned char n_type;
85 char n_other;
86 short n_desc;
87 unsigned long n_value;
91 * This is used to keep track of known datatypes so that we don't redefine
92 * them over and over again. It sucks up lots of memory otherwise.
94 struct known_typedef
96 struct known_typedef * next;
97 char * name;
98 int ndefs;
99 struct datatype * types[1];
102 #define NR_STAB_HASH 521
104 struct known_typedef * ktd_head[NR_STAB_HASH] = {NULL,};
106 static unsigned int stab_hash( const char * name )
108 unsigned int hash = 0;
109 unsigned int tmp;
110 const char * p;
112 p = name;
114 while (*p)
116 hash = (hash << 4) + *p++;
118 if( (tmp = (hash & 0xf0000000)) )
120 hash ^= tmp >> 24;
122 hash &= ~tmp;
124 return hash % NR_STAB_HASH;
128 static void stab_strcpy(char * dest, const char * source)
131 * A strcpy routine that stops when we hit the ':' character.
132 * Faster than copying the whole thing, and then nuking the
133 * ':'.
135 while(*source != '\0' && *source != ':')
136 *dest++ = *source++;
137 *dest++ = '\0';
140 extern void DEBUG_PrintAType(struct datatype*, int);
142 typedef struct {
143 char* name;
144 unsigned long value;
145 int idx;
146 struct datatype** vector;
147 int nrofentries;
148 } include_def;
150 #define MAX_INCLUDES 256
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;
160 static
161 int
162 DEBUG_CreateInclude(const char* file, unsigned long val)
164 if (num_include_def == num_alloc_include_def)
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);
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;
175 return num_include_def++;
178 static
179 int
180 DEBUG_FindInclude(const char* file, unsigned long val)
182 int i;
184 for (i = 0; i < num_include_def; i++)
186 if (val == include_defs[i].value &&
187 strcmp(file, include_defs[i].name) == 0)
188 return i;
190 return -1;
193 static
195 DEBUG_AddInclude(int idx)
197 ++cu_include_stk_idx;
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);
203 cu_include_stack[cu_include_stk_idx] = idx;
204 return cu_include_stk_idx;
207 static
208 void
209 DEBUG_ResetIncludes(void)
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
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);
219 static
220 void
221 DEBUG_FreeIncludes(void)
223 int i;
225 DEBUG_ResetIncludes();
227 for (i = 0; i < num_include_def; i++)
229 DBG_free(include_defs[i].name);
230 DBG_free(include_defs[i].vector);
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;
241 #define MAX_TD_NESTING 128
243 static
244 struct datatype**
245 DEBUG_FileSubNr2StabEnum(int filenr, int subnr)
247 struct datatype** ret;
249 /* fprintf(stderr, "creating type id for (%d,%d)\n", filenr, subnr); */
251 /* FIXME: I could perhaps create a dummy include_def for each compilation
252 * unit which would allow not to handle those two cases separately
254 if (filenr == 0)
256 if (cu_nrofentries <= subnr)
258 cu_vector = DBG_realloc(cu_vector, sizeof(cu_vector[0])*(subnr+1));
259 memset(cu_vector+cu_nrofentries, 0, sizeof(cu_vector[0])*(subnr+1-cu_nrofentries));
260 cu_nrofentries = subnr + 1;
262 ret = &cu_vector[subnr];
264 else
266 include_def* idef;
268 assert(filenr <= cu_include_stk_idx);
270 idef = &include_defs[cu_include_stack[filenr]];
272 if (idef->nrofentries <= subnr)
274 idef->vector = DBG_realloc(idef->vector, sizeof(idef->vector[0])*(subnr+1));
275 memset(idef->vector + idef->nrofentries, 0, sizeof(idef->vector[0])*(subnr+1-idef->nrofentries));
276 idef->nrofentries = subnr + 1;
278 ret = &idef->vector[subnr];
280 /* fprintf(stderr,"(%d,%d) is %d\n",filenr,subnr,ret); */
281 return ret;
284 static
285 struct datatype**
286 DEBUG_ReadTypeEnumBackwards(char*x) {
287 int filenr,subnr;
289 if (*x==')') {
290 while (*x!='(')
291 x--;
292 x++; /* '(' */
293 filenr=strtol(x,&x,10); /* <int> */
294 x++; /* ',' */
295 subnr=strtol(x,&x,10); /* <int> */
296 x++; /* ')' */
297 } else {
298 while ((*x>='0') && (*x<='9'))
299 x--;
300 filenr = 0;
301 subnr = atol(x+1);
303 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
306 static
307 struct datatype**
308 DEBUG_ReadTypeEnum(char **x) {
309 int filenr,subnr;
311 if (**x=='(') {
312 (*x)++; /* '(' */
313 filenr=strtol(*x,x,10); /* <int> */
314 (*x)++; /* ',' */
315 subnr=strtol(*x,x,10); /* <int> */
316 (*x)++; /* ')' */
317 } else {
318 filenr = 0;
319 subnr = strtol(*x,x,10); /* <int> */
321 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
324 static
326 DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef)
328 int hash;
329 struct known_typedef * ktd;
331 if( ndef == 1 )
332 return TRUE;
334 ktd = (struct known_typedef *) DBG_alloc(sizeof(struct known_typedef)
335 + (ndef - 1) * sizeof(struct datatype *));
337 hash = stab_hash(name);
339 ktd->name = DBG_strdup(name);
340 ktd->ndefs = ndef;
341 memcpy(&ktd->types[0], types, ndef * sizeof(struct datatype *));
342 ktd->next = ktd_head[hash];
343 ktd_head[hash] = ktd;
345 return TRUE;
348 static
350 DEBUG_HandlePreviousTypedef(const char * name, const char * stab)
352 int count;
353 enum debug_type expect;
354 int hash;
355 struct known_typedef * ktd;
356 char * ptr;
358 hash = stab_hash(name);
360 for(ktd = ktd_head[hash]; ktd; ktd = ktd->next)
361 if ((ktd->name[0] == name[0]) && (strcmp(name, ktd->name) == 0) )
362 break;
365 * Didn't find it. This must be a new one.
367 if( ktd == NULL )
368 return FALSE;
371 * Examine the stab to make sure it has the same number of definitions.
373 count = 0;
374 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
376 if( count >= ktd->ndefs )
377 return FALSE;
380 * Make sure the types of all of the objects is consistent with
381 * what we have already parsed.
383 switch(ptr[1])
385 case '*':
386 expect = DT_POINTER;
387 break;
388 case 's':
389 case 'u':
390 expect = DT_STRUCT;
391 break;
392 case 'a':
393 expect = DT_ARRAY;
394 break;
395 case '(': /* it's mainly a ref to another typedef, skip it */
396 expect = -1;
397 break;
398 case '1':
399 case 'r':
400 expect = DT_BASIC;
401 break;
402 case 'x':
403 expect = DT_STRUCT;
404 break;
405 case 'e':
406 expect = DT_ENUM;
407 break;
408 case 'f':
409 expect = DT_FUNC;
410 break;
411 default:
412 fprintf(stderr, "Unknown type (%c).\n",ptr[1]);
413 return FALSE;
415 if( expect != -1 && expect != DEBUG_GetType(ktd->types[count]) )
416 return FALSE;
417 count++;
420 if( ktd->ndefs != count )
421 return FALSE;
424 * Go through, dig out all of the type numbers, and substitute the
425 * appropriate things.
427 count = 0;
428 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
429 *DEBUG_ReadTypeEnumBackwards(ptr-1) = ktd->types[count++];
431 return TRUE;
434 static int DEBUG_FreeRegisteredTypedefs()
436 int count;
437 int j;
438 struct known_typedef * ktd;
439 struct known_typedef * next;
441 count = 0;
442 for(j=0; j < NR_STAB_HASH; j++ )
444 for(ktd = ktd_head[j]; ktd; ktd = next)
446 count++;
447 next = ktd->next;
448 DBG_free(ktd->name);
449 DBG_free(ktd);
451 ktd_head[j] = NULL;
454 return TRUE;
458 static
460 DEBUG_ParseTypedefStab(char * ptr, const char * typename)
462 int arrmax;
463 int arrmin;
464 char * c;
465 struct datatype * curr_type;
466 struct datatype * datatype;
467 struct datatype * curr_types[MAX_TD_NESTING];
468 char element_name[1024];
469 int ntypes = 0, ntp;
470 int offset;
471 const char * orig_typename;
472 int size;
473 char * tc;
474 char * tc2;
475 int failure;
477 orig_typename = typename;
479 if( DEBUG_HandlePreviousTypedef(typename, ptr) )
480 return TRUE;
483 * Go from back to front. First we go through and figure out what
484 * type numbers we need, and register those types. Then we go in
485 * and fill the details.
488 for( c = strchr(ptr, '='); c != NULL; c = strchr(c + 1, '=') )
491 * Back up until we get to a non-numeric character, to get datatype
493 struct datatype** dt = DEBUG_ReadTypeEnumBackwards(c-1);
495 if( ntypes >= MAX_TD_NESTING )
498 * If this ever happens, just bump the counter.
500 fprintf(stderr, "Typedef nesting overflow\n");
501 return FALSE;
504 switch(c[1])
506 case '*':
507 *dt = DEBUG_NewDataType(DT_POINTER, NULL);
508 curr_types[ntypes++] = *dt;
509 break;
510 case 's':
511 case 'u':
512 *dt = DEBUG_NewDataType(DT_STRUCT, typename);
513 curr_types[ntypes++] = *dt;
514 break;
515 case 'a':
516 *dt = DEBUG_NewDataType(DT_ARRAY, NULL);
517 curr_types[ntypes++] = *dt;
518 break;
519 case '(':
520 /* will be handled in next loop,
521 * just a ref to another type
523 curr_types[ntypes++] = NULL;
524 break;
525 case '1':
526 case 'r':
527 *dt = DEBUG_NewDataType(DT_BASIC, typename);
528 curr_types[ntypes++] = *dt;
529 break;
530 case 'x':
531 stab_strcpy(element_name, c + 3);
532 *dt = DEBUG_NewDataType(DT_STRUCT, element_name);
533 curr_types[ntypes++] = *dt;
534 break;
535 case 'e':
536 *dt = DEBUG_NewDataType(DT_ENUM, NULL);
537 curr_types[ntypes++] = *dt;
538 break;
539 case 'f':
540 *dt = DEBUG_NewDataType(DT_FUNC, NULL);
541 curr_types[ntypes++] = *dt;
542 break;
543 default:
544 fprintf(stderr, "Unknown type (%c).\n",c[1]);
546 typename = NULL;
550 ntp = ntypes - 1;
552 * OK, now take a second sweep through. Now we will be digging
553 * out the definitions of the various components, and storing
554 * them in the skeletons that we have already allocated. We take
555 * a right-to left search as this is much easier to parse.
557 for( c = strrchr(ptr, '='); c != NULL; c = strrchr(ptr, '=') )
559 struct datatype** dt = DEBUG_ReadTypeEnumBackwards(c-1);
560 struct datatype** dt2;
562 curr_type = *dt;
564 switch(c[1])
566 case 'x':
567 ntp--;
568 tc = c + 3;
569 while( *tc != ':' )
570 tc++;
571 tc++;
572 if( *tc == '\0' )
573 *c = '\0';
574 else
575 strcpy(c, tc);
576 break;
577 case '*':
578 case 'f':
579 ntp--;
580 tc = c + 2;
581 datatype = *DEBUG_ReadTypeEnum(&tc);
582 DEBUG_SetPointerType(curr_type, datatype);
583 if( *tc == '\0' )
584 *c = '\0';
585 else
586 strcpy(c, tc);
587 break;
588 case '(':
589 tc = c + 1;
590 dt2 = DEBUG_ReadTypeEnum(&tc);
592 if (!*dt && *dt2)
594 *dt = *dt2;
596 else if (!*dt && !*dt2)
598 /* this should be a basic type, define it */
599 *dt2 = *dt = DEBUG_NewDataType(DT_BASIC, typename);
601 else
603 fprintf(stderr, "Unknown condition %p %p (%s)\n", *dt, *dt2, ptr);
605 if( *tc == '\0' )
606 *c = '\0';
607 else
608 strcpy(c, tc);
609 curr_types[ntp--] = *dt;
610 break;
611 case '1':
612 case 'r':
613 ntp--;
615 * We have already handled these above.
617 *c = '\0';
618 break;
619 case 'a':
620 ntp--;
621 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */
623 tc = c + 3;
624 /* 'r' */
625 DEBUG_ReadTypeEnum(&tc);
626 tc++; /* ';' */
627 arrmin = strtol(tc, &tc, 10); /* <int> */
628 tc++; /* ';' */
629 arrmax = strtol(tc, &tc, 10); /* <int> */
630 tc++; /* ';' */
631 datatype = *DEBUG_ReadTypeEnum(&tc); /* <typeinfo> */
632 if( *tc == '\0' )
633 *c = '\0';
634 else
635 strcpy(c, tc);
636 DEBUG_SetArrayParams(curr_type, arrmin, arrmax, datatype);
637 break;
638 case 's':
639 case 'u':
640 ntp--;
641 failure = 0;
643 tc = c + 2;
644 if( DEBUG_SetStructSize(curr_type, strtol(tc, &tc, 10)) == FALSE )
647 * We have already filled out this structure. Nothing to do,
648 * so just skip forward to the end of the definition.
650 while( tc[0] != ';' && tc[1] != ';' )
651 tc++;
653 tc += 2;
655 if( *tc == '\0' )
656 *c = '\0';
657 else
658 strcpy(c, tc + 1);
659 continue;
663 * Now parse the individual elements of the structure/union.
665 while(*tc != ';')
667 char *ti;
668 tc2 = element_name;
669 while(*tc != ':')
670 *tc2++ = *tc++;
671 tc++;
672 *tc2++ = '\0';
673 ti=tc;
674 datatype = *DEBUG_ReadTypeEnum(&tc);
675 *tc='\0';
676 tc++;
677 offset = strtol(tc, &tc, 10);
678 tc++;
679 size = strtol(tc, &tc, 10);
680 tc++;
681 if (datatype)
682 DEBUG_AddStructElement(curr_type, element_name, datatype,
683 offset, size);
684 else
686 failure = 1;
687 /* ... but proceed parsing to the end of the stab */
688 fprintf(stderr, "failure on %s %s\n", ptr, ti);
692 if (failure)
695 /* if we had a undeclared value this one is undeclared too.
696 * remove it from the stab_types.
697 * I just set it to NULL to detect bugs in my thoughtprocess.
698 * FIXME: leaks the memory for the structure elements.
699 * FIXME: such structures should have been optimized away
700 * by ld.
702 *dt = NULL;
704 if( *tc == '\0' )
705 *c = '\0';
706 else
707 strcpy(c, tc + 1);
708 break;
709 case 'e':
710 ntp--;
711 tc = c + 2;
713 * Now parse the individual elements of the structure/union.
715 while(*tc != ';')
717 tc2 = element_name;
718 while(*tc != ':')
719 *tc2++ = *tc++;
720 tc++;
721 *tc2++ = '\0';
722 offset = strtol(tc, &tc, 10);
723 tc++;
724 DEBUG_AddStructElement(curr_type, element_name, NULL, offset, 0);
726 if( *tc == '\0' )
727 *c = '\0';
728 else
729 strcpy(c, tc + 1);
730 break;
731 default:
732 fprintf(stderr, "Unknown type (%c).\n",c[1]);
733 break;
737 * Now register the type so that if we encounter it again, we will know
738 * what to do.
740 DEBUG_RegisterTypedef(orig_typename, curr_types, ntypes);
742 return TRUE;
745 static struct datatype *
746 DEBUG_ParseStabType(const char * stab)
748 char * c;
751 * Look through the stab definition, and figure out what datatype
752 * this represents. If we have something we know about, assign the
753 * type.
755 c = strchr(stab, ':');
756 if( c == NULL )
757 return NULL;
759 c++;
761 * The next character says more about the type (i.e. data, function, etc)
762 * of symbol. Skip it.
764 if (*c != '(')
765 c++;
767 * The next is either an integer or a (integer,integer).
768 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
770 return *DEBUG_ReadTypeEnum(&c);
774 DEBUG_ParseStabs(char * addr, unsigned int load_offset,
775 unsigned int staboff, int stablen,
776 unsigned int strtaboff, int strtablen)
778 struct name_hash * curr_func = NULL;
779 struct wine_locals * curr_loc = NULL;
780 struct name_hash * curr_sym = NULL;
781 char currpath[PATH_MAX];
782 int i;
783 int ignore = FALSE;
784 int last_nso = -1;
785 int len;
786 DBG_ADDR new_addr;
787 int nstab;
788 char * ptr;
789 char * stabbuff;
790 int stabbufflen;
791 struct stab_nlist * stab_ptr;
792 char * strs;
793 int strtabinc;
794 char * subpath = NULL;
795 char symname[4096];
797 nstab = stablen / sizeof(struct stab_nlist);
798 stab_ptr = (struct stab_nlist *) (addr + staboff);
799 strs = (char *) (addr + strtaboff);
801 memset(currpath, 0, sizeof(currpath));
804 * Allocate a buffer into which we can build stab strings for cases
805 * where the stab is continued over multiple lines.
807 stabbufflen = 65536;
808 stabbuff = (char *) DBG_alloc(stabbufflen);
810 strtabinc = 0;
811 stabbuff[0] = '\0';
812 for(i=0; i < nstab; i++, stab_ptr++ )
814 ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
815 if( ptr[strlen(ptr) - 1] == '\\' )
818 * Indicates continuation. Append this to the buffer, and go onto the
819 * next record. Repeat the process until we find a stab without the
820 * '/' character, as this indicates we have the whole thing.
822 len = strlen(ptr);
823 if( strlen(stabbuff) + len > stabbufflen )
825 stabbufflen += 65536;
826 stabbuff = (char *) DBG_realloc(stabbuff, stabbufflen);
828 strncat(stabbuff, ptr, len - 1);
829 continue;
831 else if( stabbuff[0] != '\0' )
833 strcat( stabbuff, ptr);
834 ptr = stabbuff;
837 if( strchr(ptr, '=') != NULL )
840 * The stabs aren't in writable memory, so copy it over so we are
841 * sure we can scribble on it.
843 if( ptr != stabbuff )
845 strcpy(stabbuff, ptr);
846 ptr = stabbuff;
848 stab_strcpy(symname, ptr);
849 DEBUG_ParseTypedefStab(ptr, symname);
852 switch(stab_ptr->n_type)
854 case N_GSYM:
856 * These are useless with ELF. They have no value, and you have to
857 * read the normal symbol table to get the address. Thus we
858 * ignore them, and when we process the normal symbol table
859 * we should do the right thing.
861 * With a.out, they actually do make some amount of sense.
863 new_addr.seg = 0;
864 new_addr.type = DEBUG_ParseStabType(ptr);
865 new_addr.off = load_offset + stab_ptr->n_value;
867 stab_strcpy(symname, ptr);
868 #ifdef __ELF__
869 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
870 SYM_WINE | SYM_DATA | SYM_INVALID);
871 #else
872 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
873 SYM_WINE | SYM_DATA );
874 #endif
875 break;
876 case N_RBRAC:
877 case N_LBRAC:
879 * We need to keep track of these so we get symbol scoping
880 * right for local variables. For now, we just ignore them.
881 * The hooks are already there for dealing with this however,
882 * so all we need to do is to keep count of the nesting level,
883 * and find the RBRAC for each matching LBRAC.
885 break;
886 case N_LCSYM:
887 case N_STSYM:
889 * These are static symbols and BSS symbols.
891 new_addr.seg = 0;
892 new_addr.type = DEBUG_ParseStabType(ptr);
893 new_addr.off = load_offset + stab_ptr->n_value;
895 stab_strcpy(symname, ptr);
896 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
897 SYM_WINE | SYM_DATA );
898 break;
899 case N_PSYM:
901 * These are function parameters.
903 if( (curr_func != NULL)
904 && (stab_ptr->n_value != 0) )
906 stab_strcpy(symname, ptr);
907 curr_loc = DEBUG_AddLocal( curr_func, 0,
908 stab_ptr->n_value, 0, 0, symname );
909 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
911 break;
912 case N_RSYM:
913 if( curr_func != NULL )
915 stab_strcpy(symname, ptr);
916 curr_loc = DEBUG_AddLocal( curr_func, stab_ptr->n_value,
917 0, 0, 0, symname );
918 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
920 break;
921 case N_LSYM:
922 if( (curr_func != NULL)
923 && (stab_ptr->n_value != 0) )
925 stab_strcpy(symname, ptr);
926 curr_loc = DEBUG_AddLocal( curr_func, 0,
927 stab_ptr->n_value, 0, 0, symname );
928 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
930 else if (curr_func == NULL)
932 stab_strcpy(symname, ptr);
934 break;
935 case N_SLINE:
937 * This is a line number. These are always relative to the start
938 * of the function (N_FUN), and this makes the lookup easier.
940 if( curr_func != NULL )
942 #ifdef __ELF__
943 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
944 stab_ptr->n_value);
945 #else
946 #if 0
948 * This isn't right. The order of the stabs is different under
949 * a.out, and as a result we would end up attaching the line
950 * number to the wrong function.
952 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
953 stab_ptr->n_value - curr_func->addr.off);
954 #endif
955 #endif
957 break;
958 case N_FUN:
960 * First, clean up the previous function we were working on.
962 DEBUG_Normalize(curr_func);
965 * For now, just declare the various functions. Later
966 * on, we will add the line number information and the
967 * local symbols.
969 if( !ignore )
971 new_addr.seg = 0;
972 new_addr.type = DEBUG_ParseStabType(ptr);
973 new_addr.off = load_offset + stab_ptr->n_value;
975 * Copy the string to a temp buffer so we
976 * can kill everything after the ':'. We do
977 * it this way because otherwise we end up dirtying
978 * all of the pages related to the stabs, and that
979 * sucks up swap space like crazy.
981 stab_strcpy(symname, ptr);
982 curr_func = DEBUG_AddSymbol( symname, &new_addr, currpath,
983 SYM_WINE | SYM_FUNC);
985 else
988 * Don't add line number information for this function
989 * any more.
991 curr_func = NULL;
993 break;
994 case N_SO:
996 * This indicates a new source file. Append the records
997 * together, to build the correct path name.
999 #ifndef __ELF__
1001 * With a.out, there is no NULL string N_SO entry at the end of
1002 * the file. Thus when we find non-consecutive entries,
1003 * we consider that a new file is started.
1005 if( last_nso < i-1 )
1007 currpath[0] = '\0';
1008 DEBUG_Normalize(curr_func);
1009 curr_func = NULL;
1011 #endif
1013 if( *ptr == '\0' )
1016 * Nuke old path.
1018 currpath[0] = '\0';
1019 DEBUG_Normalize(curr_func);
1020 curr_func = NULL;
1022 else
1024 if (*ptr != '/')
1025 strcat(currpath, ptr);
1026 else
1027 strcpy(currpath, ptr);
1028 subpath = ptr;
1029 DEBUG_ResetIncludes();
1031 last_nso = i;
1032 break;
1033 case N_SOL:
1035 * This indicates we are including stuff from an include file.
1036 * If this is the main source, enable the debug stuff, otherwise
1037 * ignore it.
1039 if( subpath == NULL || strcmp(ptr, subpath) == 0 )
1041 ignore = FALSE;
1043 else
1045 ignore = TRUE;
1046 DEBUG_Normalize(curr_func);
1047 curr_func = NULL;
1049 break;
1050 case N_UNDF:
1051 strs += strtabinc;
1052 strtabinc = stab_ptr->n_value;
1053 DEBUG_Normalize(curr_func);
1054 curr_func = NULL;
1055 break;
1056 case N_OPT:
1058 * Ignore this. We don't care what it points to.
1060 break;
1061 case N_BINCL:
1062 DEBUG_AddInclude(DEBUG_CreateInclude(ptr, stab_ptr->n_value));
1063 break;
1064 case N_EINCL:
1065 break;
1066 case N_EXCL:
1067 DEBUG_AddInclude(DEBUG_FindInclude(ptr, stab_ptr->n_value));
1068 break;
1069 case N_MAIN:
1071 * Always ignore these. GCC doesn't even generate them.
1073 break;
1074 default:
1075 fprintf(stderr, "Unkown stab type 0x%02x\n", stab_ptr->n_type);
1076 break;
1079 stabbuff[0] = '\0';
1081 #if 0
1082 fprintf(stderr, "%d %x %s\n", stab_ptr->n_type,
1083 (unsigned int) stab_ptr->n_value,
1084 strs + (unsigned int) stab_ptr->n_un.n_name);
1085 #endif
1088 DEBUG_FreeRegisteredTypedefs();
1089 DEBUG_FreeIncludes();
1091 return TRUE;
1094 #ifdef __ELF__
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.
1102 * This is all really quite easy, since we don't have to worry about line
1103 * numbers or local data variables.
1105 static
1107 DEBUG_ProcessElfSymtab(char * addr, unsigned int load_offset,
1108 Elf32_Shdr * symtab, Elf32_Shdr * strtab)
1110 char * curfile = NULL;
1111 struct name_hash * curr_sym = NULL;
1112 int flags;
1113 int i;
1114 DBG_ADDR new_addr;
1115 int nsym;
1116 char * strp;
1117 char * symname;
1118 Elf32_Sym * symp;
1121 symp = (Elf32_Sym *) (addr + symtab->sh_offset);
1122 nsym = symtab->sh_size / sizeof(*symp);
1123 strp = (char *) (addr + strtab->sh_offset);
1125 for(i=0; i < nsym; i++, symp++)
1128 * Ignore certain types of entries which really aren't of that much
1129 * interest.
1131 if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION )
1133 continue;
1136 symname = strp + symp->st_name;
1139 * Save the name of the current file, so we have a way of tracking
1140 * static functions/data.
1142 if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
1144 curfile = symname;
1145 continue;
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.
1156 if( (DEBUG_GetSymbolValue(symname, -1, &new_addr, FALSE ) == TRUE)
1157 && (new_addr.off == (load_offset + symp->st_value)) )
1158 continue;
1160 new_addr.seg = 0;
1161 new_addr.type = NULL;
1162 new_addr.off = load_offset + symp->st_value;
1163 flags = SYM_WINE | (ELF32_ST_BIND(symp->st_info) == STT_FUNC
1164 ? SYM_FUNC : SYM_DATA);
1165 if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
1166 curr_sym = DEBUG_AddSymbol( symname, &new_addr, NULL, flags );
1167 else
1168 curr_sym = DEBUG_AddSymbol( symname, &new_addr, curfile, flags );
1171 * Record the size of the symbol. This can come in handy in
1172 * some cases. Not really used yet, however.
1174 if( symp->st_size != 0 )
1175 DEBUG_SetSymbolSize(curr_sym, symp->st_size);
1178 return TRUE;
1181 static
1183 DEBUG_ProcessElfObject(char * filename, unsigned int load_offset)
1185 int rtn = FALSE;
1186 struct stat statbuf;
1187 int fd = -1;
1188 int status;
1189 char * addr = (char *) 0xffffffff;
1190 Elf32_Ehdr * ehptr;
1191 Elf32_Shdr * spnt;
1192 char * shstrtab;
1193 int nsect;
1194 int i;
1195 int stabsect;
1196 int stabstrsect;
1200 * Make sure we can stat and open this file.
1202 if( filename == NULL )
1203 goto leave;
1205 status = stat(filename, &statbuf);
1206 if( status == -1 )
1208 char *s,*t,*fn,*paths;
1209 if (strchr(filename,'/'))
1210 goto leave;
1211 paths = DBG_strdup(getenv("PATH"));
1212 s = paths;
1213 while (s && *s) {
1214 t = strchr(s,':');
1215 if (t) *t='\0';
1216 fn = (char*)DBG_alloc(strlen(filename)+1+strlen(s)+1);
1217 strcpy(fn,s);
1218 strcat(fn,"/");
1219 strcat(fn,filename);
1220 if ((rtn = DEBUG_ProcessElfObject(fn,load_offset))) {
1221 DBG_free(fn);
1222 DBG_free(paths);
1223 goto leave;
1225 DBG_free(fn);
1226 if (t) s = t+1; else break;
1228 if (!s || !*s) fprintf(stderr," not found");
1229 DBG_free(paths);
1230 goto leave;
1234 * Now open the file, so that we can mmap() it.
1236 fd = open(filename, O_RDONLY);
1237 if( fd == -1 )
1238 goto leave;
1242 * Now mmap() the file.
1244 addr = mmap(0, statbuf.st_size, PROT_READ,
1245 MAP_PRIVATE, fd, 0);
1246 if( addr == (char *) 0xffffffff )
1247 goto leave;
1250 * Next, we need to find a few of the internal ELF headers within
1251 * this thing. We need the main executable header, and the section
1252 * table.
1254 ehptr = (Elf32_Ehdr *) addr;
1256 if( load_offset == 0 )
1257 DEBUG_RegisterELFDebugInfo(ehptr->e_entry, statbuf.st_size, filename);
1258 else
1259 DEBUG_RegisterELFDebugInfo(load_offset, statbuf.st_size, filename);
1261 spnt = (Elf32_Shdr *) (addr + ehptr->e_shoff);
1262 nsect = ehptr->e_shnum;
1263 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1265 stabsect = stabstrsect = -1;
1267 for(i=0; i < nsect; i++)
1269 if( strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0 )
1270 stabsect = i;
1272 if( strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0 )
1273 stabstrsect = i;
1276 if( stabsect == -1 || stabstrsect == -1 )
1277 goto leave;
1280 * OK, now just parse all of the stabs.
1282 rtn = DEBUG_ParseStabs(addr, load_offset,
1283 spnt[stabsect].sh_offset,
1284 spnt[stabsect].sh_size,
1285 spnt[stabstrsect].sh_offset,
1286 spnt[stabstrsect].sh_size);
1288 if( rtn != TRUE )
1289 goto leave;
1291 for(i=0; i < nsect; i++)
1293 if( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1294 && (spnt[i].sh_type == SHT_SYMTAB) )
1295 DEBUG_ProcessElfSymtab(addr, load_offset,
1296 spnt + i, spnt + spnt[i].sh_link);
1298 if( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1299 && (spnt[i].sh_type == SHT_DYNSYM) )
1300 DEBUG_ProcessElfSymtab(addr, load_offset,
1301 spnt + i, spnt + spnt[i].sh_link);
1304 leave:
1306 if( addr != (char *) 0xffffffff )
1307 munmap(addr, statbuf.st_size);
1309 if( fd != -1 )
1310 close(fd);
1312 return (rtn);
1317 DEBUG_ReadExecutableDbgInfo(void)
1319 Elf32_Ehdr * ehdr;
1320 char * exe_name;
1321 Elf32_Dyn * dynpnt;
1322 struct r_debug * dbg_hdr;
1323 struct link_map * lpnt = NULL;
1324 #ifdef __GNUC__
1325 extern Elf32_Dyn _DYNAMIC[] __attribute__ ((weak));
1326 #else
1327 extern Elf32_Dyn _DYNAMIC[];
1328 #endif
1329 int rtn = FALSE;
1330 int rowcount;
1332 exe_name = DEBUG_argv0;
1335 * Make sure we can stat and open this file.
1337 if( exe_name == NULL )
1338 goto leave;
1340 fprintf( stderr, "Loading symbols: %s", exe_name );
1341 rowcount = 17 + strlen(exe_name);
1342 DEBUG_ProcessElfObject(exe_name, 0);
1345 * Finally walk the tables that the dynamic loader maintains to find all
1346 * of the other shared libraries which might be loaded. Perform the
1347 * same step for all of these.
1349 if( (&_DYNAMIC == NULL) || (_DYNAMIC == NULL) )
1350 goto leave;
1352 dynpnt = _DYNAMIC;
1355 * Now walk the dynamic section (of the executable, looking for a DT_DEBUG
1356 * entry.
1358 for(; dynpnt->d_tag != DT_NULL; dynpnt++)
1359 if( dynpnt->d_tag == DT_DEBUG )
1360 break;
1362 if( (dynpnt->d_tag != DT_DEBUG)
1363 || (dynpnt->d_un.d_ptr == 0) )
1364 goto leave;
1367 * OK, now dig into the actual tables themselves.
1369 dbg_hdr = (struct r_debug *) dynpnt->d_un.d_ptr;
1370 lpnt = dbg_hdr->r_map;
1373 * Now walk the linked list. In all known ELF implementations,
1374 * the dynamic loader maintains this linked list for us. In some
1375 * cases the first entry doesn't appear with a name, in other cases it
1376 * does.
1378 for(; lpnt; lpnt = lpnt->l_next )
1381 * We already got the stuff for the executable using the
1382 * argv[0] entry above. Here we only need to concentrate on any
1383 * shared libraries which may be loaded.
1385 ehdr = (Elf32_Ehdr *) lpnt->l_addr;
1386 if( (lpnt->l_addr == 0) || (ehdr->e_type != ET_DYN) )
1387 continue;
1389 if( lpnt->l_name != NULL )
1391 if (rowcount + strlen(lpnt->l_name) > 76)
1393 fprintf( stderr, "\n " );
1394 rowcount = 3;
1396 fprintf( stderr, " %s", lpnt->l_name );
1397 rowcount += strlen(lpnt->l_name) + 1;
1398 DEBUG_ProcessElfObject(lpnt->l_name, lpnt->l_addr);
1402 rtn = TRUE;
1404 leave:
1405 fprintf( stderr, "\n" );
1406 return (rtn);
1410 #else /* !__ELF__ */
1412 #ifdef linux
1414 * a.out linux.
1417 DEBUG_ReadExecutableDbgInfo(void)
1419 char * addr = (char *) 0xffffffff;
1420 char * exe_name;
1421 struct exec * ahdr;
1422 int fd = -1;
1423 int rtn = FALSE;
1424 unsigned int staboff;
1425 struct stat statbuf;
1426 int status;
1427 unsigned int stroff;
1429 exe_name = DEBUG_argv0;
1432 * Make sure we can stat and open this file.
1434 if( exe_name == NULL )
1435 goto leave;
1437 status = stat(exe_name, &statbuf);
1438 if( status == -1 )
1439 goto leave;
1442 * Now open the file, so that we can mmap() it.
1444 fd = open(exe_name, O_RDONLY);
1445 if( fd == -1 )
1446 goto leave;
1450 * Now mmap() the file.
1452 addr = mmap(0, statbuf.st_size, PROT_READ,
1453 MAP_PRIVATE, fd, 0);
1454 if( addr == (char *) 0xffffffff )
1455 goto leave;
1457 ahdr = (struct exec *) addr;
1459 staboff = N_SYMOFF(*ahdr);
1460 stroff = N_STROFF(*ahdr);
1461 rtn = DEBUG_ParseStabs(addr, 0,
1462 staboff,
1463 ahdr->a_syms,
1464 stroff,
1465 statbuf.st_size - stroff);
1468 * Give a nice status message here...
1470 fprintf( stderr, "Loading symbols: %s", exe_name );
1472 rtn = TRUE;
1474 leave:
1476 if( addr != (char *) 0xffffffff )
1477 munmap(addr, statbuf.st_size);
1479 if( fd != -1 )
1480 close(fd);
1482 return (rtn);
1485 #else
1487 * Non-linux, non-ELF platforms.
1490 DEBUG_ReadExecutableDbgInfo(void)
1492 return FALSE;
1494 #endif
1496 #endif /* __ELF__ */