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
)
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
, &lo
) == -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 DEBUG_Printf(DBG_CHN_MESG
, "Unknown type '%c'\n", *ptd
->ptr
);
525 if ((filenr2
|| subnr2
) && !*DEBUG_FileSubNr2StabEnum(filenr2
, subnr2
)) {
527 /* this should be a basic type, define it, or even void */
528 new_dt
= DEBUG_NewDataType(DT_BASIC
, typename
);
530 *DEBUG_FileSubNr2StabEnum(filenr2
, subnr2
) = new_dt
;
534 dt1
= *DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
);
536 DEBUG_Printf(DBG_CHN_MESG
, "Nothing has been defined <%s>\n", ptd
->ptr
);
543 *DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
) = *ret_dt
= new_dt
;
547 DEBUG_Printf(DBG_CHN_MESG
, "Adding (%d,%d) %s => ", filenr1
, subnr1
, typename
);
548 DEBUG_PrintTypeCast(new_dt
);
549 DEBUG_Printf(DBG_CHN_MESG
, "\n");
556 static int DEBUG_ParseTypedefStab(char* ptr
, const char* typename
)
558 struct ParseTypedefData ptd
;
562 /* check for already existing definition */
565 if ((ptd
.ptr
= strchr(ptr
, ':'))) {
567 if (*ptd
.ptr
!= '(') ptd
.ptr
++;
568 ret
= DEBUG_PTS_ReadTypedef(&ptd
, typename
, &dt
);
571 if (ret
== -1 || *ptd
.ptr
) {
572 DEBUG_Printf(DBG_CHN_MESG
, "failure on %s at %s\n", ptr
, ptd
.ptr
);
579 static struct datatype
*
580 DEBUG_ParseStabType(const char * stab
)
585 * Look through the stab definition, and figure out what datatype
586 * this represents. If we have something we know about, assign the
589 c
= strchr(stab
, ':');
595 * The next character says more about the type (i.e. data, function, etc)
596 * of symbol. Skip it.
601 * The next is either an integer or a (integer,integer).
602 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
604 return *DEBUG_ReadTypeEnum(&c
);
607 enum DbgInfoLoad
DEBUG_ParseStabs(char * addr
, unsigned int load_offset
,
608 unsigned int staboff
, int stablen
,
609 unsigned int strtaboff
, int strtablen
)
611 struct name_hash
* curr_func
= NULL
;
612 struct wine_locals
* curr_loc
= NULL
;
613 struct name_hash
* curr_sym
= NULL
;
614 char currpath
[PATH_MAX
];
616 int in_external_file
= FALSE
;
623 unsigned int stabbufflen
;
624 struct stab_nlist
* stab_ptr
;
627 char * subpath
= NULL
;
630 nstab
= stablen
/ sizeof(struct stab_nlist
);
631 stab_ptr
= (struct stab_nlist
*) (addr
+ staboff
);
632 strs
= (char *) (addr
+ strtaboff
);
634 memset(currpath
, 0, sizeof(currpath
));
637 * Allocate a buffer into which we can build stab strings for cases
638 * where the stab is continued over multiple lines.
641 stabbuff
= (char *) DBG_alloc(stabbufflen
);
645 for(i
=0; i
< nstab
; i
++, stab_ptr
++ )
647 ptr
= strs
+ (unsigned int) stab_ptr
->n_un
.n_name
;
648 if( ptr
[strlen(ptr
) - 1] == '\\' )
651 * Indicates continuation. Append this to the buffer, and go onto the
652 * next record. Repeat the process until we find a stab without the
653 * '/' character, as this indicates we have the whole thing.
656 if( strlen(stabbuff
) + len
> stabbufflen
)
658 stabbufflen
+= 65536;
659 stabbuff
= (char *) DBG_realloc(stabbuff
, stabbufflen
);
661 strncat(stabbuff
, ptr
, len
- 1);
664 else if( stabbuff
[0] != '\0' )
666 strcat( stabbuff
, ptr
);
670 if( strchr(ptr
, '=') != NULL
)
673 * The stabs aren't in writable memory, so copy it over so we are
674 * sure we can scribble on it.
676 if( ptr
!= stabbuff
)
678 strcpy(stabbuff
, ptr
);
681 stab_strcpy(symname
, sizeof(symname
), ptr
);
682 if (!DEBUG_ParseTypedefStab(ptr
, symname
)) {
683 /* skip this definition */
689 switch(stab_ptr
->n_type
)
693 * These are useless with ELF. They have no value, and you have to
694 * read the normal symbol table to get the address. Thus we
695 * ignore them, and when we process the normal symbol table
696 * we should do the right thing.
698 * With a.out or mingw, they actually do make some amount of sense.
700 new_value
.addr
.seg
= 0;
701 new_value
.type
= DEBUG_ParseStabType(ptr
);
702 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
703 new_value
.cookie
= DV_TARGET
;
705 stab_strcpy(symname
, sizeof(symname
), ptr
);
707 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
708 SYM_WINE
| SYM_DATA
| SYM_INVALID
);
710 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
711 SYM_WINE
| SYM_DATA
);
717 * We need to keep track of these so we get symbol scoping
718 * right for local variables. For now, we just ignore them.
719 * The hooks are already there for dealing with this however,
720 * so all we need to do is to keep count of the nesting level,
721 * and find the RBRAC for each matching LBRAC.
727 * These are static symbols and BSS symbols.
729 new_value
.addr
.seg
= 0;
730 new_value
.type
= DEBUG_ParseStabType(ptr
);
731 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
732 new_value
.cookie
= DV_TARGET
;
734 stab_strcpy(symname
, sizeof(symname
), ptr
);
735 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
736 SYM_WINE
| SYM_DATA
);
740 * These are function parameters.
742 if( curr_func
!= NULL
&& !in_external_file
)
744 stab_strcpy(symname
, sizeof(symname
), ptr
);
745 curr_loc
= DEBUG_AddLocal( curr_func
, 0,
746 stab_ptr
->n_value
, 0, 0, symname
);
747 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
751 if( curr_func
!= NULL
&& !in_external_file
)
753 stab_strcpy(symname
, sizeof(symname
), ptr
);
754 curr_loc
= DEBUG_AddLocal( curr_func
, stab_ptr
->n_value
+ 1,
756 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
760 if( curr_func
!= NULL
&& !in_external_file
)
762 stab_strcpy(symname
, sizeof(symname
), ptr
);
763 curr_loc
= DEBUG_AddLocal( curr_func
, 0,
764 stab_ptr
->n_value
, 0, 0, symname
);
765 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
770 * This is a line number. These are always relative to the start
771 * of the function (N_FUN), and this makes the lookup easier.
773 if( curr_func
!= NULL
&& !in_external_file
)
776 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
781 * This isn't right. The order of the stabs is different under
782 * a.out, and as a result we would end up attaching the line
783 * number to the wrong function.
785 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
786 stab_ptr
->n_value
- curr_func
->addr
.off
);
793 * First, clean up the previous function we were working on.
795 DEBUG_Normalize(curr_func
);
798 * For now, just declare the various functions. Later
799 * on, we will add the line number information and the
802 if( !in_external_file
)
804 stab_strcpy(symname
, sizeof(symname
), ptr
);
807 new_value
.addr
.seg
= 0;
808 new_value
.type
= DEBUG_ParseStabType(ptr
);
809 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
810 new_value
.cookie
= DV_TARGET
;
812 * Copy the string to a temp buffer so we
813 * can kill everything after the ':'. We do
814 * it this way because otherwise we end up dirtying
815 * all of the pages related to the stabs, and that
816 * sucks up swap space like crazy.
819 curr_func
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
820 SYM_WINE
| SYM_FUNC
| SYM_INVALID
);
822 curr_func
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
823 SYM_WINE
| SYM_FUNC
);
828 /* some GCC seem to use a N_FUN "" to mark the end of a function */
835 * Don't add line number information for this function
843 * This indicates a new source file. Append the records
844 * together, to build the correct path name.
848 * With a.out, there is no NULL string N_SO entry at the end of
849 * the file. Thus when we find non-consecutive entries,
850 * we consider that a new file is started.
855 DEBUG_Normalize(curr_func
);
866 DEBUG_Normalize(curr_func
);
872 strcat(currpath
, ptr
);
874 strcpy(currpath
, ptr
);
876 DEBUG_ResetIncludes();
882 * This indicates we are including stuff from an include file.
883 * If this is the main source, enable the debug stuff, otherwise
886 in_external_file
= !(subpath
== NULL
|| strcmp(ptr
, subpath
) == 0);
890 strtabinc
= stab_ptr
->n_value
;
891 DEBUG_Normalize(curr_func
);
896 * Ignore this. We don't care what it points to.
900 DEBUG_AddInclude(DEBUG_CreateInclude(ptr
, stab_ptr
->n_value
));
905 DEBUG_AddInclude(DEBUG_FindInclude(ptr
, stab_ptr
->n_value
));
909 * Always ignore these. GCC doesn't even generate them.
913 DEBUG_Printf(DBG_CHN_MESG
, "Unknown stab type 0x%02x\n", stab_ptr
->n_type
);
920 DEBUG_Printf(DBG_CHN_MESG
, "%d %x %s\n", stab_ptr
->n_type
,
921 (unsigned int) stab_ptr
->n_value
,
922 strs
+ (unsigned int) stab_ptr
->n_un
.n_name
);
926 DEBUG_FreeIncludes();
934 * Walk through the entire symbol table and add any symbols we find there.
935 * This can be used in cases where we have stripped ELF shared libraries,
936 * or it can be used in cases where we have data symbols for which the address
937 * isn't encoded in the stabs.
939 * This is all really quite easy, since we don't have to worry about line
940 * numbers or local data variables.
942 static int DEBUG_ProcessElfSymtab(DBG_MODULE
* module
, char* addr
,
943 u_long load_addr
, Elf32_Shdr
* symtab
,
946 char * curfile
= NULL
;
947 struct name_hash
* curr_sym
= NULL
;
956 symp
= (Elf32_Sym
*) (addr
+ symtab
->sh_offset
);
957 nsym
= symtab
->sh_size
/ sizeof(*symp
);
958 strp
= (char *) (addr
+ strtab
->sh_offset
);
960 for(i
=0; i
< nsym
; i
++, symp
++)
963 * Ignore certain types of entries which really aren't of that much
966 if( ELF32_ST_TYPE(symp
->st_info
) == STT_SECTION
||
967 symp
->st_shndx
== STN_UNDEF
)
972 symname
= strp
+ symp
->st_name
;
975 * Save the name of the current file, so we have a way of tracking
976 * static functions/data.
978 if( ELF32_ST_TYPE(symp
->st_info
) == STT_FILE
)
985 * See if we already have something for this symbol.
986 * If so, ignore this entry, because it would have come from the
987 * stabs or from a previous symbol. If the value is different,
988 * we will have to keep the darned thing, because there can be
989 * multiple local symbols by the same name.
991 if( (DEBUG_GetSymbolValue(symname
, -1, &new_value
, FALSE
) == TRUE
)
992 && (new_value
.addr
.off
== (load_addr
+ symp
->st_value
)) )
995 new_value
.addr
.seg
= 0;
996 new_value
.type
= NULL
;
997 new_value
.addr
.off
= load_addr
+ symp
->st_value
;
998 new_value
.cookie
= DV_TARGET
;
999 flags
= SYM_WINE
| ((ELF32_ST_TYPE(symp
->st_info
) == STT_FUNC
)
1000 ? SYM_FUNC
: SYM_DATA
);
1001 if( ELF32_ST_BIND(symp
->st_info
) == STB_GLOBAL
)
1002 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, NULL
, flags
);
1004 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, curfile
, flags
);
1007 * Record the size of the symbol. This can come in handy in
1008 * some cases. Not really used yet, however.
1010 if( symp
->st_size
!= 0 )
1011 DEBUG_SetSymbolSize(curr_sym
, symp
->st_size
);
1018 * Loads the symbolic information from ELF module stored in 'filename'
1019 * the module has been loaded at 'load_offset' address, so symbols' address
1020 * relocation is performed
1022 * -1 if the file cannot be found/opened
1023 * 0 if the file doesn't contain symbolic info (or this info cannot be
1027 enum DbgInfoLoad
DEBUG_LoadElfStabs(DBG_MODULE
* module
)
1029 enum DbgInfoLoad dil
= DIL_ERROR
;
1030 char* addr
= (char*)0xffffffff;
1032 struct stat statbuf
;
1040 if (module
->type
!= DMT_ELF
|| ! module
->elf_info
) {
1041 DEBUG_Printf(DBG_CHN_ERR
, "Bad elf module '%s'\n", module
->module_name
);
1045 /* check that the file exists, and that the module hasn't been loaded yet */
1046 if (stat(module
->module_name
, &statbuf
) == -1) goto leave
;
1047 if (S_ISDIR(statbuf
.st_mode
)) goto leave
;
1050 * Now open the file, so that we can mmap() it.
1052 if ((fd
= open(module
->module_name
, O_RDONLY
)) == -1) goto leave
;
1056 * Now mmap() the file.
1058 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1059 if (addr
== (char*)0xffffffff) goto leave
;
1062 * Next, we need to find a few of the internal ELF headers within
1063 * this thing. We need the main executable header, and the section
1066 ehptr
= (Elf32_Ehdr
*) addr
;
1067 spnt
= (Elf32_Shdr
*) (addr
+ ehptr
->e_shoff
);
1068 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
1070 stabsect
= stabstrsect
= -1;
1072 for (i
= 0; i
< ehptr
->e_shnum
; i
++) {
1073 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stab") == 0)
1076 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stabstr") == 0)
1080 if (stabsect
== -1 || stabstrsect
== -1) {
1081 DEBUG_Printf(DBG_CHN_WARN
, "no .stab section\n");
1086 * OK, now just parse all of the stabs.
1088 if (DEBUG_ParseStabs(addr
,
1089 module
->elf_info
->elf_addr
,
1090 spnt
[stabsect
].sh_offset
,
1091 spnt
[stabsect
].sh_size
,
1092 spnt
[stabstrsect
].sh_offset
,
1093 spnt
[stabstrsect
].sh_size
)) {
1097 DEBUG_Printf(DBG_CHN_WARN
, "bad stabs\n");
1101 for (i
= 0; i
< ehptr
->e_shnum
; i
++) {
1102 if ( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".symtab") == 0)
1103 && (spnt
[i
].sh_type
== SHT_SYMTAB
))
1104 DEBUG_ProcessElfSymtab(module
, addr
, module
->elf_info
->elf_addr
,
1105 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
1107 if ( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynsym") == 0)
1108 && (spnt
[i
].sh_type
== SHT_DYNSYM
))
1109 DEBUG_ProcessElfSymtab(module
, addr
, module
->elf_info
->elf_addr
,
1110 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
1114 if (addr
!= (char*)0xffffffff) munmap(addr
, statbuf
.st_size
);
1115 if (fd
!= -1) close(fd
);
1121 * Loads the information for ELF module stored in 'filename'
1122 * the module has been loaded at 'load_offset' address
1124 * -1 if the file cannot be found/opened
1125 * 0 if the file doesn't contain symbolic info (or this info cannot be
1129 static enum DbgInfoLoad
DEBUG_ProcessElfFile(const char* filename
,
1130 unsigned int load_offset
,
1131 unsigned int* dyn_addr
)
1133 enum DbgInfoLoad dil
= DIL_ERROR
;
1134 char* addr
= (char*)0xffffffff;
1136 struct stat statbuf
;
1142 DBG_MODULE
* module
= NULL
;
1146 DEBUG_Printf(DBG_CHN_TRACE
, "Processing elf file '%s'\n", filename
);
1148 /* check that the file exists, and that the module hasn't been loaded yet */
1149 if (stat(filename
, &statbuf
) == -1) goto leave
;
1152 * Now open the file, so that we can mmap() it.
1154 if ((fd
= open(filename
, O_RDONLY
)) == -1) goto leave
;
1157 * Now mmap() the file.
1159 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1160 if (addr
== (char*)0xffffffff) goto leave
;
1165 * Next, we need to find a few of the internal ELF headers within
1166 * this thing. We need the main executable header, and the section
1169 ehptr
= (Elf32_Ehdr
*) addr
;
1170 spnt
= (Elf32_Shdr
*) (addr
+ ehptr
->e_shoff
);
1171 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
1173 /* if non relocatable ELF, then remove fixed address from computation
1174 * otherwise, all addresses are zero based
1176 delta
= (load_offset
== 0) ? ehptr
->e_entry
: 0;
1178 /* grab size of module once loaded in memory */
1179 ppnt
= (Elf32_Phdr
*) (addr
+ ehptr
->e_phoff
);
1181 for (i
= 0; i
< ehptr
->e_phnum
; i
++) {
1182 if (ppnt
[i
].p_type
!= PT_LOAD
) continue;
1183 if (size
< ppnt
[i
].p_vaddr
- delta
+ ppnt
[i
].p_memsz
)
1184 size
= ppnt
[i
].p_vaddr
- delta
+ ppnt
[i
].p_memsz
;
1187 for (i
= 0; i
< ehptr
->e_shnum
; i
++) {
1188 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".bss") == 0 &&
1189 spnt
[i
].sh_type
== SHT_NOBITS
) {
1190 if (size
< spnt
[i
].sh_addr
- delta
+ spnt
[i
].sh_size
)
1191 size
= spnt
[i
].sh_addr
- delta
+ spnt
[i
].sh_size
;
1193 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynamic") == 0 &&
1194 spnt
[i
].sh_type
== SHT_DYNAMIC
) {
1195 if (dyn_addr
) *dyn_addr
= spnt
[i
].sh_addr
;
1199 module
= DEBUG_RegisterELFModule((load_offset
== 0) ? ehptr
->e_entry
: load_offset
,
1206 if ((module
->elf_info
= DBG_alloc(sizeof(ELF_DBG_INFO
))) == NULL
) {
1207 DEBUG_Printf(DBG_CHN_ERR
, "OOM\n");
1211 module
->elf_info
->elf_addr
= load_offset
;
1212 dil
= DEBUG_LoadElfStabs(module
);
1215 if (addr
!= (char*)0xffffffff) munmap(addr
, statbuf
.st_size
);
1216 if (fd
!= -1) close(fd
);
1217 if (module
) module
->dil
= dil
;
1222 static enum DbgInfoLoad
DEBUG_ProcessElfFileFromPath(const char * filename
,
1223 unsigned int load_offset
,
1224 unsigned int* dyn_addr
,
1227 enum DbgInfoLoad dil
= DIL_ERROR
;
1231 if (!path
) return -1;
1233 for (s
= paths
= DBG_strdup(path
); s
&& *s
; s
= (t
) ? (t
+1) : NULL
) {
1236 fn
= (char*)DBG_alloc(strlen(filename
) + 1 + strlen(s
) + 1);
1240 strcat(fn
, filename
);
1241 dil
= DEBUG_ProcessElfFile(fn
, load_offset
, dyn_addr
);
1243 if (dil
!= DIL_ERROR
) break;
1244 s
= (t
) ? (t
+1) : NULL
;
1251 static enum DbgInfoLoad
DEBUG_ProcessElfObject(const char* filename
,
1252 unsigned int load_offset
,
1253 unsigned int* dyn_addr
)
1255 enum DbgInfoLoad dil
= DIL_ERROR
;
1257 if (filename
== NULL
) return DIL_ERROR
;
1258 if (DEBUG_FindModuleByName(filename
, DMT_ELF
)) return DIL_LOADED
;
1260 dil
= DEBUG_ProcessElfFile(filename
, load_offset
, dyn_addr
);
1262 /* if relative pathname, try some absolute base dirs */
1263 if (dil
== DIL_ERROR
&& !strchr(filename
, '/')) {
1264 dil
= DEBUG_ProcessElfFileFromPath(filename
, load_offset
, dyn_addr
, getenv("PATH"));
1265 if (dil
== DIL_ERROR
)
1266 dil
= DEBUG_ProcessElfFileFromPath(filename
, load_offset
, dyn_addr
, getenv("LD_LIBRARY_PATH"));
1269 DEBUG_ReportDIL(dil
, "ELF", filename
, load_offset
);
1274 static BOOL
DEBUG_WalkList(struct r_debug
* dbg_hdr
)
1282 * Now walk the linked list. In all known ELF implementations,
1283 * the dynamic loader maintains this linked list for us. In some
1284 * cases the first entry doesn't appear with a name, in other cases it
1287 for (lm_addr
= (u_long
)dbg_hdr
->r_map
; lm_addr
; lm_addr
= (u_long
)lm
.l_next
) {
1288 if (!DEBUG_READ_MEM_VERBOSE((void*)lm_addr
, &lm
, sizeof(lm
)))
1290 if (lm
.l_addr
!= 0 &&
1291 DEBUG_READ_MEM_VERBOSE((void*)lm
.l_addr
, &ehdr
, sizeof(ehdr
)) &&
1292 ehdr
.e_type
== ET_DYN
&& /* only look at dynamic modules */
1293 lm
.l_name
!= NULL
&&
1294 DEBUG_READ_MEM_VERBOSE((void*)lm
.l_name
, bufstr
, sizeof(bufstr
))) {
1295 bufstr
[sizeof(bufstr
) - 1] = '\0';
1296 DEBUG_ProcessElfObject(bufstr
, (unsigned)lm
.l_addr
, NULL
);
1303 static BOOL
DEBUG_RescanElf(void)
1305 struct r_debug dbg_hdr
;
1307 if (!DEBUG_CurrProcess
||
1308 !DEBUG_READ_MEM_VERBOSE((void*)DEBUG_CurrProcess
->dbg_hdr_addr
, &dbg_hdr
, sizeof(dbg_hdr
)))
1311 switch (dbg_hdr
.r_state
) {
1313 DEBUG_WalkList(&dbg_hdr
);
1314 DEBUG_CheckDelayedBP();
1319 /* FIXME: this is not currently handled, would need some kind of mark&sweep algo */
1325 enum DbgInfoLoad
DEBUG_ReadExecutableDbgInfo(const char* exe_name
)
1328 struct r_debug dbg_hdr
;
1329 enum DbgInfoLoad dil
= DIL_NOINFO
;
1330 unsigned int dyn_addr
;
1333 * Make sure we can stat and open this file.
1335 if (exe_name
== NULL
) goto leave
;
1336 DEBUG_ProcessElfObject(exe_name
, 0, &dyn_addr
);
1339 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn_addr
, &dyn
, sizeof(dyn
)))
1341 dyn_addr
+= sizeof(dyn
);
1342 } while (dyn
.d_tag
!= DT_DEBUG
&& dyn
.d_tag
!= DT_NULL
);
1343 if (dyn
.d_tag
== DT_NULL
) goto leave
;
1346 * OK, now dig into the actual tables themselves.
1348 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn
.d_un
.d_ptr
, &dbg_hdr
, sizeof(dbg_hdr
)))
1351 assert(!DEBUG_CurrProcess
->dbg_hdr_addr
);
1352 DEBUG_CurrProcess
->dbg_hdr_addr
= (u_long
)dyn
.d_un
.d_ptr
;
1354 if (dbg_hdr
.r_brk
) {
1357 DEBUG_Printf(DBG_CHN_TRACE
, "Setting up a breakpoint on r_brk(%lx)\n",
1358 (unsigned long)dbg_hdr
.r_brk
);
1360 DEBUG_SetBreakpoints(FALSE
);
1362 value
.cookie
= DV_TARGET
;
1364 value
.addr
.off
= (DWORD
)dbg_hdr
.r_brk
;
1365 DEBUG_AddBreakpoint(&value
, DEBUG_RescanElf
);
1366 DEBUG_SetBreakpoints(TRUE
);
1369 dil
= DEBUG_WalkList(&dbg_hdr
);
1375 #else /* !__ELF__ */
1377 enum DbgInfoLoad
DEBUG_ReadExecutableDbgInfo(const char* exe_name
)
1382 #endif /* __ELF__ */