1 /* -*- tab-width: 8; c-basic-offset: 2 -*- */
4 * File stabs.c - read stabs information from the wine executable itself.
6 * Copyright (C) 1996, Eric Youngdale.
12 #include <sys/types.h>
15 #ifdef HAVE_SYS_MMAN_H
24 #define PATH_MAX _MAX_PATH
30 #if defined(__svr4__) || defined(__sun)
41 #elif defined(__EMX__)
78 struct stab_nlist *n_next;
84 unsigned long n_value;
88 * This is used to keep track of known datatypes so that we don't redefine
89 * them over and over again. It sucks up lots of memory otherwise.
93 struct known_typedef * next;
96 struct datatype * types[1];
99 #define NR_STAB_HASH 521
101 struct known_typedef * ktd_head[NR_STAB_HASH] = {NULL,};
103 static unsigned int stab_hash( const char * name )
105 unsigned int hash = 0;
113 hash = (hash << 4) + *p++;
115 if( (tmp = (hash & 0xf0000000)) )
121 return hash % NR_STAB_HASH;
125 static void stab_strcpy(char * dest, const char * source)
128 * A strcpy routine that stops when we hit the ':' character.
129 * Faster than copying the whole thing, and then nuking the
132 while(*source != '\0' && *source != ':')
137 extern void DEBUG_PrintAType(struct datatype*, int);
143 struct datatype** vector;
147 #define MAX_INCLUDES 256
149 static include_def* include_defs = NULL;
150 static int num_include_def = 0;
151 static int num_alloc_include_def = 0;
152 static int cu_include_stack[MAX_INCLUDES];
153 static int cu_include_stk_idx = 0;
154 static struct datatype** cu_vector = NULL;
155 static int cu_nrofentries = 0;
159 DEBUG_CreateInclude(const char* file, unsigned long val)
161 if (num_include_def == num_alloc_include_def)
163 num_alloc_include_def += 256;
164 include_defs = DBG_realloc(include_defs, sizeof(include_defs[0])*num_alloc_include_def);
165 memset(include_defs+num_include_def, 0, sizeof(include_defs[0])*256);
167 include_defs[num_include_def].name = DBG_strdup(file);
168 include_defs[num_include_def].value = val;
169 include_defs[num_include_def].vector = NULL;
170 include_defs[num_include_def].nrofentries = 0;
172 return num_include_def++;
177 DEBUG_FindInclude(const char* file, unsigned long val)
181 for (i = 0; i < num_include_def; i++)
183 if (val == include_defs[i].value &&
184 strcmp(file, include_defs[i].name) == 0)
192 DEBUG_AddInclude(int idx)
194 ++cu_include_stk_idx;
196 /* is this happen, just bump MAX_INCLUDES */
197 /* we could also handle this as another dynarray */
198 assert(cu_include_stk_idx < MAX_INCLUDES);
200 cu_include_stack[cu_include_stk_idx] = idx;
201 return cu_include_stk_idx;
206 DEBUG_ResetIncludes(void)
209 * The datatypes that we would need to use are reset when
210 * we start a new file. (at least the ones in filenr == 0
212 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
213 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
218 DEBUG_FreeIncludes(void)
222 DEBUG_ResetIncludes();
224 for (i = 0; i < num_include_def; i++)
226 DBG_free(include_defs[i].name);
227 DBG_free(include_defs[i].vector);
229 DBG_free(include_defs);
232 num_alloc_include_def = 0;
238 #define MAX_TD_NESTING 128
242 DEBUG_FileSubNr2StabEnum(int filenr, int subnr)
244 struct datatype** ret;
246 /* fprintf(stderr, "creating type id for (%d,%d)\n", filenr, subnr); */
248 /* FIXME: I could perhaps create a dummy include_def for each compilation
249 * unit which would allow not to handle those two cases separately
253 if (cu_nrofentries <= subnr)
255 cu_vector = DBG_realloc(cu_vector, sizeof(cu_vector[0])*(subnr+1));
256 memset(cu_vector+cu_nrofentries, 0, sizeof(cu_vector[0])*(subnr+1-cu_nrofentries));
257 cu_nrofentries = subnr + 1;
259 ret = &cu_vector[subnr];
265 assert(filenr <= cu_include_stk_idx);
267 idef = &include_defs[cu_include_stack[filenr]];
269 if (idef->nrofentries <= subnr)
271 idef->vector = DBG_realloc(idef->vector, sizeof(idef->vector[0])*(subnr+1));
272 memset(idef->vector + idef->nrofentries, 0, sizeof(idef->vector[0])*(subnr+1-idef->nrofentries));
273 idef->nrofentries = subnr + 1;
275 ret = &idef->vector[subnr];
277 /* fprintf(stderr,"(%d,%d) is %d\n",filenr,subnr,ret); */
283 DEBUG_ReadTypeEnumBackwards(char*x) {
290 filenr=strtol(x,&x,10); /* <int> */
292 subnr=strtol(x,&x,10); /* <int> */
295 while ((*x>='0') && (*x<='9'))
300 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
305 DEBUG_ReadTypeEnum(char **x) {
310 filenr=strtol(*x,x,10); /* <int> */
312 subnr=strtol(*x,x,10); /* <int> */
316 subnr = strtol(*x,x,10); /* <int> */
318 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
323 DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef)
326 struct known_typedef * ktd;
331 ktd = (struct known_typedef *) DBG_alloc(sizeof(struct known_typedef)
332 + (ndef - 1) * sizeof(struct datatype *));
334 hash = stab_hash(name);
336 ktd->name = DBG_strdup(name);
338 memcpy(&ktd->types[0], types, ndef * sizeof(struct datatype *));
339 ktd->next = ktd_head[hash];
340 ktd_head[hash] = ktd;
347 DEBUG_HandlePreviousTypedef(const char * name, const char * stab)
350 enum debug_type expect;
352 struct known_typedef * ktd;
355 hash = stab_hash(name);
357 for(ktd = ktd_head[hash]; ktd; ktd = ktd->next)
358 if ((ktd->name[0] == name[0]) && (strcmp(name, ktd->name) == 0) )
362 * Didn't find it. This must be a new one.
368 * Examine the stab to make sure it has the same number of definitions.
371 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
373 if( count >= ktd->ndefs )
377 * Make sure the types of all of the objects is consistent with
378 * what we have already parsed.
392 case '(': /* it's mainly a ref to another typedef, skip it */
409 fprintf(stderr, "Unknown type (%c).\n",ptr[1]);
412 if( expect != -1 && expect != DEBUG_GetType(ktd->types[count]) )
417 if( ktd->ndefs != count )
421 * Go through, dig out all of the type numbers, and substitute the
422 * appropriate things.
425 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
426 *DEBUG_ReadTypeEnumBackwards(ptr-1) = ktd->types[count++];
431 static int DEBUG_FreeRegisteredTypedefs()
435 struct known_typedef * ktd;
436 struct known_typedef * next;
439 for(j=0; j < NR_STAB_HASH; j++ )
441 for(ktd = ktd_head[j]; ktd; ktd = next)
457 DEBUG_ParseTypedefStab(char * ptr, const char * typename)
462 struct datatype * curr_type;
463 struct datatype * datatype;
464 struct datatype * curr_types[MAX_TD_NESTING];
465 char element_name[1024];
468 const char * orig_typename;
474 orig_typename = typename;
476 if( DEBUG_HandlePreviousTypedef(typename, ptr) )
480 * Go from back to front. First we go through and figure out what
481 * type numbers we need, and register those types. Then we go in
482 * and fill the details.
485 for( c = strchr(ptr, '='); c != NULL; c = strchr(c + 1, '=') )
488 * Back up until we get to a non-numeric character, to get datatype
490 struct datatype** dt = DEBUG_ReadTypeEnumBackwards(c-1);
492 if( ntypes >= MAX_TD_NESTING )
495 * If this ever happens, just bump the counter.
497 fprintf(stderr, "Typedef nesting overflow\n");
504 *dt = DEBUG_NewDataType(DT_POINTER, NULL);
505 curr_types[ntypes++] = *dt;
509 *dt = DEBUG_NewDataType(DT_STRUCT, typename);
510 curr_types[ntypes++] = *dt;
513 *dt = DEBUG_NewDataType(DT_ARRAY, NULL);
514 curr_types[ntypes++] = *dt;
517 /* will be handled in next loop,
518 * just a ref to another type
520 curr_types[ntypes++] = NULL;
524 *dt = DEBUG_NewDataType(DT_BASIC, typename);
525 curr_types[ntypes++] = *dt;
528 stab_strcpy(element_name, c + 3);
529 *dt = DEBUG_NewDataType(DT_STRUCT, element_name);
530 curr_types[ntypes++] = *dt;
533 *dt = DEBUG_NewDataType(DT_ENUM, NULL);
534 curr_types[ntypes++] = *dt;
537 *dt = DEBUG_NewDataType(DT_FUNC, NULL);
538 curr_types[ntypes++] = *dt;
541 fprintf(stderr, "Unknown type (%c).\n",c[1]);
549 * OK, now take a second sweep through. Now we will be digging
550 * out the definitions of the various components, and storing
551 * them in the skeletons that we have already allocated. We take
552 * a right-to left search as this is much easier to parse.
554 for( c = strrchr(ptr, '='); c != NULL; c = strrchr(ptr, '=') )
556 struct datatype** dt = DEBUG_ReadTypeEnumBackwards(c-1);
557 struct datatype** dt2;
578 datatype = *DEBUG_ReadTypeEnum(&tc);
579 DEBUG_SetPointerType(curr_type, datatype);
587 dt2 = DEBUG_ReadTypeEnum(&tc);
593 else if (!*dt && !*dt2)
595 /* this should be a basic type, define it */
596 *dt2 = *dt = DEBUG_NewDataType(DT_BASIC, typename);
600 fprintf(stderr, "Unknown condition %p %p (%s)\n", *dt, *dt2, ptr);
606 curr_types[ntp--] = *dt;
612 * We have already handled these above.
618 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */
622 DEBUG_ReadTypeEnum(&tc);
624 arrmin = strtol(tc, &tc, 10); /* <int> */
626 arrmax = strtol(tc, &tc, 10); /* <int> */
628 datatype = *DEBUG_ReadTypeEnum(&tc); /* <typeinfo> */
633 DEBUG_SetArrayParams(curr_type, arrmin, arrmax, datatype);
641 if( DEBUG_SetStructSize(curr_type, strtol(tc, &tc, 10)) == FALSE )
644 * We have already filled out this structure. Nothing to do,
645 * so just skip forward to the end of the definition.
647 while( tc[0] != ';' && tc[1] != ';' )
660 * Now parse the individual elements of the structure/union.
671 datatype = *DEBUG_ReadTypeEnum(&tc);
674 offset = strtol(tc, &tc, 10);
676 size = strtol(tc, &tc, 10);
679 DEBUG_AddStructElement(curr_type, element_name, datatype,
684 /* ... but proceed parsing to the end of the stab */
685 fprintf(stderr, "failure on %s %s\n", ptr, ti);
692 /* if we had a undeclared value this one is undeclared too.
693 * remove it from the stab_types.
694 * I just set it to NULL to detect bugs in my thoughtprocess.
695 * FIXME: leaks the memory for the structure elements.
696 * FIXME: such structures should have been optimized away
710 * Now parse the individual elements of the structure/union.
719 offset = strtol(tc, &tc, 10);
721 DEBUG_AddStructElement(curr_type, element_name, NULL, offset, 0);
729 fprintf(stderr, "Unknown type (%c).\n",c[1]);
734 * Now register the type so that if we encounter it again, we will know
737 DEBUG_RegisterTypedef(orig_typename, curr_types, ntypes);
742 static struct datatype *
743 DEBUG_ParseStabType(const char * stab)
748 * Look through the stab definition, and figure out what datatype
749 * this represents. If we have something we know about, assign the
752 c = strchr(stab, ':');
758 * The next character says more about the type (i.e. data, function, etc)
759 * of symbol. Skip it.
764 * The next is either an integer or a (integer,integer).
765 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
767 return *DEBUG_ReadTypeEnum(&c);
771 DEBUG_ParseStabs(char * addr, unsigned int load_offset,
772 unsigned int staboff, int stablen,
773 unsigned int strtaboff, int strtablen)
775 struct name_hash * curr_func = NULL;
776 struct wine_locals * curr_loc = NULL;
777 struct name_hash * curr_sym = NULL;
778 char currpath[PATH_MAX];
788 struct stab_nlist * stab_ptr;
791 char * subpath = NULL;
794 nstab = stablen / sizeof(struct stab_nlist);
795 stab_ptr = (struct stab_nlist *) (addr + staboff);
796 strs = (char *) (addr + strtaboff);
798 memset(currpath, 0, sizeof(currpath));
801 * Allocate a buffer into which we can build stab strings for cases
802 * where the stab is continued over multiple lines.
805 stabbuff = (char *) DBG_alloc(stabbufflen);
809 for(i=0; i < nstab; i++, stab_ptr++ )
811 ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
812 if( ptr[strlen(ptr) - 1] == '\\' )
815 * Indicates continuation. Append this to the buffer, and go onto the
816 * next record. Repeat the process until we find a stab without the
817 * '/' character, as this indicates we have the whole thing.
820 if( strlen(stabbuff) + len > stabbufflen )
822 stabbufflen += 65536;
823 stabbuff = (char *) DBG_realloc(stabbuff, stabbufflen);
825 strncat(stabbuff, ptr, len - 1);
828 else if( stabbuff[0] != '\0' )
830 strcat( stabbuff, ptr);
834 if( strchr(ptr, '=') != NULL )
837 * The stabs aren't in writable memory, so copy it over so we are
838 * sure we can scribble on it.
840 if( ptr != stabbuff )
842 strcpy(stabbuff, ptr);
845 stab_strcpy(symname, ptr);
846 DEBUG_ParseTypedefStab(ptr, symname);
849 switch(stab_ptr->n_type)
853 * These are useless with ELF. They have no value, and you have to
854 * read the normal symbol table to get the address. Thus we
855 * ignore them, and when we process the normal symbol table
856 * we should do the right thing.
858 * With a.out, they actually do make some amount of sense.
861 new_addr.type = DEBUG_ParseStabType(ptr);
862 new_addr.off = load_offset + stab_ptr->n_value;
864 stab_strcpy(symname, ptr);
866 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
867 SYM_WINE | SYM_DATA | SYM_INVALID);
869 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
870 SYM_WINE | SYM_DATA );
876 * We need to keep track of these so we get symbol scoping
877 * right for local variables. For now, we just ignore them.
878 * The hooks are already there for dealing with this however,
879 * so all we need to do is to keep count of the nesting level,
880 * and find the RBRAC for each matching LBRAC.
886 * These are static symbols and BSS symbols.
889 new_addr.type = DEBUG_ParseStabType(ptr);
890 new_addr.off = load_offset + stab_ptr->n_value;
892 stab_strcpy(symname, ptr);
893 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
894 SYM_WINE | SYM_DATA );
898 * These are function parameters.
900 if( (curr_func != NULL)
901 && (stab_ptr->n_value != 0) )
903 stab_strcpy(symname, ptr);
904 curr_loc = DEBUG_AddLocal( curr_func, 0,
905 stab_ptr->n_value, 0, 0, symname );
906 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
910 if( curr_func != NULL )
912 stab_strcpy(symname, ptr);
913 curr_loc = DEBUG_AddLocal( curr_func, stab_ptr->n_value,
915 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
919 if( (curr_func != NULL)
920 && (stab_ptr->n_value != 0) )
922 stab_strcpy(symname, ptr);
923 curr_loc = DEBUG_AddLocal( curr_func, 0,
924 stab_ptr->n_value, 0, 0, symname );
925 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
927 else if (curr_func == NULL)
929 stab_strcpy(symname, ptr);
934 * This is a line number. These are always relative to the start
935 * of the function (N_FUN), and this makes the lookup easier.
937 if( curr_func != NULL )
940 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
945 * This isn't right. The order of the stabs is different under
946 * a.out, and as a result we would end up attaching the line
947 * number to the wrong function.
949 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
950 stab_ptr->n_value - curr_func->addr.off);
957 * First, clean up the previous function we were working on.
959 DEBUG_Normalize(curr_func);
962 * For now, just declare the various functions. Later
963 * on, we will add the line number information and the
969 new_addr.type = DEBUG_ParseStabType(ptr);
970 new_addr.off = load_offset + stab_ptr->n_value;
972 * Copy the string to a temp buffer so we
973 * can kill everything after the ':'. We do
974 * it this way because otherwise we end up dirtying
975 * all of the pages related to the stabs, and that
976 * sucks up swap space like crazy.
978 stab_strcpy(symname, ptr);
979 curr_func = DEBUG_AddSymbol( symname, &new_addr, currpath,
980 SYM_WINE | SYM_FUNC);
985 * Don't add line number information for this function
993 * This indicates a new source file. Append the records
994 * together, to build the correct path name.
998 * With a.out, there is no NULL string N_SO entry at the end of
999 * the file. Thus when we find non-consecutive entries,
1000 * we consider that a new file is started.
1002 if( last_nso < i-1 )
1005 DEBUG_Normalize(curr_func);
1016 DEBUG_Normalize(curr_func);
1022 strcat(currpath, ptr);
1024 strcpy(currpath, ptr);
1026 DEBUG_ResetIncludes();
1032 * This indicates we are including stuff from an include file.
1033 * If this is the main source, enable the debug stuff, otherwise
1036 if( subpath == NULL || strcmp(ptr, subpath) == 0 )
1043 DEBUG_Normalize(curr_func);
1049 strtabinc = stab_ptr->n_value;
1050 DEBUG_Normalize(curr_func);
1055 * Ignore this. We don't care what it points to.
1059 DEBUG_AddInclude(DEBUG_CreateInclude(ptr, stab_ptr->n_value));
1064 DEBUG_AddInclude(DEBUG_FindInclude(ptr, stab_ptr->n_value));
1068 * Always ignore these. GCC doesn't even generate them.
1072 fprintf(stderr, "Unkown stab type 0x%02x\n", stab_ptr->n_type);
1079 fprintf(stderr, "%d %x %s\n", stab_ptr->n_type,
1080 (unsigned int) stab_ptr->n_value,
1081 strs + (unsigned int) stab_ptr->n_un.n_name);
1085 DEBUG_FreeRegisteredTypedefs();
1086 DEBUG_FreeIncludes();
1094 * Walk through the entire symbol table and add any symbols we find there.
1095 * This can be used in cases where we have stripped ELF shared libraries,
1096 * or it can be used in cases where we have data symbols for which the address
1097 * isn't encoded in the stabs.
1099 * This is all really quite easy, since we don't have to worry about line
1100 * numbers or local data variables.
1104 DEBUG_ProcessElfSymtab(char * addr, unsigned int load_offset,
1105 Elf32_Shdr * symtab, Elf32_Shdr * strtab)
1107 char * curfile = NULL;
1108 struct name_hash * curr_sym = NULL;
1118 symp = (Elf32_Sym *) (addr + symtab->sh_offset);
1119 nsym = symtab->sh_size / sizeof(*symp);
1120 strp = (char *) (addr + strtab->sh_offset);
1122 for(i=0; i < nsym; i++, symp++)
1125 * Ignore certain types of entries which really aren't of that much
1128 if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION )
1133 symname = strp + symp->st_name;
1136 * Save the name of the current file, so we have a way of tracking
1137 * static functions/data.
1139 if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
1147 * See if we already have something for this symbol.
1148 * If so, ignore this entry, because it would have come from the
1149 * stabs or from a previous symbol. If the value is different,
1150 * we will have to keep the darned thing, because there can be
1151 * multiple local symbols by the same name.
1153 if( (DEBUG_GetSymbolValue(symname, -1, &new_addr, FALSE ) == TRUE)
1154 && (new_addr.off == (load_offset + symp->st_value)) )
1158 new_addr.type = NULL;
1159 new_addr.off = load_offset + symp->st_value;
1160 flags = SYM_WINE | (ELF32_ST_BIND(symp->st_info) == STT_FUNC
1161 ? SYM_FUNC : SYM_DATA);
1162 if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
1163 curr_sym = DEBUG_AddSymbol( symname, &new_addr, NULL, flags );
1165 curr_sym = DEBUG_AddSymbol( symname, &new_addr, curfile, flags );
1168 * Record the size of the symbol. This can come in handy in
1169 * some cases. Not really used yet, however.
1171 if( symp->st_size != 0 )
1172 DEBUG_SetSymbolSize(curr_sym, symp->st_size);
1180 DEBUG_ProcessElfObject(char * filename, unsigned int load_offset)
1183 struct stat statbuf;
1186 char * addr = (char *) 0xffffffff;
1197 * Make sure we can stat and open this file.
1199 if( filename == NULL )
1202 status = stat(filename, &statbuf);
1205 char *s,*t,*fn,*paths;
1206 if (strchr(filename,'/'))
1208 paths = DBG_strdup(getenv("PATH"));
1213 fn = (char*)DBG_alloc(strlen(filename)+1+strlen(s)+1);
1216 strcat(fn,filename);
1217 if ((rtn = DEBUG_ProcessElfObject(fn,load_offset))) {
1223 if (t) s = t+1; else break;
1225 if (!s || !*s) fprintf(stderr," not found");
1231 * Now open the file, so that we can mmap() it.
1233 fd = open(filename, O_RDONLY);
1239 * Now mmap() the file.
1241 addr = mmap(0, statbuf.st_size, PROT_READ,
1242 MAP_PRIVATE, fd, 0);
1243 if( addr == (char *) 0xffffffff )
1247 * Next, we need to find a few of the internal ELF headers within
1248 * this thing. We need the main executable header, and the section
1251 ehptr = (Elf32_Ehdr *) addr;
1253 if( load_offset == 0 )
1254 DEBUG_RegisterELFDebugInfo(ehptr->e_entry, statbuf.st_size, filename);
1256 DEBUG_RegisterELFDebugInfo(load_offset, statbuf.st_size, filename);
1258 spnt = (Elf32_Shdr *) (addr + ehptr->e_shoff);
1259 nsect = ehptr->e_shnum;
1260 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1262 stabsect = stabstrsect = -1;
1264 for(i=0; i < nsect; i++)
1266 if( strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0 )
1269 if( strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0 )
1273 if( stabsect == -1 || stabstrsect == -1 )
1277 * OK, now just parse all of the stabs.
1279 rtn = DEBUG_ParseStabs(addr, load_offset,
1280 spnt[stabsect].sh_offset,
1281 spnt[stabsect].sh_size,
1282 spnt[stabstrsect].sh_offset,
1283 spnt[stabstrsect].sh_size);
1288 for(i=0; i < nsect; i++)
1290 if( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1291 && (spnt[i].sh_type == SHT_SYMTAB) )
1292 DEBUG_ProcessElfSymtab(addr, load_offset,
1293 spnt + i, spnt + spnt[i].sh_link);
1295 if( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1296 && (spnt[i].sh_type == SHT_DYNSYM) )
1297 DEBUG_ProcessElfSymtab(addr, load_offset,
1298 spnt + i, spnt + spnt[i].sh_link);
1303 if( addr != (char *) 0xffffffff )
1304 munmap(addr, statbuf.st_size);
1314 DEBUG_ReadExecutableDbgInfo(void)
1317 const char * exe_name;
1319 struct r_debug * dbg_hdr;
1320 struct link_map * lpnt = NULL;
1322 extern Elf32_Dyn _DYNAMIC[] __attribute__ ((weak));
1324 extern Elf32_Dyn _DYNAMIC[];
1332 * Make sure we can stat and open this file.
1334 if( exe_name == NULL )
1337 fprintf( stderr, "Loading symbols: %s", exe_name );
1338 rowcount = 17 + strlen(exe_name);
1339 DEBUG_ProcessElfObject(exe_name, 0);
1342 * Finally walk the tables that the dynamic loader maintains to find all
1343 * of the other shared libraries which might be loaded. Perform the
1344 * same step for all of these.
1346 if( (&_DYNAMIC == NULL) || (_DYNAMIC == NULL) )
1352 * Now walk the dynamic section (of the executable, looking for a DT_DEBUG
1355 for(; dynpnt->d_tag != DT_NULL; dynpnt++)
1356 if( dynpnt->d_tag == DT_DEBUG )
1359 if( (dynpnt->d_tag != DT_DEBUG)
1360 || (dynpnt->d_un.d_ptr == 0) )
1364 * OK, now dig into the actual tables themselves.
1366 dbg_hdr = (struct r_debug *) dynpnt->d_un.d_ptr;
1367 lpnt = dbg_hdr->r_map;
1370 * Now walk the linked list. In all known ELF implementations,
1371 * the dynamic loader maintains this linked list for us. In some
1372 * cases the first entry doesn't appear with a name, in other cases it
1375 for(; lpnt; lpnt = lpnt->l_next )
1378 * We already got the stuff for the executable using the
1379 * argv[0] entry above. Here we only need to concentrate on any
1380 * shared libraries which may be loaded.
1382 ehdr = (Elf32_Ehdr *) lpnt->l_addr;
1383 if( (lpnt->l_addr == 0) || (ehdr->e_type != ET_DYN) )
1386 if( lpnt->l_name != NULL )
1388 if (rowcount + strlen(lpnt->l_name) > 76)
1390 fprintf( stderr, "\n " );
1393 fprintf( stderr, " %s", lpnt->l_name );
1394 rowcount += strlen(lpnt->l_name) + 1;
1395 DEBUG_ProcessElfObject(lpnt->l_name, lpnt->l_addr);
1402 fprintf( stderr, "\n" );
1407 #else /* !__ELF__ */
1414 DEBUG_ReadExecutableDbgInfo(void)
1416 char * addr = (char *) 0xffffffff;
1421 unsigned int staboff;
1422 struct stat statbuf;
1424 unsigned int stroff;
1429 * Make sure we can stat and open this file.
1431 if( exe_name == NULL )
1434 status = stat(exe_name, &statbuf);
1439 * Now open the file, so that we can mmap() it.
1441 fd = open(exe_name, O_RDONLY);
1447 * Now mmap() the file.
1449 addr = mmap(0, statbuf.st_size, PROT_READ,
1450 MAP_PRIVATE, fd, 0);
1451 if( addr == (char *) 0xffffffff )
1454 ahdr = (struct exec *) addr;
1456 staboff = N_SYMOFF(*ahdr);
1457 stroff = N_STROFF(*ahdr);
1458 rtn = DEBUG_ParseStabs(addr, 0,
1462 statbuf.st_size - stroff);
1465 * Give a nice status message here...
1467 fprintf( stderr, "Loading symbols: %s", exe_name );
1473 if( addr != (char *) 0xffffffff )
1474 munmap(addr, statbuf.st_size);
1484 * Non-linux, non-ELF platforms.
1487 DEBUG_ReadExecutableDbgInfo(void)
1493 #endif /* __ELF__ */