Started the implementation of IMAGEHLP.DLL.
[wine.git] / debugger / stabs.c
blob3f8e6d3676311cf8c03fa756748d051d4510895e
1 /*
2 * File stabs.c - read stabs information from the wine executable itself.
4 * Copyright (C) 1996, Eric Youngdale.
5 */
7 #include <sys/types.h>
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 #include <sys/mman.h>
11 #include <limits.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #ifndef PATH_MAX
17 #define PATH_MAX _MAX_PATH
18 #endif
20 #include "win.h"
21 #include "debugger.h"
22 #include "xmalloc.h"
24 #ifdef __svr4__
25 #define __ELF__
26 #endif
28 #ifdef __ELF__
29 #include <elf.h>
30 #include <link.h>
31 #include <sys/mman.h>
32 #elif defined(__EMX__)
33 #include <a_out.h>
34 #else
35 #include <a.out.h>
36 #endif
38 #ifndef N_UNDF
39 #define N_UNDF 0x00
40 #endif
42 #define N_GSYM 0x20
43 #define N_FUN 0x24
44 #define N_STSYM 0x26
45 #define N_LCSYM 0x28
46 #define N_MAIN 0x2a
47 #define N_ROSYM 0x2c
48 #define N_OPT 0x3c
49 #define N_RSYM 0x40
50 #define N_SLINE 0x44
51 #define N_SO 0x64
52 #define N_LSYM 0x80
53 #define N_BINCL 0x82
54 #define N_SOL 0x84
55 #define N_PSYM 0xa0
56 #define N_EINCL 0xa2
57 #define N_LBRAC 0xc0
58 #define N_RBRAC 0xe0
62 * This is how we translate stab types into our internal representations
63 * of datatypes.
65 static struct datatype ** stab_types = NULL;
66 static int num_stab_types = 0;
69 * Set so that we know the main executable name and path.
71 char * DEBUG_argv0;
73 struct stab_nlist {
74 union {
75 char *n_name;
76 struct stab_nlist *n_next;
77 long n_strx;
78 } n_un;
79 unsigned char n_type;
80 char n_other;
81 short n_desc;
82 unsigned long n_value;
86 * This is used to keep track of known datatypes so that we don't redefine
87 * them over and over again. It sucks up lots of memory otherwise.
89 struct known_typedef
91 struct known_typedef * next;
92 char * name;
93 int ndefs;
94 struct datatype * types[0];
97 #define NR_STAB_HASH 521
99 struct known_typedef * ktd_head[NR_STAB_HASH] = {NULL,};
101 static unsigned int stab_hash( const char * name )
103 unsigned int hash = 0;
104 unsigned int tmp;
105 const char * p;
107 p = name;
109 while (*p)
111 hash = (hash << 4) + *p++;
113 if( (tmp = (hash & 0xf0000000)) )
115 hash ^= tmp >> 24;
117 hash &= ~tmp;
119 return hash % NR_STAB_HASH;
123 static void stab_strcpy(char * dest, const char * source)
126 * A strcpy routine that stops when we hit the ':' character.
127 * Faster than copying the whole thing, and then nuking the
128 * ':'.
130 while(*source != '\0' && *source != ':')
131 *dest++ = *source++;
132 *dest++ = '\0';
135 #define MAX_TD_NESTING 128
137 static int **typenums;
138 static int *nroftypenums=NULL;
139 static int nrofnroftypenums=0;
140 static int curtypenum = 0;
142 static
144 DEBUG_FileSubNr2StabEnum(int filenr,int subnr) {
145 if (nrofnroftypenums<=filenr) {
146 nroftypenums = xrealloc(nroftypenums,sizeof(nroftypenums[0])*(filenr+1));
147 memset(nroftypenums+nrofnroftypenums,0,(filenr+1-nrofnroftypenums)*sizeof(nroftypenums[0]));
148 typenums = xrealloc(typenums,sizeof(typenums[0])*(filenr+1));
149 memset(typenums+nrofnroftypenums,0,sizeof(typenums[0])*(filenr+1-nrofnroftypenums));
150 nrofnroftypenums=filenr+1;
152 if (nroftypenums[filenr]<=subnr) {
153 typenums[filenr] = xrealloc(typenums[filenr],sizeof(typenums[0][0])*(subnr+1));
154 memset(typenums[filenr]+nroftypenums[filenr],0,sizeof(typenums[0][0])*(subnr+1-nroftypenums[filenr]));
155 nroftypenums[filenr] = subnr+1;
157 if (!typenums[filenr][subnr])
158 typenums[filenr][subnr]=++curtypenum;
160 if( num_stab_types <= curtypenum ) {
161 num_stab_types = curtypenum + 256;
162 stab_types = (struct datatype **) xrealloc(stab_types,
163 num_stab_types * sizeof(struct datatype *)
165 memset( stab_types + curtypenum, 0, sizeof(struct datatype *) * (num_stab_types - curtypenum) );
167 //fprintf(stderr,"(%d,%d) is %d\n",filenr,subnr,typenums[filenr][subnr]);
168 return typenums[filenr][subnr];
171 static
173 DEBUG_ReadTypeEnumBackwards(char*x) {
174 int filenr,subnr;
176 if (*x==')') {
177 while (*x!='(')
178 x--;
179 x++; /* '(' */
180 filenr=strtol(x,&x,10); /* <int> */
181 x++; /* ',' */
182 subnr=strtol(x,&x,10); /* <int> */
183 x++; /* ')' */
184 } else {
185 while ((*x>='0') && (*x<='9'))
186 x--;
187 filenr = 0;
188 subnr = atol(x+1);
190 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
193 static
195 DEBUG_ReadTypeEnum(char **x) {
196 int filenr,subnr;
198 if (**x=='(') {
199 (*x)++; /* '(' */
200 filenr=strtol(*x,x,10); /* <int> */
201 (*x)++; /* ',' */
202 subnr=strtol(*x,x,10); /* <int> */
203 (*x)++; /* ')' */
204 } else {
205 filenr = 0;
206 subnr = strtol(*x,x,10); /* <int> */
208 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
211 static
213 DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef)
215 int hash;
216 struct known_typedef * ktd;
218 if( ndef == 1 )
219 return TRUE;
221 ktd = (struct known_typedef *) xmalloc(sizeof(struct known_typedef)
222 + ndef * sizeof(struct datatype *));
224 hash = stab_hash(name);
226 ktd->name = xstrdup(name);
227 ktd->ndefs = ndef;
228 memcpy(&ktd->types[0], types, ndef * sizeof(struct datatype *));
229 ktd->next = ktd_head[hash];
230 ktd_head[hash] = ktd;
232 return TRUE;
235 static
237 DEBUG_HandlePreviousTypedef(const char * name, const char * stab)
239 int count;
240 enum debug_type expect;
241 int hash;
242 struct known_typedef * ktd;
243 char * ptr;
245 hash = stab_hash(name);
247 for(ktd = ktd_head[hash]; ktd; ktd = ktd->next)
248 if ((ktd->name[0] == name[0]) && (strcmp(name, ktd->name) == 0) )
249 break;
252 * Didn't find it. This must be a new one.
254 if( ktd == NULL )
255 return FALSE;
258 * Examine the stab to make sure it has the same number of definitions.
260 count = 0;
261 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
263 if( count >= ktd->ndefs )
264 return FALSE;
267 * Make sure the types of all of the objects is consistent with
268 * what we have already parsed.
270 switch(ptr[1])
272 case '*':
273 expect = POINTER;
274 break;
275 case 's':
276 case 'u':
277 expect = STRUCT;
278 break;
279 case 'a':
280 expect = ARRAY;
281 break;
282 case '1':
283 case '(':
284 case 'r':
285 expect = BASIC;
286 break;
287 case 'x':
288 expect = STRUCT;
289 break;
290 case 'e':
291 expect = ENUM;
292 break;
293 case 'f':
294 expect = FUNC;
295 break;
296 default:
297 fprintf(stderr, "Unknown type (%c).\n",ptr[1]);
298 return FALSE;
300 if( expect != DEBUG_GetType(ktd->types[count]) )
301 return FALSE;
302 count++;
305 if( ktd->ndefs != count )
306 return FALSE;
309 * Go through, dig out all of the type numbers, and substitute the
310 * appropriate things.
312 count = 0;
313 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
314 stab_types[DEBUG_ReadTypeEnumBackwards(ptr-1)] = ktd->types[count++];
316 return TRUE;
319 static int DEBUG_FreeRegisteredTypedefs()
321 int count;
322 int j;
323 struct known_typedef * ktd;
324 struct known_typedef * next;
326 count = 0;
327 for(j=0; j < NR_STAB_HASH; j++ )
329 for(ktd = ktd_head[j]; ktd; ktd = next)
331 count++;
332 next = ktd->next;
333 free(ktd->name);
334 free(ktd);
336 ktd_head[j] = NULL;
339 return TRUE;
343 static
345 DEBUG_ParseTypedefStab(char * ptr, const char * typename)
347 int arrmax;
348 int arrmin;
349 char * c;
350 struct datatype * curr_type;
351 struct datatype * datatype;
352 struct datatype * curr_types[MAX_TD_NESTING];
353 char element_name[1024];
354 int ntypes = 0;
355 int offset;
356 const char * orig_typename;
357 int size;
358 char * tc;
359 char * tc2;
360 int typenum;
362 orig_typename = typename;
364 if( DEBUG_HandlePreviousTypedef(typename, ptr) )
365 return TRUE;
368 * Go from back to front. First we go through and figure out what
369 * type numbers we need, and register those types. Then we go in
370 * and fill the details.
373 for( c = strchr(ptr, '='); c != NULL; c = strchr(c + 1, '=') )
376 * Back up until we get to a non-numeric character. This is the type
377 * number.
379 typenum = DEBUG_ReadTypeEnumBackwards(c-1);
381 if( ntypes >= MAX_TD_NESTING )
384 * If this ever happens, just bump the counter.
386 fprintf(stderr, "Typedef nesting overflow\n");
387 return FALSE;
390 switch(c[1])
392 case '*':
393 stab_types[typenum] = DEBUG_NewDataType(POINTER, NULL);
394 curr_types[ntypes++] = stab_types[typenum];
395 break;
396 case 's':
397 case 'u':
398 stab_types[typenum] = DEBUG_NewDataType(STRUCT, typename);
399 curr_types[ntypes++] = stab_types[typenum];
400 break;
401 case 'a':
402 stab_types[typenum] = DEBUG_NewDataType(ARRAY, NULL);
403 curr_types[ntypes++] = stab_types[typenum];
404 break;
405 case '(':
406 case '1':
407 case 'r':
408 stab_types[typenum] = DEBUG_NewDataType(BASIC, typename);
409 curr_types[ntypes++] = stab_types[typenum];
410 break;
411 case 'x':
412 stab_strcpy(element_name, c + 3);
413 stab_types[typenum] = DEBUG_NewDataType(STRUCT, element_name);
414 curr_types[ntypes++] = stab_types[typenum];
415 break;
416 case 'e':
417 stab_types[typenum] = DEBUG_NewDataType(ENUM, NULL);
418 curr_types[ntypes++] = stab_types[typenum];
419 break;
420 case 'f':
421 stab_types[typenum] = DEBUG_NewDataType(FUNC, NULL);
422 curr_types[ntypes++] = stab_types[typenum];
423 break;
424 default:
425 fprintf(stderr, "Unknown type (%c).\n",c[1]);
427 typename = NULL;
431 * Now register the type so that if we encounter it again, we will know
432 * what to do.
434 DEBUG_RegisterTypedef(orig_typename, curr_types, ntypes);
437 * OK, now take a second sweep through. Now we will be digging
438 * out the definitions of the various components, and storing
439 * them in the skeletons that we have already allocated. We take
440 * a right-to left search as this is much easier to parse.
442 for( c = strrchr(ptr, '='); c != NULL; c = strrchr(ptr, '=') )
444 int typenum = DEBUG_ReadTypeEnumBackwards(c-1);
445 curr_type = stab_types[typenum];
447 switch(c[1])
449 case 'x':
450 tc = c + 3;
451 while( *tc != ':' )
452 tc++;
453 tc++;
454 if( *tc == '\0' )
455 *c = '\0';
456 else
457 strcpy(c, tc);
458 break;
459 case '*':
460 case 'f':
461 tc = c + 2;
462 datatype = stab_types[DEBUG_ReadTypeEnum(&tc)];
463 DEBUG_SetPointerType(curr_type, datatype);
464 if( *tc == '\0' )
465 *c = '\0';
466 else
467 strcpy(c, tc);
468 break;
469 case '(':
470 case '1':
471 case 'r':
473 * We have already handled these above.
475 *c = '\0';
476 break;
477 case 'a':
478 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */
480 tc = c + 3;
481 /* 'r' */
482 DEBUG_ReadTypeEnum(&tc);
483 tc++; /* ';' */
484 arrmin = strtol(tc, &tc, 10); /* <int> */
485 tc++; /* ';' */
486 arrmax = strtol(tc, &tc, 10); /* <int> */
487 tc++; /* ';' */
488 datatype = stab_types[DEBUG_ReadTypeEnum(&tc)]; /* <typeinfo> */
489 if( *tc == '\0' )
490 *c = '\0';
491 else
492 strcpy(c, tc);
493 DEBUG_SetArrayParams(curr_type, arrmin, arrmax, datatype);
494 break;
495 case 's':
496 case 'u': {
497 int failure = 0;
499 tc = c + 2;
500 if( DEBUG_SetStructSize(curr_type, strtol(tc, &tc, 10)) == FALSE )
503 * We have already filled out this structure. Nothing to do,
504 * so just skip forward to the end of the definition.
506 while( tc[0] != ';' && tc[1] != ';' )
507 tc++;
509 tc += 2;
511 if( *tc == '\0' )
512 *c = '\0';
513 else
514 strcpy(c, tc + 1);
515 continue;
519 * Now parse the individual elements of the structure/union.
521 while(*tc != ';')
523 char *ti;
524 tc2 = element_name;
525 while(*tc != ':')
526 *tc2++ = *tc++;
527 tc++;
528 *tc2++ = '\0';
529 ti=tc;
530 datatype = stab_types[DEBUG_ReadTypeEnum(&tc)];
531 *tc='\0';
532 tc++;
533 offset = strtol(tc, &tc, 10);
534 tc++;
535 size = strtol(tc, &tc, 10);
536 tc++;
537 if (datatype)
538 DEBUG_AddStructElement(curr_type, element_name, datatype, offset, size);
539 else {
540 failure = 1;
541 /* ... but proceed parsing to the end of the stab */
545 if (failure) {
546 /* if we had a undeclared value this one is undeclared too.
547 * remove it from the stab_types.
548 * I just set it to NULL to detect bugs in my thoughtprocess.
549 * FIXME: leaks the memory for the structure elements.
550 * FIXME: such structures should have been optimized away
551 * by ld.
553 stab_types[typenum] = NULL;
555 if( *tc == '\0' )
556 *c = '\0';
557 else
558 strcpy(c, tc + 1);
559 break;
561 case 'e':
562 tc = c + 2;
564 * Now parse the individual elements of the structure/union.
566 while(*tc != ';')
568 tc2 = element_name;
569 while(*tc != ':')
570 *tc2++ = *tc++;
571 tc++;
572 *tc2++ = '\0';
573 offset = strtol(tc, &tc, 10);
574 tc++;
575 DEBUG_AddStructElement(curr_type, element_name, NULL, offset, 0);
577 if( *tc == '\0' )
578 *c = '\0';
579 else
580 strcpy(c, tc + 1);
581 break;
582 default:
583 fprintf(stderr, "Unknown type (%c).\n",c[1]);
584 break;
588 return TRUE;
592 static struct datatype *
593 DEBUG_ParseStabType(const char * stab)
595 char * c;
598 * Look through the stab definition, and figure out what datatype
599 * this represents. If we have something we know about, assign the
600 * type.
602 c = strchr(stab, ':');
603 if( c == NULL )
604 return NULL;
606 c++;
608 * The next character says more about the type (i.e. data, function, etc)
609 * of symbol. Skip it.
611 c++;
613 * The next is either an integer or a (integer,integer).
614 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
616 return stab_types[DEBUG_ReadTypeEnum(&c)];
619 static
621 DEBUG_ParseStabs(char * addr, unsigned int load_offset,
622 unsigned int staboff, int stablen,
623 unsigned int strtaboff, int strtablen)
625 struct name_hash * curr_func = NULL;
626 struct wine_locals * curr_loc = NULL;
627 struct name_hash * curr_sym = NULL;
628 char currpath[PATH_MAX];
629 int i;
630 int ignore = FALSE;
631 int last_nso = -1;
632 int len;
633 DBG_ADDR new_addr;
634 int nstab;
635 char * ptr;
636 char * stabbuff;
637 int stabbufflen;
638 struct stab_nlist * stab_ptr;
639 char * strs;
640 int strtabinc;
641 char * subpath = NULL;
642 char symname[4096];
644 nstab = stablen / sizeof(struct stab_nlist);
645 stab_ptr = (struct stab_nlist *) (addr + staboff);
646 strs = (char *) (addr + strtaboff);
648 memset(currpath, 0, sizeof(currpath));
651 * Allocate a buffer into which we can build stab strings for cases
652 * where the stab is continued over multiple lines.
654 stabbufflen = 65536;
655 stabbuff = (char *) xmalloc(stabbufflen);
657 strtabinc = 0;
658 stabbuff[0] = '\0';
659 for(i=0; i < nstab; i++, stab_ptr++ )
661 ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
662 if( ptr[strlen(ptr) - 1] == '\\' )
665 * Indicates continuation. Append this to the buffer, and go onto the
666 * next record. Repeat the process until we find a stab without the
667 * '/' character, as this indicates we have the whole thing.
669 len = strlen(ptr);
670 if( strlen(stabbuff) + len > stabbufflen )
672 stabbufflen += 65536;
673 stabbuff = (char *) xrealloc(stabbuff, stabbufflen);
675 strncat(stabbuff, ptr, len - 1);
676 continue;
678 else if( stabbuff[0] != '\0' )
680 strcat( stabbuff, ptr);
681 ptr = stabbuff;
684 if( strchr(ptr, '=') != NULL )
687 * The stabs aren't in writable memory, so copy it over so we are
688 * sure we can scribble on it.
690 if( ptr != stabbuff )
692 strcpy(stabbuff, ptr);
693 ptr = stabbuff;
695 stab_strcpy(symname, ptr);
696 DEBUG_ParseTypedefStab(ptr, symname);
699 switch(stab_ptr->n_type)
701 case N_GSYM:
703 * These are useless with ELF. They have no value, and you have to
704 * read the normal symbol table to get the address. Thus we
705 * ignore them, and when we process the normal symbol table
706 * we should do the right thing.
708 * With a.out, they actually do make some amount of sense.
710 new_addr.seg = 0;
711 new_addr.type = DEBUG_ParseStabType(ptr);
712 new_addr.off = load_offset + stab_ptr->n_value;
714 stab_strcpy(symname, ptr);
715 #ifdef __ELF__
716 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
717 SYM_WINE | SYM_DATA | SYM_INVALID);
718 #else
719 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
720 SYM_WINE | SYM_DATA );
721 #endif
722 break;
723 case N_RBRAC:
724 case N_LBRAC:
726 * We need to keep track of these so we get symbol scoping
727 * right for local variables. For now, we just ignore them.
728 * The hooks are already there for dealing with this however,
729 * so all we need to do is to keep count of the nesting level,
730 * and find the RBRAC for each matching LBRAC.
732 break;
733 case N_LCSYM:
734 case N_STSYM:
736 * These are static symbols and BSS symbols.
738 new_addr.seg = 0;
739 new_addr.type = DEBUG_ParseStabType(ptr);
740 new_addr.off = load_offset + stab_ptr->n_value;
742 stab_strcpy(symname, ptr);
743 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
744 SYM_WINE | SYM_DATA );
745 break;
746 case N_PSYM:
748 * These are function parameters.
750 if( (curr_func != NULL)
751 && (stab_ptr->n_value != 0) )
753 stab_strcpy(symname, ptr);
754 curr_loc = DEBUG_AddLocal(curr_func, 0,
755 stab_ptr->n_value, 0, 0, symname);
756 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr));
758 break;
759 case N_RSYM:
760 if( curr_func != NULL )
762 stab_strcpy(symname, ptr);
763 curr_loc = DEBUG_AddLocal(curr_func, stab_ptr->n_value, 0, 0, 0, symname);
764 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr));
766 break;
767 case N_LSYM:
768 if( (curr_func != NULL)
769 && (stab_ptr->n_value != 0) )
771 stab_strcpy(symname, ptr);
772 DEBUG_AddLocal(curr_func, 0,
773 stab_ptr->n_value, 0, 0, symname);
775 else if (curr_func == NULL)
777 stab_strcpy(symname, ptr);
779 break;
780 case N_SLINE:
782 * This is a line number. These are always relative to the start
783 * of the function (N_FUN), and this makes the lookup easier.
785 if( curr_func != NULL )
787 #ifdef __ELF__
788 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
789 stab_ptr->n_value);
790 #else
791 #if 0
793 * This isn't right. The order of the stabs is different under
794 * a.out, and as a result we would end up attaching the line
795 * number to the wrong function.
797 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
798 stab_ptr->n_value - curr_func->addr.off);
799 #endif
800 #endif
802 break;
803 case N_FUN:
805 * First, clean up the previous function we were working on.
807 DEBUG_Normalize(curr_func);
810 * For now, just declare the various functions. Later
811 * on, we will add the line number information and the
812 * local symbols.
814 if( !ignore )
816 new_addr.seg = 0;
817 new_addr.type = DEBUG_ParseStabType(ptr);
818 new_addr.off = load_offset + stab_ptr->n_value;
820 * Copy the string to a temp buffer so we
821 * can kill everything after the ':'. We do
822 * it this way because otherwise we end up dirtying
823 * all of the pages related to the stabs, and that
824 * sucks up swap space like crazy.
826 stab_strcpy(symname, ptr);
827 curr_func = DEBUG_AddSymbol( symname, &new_addr, currpath,
828 SYM_WINE | SYM_FUNC);
830 else
833 * Don't add line number information for this function
834 * any more.
836 curr_func = NULL;
838 break;
839 case N_SO:
841 * This indicates a new source file. Append the records
842 * together, to build the correct path name.
844 #ifndef __ELF__
846 * With a.out, there is no NULL string N_SO entry at the end of
847 * the file. Thus when we find non-consecutive entries,
848 * we consider that a new file is started.
850 if( last_nso < i-1 )
852 currpath[0] = '\0';
853 DEBUG_Normalize(curr_func);
854 curr_func = NULL;
856 #endif
858 if( *ptr == '\0' )
861 * Nuke old path.
863 currpath[0] = '\0';
864 DEBUG_Normalize(curr_func);
865 curr_func = NULL;
867 * The datatypes that we would need to use are reset when
868 * we start a new file.
870 memset(stab_types, 0, num_stab_types * sizeof(stab_types[0]));
872 for (i=0;i<nrofnroftypenums;i++)
873 memset(typenums[i],0,sizeof(typenums[i][0])*nroftypenums[i]);
876 else
878 if (*ptr != '/')
879 strcat(currpath, ptr);
880 else
881 strcpy(currpath, ptr);
882 subpath = ptr;
884 last_nso = i;
885 break;
886 case N_SOL:
888 * This indicates we are including stuff from an include file.
889 * If this is the main source, enable the debug stuff, otherwise
890 * ignore it.
892 if( subpath == NULL || strcmp(ptr, subpath) == 0 )
894 ignore = FALSE;
896 else
898 ignore = TRUE;
899 DEBUG_Normalize(curr_func);
900 curr_func = NULL;
902 break;
903 case N_UNDF:
904 strs += strtabinc;
905 strtabinc = stab_ptr->n_value;
906 DEBUG_Normalize(curr_func);
907 curr_func = NULL;
908 break;
909 case N_OPT:
911 * Ignore this. We don't care what it points to.
913 break;
914 case N_BINCL:
915 case N_EINCL:
916 case N_MAIN:
918 * Always ignore these. GCC doesn't even generate them.
920 break;
921 default:
922 break;
925 stabbuff[0] = '\0';
927 #if 0
928 fprintf(stderr, "%d %x %s\n", stab_ptr->n_type,
929 (unsigned int) stab_ptr->n_value,
930 strs + (unsigned int) stab_ptr->n_un.n_name);
931 #endif
934 if( stab_types != NULL )
936 free(stab_types);
937 stab_types = NULL;
938 num_stab_types = 0;
942 DEBUG_FreeRegisteredTypedefs();
944 return TRUE;
947 #ifdef __ELF__
950 * Walk through the entire symbol table and add any symbols we find there.
951 * This can be used in cases where we have stripped ELF shared libraries,
952 * or it can be used in cases where we have data symbols for which the address
953 * isn't encoded in the stabs.
955 * This is all really quite easy, since we don't have to worry about line
956 * numbers or local data variables.
958 static
960 DEBUG_ProcessElfSymtab(char * addr, unsigned int load_offset,
961 Elf32_Shdr * symtab, Elf32_Shdr * strtab)
963 char * curfile = NULL;
964 struct name_hash * curr_sym = NULL;
965 int flags;
966 int i;
967 DBG_ADDR new_addr;
968 int nsym;
969 char * strp;
970 char * symname;
971 Elf32_Sym * symp;
974 symp = (Elf32_Sym *) (addr + symtab->sh_offset);
975 nsym = symtab->sh_size / sizeof(*symp);
976 strp = (char *) (addr + strtab->sh_offset);
978 for(i=0; i < nsym; i++, symp++)
981 * Ignore certain types of entries which really aren't of that much
982 * interest.
984 if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION )
986 continue;
989 symname = strp + symp->st_name;
992 * Save the name of the current file, so we have a way of tracking
993 * static functions/data.
995 if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
997 curfile = symname;
998 continue;
1003 * See if we already have something for this symbol.
1004 * If so, ignore this entry, because it would have come from the
1005 * stabs or from a previous symbol. If the value is different,
1006 * we will have to keep the darned thing, because there can be
1007 * multiple local symbols by the same name.
1009 if( (DEBUG_GetSymbolValue(symname, -1, &new_addr, FALSE ) == TRUE)
1010 && (new_addr.off == (load_offset + symp->st_value)) )
1011 continue;
1013 new_addr.seg = 0;
1014 new_addr.type = NULL;
1015 new_addr.off = load_offset + symp->st_value;
1016 flags = SYM_WINE | (ELF32_ST_BIND(symp->st_info) == STT_FUNC
1017 ? SYM_FUNC : SYM_DATA);
1018 if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
1019 curr_sym = DEBUG_AddSymbol( symname, &new_addr, NULL, flags );
1020 else
1021 curr_sym = DEBUG_AddSymbol( symname, &new_addr, curfile, flags );
1024 * Record the size of the symbol. This can come in handy in
1025 * some cases. Not really used yet, however.
1027 if( symp->st_size != 0 )
1028 DEBUG_SetSymbolSize(curr_sym, symp->st_size);
1031 return TRUE;
1034 static
1036 DEBUG_ProcessElfObject(char * filename, unsigned int load_offset)
1038 int rtn = FALSE;
1039 struct stat statbuf;
1040 int fd = -1;
1041 int status;
1042 char * addr = (char *) 0xffffffff;
1043 Elf32_Ehdr * ehptr;
1044 Elf32_Shdr * spnt;
1045 char * shstrtab;
1046 int nsect;
1047 int i;
1048 int stabsect;
1049 int stabstrsect;
1053 * Make sure we can stat and open this file.
1055 if( filename == NULL )
1056 goto leave;
1058 status = stat(filename, &statbuf);
1059 if( status == -1 )
1061 char *s,*t,*fn,*paths;
1062 if (strchr(filename,'/'))
1063 goto leave;
1064 paths = xstrdup(getenv("PATH"));
1065 s = paths;
1066 while (s && *s) {
1067 t = strchr(s,':');
1068 if (t) *t='\0';
1069 fn = (char*)xmalloc(strlen(filename)+1+strlen(s)+1);
1070 strcpy(fn,s);
1071 strcat(fn,"/");
1072 strcat(fn,filename);
1073 if ((rtn = DEBUG_ProcessElfObject(fn,load_offset))) {
1074 free(fn);
1075 free(paths);
1076 goto leave;
1078 free(fn);
1079 if (t) s = t+1; else break;
1081 if (!s || !*s) fprintf(stderr," not found");
1082 free(paths);
1083 goto leave;
1087 * Now open the file, so that we can mmap() it.
1089 fd = open(filename, O_RDONLY);
1090 if( fd == -1 )
1091 goto leave;
1095 * Now mmap() the file.
1097 addr = mmap(0, statbuf.st_size, PROT_READ,
1098 MAP_PRIVATE, fd, 0);
1099 if( addr == (char *) 0xffffffff )
1100 goto leave;
1103 * Next, we need to find a few of the internal ELF headers within
1104 * this thing. We need the main executable header, and the section
1105 * table.
1107 ehptr = (Elf32_Ehdr *) addr;
1109 if( load_offset == 0 )
1110 DEBUG_RegisterELFDebugInfo(ehptr->e_entry, statbuf.st_size, filename);
1111 else
1112 DEBUG_RegisterELFDebugInfo(load_offset, statbuf.st_size, filename);
1114 spnt = (Elf32_Shdr *) (addr + ehptr->e_shoff);
1115 nsect = ehptr->e_shnum;
1116 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1118 stabsect = stabstrsect = -1;
1120 for(i=0; i < nsect; i++)
1122 if( strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0 )
1123 stabsect = i;
1125 if( strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0 )
1126 stabstrsect = i;
1129 if( stabsect == -1 || stabstrsect == -1 )
1130 goto leave;
1133 * OK, now just parse all of the stabs.
1135 rtn = DEBUG_ParseStabs(addr, load_offset,
1136 spnt[stabsect].sh_offset,
1137 spnt[stabsect].sh_size,
1138 spnt[stabstrsect].sh_offset,
1139 spnt[stabstrsect].sh_size);
1141 if( rtn != TRUE )
1142 goto leave;
1144 for(i=0; i < nsect; i++)
1146 if( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1147 && (spnt[i].sh_type == SHT_SYMTAB) )
1148 DEBUG_ProcessElfSymtab(addr, load_offset,
1149 spnt + i, spnt + spnt[i].sh_link);
1151 if( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1152 && (spnt[i].sh_type == SHT_DYNSYM) )
1153 DEBUG_ProcessElfSymtab(addr, load_offset,
1154 spnt + i, spnt + spnt[i].sh_link);
1157 leave:
1159 if( addr != (char *) 0xffffffff )
1160 munmap(addr, statbuf.st_size);
1162 if( fd != -1 )
1163 close(fd);
1165 return (rtn);
1170 DEBUG_ReadExecutableDbgInfo(void)
1172 Elf32_Ehdr * ehdr;
1173 char * exe_name;
1174 Elf32_Dyn * dynpnt;
1175 struct r_debug * dbg_hdr;
1176 struct link_map * lpnt = NULL;
1177 extern Elf32_Dyn _DYNAMIC[];
1178 int rtn = FALSE;
1179 int rowcount;
1181 exe_name = DEBUG_argv0;
1184 * Make sure we can stat and open this file.
1186 if( exe_name == NULL )
1187 goto leave;
1189 fprintf( stderr, "Loading symbols: %s", exe_name );
1190 rowcount = 17 + strlen(exe_name);
1191 DEBUG_ProcessElfObject(exe_name, 0);
1194 * Finally walk the tables that the dynamic loader maintains to find all
1195 * of the other shared libraries which might be loaded. Perform the
1196 * same step for all of these.
1198 dynpnt = _DYNAMIC;
1199 if( dynpnt == NULL )
1200 goto leave;
1203 * Now walk the dynamic section (of the executable, looking for a DT_DEBUG
1204 * entry.
1206 for(; dynpnt->d_tag != DT_NULL; dynpnt++)
1207 if( dynpnt->d_tag == DT_DEBUG )
1208 break;
1210 if( (dynpnt->d_tag != DT_DEBUG)
1211 || (dynpnt->d_un.d_ptr == 0) )
1212 goto leave;
1215 * OK, now dig into the actual tables themselves.
1217 dbg_hdr = (struct r_debug *) dynpnt->d_un.d_ptr;
1218 lpnt = dbg_hdr->r_map;
1221 * Now walk the linked list. In all known ELF implementations,
1222 * the dynamic loader maintains this linked list for us. In some
1223 * cases the first entry doesn't appear with a name, in other cases it
1224 * does.
1226 for(; lpnt; lpnt = lpnt->l_next )
1229 * We already got the stuff for the executable using the
1230 * argv[0] entry above. Here we only need to concentrate on any
1231 * shared libraries which may be loaded.
1233 ehdr = (Elf32_Ehdr *) lpnt->l_addr;
1234 if( (lpnt->l_addr == 0) || (ehdr->e_type != ET_DYN) )
1235 continue;
1237 if( lpnt->l_name != NULL )
1239 if (rowcount + strlen(lpnt->l_name) > 76)
1241 fprintf( stderr, "\n " );
1242 rowcount = 3;
1244 fprintf( stderr, " %s", lpnt->l_name );
1245 rowcount += strlen(lpnt->l_name) + 1;
1246 DEBUG_ProcessElfObject(lpnt->l_name, lpnt->l_addr);
1250 rtn = TRUE;
1252 leave:
1253 fprintf( stderr, "\n" );
1254 return (rtn);
1258 #else /* !__ELF__ */
1260 #ifdef linux
1262 * a.out linux.
1265 DEBUG_ReadExecutableDbgInfo(void)
1267 char * addr = (char *) 0xffffffff;
1268 char * exe_name;
1269 struct exec * ahdr;
1270 int fd = -1;
1271 int rtn = FALSE;
1272 unsigned int staboff;
1273 struct stat statbuf;
1274 int status;
1275 unsigned int stroff;
1277 exe_name = DEBUG_argv0;
1280 * Make sure we can stat and open this file.
1282 if( exe_name == NULL )
1283 goto leave;
1285 status = stat(exe_name, &statbuf);
1286 if( status == -1 )
1287 goto leave;
1290 * Now open the file, so that we can mmap() it.
1292 fd = open(exe_name, O_RDONLY);
1293 if( fd == -1 )
1294 goto leave;
1298 * Now mmap() the file.
1300 addr = mmap(0, statbuf.st_size, PROT_READ,
1301 MAP_PRIVATE, fd, 0);
1302 if( addr == (char *) 0xffffffff )
1303 goto leave;
1305 ahdr = (struct exec *) addr;
1307 staboff = N_SYMOFF(*ahdr);
1308 stroff = N_STROFF(*ahdr);
1309 rtn = DEBUG_ParseStabs(addr, 0,
1310 staboff,
1311 ahdr->a_syms,
1312 stroff,
1313 statbuf.st_size - stroff);
1316 * Give a nice status message here...
1318 fprintf( stderr, "Loading symbols: %s", exe_name );
1320 rtn = TRUE;
1322 leave:
1324 if( addr != (char *) 0xffffffff )
1325 munmap(addr, statbuf.st_size);
1327 if( fd != -1 )
1328 close(fd);
1330 return (rtn);
1333 #else
1335 * Non-linux, non-ELF platforms.
1338 DEBUG_ReadExecutableDbgInfo(void)
1340 return FALSE;
1342 #endif
1344 #endif /* __ELF__ */