Release 970509
[wine/multimedia.git] / debugger / stabs.c
blobdcdeaeb6d2f60ab9a8a74250a78413278bf5590f
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];
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 != ':')
132 *dest++ = *source++;
134 *dest++ = '\0';
137 #define MAX_TD_NESTING 128
139 static
141 DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef)
143 int hash;
144 struct known_typedef * ktd;
146 if( ndef == 1 )
148 return TRUE;
151 ktd = (struct known_typedef *) malloc(sizeof(struct known_typedef)
152 + ndef * sizeof(struct datatype *));
154 hash = stab_hash(name);
156 ktd->name = xstrdup(name);
157 ktd->ndefs = ndef;
158 memcpy(&ktd->types[0], types, ndef * sizeof(struct datatype *));
159 ktd->next = ktd_head[hash];
160 ktd_head[hash] = ktd;
162 return TRUE;
165 static
167 DEBUG_HandlePreviousTypedef(const char * name, const char * stab)
169 int count;
170 enum debug_type expect;
171 int hash;
172 struct known_typedef * ktd;
173 char * ptr;
174 char * tc;
175 int typenum;
177 hash = stab_hash(name);
179 for(ktd = ktd_head[hash]; ktd; ktd = ktd->next)
181 if( (ktd->name[0] == name[0])
182 && (strcmp(name, ktd->name) == 0) )
184 break;
189 * Didn't find it. This must be a new one.
191 if( ktd == NULL )
193 return FALSE;
197 * Examine the stab to make sure it has the same number of definitions.
199 count = 0;
200 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
202 if( count >= ktd->ndefs )
204 return FALSE;
208 * Make sure the types of all of the objects is consistent with
209 * what we have already parsed.
211 switch(ptr[1])
213 case '*':
214 expect = POINTER;
215 break;
216 case 's':
217 case 'u':
218 expect = STRUCT;
219 break;
220 case 'a':
221 expect = ARRAY;
222 break;
223 case '1':
224 case 'r':
225 expect = BASIC;
226 break;
227 case 'x':
228 expect = STRUCT;
229 break;
230 case 'e':
231 expect = ENUM;
232 break;
233 case 'f':
234 expect = FUNC;
235 break;
236 default:
237 fprintf(stderr, "Unknown type.\n");
238 return FALSE;
240 if( expect != DEBUG_GetType(ktd->types[count]) )
242 return FALSE;
244 count++;
247 if( ktd->ndefs != count )
249 return FALSE;
253 * OK, this one is safe. Go through, dig out all of the type numbers,
254 * and substitute the appropriate things.
256 count = 0;
257 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
260 * Back up until we get to a non-numeric character. This is the type
261 * number.
263 tc = ptr - 1;
264 while( *tc >= '0' && *tc <= '9' )
266 tc--;
269 typenum = atol(tc + 1);
270 if( num_stab_types <= typenum )
272 num_stab_types = typenum + 32;
273 stab_types = (struct datatype **) xrealloc(stab_types,
274 num_stab_types * sizeof(struct datatype *));
275 if( stab_types == NULL )
277 return FALSE;
281 stab_types[typenum] = ktd->types[count++];
284 return TRUE;
287 static int DEBUG_FreeRegisteredTypedefs()
289 int count;
290 int j;
291 struct known_typedef * ktd;
292 struct known_typedef * next;
294 count = 0;
295 for(j=0; j < NR_STAB_HASH; j++ )
297 for(ktd = ktd_head[j]; ktd; ktd = next)
299 count++;
300 next = ktd->next;
301 free(ktd->name);
302 free(ktd);
304 ktd_head[j] = NULL;
307 return TRUE;
311 static
313 DEBUG_ParseTypedefStab(char * ptr, const char * typename)
315 int arrmax;
316 int arrmin;
317 char * c;
318 struct datatype * curr_type;
319 struct datatype * datatype;
320 struct datatype * curr_types[MAX_TD_NESTING];
321 char element_name[1024];
322 int ntypes = 0;
323 int offset;
324 const char * orig_typename;
325 int rtn = FALSE;
326 int size;
327 char * tc;
328 char * tc2;
329 int typenum;
331 orig_typename = typename;
333 if( DEBUG_HandlePreviousTypedef(typename, ptr) == TRUE )
335 return TRUE;
339 * Go from back to front. First we go through and figure out what
340 * type numbers we need, and register those types. Then we go in
341 * and fill the details.
344 for( c = strchr(ptr, '='); c != NULL; c = strchr(c + 1, '=') )
347 * Back up until we get to a non-numeric character. This is the type
348 * number.
350 tc = c - 1;
351 while( *tc >= '0' && *tc <= '9' )
353 tc--;
355 typenum = atol(tc + 1);
356 if( num_stab_types <= typenum )
358 num_stab_types = typenum + 32;
359 stab_types = (struct datatype **) xrealloc(stab_types,
360 num_stab_types * sizeof(struct datatype *));
361 if( stab_types == NULL )
363 goto leave;
367 if( ntypes >= MAX_TD_NESTING )
370 * If this ever happens, just bump the counter.
372 fprintf(stderr, "Typedef nesting overflow\n");
373 return FALSE;
376 switch(c[1])
378 case '*':
379 stab_types[typenum] = DEBUG_NewDataType(POINTER, NULL);
380 curr_types[ntypes++] = stab_types[typenum];
381 break;
382 case 's':
383 case 'u':
384 stab_types[typenum] = DEBUG_NewDataType(STRUCT, typename);
385 curr_types[ntypes++] = stab_types[typenum];
386 break;
387 case 'a':
388 stab_types[typenum] = DEBUG_NewDataType(ARRAY, NULL);
389 curr_types[ntypes++] = stab_types[typenum];
390 break;
391 case '1':
392 case 'r':
393 stab_types[typenum] = DEBUG_NewDataType(BASIC, typename);
394 curr_types[ntypes++] = stab_types[typenum];
395 break;
396 case 'x':
397 stab_strcpy(element_name, c + 3);
398 stab_types[typenum] = DEBUG_NewDataType(STRUCT, element_name);
399 curr_types[ntypes++] = stab_types[typenum];
400 break;
401 case 'e':
402 stab_types[typenum] = DEBUG_NewDataType(ENUM, NULL);
403 curr_types[ntypes++] = stab_types[typenum];
404 break;
405 case 'f':
406 stab_types[typenum] = DEBUG_NewDataType(FUNC, NULL);
407 curr_types[ntypes++] = stab_types[typenum];
408 break;
409 default:
410 fprintf(stderr, "Unknown type.\n");
412 typename = NULL;
416 * Now register the type so that if we encounter it again, we will know
417 * what to do.
419 DEBUG_RegisterTypedef(orig_typename, curr_types, ntypes);
422 * OK, now take a second sweep through. Now we will be digging
423 * out the definitions of the various components, and storing
424 * them in the skeletons that we have already allocated. We take
425 * a right-to left search as this is much easier to parse.
427 for( c = strrchr(ptr, '='); c != NULL; c = strrchr(ptr, '=') )
430 * Back up until we get to a non-numeric character. This is the type
431 * number.
433 tc = c - 1;
434 while( *tc >= '0' && *tc <= '9' )
436 tc--;
438 typenum = atol(tc + 1);
439 curr_type = stab_types[typenum];
441 switch(c[1])
443 case 'x':
444 tc = c + 3;
445 while( *tc != ':' )
447 tc ++;
449 tc++;
450 if( *tc == '\0' )
452 *c = '\0';
454 else
456 strcpy(c, tc);
459 break;
460 case '*':
461 case 'f':
462 tc = c + 2;
463 datatype = stab_types[strtol(tc, &tc, 10)];
464 DEBUG_SetPointerType(curr_type, datatype);
465 if( *tc == '\0' )
467 *c = '\0';
469 else
471 strcpy(c, tc);
473 break;
474 case '1':
475 case 'r':
477 * We have already handled these above.
479 *c = '\0';
480 break;
481 case 'a':
482 tc = c + 5;
483 arrmin = strtol(tc, &tc, 10);
484 tc++;
485 arrmax = strtol(tc, &tc, 10);
486 tc++;
487 datatype = stab_types[strtol(tc, &tc, 10)];
488 if( *tc == '\0' )
490 *c = '\0';
492 else
494 strcpy(c, tc);
497 DEBUG_SetArrayParams(curr_type, arrmin, arrmax, datatype);
498 break;
499 case 's':
500 case 'u':
501 tc = c + 2;
502 if( DEBUG_SetStructSize(curr_type, strtol(tc, &tc, 10)) == FALSE )
505 * We have already filled out this structure. Nothing to do,
506 * so just skip forward to the end of the definition.
508 while( tc[0] != ';' && tc[1] != ';' )
510 tc++;
513 tc += 2;
515 if( *tc == '\0' )
517 *c = '\0';
519 else
521 strcpy(c, tc + 1);
523 continue;
527 * Now parse the individual elements of the structure/union.
529 while(*tc != ';')
531 tc2 = element_name;
532 while(*tc != ':')
534 *tc2++ = *tc++;
536 tc++;
537 *tc2++ = '\0';
538 datatype = stab_types[strtol(tc, &tc, 10)];
539 tc++;
540 offset = strtol(tc, &tc, 10);
541 tc++;
542 size = strtol(tc, &tc, 10);
543 tc++;
544 DEBUG_AddStructElement(curr_type, element_name, datatype, offset, size);
546 if( *tc == '\0' )
548 *c = '\0';
550 else
552 strcpy(c, tc + 1);
554 break;
555 case 'e':
556 tc = c + 2;
558 * Now parse the individual elements of the structure/union.
560 while(*tc != ';')
562 tc2 = element_name;
563 while(*tc != ':')
565 *tc2++ = *tc++;
567 tc++;
568 *tc2++ = '\0';
569 offset = strtol(tc, &tc, 10);
570 tc++;
571 DEBUG_AddStructElement(curr_type, element_name, NULL, offset, 0);
573 if( *tc == '\0' )
575 *c = '\0';
577 else
579 strcpy(c, tc + 1);
581 break;
582 default:
583 fprintf(stderr, "Unknown type.\n");
584 break;
588 rtn = TRUE;
590 leave:
592 return rtn;
596 static struct datatype *
597 DEBUG_ParseStabType(const char * stab)
599 char * c;
600 int typenum;
603 * Look through the stab definition, and figure out what datatype
604 * this represents. If we have something we know about, assign the
605 * type.
607 c = strchr(stab, ':');
608 if( c == NULL )
610 return NULL;
613 c++;
615 * The next character says more about the type (i.e. data, function, etc)
616 * of symbol. Skip it.
618 c++;
620 typenum = atol(c);
622 if( typenum < num_stab_types && stab_types[typenum] != NULL )
624 return stab_types[typenum];
627 return NULL;
630 static
632 DEBUG_ParseStabs(char * addr, unsigned int load_offset,
633 unsigned int staboff, int stablen,
634 unsigned int strtaboff, int strtablen)
636 struct name_hash * curr_func = NULL;
637 struct wine_locals * curr_loc = NULL;
638 struct name_hash * curr_sym = NULL;
639 char currpath[PATH_MAX];
640 int i;
641 int ignore = FALSE;
642 int last_nso = -1;
643 int len;
644 DBG_ADDR new_addr;
645 int nstab;
646 char * ptr;
647 char * stabbuff;
648 int stabbufflen;
649 struct stab_nlist * stab_ptr;
650 char * strs;
651 int strtabinc;
652 char * subpath = NULL;
653 char symname[4096];
655 nstab = stablen / sizeof(struct stab_nlist);
656 stab_ptr = (struct stab_nlist *) (addr + staboff);
657 strs = (char *) (addr + strtaboff);
659 memset(currpath, 0, sizeof(currpath));
662 * Allocate a buffer into which we can build stab strings for cases
663 * where the stab is continued over multiple lines.
665 stabbufflen = 65536;
666 stabbuff = (char *) xmalloc(stabbufflen);
667 if( stabbuff == NULL )
669 goto leave;
672 strtabinc = 0;
673 stabbuff[0] = '\0';
674 for(i=0; i < nstab; i++, stab_ptr++ )
676 ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
677 if( ptr[strlen(ptr) - 1] == '\\' )
680 * Indicates continuation. Append this to the buffer, and go onto the
681 * next record. Repeat the process until we find a stab without the
682 * '/' character, as this indicates we have the whole thing.
684 len = strlen(ptr);
685 if( strlen(stabbuff) + len > stabbufflen )
687 stabbufflen += 65536;
688 stabbuff = (char *) xrealloc(stabbuff, stabbufflen);
689 if( stabbuff == NULL )
691 goto leave;
694 strncat(stabbuff, ptr, len - 1);
695 continue;
697 else if( stabbuff[0] != '\0' )
699 strcat( stabbuff, ptr);
700 ptr = stabbuff;
703 if( strchr(ptr, '=') != NULL )
706 * The stabs aren't in writable memory, so copy it over so we are
707 * sure we can scribble on it.
709 if( ptr != stabbuff )
711 strcpy(stabbuff, ptr);
712 ptr = stabbuff;
714 stab_strcpy(symname, ptr);
715 DEBUG_ParseTypedefStab(ptr, symname);
718 switch(stab_ptr->n_type)
720 case N_GSYM:
722 * These are useless with ELF. They have no value, and you have to
723 * read the normal symbol table to get the address. Thus we
724 * ignore them, and when we process the normal symbol table
725 * we should do the right thing.
727 * With a.out, they actually do make some amount of sense.
729 new_addr.seg = 0;
730 new_addr.type = DEBUG_ParseStabType(ptr);
731 new_addr.off = load_offset + stab_ptr->n_value;
733 stab_strcpy(symname, ptr);
734 #ifdef __ELF__
735 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
736 SYM_WINE | SYM_DATA | SYM_INVALID);
737 #else
738 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
739 SYM_WINE | SYM_DATA );
740 #endif
741 break;
742 case N_RBRAC:
743 case N_LBRAC:
745 * We need to keep track of these so we get symbol scoping
746 * right for local variables. For now, we just ignore them.
747 * The hooks are already there for dealing with this however,
748 * so all we need to do is to keep count of the nesting level,
749 * and find the RBRAC for each matching LBRAC.
751 break;
752 case N_LCSYM:
753 case N_STSYM:
755 * These are static symbols and BSS symbols.
757 new_addr.seg = 0;
758 new_addr.type = DEBUG_ParseStabType(ptr);
759 new_addr.off = load_offset + stab_ptr->n_value;
761 stab_strcpy(symname, ptr);
762 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
763 SYM_WINE | SYM_DATA );
764 break;
765 case N_PSYM:
767 * These are function parameters.
769 if( (curr_func != NULL)
770 && (stab_ptr->n_value != 0) )
772 stab_strcpy(symname, ptr);
773 curr_loc = DEBUG_AddLocal(curr_func, 0,
774 stab_ptr->n_value, 0, 0, symname);
775 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr));
777 break;
778 case N_RSYM:
779 if( curr_func != NULL )
781 stab_strcpy(symname, ptr);
782 curr_loc = DEBUG_AddLocal(curr_func, stab_ptr->n_value, 0, 0, 0, symname);
783 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr));
785 break;
786 case N_LSYM:
787 if( (curr_func != NULL)
788 && (stab_ptr->n_value != 0) )
790 stab_strcpy(symname, ptr);
791 DEBUG_AddLocal(curr_func, 0,
792 stab_ptr->n_value, 0, 0, symname);
794 else if (curr_func == NULL)
796 stab_strcpy(symname, ptr);
798 break;
799 case N_SLINE:
801 * This is a line number. These are always relative to the start
802 * of the function (N_FUN), and this makes the lookup easier.
804 if( curr_func != NULL )
806 #ifdef __ELF__
807 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
808 stab_ptr->n_value);
809 #else
810 #if 0
812 * This isn't right. The order of the stabs is different under
813 * a.out, and as a result we would end up attaching the line
814 * number to the wrong function.
816 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
817 stab_ptr->n_value - curr_func->addr.off);
818 #endif
819 #endif
821 break;
822 case N_FUN:
824 * First, clean up the previous function we were working on.
826 DEBUG_Normalize(curr_func);
829 * For now, just declare the various functions. Later
830 * on, we will add the line number information and the
831 * local symbols.
833 if( !ignore )
835 new_addr.seg = 0;
836 new_addr.type = DEBUG_ParseStabType(ptr);
837 new_addr.off = load_offset + stab_ptr->n_value;
839 * Copy the string to a temp buffer so we
840 * can kill everything after the ':'. We do
841 * it this way because otherwise we end up dirtying
842 * all of the pages related to the stabs, and that
843 * sucks up swap space like crazy.
845 stab_strcpy(symname, ptr);
846 curr_func = DEBUG_AddSymbol( symname, &new_addr, currpath,
847 SYM_WINE | SYM_FUNC);
849 else
852 * Don't add line number information for this function
853 * any more.
855 curr_func = NULL;
857 break;
858 case N_SO:
860 * This indicates a new source file. Append the records
861 * together, to build the correct path name.
863 #ifndef __ELF__
865 * With a.out, there is no NULL string N_SO entry at the end of
866 * the file. Thus when we find non-consecutive entries,
867 * we consider that a new file is started.
869 if( last_nso < i-1 )
871 currpath[0] = '\0';
872 DEBUG_Normalize(curr_func);
873 curr_func = NULL;
875 #endif
877 if( *ptr == '\0' )
880 * Nuke old path.
882 currpath[0] = '\0';
883 DEBUG_Normalize(curr_func);
884 curr_func = NULL;
886 * The datatypes that we would need to use are reset when
887 * we start a new file.
889 memset(stab_types, 0, num_stab_types * sizeof(stab_types));
891 else
893 if (*ptr != '/')
894 strcat(currpath, ptr);
895 else
896 strcpy(currpath, ptr);
897 subpath = ptr;
899 last_nso = i;
900 break;
901 case N_SOL:
903 * This indicates we are including stuff from an include file.
904 * If this is the main source, enable the debug stuff, otherwise
905 * ignore it.
907 if( subpath == NULL || strcmp(ptr, subpath) == 0 )
909 ignore = FALSE;
911 else
913 ignore = TRUE;
914 DEBUG_Normalize(curr_func);
915 curr_func = NULL;
917 break;
918 case N_UNDF:
919 strs += strtabinc;
920 strtabinc = stab_ptr->n_value;
921 DEBUG_Normalize(curr_func);
922 curr_func = NULL;
923 break;
924 case N_OPT:
926 * Ignore this. We don't care what it points to.
928 break;
929 case N_BINCL:
930 case N_EINCL:
931 case N_MAIN:
933 * Always ignore these. GCC doesn't even generate them.
935 break;
936 default:
937 break;
940 stabbuff[0] = '\0';
942 #if 0
943 fprintf(stderr, "%d %x %s\n", stab_ptr->n_type,
944 (unsigned int) stab_ptr->n_value,
945 strs + (unsigned int) stab_ptr->n_un.n_name);
946 #endif
949 leave:
951 if( stab_types != NULL )
953 free(stab_types);
954 stab_types = NULL;
955 num_stab_types = 0;
959 DEBUG_FreeRegisteredTypedefs();
961 return TRUE;
964 #ifdef __ELF__
967 * Walk through the entire symbol table and add any symbols we find there.
968 * This can be used in cases where we have stripped ELF shared libraries,
969 * or it can be used in cases where we have data symbols for which the address
970 * isn't encoded in the stabs.
972 * This is all really quite easy, since we don't have to worry about line
973 * numbers or local data variables.
975 static
977 DEBUG_ProcessElfSymtab(char * addr, unsigned int load_offset,
978 Elf32_Shdr * symtab, Elf32_Shdr * strtab)
980 char * curfile = NULL;
981 struct name_hash * curr_sym = NULL;
982 int flags;
983 int i;
984 DBG_ADDR new_addr;
985 int nsym;
986 char * strp;
987 char * symname;
988 Elf32_Sym * symp;
991 symp = (Elf32_Sym *) (addr + symtab->sh_offset);
992 nsym = symtab->sh_size / sizeof(*symp);
993 strp = (char *) (addr + strtab->sh_offset);
995 for(i=0; i < nsym; i++, symp++)
998 * Ignore certain types of entries which really aren't of that much
999 * interest.
1001 if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION )
1003 continue;
1006 symname = strp + symp->st_name;
1009 * Save the name of the current file, so we have a way of tracking
1010 * static functions/data.
1012 if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
1014 curfile = symname;
1015 continue;
1020 * See if we already have something for this symbol.
1021 * If so, ignore this entry, because it would have come from the
1022 * stabs or from a previous symbol. If the value is different,
1023 * we will have to keep the darned thing, because there can be
1024 * multiple local symbols by the same name.
1026 if( (DEBUG_GetSymbolValue(symname, -1, &new_addr, FALSE ) == TRUE)
1027 && (new_addr.off == (load_offset + symp->st_value)) )
1029 continue;
1032 new_addr.seg = 0;
1033 new_addr.type = NULL;
1034 new_addr.off = load_offset + symp->st_value;
1035 flags = SYM_WINE | (ELF32_ST_BIND(symp->st_info) == STT_FUNC
1036 ? SYM_FUNC : SYM_DATA);
1037 if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
1039 curr_sym = DEBUG_AddSymbol( symname, &new_addr, NULL, flags );
1041 else
1043 curr_sym = DEBUG_AddSymbol( symname, &new_addr, curfile, flags );
1047 * Record the size of the symbol. This can come in handy in
1048 * some cases. Not really used yet, however.
1050 if( symp->st_size != 0 )
1052 DEBUG_SetSymbolSize(curr_sym, symp->st_size);
1056 return TRUE;
1059 static
1061 DEBUG_ProcessElfObject(char * filename, unsigned int load_offset)
1063 int rtn = FALSE;
1064 struct stat statbuf;
1065 int fd = -1;
1066 int status;
1067 char * addr = (char *) 0xffffffff;
1068 Elf32_Ehdr * ehptr;
1069 Elf32_Shdr * spnt;
1070 char * shstrtab;
1071 int nsect;
1072 int i;
1073 int stabsect;
1074 int stabstrsect;
1078 * Make sure we can stat and open this file.
1080 if( filename == NULL )
1082 goto leave;
1085 status = stat(filename, &statbuf);
1086 if( status == -1 )
1088 goto leave;
1092 * Now open the file, so that we can mmap() it.
1094 fd = open(filename, O_RDONLY);
1095 if( fd == -1 )
1097 goto leave;
1102 * Now mmap() the file.
1104 addr = mmap(0, statbuf.st_size, PROT_READ,
1105 MAP_PRIVATE, fd, 0);
1106 if( addr == (char *) 0xffffffff )
1108 goto leave;
1112 * Give a nice status message here...
1113 * Well not, just print the name.
1115 fprintf(stderr, " %s", filename);
1118 * Next, we need to find a few of the internal ELF headers within
1119 * this thing. We need the main executable header, and the section
1120 * table.
1122 ehptr = (Elf32_Ehdr *) addr;
1124 if( load_offset == NULL )
1126 DEBUG_RegisterELFDebugInfo(ehptr->e_entry, statbuf.st_size, filename);
1128 else
1130 DEBUG_RegisterELFDebugInfo(load_offset, statbuf.st_size, filename);
1133 spnt = (Elf32_Shdr *) (addr + ehptr->e_shoff);
1134 nsect = ehptr->e_shnum;
1135 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1137 stabsect = stabstrsect = -1;
1139 for(i=0; i < nsect; i++)
1141 if( strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0 )
1143 stabsect = i;
1146 if( strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0 )
1148 stabstrsect = i;
1152 if( stabsect == -1 || stabstrsect == -1 )
1154 goto leave;
1158 * OK, now just parse all of the stabs.
1160 rtn = DEBUG_ParseStabs(addr, load_offset,
1161 spnt[stabsect].sh_offset,
1162 spnt[stabsect].sh_size,
1163 spnt[stabstrsect].sh_offset,
1164 spnt[stabstrsect].sh_size);
1166 if( rtn != TRUE )
1168 goto leave;
1171 for(i=0; i < nsect; i++)
1173 if( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1174 && (spnt[i].sh_type == SHT_SYMTAB) )
1176 DEBUG_ProcessElfSymtab(addr, load_offset,
1177 spnt + i, spnt + spnt[i].sh_link);
1180 if( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1181 && (spnt[i].sh_type == SHT_DYNSYM) )
1183 DEBUG_ProcessElfSymtab(addr, load_offset,
1184 spnt + i, spnt + spnt[i].sh_link);
1188 leave:
1190 if( addr != (char *) 0xffffffff )
1192 munmap(addr, statbuf.st_size);
1195 if( fd != -1 )
1197 close(fd);
1200 return (rtn);
1205 DEBUG_ReadExecutableDbgInfo(void)
1207 Elf32_Ehdr * ehdr;
1208 char * exe_name;
1209 Elf32_Dyn * dynpnt;
1210 struct r_debug * dbg_hdr;
1211 struct link_map * lpnt = NULL;
1212 extern Elf32_Dyn _DYNAMIC[];
1213 int rtn = FALSE;
1215 exe_name = DEBUG_argv0;
1218 * Make sure we can stat and open this file.
1220 if( exe_name == NULL )
1222 goto leave;
1225 DEBUG_ProcessElfObject(exe_name, 0);
1228 * Finally walk the tables that the dynamic loader maintains to find all
1229 * of the other shared libraries which might be loaded. Perform the
1230 * same step for all of these.
1232 dynpnt = _DYNAMIC;
1233 if( dynpnt == NULL )
1235 goto leave;
1239 * Now walk the dynamic section (of the executable, looking for a DT_DEBUG
1240 * entry.
1242 for(; dynpnt->d_tag != DT_NULL; dynpnt++)
1244 if( dynpnt->d_tag == DT_DEBUG )
1246 break;
1250 if( (dynpnt->d_tag != DT_DEBUG)
1251 || (dynpnt->d_un.d_ptr == NULL) )
1253 goto leave;
1257 * OK, now dig into the actual tables themselves.
1259 dbg_hdr = (struct r_debug *) dynpnt->d_un.d_ptr;
1260 lpnt = dbg_hdr->r_map;
1263 * Now walk the linked list. In all known ELF implementations,
1264 * the dynamic loader maintains this linked list for us. In some
1265 * cases the first entry doesn't appear with a name, in other cases it
1266 * does.
1268 for(; lpnt; lpnt = lpnt->l_next )
1271 * We already got the stuff for the executable using the
1272 * argv[0] entry above. Here we only need to concentrate on any
1273 * shared libraries which may be loaded.
1275 ehdr = (Elf32_Ehdr *) lpnt->l_addr;
1276 if( (lpnt->l_addr == NULL) || (ehdr->e_type != ET_DYN) )
1278 continue;
1281 if( lpnt->l_name != NULL )
1283 DEBUG_ProcessElfObject(lpnt->l_name, lpnt->l_addr);
1287 rtn = TRUE;
1289 leave:
1291 return (rtn);
1295 #else /* !__ELF__ */
1297 #ifdef linux
1299 * a.out linux.
1302 DEBUG_ReadExecutableDbgInfo(void)
1304 char * addr = (char *) 0xffffffff;
1305 char * exe_name;
1306 struct exec * ahdr;
1307 int fd = -1;
1308 int rtn = FALSE;
1309 unsigned int staboff;
1310 struct stat statbuf;
1311 int status;
1312 unsigned int stroff;
1314 exe_name = DEBUG_argv0;
1317 * Make sure we can stat and open this file.
1319 if( exe_name == NULL )
1321 goto leave;
1324 status = stat(exe_name, &statbuf);
1325 if( status == -1 )
1327 goto leave;
1331 * Now open the file, so that we can mmap() it.
1333 fd = open(exe_name, O_RDONLY);
1334 if( fd == -1 )
1336 goto leave;
1341 * Now mmap() the file.
1343 addr = mmap(0, statbuf.st_size, PROT_READ,
1344 MAP_PRIVATE, fd, 0);
1345 if( addr == (char *) 0xffffffff )
1347 goto leave;
1350 ahdr = (struct exec *) addr;
1352 staboff = N_SYMOFF(*ahdr);
1353 stroff = N_STROFF(*ahdr);
1354 rtn = DEBUG_ParseStabs(addr, 0,
1355 staboff,
1356 ahdr->a_syms,
1357 stroff,
1358 statbuf.st_size - stroff);
1361 * Give a nice status message here...
1363 fprintf(stderr, " %s", exe_name);
1365 rtn = TRUE;
1367 leave:
1369 if( addr != (char *) 0xffffffff )
1371 munmap(addr, statbuf.st_size);
1374 if( fd != -1 )
1376 close(fd);
1379 return (rtn);
1382 #else
1384 * Non-linux, non-ELF platforms.
1387 DEBUG_ReadExecutableDbgInfo(void)
1389 return FALSE;
1391 #endif
1393 #endif /* __ELF__ */