Changed initial process creation to avoid memory allocations.
[wine/multimedia.git] / debugger / stabs.c
blob966f8f0bd38854e8a5f554b1b9746c91c371e3fe
1 /*
2 * File stabs.c - read stabs information from the wine executable itself.
4 * Copyright (C) 1996, Eric Youngdale.
5 */
7 #include "config.h"
9 #include <sys/types.h>
10 #include <fcntl.h>
11 #include <sys/stat.h>
12 #include <sys/mman.h>
13 #include <limits.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #ifndef PATH_MAX
19 #define PATH_MAX _MAX_PATH
20 #endif
22 #include "debugger.h"
23 #include "xmalloc.h"
25 #ifdef __svr4__
26 #define __ELF__
27 #endif
29 #ifdef __ELF__
30 #ifdef HAVE_ELF_H
31 # include <elf.h>
32 #endif
33 #include <link.h>
34 #include <sys/mman.h>
35 #elif defined(__EMX__)
36 #include <a_out.h>
37 #else
38 #include <a.out.h>
39 #endif
41 #ifndef N_UNDF
42 #define N_UNDF 0x00
43 #endif
45 #define N_GSYM 0x20
46 #define N_FUN 0x24
47 #define N_STSYM 0x26
48 #define N_LCSYM 0x28
49 #define N_MAIN 0x2a
50 #define N_ROSYM 0x2c
51 #define N_OPT 0x3c
52 #define N_RSYM 0x40
53 #define N_SLINE 0x44
54 #define N_SO 0x64
55 #define N_LSYM 0x80
56 #define N_BINCL 0x82
57 #define N_SOL 0x84
58 #define N_PSYM 0xa0
59 #define N_EINCL 0xa2
60 #define N_LBRAC 0xc0
61 #define N_RBRAC 0xe0
65 * This is how we translate stab types into our internal representations
66 * of datatypes.
68 static struct datatype ** stab_types = NULL;
69 static int num_stab_types = 0;
72 * Set so that we know the main executable name and path.
74 char * DEBUG_argv0;
76 struct stab_nlist {
77 union {
78 char *n_name;
79 struct stab_nlist *n_next;
80 long n_strx;
81 } n_un;
82 unsigned char n_type;
83 char n_other;
84 short n_desc;
85 unsigned long n_value;
89 * This is used to keep track of known datatypes so that we don't redefine
90 * them over and over again. It sucks up lots of memory otherwise.
92 struct known_typedef
94 struct known_typedef * next;
95 char * name;
96 int ndefs;
97 struct datatype * types[0];
100 #define NR_STAB_HASH 521
102 struct known_typedef * ktd_head[NR_STAB_HASH] = {NULL,};
104 static unsigned int stab_hash( const char * name )
106 unsigned int hash = 0;
107 unsigned int tmp;
108 const char * p;
110 p = name;
112 while (*p)
114 hash = (hash << 4) + *p++;
116 if( (tmp = (hash & 0xf0000000)) )
118 hash ^= tmp >> 24;
120 hash &= ~tmp;
122 return hash % NR_STAB_HASH;
126 static void stab_strcpy(char * dest, const char * source)
129 * A strcpy routine that stops when we hit the ':' character.
130 * Faster than copying the whole thing, and then nuking the
131 * ':'.
133 while(*source != '\0' && *source != ':')
134 *dest++ = *source++;
135 *dest++ = '\0';
138 #define MAX_TD_NESTING 128
140 static int **typenums;
141 static int *nroftypenums=NULL;
142 static int nrofnroftypenums=0;
143 static int curtypenum = 0;
145 static
147 DEBUG_FileSubNr2StabEnum(int filenr,int subnr) {
148 if (nrofnroftypenums<=filenr) {
149 nroftypenums = xrealloc(nroftypenums,sizeof(nroftypenums[0])*(filenr+1));
150 memset(nroftypenums+nrofnroftypenums,0,(filenr+1-nrofnroftypenums)*sizeof(nroftypenums[0]));
151 typenums = xrealloc(typenums,sizeof(typenums[0])*(filenr+1));
152 memset(typenums+nrofnroftypenums,0,sizeof(typenums[0])*(filenr+1-nrofnroftypenums));
153 nrofnroftypenums=filenr+1;
155 if (nroftypenums[filenr]<=subnr) {
156 typenums[filenr] = xrealloc(typenums[filenr],sizeof(typenums[0][0])*(subnr+1));
157 memset(typenums[filenr]+nroftypenums[filenr],0,sizeof(typenums[0][0])*(subnr+1-nroftypenums[filenr]));
158 nroftypenums[filenr] = subnr+1;
160 if (!typenums[filenr][subnr])
161 typenums[filenr][subnr]=++curtypenum;
163 if( num_stab_types <= curtypenum ) {
164 num_stab_types = curtypenum + 256;
165 stab_types = (struct datatype **) xrealloc(stab_types,
166 num_stab_types * sizeof(struct datatype *)
168 memset( stab_types + curtypenum, 0, sizeof(struct datatype *) * (num_stab_types - curtypenum) );
170 /*fprintf(stderr,"(%d,%d) is %d\n",filenr,subnr,typenums[filenr][subnr]); */
171 return typenums[filenr][subnr];
174 static
176 DEBUG_ReadTypeEnumBackwards(char*x) {
177 int filenr,subnr;
179 if (*x==')') {
180 while (*x!='(')
181 x--;
182 x++; /* '(' */
183 filenr=strtol(x,&x,10); /* <int> */
184 x++; /* ',' */
185 subnr=strtol(x,&x,10); /* <int> */
186 x++; /* ')' */
187 } else {
188 while ((*x>='0') && (*x<='9'))
189 x--;
190 filenr = 0;
191 subnr = atol(x+1);
193 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
196 static
198 DEBUG_ReadTypeEnum(char **x) {
199 int filenr,subnr;
201 if (**x=='(') {
202 (*x)++; /* '(' */
203 filenr=strtol(*x,x,10); /* <int> */
204 (*x)++; /* ',' */
205 subnr=strtol(*x,x,10); /* <int> */
206 (*x)++; /* ')' */
207 } else {
208 filenr = 0;
209 subnr = strtol(*x,x,10); /* <int> */
211 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
214 static
216 DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef)
218 int hash;
219 struct known_typedef * ktd;
221 if( ndef == 1 )
222 return TRUE;
224 ktd = (struct known_typedef *) xmalloc(sizeof(struct known_typedef)
225 + ndef * sizeof(struct datatype *));
227 hash = stab_hash(name);
229 ktd->name = xstrdup(name);
230 ktd->ndefs = ndef;
231 memcpy(&ktd->types[0], types, ndef * sizeof(struct datatype *));
232 ktd->next = ktd_head[hash];
233 ktd_head[hash] = ktd;
235 return TRUE;
238 static
240 DEBUG_HandlePreviousTypedef(const char * name, const char * stab)
242 int count;
243 enum debug_type expect;
244 int hash;
245 struct known_typedef * ktd;
246 char * ptr;
248 hash = stab_hash(name);
250 for(ktd = ktd_head[hash]; ktd; ktd = ktd->next)
251 if ((ktd->name[0] == name[0]) && (strcmp(name, ktd->name) == 0) )
252 break;
255 * Didn't find it. This must be a new one.
257 if( ktd == NULL )
258 return FALSE;
261 * Examine the stab to make sure it has the same number of definitions.
263 count = 0;
264 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
266 if( count >= ktd->ndefs )
267 return FALSE;
270 * Make sure the types of all of the objects is consistent with
271 * what we have already parsed.
273 switch(ptr[1])
275 case '*':
276 expect = DT_POINTER;
277 break;
278 case 's':
279 case 'u':
280 expect = DT_STRUCT;
281 break;
282 case 'a':
283 expect = DT_ARRAY;
284 break;
285 case '1':
286 case '(':
287 case 'r':
288 expect = DT_BASIC;
289 break;
290 case 'x':
291 expect = DT_STRUCT;
292 break;
293 case 'e':
294 expect = DT_ENUM;
295 break;
296 case 'f':
297 expect = DT_FUNC;
298 break;
299 default:
300 fprintf(stderr, "Unknown type (%c).\n",ptr[1]);
301 return FALSE;
303 if( expect != DEBUG_GetType(ktd->types[count]) )
304 return FALSE;
305 count++;
308 if( ktd->ndefs != count )
309 return FALSE;
312 * Go through, dig out all of the type numbers, and substitute the
313 * appropriate things.
315 count = 0;
316 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
317 stab_types[DEBUG_ReadTypeEnumBackwards(ptr-1)] = ktd->types[count++];
319 return TRUE;
322 static int DEBUG_FreeRegisteredTypedefs()
324 int count;
325 int j;
326 struct known_typedef * ktd;
327 struct known_typedef * next;
329 count = 0;
330 for(j=0; j < NR_STAB_HASH; j++ )
332 for(ktd = ktd_head[j]; ktd; ktd = next)
334 count++;
335 next = ktd->next;
336 free(ktd->name);
337 free(ktd);
339 ktd_head[j] = NULL;
342 return TRUE;
346 static
348 DEBUG_ParseTypedefStab(char * ptr, const char * typename)
350 int arrmax;
351 int arrmin;
352 char * c;
353 struct datatype * curr_type;
354 struct datatype * datatype;
355 struct datatype * curr_types[MAX_TD_NESTING];
356 char element_name[1024];
357 int ntypes = 0;
358 int offset;
359 const char * orig_typename;
360 int size;
361 char * tc;
362 char * tc2;
363 int typenum;
365 orig_typename = typename;
367 if( DEBUG_HandlePreviousTypedef(typename, ptr) )
368 return TRUE;
371 * Go from back to front. First we go through and figure out what
372 * type numbers we need, and register those types. Then we go in
373 * and fill the details.
376 for( c = strchr(ptr, '='); c != NULL; c = strchr(c + 1, '=') )
379 * Back up until we get to a non-numeric character. This is the type
380 * number.
382 typenum = DEBUG_ReadTypeEnumBackwards(c-1);
384 if( ntypes >= MAX_TD_NESTING )
387 * If this ever happens, just bump the counter.
389 fprintf(stderr, "Typedef nesting overflow\n");
390 return FALSE;
393 switch(c[1])
395 case '*':
396 stab_types[typenum] = DEBUG_NewDataType(DT_POINTER, NULL);
397 curr_types[ntypes++] = stab_types[typenum];
398 break;
399 case 's':
400 case 'u':
401 stab_types[typenum] = DEBUG_NewDataType(DT_STRUCT, typename);
402 curr_types[ntypes++] = stab_types[typenum];
403 break;
404 case 'a':
405 stab_types[typenum] = DEBUG_NewDataType(DT_ARRAY, NULL);
406 curr_types[ntypes++] = stab_types[typenum];
407 break;
408 case '(':
409 case '1':
410 case 'r':
411 stab_types[typenum] = DEBUG_NewDataType(DT_BASIC, typename);
412 curr_types[ntypes++] = stab_types[typenum];
413 break;
414 case 'x':
415 stab_strcpy(element_name, c + 3);
416 stab_types[typenum] = DEBUG_NewDataType(DT_STRUCT, element_name);
417 curr_types[ntypes++] = stab_types[typenum];
418 break;
419 case 'e':
420 stab_types[typenum] = DEBUG_NewDataType(DT_ENUM, NULL);
421 curr_types[ntypes++] = stab_types[typenum];
422 break;
423 case 'f':
424 stab_types[typenum] = DEBUG_NewDataType(DT_FUNC, NULL);
425 curr_types[ntypes++] = stab_types[typenum];
426 break;
427 default:
428 fprintf(stderr, "Unknown type (%c).\n",c[1]);
430 typename = NULL;
434 * Now register the type so that if we encounter it again, we will know
435 * what to do.
437 DEBUG_RegisterTypedef(orig_typename, curr_types, ntypes);
440 * OK, now take a second sweep through. Now we will be digging
441 * out the definitions of the various components, and storing
442 * them in the skeletons that we have already allocated. We take
443 * a right-to left search as this is much easier to parse.
445 for( c = strrchr(ptr, '='); c != NULL; c = strrchr(ptr, '=') )
447 int typenum = DEBUG_ReadTypeEnumBackwards(c-1);
448 curr_type = stab_types[typenum];
450 switch(c[1])
452 case 'x':
453 tc = c + 3;
454 while( *tc != ':' )
455 tc++;
456 tc++;
457 if( *tc == '\0' )
458 *c = '\0';
459 else
460 strcpy(c, tc);
461 break;
462 case '*':
463 case 'f':
464 tc = c + 2;
465 datatype = stab_types[DEBUG_ReadTypeEnum(&tc)];
466 DEBUG_SetPointerType(curr_type, datatype);
467 if( *tc == '\0' )
468 *c = '\0';
469 else
470 strcpy(c, tc);
471 break;
472 case '(':
473 case '1':
474 case 'r':
476 * We have already handled these above.
478 *c = '\0';
479 break;
480 case 'a':
481 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */
483 tc = c + 3;
484 /* 'r' */
485 DEBUG_ReadTypeEnum(&tc);
486 tc++; /* ';' */
487 arrmin = strtol(tc, &tc, 10); /* <int> */
488 tc++; /* ';' */
489 arrmax = strtol(tc, &tc, 10); /* <int> */
490 tc++; /* ';' */
491 datatype = stab_types[DEBUG_ReadTypeEnum(&tc)]; /* <typeinfo> */
492 if( *tc == '\0' )
493 *c = '\0';
494 else
495 strcpy(c, tc);
496 DEBUG_SetArrayParams(curr_type, arrmin, arrmax, datatype);
497 break;
498 case 's':
499 case 'u': {
500 int failure = 0;
502 tc = c + 2;
503 if( DEBUG_SetStructSize(curr_type, strtol(tc, &tc, 10)) == FALSE )
506 * We have already filled out this structure. Nothing to do,
507 * so just skip forward to the end of the definition.
509 while( tc[0] != ';' && tc[1] != ';' )
510 tc++;
512 tc += 2;
514 if( *tc == '\0' )
515 *c = '\0';
516 else
517 strcpy(c, tc + 1);
518 continue;
522 * Now parse the individual elements of the structure/union.
524 while(*tc != ';')
526 char *ti;
527 tc2 = element_name;
528 while(*tc != ':')
529 *tc2++ = *tc++;
530 tc++;
531 *tc2++ = '\0';
532 ti=tc;
533 datatype = stab_types[DEBUG_ReadTypeEnum(&tc)];
534 *tc='\0';
535 tc++;
536 offset = strtol(tc, &tc, 10);
537 tc++;
538 size = strtol(tc, &tc, 10);
539 tc++;
540 if (datatype)
541 DEBUG_AddStructElement(curr_type, element_name, datatype, offset, size);
542 else {
543 failure = 1;
544 /* ... but proceed parsing to the end of the stab */
548 if (failure) {
549 /* if we had a undeclared value this one is undeclared too.
550 * remove it from the stab_types.
551 * I just set it to NULL to detect bugs in my thoughtprocess.
552 * FIXME: leaks the memory for the structure elements.
553 * FIXME: such structures should have been optimized away
554 * by ld.
556 stab_types[typenum] = NULL;
558 if( *tc == '\0' )
559 *c = '\0';
560 else
561 strcpy(c, tc + 1);
562 break;
564 case 'e':
565 tc = c + 2;
567 * Now parse the individual elements of the structure/union.
569 while(*tc != ';')
571 tc2 = element_name;
572 while(*tc != ':')
573 *tc2++ = *tc++;
574 tc++;
575 *tc2++ = '\0';
576 offset = strtol(tc, &tc, 10);
577 tc++;
578 DEBUG_AddStructElement(curr_type, element_name, NULL, offset, 0);
580 if( *tc == '\0' )
581 *c = '\0';
582 else
583 strcpy(c, tc + 1);
584 break;
585 default:
586 fprintf(stderr, "Unknown type (%c).\n",c[1]);
587 break;
591 return TRUE;
595 static struct datatype *
596 DEBUG_ParseStabType(const char * stab)
598 char * c;
601 * Look through the stab definition, and figure out what datatype
602 * this represents. If we have something we know about, assign the
603 * type.
605 c = strchr(stab, ':');
606 if( c == NULL )
607 return NULL;
609 c++;
611 * The next character says more about the type (i.e. data, function, etc)
612 * of symbol. Skip it.
614 c++;
616 * The next is either an integer or a (integer,integer).
617 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
619 return stab_types[DEBUG_ReadTypeEnum(&c)];
623 DEBUG_ParseStabs(char * addr, unsigned int load_offset,
624 unsigned int staboff, int stablen,
625 unsigned int strtaboff, int strtablen)
627 struct name_hash * curr_func = NULL;
628 struct wine_locals * curr_loc = NULL;
629 struct name_hash * curr_sym = NULL;
630 char currpath[PATH_MAX];
631 int i;
632 int ignore = FALSE;
633 int last_nso = -1;
634 int len;
635 DBG_ADDR new_addr;
636 int nstab;
637 char * ptr;
638 char * stabbuff;
639 int stabbufflen;
640 struct stab_nlist * stab_ptr;
641 char * strs;
642 int strtabinc;
643 char * subpath = NULL;
644 char symname[4096];
646 nstab = stablen / sizeof(struct stab_nlist);
647 stab_ptr = (struct stab_nlist *) (addr + staboff);
648 strs = (char *) (addr + strtaboff);
650 memset(currpath, 0, sizeof(currpath));
653 * Allocate a buffer into which we can build stab strings for cases
654 * where the stab is continued over multiple lines.
656 stabbufflen = 65536;
657 stabbuff = (char *) xmalloc(stabbufflen);
659 strtabinc = 0;
660 stabbuff[0] = '\0';
661 for(i=0; i < nstab; i++, stab_ptr++ )
663 ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
664 if( ptr[strlen(ptr) - 1] == '\\' )
667 * Indicates continuation. Append this to the buffer, and go onto the
668 * next record. Repeat the process until we find a stab without the
669 * '/' character, as this indicates we have the whole thing.
671 len = strlen(ptr);
672 if( strlen(stabbuff) + len > stabbufflen )
674 stabbufflen += 65536;
675 stabbuff = (char *) xrealloc(stabbuff, stabbufflen);
677 strncat(stabbuff, ptr, len - 1);
678 continue;
680 else if( stabbuff[0] != '\0' )
682 strcat( stabbuff, ptr);
683 ptr = stabbuff;
686 if( strchr(ptr, '=') != NULL )
689 * The stabs aren't in writable memory, so copy it over so we are
690 * sure we can scribble on it.
692 if( ptr != stabbuff )
694 strcpy(stabbuff, ptr);
695 ptr = stabbuff;
697 stab_strcpy(symname, ptr);
698 DEBUG_ParseTypedefStab(ptr, symname);
701 switch(stab_ptr->n_type)
703 case N_GSYM:
705 * These are useless with ELF. They have no value, and you have to
706 * read the normal symbol table to get the address. Thus we
707 * ignore them, and when we process the normal symbol table
708 * we should do the right thing.
710 * With a.out, they actually do make some amount of sense.
712 new_addr.seg = 0;
713 new_addr.type = DEBUG_ParseStabType(ptr);
714 new_addr.off = load_offset + stab_ptr->n_value;
716 stab_strcpy(symname, ptr);
717 #ifdef __ELF__
718 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
719 SYM_WINE | SYM_DATA | SYM_INVALID);
720 #else
721 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
722 SYM_WINE | SYM_DATA );
723 #endif
724 break;
725 case N_RBRAC:
726 case N_LBRAC:
728 * We need to keep track of these so we get symbol scoping
729 * right for local variables. For now, we just ignore them.
730 * The hooks are already there for dealing with this however,
731 * so all we need to do is to keep count of the nesting level,
732 * and find the RBRAC for each matching LBRAC.
734 break;
735 case N_LCSYM:
736 case N_STSYM:
738 * These are static symbols and BSS symbols.
740 new_addr.seg = 0;
741 new_addr.type = DEBUG_ParseStabType(ptr);
742 new_addr.off = load_offset + stab_ptr->n_value;
744 stab_strcpy(symname, ptr);
745 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
746 SYM_WINE | SYM_DATA );
747 break;
748 case N_PSYM:
750 * These are function parameters.
752 if( (curr_func != NULL)
753 && (stab_ptr->n_value != 0) )
755 stab_strcpy(symname, ptr);
756 curr_loc = DEBUG_AddLocal(curr_func, 0,
757 stab_ptr->n_value, 0, 0, symname);
758 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr));
760 break;
761 case N_RSYM:
762 if( curr_func != NULL )
764 stab_strcpy(symname, ptr);
765 curr_loc = DEBUG_AddLocal(curr_func, stab_ptr->n_value, 0, 0, 0, symname);
766 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr));
768 break;
769 case N_LSYM:
770 if( (curr_func != NULL)
771 && (stab_ptr->n_value != 0) )
773 stab_strcpy(symname, ptr);
774 DEBUG_AddLocal(curr_func, 0,
775 stab_ptr->n_value, 0, 0, symname);
777 else if (curr_func == NULL)
779 stab_strcpy(symname, ptr);
781 break;
782 case N_SLINE:
784 * This is a line number. These are always relative to the start
785 * of the function (N_FUN), and this makes the lookup easier.
787 if( curr_func != NULL )
789 #ifdef __ELF__
790 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
791 stab_ptr->n_value);
792 #else
793 #if 0
795 * This isn't right. The order of the stabs is different under
796 * a.out, and as a result we would end up attaching the line
797 * number to the wrong function.
799 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
800 stab_ptr->n_value - curr_func->addr.off);
801 #endif
802 #endif
804 break;
805 case N_FUN:
807 * First, clean up the previous function we were working on.
809 DEBUG_Normalize(curr_func);
812 * For now, just declare the various functions. Later
813 * on, we will add the line number information and the
814 * local symbols.
816 if( !ignore )
818 new_addr.seg = 0;
819 new_addr.type = DEBUG_ParseStabType(ptr);
820 new_addr.off = load_offset + stab_ptr->n_value;
822 * Copy the string to a temp buffer so we
823 * can kill everything after the ':'. We do
824 * it this way because otherwise we end up dirtying
825 * all of the pages related to the stabs, and that
826 * sucks up swap space like crazy.
828 stab_strcpy(symname, ptr);
829 curr_func = DEBUG_AddSymbol( symname, &new_addr, currpath,
830 SYM_WINE | SYM_FUNC);
832 else
835 * Don't add line number information for this function
836 * any more.
838 curr_func = NULL;
840 break;
841 case N_SO:
843 * This indicates a new source file. Append the records
844 * together, to build the correct path name.
846 #ifndef __ELF__
848 * With a.out, there is no NULL string N_SO entry at the end of
849 * the file. Thus when we find non-consecutive entries,
850 * we consider that a new file is started.
852 if( last_nso < i-1 )
854 currpath[0] = '\0';
855 DEBUG_Normalize(curr_func);
856 curr_func = NULL;
858 #endif
860 if( *ptr == '\0' )
863 * Nuke old path.
865 currpath[0] = '\0';
866 DEBUG_Normalize(curr_func);
867 curr_func = NULL;
869 * The datatypes that we would need to use are reset when
870 * we start a new file.
872 memset(stab_types, 0, num_stab_types * sizeof(stab_types[0]));
874 for (i=0;i<nrofnroftypenums;i++)
875 memset(typenums[i],0,sizeof(typenums[i][0])*nroftypenums[i]);
878 else
880 if (*ptr != '/')
881 strcat(currpath, ptr);
882 else
883 strcpy(currpath, ptr);
884 subpath = ptr;
886 last_nso = i;
887 break;
888 case N_SOL:
890 * This indicates we are including stuff from an include file.
891 * If this is the main source, enable the debug stuff, otherwise
892 * ignore it.
894 if( subpath == NULL || strcmp(ptr, subpath) == 0 )
896 ignore = FALSE;
898 else
900 ignore = TRUE;
901 DEBUG_Normalize(curr_func);
902 curr_func = NULL;
904 break;
905 case N_UNDF:
906 strs += strtabinc;
907 strtabinc = stab_ptr->n_value;
908 DEBUG_Normalize(curr_func);
909 curr_func = NULL;
910 break;
911 case N_OPT:
913 * Ignore this. We don't care what it points to.
915 break;
916 case N_BINCL:
917 case N_EINCL:
918 case N_MAIN:
920 * Always ignore these. GCC doesn't even generate them.
922 break;
923 default:
924 break;
927 stabbuff[0] = '\0';
929 #if 0
930 fprintf(stderr, "%d %x %s\n", stab_ptr->n_type,
931 (unsigned int) stab_ptr->n_value,
932 strs + (unsigned int) stab_ptr->n_un.n_name);
933 #endif
936 if( stab_types != NULL )
938 free(stab_types);
939 stab_types = NULL;
940 num_stab_types = 0;
944 DEBUG_FreeRegisteredTypedefs();
946 return TRUE;
949 #ifdef __ELF__
952 * Walk through the entire symbol table and add any symbols we find there.
953 * This can be used in cases where we have stripped ELF shared libraries,
954 * or it can be used in cases where we have data symbols for which the address
955 * isn't encoded in the stabs.
957 * This is all really quite easy, since we don't have to worry about line
958 * numbers or local data variables.
960 static
962 DEBUG_ProcessElfSymtab(char * addr, unsigned int load_offset,
963 Elf32_Shdr * symtab, Elf32_Shdr * strtab)
965 char * curfile = NULL;
966 struct name_hash * curr_sym = NULL;
967 int flags;
968 int i;
969 DBG_ADDR new_addr;
970 int nsym;
971 char * strp;
972 char * symname;
973 Elf32_Sym * symp;
976 symp = (Elf32_Sym *) (addr + symtab->sh_offset);
977 nsym = symtab->sh_size / sizeof(*symp);
978 strp = (char *) (addr + strtab->sh_offset);
980 for(i=0; i < nsym; i++, symp++)
983 * Ignore certain types of entries which really aren't of that much
984 * interest.
986 if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION )
988 continue;
991 symname = strp + symp->st_name;
994 * Save the name of the current file, so we have a way of tracking
995 * static functions/data.
997 if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
999 curfile = symname;
1000 continue;
1005 * See if we already have something for this symbol.
1006 * If so, ignore this entry, because it would have come from the
1007 * stabs or from a previous symbol. If the value is different,
1008 * we will have to keep the darned thing, because there can be
1009 * multiple local symbols by the same name.
1011 if( (DEBUG_GetSymbolValue(symname, -1, &new_addr, FALSE ) == TRUE)
1012 && (new_addr.off == (load_offset + symp->st_value)) )
1013 continue;
1015 new_addr.seg = 0;
1016 new_addr.type = NULL;
1017 new_addr.off = load_offset + symp->st_value;
1018 flags = SYM_WINE | (ELF32_ST_BIND(symp->st_info) == STT_FUNC
1019 ? SYM_FUNC : SYM_DATA);
1020 if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
1021 curr_sym = DEBUG_AddSymbol( symname, &new_addr, NULL, flags );
1022 else
1023 curr_sym = DEBUG_AddSymbol( symname, &new_addr, curfile, flags );
1026 * Record the size of the symbol. This can come in handy in
1027 * some cases. Not really used yet, however.
1029 if( symp->st_size != 0 )
1030 DEBUG_SetSymbolSize(curr_sym, symp->st_size);
1033 return TRUE;
1036 static
1038 DEBUG_ProcessElfObject(char * filename, unsigned int load_offset)
1040 int rtn = FALSE;
1041 struct stat statbuf;
1042 int fd = -1;
1043 int status;
1044 char * addr = (char *) 0xffffffff;
1045 Elf32_Ehdr * ehptr;
1046 Elf32_Shdr * spnt;
1047 char * shstrtab;
1048 int nsect;
1049 int i;
1050 int stabsect;
1051 int stabstrsect;
1055 * Make sure we can stat and open this file.
1057 if( filename == NULL )
1058 goto leave;
1060 status = stat(filename, &statbuf);
1061 if( status == -1 )
1063 char *s,*t,*fn,*paths;
1064 if (strchr(filename,'/'))
1065 goto leave;
1066 paths = xstrdup(getenv("PATH"));
1067 s = paths;
1068 while (s && *s) {
1069 t = strchr(s,':');
1070 if (t) *t='\0';
1071 fn = (char*)xmalloc(strlen(filename)+1+strlen(s)+1);
1072 strcpy(fn,s);
1073 strcat(fn,"/");
1074 strcat(fn,filename);
1075 if ((rtn = DEBUG_ProcessElfObject(fn,load_offset))) {
1076 free(fn);
1077 free(paths);
1078 goto leave;
1080 free(fn);
1081 if (t) s = t+1; else break;
1083 if (!s || !*s) fprintf(stderr," not found");
1084 free(paths);
1085 goto leave;
1089 * Now open the file, so that we can mmap() it.
1091 fd = open(filename, O_RDONLY);
1092 if( fd == -1 )
1093 goto leave;
1097 * Now mmap() the file.
1099 addr = mmap(0, statbuf.st_size, PROT_READ,
1100 MAP_PRIVATE, fd, 0);
1101 if( addr == (char *) 0xffffffff )
1102 goto leave;
1105 * Next, we need to find a few of the internal ELF headers within
1106 * this thing. We need the main executable header, and the section
1107 * table.
1109 ehptr = (Elf32_Ehdr *) addr;
1111 if( load_offset == 0 )
1112 DEBUG_RegisterELFDebugInfo(ehptr->e_entry, statbuf.st_size, filename);
1113 else
1114 DEBUG_RegisterELFDebugInfo(load_offset, statbuf.st_size, filename);
1116 spnt = (Elf32_Shdr *) (addr + ehptr->e_shoff);
1117 nsect = ehptr->e_shnum;
1118 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1120 stabsect = stabstrsect = -1;
1122 for(i=0; i < nsect; i++)
1124 if( strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0 )
1125 stabsect = i;
1127 if( strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0 )
1128 stabstrsect = i;
1131 if( stabsect == -1 || stabstrsect == -1 )
1132 goto leave;
1135 * OK, now just parse all of the stabs.
1137 rtn = DEBUG_ParseStabs(addr, load_offset,
1138 spnt[stabsect].sh_offset,
1139 spnt[stabsect].sh_size,
1140 spnt[stabstrsect].sh_offset,
1141 spnt[stabstrsect].sh_size);
1143 if( rtn != TRUE )
1144 goto leave;
1146 for(i=0; i < nsect; i++)
1148 if( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1149 && (spnt[i].sh_type == SHT_SYMTAB) )
1150 DEBUG_ProcessElfSymtab(addr, load_offset,
1151 spnt + i, spnt + spnt[i].sh_link);
1153 if( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1154 && (spnt[i].sh_type == SHT_DYNSYM) )
1155 DEBUG_ProcessElfSymtab(addr, load_offset,
1156 spnt + i, spnt + spnt[i].sh_link);
1159 leave:
1161 if( addr != (char *) 0xffffffff )
1162 munmap(addr, statbuf.st_size);
1164 if( fd != -1 )
1165 close(fd);
1167 return (rtn);
1172 DEBUG_ReadExecutableDbgInfo(void)
1174 Elf32_Ehdr * ehdr;
1175 char * exe_name;
1176 Elf32_Dyn * dynpnt;
1177 struct r_debug * dbg_hdr;
1178 struct link_map * lpnt = NULL;
1179 extern Elf32_Dyn _DYNAMIC[];
1180 int rtn = FALSE;
1181 int rowcount;
1183 exe_name = DEBUG_argv0;
1186 * Make sure we can stat and open this file.
1188 if( exe_name == NULL )
1189 goto leave;
1191 fprintf( stderr, "Loading symbols: %s", exe_name );
1192 rowcount = 17 + strlen(exe_name);
1193 DEBUG_ProcessElfObject(exe_name, 0);
1196 * Finally walk the tables that the dynamic loader maintains to find all
1197 * of the other shared libraries which might be loaded. Perform the
1198 * same step for all of these.
1200 dynpnt = _DYNAMIC;
1201 if( dynpnt == NULL )
1202 goto leave;
1205 * Now walk the dynamic section (of the executable, looking for a DT_DEBUG
1206 * entry.
1208 for(; dynpnt->d_tag != DT_NULL; dynpnt++)
1209 if( dynpnt->d_tag == DT_DEBUG )
1210 break;
1212 if( (dynpnt->d_tag != DT_DEBUG)
1213 || (dynpnt->d_un.d_ptr == 0) )
1214 goto leave;
1217 * OK, now dig into the actual tables themselves.
1219 dbg_hdr = (struct r_debug *) dynpnt->d_un.d_ptr;
1220 lpnt = dbg_hdr->r_map;
1223 * Now walk the linked list. In all known ELF implementations,
1224 * the dynamic loader maintains this linked list for us. In some
1225 * cases the first entry doesn't appear with a name, in other cases it
1226 * does.
1228 for(; lpnt; lpnt = lpnt->l_next )
1231 * We already got the stuff for the executable using the
1232 * argv[0] entry above. Here we only need to concentrate on any
1233 * shared libraries which may be loaded.
1235 ehdr = (Elf32_Ehdr *) lpnt->l_addr;
1236 if( (lpnt->l_addr == 0) || (ehdr->e_type != ET_DYN) )
1237 continue;
1239 if( lpnt->l_name != NULL )
1241 if (rowcount + strlen(lpnt->l_name) > 76)
1243 fprintf( stderr, "\n " );
1244 rowcount = 3;
1246 fprintf( stderr, " %s", lpnt->l_name );
1247 rowcount += strlen(lpnt->l_name) + 1;
1248 DEBUG_ProcessElfObject(lpnt->l_name, lpnt->l_addr);
1252 rtn = TRUE;
1254 leave:
1255 fprintf( stderr, "\n" );
1256 return (rtn);
1260 #else /* !__ELF__ */
1262 #ifdef linux
1264 * a.out linux.
1267 DEBUG_ReadExecutableDbgInfo(void)
1269 char * addr = (char *) 0xffffffff;
1270 char * exe_name;
1271 struct exec * ahdr;
1272 int fd = -1;
1273 int rtn = FALSE;
1274 unsigned int staboff;
1275 struct stat statbuf;
1276 int status;
1277 unsigned int stroff;
1279 exe_name = DEBUG_argv0;
1282 * Make sure we can stat and open this file.
1284 if( exe_name == NULL )
1285 goto leave;
1287 status = stat(exe_name, &statbuf);
1288 if( status == -1 )
1289 goto leave;
1292 * Now open the file, so that we can mmap() it.
1294 fd = open(exe_name, O_RDONLY);
1295 if( fd == -1 )
1296 goto leave;
1300 * Now mmap() the file.
1302 addr = mmap(0, statbuf.st_size, PROT_READ,
1303 MAP_PRIVATE, fd, 0);
1304 if( addr == (char *) 0xffffffff )
1305 goto leave;
1307 ahdr = (struct exec *) addr;
1309 staboff = N_SYMOFF(*ahdr);
1310 stroff = N_STROFF(*ahdr);
1311 rtn = DEBUG_ParseStabs(addr, 0,
1312 staboff,
1313 ahdr->a_syms,
1314 stroff,
1315 statbuf.st_size - stroff);
1318 * Give a nice status message here...
1320 fprintf( stderr, "Loading symbols: %s", exe_name );
1322 rtn = TRUE;
1324 leave:
1326 if( addr != (char *) 0xffffffff )
1327 munmap(addr, statbuf.st_size);
1329 if( fd != -1 )
1330 close(fd);
1332 return (rtn);
1335 #else
1337 * Non-linux, non-ELF platforms.
1340 DEBUG_ReadExecutableDbgInfo(void)
1342 return FALSE;
1344 #endif
1346 #endif /* __ELF__ */