Implementation of custom dialog messages and notifications.
[wine.git] / debugger / stabs.c
blobb318a0d2606d4c00ecc1f4a2e9a35048b0ad21f2
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 #ifdef HAVE_SYS_MMAN_H
41 # include <sys/mman.h>
42 #endif
43 #elif defined(__EMX__)
44 #ifdef HAVE_A_OUT_H
45 # include <a_out.h>
46 #endif
47 #else
48 #ifdef HAVE_A_OUT_H
49 # include <a.out.h>
50 #endif
51 #endif
53 #ifndef N_UNDF
54 #define N_UNDF 0x00
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 * Set so that we know the main executable name and path.
79 char * DEBUG_argv0;
81 struct stab_nlist {
82 union {
83 char *n_name;
84 struct stab_nlist *n_next;
85 long n_strx;
86 } n_un;
87 unsigned char n_type;
88 char n_other;
89 short n_desc;
90 unsigned long n_value;
94 * This is used to keep track of known datatypes so that we don't redefine
95 * them over and over again. It sucks up lots of memory otherwise.
97 struct known_typedef
99 struct known_typedef * next;
100 char * name;
101 int ndefs;
102 struct datatype * types[1];
105 #define NR_STAB_HASH 521
107 struct known_typedef * ktd_head[NR_STAB_HASH] = {NULL,};
109 static unsigned int stab_hash( const char * name )
111 unsigned int hash = 0;
112 unsigned int tmp;
113 const char * p;
115 p = name;
117 while (*p)
119 hash = (hash << 4) + *p++;
121 if( (tmp = (hash & 0xf0000000)) )
123 hash ^= tmp >> 24;
125 hash &= ~tmp;
127 return hash % NR_STAB_HASH;
131 static void stab_strcpy(char * dest, const char * source)
134 * A strcpy routine that stops when we hit the ':' character.
135 * Faster than copying the whole thing, and then nuking the
136 * ':'.
138 while(*source != '\0' && *source != ':')
139 *dest++ = *source++;
140 *dest++ = '\0';
143 extern void DEBUG_PrintAType(struct datatype*, int);
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 #define MAX_TD_NESTING 128
246 static
247 struct datatype**
248 DEBUG_FileSubNr2StabEnum(int filenr, int subnr)
250 struct datatype** ret;
252 /* fprintf(stderr, "creating type id for (%d,%d)\n", filenr, subnr); */
254 /* FIXME: I could perhaps create a dummy include_def for each compilation
255 * unit which would allow not to handle those two cases separately
257 if (filenr == 0)
259 if (cu_nrofentries <= subnr)
261 cu_vector = DBG_realloc(cu_vector, sizeof(cu_vector[0])*(subnr+1));
262 memset(cu_vector+cu_nrofentries, 0, sizeof(cu_vector[0])*(subnr+1-cu_nrofentries));
263 cu_nrofentries = subnr + 1;
265 ret = &cu_vector[subnr];
267 else
269 include_def* idef;
271 assert(filenr <= cu_include_stk_idx);
273 idef = &include_defs[cu_include_stack[filenr]];
275 if (idef->nrofentries <= subnr)
277 idef->vector = DBG_realloc(idef->vector, sizeof(idef->vector[0])*(subnr+1));
278 memset(idef->vector + idef->nrofentries, 0, sizeof(idef->vector[0])*(subnr+1-idef->nrofentries));
279 idef->nrofentries = subnr + 1;
281 ret = &idef->vector[subnr];
283 /* fprintf(stderr,"(%d,%d) is %d\n",filenr,subnr,ret); */
284 return ret;
287 static
288 struct datatype**
289 DEBUG_ReadTypeEnumBackwards(char*x) {
290 int filenr,subnr;
292 if (*x==')') {
293 while (*x!='(')
294 x--;
295 x++; /* '(' */
296 filenr=strtol(x,&x,10); /* <int> */
297 x++; /* ',' */
298 subnr=strtol(x,&x,10); /* <int> */
299 x++; /* ')' */
300 } else {
301 while ((*x>='0') && (*x<='9'))
302 x--;
303 filenr = 0;
304 subnr = atol(x+1);
306 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
309 static
310 struct datatype**
311 DEBUG_ReadTypeEnum(char **x) {
312 int filenr,subnr;
314 if (**x=='(') {
315 (*x)++; /* '(' */
316 filenr=strtol(*x,x,10); /* <int> */
317 (*x)++; /* ',' */
318 subnr=strtol(*x,x,10); /* <int> */
319 (*x)++; /* ')' */
320 } else {
321 filenr = 0;
322 subnr = strtol(*x,x,10); /* <int> */
324 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
327 static
329 DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef)
331 int hash;
332 struct known_typedef * ktd;
334 if( ndef == 1 )
335 return TRUE;
337 ktd = (struct known_typedef *) DBG_alloc(sizeof(struct known_typedef)
338 + (ndef - 1) * sizeof(struct datatype *));
340 hash = stab_hash(name);
342 ktd->name = DBG_strdup(name);
343 ktd->ndefs = ndef;
344 memcpy(&ktd->types[0], types, ndef * sizeof(struct datatype *));
345 ktd->next = ktd_head[hash];
346 ktd_head[hash] = ktd;
348 return TRUE;
351 static
353 DEBUG_HandlePreviousTypedef(const char * name, const char * stab)
355 int count;
356 enum debug_type expect;
357 int hash;
358 struct known_typedef * ktd;
359 char * ptr;
361 hash = stab_hash(name);
363 for(ktd = ktd_head[hash]; ktd; ktd = ktd->next)
364 if ((ktd->name[0] == name[0]) && (strcmp(name, ktd->name) == 0) )
365 break;
368 * Didn't find it. This must be a new one.
370 if( ktd == NULL )
371 return FALSE;
374 * Examine the stab to make sure it has the same number of definitions.
376 count = 0;
377 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
379 if( count >= ktd->ndefs )
380 return FALSE;
383 * Make sure the types of all of the objects is consistent with
384 * what we have already parsed.
386 switch(ptr[1])
388 case '*':
389 expect = DT_POINTER;
390 break;
391 case 's':
392 case 'u':
393 expect = DT_STRUCT;
394 break;
395 case 'a':
396 expect = DT_ARRAY;
397 break;
398 case '(': /* it's mainly a ref to another typedef, skip it */
399 expect = -1;
400 break;
401 case '1':
402 case 'r':
403 expect = DT_BASIC;
404 break;
405 case 'x':
406 expect = DT_STRUCT;
407 break;
408 case 'e':
409 expect = DT_ENUM;
410 break;
411 case 'f':
412 expect = DT_FUNC;
413 break;
414 default:
415 fprintf(stderr, "Unknown type (%c).\n",ptr[1]);
416 return FALSE;
418 if( expect != -1 && expect != DEBUG_GetType(ktd->types[count]) )
419 return FALSE;
420 count++;
423 if( ktd->ndefs != count )
424 return FALSE;
427 * Go through, dig out all of the type numbers, and substitute the
428 * appropriate things.
430 count = 0;
431 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
432 *DEBUG_ReadTypeEnumBackwards(ptr-1) = ktd->types[count++];
434 return TRUE;
437 static int DEBUG_FreeRegisteredTypedefs()
439 int count;
440 int j;
441 struct known_typedef * ktd;
442 struct known_typedef * next;
444 count = 0;
445 for(j=0; j < NR_STAB_HASH; j++ )
447 for(ktd = ktd_head[j]; ktd; ktd = next)
449 count++;
450 next = ktd->next;
451 DBG_free(ktd->name);
452 DBG_free(ktd);
454 ktd_head[j] = NULL;
457 return TRUE;
461 static
463 DEBUG_ParseTypedefStab(char * ptr, const char * typename)
465 int arrmax;
466 int arrmin;
467 char * c;
468 struct datatype * curr_type;
469 struct datatype * datatype;
470 struct datatype * curr_types[MAX_TD_NESTING];
471 char element_name[1024];
472 int ntypes = 0, ntp;
473 int offset;
474 const char * orig_typename;
475 int size;
476 char * tc;
477 char * tc2;
478 int failure;
480 orig_typename = typename;
482 if( DEBUG_HandlePreviousTypedef(typename, ptr) )
483 return TRUE;
486 * Go from back to front. First we go through and figure out what
487 * type numbers we need, and register those types. Then we go in
488 * and fill the details.
491 for( c = strchr(ptr, '='); c != NULL; c = strchr(c + 1, '=') )
494 * Back up until we get to a non-numeric character, to get datatype
496 struct datatype** dt = DEBUG_ReadTypeEnumBackwards(c-1);
498 if( ntypes >= MAX_TD_NESTING )
501 * If this ever happens, just bump the counter.
503 fprintf(stderr, "Typedef nesting overflow\n");
504 return FALSE;
507 switch(c[1])
509 case '*':
510 *dt = DEBUG_NewDataType(DT_POINTER, NULL);
511 curr_types[ntypes++] = *dt;
512 break;
513 case 's':
514 case 'u':
515 *dt = DEBUG_NewDataType(DT_STRUCT, typename);
516 curr_types[ntypes++] = *dt;
517 break;
518 case 'a':
519 *dt = DEBUG_NewDataType(DT_ARRAY, NULL);
520 curr_types[ntypes++] = *dt;
521 break;
522 case '(':
523 /* will be handled in next loop,
524 * just a ref to another type
526 curr_types[ntypes++] = NULL;
527 break;
528 case '1':
529 case 'r':
530 *dt = DEBUG_NewDataType(DT_BASIC, typename);
531 curr_types[ntypes++] = *dt;
532 break;
533 case 'x':
534 stab_strcpy(element_name, c + 3);
535 *dt = DEBUG_NewDataType(DT_STRUCT, element_name);
536 curr_types[ntypes++] = *dt;
537 break;
538 case 'e':
539 *dt = DEBUG_NewDataType(DT_ENUM, NULL);
540 curr_types[ntypes++] = *dt;
541 break;
542 case 'f':
543 *dt = DEBUG_NewDataType(DT_FUNC, NULL);
544 curr_types[ntypes++] = *dt;
545 break;
546 default:
547 fprintf(stderr, "Unknown type (%c).\n",c[1]);
549 typename = NULL;
553 ntp = ntypes - 1;
555 * OK, now take a second sweep through. Now we will be digging
556 * out the definitions of the various components, and storing
557 * them in the skeletons that we have already allocated. We take
558 * a right-to left search as this is much easier to parse.
560 for( c = strrchr(ptr, '='); c != NULL; c = strrchr(ptr, '=') )
562 struct datatype** dt = DEBUG_ReadTypeEnumBackwards(c-1);
563 struct datatype** dt2;
565 curr_type = *dt;
567 switch(c[1])
569 case 'x':
570 ntp--;
571 tc = c + 3;
572 while( *tc != ':' )
573 tc++;
574 tc++;
575 if( *tc == '\0' )
576 *c = '\0';
577 else
578 strcpy(c, tc);
579 break;
580 case '*':
581 case 'f':
582 ntp--;
583 tc = c + 2;
584 datatype = *DEBUG_ReadTypeEnum(&tc);
585 DEBUG_SetPointerType(curr_type, datatype);
586 if( *tc == '\0' )
587 *c = '\0';
588 else
589 strcpy(c, tc);
590 break;
591 case '(':
592 tc = c + 1;
593 dt2 = DEBUG_ReadTypeEnum(&tc);
595 if (!*dt && *dt2)
597 *dt = *dt2;
599 else if (!*dt && !*dt2)
601 /* this should be a basic type, define it */
602 *dt2 = *dt = DEBUG_NewDataType(DT_BASIC, typename);
604 else
606 fprintf(stderr, "Unknown condition %p %p (%s)\n", *dt, *dt2, ptr);
608 if( *tc == '\0' )
609 *c = '\0';
610 else
611 strcpy(c, tc);
612 curr_types[ntp--] = *dt;
613 break;
614 case '1':
615 case 'r':
616 ntp--;
618 * We have already handled these above.
620 *c = '\0';
621 break;
622 case 'a':
623 ntp--;
624 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */
626 tc = c + 3;
627 /* 'r' */
628 DEBUG_ReadTypeEnum(&tc);
629 tc++; /* ';' */
630 arrmin = strtol(tc, &tc, 10); /* <int> */
631 tc++; /* ';' */
632 arrmax = strtol(tc, &tc, 10); /* <int> */
633 tc++; /* ';' */
634 datatype = *DEBUG_ReadTypeEnum(&tc); /* <typeinfo> */
635 if( *tc == '\0' )
636 *c = '\0';
637 else
638 strcpy(c, tc);
639 DEBUG_SetArrayParams(curr_type, arrmin, arrmax, datatype);
640 break;
641 case 's':
642 case 'u':
643 ntp--;
644 failure = 0;
646 tc = c + 2;
647 if( DEBUG_SetStructSize(curr_type, strtol(tc, &tc, 10)) == FALSE )
650 * We have already filled out this structure. Nothing to do,
651 * so just skip forward to the end of the definition.
653 while( tc[0] != ';' && tc[1] != ';' )
654 tc++;
656 tc += 2;
658 if( *tc == '\0' )
659 *c = '\0';
660 else
661 strcpy(c, tc + 1);
662 continue;
666 * Now parse the individual elements of the structure/union.
668 while(*tc != ';')
670 char *ti;
671 tc2 = element_name;
672 while(*tc != ':')
673 *tc2++ = *tc++;
674 tc++;
675 *tc2++ = '\0';
676 ti=tc;
677 datatype = *DEBUG_ReadTypeEnum(&tc);
678 *tc='\0';
679 tc++;
680 offset = strtol(tc, &tc, 10);
681 tc++;
682 size = strtol(tc, &tc, 10);
683 tc++;
684 if (datatype)
685 DEBUG_AddStructElement(curr_type, element_name, datatype,
686 offset, size);
687 else
689 failure = 1;
690 /* ... but proceed parsing to the end of the stab */
691 fprintf(stderr, "failure on %s %s\n", ptr, ti);
695 if (failure)
698 /* if we had a undeclared value this one is undeclared too.
699 * remove it from the stab_types.
700 * I just set it to NULL to detect bugs in my thoughtprocess.
701 * FIXME: leaks the memory for the structure elements.
702 * FIXME: such structures should have been optimized away
703 * by ld.
705 *dt = NULL;
707 if( *tc == '\0' )
708 *c = '\0';
709 else
710 strcpy(c, tc + 1);
711 break;
712 case 'e':
713 ntp--;
714 tc = c + 2;
716 * Now parse the individual elements of the structure/union.
718 while(*tc != ';')
720 tc2 = element_name;
721 while(*tc != ':')
722 *tc2++ = *tc++;
723 tc++;
724 *tc2++ = '\0';
725 offset = strtol(tc, &tc, 10);
726 tc++;
727 DEBUG_AddStructElement(curr_type, element_name, NULL, offset, 0);
729 if( *tc == '\0' )
730 *c = '\0';
731 else
732 strcpy(c, tc + 1);
733 break;
734 default:
735 fprintf(stderr, "Unknown type (%c).\n",c[1]);
736 break;
740 * Now register the type so that if we encounter it again, we will know
741 * what to do.
743 DEBUG_RegisterTypedef(orig_typename, curr_types, ntypes);
745 return TRUE;
748 static struct datatype *
749 DEBUG_ParseStabType(const char * stab)
751 char * c;
754 * Look through the stab definition, and figure out what datatype
755 * this represents. If we have something we know about, assign the
756 * type.
758 c = strchr(stab, ':');
759 if( c == NULL )
760 return NULL;
762 c++;
764 * The next character says more about the type (i.e. data, function, etc)
765 * of symbol. Skip it.
767 if (*c != '(')
768 c++;
770 * The next is either an integer or a (integer,integer).
771 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
773 return *DEBUG_ReadTypeEnum(&c);
777 DEBUG_ParseStabs(char * addr, unsigned int load_offset,
778 unsigned int staboff, int stablen,
779 unsigned int strtaboff, int strtablen)
781 struct name_hash * curr_func = NULL;
782 struct wine_locals * curr_loc = NULL;
783 struct name_hash * curr_sym = NULL;
784 char currpath[PATH_MAX];
785 int i;
786 int ignore = FALSE;
787 int last_nso = -1;
788 int len;
789 DBG_ADDR new_addr;
790 int nstab;
791 char * ptr;
792 char * stabbuff;
793 int stabbufflen;
794 struct stab_nlist * stab_ptr;
795 char * strs;
796 int strtabinc;
797 char * subpath = NULL;
798 char symname[4096];
800 nstab = stablen / sizeof(struct stab_nlist);
801 stab_ptr = (struct stab_nlist *) (addr + staboff);
802 strs = (char *) (addr + strtaboff);
804 memset(currpath, 0, sizeof(currpath));
807 * Allocate a buffer into which we can build stab strings for cases
808 * where the stab is continued over multiple lines.
810 stabbufflen = 65536;
811 stabbuff = (char *) DBG_alloc(stabbufflen);
813 strtabinc = 0;
814 stabbuff[0] = '\0';
815 for(i=0; i < nstab; i++, stab_ptr++ )
817 ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
818 if( ptr[strlen(ptr) - 1] == '\\' )
821 * Indicates continuation. Append this to the buffer, and go onto the
822 * next record. Repeat the process until we find a stab without the
823 * '/' character, as this indicates we have the whole thing.
825 len = strlen(ptr);
826 if( strlen(stabbuff) + len > stabbufflen )
828 stabbufflen += 65536;
829 stabbuff = (char *) DBG_realloc(stabbuff, stabbufflen);
831 strncat(stabbuff, ptr, len - 1);
832 continue;
834 else if( stabbuff[0] != '\0' )
836 strcat( stabbuff, ptr);
837 ptr = stabbuff;
840 if( strchr(ptr, '=') != NULL )
843 * The stabs aren't in writable memory, so copy it over so we are
844 * sure we can scribble on it.
846 if( ptr != stabbuff )
848 strcpy(stabbuff, ptr);
849 ptr = stabbuff;
851 stab_strcpy(symname, ptr);
852 DEBUG_ParseTypedefStab(ptr, symname);
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, they actually do make some amount of sense.
866 new_addr.seg = 0;
867 new_addr.type = DEBUG_ParseStabType(ptr);
868 new_addr.off = load_offset + stab_ptr->n_value;
870 stab_strcpy(symname, ptr);
871 #ifdef __ELF__
872 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
873 SYM_WINE | SYM_DATA | SYM_INVALID);
874 #else
875 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
876 SYM_WINE | SYM_DATA );
877 #endif
878 break;
879 case N_RBRAC:
880 case N_LBRAC:
882 * We need to keep track of these so we get symbol scoping
883 * right for local variables. For now, we just ignore them.
884 * The hooks are already there for dealing with this however,
885 * so all we need to do is to keep count of the nesting level,
886 * and find the RBRAC for each matching LBRAC.
888 break;
889 case N_LCSYM:
890 case N_STSYM:
892 * These are static symbols and BSS symbols.
894 new_addr.seg = 0;
895 new_addr.type = DEBUG_ParseStabType(ptr);
896 new_addr.off = load_offset + stab_ptr->n_value;
898 stab_strcpy(symname, ptr);
899 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
900 SYM_WINE | SYM_DATA );
901 break;
902 case N_PSYM:
904 * These are function parameters.
906 if( (curr_func != NULL)
907 && (stab_ptr->n_value != 0) )
909 stab_strcpy(symname, ptr);
910 curr_loc = DEBUG_AddLocal( curr_func, 0,
911 stab_ptr->n_value, 0, 0, symname );
912 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
914 break;
915 case N_RSYM:
916 if( curr_func != NULL )
918 stab_strcpy(symname, ptr);
919 curr_loc = DEBUG_AddLocal( curr_func, stab_ptr->n_value,
920 0, 0, 0, symname );
921 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
923 break;
924 case N_LSYM:
925 if( (curr_func != NULL)
926 && (stab_ptr->n_value != 0) )
928 stab_strcpy(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 else if (curr_func == NULL)
935 stab_strcpy(symname, ptr);
937 break;
938 case N_SLINE:
940 * This is a line number. These are always relative to the start
941 * of the function (N_FUN), and this makes the lookup easier.
943 if( curr_func != NULL )
945 #ifdef __ELF__
946 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
947 stab_ptr->n_value);
948 #else
949 #if 0
951 * This isn't right. The order of the stabs is different under
952 * a.out, and as a result we would end up attaching the line
953 * number to the wrong function.
955 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
956 stab_ptr->n_value - curr_func->addr.off);
957 #endif
958 #endif
960 break;
961 case N_FUN:
963 * First, clean up the previous function we were working on.
965 DEBUG_Normalize(curr_func);
968 * For now, just declare the various functions. Later
969 * on, we will add the line number information and the
970 * local symbols.
972 if( !ignore )
974 new_addr.seg = 0;
975 new_addr.type = DEBUG_ParseStabType(ptr);
976 new_addr.off = load_offset + stab_ptr->n_value;
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 stab_strcpy(symname, ptr);
985 curr_func = DEBUG_AddSymbol( symname, &new_addr, currpath,
986 SYM_WINE | SYM_FUNC);
988 else
991 * Don't add line number information for this function
992 * any more.
994 curr_func = NULL;
996 break;
997 case N_SO:
999 * This indicates a new source file. Append the records
1000 * together, to build the correct path name.
1002 #ifndef __ELF__
1004 * With a.out, there is no NULL string N_SO entry at the end of
1005 * the file. Thus when we find non-consecutive entries,
1006 * we consider that a new file is started.
1008 if( last_nso < i-1 )
1010 currpath[0] = '\0';
1011 DEBUG_Normalize(curr_func);
1012 curr_func = NULL;
1014 #endif
1016 if( *ptr == '\0' )
1019 * Nuke old path.
1021 currpath[0] = '\0';
1022 DEBUG_Normalize(curr_func);
1023 curr_func = NULL;
1025 else
1027 if (*ptr != '/')
1028 strcat(currpath, ptr);
1029 else
1030 strcpy(currpath, ptr);
1031 subpath = ptr;
1032 DEBUG_ResetIncludes();
1034 last_nso = i;
1035 break;
1036 case N_SOL:
1038 * This indicates we are including stuff from an include file.
1039 * If this is the main source, enable the debug stuff, otherwise
1040 * ignore it.
1042 if( subpath == NULL || strcmp(ptr, subpath) == 0 )
1044 ignore = FALSE;
1046 else
1048 ignore = TRUE;
1049 DEBUG_Normalize(curr_func);
1050 curr_func = NULL;
1052 break;
1053 case N_UNDF:
1054 strs += strtabinc;
1055 strtabinc = stab_ptr->n_value;
1056 DEBUG_Normalize(curr_func);
1057 curr_func = NULL;
1058 break;
1059 case N_OPT:
1061 * Ignore this. We don't care what it points to.
1063 break;
1064 case N_BINCL:
1065 DEBUG_AddInclude(DEBUG_CreateInclude(ptr, stab_ptr->n_value));
1066 break;
1067 case N_EINCL:
1068 break;
1069 case N_EXCL:
1070 DEBUG_AddInclude(DEBUG_FindInclude(ptr, stab_ptr->n_value));
1071 break;
1072 case N_MAIN:
1074 * Always ignore these. GCC doesn't even generate them.
1076 break;
1077 default:
1078 fprintf(stderr, "Unkown stab type 0x%02x\n", stab_ptr->n_type);
1079 break;
1082 stabbuff[0] = '\0';
1084 #if 0
1085 fprintf(stderr, "%d %x %s\n", stab_ptr->n_type,
1086 (unsigned int) stab_ptr->n_value,
1087 strs + (unsigned int) stab_ptr->n_un.n_name);
1088 #endif
1091 DEBUG_FreeRegisteredTypedefs();
1092 DEBUG_FreeIncludes();
1094 return TRUE;
1097 #ifdef __ELF__
1100 * Walk through the entire symbol table and add any symbols we find there.
1101 * This can be used in cases where we have stripped ELF shared libraries,
1102 * or it can be used in cases where we have data symbols for which the address
1103 * isn't encoded in the stabs.
1105 * This is all really quite easy, since we don't have to worry about line
1106 * numbers or local data variables.
1108 static
1110 DEBUG_ProcessElfSymtab(char * addr, unsigned int load_offset,
1111 Elf32_Shdr * symtab, Elf32_Shdr * strtab)
1113 char * curfile = NULL;
1114 struct name_hash * curr_sym = NULL;
1115 int flags;
1116 int i;
1117 DBG_ADDR new_addr;
1118 int nsym;
1119 char * strp;
1120 char * symname;
1121 Elf32_Sym * symp;
1124 symp = (Elf32_Sym *) (addr + symtab->sh_offset);
1125 nsym = symtab->sh_size / sizeof(*symp);
1126 strp = (char *) (addr + strtab->sh_offset);
1128 for(i=0; i < nsym; i++, symp++)
1131 * Ignore certain types of entries which really aren't of that much
1132 * interest.
1134 if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION )
1136 continue;
1139 symname = strp + symp->st_name;
1142 * Save the name of the current file, so we have a way of tracking
1143 * static functions/data.
1145 if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
1147 curfile = symname;
1148 continue;
1153 * See if we already have something for this symbol.
1154 * If so, ignore this entry, because it would have come from the
1155 * stabs or from a previous symbol. If the value is different,
1156 * we will have to keep the darned thing, because there can be
1157 * multiple local symbols by the same name.
1159 if( (DEBUG_GetSymbolValue(symname, -1, &new_addr, FALSE ) == TRUE)
1160 && (new_addr.off == (load_offset + symp->st_value)) )
1161 continue;
1163 new_addr.seg = 0;
1164 new_addr.type = NULL;
1165 new_addr.off = load_offset + symp->st_value;
1166 flags = SYM_WINE | (ELF32_ST_BIND(symp->st_info) == STT_FUNC
1167 ? SYM_FUNC : SYM_DATA);
1168 if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
1169 curr_sym = DEBUG_AddSymbol( symname, &new_addr, NULL, flags );
1170 else
1171 curr_sym = DEBUG_AddSymbol( symname, &new_addr, curfile, flags );
1174 * Record the size of the symbol. This can come in handy in
1175 * some cases. Not really used yet, however.
1177 if( symp->st_size != 0 )
1178 DEBUG_SetSymbolSize(curr_sym, symp->st_size);
1181 return TRUE;
1184 static
1186 DEBUG_ProcessElfObject(char * filename, unsigned int load_offset)
1188 int rtn = FALSE;
1189 struct stat statbuf;
1190 int fd = -1;
1191 int status;
1192 char * addr = (char *) 0xffffffff;
1193 Elf32_Ehdr * ehptr;
1194 Elf32_Shdr * spnt;
1195 char * shstrtab;
1196 int nsect;
1197 int i;
1198 int stabsect;
1199 int stabstrsect;
1203 * Make sure we can stat and open this file.
1205 if( filename == NULL )
1206 goto leave;
1208 status = stat(filename, &statbuf);
1209 if( status == -1 )
1211 char *s,*t,*fn,*paths;
1212 if (strchr(filename,'/'))
1213 goto leave;
1214 paths = DBG_strdup(getenv("PATH"));
1215 s = paths;
1216 while (s && *s) {
1217 t = strchr(s,':');
1218 if (t) *t='\0';
1219 fn = (char*)DBG_alloc(strlen(filename)+1+strlen(s)+1);
1220 strcpy(fn,s);
1221 strcat(fn,"/");
1222 strcat(fn,filename);
1223 if ((rtn = DEBUG_ProcessElfObject(fn,load_offset))) {
1224 DBG_free(fn);
1225 DBG_free(paths);
1226 goto leave;
1228 DBG_free(fn);
1229 if (t) s = t+1; else break;
1231 if (!s || !*s) fprintf(stderr," not found");
1232 DBG_free(paths);
1233 goto leave;
1237 * Now open the file, so that we can mmap() it.
1239 fd = open(filename, O_RDONLY);
1240 if( fd == -1 )
1241 goto leave;
1245 * Now mmap() the file.
1247 addr = mmap(0, statbuf.st_size, PROT_READ,
1248 MAP_PRIVATE, fd, 0);
1249 if( addr == (char *) 0xffffffff )
1250 goto leave;
1253 * Next, we need to find a few of the internal ELF headers within
1254 * this thing. We need the main executable header, and the section
1255 * table.
1257 ehptr = (Elf32_Ehdr *) addr;
1259 if( load_offset == 0 )
1260 DEBUG_RegisterELFDebugInfo(ehptr->e_entry, statbuf.st_size, filename);
1261 else
1262 DEBUG_RegisterELFDebugInfo(load_offset, statbuf.st_size, filename);
1264 spnt = (Elf32_Shdr *) (addr + ehptr->e_shoff);
1265 nsect = ehptr->e_shnum;
1266 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1268 stabsect = stabstrsect = -1;
1270 for(i=0; i < nsect; i++)
1272 if( strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0 )
1273 stabsect = i;
1275 if( strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0 )
1276 stabstrsect = i;
1279 if( stabsect == -1 || stabstrsect == -1 )
1280 goto leave;
1283 * OK, now just parse all of the stabs.
1285 rtn = DEBUG_ParseStabs(addr, load_offset,
1286 spnt[stabsect].sh_offset,
1287 spnt[stabsect].sh_size,
1288 spnt[stabstrsect].sh_offset,
1289 spnt[stabstrsect].sh_size);
1291 if( rtn != TRUE )
1292 goto leave;
1294 for(i=0; i < nsect; i++)
1296 if( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1297 && (spnt[i].sh_type == SHT_SYMTAB) )
1298 DEBUG_ProcessElfSymtab(addr, load_offset,
1299 spnt + i, spnt + spnt[i].sh_link);
1301 if( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1302 && (spnt[i].sh_type == SHT_DYNSYM) )
1303 DEBUG_ProcessElfSymtab(addr, load_offset,
1304 spnt + i, spnt + spnt[i].sh_link);
1307 leave:
1309 if( addr != (char *) 0xffffffff )
1310 munmap(addr, statbuf.st_size);
1312 if( fd != -1 )
1313 close(fd);
1315 return (rtn);
1320 DEBUG_ReadExecutableDbgInfo(void)
1322 Elf32_Ehdr * ehdr;
1323 char * exe_name;
1324 Elf32_Dyn * dynpnt;
1325 struct r_debug * dbg_hdr;
1326 struct link_map * lpnt = NULL;
1327 #ifdef __GNUC__
1328 extern Elf32_Dyn _DYNAMIC[] __attribute__ ((weak));
1329 #else
1330 extern Elf32_Dyn _DYNAMIC[];
1331 #endif
1332 int rtn = FALSE;
1333 int rowcount;
1335 exe_name = DEBUG_argv0;
1338 * Make sure we can stat and open this file.
1340 if( exe_name == NULL )
1341 goto leave;
1343 fprintf( stderr, "Loading symbols: %s", exe_name );
1344 rowcount = 17 + strlen(exe_name);
1345 DEBUG_ProcessElfObject(exe_name, 0);
1348 * Finally walk the tables that the dynamic loader maintains to find all
1349 * of the other shared libraries which might be loaded. Perform the
1350 * same step for all of these.
1352 if( (&_DYNAMIC == NULL) || (_DYNAMIC == NULL) )
1353 goto leave;
1355 dynpnt = _DYNAMIC;
1358 * Now walk the dynamic section (of the executable, looking for a DT_DEBUG
1359 * entry.
1361 for(; dynpnt->d_tag != DT_NULL; dynpnt++)
1362 if( dynpnt->d_tag == DT_DEBUG )
1363 break;
1365 if( (dynpnt->d_tag != DT_DEBUG)
1366 || (dynpnt->d_un.d_ptr == 0) )
1367 goto leave;
1370 * OK, now dig into the actual tables themselves.
1372 dbg_hdr = (struct r_debug *) dynpnt->d_un.d_ptr;
1373 lpnt = dbg_hdr->r_map;
1376 * Now walk the linked list. In all known ELF implementations,
1377 * the dynamic loader maintains this linked list for us. In some
1378 * cases the first entry doesn't appear with a name, in other cases it
1379 * does.
1381 for(; lpnt; lpnt = lpnt->l_next )
1384 * We already got the stuff for the executable using the
1385 * argv[0] entry above. Here we only need to concentrate on any
1386 * shared libraries which may be loaded.
1388 ehdr = (Elf32_Ehdr *) lpnt->l_addr;
1389 if( (lpnt->l_addr == 0) || (ehdr->e_type != ET_DYN) )
1390 continue;
1392 if( lpnt->l_name != NULL )
1394 if (rowcount + strlen(lpnt->l_name) > 76)
1396 fprintf( stderr, "\n " );
1397 rowcount = 3;
1399 fprintf( stderr, " %s", lpnt->l_name );
1400 rowcount += strlen(lpnt->l_name) + 1;
1401 DEBUG_ProcessElfObject(lpnt->l_name, lpnt->l_addr);
1405 rtn = TRUE;
1407 leave:
1408 fprintf( stderr, "\n" );
1409 return (rtn);
1413 #else /* !__ELF__ */
1415 #ifdef linux
1417 * a.out linux.
1420 DEBUG_ReadExecutableDbgInfo(void)
1422 char * addr = (char *) 0xffffffff;
1423 char * exe_name;
1424 struct exec * ahdr;
1425 int fd = -1;
1426 int rtn = FALSE;
1427 unsigned int staboff;
1428 struct stat statbuf;
1429 int status;
1430 unsigned int stroff;
1432 exe_name = DEBUG_argv0;
1435 * Make sure we can stat and open this file.
1437 if( exe_name == NULL )
1438 goto leave;
1440 status = stat(exe_name, &statbuf);
1441 if( status == -1 )
1442 goto leave;
1445 * Now open the file, so that we can mmap() it.
1447 fd = open(exe_name, O_RDONLY);
1448 if( fd == -1 )
1449 goto leave;
1453 * Now mmap() the file.
1455 addr = mmap(0, statbuf.st_size, PROT_READ,
1456 MAP_PRIVATE, fd, 0);
1457 if( addr == (char *) 0xffffffff )
1458 goto leave;
1460 ahdr = (struct exec *) addr;
1462 staboff = N_SYMOFF(*ahdr);
1463 stroff = N_STROFF(*ahdr);
1464 rtn = DEBUG_ParseStabs(addr, 0,
1465 staboff,
1466 ahdr->a_syms,
1467 stroff,
1468 statbuf.st_size - stroff);
1471 * Give a nice status message here...
1473 fprintf( stderr, "Loading symbols: %s", exe_name );
1475 rtn = TRUE;
1477 leave:
1479 if( addr != (char *) 0xffffffff )
1480 munmap(addr, statbuf.st_size);
1482 if( fd != -1 )
1483 close(fd);
1485 return (rtn);
1488 #else
1490 * Non-linux, non-ELF platforms.
1493 DEBUG_ReadExecutableDbgInfo(void)
1495 return FALSE;
1497 #endif
1499 #endif /* __ELF__ */