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 != ':')
141 struct datatype** vector;
145 #define MAX_INCLUDES 256
147 static include_def* include_defs = NULL;
148 static int num_include_def = 0;
149 static int num_alloc_include_def = 0;
150 static int cu_include_stack[MAX_INCLUDES];
151 static int cu_include_stk_idx = 0;
152 static struct datatype** cu_vector = NULL;
153 static int cu_nrofentries = 0;
157 DEBUG_CreateInclude(const char* file, unsigned long val)
159 if (num_include_def == num_alloc_include_def)
161 num_alloc_include_def += 256;
162 include_defs = DBG_realloc(include_defs, sizeof(include_defs[0])*num_alloc_include_def);
163 memset(include_defs+num_include_def, 0, sizeof(include_defs[0])*256);
165 include_defs[num_include_def].name = DBG_strdup(file);
166 include_defs[num_include_def].value = val;
167 include_defs[num_include_def].vector = NULL;
168 include_defs[num_include_def].nrofentries = 0;
170 return num_include_def++;
175 DEBUG_FindInclude(const char* file, unsigned long val)
179 for (i = 0; i < num_include_def; i++)
181 if (val == include_defs[i].value &&
182 strcmp(file, include_defs[i].name) == 0)
190 DEBUG_AddInclude(int idx)
192 ++cu_include_stk_idx;
194 /* is this happen, just bump MAX_INCLUDES */
195 /* we could also handle this as another dynarray */
196 assert(cu_include_stk_idx < MAX_INCLUDES);
198 cu_include_stack[cu_include_stk_idx] = idx;
199 return cu_include_stk_idx;
204 DEBUG_ResetIncludes(void)
207 * The datatypes that we would need to use are reset when
208 * we start a new file. (at least the ones in filenr == 0
210 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
211 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
216 DEBUG_FreeIncludes(void)
220 DEBUG_ResetIncludes();
222 for (i = 0; i < num_include_def; i++)
224 DBG_free(include_defs[i].name);
225 DBG_free(include_defs[i].vector);
227 DBG_free(include_defs);
230 num_alloc_include_def = 0;
236 #define MAX_TD_NESTING 128
240 DEBUG_FileSubNr2StabEnum(int filenr, int subnr)
242 struct datatype** ret;
244 /* fprintf(stderr, "creating type id for (%d,%d)\n", filenr, subnr); */
246 /* FIXME: I could perhaps create a dummy include_def for each compilation
247 * unit which would allow not to handle those two cases separately
251 if (cu_nrofentries <= subnr)
253 cu_vector = DBG_realloc(cu_vector, sizeof(cu_vector[0])*(subnr+1));
254 memset(cu_vector+cu_nrofentries, 0, sizeof(cu_vector[0])*(subnr+1-cu_nrofentries));
255 cu_nrofentries = subnr + 1;
257 ret = &cu_vector[subnr];
263 assert(filenr <= cu_include_stk_idx);
265 idef = &include_defs[cu_include_stack[filenr]];
267 if (idef->nrofentries <= subnr)
269 idef->vector = DBG_realloc(idef->vector, sizeof(idef->vector[0])*(subnr+1));
270 memset(idef->vector + idef->nrofentries, 0, sizeof(idef->vector[0])*(subnr+1-idef->nrofentries));
271 idef->nrofentries = subnr + 1;
273 ret = &idef->vector[subnr];
275 /* fprintf(stderr,"(%d,%d) is %d\n",filenr,subnr,ret); */
281 DEBUG_ReadTypeEnumBackwards(char*x) {
288 filenr=strtol(x,&x,10); /* <int> */
290 subnr=strtol(x,&x,10); /* <int> */
293 while ((*x>='0') && (*x<='9'))
298 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
303 DEBUG_ReadTypeEnum(char **x) {
308 filenr=strtol(*x,x,10); /* <int> */
310 subnr=strtol(*x,x,10); /* <int> */
314 subnr = strtol(*x,x,10); /* <int> */
316 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
321 DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef)
324 struct known_typedef * ktd;
329 ktd = (struct known_typedef *) DBG_alloc(sizeof(struct known_typedef)
330 + (ndef - 1) * sizeof(struct datatype *));
332 hash = stab_hash(name);
334 ktd->name = DBG_strdup(name);
336 memcpy(&ktd->types[0], types, ndef * sizeof(struct datatype *));
337 ktd->next = ktd_head[hash];
338 ktd_head[hash] = ktd;
345 DEBUG_HandlePreviousTypedef(const char * name, const char * stab)
348 enum debug_type expect;
350 struct known_typedef * ktd;
353 hash = stab_hash(name);
355 for(ktd = ktd_head[hash]; ktd; ktd = ktd->next)
356 if ((ktd->name[0] == name[0]) && (strcmp(name, ktd->name) == 0) )
360 * Didn't find it. This must be a new one.
366 * Examine the stab to make sure it has the same number of definitions.
369 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
371 if( count >= ktd->ndefs )
375 * Make sure the types of all of the objects is consistent with
376 * what we have already parsed.
390 case '(': /* it's mainly a ref to another typedef, skip it */
407 fprintf(stderr, "Unknown type (%c).\n",ptr[1]);
410 if( expect != -1 && expect != DEBUG_GetType(ktd->types[count]) )
415 if( ktd->ndefs != count )
419 * Go through, dig out all of the type numbers, and substitute the
420 * appropriate things.
423 for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
424 *DEBUG_ReadTypeEnumBackwards(ptr-1) = ktd->types[count++];
429 static int DEBUG_FreeRegisteredTypedefs()
433 struct known_typedef * ktd;
434 struct known_typedef * next;
437 for(j=0; j < NR_STAB_HASH; j++ )
439 for(ktd = ktd_head[j]; ktd; ktd = next)
455 DEBUG_ParseTypedefStab(char * ptr, const char * typename)
460 struct datatype * curr_type;
461 struct datatype * datatype;
462 struct datatype * curr_types[MAX_TD_NESTING];
463 char element_name[1024];
466 const char * orig_typename;
472 orig_typename = typename;
474 if( DEBUG_HandlePreviousTypedef(typename, ptr) )
478 * Go from back to front. First we go through and figure out what
479 * type numbers we need, and register those types. Then we go in
480 * and fill the details.
483 for( c = strchr(ptr, '='); c != NULL; c = strchr(c + 1, '=') )
486 * Back up until we get to a non-numeric character, to get datatype
488 struct datatype** dt = DEBUG_ReadTypeEnumBackwards(c-1);
490 if( ntypes >= MAX_TD_NESTING )
493 * If this ever happens, just bump the counter.
495 fprintf(stderr, "Typedef nesting overflow\n");
502 *dt = DEBUG_NewDataType(DT_POINTER, NULL);
503 curr_types[ntypes++] = *dt;
507 *dt = DEBUG_NewDataType(DT_STRUCT, typename);
508 curr_types[ntypes++] = *dt;
511 *dt = DEBUG_NewDataType(DT_ARRAY, NULL);
512 curr_types[ntypes++] = *dt;
515 /* will be handled in next loop,
516 * just a ref to another type
518 curr_types[ntypes++] = NULL;
522 *dt = DEBUG_NewDataType(DT_BASIC, typename);
523 curr_types[ntypes++] = *dt;
526 stab_strcpy(element_name, c + 3);
527 *dt = DEBUG_NewDataType(DT_STRUCT, element_name);
528 curr_types[ntypes++] = *dt;
531 *dt = DEBUG_NewDataType(DT_ENUM, NULL);
532 curr_types[ntypes++] = *dt;
535 *dt = DEBUG_NewDataType(DT_FUNC, NULL);
536 curr_types[ntypes++] = *dt;
539 fprintf(stderr, "Unknown type (%c).\n",c[1]);
547 * OK, now take a second sweep through. Now we will be digging
548 * out the definitions of the various components, and storing
549 * them in the skeletons that we have already allocated. We take
550 * a right-to left search as this is much easier to parse.
552 for( c = strrchr(ptr, '='); c != NULL; c = strrchr(ptr, '=') )
554 struct datatype** dt = DEBUG_ReadTypeEnumBackwards(c-1);
555 struct datatype** dt2;
576 datatype = *DEBUG_ReadTypeEnum(&tc);
577 DEBUG_SetPointerType(curr_type, datatype);
585 dt2 = DEBUG_ReadTypeEnum(&tc);
591 else if (!*dt && !*dt2)
593 /* this should be a basic type, define it */
594 *dt2 = *dt = DEBUG_NewDataType(DT_BASIC, typename);
598 fprintf(stderr, "Unknown condition %p %p (%s)\n", *dt, *dt2, ptr);
604 curr_types[ntp--] = *dt;
610 * We have already handled these above.
616 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */
620 DEBUG_ReadTypeEnum(&tc);
622 arrmin = strtol(tc, &tc, 10); /* <int> */
624 arrmax = strtol(tc, &tc, 10); /* <int> */
626 datatype = *DEBUG_ReadTypeEnum(&tc); /* <typeinfo> */
631 DEBUG_SetArrayParams(curr_type, arrmin, arrmax, datatype);
639 if( DEBUG_SetStructSize(curr_type, strtol(tc, &tc, 10)) == FALSE )
642 * We have already filled out this structure. Nothing to do,
643 * so just skip forward to the end of the definition.
645 while( tc[0] != ';' && tc[1] != ';' )
658 * Now parse the individual elements of the structure/union.
669 datatype = *DEBUG_ReadTypeEnum(&tc);
672 offset = strtol(tc, &tc, 10);
674 size = strtol(tc, &tc, 10);
677 DEBUG_AddStructElement(curr_type, element_name, datatype,
682 /* ... but proceed parsing to the end of the stab */
683 fprintf(stderr, "failure on %s %s\n", ptr, ti);
690 /* if we had a undeclared value this one is undeclared too.
691 * remove it from the stab_types.
692 * I just set it to NULL to detect bugs in my thoughtprocess.
693 * FIXME: leaks the memory for the structure elements.
694 * FIXME: such structures should have been optimized away
708 * Now parse the individual elements of the structure/union.
717 offset = strtol(tc, &tc, 10);
719 DEBUG_AddStructElement(curr_type, element_name, NULL, offset, 0);
727 fprintf(stderr, "Unknown type (%c).\n",c[1]);
732 * Now register the type so that if we encounter it again, we will know
735 DEBUG_RegisterTypedef(orig_typename, curr_types, ntypes);
740 static struct datatype *
741 DEBUG_ParseStabType(const char * stab)
746 * Look through the stab definition, and figure out what datatype
747 * this represents. If we have something we know about, assign the
750 c = strchr(stab, ':');
756 * The next character says more about the type (i.e. data, function, etc)
757 * of symbol. Skip it.
762 * The next is either an integer or a (integer,integer).
763 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
765 return *DEBUG_ReadTypeEnum(&c);
769 DEBUG_ParseStabs(char * addr, unsigned int load_offset,
770 unsigned int staboff, int stablen,
771 unsigned int strtaboff, int strtablen)
773 struct name_hash * curr_func = NULL;
774 struct wine_locals * curr_loc = NULL;
775 struct name_hash * curr_sym = NULL;
776 char currpath[PATH_MAX];
786 struct stab_nlist * stab_ptr;
789 char * subpath = NULL;
792 nstab = stablen / sizeof(struct stab_nlist);
793 stab_ptr = (struct stab_nlist *) (addr + staboff);
794 strs = (char *) (addr + strtaboff);
796 memset(currpath, 0, sizeof(currpath));
799 * Allocate a buffer into which we can build stab strings for cases
800 * where the stab is continued over multiple lines.
803 stabbuff = (char *) DBG_alloc(stabbufflen);
807 for(i=0; i < nstab; i++, stab_ptr++ )
809 ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
810 if( ptr[strlen(ptr) - 1] == '\\' )
813 * Indicates continuation. Append this to the buffer, and go onto the
814 * next record. Repeat the process until we find a stab without the
815 * '/' character, as this indicates we have the whole thing.
818 if( strlen(stabbuff) + len > stabbufflen )
820 stabbufflen += 65536;
821 stabbuff = (char *) DBG_realloc(stabbuff, stabbufflen);
823 strncat(stabbuff, ptr, len - 1);
826 else if( stabbuff[0] != '\0' )
828 strcat( stabbuff, ptr);
832 if( strchr(ptr, '=') != NULL )
835 * The stabs aren't in writable memory, so copy it over so we are
836 * sure we can scribble on it.
838 if( ptr != stabbuff )
840 strcpy(stabbuff, ptr);
843 stab_strcpy(symname, ptr);
844 DEBUG_ParseTypedefStab(ptr, symname);
847 switch(stab_ptr->n_type)
851 * These are useless with ELF. They have no value, and you have to
852 * read the normal symbol table to get the address. Thus we
853 * ignore them, and when we process the normal symbol table
854 * we should do the right thing.
856 * With a.out, they actually do make some amount of sense.
859 new_addr.type = DEBUG_ParseStabType(ptr);
860 new_addr.off = load_offset + stab_ptr->n_value;
862 stab_strcpy(symname, ptr);
864 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
865 SYM_WINE | SYM_DATA | SYM_INVALID);
867 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
868 SYM_WINE | SYM_DATA );
874 * We need to keep track of these so we get symbol scoping
875 * right for local variables. For now, we just ignore them.
876 * The hooks are already there for dealing with this however,
877 * so all we need to do is to keep count of the nesting level,
878 * and find the RBRAC for each matching LBRAC.
884 * These are static symbols and BSS symbols.
887 new_addr.type = DEBUG_ParseStabType(ptr);
888 new_addr.off = load_offset + stab_ptr->n_value;
890 stab_strcpy(symname, ptr);
891 curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
892 SYM_WINE | SYM_DATA );
896 * These are function parameters.
898 if( (curr_func != NULL)
899 && (stab_ptr->n_value != 0) )
901 stab_strcpy(symname, ptr);
902 curr_loc = DEBUG_AddLocal( curr_func, 0,
903 stab_ptr->n_value, 0, 0, symname );
904 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
908 if( curr_func != NULL )
910 stab_strcpy(symname, ptr);
911 curr_loc = DEBUG_AddLocal( curr_func, stab_ptr->n_value,
913 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
917 if( (curr_func != NULL)
918 && (stab_ptr->n_value != 0) )
920 stab_strcpy(symname, ptr);
921 curr_loc = DEBUG_AddLocal( curr_func, 0,
922 stab_ptr->n_value, 0, 0, symname );
923 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
925 else if (curr_func == NULL)
927 stab_strcpy(symname, ptr);
932 * This is a line number. These are always relative to the start
933 * of the function (N_FUN), and this makes the lookup easier.
935 if( curr_func != NULL )
938 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
943 * This isn't right. The order of the stabs is different under
944 * a.out, and as a result we would end up attaching the line
945 * number to the wrong function.
947 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
948 stab_ptr->n_value - curr_func->addr.off);
955 * First, clean up the previous function we were working on.
957 DEBUG_Normalize(curr_func);
960 * For now, just declare the various functions. Later
961 * on, we will add the line number information and the
967 new_addr.type = DEBUG_ParseStabType(ptr);
968 new_addr.off = load_offset + stab_ptr->n_value;
970 * Copy the string to a temp buffer so we
971 * can kill everything after the ':'. We do
972 * it this way because otherwise we end up dirtying
973 * all of the pages related to the stabs, and that
974 * sucks up swap space like crazy.
976 stab_strcpy(symname, ptr);
977 curr_func = DEBUG_AddSymbol( symname, &new_addr, currpath,
978 SYM_WINE | SYM_FUNC);
983 * Don't add line number information for this function
991 * This indicates a new source file. Append the records
992 * together, to build the correct path name.
996 * With a.out, there is no NULL string N_SO entry at the end of
997 * the file. Thus when we find non-consecutive entries,
998 * we consider that a new file is started.
1000 if( last_nso < i-1 )
1003 DEBUG_Normalize(curr_func);
1014 DEBUG_Normalize(curr_func);
1020 strcat(currpath, ptr);
1022 strcpy(currpath, ptr);
1024 DEBUG_ResetIncludes();
1030 * This indicates we are including stuff from an include file.
1031 * If this is the main source, enable the debug stuff, otherwise
1034 if( subpath == NULL || strcmp(ptr, subpath) == 0 )
1041 DEBUG_Normalize(curr_func);
1047 strtabinc = stab_ptr->n_value;
1048 DEBUG_Normalize(curr_func);
1053 * Ignore this. We don't care what it points to.
1057 DEBUG_AddInclude(DEBUG_CreateInclude(ptr, stab_ptr->n_value));
1062 DEBUG_AddInclude(DEBUG_FindInclude(ptr, stab_ptr->n_value));
1066 * Always ignore these. GCC doesn't even generate them.
1070 fprintf(stderr, "Unkown stab type 0x%02x\n", stab_ptr->n_type);
1077 fprintf(stderr, "%d %x %s\n", stab_ptr->n_type,
1078 (unsigned int) stab_ptr->n_value,
1079 strs + (unsigned int) stab_ptr->n_un.n_name);
1083 DEBUG_FreeRegisteredTypedefs();
1084 DEBUG_FreeIncludes();
1092 * Walk through the entire symbol table and add any symbols we find there.
1093 * This can be used in cases where we have stripped ELF shared libraries,
1094 * or it can be used in cases where we have data symbols for which the address
1095 * isn't encoded in the stabs.
1097 * This is all really quite easy, since we don't have to worry about line
1098 * numbers or local data variables.
1102 DEBUG_ProcessElfSymtab(char * addr, unsigned int load_offset,
1103 Elf32_Shdr * symtab, Elf32_Shdr * strtab)
1105 char * curfile = NULL;
1106 struct name_hash * curr_sym = NULL;
1116 symp = (Elf32_Sym *) (addr + symtab->sh_offset);
1117 nsym = symtab->sh_size / sizeof(*symp);
1118 strp = (char *) (addr + strtab->sh_offset);
1120 for(i=0; i < nsym; i++, symp++)
1123 * Ignore certain types of entries which really aren't of that much
1126 if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION )
1131 symname = strp + symp->st_name;
1134 * Save the name of the current file, so we have a way of tracking
1135 * static functions/data.
1137 if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
1145 * See if we already have something for this symbol.
1146 * If so, ignore this entry, because it would have come from the
1147 * stabs or from a previous symbol. If the value is different,
1148 * we will have to keep the darned thing, because there can be
1149 * multiple local symbols by the same name.
1151 if( (DEBUG_GetSymbolValue(symname, -1, &new_addr, FALSE ) == TRUE)
1152 && (new_addr.off == (load_offset + symp->st_value)) )
1156 new_addr.type = NULL;
1157 new_addr.off = load_offset + symp->st_value;
1158 flags = SYM_WINE | (ELF32_ST_BIND(symp->st_info) == STT_FUNC
1159 ? SYM_FUNC : SYM_DATA);
1160 if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
1161 curr_sym = DEBUG_AddSymbol( symname, &new_addr, NULL, flags );
1163 curr_sym = DEBUG_AddSymbol( symname, &new_addr, curfile, flags );
1166 * Record the size of the symbol. This can come in handy in
1167 * some cases. Not really used yet, however.
1169 if( symp->st_size != 0 )
1170 DEBUG_SetSymbolSize(curr_sym, symp->st_size);
1178 DEBUG_ProcessElfObject(const char * filename, unsigned int load_offset)
1181 struct stat statbuf;
1184 char * addr = (char *) 0xffffffff;
1195 * Make sure we can stat and open this file.
1197 if( filename == NULL )
1200 status = stat(filename, &statbuf);
1203 char *s,*t,*fn,*paths;
1204 if (strchr(filename,'/'))
1206 paths = DBG_strdup(getenv("PATH"));
1211 fn = (char*)DBG_alloc(strlen(filename)+1+strlen(s)+1);
1214 strcat(fn,filename);
1215 if ((rtn = DEBUG_ProcessElfObject(fn,load_offset))) {
1221 if (t) s = t+1; else break;
1223 if (!s || !*s) fprintf(stderr," not found");
1229 * Now open the file, so that we can mmap() it.
1231 fd = open(filename, O_RDONLY);
1237 * Now mmap() the file.
1239 addr = mmap(0, statbuf.st_size, PROT_READ,
1240 MAP_PRIVATE, fd, 0);
1241 if( addr == (char *) 0xffffffff )
1245 * Next, we need to find a few of the internal ELF headers within
1246 * this thing. We need the main executable header, and the section
1249 ehptr = (Elf32_Ehdr *) addr;
1251 if( load_offset == 0 )
1252 DEBUG_RegisterELFDebugInfo(ehptr->e_entry, statbuf.st_size, filename);
1254 DEBUG_RegisterELFDebugInfo(load_offset, statbuf.st_size, filename);
1256 spnt = (Elf32_Shdr *) (addr + ehptr->e_shoff);
1257 nsect = ehptr->e_shnum;
1258 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1260 stabsect = stabstrsect = -1;
1262 for(i=0; i < nsect; i++)
1264 if( strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0 )
1267 if( strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0 )
1271 if( stabsect == -1 || stabstrsect == -1 )
1275 * OK, now just parse all of the stabs.
1277 rtn = DEBUG_ParseStabs(addr, load_offset,
1278 spnt[stabsect].sh_offset,
1279 spnt[stabsect].sh_size,
1280 spnt[stabstrsect].sh_offset,
1281 spnt[stabstrsect].sh_size);
1286 for(i=0; i < nsect; i++)
1288 if( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1289 && (spnt[i].sh_type == SHT_SYMTAB) )
1290 DEBUG_ProcessElfSymtab(addr, load_offset,
1291 spnt + i, spnt + spnt[i].sh_link);
1293 if( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1294 && (spnt[i].sh_type == SHT_DYNSYM) )
1295 DEBUG_ProcessElfSymtab(addr, load_offset,
1296 spnt + i, spnt + spnt[i].sh_link);
1301 if( addr != (char *) 0xffffffff )
1302 munmap(addr, statbuf.st_size);
1312 DEBUG_ReadExecutableDbgInfo(void)
1315 const char * exe_name;
1317 struct r_debug * dbg_hdr;
1318 struct link_map * lpnt = NULL;
1320 extern Elf32_Dyn _DYNAMIC[] __attribute__ ((weak));
1322 extern Elf32_Dyn _DYNAMIC[];
1330 * Make sure we can stat and open this file.
1332 if( exe_name == NULL )
1335 fprintf( stderr, "Loading symbols: %s", exe_name );
1336 rowcount = 17 + strlen(exe_name);
1337 DEBUG_ProcessElfObject(exe_name, 0);
1340 * Finally walk the tables that the dynamic loader maintains to find all
1341 * of the other shared libraries which might be loaded. Perform the
1342 * same step for all of these.
1344 if( (&_DYNAMIC == NULL) || (_DYNAMIC == NULL) )
1350 * Now walk the dynamic section (of the executable, looking for a DT_DEBUG
1353 for(; dynpnt->d_tag != DT_NULL; dynpnt++)
1354 if( dynpnt->d_tag == DT_DEBUG )
1357 if( (dynpnt->d_tag != DT_DEBUG)
1358 || (dynpnt->d_un.d_ptr == 0) )
1362 * OK, now dig into the actual tables themselves.
1364 dbg_hdr = (struct r_debug *) dynpnt->d_un.d_ptr;
1365 lpnt = dbg_hdr->r_map;
1368 * Now walk the linked list. In all known ELF implementations,
1369 * the dynamic loader maintains this linked list for us. In some
1370 * cases the first entry doesn't appear with a name, in other cases it
1373 for(; lpnt; lpnt = lpnt->l_next )
1376 * We already got the stuff for the executable using the
1377 * argv[0] entry above. Here we only need to concentrate on any
1378 * shared libraries which may be loaded.
1380 ehdr = (Elf32_Ehdr *) lpnt->l_addr;
1381 if( (lpnt->l_addr == 0) || (ehdr->e_type != ET_DYN) )
1384 if( lpnt->l_name != NULL )
1386 if (rowcount + strlen(lpnt->l_name) > 76)
1388 fprintf( stderr, "\n " );
1391 fprintf( stderr, " %s", lpnt->l_name );
1392 rowcount += strlen(lpnt->l_name) + 1;
1393 DEBUG_ProcessElfObject(lpnt->l_name, lpnt->l_addr);
1400 fprintf( stderr, "\n" );
1405 #else /* !__ELF__ */
1412 DEBUG_ReadExecutableDbgInfo(void)
1414 char * addr = (char *) 0xffffffff;
1419 unsigned int staboff;
1420 struct stat statbuf;
1422 unsigned int stroff;
1427 * Make sure we can stat and open this file.
1429 if( exe_name == NULL )
1432 status = stat(exe_name, &statbuf);
1437 * Now open the file, so that we can mmap() it.
1439 fd = open(exe_name, O_RDONLY);
1445 * Now mmap() the file.
1447 addr = mmap(0, statbuf.st_size, PROT_READ,
1448 MAP_PRIVATE, fd, 0);
1449 if( addr == (char *) 0xffffffff )
1452 ahdr = (struct exec *) addr;
1454 staboff = N_SYMOFF(*ahdr);
1455 stroff = N_STROFF(*ahdr);
1456 rtn = DEBUG_ParseStabs(addr, 0,
1460 statbuf.st_size - stroff);
1463 * Give a nice status message here...
1465 fprintf( stderr, "Loading symbols: %s", exe_name );
1471 if( addr != (char *) 0xffffffff )
1472 munmap(addr, statbuf.st_size);
1482 * Non-linux, non-ELF platforms.
1485 DEBUG_ReadExecutableDbgInfo(void)
1491 #endif /* __ELF__ */