Implemented the PSH_USEICONID/PSH_USEHICON and the PSP_USETITLE
[wine.git] / debugger / stabs.c
blob02b3bf9f37da69618e415ddfaf6afb7378a5e08a
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 #ifdef HAVE_SYS_MMAN_H
13 #include <sys/mman.h>
14 #endif
15 #include <limits.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
20 #ifndef PATH_MAX
21 #define PATH_MAX _MAX_PATH
22 #endif
24 #include "debugger.h"
26 #if defined(__svr4__) || defined(__sun)
27 #define __ELF__
28 #endif
30 #ifdef __ELF__
31 #ifdef HAVE_ELF_H
32 # include <elf.h>
33 #endif
34 #include <link.h>
35 #ifdef HAVE_SYS_MMAN_H
36 #include <sys/mman.h>
37 #endif
38 #elif defined(__EMX__)
39 #include <a_out.h>
40 #else
41 #include <a.out.h>
42 #endif
44 #ifndef N_UNDF
45 #define N_UNDF 0x00
46 #endif
48 #define N_GSYM 0x20
49 #define N_FUN 0x24
50 #define N_STSYM 0x26
51 #define N_LCSYM 0x28
52 #define N_MAIN 0x2a
53 #define N_ROSYM 0x2c
54 #define N_OPT 0x3c
55 #define N_RSYM 0x40
56 #define N_SLINE 0x44
57 #define N_SO 0x64
58 #define N_LSYM 0x80
59 #define N_BINCL 0x82
60 #define N_SOL 0x84
61 #define N_PSYM 0xa0
62 #define N_EINCL 0xa2
63 #define N_LBRAC 0xc0
64 #define N_RBRAC 0xe0
68 * This is how we translate stab types into our internal representations
69 * of datatypes.
71 static struct datatype ** stab_types = NULL;
72 static int num_stab_types = 0;
75 * Set so that we know the main executable name and path.
77 char * DEBUG_argv0;
79 struct stab_nlist {
80 union {
81 char *n_name;
82 struct stab_nlist *n_next;
83 long n_strx;
84 } n_un;
85 unsigned char n_type;
86 char n_other;
87 short n_desc;
88 unsigned long n_value;
92 * This is used to keep track of known datatypes so that we don't redefine
93 * them over and over again. It sucks up lots of memory otherwise.
95 struct known_typedef
97 struct known_typedef * next;
98 char * name;
99 int ndefs;
100 struct datatype * types[1];
103 #define NR_STAB_HASH 521
105 struct known_typedef * ktd_head[NR_STAB_HASH] = {NULL,};
107 static unsigned int stab_hash( const char * name )
109 unsigned int hash = 0;
110 unsigned int tmp;
111 const char * p;
113 p = name;
115 while (*p)
117 hash = (hash << 4) + *p++;
119 if( (tmp = (hash & 0xf0000000)) )
121 hash ^= tmp >> 24;
123 hash &= ~tmp;
125 return hash % NR_STAB_HASH;
129 static void stab_strcpy(char * dest, const char * source)
132 * A strcpy routine that stops when we hit the ':' character.
133 * Faster than copying the whole thing, and then nuking the
134 * ':'.
136 while(*source != '\0' && *source != ':')
137 *dest++ = *source++;
138 *dest++ = '\0';
141 #define MAX_TD_NESTING 128
143 static int **typenums;
144 static int *nroftypenums=NULL;
145 static int nrofnroftypenums=0;
146 static int curtypenum = 0;
148 static
150 DEBUG_FileSubNr2StabEnum(int filenr,int subnr) {
151 if (nrofnroftypenums<=filenr) {
152 nroftypenums = DBG_realloc(nroftypenums,sizeof(nroftypenums[0])*(filenr+1));
153 memset(nroftypenums+nrofnroftypenums,0,(filenr+1-nrofnroftypenums)*sizeof(nroftypenums[0]));
154 typenums = DBG_realloc(typenums,sizeof(typenums[0])*(filenr+1));
155 memset(typenums+nrofnroftypenums,0,sizeof(typenums[0])*(filenr+1-nrofnroftypenums));
156 nrofnroftypenums=filenr+1;
158 if (nroftypenums[filenr]<=subnr) {
159 typenums[filenr] = DBG_realloc(typenums[filenr],sizeof(typenums[0][0])*(subnr+1));
160 memset(typenums[filenr]+nroftypenums[filenr],0,sizeof(typenums[0][0])*(subnr+1-nroftypenums[filenr]));
161 nroftypenums[filenr] = subnr+1;
163 if (!typenums[filenr][subnr])
164 typenums[filenr][subnr]=++curtypenum;
166 if( num_stab_types <= curtypenum ) {
167 num_stab_types = curtypenum + 256;
168 stab_types = (struct datatype **) DBG_realloc(stab_types,
169 num_stab_types * sizeof(struct datatype *)
171 memset( stab_types + curtypenum, 0, sizeof(struct datatype *) * (num_stab_types - curtypenum) );
173 /*fprintf(stderr,"(%d,%d) is %d\n",filenr,subnr,typenums[filenr][subnr]); */
174 return typenums[filenr][subnr];
177 static
179 DEBUG_ReadTypeEnumBackwards(char*x) {
180 int filenr,subnr;
182 if (*x==')') {
183 while (*x!='(')
184 x--;
185 x++; /* '(' */
186 filenr=strtol(x,&x,10); /* <int> */
187 x++; /* ',' */
188 subnr=strtol(x,&x,10); /* <int> */
189 x++; /* ')' */
190 } else {
191 while ((*x>='0') && (*x<='9'))
192 x--;
193 filenr = 0;
194 subnr = atol(x+1);
196 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
199 static
201 DEBUG_ReadTypeEnum(char **x) {
202 int filenr,subnr;
204 if (**x=='(') {
205 (*x)++; /* '(' */
206 filenr=strtol(*x,x,10); /* <int> */
207 (*x)++; /* ',' */
208 subnr=strtol(*x,x,10); /* <int> */
209 (*x)++; /* ')' */
210 } else {
211 filenr = 0;
212 subnr = strtol(*x,x,10); /* <int> */
214 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
217 static
219 DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef)
221 int hash;
222 struct known_typedef * ktd;
224 if( ndef == 1 )
225 return TRUE;
227 ktd = (struct known_typedef *) DBG_alloc(sizeof(struct known_typedef)
228 + (ndef - 1) * sizeof(struct datatype *));
230 hash = stab_hash(name);
232 ktd->name = DBG_strdup(name);
233 ktd->ndefs = ndef;
234 memcpy(&ktd->types[0], types, ndef * sizeof(struct datatype *));
235 ktd->next = ktd_head[hash];
236 ktd_head[hash] = ktd;
238 return TRUE;
241 static
243 DEBUG_HandlePreviousTypedef(const char * name, const char * stab)
245 int count;
246 enum debug_type expect;
247 int hash;
248 struct known_typedef * ktd;
249 char * ptr;
251 hash = stab_hash(name);
253 for(ktd = ktd_head[hash]; ktd; ktd = ktd->next)
254 if ((ktd->name[0] == name[0]) && (strcmp(name, ktd->name) == 0) )
255 break;
258 * Didn't find it. This must be a new one.
260 if( ktd == NULL )
261 return FALSE;
264 * Examine the stab to make sure it has the same number of definitions.
266 count = 0;
267 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
269 if( count >= ktd->ndefs )
270 return FALSE;
273 * Make sure the types of all of the objects is consistent with
274 * what we have already parsed.
276 switch(ptr[1])
278 case '*':
279 expect = DT_POINTER;
280 break;
281 case 's':
282 case 'u':
283 expect = DT_STRUCT;
284 break;
285 case 'a':
286 expect = DT_ARRAY;
287 break;
288 case '1':
289 case '(':
290 case 'r':
291 expect = DT_BASIC;
292 break;
293 case 'x':
294 expect = DT_STRUCT;
295 break;
296 case 'e':
297 expect = DT_ENUM;
298 break;
299 case 'f':
300 expect = DT_FUNC;
301 break;
302 default:
303 fprintf(stderr, "Unknown type (%c).\n",ptr[1]);
304 return FALSE;
306 if( expect != DEBUG_GetType(ktd->types[count]) )
307 return FALSE;
308 count++;
311 if( ktd->ndefs != count )
312 return FALSE;
315 * Go through, dig out all of the type numbers, and substitute the
316 * appropriate things.
318 count = 0;
319 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
320 stab_types[DEBUG_ReadTypeEnumBackwards(ptr-1)] = ktd->types[count++];
322 return TRUE;
325 static int DEBUG_FreeRegisteredTypedefs()
327 int count;
328 int j;
329 struct known_typedef * ktd;
330 struct known_typedef * next;
332 count = 0;
333 for(j=0; j < NR_STAB_HASH; j++ )
335 for(ktd = ktd_head[j]; ktd; ktd = next)
337 count++;
338 next = ktd->next;
339 DBG_free(ktd->name);
340 DBG_free(ktd);
342 ktd_head[j] = NULL;
345 return TRUE;
349 static
351 DEBUG_ParseTypedefStab(char * ptr, const char * typename)
353 int arrmax;
354 int arrmin;
355 char * c;
356 struct datatype * curr_type;
357 struct datatype * datatype;
358 struct datatype * curr_types[MAX_TD_NESTING];
359 char element_name[1024];
360 int ntypes = 0;
361 int offset;
362 const char * orig_typename;
363 int size;
364 char * tc;
365 char * tc2;
366 int typenum;
368 orig_typename = typename;
370 if( DEBUG_HandlePreviousTypedef(typename, ptr) )
371 return TRUE;
374 * Go from back to front. First we go through and figure out what
375 * type numbers we need, and register those types. Then we go in
376 * and fill the details.
379 for( c = strchr(ptr, '='); c != NULL; c = strchr(c + 1, '=') )
382 * Back up until we get to a non-numeric character. This is the type
383 * number.
385 typenum = DEBUG_ReadTypeEnumBackwards(c-1);
387 if( ntypes >= MAX_TD_NESTING )
390 * If this ever happens, just bump the counter.
392 fprintf(stderr, "Typedef nesting overflow\n");
393 return FALSE;
396 switch(c[1])
398 case '*':
399 stab_types[typenum] = DEBUG_NewDataType(DT_POINTER, NULL);
400 curr_types[ntypes++] = stab_types[typenum];
401 break;
402 case 's':
403 case 'u':
404 stab_types[typenum] = DEBUG_NewDataType(DT_STRUCT, typename);
405 curr_types[ntypes++] = stab_types[typenum];
406 break;
407 case 'a':
408 stab_types[typenum] = DEBUG_NewDataType(DT_ARRAY, NULL);
409 curr_types[ntypes++] = stab_types[typenum];
410 break;
411 case '(':
412 case '1':
413 case 'r':
414 stab_types[typenum] = DEBUG_NewDataType(DT_BASIC, typename);
415 curr_types[ntypes++] = stab_types[typenum];
416 break;
417 case 'x':
418 stab_strcpy(element_name, c + 3);
419 stab_types[typenum] = DEBUG_NewDataType(DT_STRUCT, element_name);
420 curr_types[ntypes++] = stab_types[typenum];
421 break;
422 case 'e':
423 stab_types[typenum] = DEBUG_NewDataType(DT_ENUM, NULL);
424 curr_types[ntypes++] = stab_types[typenum];
425 break;
426 case 'f':
427 stab_types[typenum] = DEBUG_NewDataType(DT_FUNC, NULL);
428 curr_types[ntypes++] = stab_types[typenum];
429 break;
430 default:
431 fprintf(stderr, "Unknown type (%c).\n",c[1]);
433 typename = NULL;
437 * Now register the type so that if we encounter it again, we will know
438 * what to do.
440 DEBUG_RegisterTypedef(orig_typename, curr_types, ntypes);
443 * OK, now take a second sweep through. Now we will be digging
444 * out the definitions of the various components, and storing
445 * them in the skeletons that we have already allocated. We take
446 * a right-to left search as this is much easier to parse.
448 for( c = strrchr(ptr, '='); c != NULL; c = strrchr(ptr, '=') )
450 int typenum = DEBUG_ReadTypeEnumBackwards(c-1);
451 curr_type = stab_types[typenum];
453 switch(c[1])
455 case 'x':
456 tc = c + 3;
457 while( *tc != ':' )
458 tc++;
459 tc++;
460 if( *tc == '\0' )
461 *c = '\0';
462 else
463 strcpy(c, tc);
464 break;
465 case '*':
466 case 'f':
467 tc = c + 2;
468 datatype = stab_types[DEBUG_ReadTypeEnum(&tc)];
469 DEBUG_SetPointerType(curr_type, datatype);
470 if( *tc == '\0' )
471 *c = '\0';
472 else
473 strcpy(c, tc);
474 break;
475 case '(':
476 case '1':
477 case 'r':
479 * We have already handled these above.
481 *c = '\0';
482 break;
483 case 'a':
484 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */
486 tc = c + 3;
487 /* 'r' */
488 DEBUG_ReadTypeEnum(&tc);
489 tc++; /* ';' */
490 arrmin = strtol(tc, &tc, 10); /* <int> */
491 tc++; /* ';' */
492 arrmax = strtol(tc, &tc, 10); /* <int> */
493 tc++; /* ';' */
494 datatype = stab_types[DEBUG_ReadTypeEnum(&tc)]; /* <typeinfo> */
495 if( *tc == '\0' )
496 *c = '\0';
497 else
498 strcpy(c, tc);
499 DEBUG_SetArrayParams(curr_type, arrmin, arrmax, datatype);
500 break;
501 case 's':
502 case 'u': {
503 int failure = 0;
505 tc = c + 2;
506 if( DEBUG_SetStructSize(curr_type, strtol(tc, &tc, 10)) == FALSE )
509 * We have already filled out this structure. Nothing to do,
510 * so just skip forward to the end of the definition.
512 while( tc[0] != ';' && tc[1] != ';' )
513 tc++;
515 tc += 2;
517 if( *tc == '\0' )
518 *c = '\0';
519 else
520 strcpy(c, tc + 1);
521 continue;
525 * Now parse the individual elements of the structure/union.
527 while(*tc != ';')
529 char *ti;
530 tc2 = element_name;
531 while(*tc != ':')
532 *tc2++ = *tc++;
533 tc++;
534 *tc2++ = '\0';
535 ti=tc;
536 datatype = stab_types[DEBUG_ReadTypeEnum(&tc)];
537 *tc='\0';
538 tc++;
539 offset = strtol(tc, &tc, 10);
540 tc++;
541 size = strtol(tc, &tc, 10);
542 tc++;
543 if (datatype)
544 DEBUG_AddStructElement(curr_type, element_name, datatype, offset, size);
545 else {
546 failure = 1;
547 /* ... but proceed parsing to the end of the stab */
551 if (failure) {
552 /* if we had a undeclared value this one is undeclared too.
553 * remove it from the stab_types.
554 * I just set it to NULL to detect bugs in my thoughtprocess.
555 * FIXME: leaks the memory for the structure elements.
556 * FIXME: such structures should have been optimized away
557 * by ld.
559 stab_types[typenum] = NULL;
561 if( *tc == '\0' )
562 *c = '\0';
563 else
564 strcpy(c, tc + 1);
565 break;
567 case 'e':
568 tc = c + 2;
570 * Now parse the individual elements of the structure/union.
572 while(*tc != ';')
574 tc2 = element_name;
575 while(*tc != ':')
576 *tc2++ = *tc++;
577 tc++;
578 *tc2++ = '\0';
579 offset = strtol(tc, &tc, 10);
580 tc++;
581 DEBUG_AddStructElement(curr_type, element_name, NULL, offset, 0);
583 if( *tc == '\0' )
584 *c = '\0';
585 else
586 strcpy(c, tc + 1);
587 break;
588 default:
589 fprintf(stderr, "Unknown type (%c).\n",c[1]);
590 break;
594 return TRUE;
598 static struct datatype *
599 DEBUG_ParseStabType(const char * stab)
601 char * c;
604 * Look through the stab definition, and figure out what datatype
605 * this represents. If we have something we know about, assign the
606 * type.
608 c = strchr(stab, ':');
609 if( c == NULL )
610 return NULL;
612 c++;
614 * The next character says more about the type (i.e. data, function, etc)
615 * of symbol. Skip it.
617 c++;
619 * The next is either an integer or a (integer,integer).
620 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
622 return stab_types[DEBUG_ReadTypeEnum(&c)];
626 DEBUG_ParseStabs(char * addr, unsigned int load_offset,
627 unsigned int staboff, int stablen,
628 unsigned int strtaboff, int strtablen)
630 struct name_hash * curr_func = NULL;
631 struct wine_locals * curr_loc = NULL;
632 struct name_hash * curr_sym = NULL;
633 char currpath[PATH_MAX];
634 int i;
635 int ignore = FALSE;
636 int last_nso = -1;
637 int len;
638 DBG_ADDR new_addr;
639 int nstab;
640 char * ptr;
641 char * stabbuff;
642 int stabbufflen;
643 struct stab_nlist * stab_ptr;
644 char * strs;
645 int strtabinc;
646 char * subpath = NULL;
647 char symname[4096];
649 nstab = stablen / sizeof(struct stab_nlist);
650 stab_ptr = (struct stab_nlist *) (addr + staboff);
651 strs = (char *) (addr + strtaboff);
653 memset(currpath, 0, sizeof(currpath));
656 * Allocate a buffer into which we can build stab strings for cases
657 * where the stab is continued over multiple lines.
659 stabbufflen = 65536;
660 stabbuff = (char *) DBG_alloc(stabbufflen);
662 strtabinc = 0;
663 stabbuff[0] = '\0';
664 for(i=0; i < nstab; i++, stab_ptr++ )
666 ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
667 if( ptr[strlen(ptr) - 1] == '\\' )
670 * Indicates continuation. Append this to the buffer, and go onto the
671 * next record. Repeat the process until we find a stab without the
672 * '/' character, as this indicates we have the whole thing.
674 len = strlen(ptr);
675 if( strlen(stabbuff) + len > stabbufflen )
677 stabbufflen += 65536;
678 stabbuff = (char *) DBG_realloc(stabbuff, stabbufflen);
680 strncat(stabbuff, ptr, len - 1);
681 continue;
683 else if( stabbuff[0] != '\0' )
685 strcat( stabbuff, ptr);
686 ptr = stabbuff;
689 if( strchr(ptr, '=') != NULL )
692 * The stabs aren't in writable memory, so copy it over so we are
693 * sure we can scribble on it.
695 if( ptr != stabbuff )
697 strcpy(stabbuff, ptr);
698 ptr = stabbuff;
700 stab_strcpy(symname, ptr);
701 DEBUG_ParseTypedefStab(ptr, symname);
704 switch(stab_ptr->n_type)
706 case N_GSYM:
708 * These are useless with ELF. They have no value, and you have to
709 * read the normal symbol table to get the address. Thus we
710 * ignore them, and when we process the normal symbol table
711 * we should do the right thing.
713 * With a.out, they actually do make some amount of sense.
715 new_addr.seg = 0;
716 new_addr.type = DEBUG_ParseStabType(ptr);
717 new_addr.off = load_offset + stab_ptr->n_value;
719 stab_strcpy(symname, ptr);
720 #ifdef __ELF__
721 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
722 SYM_WINE | SYM_DATA | SYM_INVALID);
723 #else
724 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
725 SYM_WINE | SYM_DATA );
726 #endif
727 break;
728 case N_RBRAC:
729 case N_LBRAC:
731 * We need to keep track of these so we get symbol scoping
732 * right for local variables. For now, we just ignore them.
733 * The hooks are already there for dealing with this however,
734 * so all we need to do is to keep count of the nesting level,
735 * and find the RBRAC for each matching LBRAC.
737 break;
738 case N_LCSYM:
739 case N_STSYM:
741 * These are static symbols and BSS symbols.
743 new_addr.seg = 0;
744 new_addr.type = DEBUG_ParseStabType(ptr);
745 new_addr.off = load_offset + stab_ptr->n_value;
747 stab_strcpy(symname, ptr);
748 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
749 SYM_WINE | SYM_DATA );
750 break;
751 case N_PSYM:
753 * These are function parameters.
755 if( (curr_func != NULL)
756 && (stab_ptr->n_value != 0) )
758 stab_strcpy(symname, ptr);
759 curr_loc = DEBUG_AddLocal(curr_func, 0,
760 stab_ptr->n_value, 0, 0, symname);
761 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr));
763 break;
764 case N_RSYM:
765 if( curr_func != NULL )
767 stab_strcpy(symname, ptr);
768 curr_loc = DEBUG_AddLocal(curr_func, stab_ptr->n_value, 0, 0, 0, symname);
769 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr));
771 break;
772 case N_LSYM:
773 if( (curr_func != NULL)
774 && (stab_ptr->n_value != 0) )
776 stab_strcpy(symname, ptr);
777 DEBUG_AddLocal(curr_func, 0,
778 stab_ptr->n_value, 0, 0, symname);
780 else if (curr_func == NULL)
782 stab_strcpy(symname, ptr);
784 break;
785 case N_SLINE:
787 * This is a line number. These are always relative to the start
788 * of the function (N_FUN), and this makes the lookup easier.
790 if( curr_func != NULL )
792 #ifdef __ELF__
793 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
794 stab_ptr->n_value);
795 #else
796 #if 0
798 * This isn't right. The order of the stabs is different under
799 * a.out, and as a result we would end up attaching the line
800 * number to the wrong function.
802 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
803 stab_ptr->n_value - curr_func->addr.off);
804 #endif
805 #endif
807 break;
808 case N_FUN:
810 * First, clean up the previous function we were working on.
812 DEBUG_Normalize(curr_func);
815 * For now, just declare the various functions. Later
816 * on, we will add the line number information and the
817 * local symbols.
819 if( !ignore )
821 new_addr.seg = 0;
822 new_addr.type = DEBUG_ParseStabType(ptr);
823 new_addr.off = load_offset + stab_ptr->n_value;
825 * Copy the string to a temp buffer so we
826 * can kill everything after the ':'. We do
827 * it this way because otherwise we end up dirtying
828 * all of the pages related to the stabs, and that
829 * sucks up swap space like crazy.
831 stab_strcpy(symname, ptr);
832 curr_func = DEBUG_AddSymbol( symname, &new_addr, currpath,
833 SYM_WINE | SYM_FUNC);
835 else
838 * Don't add line number information for this function
839 * any more.
841 curr_func = NULL;
843 break;
844 case N_SO:
846 * This indicates a new source file. Append the records
847 * together, to build the correct path name.
849 #ifndef __ELF__
851 * With a.out, there is no NULL string N_SO entry at the end of
852 * the file. Thus when we find non-consecutive entries,
853 * we consider that a new file is started.
855 if( last_nso < i-1 )
857 currpath[0] = '\0';
858 DEBUG_Normalize(curr_func);
859 curr_func = NULL;
861 #endif
863 if( *ptr == '\0' )
866 * Nuke old path.
868 currpath[0] = '\0';
869 DEBUG_Normalize(curr_func);
870 curr_func = NULL;
872 * The datatypes that we would need to use are reset when
873 * we start a new file.
875 memset(stab_types, 0, num_stab_types * sizeof(stab_types[0]));
877 for (i=0;i<nrofnroftypenums;i++)
878 memset(typenums[i],0,sizeof(typenums[i][0])*nroftypenums[i]);
881 else
883 if (*ptr != '/')
884 strcat(currpath, ptr);
885 else
886 strcpy(currpath, ptr);
887 subpath = ptr;
889 last_nso = i;
890 break;
891 case N_SOL:
893 * This indicates we are including stuff from an include file.
894 * If this is the main source, enable the debug stuff, otherwise
895 * ignore it.
897 if( subpath == NULL || strcmp(ptr, subpath) == 0 )
899 ignore = FALSE;
901 else
903 ignore = TRUE;
904 DEBUG_Normalize(curr_func);
905 curr_func = NULL;
907 break;
908 case N_UNDF:
909 strs += strtabinc;
910 strtabinc = stab_ptr->n_value;
911 DEBUG_Normalize(curr_func);
912 curr_func = NULL;
913 break;
914 case N_OPT:
916 * Ignore this. We don't care what it points to.
918 break;
919 case N_BINCL:
920 case N_EINCL:
921 case N_MAIN:
923 * Always ignore these. GCC doesn't even generate them.
925 break;
926 default:
927 break;
930 stabbuff[0] = '\0';
932 #if 0
933 fprintf(stderr, "%d %x %s\n", stab_ptr->n_type,
934 (unsigned int) stab_ptr->n_value,
935 strs + (unsigned int) stab_ptr->n_un.n_name);
936 #endif
939 if( stab_types != NULL )
941 DBG_free(stab_types);
942 stab_types = NULL;
943 num_stab_types = 0;
947 DEBUG_FreeRegisteredTypedefs();
949 return TRUE;
952 #ifdef __ELF__
955 * Walk through the entire symbol table and add any symbols we find there.
956 * This can be used in cases where we have stripped ELF shared libraries,
957 * or it can be used in cases where we have data symbols for which the address
958 * isn't encoded in the stabs.
960 * This is all really quite easy, since we don't have to worry about line
961 * numbers or local data variables.
963 static
965 DEBUG_ProcessElfSymtab(char * addr, unsigned int load_offset,
966 Elf32_Shdr * symtab, Elf32_Shdr * strtab)
968 char * curfile = NULL;
969 struct name_hash * curr_sym = NULL;
970 int flags;
971 int i;
972 DBG_ADDR new_addr;
973 int nsym;
974 char * strp;
975 char * symname;
976 Elf32_Sym * symp;
979 symp = (Elf32_Sym *) (addr + symtab->sh_offset);
980 nsym = symtab->sh_size / sizeof(*symp);
981 strp = (char *) (addr + strtab->sh_offset);
983 for(i=0; i < nsym; i++, symp++)
986 * Ignore certain types of entries which really aren't of that much
987 * interest.
989 if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION )
991 continue;
994 symname = strp + symp->st_name;
997 * Save the name of the current file, so we have a way of tracking
998 * static functions/data.
1000 if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
1002 curfile = symname;
1003 continue;
1008 * See if we already have something for this symbol.
1009 * If so, ignore this entry, because it would have come from the
1010 * stabs or from a previous symbol. If the value is different,
1011 * we will have to keep the darned thing, because there can be
1012 * multiple local symbols by the same name.
1014 if( (DEBUG_GetSymbolValue(symname, -1, &new_addr, FALSE ) == TRUE)
1015 && (new_addr.off == (load_offset + symp->st_value)) )
1016 continue;
1018 new_addr.seg = 0;
1019 new_addr.type = NULL;
1020 new_addr.off = load_offset + symp->st_value;
1021 flags = SYM_WINE | (ELF32_ST_BIND(symp->st_info) == STT_FUNC
1022 ? SYM_FUNC : SYM_DATA);
1023 if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
1024 curr_sym = DEBUG_AddSymbol( symname, &new_addr, NULL, flags );
1025 else
1026 curr_sym = DEBUG_AddSymbol( symname, &new_addr, curfile, flags );
1029 * Record the size of the symbol. This can come in handy in
1030 * some cases. Not really used yet, however.
1032 if( symp->st_size != 0 )
1033 DEBUG_SetSymbolSize(curr_sym, symp->st_size);
1036 return TRUE;
1039 static
1041 DEBUG_ProcessElfObject(char * filename, unsigned int load_offset)
1043 int rtn = FALSE;
1044 struct stat statbuf;
1045 int fd = -1;
1046 int status;
1047 char * addr = (char *) 0xffffffff;
1048 Elf32_Ehdr * ehptr;
1049 Elf32_Shdr * spnt;
1050 char * shstrtab;
1051 int nsect;
1052 int i;
1053 int stabsect;
1054 int stabstrsect;
1058 * Make sure we can stat and open this file.
1060 if( filename == NULL )
1061 goto leave;
1063 status = stat(filename, &statbuf);
1064 if( status == -1 )
1066 char *s,*t,*fn,*paths;
1067 if (strchr(filename,'/'))
1068 goto leave;
1069 paths = DBG_strdup(getenv("PATH"));
1070 s = paths;
1071 while (s && *s) {
1072 t = strchr(s,':');
1073 if (t) *t='\0';
1074 fn = (char*)DBG_alloc(strlen(filename)+1+strlen(s)+1);
1075 strcpy(fn,s);
1076 strcat(fn,"/");
1077 strcat(fn,filename);
1078 if ((rtn = DEBUG_ProcessElfObject(fn,load_offset))) {
1079 DBG_free(fn);
1080 DBG_free(paths);
1081 goto leave;
1083 DBG_free(fn);
1084 if (t) s = t+1; else break;
1086 if (!s || !*s) fprintf(stderr," not found");
1087 DBG_free(paths);
1088 goto leave;
1092 * Now open the file, so that we can mmap() it.
1094 fd = open(filename, O_RDONLY);
1095 if( fd == -1 )
1096 goto leave;
1100 * Now mmap() the file.
1102 addr = mmap(0, statbuf.st_size, PROT_READ,
1103 MAP_PRIVATE, fd, 0);
1104 if( addr == (char *) 0xffffffff )
1105 goto leave;
1108 * Next, we need to find a few of the internal ELF headers within
1109 * this thing. We need the main executable header, and the section
1110 * table.
1112 ehptr = (Elf32_Ehdr *) addr;
1114 if( load_offset == 0 )
1115 DEBUG_RegisterELFDebugInfo(ehptr->e_entry, statbuf.st_size, filename);
1116 else
1117 DEBUG_RegisterELFDebugInfo(load_offset, statbuf.st_size, filename);
1119 spnt = (Elf32_Shdr *) (addr + ehptr->e_shoff);
1120 nsect = ehptr->e_shnum;
1121 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1123 stabsect = stabstrsect = -1;
1125 for(i=0; i < nsect; i++)
1127 if( strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0 )
1128 stabsect = i;
1130 if( strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0 )
1131 stabstrsect = i;
1134 if( stabsect == -1 || stabstrsect == -1 )
1135 goto leave;
1138 * OK, now just parse all of the stabs.
1140 rtn = DEBUG_ParseStabs(addr, load_offset,
1141 spnt[stabsect].sh_offset,
1142 spnt[stabsect].sh_size,
1143 spnt[stabstrsect].sh_offset,
1144 spnt[stabstrsect].sh_size);
1146 if( rtn != TRUE )
1147 goto leave;
1149 for(i=0; i < nsect; i++)
1151 if( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1152 && (spnt[i].sh_type == SHT_SYMTAB) )
1153 DEBUG_ProcessElfSymtab(addr, load_offset,
1154 spnt + i, spnt + spnt[i].sh_link);
1156 if( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1157 && (spnt[i].sh_type == SHT_DYNSYM) )
1158 DEBUG_ProcessElfSymtab(addr, load_offset,
1159 spnt + i, spnt + spnt[i].sh_link);
1162 leave:
1164 if( addr != (char *) 0xffffffff )
1165 munmap(addr, statbuf.st_size);
1167 if( fd != -1 )
1168 close(fd);
1170 return (rtn);
1175 DEBUG_ReadExecutableDbgInfo(void)
1177 Elf32_Ehdr * ehdr;
1178 char * exe_name;
1179 Elf32_Dyn * dynpnt;
1180 struct r_debug * dbg_hdr;
1181 struct link_map * lpnt = NULL;
1182 #ifdef __GNUC__
1183 extern Elf32_Dyn _DYNAMIC[] __attribute__ ((weak));
1184 #else
1185 extern Elf32_Dyn _DYNAMIC[];
1186 #endif
1187 int rtn = FALSE;
1188 int rowcount;
1190 exe_name = DEBUG_argv0;
1193 * Make sure we can stat and open this file.
1195 if( exe_name == NULL )
1196 goto leave;
1198 fprintf( stderr, "Loading symbols: %s", exe_name );
1199 rowcount = 17 + strlen(exe_name);
1200 DEBUG_ProcessElfObject(exe_name, 0);
1203 * Finally walk the tables that the dynamic loader maintains to find all
1204 * of the other shared libraries which might be loaded. Perform the
1205 * same step for all of these.
1207 if( (&_DYNAMIC == NULL) || (_DYNAMIC == NULL) )
1208 goto leave;
1210 dynpnt = _DYNAMIC;
1213 * Now walk the dynamic section (of the executable, looking for a DT_DEBUG
1214 * entry.
1216 for(; dynpnt->d_tag != DT_NULL; dynpnt++)
1217 if( dynpnt->d_tag == DT_DEBUG )
1218 break;
1220 if( (dynpnt->d_tag != DT_DEBUG)
1221 || (dynpnt->d_un.d_ptr == 0) )
1222 goto leave;
1225 * OK, now dig into the actual tables themselves.
1227 dbg_hdr = (struct r_debug *) dynpnt->d_un.d_ptr;
1228 lpnt = dbg_hdr->r_map;
1231 * Now walk the linked list. In all known ELF implementations,
1232 * the dynamic loader maintains this linked list for us. In some
1233 * cases the first entry doesn't appear with a name, in other cases it
1234 * does.
1236 for(; lpnt; lpnt = lpnt->l_next )
1239 * We already got the stuff for the executable using the
1240 * argv[0] entry above. Here we only need to concentrate on any
1241 * shared libraries which may be loaded.
1243 ehdr = (Elf32_Ehdr *) lpnt->l_addr;
1244 if( (lpnt->l_addr == 0) || (ehdr->e_type != ET_DYN) )
1245 continue;
1247 if( lpnt->l_name != NULL )
1249 if (rowcount + strlen(lpnt->l_name) > 76)
1251 fprintf( stderr, "\n " );
1252 rowcount = 3;
1254 fprintf( stderr, " %s", lpnt->l_name );
1255 rowcount += strlen(lpnt->l_name) + 1;
1256 DEBUG_ProcessElfObject(lpnt->l_name, lpnt->l_addr);
1260 rtn = TRUE;
1262 leave:
1263 fprintf( stderr, "\n" );
1264 return (rtn);
1268 #else /* !__ELF__ */
1270 #ifdef linux
1272 * a.out linux.
1275 DEBUG_ReadExecutableDbgInfo(void)
1277 char * addr = (char *) 0xffffffff;
1278 char * exe_name;
1279 struct exec * ahdr;
1280 int fd = -1;
1281 int rtn = FALSE;
1282 unsigned int staboff;
1283 struct stat statbuf;
1284 int status;
1285 unsigned int stroff;
1287 exe_name = DEBUG_argv0;
1290 * Make sure we can stat and open this file.
1292 if( exe_name == NULL )
1293 goto leave;
1295 status = stat(exe_name, &statbuf);
1296 if( status == -1 )
1297 goto leave;
1300 * Now open the file, so that we can mmap() it.
1302 fd = open(exe_name, O_RDONLY);
1303 if( fd == -1 )
1304 goto leave;
1308 * Now mmap() the file.
1310 addr = mmap(0, statbuf.st_size, PROT_READ,
1311 MAP_PRIVATE, fd, 0);
1312 if( addr == (char *) 0xffffffff )
1313 goto leave;
1315 ahdr = (struct exec *) addr;
1317 staboff = N_SYMOFF(*ahdr);
1318 stroff = N_STROFF(*ahdr);
1319 rtn = DEBUG_ParseStabs(addr, 0,
1320 staboff,
1321 ahdr->a_syms,
1322 stroff,
1323 statbuf.st_size - stroff);
1326 * Give a nice status message here...
1328 fprintf( stderr, "Loading symbols: %s", exe_name );
1330 rtn = TRUE;
1332 leave:
1334 if( addr != (char *) 0xffffffff )
1335 munmap(addr, statbuf.st_size);
1337 if( fd != -1 )
1338 close(fd);
1340 return (rtn);
1343 #else
1345 * Non-linux, non-ELF platforms.
1348 DEBUG_ReadExecutableDbgInfo(void)
1350 return FALSE;
1352 #endif
1354 #endif /* __ELF__ */