1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * File stabs.c - read stabs information from the wine executable itself.
6 * Copyright (C) 1996, Eric Youngdale.
7 * 1999, 2000 Eric Pouech
12 #include <sys/types.h>
15 #ifdef HAVE_SYS_MMAN_H
23 #define PATH_MAX _MAX_PATH
28 #if defined(__svr4__) || defined(__sun)
39 #ifdef HAVE_SYS_LINK_H
40 # include <sys/link.h>
71 typedef struct tagELF_DBG_INFO {
72 unsigned long elf_addr;
78 struct stab_nlist *n_next;
84 unsigned long n_value;
87 static void stab_strcpy(char * dest, int sz, const char * source)
90 * A strcpy routine that stops when we hit the ':' character.
91 * Faster than copying the whole thing, and then nuking the
94 while(*source != '\0' && *source != ':' && sz-- > 0)
104 struct datatype** vector;
108 #define MAX_INCLUDES 512
110 static include_def* include_defs = NULL;
111 static int num_include_def = 0;
112 static int num_alloc_include_def = 0;
113 static int cu_include_stack[MAX_INCLUDES];
114 static int cu_include_stk_idx = 0;
115 static struct datatype** cu_vector = NULL;
116 static int cu_nrofentries = 0;
120 DEBUG_CreateInclude(const char* file, unsigned long val)
122 if (num_include_def == num_alloc_include_def)
124 num_alloc_include_def += 256;
125 include_defs = DBG_realloc(include_defs, sizeof(include_defs[0])*num_alloc_include_def);
126 memset(include_defs+num_include_def, 0, sizeof(include_defs[0])*256);
128 include_defs[num_include_def].name = DBG_strdup(file);
129 include_defs[num_include_def].value = val;
130 include_defs[num_include_def].vector = NULL;
131 include_defs[num_include_def].nrofentries = 0;
133 return num_include_def++;
138 DEBUG_FindInclude(const char* file, unsigned long val)
142 for (i = 0; i < num_include_def; i++)
144 if (val == include_defs[i].value &&
145 strcmp(file, include_defs[i].name) == 0)
153 DEBUG_AddInclude(int idx)
155 ++cu_include_stk_idx;
157 /* is this happen, just bump MAX_INCLUDES */
158 /* we could also handle this as another dynarray */
159 assert(cu_include_stk_idx < MAX_INCLUDES);
161 cu_include_stack[cu_include_stk_idx] = idx;
162 return cu_include_stk_idx;
167 DEBUG_ResetIncludes(void)
170 * The datatypes that we would need to use are reset when
171 * we start a new file. (at least the ones in filenr == 0
173 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
174 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
179 DEBUG_FreeIncludes(void)
183 DEBUG_ResetIncludes();
185 for (i = 0; i < num_include_def; i++)
187 DBG_free(include_defs[i].name);
188 DBG_free(include_defs[i].vector);
190 DBG_free(include_defs);
193 num_alloc_include_def = 0;
201 DEBUG_FileSubNr2StabEnum(int filenr, int subnr)
203 struct datatype** ret;
205 /* DEBUG_Printf(DBG_CHN_MESG, "creating type id for (%d,%d)\n", filenr, subnr); */
207 /* FIXME: I could perhaps create a dummy include_def for each compilation
208 * unit which would allow not to handle those two cases separately
212 if (cu_nrofentries <= subnr)
214 cu_vector = DBG_realloc(cu_vector, sizeof(cu_vector[0])*(subnr+1));
215 memset(cu_vector+cu_nrofentries, 0, sizeof(cu_vector[0])*(subnr+1-cu_nrofentries));
216 cu_nrofentries = subnr + 1;
218 ret = &cu_vector[subnr];
224 assert(filenr <= cu_include_stk_idx);
226 idef = &include_defs[cu_include_stack[filenr]];
228 if (idef->nrofentries <= subnr)
230 idef->vector = DBG_realloc(idef->vector, sizeof(idef->vector[0])*(subnr+1));
231 memset(idef->vector + idef->nrofentries, 0, sizeof(idef->vector[0])*(subnr+1-idef->nrofentries));
232 idef->nrofentries = subnr + 1;
234 ret = &idef->vector[subnr];
236 /* DEBUG_Printf(DBG_CHN_MESG,"(%d,%d) is %d\n",filenr,subnr,ret); */
242 DEBUG_ReadTypeEnum(char **x) {
247 filenr=strtol(*x,x,10); /* <int> */
249 subnr=strtol(*x,x,10); /* <int> */
253 subnr = strtol(*x,x,10); /* <int> */
255 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
258 struct ParseTypedefData {
264 static int DEBUG_PTS_ReadTypedef(struct ParseTypedefData* ptd, const char* typename,
265 struct datatype** dt);
267 static int DEBUG_PTS_ReadID(struct ParseTypedefData* ptd)
269 char* first = ptd->ptr;
272 if ((ptd->ptr = strchr(ptd->ptr, ':')) == NULL) return -1;
273 len = ptd->ptr - first;
274 if (len >= sizeof(ptd->buf) - ptd->idx) return -1;
275 memcpy(ptd->buf + ptd->idx, first, len);
276 ptd->buf[ptd->idx + len] = '\0';
278 ptd->ptr++; /* ':' */
282 static int DEBUG_PTS_ReadNum(struct ParseTypedefData* ptd, int* v)
286 *v = strtol(ptd->ptr, &last, 10);
287 if (last == ptd->ptr) return -1;
292 static int DEBUG_PTS_ReadTypeReference(struct ParseTypedefData* ptd,
293 int* filenr, int* subnr)
295 if (*ptd->ptr == '(') {
296 /* '(' <int> ',' <int> ')' */
298 if (DEBUG_PTS_ReadNum(ptd, filenr) == -1) return -1;
299 if (*ptd->ptr++ != ',') return -1;
300 if (DEBUG_PTS_ReadNum(ptd, subnr) == -1) return -1;
301 if (*ptd->ptr++ != ')') return -1;
304 if (DEBUG_PTS_ReadNum(ptd, subnr) == -1) return -1;
309 static int DEBUG_PTS_ReadRange(struct ParseTypedefData* ptd, struct datatype** dt,
312 /* type ';' <int> ';' <int> ';' */
313 if (DEBUG_PTS_ReadTypedef(ptd, NULL, dt) == -1) return -1;
314 if (*ptd->ptr++ != ';') return -1; /* ';' */
315 if (DEBUG_PTS_ReadNum(ptd, lo) == -1) return -1;
316 if (*ptd->ptr++ != ';') return -1; /* ';' */
317 if (DEBUG_PTS_ReadNum(ptd, hi) == -1) return -1;
318 if (*ptd->ptr++ != ';') return -1; /* ';' */
322 static inline int DEBUG_PTS_ReadAggregate(struct ParseTypedefData* ptd, struct datatype* sdt)
326 struct datatype* adt;
330 sz = strtol(ptd->ptr, &last, 10);
331 if (last == ptd->ptr) return -1;
334 doadd = DEBUG_SetStructSize(sdt, sz);
335 /* if the structure has already been filled, just redo the parsing
336 * but don't store results into the struct
337 * FIXME: there's a quite ugly memory leak in there...
340 /* Now parse the individual elements of the structure/union. */
341 while (*ptd->ptr != ';') {
342 /* agg_name : type ',' <int:offset> ',' <int:size> */
344 if (DEBUG_PTS_ReadID(ptd) == -1) return -1;
346 if (DEBUG_PTS_ReadTypedef(ptd, NULL, &adt) == -1) return -1;
349 if (*ptd->ptr++ != ',') return -1;
350 if (DEBUG_PTS_ReadNum(ptd, &ofs) == -1) return -1;
351 if (*ptd->ptr++ != ',') return -1;
352 if (DEBUG_PTS_ReadNum(ptd, &sz) == -1) return -1;
353 if (*ptd->ptr++ != ';') return -1;
355 if (doadd) DEBUG_AddStructElement(sdt, ptd->buf + idx, adt, ofs, sz);
358 ptd->ptr++; /* ';' */
362 static inline int DEBUG_PTS_ReadEnum(struct ParseTypedefData* ptd, struct datatype* edt)
367 while (*ptd->ptr != ';') {
369 if (DEBUG_PTS_ReadID(ptd) == -1) return -1;
370 if (DEBUG_PTS_ReadNum(ptd, &ofs) == -1) return -1;
371 if (*ptd->ptr++ != ',') return -1;
372 DEBUG_AddStructElement(edt, ptd->buf + idx, NULL, ofs, 0);
379 static inline int DEBUG_PTS_ReadArray(struct ParseTypedefData* ptd, struct datatype* adt)
382 struct datatype* rdt;
384 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
386 if (*ptd->ptr++ != 'r') return -1;
387 /* FIXME: range type is lost, always assume int */
388 if (DEBUG_PTS_ReadRange(ptd, &rdt, &lo, &hi) == -1) return -1;
389 if (DEBUG_PTS_ReadTypedef(ptd, NULL, &rdt) == -1) return -1;
391 DEBUG_SetArrayParams(adt, lo, hi, rdt);
395 static int DEBUG_PTS_ReadTypedef(struct ParseTypedefData* ptd, const char* typename,
396 struct datatype** ret_dt)
398 int idx, lo, hi, sz = -1;
399 struct datatype* new_dt = NULL; /* newly created data type */
400 struct datatype* ref_dt; /* referenced data type (pointer...) */
401 struct datatype* dt1; /* intermediate data type (scope is limited) */
402 struct datatype* dt2; /* intermediate data type: t1=t2=new_dt */
404 int filenr2 = 0, subnr2 = 0;
406 /* things are a bit complicated because of the way the typedefs are stored inside
407 * the file (we cannot keep the struct datatype** around, because address can
408 * change when realloc is done, so we must call over and over
409 * DEBUG_FileSubNr2StabEnum to keep the correct values around
410 * (however, keeping struct datatype* is valid
412 if (DEBUG_PTS_ReadTypeReference(ptd, &filenr1, &subnr1) == -1) return -1;
414 while (*ptd->ptr == '=') {
417 DEBUG_Printf(DBG_CHN_MESG, "Bad recursion (1) in typedef\n");
420 /* first handle attribute if any */
423 if (*++ptd->ptr == 's') {
425 if (DEBUG_PTS_ReadNum(ptd, &sz) == -1) {
426 DEBUG_Printf(DBG_CHN_MESG, "Not an attribute... NIY\n");
430 if (*ptd->ptr++ != ';') return -1;
434 /* then the real definitions */
435 switch (*ptd->ptr++) {
437 new_dt = DEBUG_NewDataType(DT_POINTER, NULL);
438 if (DEBUG_PTS_ReadTypedef(ptd, NULL, &ref_dt) == -1) return -1;
439 DEBUG_SetPointerType(new_dt, ref_dt);
443 /* doit a two level by hand, otherwise we'd need a stack */
444 if (filenr2 || subnr2) {
445 DEBUG_Printf(DBG_CHN_MESG, "Bad recursion (2) in typedef\n");
448 if (DEBUG_PTS_ReadTypeReference(ptd, &filenr2, &subnr2) == -1) return -1;
450 dt1 = *DEBUG_FileSubNr2StabEnum(filenr1, subnr1);
451 dt2 = *DEBUG_FileSubNr2StabEnum(filenr2, subnr2);
455 filenr2 = subnr2 = 0;
456 } else if (!dt1 && !dt2) {
459 DEBUG_Printf(DBG_CHN_MESG, "Unknown condition %08lx %08lx (%s)\n",
460 (unsigned long)dt1, (unsigned long)dt2, ptd->ptr);
465 new_dt = DEBUG_NewDataType(DT_ARRAY, NULL);
466 if (DEBUG_PTS_ReadArray(ptd, new_dt) == -1) return -1;
469 new_dt = DEBUG_NewDataType(DT_BASIC, typename);
470 assert(!*DEBUG_FileSubNr2StabEnum(filenr1, subnr1));
471 *DEBUG_FileSubNr2StabEnum(filenr1, subnr1) = new_dt;
472 if (DEBUG_PTS_ReadRange(ptd, &ref_dt, &lo, &hi) == -1) return -1;
473 /* should perhaps do more here... */
476 new_dt = DEBUG_NewDataType(DT_FUNC, NULL);
477 if (DEBUG_PTS_ReadTypedef(ptd, NULL, &ref_dt) == -1) return -1;
478 DEBUG_SetPointerType(new_dt, ref_dt);
481 new_dt = DEBUG_NewDataType(DT_ENUM, NULL);
482 if (DEBUG_PTS_ReadEnum(ptd, new_dt) == -1) return -1;
486 /* dt1 can have been already defined in a forward definition */
487 dt1 = *DEBUG_FileSubNr2StabEnum(filenr1, subnr1);
488 dt2 = DEBUG_TypeCast(DT_STRUCT, typename);
490 new_dt = DEBUG_NewDataType(DT_STRUCT, typename);
491 /* we need to set it here, because a struct can hold a pointer
494 *DEBUG_FileSubNr2StabEnum(filenr1, subnr1) = new_dt;
496 if (DEBUG_GetType(dt1) != DT_STRUCT) {
497 DEBUG_Printf(DBG_CHN_MESG,
498 "Forward declaration is not an aggregate\n");
502 /* should check typename is the same too */
505 if (DEBUG_PTS_ReadAggregate(ptd, new_dt) == -1) return -1;
508 switch (*ptd->ptr++) {
509 case 'e': lo = DT_ENUM; break;
510 case 's': case 'u': lo = DT_STRUCT; break;
515 if (DEBUG_PTS_ReadID(ptd) == -1) return -1;
516 new_dt = DEBUG_NewDataType(lo, ptd->buf + idx);
520 if (DEBUG_PTS_ReadNum(ptd, &lo) == -1) {
521 DEBUG_Printf(DBG_CHN_MESG, "Should be a number (%s)...\n", ptd->ptr);
524 enum debug_type_basic basic = DT_BASIC_LAST;
527 case 1: basic = DT_BASIC_INT; break;
528 case 2: basic = DT_BASIC_CHAR; break;
529 case 3: basic = DT_BASIC_SHORTINT; break;
530 case 4: basic = DT_BASIC_LONGINT; break;
531 case 5: basic = DT_BASIC_UCHAR; break;
532 case 6: basic = DT_BASIC_SCHAR; break;
533 case 7: basic = DT_BASIC_USHORTINT; break;
534 case 8: basic = DT_BASIC_UINT; break;
535 /* case 9: basic = DT_BASIC_UINT"; */
536 case 10: basic = DT_BASIC_ULONGINT; break;
537 case 11: basic = DT_BASIC_VOID; break;
538 case 12: basic = DT_BASIC_FLOAT; break;
539 case 13: basic = DT_BASIC_DOUBLE; break;
540 case 14: basic = DT_BASIC_LONGDOUBLE; break;
541 /* case 15: basic = DT_BASIC_INT; break; */
544 case 32: basic = DT_BASIC_BOOL1; break;
545 case 16: basic = DT_BASIC_BOOL2; break;
546 case 8: basic = DT_BASIC_BOOL4; break;
549 /* case 17: basic = DT_BASIC_SHORT real; break; */
550 /* case 18: basic = DT_BASIC_REAL; break; */
551 case 25: basic = DT_BASIC_CMPLX_FLOAT; break;
552 case 26: basic = DT_BASIC_CMPLX_DOUBLE; break;
553 /* case 30: basic = DT_BASIC_wchar"; break; */
554 case 31: basic = DT_BASIC_LONGLONGINT; break;
555 case 32: basic = DT_BASIC_ULONGLONGINT; break;
557 DEBUG_Printf(DBG_CHN_MESG, "Unsupported integral type (%d/%d)\n", lo, sz);
560 if (!(new_dt = DEBUG_GetBasicType(basic))) {
561 DEBUG_Printf(DBG_CHN_MESG, "Basic type %d not found\n", basic);
564 if (*ptd->ptr++ != ';') return -1;
568 DEBUG_Printf(DBG_CHN_MESG, "Unknown type '%c'\n", ptd->ptr[-1]);
573 if ((filenr2 || subnr2) && !*DEBUG_FileSubNr2StabEnum(filenr2, subnr2)) {
575 /* this should be a basic type, define it, or even void */
576 new_dt = DEBUG_NewDataType(DT_BASIC, typename);
578 *DEBUG_FileSubNr2StabEnum(filenr2, subnr2) = new_dt;
582 dt1 = *DEBUG_FileSubNr2StabEnum(filenr1, subnr1);
584 DEBUG_Printf(DBG_CHN_MESG, "Nothing has been defined <%s>\n", ptd->ptr);
591 *DEBUG_FileSubNr2StabEnum(filenr1, subnr1) = *ret_dt = new_dt;
595 DEBUG_Printf(DBG_CHN_MESG, "Adding (%d,%d) %s => ", filenr1, subnr1, typename);
596 DEBUG_PrintTypeCast(new_dt);
597 DEBUG_Printf(DBG_CHN_MESG, "\n");
604 static int DEBUG_ParseTypedefStab(char* ptr, const char* typename)
606 struct ParseTypedefData ptd;
610 /* check for already existing definition */
613 if ((ptd.ptr = strchr(ptr, ':'))) {
615 if (*ptd.ptr != '(') ptd.ptr++;
616 ret = DEBUG_PTS_ReadTypedef(&ptd, typename, &dt);
619 if (ret == -1 || *ptd.ptr) {
620 DEBUG_Printf(DBG_CHN_MESG, "failure on %s at %s\n", ptr, ptd.ptr);
627 static struct datatype *
628 DEBUG_ParseStabType(const char * stab)
633 * Look through the stab definition, and figure out what datatype
634 * this represents. If we have something we know about, assign the
637 c = strchr(stab, ':');
643 * The next character says more about the type (i.e. data, function, etc)
644 * of symbol. Skip it.
649 * The next is either an integer or a (integer,integer).
650 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
652 return *DEBUG_ReadTypeEnum(&c);
655 enum DbgInfoLoad DEBUG_ParseStabs(char * addr, unsigned int load_offset,
656 unsigned int staboff, int stablen,
657 unsigned int strtaboff, int strtablen)
659 struct name_hash * curr_func = NULL;
660 struct wine_locals * curr_loc = NULL;
661 struct name_hash * curr_sym = NULL;
662 char currpath[PATH_MAX];
664 int in_external_file = FALSE;
671 unsigned int stabbufflen;
672 struct stab_nlist * stab_ptr;
675 char * subpath = NULL;
678 nstab = stablen / sizeof(struct stab_nlist);
679 stab_ptr = (struct stab_nlist *) (addr + staboff);
680 strs = (char *) (addr + strtaboff);
682 memset(currpath, 0, sizeof(currpath));
685 * Allocate a buffer into which we can build stab strings for cases
686 * where the stab is continued over multiple lines.
689 stabbuff = (char *) DBG_alloc(stabbufflen);
693 for(i=0; i < nstab; i++, stab_ptr++ )
695 ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
696 if( ptr[strlen(ptr) - 1] == '\\' )
699 * Indicates continuation. Append this to the buffer, and go onto the
700 * next record. Repeat the process until we find a stab without the
701 * '/' character, as this indicates we have the whole thing.
704 if( strlen(stabbuff) + len > stabbufflen )
706 stabbufflen += 65536;
707 stabbuff = (char *) DBG_realloc(stabbuff, stabbufflen);
709 strncat(stabbuff, ptr, len - 1);
712 else if( stabbuff[0] != '\0' )
714 strcat( stabbuff, ptr);
718 if( strchr(ptr, '=') != NULL )
721 * The stabs aren't in writable memory, so copy it over so we are
722 * sure we can scribble on it.
724 if( ptr != stabbuff )
726 strcpy(stabbuff, ptr);
729 stab_strcpy(symname, sizeof(symname), ptr);
730 if (!DEBUG_ParseTypedefStab(ptr, symname)) {
731 /* skip this definition */
737 switch(stab_ptr->n_type)
741 * These are useless with ELF. They have no value, and you have to
742 * read the normal symbol table to get the address. Thus we
743 * ignore them, and when we process the normal symbol table
744 * we should do the right thing.
746 * With a.out or mingw, they actually do make some amount of sense.
748 new_value.addr.seg = 0;
749 new_value.type = DEBUG_ParseStabType(ptr);
750 new_value.addr.off = load_offset + stab_ptr->n_value;
751 new_value.cookie = DV_TARGET;
753 stab_strcpy(symname, sizeof(symname), ptr);
755 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
756 SYM_WINE | SYM_DATA | SYM_INVALID );
758 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
759 SYM_WINE | SYM_DATA );
765 * We need to keep track of these so we get symbol scoping
766 * right for local variables. For now, we just ignore them.
767 * The hooks are already there for dealing with this however,
768 * so all we need to do is to keep count of the nesting level,
769 * and find the RBRAC for each matching LBRAC.
775 * These are static symbols and BSS symbols.
777 new_value.addr.seg = 0;
778 new_value.type = DEBUG_ParseStabType(ptr);
779 new_value.addr.off = load_offset + stab_ptr->n_value;
780 new_value.cookie = DV_TARGET;
782 stab_strcpy(symname, sizeof(symname), ptr);
783 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
784 SYM_WINE | SYM_DATA );
788 * These are function parameters.
790 if( curr_func != NULL && !in_external_file )
792 stab_strcpy(symname, sizeof(symname), ptr);
793 curr_loc = DEBUG_AddLocal( curr_func, 0,
794 stab_ptr->n_value, 0, 0, symname );
795 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
799 if( curr_func != NULL && !in_external_file )
801 stab_strcpy(symname, sizeof(symname), ptr);
802 curr_loc = DEBUG_AddLocal( curr_func, stab_ptr->n_value + 1,
804 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
808 if( curr_func != NULL && !in_external_file )
810 stab_strcpy(symname, sizeof(symname), ptr);
811 curr_loc = DEBUG_AddLocal( curr_func, 0,
812 stab_ptr->n_value, 0, 0, symname );
813 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
818 * This is a line number. These are always relative to the start
819 * of the function (N_FUN), and this makes the lookup easier.
821 if( curr_func != NULL && !in_external_file )
824 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
829 * This isn't right. The order of the stabs is different under
830 * a.out, and as a result we would end up attaching the line
831 * number to the wrong function.
833 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
834 stab_ptr->n_value - curr_func->addr.off);
841 * First, clean up the previous function we were working on.
843 DEBUG_Normalize(curr_func);
846 * For now, just declare the various functions. Later
847 * on, we will add the line number information and the
850 if( !in_external_file)
852 stab_strcpy(symname, sizeof(symname), ptr);
855 new_value.addr.seg = 0;
856 new_value.type = DEBUG_ParseStabType(ptr);
857 new_value.addr.off = load_offset + stab_ptr->n_value;
858 new_value.cookie = DV_TARGET;
860 * Copy the string to a temp buffer so we
861 * can kill everything after the ':'. We do
862 * it this way because otherwise we end up dirtying
863 * all of the pages related to the stabs, and that
864 * sucks up swap space like crazy.
867 curr_func = DEBUG_AddSymbol( symname, &new_value, currpath,
868 SYM_WINE | SYM_FUNC | SYM_INVALID );
870 curr_func = DEBUG_AddSymbol( symname, &new_value, currpath,
871 SYM_WINE | SYM_FUNC );
876 /* some GCC seem to use a N_FUN "" to mark the end of a function */
883 * Don't add line number information for this function
891 * This indicates a new source file. Append the records
892 * together, to build the correct path name.
896 * With a.out, there is no NULL string N_SO entry at the end of
897 * the file. Thus when we find non-consecutive entries,
898 * we consider that a new file is started.
903 DEBUG_Normalize(curr_func);
914 DEBUG_Normalize(curr_func);
920 strcat(currpath, ptr);
922 strcpy(currpath, ptr);
924 DEBUG_ResetIncludes();
930 * This indicates we are including stuff from an include file.
931 * If this is the main source, enable the debug stuff, otherwise
934 in_external_file = !(subpath == NULL || strcmp(ptr, subpath) == 0);
938 strtabinc = stab_ptr->n_value;
939 DEBUG_Normalize(curr_func);
944 * Ignore this. We don't care what it points to.
948 DEBUG_AddInclude(DEBUG_CreateInclude(ptr, stab_ptr->n_value));
953 DEBUG_AddInclude(DEBUG_FindInclude(ptr, stab_ptr->n_value));
957 * Always ignore these. GCC doesn't even generate them.
961 DEBUG_Printf(DBG_CHN_MESG, "Unknown stab type 0x%02x\n", stab_ptr->n_type);
968 DEBUG_Printf(DBG_CHN_MESG, "%d %x %s\n", stab_ptr->n_type,
969 (unsigned int) stab_ptr->n_value,
970 strs + (unsigned int) stab_ptr->n_un.n_name);
974 DEBUG_FreeIncludes();
982 * Walk through the entire symbol table and add any symbols we find there.
983 * This can be used in cases where we have stripped ELF shared libraries,
984 * or it can be used in cases where we have data symbols for which the address
985 * isn't encoded in the stabs.
987 * This is all really quite easy, since we don't have to worry about line
988 * numbers or local data variables.
990 static int DEBUG_ProcessElfSymtab(DBG_MODULE* module, char* addr,
991 u_long load_addr, Elf32_Shdr* symtab,
994 char * curfile = NULL;
995 struct name_hash * curr_sym = NULL;
1004 symp = (Elf32_Sym *) (addr + symtab->sh_offset);
1005 nsym = symtab->sh_size / sizeof(*symp);
1006 strp = (char *) (addr + strtab->sh_offset);
1008 for(i=0; i < nsym; i++, symp++)
1011 * Ignore certain types of entries which really aren't of that much
1014 if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION ||
1015 symp->st_shndx == STN_UNDEF )
1020 symname = strp + symp->st_name;
1023 * Save the name of the current file, so we have a way of tracking
1024 * static functions/data.
1026 if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
1033 * See if we already have something for this symbol.
1034 * If so, ignore this entry, because it would have come from the
1035 * stabs or from a previous symbol. If the value is different,
1036 * we will have to keep the darned thing, because there can be
1037 * multiple local symbols by the same name.
1039 if( (DEBUG_GetSymbolValue(symname, -1, &new_value, FALSE ) == TRUE)
1040 && (new_value.addr.off == (load_addr + symp->st_value)) )
1043 new_value.addr.seg = 0;
1044 new_value.type = NULL;
1045 new_value.addr.off = load_addr + symp->st_value;
1046 new_value.cookie = DV_TARGET;
1047 flags = SYM_WINE | ((ELF32_ST_TYPE(symp->st_info) == STT_FUNC)
1048 ? SYM_FUNC : SYM_DATA);
1049 if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
1050 curr_sym = DEBUG_AddSymbol( symname, &new_value, NULL, flags );
1052 curr_sym = DEBUG_AddSymbol( symname, &new_value, curfile, flags );
1055 * Record the size of the symbol. This can come in handy in
1056 * some cases. Not really used yet, however.
1058 if( symp->st_size != 0 )
1059 DEBUG_SetSymbolSize(curr_sym, symp->st_size);
1066 * Loads the symbolic information from ELF module stored in 'filename'
1067 * the module has been loaded at 'load_offset' address, so symbols' address
1068 * relocation is performed
1070 * -1 if the file cannot be found/opened
1071 * 0 if the file doesn't contain symbolic info (or this info cannot be
1075 enum DbgInfoLoad DEBUG_LoadElfStabs(DBG_MODULE* module)
1077 enum DbgInfoLoad dil = DIL_ERROR;
1078 char* addr = (char*)0xffffffff;
1080 struct stat statbuf;
1088 if (module->type != DMT_ELF || ! module->elf_info) {
1089 DEBUG_Printf(DBG_CHN_ERR, "Bad elf module '%s'\n", module->module_name);
1093 /* check that the file exists, and that the module hasn't been loaded yet */
1094 if (stat(module->module_name, &statbuf) == -1) goto leave;
1095 if (S_ISDIR(statbuf.st_mode)) goto leave;
1098 * Now open the file, so that we can mmap() it.
1100 if ((fd = open(module->module_name, O_RDONLY)) == -1) goto leave;
1104 * Now mmap() the file.
1106 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1107 if (addr == (char*)0xffffffff) goto leave;
1110 * Next, we need to find a few of the internal ELF headers within
1111 * this thing. We need the main executable header, and the section
1114 ehptr = (Elf32_Ehdr*) addr;
1115 spnt = (Elf32_Shdr*) (addr + ehptr->e_shoff);
1116 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1118 stabsect = stabstrsect = -1;
1120 for (i = 0; i < ehptr->e_shnum; i++) {
1121 if (strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0)
1124 if (strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0)
1128 if (stabsect == -1 || stabstrsect == -1) {
1129 DEBUG_Printf(DBG_CHN_WARN, "no .stab section\n");
1134 * OK, now just parse all of the stabs.
1136 if (DEBUG_ParseStabs(addr,
1137 module->elf_info->elf_addr,
1138 spnt[stabsect].sh_offset,
1139 spnt[stabsect].sh_size,
1140 spnt[stabstrsect].sh_offset,
1141 spnt[stabstrsect].sh_size)) {
1145 DEBUG_Printf(DBG_CHN_WARN, "bad stabs\n");
1149 for (i = 0; i < ehptr->e_shnum; i++) {
1150 if ( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1151 && (spnt[i].sh_type == SHT_SYMTAB))
1152 DEBUG_ProcessElfSymtab(module, addr, module->elf_info->elf_addr,
1153 spnt + i, spnt + spnt[i].sh_link);
1155 if ( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1156 && (spnt[i].sh_type == SHT_DYNSYM))
1157 DEBUG_ProcessElfSymtab(module, addr, module->elf_info->elf_addr,
1158 spnt + i, spnt + spnt[i].sh_link);
1162 if (addr != (char*)0xffffffff) munmap(addr, statbuf.st_size);
1163 if (fd != -1) close(fd);
1169 * Loads the information for ELF module stored in 'filename'
1170 * the module has been loaded at 'load_offset' address
1172 * -1 if the file cannot be found/opened
1173 * 0 if the file doesn't contain symbolic info (or this info cannot be
1177 static enum DbgInfoLoad DEBUG_ProcessElfFile(const char* filename,
1178 unsigned int load_offset,
1179 unsigned int* dyn_addr)
1181 enum DbgInfoLoad dil = DIL_ERROR;
1182 char* addr = (char*)0xffffffff;
1184 struct stat statbuf;
1190 DBG_MODULE* module = NULL;
1194 DEBUG_Printf(DBG_CHN_TRACE, "Processing elf file '%s'\n", filename);
1196 /* check that the file exists, and that the module hasn't been loaded yet */
1197 if (stat(filename, &statbuf) == -1) goto leave;
1200 * Now open the file, so that we can mmap() it.
1202 if ((fd = open(filename, O_RDONLY)) == -1) goto leave;
1205 * Now mmap() the file.
1207 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1208 if (addr == (char*)0xffffffff) goto leave;
1213 * Next, we need to find a few of the internal ELF headers within
1214 * this thing. We need the main executable header, and the section
1217 ehptr = (Elf32_Ehdr*) addr;
1218 spnt = (Elf32_Shdr*) (addr + ehptr->e_shoff);
1219 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1221 /* if non relocatable ELF, then remove fixed address from computation
1222 * otherwise, all addresses are zero based
1224 delta = (load_offset == 0) ? ehptr->e_entry : 0;
1226 /* grab size of module once loaded in memory */
1227 ppnt = (Elf32_Phdr*) (addr + ehptr->e_phoff);
1229 for (i = 0; i < ehptr->e_phnum; i++) {
1230 if (ppnt[i].p_type != PT_LOAD) continue;
1231 if (size < ppnt[i].p_vaddr - delta + ppnt[i].p_memsz)
1232 size = ppnt[i].p_vaddr - delta + ppnt[i].p_memsz;
1235 for (i = 0; i < ehptr->e_shnum; i++) {
1236 if (strcmp(shstrtab + spnt[i].sh_name, ".bss") == 0 &&
1237 spnt[i].sh_type == SHT_NOBITS) {
1238 if (size < spnt[i].sh_addr - delta + spnt[i].sh_size)
1239 size = spnt[i].sh_addr - delta + spnt[i].sh_size;
1241 if (strcmp(shstrtab + spnt[i].sh_name, ".dynamic") == 0 &&
1242 spnt[i].sh_type == SHT_DYNAMIC) {
1243 if (dyn_addr) *dyn_addr = spnt[i].sh_addr;
1247 module = DEBUG_RegisterELFModule((load_offset == 0) ? ehptr->e_entry : load_offset,
1254 if ((module->elf_info = DBG_alloc(sizeof(ELF_DBG_INFO))) == NULL) {
1255 DEBUG_Printf(DBG_CHN_ERR, "OOM\n");
1259 module->elf_info->elf_addr = load_offset;
1260 dil = DEBUG_LoadElfStabs(module);
1263 if (addr != (char*)0xffffffff) munmap(addr, statbuf.st_size);
1264 if (fd != -1) close(fd);
1265 if (module) module->dil = dil;
1270 static enum DbgInfoLoad DEBUG_ProcessElfFileFromPath(const char * filename,
1271 unsigned int load_offset,
1272 unsigned int* dyn_addr,
1275 enum DbgInfoLoad dil = DIL_ERROR;
1279 if (!path) return -1;
1281 for (s = paths = DBG_strdup(path); s && *s; s = (t) ? (t+1) : NULL) {
1284 fn = (char*)DBG_alloc(strlen(filename) + 1 + strlen(s) + 1);
1288 strcat(fn, filename);
1289 dil = DEBUG_ProcessElfFile(fn, load_offset, dyn_addr);
1291 if (dil != DIL_ERROR) break;
1292 s = (t) ? (t+1) : NULL;
1299 static enum DbgInfoLoad DEBUG_ProcessElfObject(const char* filename,
1300 unsigned int load_offset,
1301 unsigned int* dyn_addr)
1303 enum DbgInfoLoad dil = DIL_ERROR;
1305 if (filename == NULL) return DIL_ERROR;
1306 if (DEBUG_FindModuleByName(filename, DMT_ELF)) return DIL_LOADED;
1308 dil = DEBUG_ProcessElfFile(filename, load_offset, dyn_addr);
1310 /* if relative pathname, try some absolute base dirs */
1311 if (dil == DIL_ERROR && !strchr(filename, '/')) {
1312 dil = DEBUG_ProcessElfFileFromPath(filename, load_offset, dyn_addr, getenv("PATH"));
1313 if (dil == DIL_ERROR)
1314 dil = DEBUG_ProcessElfFileFromPath(filename, load_offset, dyn_addr, getenv("LD_LIBRARY_PATH"));
1317 DEBUG_ReportDIL(dil, "ELF", filename, load_offset);
1322 static BOOL DEBUG_WalkList(struct r_debug* dbg_hdr)
1330 * Now walk the linked list. In all known ELF implementations,
1331 * the dynamic loader maintains this linked list for us. In some
1332 * cases the first entry doesn't appear with a name, in other cases it
1335 for (lm_addr = (u_long)dbg_hdr->r_map; lm_addr; lm_addr = (u_long)lm.l_next) {
1336 if (!DEBUG_READ_MEM_VERBOSE((void*)lm_addr, &lm, sizeof(lm)))
1338 if (lm.l_addr != 0 &&
1339 DEBUG_READ_MEM_VERBOSE((void*)lm.l_addr, &ehdr, sizeof(ehdr)) &&
1340 ehdr.e_type == ET_DYN && /* only look at dynamic modules */
1341 lm.l_name != NULL &&
1342 DEBUG_READ_MEM_VERBOSE((void*)lm.l_name, bufstr, sizeof(bufstr))) {
1343 bufstr[sizeof(bufstr) - 1] = '\0';
1344 DEBUG_ProcessElfObject(bufstr, (unsigned)lm.l_addr, NULL);
1351 static BOOL DEBUG_RescanElf(void)
1353 struct r_debug dbg_hdr;
1355 if (!DEBUG_CurrProcess ||
1356 !DEBUG_READ_MEM_VERBOSE((void*)DEBUG_CurrProcess->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)))
1359 switch (dbg_hdr.r_state) {
1361 DEBUG_WalkList(&dbg_hdr);
1362 DEBUG_CheckDelayedBP();
1367 /* FIXME: this is not currently handled, would need some kind of mark&sweep algo */
1373 enum DbgInfoLoad DEBUG_ReadExecutableDbgInfo(const char* exe_name)
1376 struct r_debug dbg_hdr;
1377 enum DbgInfoLoad dil = DIL_NOINFO;
1378 unsigned int dyn_addr;
1381 * Make sure we can stat and open this file.
1383 if (exe_name == NULL) goto leave;
1384 DEBUG_ProcessElfObject(exe_name, 0, &dyn_addr);
1387 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn_addr, &dyn, sizeof(dyn)))
1389 dyn_addr += sizeof(dyn);
1390 } while (dyn.d_tag != DT_DEBUG && dyn.d_tag != DT_NULL);
1391 if (dyn.d_tag == DT_NULL) goto leave;
1394 * OK, now dig into the actual tables themselves.
1396 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn.d_un.d_ptr, &dbg_hdr, sizeof(dbg_hdr)))
1399 assert(!DEBUG_CurrProcess->dbg_hdr_addr);
1400 DEBUG_CurrProcess->dbg_hdr_addr = (u_long)dyn.d_un.d_ptr;
1402 if (dbg_hdr.r_brk) {
1405 DEBUG_Printf(DBG_CHN_TRACE, "Setting up a breakpoint on r_brk(%lx)\n",
1406 (unsigned long)dbg_hdr.r_brk);
1408 DEBUG_SetBreakpoints(FALSE);
1410 value.cookie = DV_TARGET;
1412 value.addr.off = (DWORD)dbg_hdr.r_brk;
1413 DEBUG_AddBreakpoint(&value, DEBUG_RescanElf);
1414 DEBUG_SetBreakpoints(TRUE);
1417 dil = DEBUG_WalkList(&dbg_hdr);
1423 #else /* !__ELF__ */
1425 enum DbgInfoLoad DEBUG_ReadExecutableDbgInfo(const char* exe_name)
1430 #endif /* __ELF__ */