Made HEAP_strdup* functions inline (temporary).
[wine/multimedia.git] / debugger / stabs.c
blob91329243ecb0dfffa2f8064b385680e947f5d31a
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
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
76 typedef struct tagELF_DBG_INFO {
77 unsigned long elf_addr;
78 } ELF_DBG_INFO;
80 struct stab_nlist {
81 union {
82 char *n_name;
83 struct stab_nlist *n_next;
84 long n_strx;
85 } n_un;
86 unsigned char n_type;
87 char n_other;
88 short n_desc;
89 unsigned long n_value;
93 * This is used to keep track of known datatypes so that we don't redefine
94 * them over and over again. It sucks up lots of memory otherwise.
96 struct known_typedef
98 struct known_typedef * next;
99 char * name;
100 int ndefs;
101 struct datatype * types[1];
104 #define NR_STAB_HASH 521
106 static struct known_typedef * ktd_head[NR_STAB_HASH] = {NULL,};
107 static struct datatype ** curr_types = NULL;
108 static int allocated_types = 0;
110 static unsigned int stab_hash( const char * name )
112 unsigned int hash = 0;
113 unsigned int tmp;
114 const char * p;
116 p = name;
118 while (*p)
120 hash = (hash << 4) + *p++;
122 if( (tmp = (hash & 0xf0000000)) )
124 hash ^= tmp >> 24;
126 hash &= ~tmp;
128 return hash % NR_STAB_HASH;
132 static void stab_strcpy(char * dest, int sz, const char * source)
135 * A strcpy routine that stops when we hit the ':' character.
136 * Faster than copying the whole thing, and then nuking the
137 * ':'.
139 while(*source != '\0' && *source != ':' && sz-- > 0)
140 *dest++ = *source++;
141 *dest = '\0';
142 assert(sz > 0);
145 typedef struct {
146 char* name;
147 unsigned long value;
148 int idx;
149 struct datatype** vector;
150 int nrofentries;
151 } include_def;
153 #define MAX_INCLUDES 256
155 static include_def* include_defs = NULL;
156 static int num_include_def = 0;
157 static int num_alloc_include_def = 0;
158 static int cu_include_stack[MAX_INCLUDES];
159 static int cu_include_stk_idx = 0;
160 static struct datatype** cu_vector = NULL;
161 static int cu_nrofentries = 0;
163 static
164 int
165 DEBUG_CreateInclude(const char* file, unsigned long val)
167 if (num_include_def == num_alloc_include_def)
169 num_alloc_include_def += 256;
170 include_defs = DBG_realloc(include_defs, sizeof(include_defs[0])*num_alloc_include_def);
171 memset(include_defs+num_include_def, 0, sizeof(include_defs[0])*256);
173 include_defs[num_include_def].name = DBG_strdup(file);
174 include_defs[num_include_def].value = val;
175 include_defs[num_include_def].vector = NULL;
176 include_defs[num_include_def].nrofentries = 0;
178 return num_include_def++;
181 static
182 int
183 DEBUG_FindInclude(const char* file, unsigned long val)
185 int i;
187 for (i = 0; i < num_include_def; i++)
189 if (val == include_defs[i].value &&
190 strcmp(file, include_defs[i].name) == 0)
191 return i;
193 return -1;
196 static
198 DEBUG_AddInclude(int idx)
200 ++cu_include_stk_idx;
202 /* is this happen, just bump MAX_INCLUDES */
203 /* we could also handle this as another dynarray */
204 assert(cu_include_stk_idx < MAX_INCLUDES);
206 cu_include_stack[cu_include_stk_idx] = idx;
207 return cu_include_stk_idx;
210 static
211 void
212 DEBUG_ResetIncludes(void)
215 * The datatypes that we would need to use are reset when
216 * we start a new file. (at least the ones in filenr == 0
218 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
219 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
222 static
223 void
224 DEBUG_FreeIncludes(void)
226 int i;
228 DEBUG_ResetIncludes();
230 for (i = 0; i < num_include_def; i++)
232 DBG_free(include_defs[i].name);
233 DBG_free(include_defs[i].vector);
235 DBG_free(include_defs);
236 include_defs = NULL;
237 num_include_def = 0;
238 num_alloc_include_def = 0;
239 DBG_free(cu_vector);
240 cu_vector = NULL;
241 cu_nrofentries = 0;
244 static
245 struct datatype**
246 DEBUG_FileSubNr2StabEnum(int filenr, int subnr)
248 struct datatype** ret;
250 /* DEBUG_Printf(DBG_CHN_MESG, "creating type id for (%d,%d)\n", filenr, subnr); */
252 /* FIXME: I could perhaps create a dummy include_def for each compilation
253 * unit which would allow not to handle those two cases separately
255 if (filenr == 0)
257 if (cu_nrofentries <= subnr)
259 cu_vector = DBG_realloc(cu_vector, sizeof(cu_vector[0])*(subnr+1));
260 memset(cu_vector+cu_nrofentries, 0, sizeof(cu_vector[0])*(subnr+1-cu_nrofentries));
261 cu_nrofentries = subnr + 1;
263 ret = &cu_vector[subnr];
265 else
267 include_def* idef;
269 assert(filenr <= cu_include_stk_idx);
271 idef = &include_defs[cu_include_stack[filenr]];
273 if (idef->nrofentries <= subnr)
275 idef->vector = DBG_realloc(idef->vector, sizeof(idef->vector[0])*(subnr+1));
276 memset(idef->vector + idef->nrofentries, 0, sizeof(idef->vector[0])*(subnr+1-idef->nrofentries));
277 idef->nrofentries = subnr + 1;
279 ret = &idef->vector[subnr];
281 /* DEBUG_Printf(DBG_CHN_MESG,"(%d,%d) is %d\n",filenr,subnr,ret); */
282 return ret;
285 static
286 struct datatype**
287 DEBUG_ReadTypeEnumBackwards(char*x) {
288 int filenr,subnr;
290 if (*x==')') {
291 while (*x!='(')
292 x--;
293 x++; /* '(' */
294 filenr=strtol(x,&x,10); /* <int> */
295 x++; /* ',' */
296 subnr=strtol(x,&x,10); /* <int> */
297 x++; /* ')' */
298 } else {
299 while ((*x>='0') && (*x<='9'))
300 x--;
301 filenr = 0;
302 subnr = atol(x+1);
304 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
307 static
308 struct datatype**
309 DEBUG_ReadTypeEnum(char **x) {
310 int filenr,subnr;
312 if (**x=='(') {
313 (*x)++; /* '(' */
314 filenr=strtol(*x,x,10); /* <int> */
315 (*x)++; /* ',' */
316 subnr=strtol(*x,x,10); /* <int> */
317 (*x)++; /* ')' */
318 } else {
319 filenr = 0;
320 subnr = strtol(*x,x,10); /* <int> */
322 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
325 static
327 DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef)
329 int hash;
330 struct known_typedef * ktd;
332 if( ndef == 1 )
333 return TRUE;
335 ktd = (struct known_typedef *) DBG_alloc(sizeof(struct known_typedef)
336 + (ndef - 1) * sizeof(struct datatype *));
338 hash = stab_hash(name);
340 ktd->name = DBG_strdup(name);
341 ktd->ndefs = ndef;
342 memcpy(&ktd->types[0], types, ndef * sizeof(struct datatype *));
343 ktd->next = ktd_head[hash];
344 ktd_head[hash] = ktd;
346 return TRUE;
349 static
351 DEBUG_HandlePreviousTypedef(const char * name, const char * stab)
353 int count;
354 enum debug_type expect;
355 int hash;
356 struct known_typedef * ktd;
357 char * ptr;
359 hash = stab_hash(name);
361 for(ktd = ktd_head[hash]; ktd; ktd = ktd->next)
362 if ((ktd->name[0] == name[0]) && (strcmp(name, ktd->name) == 0) )
363 break;
366 * Didn't find it. This must be a new one.
368 if( ktd == NULL )
369 return FALSE;
372 * Examine the stab to make sure it has the same number of definitions.
374 count = 0;
375 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
377 if( count >= ktd->ndefs )
378 return FALSE;
381 * Make sure the types of all of the objects is consistent with
382 * what we have already parsed.
384 switch(ptr[1])
386 case '*':
387 expect = DT_POINTER;
388 break;
389 case 's':
390 case 'u':
391 expect = DT_STRUCT;
392 break;
393 case 'a':
394 expect = DT_ARRAY;
395 break;
396 case '(': /* it's mainly a ref to another typedef, skip it */
397 expect = -1;
398 break;
399 case '1':
400 case 'r':
401 expect = DT_BASIC;
402 break;
403 case 'x':
404 expect = DT_STRUCT;
405 break;
406 case 'e':
407 expect = DT_ENUM;
408 break;
409 case 'f':
410 expect = DT_FUNC;
411 break;
412 default:
413 DEBUG_Printf(DBG_CHN_FIXME, "Unknown type (%c).\n",ptr[1]);
414 return FALSE;
416 if( expect != -1 && expect != DEBUG_GetType(ktd->types[count]) )
417 return FALSE;
418 count++;
421 if( ktd->ndefs != count )
422 return FALSE;
425 * Go through, dig out all of the type numbers, and substitute the
426 * appropriate things.
428 count = 0;
429 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
430 *DEBUG_ReadTypeEnumBackwards(ptr-1) = ktd->types[count++];
432 return TRUE;
435 static int DEBUG_FreeRegisteredTypedefs(void)
437 int count;
438 int j;
439 struct known_typedef * ktd;
440 struct known_typedef * next;
442 count = 0;
443 for(j=0; j < NR_STAB_HASH; j++ )
445 for(ktd = ktd_head[j]; ktd; ktd = next)
447 count++;
448 next = ktd->next;
449 DBG_free(ktd->name);
450 DBG_free(ktd);
452 ktd_head[j] = NULL;
455 return TRUE;
459 static
461 DEBUG_ParseTypedefStab(char * ptr, const char * typename)
463 int arrmax;
464 int arrmin;
465 char * c;
466 struct datatype * curr_type;
467 struct datatype * datatype;
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 >= allocated_types )
497 allocated_types += 64;
498 curr_types = DBG_realloc(curr_types, sizeof(struct datatype*) * allocated_types);
499 if (!curr_types) 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);
773 enum DbgInfoLoad DEBUG_ParseStabs(char * addr, unsigned int load_offset,
774 unsigned int staboff, int stablen,
775 unsigned int strtaboff, int strtablen)
777 struct name_hash * curr_func = NULL;
778 struct wine_locals * curr_loc = NULL;
779 struct name_hash * curr_sym = NULL;
780 char currpath[PATH_MAX];
781 int i;
782 int in_external_file = FALSE;
783 int last_nso = -1;
784 int len;
785 DBG_VALUE new_value;
786 int nstab;
787 char * ptr;
788 char * stabbuff;
789 int stabbufflen;
790 struct stab_nlist * stab_ptr;
791 char * strs;
792 int strtabinc;
793 char * subpath = NULL;
794 char symname[4096];
796 nstab = stablen / sizeof(struct stab_nlist);
797 stab_ptr = (struct stab_nlist *) (addr + staboff);
798 strs = (char *) (addr + strtaboff);
800 memset(currpath, 0, sizeof(currpath));
803 * Allocate a buffer into which we can build stab strings for cases
804 * where the stab is continued over multiple lines.
806 stabbufflen = 65536;
807 stabbuff = (char *) DBG_alloc(stabbufflen);
809 strtabinc = 0;
810 stabbuff[0] = '\0';
811 for(i=0; i < nstab; i++, stab_ptr++ )
813 ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
814 if( ptr[strlen(ptr) - 1] == '\\' )
817 * Indicates continuation. Append this to the buffer, and go onto the
818 * next record. Repeat the process until we find a stab without the
819 * '/' character, as this indicates we have the whole thing.
821 len = strlen(ptr);
822 if( strlen(stabbuff) + len > stabbufflen )
824 stabbufflen += 65536;
825 stabbuff = (char *) DBG_realloc(stabbuff, stabbufflen);
827 strncat(stabbuff, ptr, len - 1);
828 continue;
830 else if( stabbuff[0] != '\0' )
832 strcat( stabbuff, ptr);
833 ptr = stabbuff;
836 if( strchr(ptr, '=') != NULL )
839 * The stabs aren't in writable memory, so copy it over so we are
840 * sure we can scribble on it.
842 if( ptr != stabbuff )
844 strcpy(stabbuff, ptr);
845 ptr = stabbuff;
847 stab_strcpy(symname, sizeof(symname), ptr);
848 if (!DEBUG_ParseTypedefStab(ptr, symname)) {
849 /* skip this definition */
850 stabbuff[0] = '\0';
851 continue;
855 switch(stab_ptr->n_type)
857 case N_GSYM:
859 * These are useless with ELF. They have no value, and you have to
860 * read the normal symbol table to get the address. Thus we
861 * ignore them, and when we process the normal symbol table
862 * we should do the right thing.
864 * With a.out or mingw, they actually do make some amount of sense.
866 new_value.addr.seg = 0;
867 new_value.type = DEBUG_ParseStabType(ptr);
868 new_value.addr.off = load_offset + stab_ptr->n_value;
869 new_value.cookie = DV_TARGET;
871 stab_strcpy(symname, sizeof(symname), ptr);
872 #ifdef __ELF__
873 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
874 SYM_WINE | SYM_DATA | SYM_INVALID );
875 #else
876 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
877 SYM_WINE | SYM_DATA );
878 #endif
879 break;
880 case N_RBRAC:
881 case N_LBRAC:
883 * We need to keep track of these so we get symbol scoping
884 * right for local variables. For now, we just ignore them.
885 * The hooks are already there for dealing with this however,
886 * so all we need to do is to keep count of the nesting level,
887 * and find the RBRAC for each matching LBRAC.
889 break;
890 case N_LCSYM:
891 case N_STSYM:
893 * These are static symbols and BSS symbols.
895 new_value.addr.seg = 0;
896 new_value.type = DEBUG_ParseStabType(ptr);
897 new_value.addr.off = load_offset + stab_ptr->n_value;
898 new_value.cookie = DV_TARGET;
900 stab_strcpy(symname, sizeof(symname), ptr);
901 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
902 SYM_WINE | SYM_DATA );
903 break;
904 case N_PSYM:
906 * These are function parameters.
908 if( curr_func != NULL && !in_external_file )
910 stab_strcpy(symname, sizeof(symname), ptr);
911 curr_loc = DEBUG_AddLocal( curr_func, 0,
912 stab_ptr->n_value, 0, 0, symname );
913 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
915 break;
916 case N_RSYM:
917 if( curr_func != NULL && !in_external_file )
919 stab_strcpy(symname, sizeof(symname), ptr);
920 curr_loc = DEBUG_AddLocal( curr_func, stab_ptr->n_value + 1,
921 0, 0, 0, symname );
922 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
924 break;
925 case N_LSYM:
926 if( curr_func != NULL && !in_external_file )
928 stab_strcpy(symname, sizeof(symname), ptr);
929 curr_loc = DEBUG_AddLocal( curr_func, 0,
930 stab_ptr->n_value, 0, 0, symname );
931 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
933 break;
934 case N_SLINE:
936 * This is a line number. These are always relative to the start
937 * of the function (N_FUN), and this makes the lookup easier.
939 if( curr_func != NULL && !in_external_file )
941 #ifdef __ELF__
942 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
943 stab_ptr->n_value);
944 #else
945 #if 0
947 * This isn't right. The order of the stabs is different under
948 * a.out, and as a result we would end up attaching the line
949 * number to the wrong function.
951 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
952 stab_ptr->n_value - curr_func->addr.off);
953 #endif
954 #endif
956 break;
957 case N_FUN:
959 * First, clean up the previous function we were working on.
961 DEBUG_Normalize(curr_func);
964 * For now, just declare the various functions. Later
965 * on, we will add the line number information and the
966 * local symbols.
968 if( !in_external_file)
970 stab_strcpy(symname, sizeof(symname), ptr);
971 if (*symname)
973 new_value.addr.seg = 0;
974 new_value.type = DEBUG_ParseStabType(ptr);
975 new_value.addr.off = load_offset + stab_ptr->n_value;
976 new_value.cookie = DV_TARGET;
978 * Copy the string to a temp buffer so we
979 * can kill everything after the ':'. We do
980 * it this way because otherwise we end up dirtying
981 * all of the pages related to the stabs, and that
982 * sucks up swap space like crazy.
984 #ifdef __ELF__
985 curr_func = DEBUG_AddSymbol( symname, &new_value, currpath,
986 SYM_WINE | SYM_FUNC | SYM_INVALID );
987 #else
988 curr_func = DEBUG_AddSymbol( symname, &new_value, currpath,
989 SYM_WINE | SYM_FUNC );
990 #endif
992 else
994 /* some GCC seem to use a N_FUN "" to mark the end of a function */
995 curr_func = NULL;
998 else
1001 * Don't add line number information for this function
1002 * any more.
1004 curr_func = NULL;
1006 break;
1007 case N_SO:
1009 * This indicates a new source file. Append the records
1010 * together, to build the correct path name.
1012 #ifndef __ELF__
1014 * With a.out, there is no NULL string N_SO entry at the end of
1015 * the file. Thus when we find non-consecutive entries,
1016 * we consider that a new file is started.
1018 if( last_nso < i-1 )
1020 currpath[0] = '\0';
1021 DEBUG_Normalize(curr_func);
1022 curr_func = NULL;
1024 #endif
1026 if( *ptr == '\0' )
1029 * Nuke old path.
1031 currpath[0] = '\0';
1032 DEBUG_Normalize(curr_func);
1033 curr_func = NULL;
1035 else
1037 if (*ptr != '/')
1038 strcat(currpath, ptr);
1039 else
1040 strcpy(currpath, ptr);
1041 subpath = ptr;
1042 DEBUG_ResetIncludes();
1044 last_nso = i;
1045 break;
1046 case N_SOL:
1048 * This indicates we are including stuff from an include file.
1049 * If this is the main source, enable the debug stuff, otherwise
1050 * ignore it.
1052 in_external_file = !(subpath == NULL || strcmp(ptr, subpath) == 0);
1053 break;
1054 case N_UNDF:
1055 strs += strtabinc;
1056 strtabinc = stab_ptr->n_value;
1057 DEBUG_Normalize(curr_func);
1058 curr_func = NULL;
1059 break;
1060 case N_OPT:
1062 * Ignore this. We don't care what it points to.
1064 break;
1065 case N_BINCL:
1066 DEBUG_AddInclude(DEBUG_CreateInclude(ptr, stab_ptr->n_value));
1067 break;
1068 case N_EINCL:
1069 break;
1070 case N_EXCL:
1071 DEBUG_AddInclude(DEBUG_FindInclude(ptr, stab_ptr->n_value));
1072 break;
1073 case N_MAIN:
1075 * Always ignore these. GCC doesn't even generate them.
1077 break;
1078 default:
1079 DEBUG_Printf(DBG_CHN_MESG, "Unknown stab type 0x%02x\n", stab_ptr->n_type);
1080 break;
1083 stabbuff[0] = '\0';
1085 #if 0
1086 DEBUG_Printf(DBG_CHN_MESG, "%d %x %s\n", stab_ptr->n_type,
1087 (unsigned int) stab_ptr->n_value,
1088 strs + (unsigned int) stab_ptr->n_un.n_name);
1089 #endif
1092 DEBUG_FreeRegisteredTypedefs();
1093 DEBUG_FreeIncludes();
1094 DBG_free(curr_types);
1095 curr_types = NULL;
1096 allocated_types = 0;
1098 return DIL_LOADED;
1101 #ifdef __ELF__
1104 * Walk through the entire symbol table and add any symbols we find there.
1105 * This can be used in cases where we have stripped ELF shared libraries,
1106 * or it can be used in cases where we have data symbols for which the address
1107 * isn't encoded in the stabs.
1109 * This is all really quite easy, since we don't have to worry about line
1110 * numbers or local data variables.
1112 static int DEBUG_ProcessElfSymtab(DBG_MODULE* module, char* addr,
1113 u_long load_addr, Elf32_Shdr* symtab,
1114 Elf32_Shdr* strtab)
1116 char * curfile = NULL;
1117 struct name_hash * curr_sym = NULL;
1118 int flags;
1119 int i;
1120 DBG_VALUE new_value;
1121 int nsym;
1122 char * strp;
1123 char * symname;
1124 Elf32_Sym * symp;
1126 symp = (Elf32_Sym *) (addr + symtab->sh_offset);
1127 nsym = symtab->sh_size / sizeof(*symp);
1128 strp = (char *) (addr + strtab->sh_offset);
1130 for(i=0; i < nsym; i++, symp++)
1133 * Ignore certain types of entries which really aren't of that much
1134 * interest.
1136 if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION ||
1137 symp->st_shndx == STN_UNDEF )
1139 continue;
1142 symname = strp + symp->st_name;
1145 * Save the name of the current file, so we have a way of tracking
1146 * static functions/data.
1148 if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
1150 curfile = symname;
1151 continue;
1155 * See if we already have something for this symbol.
1156 * If so, ignore this entry, because it would have come from the
1157 * stabs or from a previous symbol. If the value is different,
1158 * we will have to keep the darned thing, because there can be
1159 * multiple local symbols by the same name.
1161 if( (DEBUG_GetSymbolValue(symname, -1, &new_value, FALSE ) == TRUE)
1162 && (new_value.addr.off == (load_addr + symp->st_value)) )
1163 continue;
1165 new_value.addr.seg = 0;
1166 new_value.type = NULL;
1167 new_value.addr.off = load_addr + symp->st_value;
1168 new_value.cookie = DV_TARGET;
1169 flags = SYM_WINE | ((ELF32_ST_TYPE(symp->st_info) == STT_FUNC)
1170 ? SYM_FUNC : SYM_DATA);
1171 if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
1172 curr_sym = DEBUG_AddSymbol( symname, &new_value, NULL, flags );
1173 else
1174 curr_sym = DEBUG_AddSymbol( symname, &new_value, curfile, flags );
1177 * Record the size of the symbol. This can come in handy in
1178 * some cases. Not really used yet, however.
1180 if( symp->st_size != 0 )
1181 DEBUG_SetSymbolSize(curr_sym, symp->st_size);
1184 return TRUE;
1188 * Loads the symbolic information from ELF module stored in 'filename'
1189 * the module has been loaded at 'load_offset' address, so symbols' address
1190 * relocation is performed
1191 * returns
1192 * -1 if the file cannot be found/opened
1193 * 0 if the file doesn't contain symbolic info (or this info cannot be
1194 * read or parsed)
1195 * 1 on success
1197 enum DbgInfoLoad DEBUG_LoadElfStabs(DBG_MODULE* module)
1199 enum DbgInfoLoad dil = DIL_ERROR;
1200 char* addr = (char*)0xffffffff;
1201 int fd = -1;
1202 struct stat statbuf;
1203 Elf32_Ehdr* ehptr;
1204 Elf32_Shdr* spnt;
1205 char* shstrtab;
1206 int i;
1207 int stabsect;
1208 int stabstrsect;
1210 if (module->type != DMT_ELF || ! module->elf_info) {
1211 DEBUG_Printf(DBG_CHN_ERR, "Bad elf module '%s'\n", module->module_name);
1212 return DIL_ERROR;
1215 /* check that the file exists, and that the module hasn't been loaded yet */
1216 if (stat(module->module_name, &statbuf) == -1) goto leave;
1217 if (S_ISDIR(statbuf.st_mode)) goto leave;
1220 * Now open the file, so that we can mmap() it.
1222 if ((fd = open(module->module_name, O_RDONLY)) == -1) goto leave;
1224 dil = DIL_NOINFO;
1226 * Now mmap() the file.
1228 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1229 if (addr == (char*)0xffffffff) goto leave;
1232 * Next, we need to find a few of the internal ELF headers within
1233 * this thing. We need the main executable header, and the section
1234 * table.
1236 ehptr = (Elf32_Ehdr*) addr;
1237 spnt = (Elf32_Shdr*) (addr + ehptr->e_shoff);
1238 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1240 stabsect = stabstrsect = -1;
1242 for (i = 0; i < ehptr->e_shnum; i++) {
1243 if (strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0)
1244 stabsect = i;
1246 if (strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0)
1247 stabstrsect = i;
1250 if (stabsect == -1 || stabstrsect == -1) {
1251 DEBUG_Printf(DBG_CHN_WARN, "no .stab section\n");
1252 goto leave;
1256 * OK, now just parse all of the stabs.
1258 if (DEBUG_ParseStabs(addr,
1259 module->elf_info->elf_addr,
1260 spnt[stabsect].sh_offset,
1261 spnt[stabsect].sh_size,
1262 spnt[stabstrsect].sh_offset,
1263 spnt[stabstrsect].sh_size)) {
1264 dil = DIL_LOADED;
1265 } else {
1266 dil = DIL_ERROR;
1267 DEBUG_Printf(DBG_CHN_WARN, "bad stabs\n");
1268 goto leave;
1271 for (i = 0; i < ehptr->e_shnum; i++) {
1272 if ( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1273 && (spnt[i].sh_type == SHT_SYMTAB))
1274 DEBUG_ProcessElfSymtab(module, addr, module->elf_info->elf_addr,
1275 spnt + i, spnt + spnt[i].sh_link);
1277 if ( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1278 && (spnt[i].sh_type == SHT_DYNSYM))
1279 DEBUG_ProcessElfSymtab(module, addr, module->elf_info->elf_addr,
1280 spnt + i, spnt + spnt[i].sh_link);
1283 leave:
1284 if (addr != (char*)0xffffffff) munmap(addr, statbuf.st_size);
1285 if (fd != -1) close(fd);
1287 return dil;
1291 * Loads the information for ELF module stored in 'filename'
1292 * the module has been loaded at 'load_offset' address
1293 * returns
1294 * -1 if the file cannot be found/opened
1295 * 0 if the file doesn't contain symbolic info (or this info cannot be
1296 * read or parsed)
1297 * 1 on success
1299 static enum DbgInfoLoad DEBUG_ProcessElfFile(const char* filename,
1300 unsigned int load_offset,
1301 unsigned int* dyn_addr)
1303 enum DbgInfoLoad dil = DIL_ERROR;
1304 char* addr = (char*)0xffffffff;
1305 int fd = -1;
1306 struct stat statbuf;
1307 Elf32_Ehdr* ehptr;
1308 Elf32_Shdr* spnt;
1309 Elf32_Phdr* ppnt;
1310 char * shstrtab;
1311 int i;
1312 DBG_MODULE* module = NULL;
1313 DWORD size;
1314 DWORD delta;
1316 DEBUG_Printf(DBG_CHN_TRACE, "Processing elf file '%s'\n", filename);
1318 /* check that the file exists, and that the module hasn't been loaded yet */
1319 if (stat(filename, &statbuf) == -1) goto leave;
1322 * Now open the file, so that we can mmap() it.
1324 if ((fd = open(filename, O_RDONLY)) == -1) goto leave;
1327 * Now mmap() the file.
1329 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1330 if (addr == (char*)0xffffffff) goto leave;
1332 dil = DIL_NOINFO;
1335 * Next, we need to find a few of the internal ELF headers within
1336 * this thing. We need the main executable header, and the section
1337 * table.
1339 ehptr = (Elf32_Ehdr*) addr;
1340 spnt = (Elf32_Shdr*) (addr + ehptr->e_shoff);
1341 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1343 /* if non relocatable ELF, then remove fixed address from computation
1344 * otherwise, all addresses are zero based
1346 delta = (load_offset == 0) ? ehptr->e_entry : 0;
1348 /* grab size of module once loaded in memory */
1349 ppnt = (Elf32_Phdr*) (addr + ehptr->e_phoff);
1350 size = 0;
1351 for (i = 0; i < ehptr->e_phnum; i++) {
1352 if (ppnt[i].p_type != PT_LOAD) continue;
1353 if (size < ppnt[i].p_vaddr - delta + ppnt[i].p_memsz)
1354 size = ppnt[i].p_vaddr - delta + ppnt[i].p_memsz;
1357 for (i = 0; i < ehptr->e_shnum; i++) {
1358 if (strcmp(shstrtab + spnt[i].sh_name, ".bss") == 0 &&
1359 spnt[i].sh_type == SHT_NOBITS) {
1360 if (size < spnt[i].sh_addr - delta + spnt[i].sh_size)
1361 size = spnt[i].sh_addr - delta + spnt[i].sh_size;
1363 if (strcmp(shstrtab + spnt[i].sh_name, ".dynamic") == 0 &&
1364 spnt[i].sh_type == SHT_DYNAMIC) {
1365 if (dyn_addr) *dyn_addr = spnt[i].sh_addr;
1369 module = DEBUG_RegisterELFModule((load_offset == 0) ? ehptr->e_entry : load_offset,
1370 size, filename);
1371 if (!module) {
1372 dil = DIL_ERROR;
1373 goto leave;
1376 if ((module->elf_info = DBG_alloc(sizeof(ELF_DBG_INFO))) == NULL) {
1377 DEBUG_Printf(DBG_CHN_ERR, "OOM\n");
1378 exit(0);
1381 module->elf_info->elf_addr = load_offset;
1382 dil = DEBUG_LoadElfStabs(module);
1384 leave:
1385 if (addr != (char*)0xffffffff) munmap(addr, statbuf.st_size);
1386 if (fd != -1) close(fd);
1387 if (module) module->dil = dil;
1389 return dil;
1392 static enum DbgInfoLoad DEBUG_ProcessElfFileFromPath(const char * filename,
1393 unsigned int load_offset,
1394 unsigned int* dyn_addr,
1395 const char* path)
1397 enum DbgInfoLoad dil = DIL_ERROR;
1398 char *s, *t, *fn;
1399 char* paths = NULL;
1401 if (!path) return -1;
1403 for (s = paths = DBG_strdup(path); s && *s; s = (t) ? (t+1) : NULL) {
1404 t = strchr(s, ':');
1405 if (t) *t = '\0';
1406 fn = (char*)DBG_alloc(strlen(filename) + 1 + strlen(s) + 1);
1407 if (!fn) break;
1408 strcpy(fn, s );
1409 strcat(fn, "/");
1410 strcat(fn, filename);
1411 dil = DEBUG_ProcessElfFile(fn, load_offset, dyn_addr);
1412 DBG_free(fn);
1413 if (dil != DIL_ERROR) break;
1414 s = (t) ? (t+1) : NULL;
1417 DBG_free(paths);
1418 return dil;
1421 static enum DbgInfoLoad DEBUG_ProcessElfObject(const char* filename,
1422 unsigned int load_offset,
1423 unsigned int* dyn_addr)
1425 enum DbgInfoLoad dil = DIL_ERROR;
1427 if (filename == NULL) return DIL_ERROR;
1428 if (DEBUG_FindModuleByName(filename, DMT_ELF)) return DIL_LOADED;
1430 dil = DEBUG_ProcessElfFile(filename, load_offset, dyn_addr);
1432 /* if relative pathname, try some absolute base dirs */
1433 if (dil == DIL_ERROR && !strchr(filename, '/')) {
1434 dil = DEBUG_ProcessElfFileFromPath(filename, load_offset, dyn_addr, getenv("PATH"));
1435 if (dil == DIL_ERROR)
1436 dil = DEBUG_ProcessElfFileFromPath(filename, load_offset, dyn_addr, getenv("LD_LIBRARY_PATH"));
1439 DEBUG_ReportDIL(dil, "ELF", filename, load_offset);
1441 return dil;
1444 static BOOL DEBUG_WalkList(struct r_debug* dbg_hdr)
1446 u_long lm_addr;
1447 struct link_map lm;
1448 Elf32_Ehdr ehdr;
1449 char bufstr[256];
1452 * Now walk the linked list. In all known ELF implementations,
1453 * the dynamic loader maintains this linked list for us. In some
1454 * cases the first entry doesn't appear with a name, in other cases it
1455 * does.
1457 for (lm_addr = (u_long)dbg_hdr->r_map; lm_addr; lm_addr = (u_long)lm.l_next) {
1458 if (!DEBUG_READ_MEM_VERBOSE((void*)lm_addr, &lm, sizeof(lm)))
1459 return FALSE;
1460 if (lm.l_addr != 0 &&
1461 DEBUG_READ_MEM_VERBOSE((void*)lm.l_addr, &ehdr, sizeof(ehdr)) &&
1462 ehdr.e_type == ET_DYN && /* only look at dynamic modules */
1463 lm.l_name != NULL &&
1464 DEBUG_READ_MEM_VERBOSE((void*)lm.l_name, bufstr, sizeof(bufstr))) {
1465 bufstr[sizeof(bufstr) - 1] = '\0';
1466 DEBUG_ProcessElfObject(bufstr, (unsigned)lm.l_addr, NULL);
1470 return TRUE;
1473 static BOOL DEBUG_RescanElf(void)
1475 struct r_debug dbg_hdr;
1477 if (!DEBUG_CurrProcess ||
1478 !DEBUG_READ_MEM_VERBOSE((void*)DEBUG_CurrProcess->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)))
1479 return FALSE;
1481 switch (dbg_hdr.r_state) {
1482 case RT_CONSISTENT:
1483 DEBUG_WalkList(&dbg_hdr);
1484 break;
1485 case RT_ADD:
1486 break;
1487 case RT_DELETE:
1488 /* FIXME: this is not currently handled, would need some kind of mark&sweep algo */
1489 break;
1491 return FALSE;
1494 enum DbgInfoLoad DEBUG_ReadExecutableDbgInfo(const char* exe_name)
1496 Elf32_Dyn dyn;
1497 struct r_debug dbg_hdr;
1498 enum DbgInfoLoad dil = DIL_NOINFO;
1499 unsigned int dyn_addr;
1502 * Make sure we can stat and open this file.
1504 if (exe_name == NULL) goto leave;
1505 DEBUG_ProcessElfObject(exe_name, 0, &dyn_addr);
1507 do {
1508 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn_addr, &dyn, sizeof(dyn)))
1509 goto leave;
1510 dyn_addr += sizeof(dyn);
1511 } while (dyn.d_tag != DT_DEBUG && dyn.d_tag != DT_NULL);
1512 if (dyn.d_tag == DT_NULL) goto leave;
1515 * OK, now dig into the actual tables themselves.
1517 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn.d_un.d_ptr, &dbg_hdr, sizeof(dbg_hdr)))
1518 goto leave;
1520 assert(!DEBUG_CurrProcess->dbg_hdr_addr);
1521 DEBUG_CurrProcess->dbg_hdr_addr = (u_long)dyn.d_un.d_ptr;
1523 if (dbg_hdr.r_brk) {
1524 DBG_VALUE value;
1526 DEBUG_Printf(DBG_CHN_TRACE, "Setting up a breakpoint on r_brk(%lx)\n",
1527 (unsigned long)dbg_hdr.r_brk);
1529 DEBUG_SetBreakpoints(FALSE);
1530 value.type = NULL;
1531 value.cookie = DV_TARGET;
1532 value.addr.seg = 0;
1533 value.addr.off = (DWORD)dbg_hdr.r_brk;
1534 DEBUG_AddBreakpoint(&value, DEBUG_RescanElf);
1535 DEBUG_SetBreakpoints(TRUE);
1538 dil = DEBUG_WalkList(&dbg_hdr);
1540 leave:
1541 return dil;
1544 #else /* !__ELF__ */
1546 int DEBUG_ReadExecutableDbgInfo(const char* exe_name)
1548 return FALSE;
1551 #endif /* __ELF__ */