Revived the GEN_C_SRCS variable to support wrc lex/yacc sources.
[wine.git] / debugger / stabs.c
blob95765b2f93e18803f5a2d26c253a7b48154ebfef
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 * 1999, 2000 Eric Pouech
8 */
10 #include "config.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 <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #ifndef PATH_MAX
23 #define PATH_MAX _MAX_PATH
24 #endif
26 #include "debugger.h"
28 #if defined(__svr4__) || defined(__sun)
29 #define __ELF__
30 #endif
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
49 #ifndef N_UNDF
50 #define N_UNDF 0x00
51 #endif
53 #ifndef STN_UNDEF
54 # define STN_UNDEF 0
55 #endif
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
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;
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.
93 struct known_typedef
95 struct known_typedef * next;
96 char * name;
97 int ndefs;
98 struct datatype * types[1];
101 #define NR_STAB_HASH 521
103 struct known_typedef * ktd_head[NR_STAB_HASH] = {NULL,};
105 static unsigned int stab_hash( const char * name )
107 unsigned int hash = 0;
108 unsigned int tmp;
109 const char * p;
111 p = name;
113 while (*p)
115 hash = (hash << 4) + *p++;
117 if( (tmp = (hash & 0xf0000000)) )
119 hash ^= tmp >> 24;
121 hash &= ~tmp;
123 return hash % NR_STAB_HASH;
127 static void stab_strcpy(char * dest, int sz, const char * source)
130 * A strcpy routine that stops when we hit the ':' character.
131 * Faster than copying the whole thing, and then nuking the
132 * ':'.
134 while(*source != '\0' && *source != ':' && sz-- > 0)
135 *dest++ = *source++;
136 *dest = '\0';
137 assert(sz > 0);
140 typedef struct {
141 char* name;
142 unsigned long value;
143 int idx;
144 struct datatype** vector;
145 int nrofentries;
146 } include_def;
148 #define MAX_INCLUDES 256
150 static include_def* include_defs = NULL;
151 static int num_include_def = 0;
152 static int num_alloc_include_def = 0;
153 static int cu_include_stack[MAX_INCLUDES];
154 static int cu_include_stk_idx = 0;
155 static struct datatype** cu_vector = NULL;
156 static int cu_nrofentries = 0;
158 static
159 int
160 DEBUG_CreateInclude(const char* file, unsigned long val)
162 if (num_include_def == num_alloc_include_def)
164 num_alloc_include_def += 256;
165 include_defs = DBG_realloc(include_defs, sizeof(include_defs[0])*num_alloc_include_def);
166 memset(include_defs+num_include_def, 0, sizeof(include_defs[0])*256);
168 include_defs[num_include_def].name = DBG_strdup(file);
169 include_defs[num_include_def].value = val;
170 include_defs[num_include_def].vector = NULL;
171 include_defs[num_include_def].nrofentries = 0;
173 return num_include_def++;
176 static
177 int
178 DEBUG_FindInclude(const char* file, unsigned long val)
180 int i;
182 for (i = 0; i < num_include_def; i++)
184 if (val == include_defs[i].value &&
185 strcmp(file, include_defs[i].name) == 0)
186 return i;
188 return -1;
191 static
193 DEBUG_AddInclude(int idx)
195 ++cu_include_stk_idx;
197 /* is this happen, just bump MAX_INCLUDES */
198 /* we could also handle this as another dynarray */
199 assert(cu_include_stk_idx < MAX_INCLUDES);
201 cu_include_stack[cu_include_stk_idx] = idx;
202 return cu_include_stk_idx;
205 static
206 void
207 DEBUG_ResetIncludes(void)
210 * The datatypes that we would need to use are reset when
211 * we start a new file. (at least the ones in filenr == 0
213 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
214 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
217 static
218 void
219 DEBUG_FreeIncludes(void)
221 int i;
223 DEBUG_ResetIncludes();
225 for (i = 0; i < num_include_def; i++)
227 DBG_free(include_defs[i].name);
228 DBG_free(include_defs[i].vector);
230 DBG_free(include_defs);
231 include_defs = NULL;
232 num_include_def = 0;
233 num_alloc_include_def = 0;
234 DBG_free(cu_vector);
235 cu_vector = NULL;
236 cu_nrofentries = 0;
239 #define MAX_TD_NESTING 128
241 static
242 struct datatype**
243 DEBUG_FileSubNr2StabEnum(int filenr, int subnr)
245 struct datatype** ret;
247 /* DEBUG_Printf(DBG_CHN_MESG, "creating type id for (%d,%d)\n", filenr, subnr); */
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
252 if (filenr == 0)
254 if (cu_nrofentries <= subnr)
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;
260 ret = &cu_vector[subnr];
262 else
264 include_def* idef;
266 assert(filenr <= cu_include_stk_idx);
268 idef = &include_defs[cu_include_stack[filenr]];
270 if (idef->nrofentries <= subnr)
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;
276 ret = &idef->vector[subnr];
278 /* DEBUG_Printf(DBG_CHN_MESG,"(%d,%d) is %d\n",filenr,subnr,ret); */
279 return ret;
282 static
283 struct datatype**
284 DEBUG_ReadTypeEnumBackwards(char*x) {
285 int filenr,subnr;
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);
301 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
304 static
305 struct datatype**
306 DEBUG_ReadTypeEnum(char **x) {
307 int filenr,subnr;
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> */
319 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
322 static
324 DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef)
326 int hash;
327 struct known_typedef * ktd;
329 if( ndef == 1 )
330 return TRUE;
332 ktd = (struct known_typedef *) DBG_alloc(sizeof(struct known_typedef)
333 + (ndef - 1) * sizeof(struct datatype *));
335 hash = stab_hash(name);
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;
343 return TRUE;
346 static
348 DEBUG_HandlePreviousTypedef(const char * name, const char * stab)
350 int count;
351 enum debug_type expect;
352 int hash;
353 struct known_typedef * ktd;
354 char * ptr;
356 hash = stab_hash(name);
358 for(ktd = ktd_head[hash]; ktd; ktd = ktd->next)
359 if ((ktd->name[0] == name[0]) && (strcmp(name, ktd->name) == 0) )
360 break;
363 * Didn't find it. This must be a new one.
365 if( ktd == NULL )
366 return FALSE;
369 * Examine the stab to make sure it has the same number of definitions.
371 count = 0;
372 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
374 if( count >= ktd->ndefs )
375 return FALSE;
378 * Make sure the types of all of the objects is consistent with
379 * what we have already parsed.
381 switch(ptr[1])
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;
413 if( expect != -1 && expect != DEBUG_GetType(ktd->types[count]) )
414 return FALSE;
415 count++;
418 if( ktd->ndefs != count )
419 return FALSE;
422 * Go through, dig out all of the type numbers, and substitute the
423 * appropriate things.
425 count = 0;
426 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
427 *DEBUG_ReadTypeEnumBackwards(ptr-1) = ktd->types[count++];
429 return TRUE;
432 static int DEBUG_FreeRegisteredTypedefs(void)
434 int count;
435 int j;
436 struct known_typedef * ktd;
437 struct known_typedef * next;
439 count = 0;
440 for(j=0; j < NR_STAB_HASH; j++ )
442 for(ktd = ktd_head[j]; ktd; ktd = next)
444 count++;
445 next = ktd->next;
446 DBG_free(ktd->name);
447 DBG_free(ktd);
449 ktd_head[j] = NULL;
452 return TRUE;
456 static
458 DEBUG_ParseTypedefStab(char * ptr, const char * typename)
460 int arrmax;
461 int arrmin;
462 char * c;
463 struct datatype * curr_type;
464 struct datatype * datatype;
465 struct datatype * curr_types[MAX_TD_NESTING];
466 char element_name[1024];
467 int ntypes = 0, ntp;
468 int offset;
469 const char * orig_typename;
470 int size;
471 char * tc;
472 char * tc2;
473 int failure;
475 orig_typename = typename;
477 if( DEBUG_HandlePreviousTypedef(typename, ptr) )
478 return TRUE;
481 * Go from back to front. First we go through and figure out what
482 * type numbers we need, and register those types. Then we go in
483 * and fill the details.
486 for( c = strchr(ptr, '='); c != NULL; c = strchr(c + 1, '=') )
489 * Back up until we get to a non-numeric character, to get datatype
491 struct datatype** dt = DEBUG_ReadTypeEnumBackwards(c-1);
493 if( ntypes >= MAX_TD_NESTING )
496 * If this ever happens, just bump the counter.
498 DEBUG_Printf(DBG_CHN_MESG, "Typedef nesting overflow\n");
499 return FALSE;
502 switch(c[1])
504 case '*':
505 *dt = DEBUG_NewDataType(DT_POINTER, NULL);
506 curr_types[ntypes++] = *dt;
507 break;
508 case 's':
509 case 'u':
510 *dt = DEBUG_NewDataType(DT_STRUCT, typename);
511 curr_types[ntypes++] = *dt;
512 break;
513 case 'a':
514 *dt = DEBUG_NewDataType(DT_ARRAY, NULL);
515 curr_types[ntypes++] = *dt;
516 break;
517 case '(':
518 /* will be handled in next loop,
519 * just a ref to another type
521 curr_types[ntypes++] = NULL;
522 break;
523 case '1':
524 case 'r':
525 *dt = DEBUG_NewDataType(DT_BASIC, typename);
526 curr_types[ntypes++] = *dt;
527 break;
528 case 'x':
529 stab_strcpy(element_name, sizeof(element_name), c + 3);
530 *dt = DEBUG_NewDataType(DT_STRUCT, element_name);
531 curr_types[ntypes++] = *dt;
532 break;
533 case 'e':
534 *dt = DEBUG_NewDataType(DT_ENUM, NULL);
535 curr_types[ntypes++] = *dt;
536 break;
537 case 'f':
538 *dt = DEBUG_NewDataType(DT_FUNC, NULL);
539 curr_types[ntypes++] = *dt;
540 break;
541 default:
542 DEBUG_Printf(DBG_CHN_FIXME, "Unknown type (%c).\n",c[1]);
543 return FALSE;
545 typename = NULL;
549 ntp = ntypes - 1;
551 * OK, now take a second sweep through. Now we will be digging
552 * out the definitions of the various components, and storing
553 * them in the skeletons that we have already allocated. We take
554 * a right-to left search as this is much easier to parse.
556 for( c = strrchr(ptr, '='); c != NULL; c = strrchr(ptr, '=') )
558 struct datatype** dt = DEBUG_ReadTypeEnumBackwards(c-1);
559 struct datatype** dt2;
561 curr_type = *dt;
563 switch(c[1])
565 case 'x':
566 ntp--;
567 tc = c + 3;
568 while( *tc != ':' )
569 tc++;
570 tc++;
571 if( *tc == '\0' )
572 *c = '\0';
573 else
574 strcpy(c, tc);
575 break;
576 case '*':
577 case 'f':
578 ntp--;
579 tc = c + 2;
580 datatype = *DEBUG_ReadTypeEnum(&tc);
581 DEBUG_SetPointerType(curr_type, datatype);
582 if( *tc == '\0' )
583 *c = '\0';
584 else
585 strcpy(c, tc);
586 break;
587 case '(':
588 tc = c + 1;
589 dt2 = DEBUG_ReadTypeEnum(&tc);
591 if (!*dt && *dt2)
593 *dt = *dt2;
595 else if (!*dt && !*dt2)
597 /* this should be a basic type, define it */
598 *dt2 = *dt = DEBUG_NewDataType(DT_BASIC, typename);
600 else
602 DEBUG_Printf(DBG_CHN_MESG, "Unknown condition %08lx %08lx (%s)\n",
603 (unsigned long)*dt, (unsigned long)*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 DEBUG_Printf(DBG_CHN_MESG, "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 DEBUG_Printf(DBG_CHN_FIXME, "Unknown type (%c).\n",c[1]);
733 return FALSE;
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 in_external_file = FALSE;
784 int last_nso = -1;
785 int len;
786 DBG_VALUE new_value;
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, sizeof(symname), ptr);
849 if (!DEBUG_ParseTypedefStab(ptr, symname)) {
850 /* skip this definition */
851 stabbuff[0] = '\0';
852 continue;
856 switch(stab_ptr->n_type)
858 case N_GSYM:
860 * These are useless with ELF. They have no value, and you have to
861 * read the normal symbol table to get the address. Thus we
862 * ignore them, and when we process the normal symbol table
863 * we should do the right thing.
865 * With a.out or mingw, they actually do make some amount of sense.
867 new_value.addr.seg = 0;
868 new_value.type = DEBUG_ParseStabType(ptr);
869 new_value.addr.off = load_offset + stab_ptr->n_value;
870 new_value.cookie = DV_TARGET;
872 stab_strcpy(symname, sizeof(symname), ptr);
873 #ifdef __ELF__
874 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
875 SYM_WINE | SYM_DATA | SYM_INVALID );
876 #else
877 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
878 SYM_WINE | SYM_DATA );
879 #endif
880 break;
881 case N_RBRAC:
882 case N_LBRAC:
884 * We need to keep track of these so we get symbol scoping
885 * right for local variables. For now, we just ignore them.
886 * The hooks are already there for dealing with this however,
887 * so all we need to do is to keep count of the nesting level,
888 * and find the RBRAC for each matching LBRAC.
890 break;
891 case N_LCSYM:
892 case N_STSYM:
894 * These are static symbols and BSS symbols.
896 new_value.addr.seg = 0;
897 new_value.type = DEBUG_ParseStabType(ptr);
898 new_value.addr.off = load_offset + stab_ptr->n_value;
899 new_value.cookie = DV_TARGET;
901 stab_strcpy(symname, sizeof(symname), ptr);
902 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
903 SYM_WINE | SYM_DATA );
904 break;
905 case N_PSYM:
907 * These are function parameters.
909 if( curr_func != NULL && !in_external_file )
911 stab_strcpy(symname, sizeof(symname), ptr);
912 curr_loc = DEBUG_AddLocal( curr_func, 0,
913 stab_ptr->n_value, 0, 0, symname );
914 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
916 break;
917 case N_RSYM:
918 if( curr_func != NULL && !in_external_file )
920 stab_strcpy(symname, sizeof(symname), ptr);
921 curr_loc = DEBUG_AddLocal( curr_func, stab_ptr->n_value + 1,
922 0, 0, 0, symname );
923 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
925 break;
926 case N_LSYM:
927 if( curr_func != NULL && !in_external_file )
929 stab_strcpy(symname, sizeof(symname), ptr);
930 curr_loc = DEBUG_AddLocal( curr_func, 0,
931 stab_ptr->n_value, 0, 0, symname );
932 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(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 && !in_external_file )
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( !in_external_file)
971 stab_strcpy(symname, sizeof(symname), ptr);
972 if (*symname)
974 new_value.addr.seg = 0;
975 new_value.type = DEBUG_ParseStabType(ptr);
976 new_value.addr.off = load_offset + stab_ptr->n_value;
977 new_value.cookie = DV_TARGET;
979 * Copy the string to a temp buffer so we
980 * can kill everything after the ':'. We do
981 * it this way because otherwise we end up dirtying
982 * all of the pages related to the stabs, and that
983 * sucks up swap space like crazy.
985 curr_func = DEBUG_AddSymbol( symname, &new_value, currpath,
986 SYM_WINE | SYM_FUNC);
988 else
990 /* some GCC seem to use a N_FUN "" to mark the end of a function */
991 curr_func = NULL;
994 else
997 * Don't add line number information for this function
998 * any more.
1000 curr_func = NULL;
1002 break;
1003 case N_SO:
1005 * This indicates a new source file. Append the records
1006 * together, to build the correct path name.
1008 #ifndef __ELF__
1010 * With a.out, there is no NULL string N_SO entry at the end of
1011 * the file. Thus when we find non-consecutive entries,
1012 * we consider that a new file is started.
1014 if( last_nso < i-1 )
1016 currpath[0] = '\0';
1017 DEBUG_Normalize(curr_func);
1018 curr_func = NULL;
1020 #endif
1022 if( *ptr == '\0' )
1025 * Nuke old path.
1027 currpath[0] = '\0';
1028 DEBUG_Normalize(curr_func);
1029 curr_func = NULL;
1031 else
1033 if (*ptr != '/')
1034 strcat(currpath, ptr);
1035 else
1036 strcpy(currpath, ptr);
1037 subpath = ptr;
1038 DEBUG_ResetIncludes();
1040 last_nso = i;
1041 break;
1042 case N_SOL:
1044 * This indicates we are including stuff from an include file.
1045 * If this is the main source, enable the debug stuff, otherwise
1046 * ignore it.
1048 in_external_file = !(subpath == NULL || strcmp(ptr, subpath) == 0);
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 DEBUG_Printf(DBG_CHN_MESG, "Unkown stab type 0x%02x\n", stab_ptr->n_type);
1076 break;
1079 stabbuff[0] = '\0';
1081 #if 0
1082 DEBUG_Printf(DBG_CHN_MESG, "%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_VALUE new_value;
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 ||
1132 symp->st_shndx == STN_UNDEF )
1134 continue;
1137 symname = strp + symp->st_name;
1140 * Save the name of the current file, so we have a way of tracking
1141 * static functions/data.
1143 if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
1145 curfile = symname;
1146 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_value, FALSE ) == TRUE)
1157 && (new_value.addr.off == (load_offset + symp->st_value)) )
1158 continue;
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 );
1172 * Record the size of the symbol. This can come in handy in
1173 * some cases. Not really used yet, however.
1175 if( symp->st_size != 0 )
1176 DEBUG_SetSymbolSize(curr_sym, symp->st_size);
1179 return TRUE;
1183 DEBUG_ProcessElfObject(const char * filename, unsigned int load_offset)
1185 int rtn = FALSE;
1186 char * addr = (char*)0xffffffff;
1187 int fd = -1;
1188 struct stat statbuf;
1189 Elf32_Ehdr * ehptr;
1190 Elf32_Shdr * spnt;
1191 char * shstrtab;
1192 int nsect;
1193 int i;
1194 int stabsect;
1195 int stabstrsect;
1198 * Make sure we can stat and open this file.
1200 if( filename == NULL )
1201 goto leave;
1203 if (stat(filename, &statbuf) == -1)
1205 char *s,*t,*fn,*paths;
1206 if (strchr(filename,'/'))
1207 goto leave;
1208 paths = DBG_strdup(getenv("PATH"));
1209 s = paths;
1210 while (s && *s) {
1211 t = strchr(s,':');
1212 if (t) *t='\0';
1213 fn = (char*)DBG_alloc(strlen(filename)+1+strlen(s)+1);
1214 strcpy(fn,s);
1215 strcat(fn,"/");
1216 strcat(fn,filename);
1217 if ((rtn = DEBUG_ProcessElfObject(fn,load_offset))) {
1218 DBG_free(fn);
1219 DBG_free(paths);
1220 goto leave;
1222 DBG_free(fn);
1223 if (t) s = t+1; else break;
1225 if (!s || !*s) DEBUG_Printf(DBG_CHN_MESG," not found");
1226 DBG_free(paths);
1227 goto leave;
1230 if (DEBUG_FindModuleByName(filename, DM_TYPE_ELF))
1231 goto leave;
1233 DEBUG_Printf(DBG_CHN_MESG, "Loading stabs debug symbols from %s (0x%08x)\n",
1234 filename, load_offset);
1237 * Now open the file, so that we can mmap() it.
1239 if ((fd = open(filename, O_RDONLY)) == -1)
1240 goto leave;
1243 * Now mmap() the file.
1245 addr = mmap(0, statbuf.st_size, PROT_READ, 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_RegisterELFModule(ehptr->e_entry, filename);
1258 else
1259 DEBUG_RegisterELFModule(load_offset, 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 DEBUG_Printf(DBG_CHN_WARN, "no .stab section\n");
1278 goto leave;
1282 * OK, now just parse all of the stabs.
1284 if (!(rtn = DEBUG_ParseStabs(addr, load_offset,
1285 spnt[stabsect].sh_offset,
1286 spnt[stabsect].sh_size,
1287 spnt[stabstrsect].sh_offset,
1288 spnt[stabstrsect].sh_size))) {
1289 DEBUG_Printf(DBG_CHN_WARN, "bad stabs\n");
1290 goto leave;
1293 for(i=0; i < nsect; i++)
1295 if( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1296 && (spnt[i].sh_type == SHT_SYMTAB) )
1297 DEBUG_ProcessElfSymtab(addr, load_offset,
1298 spnt + i, spnt + spnt[i].sh_link);
1300 if( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1301 && (spnt[i].sh_type == SHT_DYNSYM) )
1302 DEBUG_ProcessElfSymtab(addr, load_offset,
1303 spnt + i, spnt + spnt[i].sh_link);
1306 leave:
1308 if (addr != (char*)0xffffffff)
1309 munmap(addr, statbuf.st_size);
1311 if (fd != -1) close(fd);
1313 return rtn;
1316 static BOOL DEBUG_WalkList(struct r_debug* dbg_hdr)
1318 u_long lm_addr;
1319 struct link_map lm;
1320 Elf32_Ehdr ehdr;
1321 char bufstr[256];
1324 * Now walk the linked list. In all known ELF implementations,
1325 * the dynamic loader maintains this linked list for us. In some
1326 * cases the first entry doesn't appear with a name, in other cases it
1327 * does.
1329 for (lm_addr = (u_long)dbg_hdr->r_map; lm_addr; lm_addr = (u_long)lm.l_next) {
1330 if (!DEBUG_READ_MEM_VERBOSE((void*)lm_addr, &lm, sizeof(lm)))
1331 return FALSE;
1332 if (lm.l_addr != 0 &&
1333 DEBUG_READ_MEM_VERBOSE((void*)lm.l_addr, &ehdr, sizeof(ehdr)) &&
1334 ehdr.e_type == ET_DYN && /* only look at dynamic modules */
1335 lm.l_name != NULL &&
1336 DEBUG_READ_MEM_VERBOSE(lm.l_name, bufstr, sizeof(bufstr))) {
1337 bufstr[sizeof(bufstr) - 1] = '\0';
1338 DEBUG_ProcessElfObject(bufstr, lm.l_addr);
1342 return TRUE;
1345 static BOOL DEBUG_RescanElf(void)
1347 struct r_debug dbg_hdr;
1349 if (!DEBUG_CurrProcess ||
1350 !DEBUG_READ_MEM_VERBOSE((void*)DEBUG_CurrProcess->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)))
1351 return FALSE;
1353 switch (dbg_hdr.r_state) {
1354 case RT_CONSISTENT:
1355 DEBUG_WalkList(&dbg_hdr);
1356 break;
1357 case RT_ADD:
1358 break;
1359 case RT_DELETE:
1360 /*FIXME: this is not currently handled, would need some kind of mark&sweep algo */
1361 break;
1363 return FALSE;
1367 DEBUG_ReadExecutableDbgInfo(const char* exe_name)
1369 Elf32_Dyn dyn;
1370 struct r_debug dbg_hdr;
1371 int rtn = FALSE;
1372 DBG_VALUE val;
1375 * Make sure we can stat and open this file.
1377 if (exe_name == NULL) goto leave;
1378 DEBUG_ProcessElfObject(exe_name, 0);
1380 /* previous step should have loaded symbol _DYNAMIC if it exists inside
1381 * the main executable
1383 if (!DEBUG_GetSymbolValue("_DYNAMIC", -1, &val, FALSE)) {
1384 DEBUG_Printf(DBG_CHN_WARN, "Can't find symbol _DYNAMIC\n");
1385 goto leave;
1388 do {
1389 if (!DEBUG_READ_MEM_VERBOSE((void*)val.addr.off, &dyn, sizeof(dyn)))
1390 goto leave;
1391 val.addr.off += sizeof(dyn);
1392 } while (dyn.d_tag != DT_DEBUG && dyn.d_tag != DT_NULL);
1393 if (dyn.d_tag == DT_NULL) goto leave;
1396 * OK, now dig into the actual tables themselves.
1398 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn.d_un.d_ptr, &dbg_hdr, sizeof(dbg_hdr)))
1399 goto leave;
1401 assert(!DEBUG_CurrProcess->dbg_hdr_addr);
1402 DEBUG_CurrProcess->dbg_hdr_addr = (u_long)dyn.d_un.d_ptr;
1404 if (dbg_hdr.r_brk) {
1405 DBG_VALUE value;
1407 DEBUG_Printf(DBG_CHN_TRACE, "Setting up a breakpoint on r_brk(%lx)\n",
1408 (unsigned long)dbg_hdr.r_brk);
1410 DEBUG_SetBreakpoints(FALSE);
1411 value.type = NULL;
1412 value.cookie = DV_TARGET;
1413 value.addr.seg = 0;
1414 value.addr.off = dbg_hdr.r_brk;
1415 DEBUG_AddBreakpoint(&value, DEBUG_RescanElf);
1416 DEBUG_SetBreakpoints(TRUE);
1419 rtn = DEBUG_WalkList(&dbg_hdr);
1421 leave:
1422 return rtn;
1425 #else /* !__ELF__ */
1428 DEBUG_ProcessElfObject(const char * filename, unsigned int load_offset)
1430 return FALSE;
1434 DEBUG_ReadExecutableDbgInfo(const char* exe_name)
1436 return FALSE;
1439 #endif /* __ELF__ */