2 * File stabs.c - read stabs information from the wine executable itself.
4 * Copyright (C) 1996, Eric Youngdale.
17 #define PATH_MAX _MAX_PATH
32 #elif defined(__EMX__)
62 * This is how we translate stab types into our internal representations
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.
76 struct stab_nlist *n_next;
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.
91 struct known_typedef * next;
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;
111 hash = (hash << 4) + *p++;
113 if( (tmp = (hash & 0xf0000000)) )
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
130 while(*source != '\0' && *source != ':')
137 #define MAX_TD_NESTING 128
141 DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef)
144 struct known_typedef * ktd;
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);
158 memcpy(&ktd->types[0], types, ndef * sizeof(struct datatype *));
159 ktd->next = ktd_head[hash];
160 ktd_head[hash] = ktd;
167 DEBUG_HandlePreviousTypedef(const char * name, const char * stab)
170 enum debug_type expect;
172 struct known_typedef * ktd;
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) )
189 * Didn't find it. This must be a new one.
197 * Examine the stab to make sure it has the same number of definitions.
200 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
202 if( count >= ktd->ndefs )
208 * Make sure the types of all of the objects is consistent with
209 * what we have already parsed.
237 fprintf(stderr, "Unknown type.\n");
240 if( expect != DEBUG_GetType(ktd->types[count]) )
247 if( ktd->ndefs != count )
253 * OK, this one is safe. Go through, dig out all of the type numbers,
254 * and substitute the appropriate things.
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
264 while( *tc >= '0' && *tc <= '9' )
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 )
281 stab_types[typenum] = ktd->types[count++];
287 static int DEBUG_FreeRegisteredTypedefs()
291 struct known_typedef * ktd;
292 struct known_typedef * next;
295 for(j=0; j < NR_STAB_HASH; j++ )
297 for(ktd = ktd_head[j]; ktd; ktd = next)
313 DEBUG_ParseTypedefStab(char * ptr, const char * typename)
318 struct datatype * curr_type;
319 struct datatype * datatype;
320 struct datatype * curr_types[MAX_TD_NESTING];
321 char element_name[1024];
324 const char * orig_typename;
331 orig_typename = typename;
333 if( DEBUG_HandlePreviousTypedef(typename, ptr) == 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
351 while( *tc >= '0' && *tc <= '9' )
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 )
367 if( ntypes >= MAX_TD_NESTING )
370 * If this ever happens, just bump the counter.
372 fprintf(stderr, "Typedef nesting overflow\n");
379 stab_types[typenum] = DEBUG_NewDataType(POINTER, NULL);
380 curr_types[ntypes++] = stab_types[typenum];
384 stab_types[typenum] = DEBUG_NewDataType(STRUCT, typename);
385 curr_types[ntypes++] = stab_types[typenum];
388 stab_types[typenum] = DEBUG_NewDataType(ARRAY, NULL);
389 curr_types[ntypes++] = stab_types[typenum];
393 stab_types[typenum] = DEBUG_NewDataType(BASIC, typename);
394 curr_types[ntypes++] = stab_types[typenum];
397 stab_strcpy(element_name, c + 3);
398 stab_types[typenum] = DEBUG_NewDataType(STRUCT, element_name);
399 curr_types[ntypes++] = stab_types[typenum];
402 stab_types[typenum] = DEBUG_NewDataType(ENUM, NULL);
403 curr_types[ntypes++] = stab_types[typenum];
406 stab_types[typenum] = DEBUG_NewDataType(FUNC, NULL);
407 curr_types[ntypes++] = stab_types[typenum];
410 fprintf(stderr, "Unknown type.\n");
416 * Now register the type so that if we encounter it again, we will know
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
434 while( *tc >= '0' && *tc <= '9' )
438 typenum = atol(tc + 1);
439 curr_type = stab_types[typenum];
463 datatype = stab_types[strtol(tc, &tc, 10)];
464 DEBUG_SetPointerType(curr_type, datatype);
477 * We have already handled these above.
483 arrmin = strtol(tc, &tc, 10);
485 arrmax = strtol(tc, &tc, 10);
487 datatype = stab_types[strtol(tc, &tc, 10)];
497 DEBUG_SetArrayParams(curr_type, arrmin, arrmax, datatype);
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] != ';' )
527 * Now parse the individual elements of the structure/union.
538 datatype = stab_types[strtol(tc, &tc, 10)];
540 offset = strtol(tc, &tc, 10);
542 size = strtol(tc, &tc, 10);
544 DEBUG_AddStructElement(curr_type, element_name, datatype, offset, size);
558 * Now parse the individual elements of the structure/union.
569 offset = strtol(tc, &tc, 10);
571 DEBUG_AddStructElement(curr_type, element_name, NULL, offset, 0);
583 fprintf(stderr, "Unknown type.\n");
596 static struct datatype *
597 DEBUG_ParseStabType(const char * stab)
603 * Look through the stab definition, and figure out what datatype
604 * this represents. If we have something we know about, assign the
607 c = strchr(stab, ':');
615 * The next character says more about the type (i.e. data, function, etc)
616 * of symbol. Skip it.
622 if( typenum < num_stab_types && stab_types[typenum] != NULL )
624 return stab_types[typenum];
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];
649 struct stab_nlist * stab_ptr;
652 char * subpath = NULL;
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.
666 stabbuff = (char *) xmalloc(stabbufflen);
667 if( stabbuff == NULL )
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.
685 if( strlen(stabbuff) + len > stabbufflen )
687 stabbufflen += 65536;
688 stabbuff = (char *) xrealloc(stabbuff, stabbufflen);
689 if( stabbuff == NULL )
694 strncat(stabbuff, ptr, len - 1);
697 else if( stabbuff[0] != '\0' )
699 strcat( stabbuff, ptr);
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);
714 stab_strcpy(symname, ptr);
715 DEBUG_ParseTypedefStab(ptr, symname);
718 switch(stab_ptr->n_type)
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.
730 new_addr.type = DEBUG_ParseStabType(ptr);
731 new_addr.off = load_offset + stab_ptr->n_value;
733 stab_strcpy(symname, ptr);
735 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
736 SYM_WINE | SYM_DATA | SYM_INVALID);
738 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
739 SYM_WINE | SYM_DATA );
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.
755 * These are static symbols and BSS symbols.
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 );
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));
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));
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);
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 )
807 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
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);
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
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);
852 * Don't add line number information for this function
860 * This indicates a new source file. Append the records
861 * together, to build the correct path name.
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.
872 DEBUG_Normalize(curr_func);
883 DEBUG_Normalize(curr_func);
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));
894 strcat(currpath, ptr);
896 strcpy(currpath, ptr);
903 * This indicates we are including stuff from an include file.
904 * If this is the main source, enable the debug stuff, otherwise
907 if( subpath == NULL || strcmp(ptr, subpath) == 0 )
914 DEBUG_Normalize(curr_func);
920 strtabinc = stab_ptr->n_value;
921 DEBUG_Normalize(curr_func);
926 * Ignore this. We don't care what it points to.
933 * Always ignore these. GCC doesn't even generate them.
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);
951 if( stab_types != NULL )
959 DEBUG_FreeRegisteredTypedefs();
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.
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;
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
1001 if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION )
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 )
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)) )
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 );
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);
1061 DEBUG_ProcessElfObject(char * filename, unsigned int load_offset)
1064 struct stat statbuf;
1067 char * addr = (char *) 0xffffffff;
1078 * Make sure we can stat and open this file.
1080 if( filename == NULL )
1085 status = stat(filename, &statbuf);
1092 * Now open the file, so that we can mmap() it.
1094 fd = open(filename, O_RDONLY);
1102 * Now mmap() the file.
1104 addr = mmap(0, statbuf.st_size, PROT_READ,
1105 MAP_PRIVATE, fd, 0);
1108 * Give a nice status message here...
1110 fprintf(stderr, "Loading symbols from ELF file %s...\n", filename);
1113 * Next, we need to find a few of the internal ELF headers within
1114 * this thing. We need the main executable header, and the section
1117 ehptr = (Elf32_Ehdr *) addr;
1119 if( load_offset == NULL )
1121 DEBUG_RegisterELFDebugInfo(ehptr->e_entry, statbuf.st_size, filename);
1125 DEBUG_RegisterELFDebugInfo(load_offset, statbuf.st_size, filename);
1128 spnt = (Elf32_Shdr *) (addr + ehptr->e_shoff);
1129 nsect = ehptr->e_shnum;
1130 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1132 stabsect = stabstrsect = -1;
1134 for(i=0; i < nsect; i++)
1136 if( strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0 )
1141 if( strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0 )
1147 if( stabsect == -1 || stabstrsect == -1 )
1153 * OK, now just parse all of the stabs.
1155 rtn = DEBUG_ParseStabs(addr, load_offset,
1156 spnt[stabsect].sh_offset,
1157 spnt[stabsect].sh_size,
1158 spnt[stabstrsect].sh_offset,
1159 spnt[stabstrsect].sh_size);
1166 for(i=0; i < nsect; i++)
1168 if( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1169 && (spnt[i].sh_type == SHT_SYMTAB) )
1171 DEBUG_ProcessElfSymtab(addr, load_offset,
1172 spnt + i, spnt + spnt[i].sh_link);
1175 if( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1176 && (spnt[i].sh_type == SHT_DYNSYM) )
1178 DEBUG_ProcessElfSymtab(addr, load_offset,
1179 spnt + i, spnt + spnt[i].sh_link);
1185 if( addr != (char *) 0xffffffff )
1187 munmap(addr, statbuf.st_size);
1200 DEBUG_ReadExecutableDbgInfo(void)
1205 struct r_debug * dbg_hdr;
1206 struct link_map * lpnt = NULL;
1207 extern Elf32_Dyn _DYNAMIC[];
1210 exe_name = DEBUG_argv0;
1213 * Make sure we can stat and open this file.
1215 if( exe_name == NULL )
1220 DEBUG_ProcessElfObject(exe_name, 0);
1223 * Finally walk the tables that the dynamic loader maintains to find all
1224 * of the other shared libraries which might be loaded. Perform the
1225 * same step for all of these.
1228 if( dynpnt == NULL )
1234 * Now walk the dynamic section (of the executable, looking for a DT_DEBUG
1237 for(; dynpnt->d_tag != DT_NULL; dynpnt++)
1239 if( dynpnt->d_tag == DT_DEBUG )
1245 if( (dynpnt->d_tag != DT_DEBUG)
1246 || (dynpnt->d_un.d_ptr == NULL) )
1252 * OK, now dig into the actual tables themselves.
1254 dbg_hdr = (struct r_debug *) dynpnt->d_un.d_ptr;
1255 lpnt = dbg_hdr->r_map;
1258 * Now walk the linked list. In all known ELF implementations,
1259 * the dynamic loader maintains this linked list for us. In some
1260 * cases the first entry doesn't appear with a name, in other cases it
1263 for(; lpnt; lpnt = lpnt->l_next )
1266 * We already got the stuff for the executable using the
1267 * argv[0] entry above. Here we only need to concentrate on any
1268 * shared libraries which may be loaded.
1270 ehdr = (Elf32_Ehdr *) lpnt->l_addr;
1271 if( (lpnt->l_addr == NULL) || (ehdr->e_type != ET_DYN) )
1276 if( lpnt->l_name != NULL )
1278 DEBUG_ProcessElfObject(lpnt->l_name, lpnt->l_addr);
1290 #else /* !__ELF__ */
1297 DEBUG_ReadExecutableDbgInfo(void)
1299 char * addr = (char *) 0xffffffff;
1304 unsigned int staboff;
1305 struct stat statbuf;
1307 unsigned int stroff;
1309 exe_name = DEBUG_argv0;
1312 * Make sure we can stat and open this file.
1314 if( exe_name == NULL )
1319 status = stat(exe_name, &statbuf);
1326 * Now open the file, so that we can mmap() it.
1328 fd = open(exe_name, O_RDONLY);
1336 * Now mmap() the file.
1338 addr = mmap(0, statbuf.st_size, PROT_READ,
1339 MAP_PRIVATE, fd, 0);
1341 ahdr = (struct exec *) addr;
1343 staboff = N_SYMOFF(*ahdr);
1344 stroff = N_STROFF(*ahdr);
1345 rtn = DEBUG_ParseStabs(addr, 0,
1349 statbuf.st_size - stroff);
1352 * Give a nice status message here...
1354 fprintf(stderr, "Loading symbols from a.out file %s...\n", exe_name);
1360 if( addr != (char *) 0xffffffff )
1362 munmap(addr, statbuf.st_size);
1375 * Non-linux, non-ELF platforms.
1378 DEBUG_ReadExecutableDbgInfo(void)
1384 #endif /* __ELF__ */