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)
68 typedef struct tagELF_DBG_INFO
{
69 unsigned long elf_addr
;
75 struct stab_nlist
*n_next
;
81 unsigned long n_value
;
84 static void stab_strcpy(char * dest
, int sz
, const char * source
)
87 * A strcpy routine that stops when we hit the ':' character.
88 * Faster than copying the whole thing, and then nuking the
91 while(*source
!= '\0' && *source
!= ':' && sz
-- > 0)
101 struct datatype
** vector
;
105 #define MAX_INCLUDES 512
107 static include_def
* include_defs
= NULL
;
108 static int num_include_def
= 0;
109 static int num_alloc_include_def
= 0;
110 static int cu_include_stack
[MAX_INCLUDES
];
111 static int cu_include_stk_idx
= 0;
112 static struct datatype
** cu_vector
= NULL
;
113 static int cu_nrofentries
= 0;
117 DEBUG_CreateInclude(const char* file
, unsigned long val
)
119 if (num_include_def
== num_alloc_include_def
)
121 num_alloc_include_def
+= 256;
122 include_defs
= DBG_realloc(include_defs
, sizeof(include_defs
[0])*num_alloc_include_def
);
123 memset(include_defs
+num_include_def
, 0, sizeof(include_defs
[0])*256);
125 include_defs
[num_include_def
].name
= DBG_strdup(file
);
126 include_defs
[num_include_def
].value
= val
;
127 include_defs
[num_include_def
].vector
= NULL
;
128 include_defs
[num_include_def
].nrofentries
= 0;
130 return num_include_def
++;
135 DEBUG_FindInclude(const char* file
, unsigned long val
)
139 for (i
= 0; i
< num_include_def
; i
++)
141 if (val
== include_defs
[i
].value
&&
142 strcmp(file
, include_defs
[i
].name
) == 0)
150 DEBUG_AddInclude(int idx
)
152 ++cu_include_stk_idx
;
154 /* is this happen, just bump MAX_INCLUDES */
155 /* we could also handle this as another dynarray */
156 assert(cu_include_stk_idx
< MAX_INCLUDES
);
158 cu_include_stack
[cu_include_stk_idx
] = idx
;
159 return cu_include_stk_idx
;
164 DEBUG_ResetIncludes(void)
167 * The datatypes that we would need to use are reset when
168 * we start a new file. (at least the ones in filenr == 0
170 cu_include_stk_idx
= 0;/* keep 0 as index for the .c file itself */
171 memset(cu_vector
, 0, sizeof(cu_vector
[0]) * cu_nrofentries
);
176 DEBUG_FreeIncludes(void)
180 DEBUG_ResetIncludes();
182 for (i
= 0; i
< num_include_def
; i
++)
184 DBG_free(include_defs
[i
].name
);
185 DBG_free(include_defs
[i
].vector
);
187 DBG_free(include_defs
);
190 num_alloc_include_def
= 0;
198 DEBUG_FileSubNr2StabEnum(int filenr
, int subnr
)
200 struct datatype
** ret
;
202 /* DEBUG_Printf(DBG_CHN_MESG, "creating type id for (%d,%d)\n", filenr, subnr); */
204 /* FIXME: I could perhaps create a dummy include_def for each compilation
205 * unit which would allow not to handle those two cases separately
209 if (cu_nrofentries
<= subnr
)
211 cu_vector
= DBG_realloc(cu_vector
, sizeof(cu_vector
[0])*(subnr
+1));
212 memset(cu_vector
+cu_nrofentries
, 0, sizeof(cu_vector
[0])*(subnr
+1-cu_nrofentries
));
213 cu_nrofentries
= subnr
+ 1;
215 ret
= &cu_vector
[subnr
];
221 assert(filenr
<= cu_include_stk_idx
);
223 idef
= &include_defs
[cu_include_stack
[filenr
]];
225 if (idef
->nrofentries
<= subnr
)
227 idef
->vector
= DBG_realloc(idef
->vector
, sizeof(idef
->vector
[0])*(subnr
+1));
228 memset(idef
->vector
+ idef
->nrofentries
, 0, sizeof(idef
->vector
[0])*(subnr
+1-idef
->nrofentries
));
229 idef
->nrofentries
= subnr
+ 1;
231 ret
= &idef
->vector
[subnr
];
233 /* DEBUG_Printf(DBG_CHN_MESG,"(%d,%d) is %d\n",filenr,subnr,ret); */
239 DEBUG_ReadTypeEnum(char **x
) {
244 filenr
=strtol(*x
,x
,10); /* <int> */
246 subnr
=strtol(*x
,x
,10); /* <int> */
250 subnr
= strtol(*x
,x
,10); /* <int> */
252 return DEBUG_FileSubNr2StabEnum(filenr
,subnr
);
255 struct ParseTypedefData
{
261 static int DEBUG_PTS_ReadTypedef(struct ParseTypedefData
* ptd
, const char* typename
,
262 struct datatype
** dt
);
264 static int DEBUG_PTS_ReadID(struct ParseTypedefData
* ptd
)
266 char* first
= ptd
->ptr
;
269 if ((ptd
->ptr
= strchr(ptd
->ptr
, ':')) == NULL
) return -1;
270 len
= ptd
->ptr
- first
;
271 if (len
>= sizeof(ptd
->buf
) - ptd
->idx
) return -1;
272 memcpy(ptd
->buf
+ ptd
->idx
, first
, len
);
273 ptd
->buf
[ptd
->idx
+ len
] = '\0';
275 ptd
->ptr
++; /* ':' */
279 static int DEBUG_PTS_ReadNum(struct ParseTypedefData
* ptd
, int* v
)
283 *v
= strtol(ptd
->ptr
, &last
, 10);
284 if (last
== ptd
->ptr
) return -1;
289 static int DEBUG_PTS_ReadTypeReference(struct ParseTypedefData
* ptd
,
290 int* filenr
, int* subnr
)
292 if (*ptd
->ptr
== '(') {
293 /* '(' <int> ',' <int> ')' */
295 if (DEBUG_PTS_ReadNum(ptd
, filenr
) == -1) return -1;
296 if (*ptd
->ptr
++ != ',') return -1;
297 if (DEBUG_PTS_ReadNum(ptd
, subnr
) == -1) return -1;
298 if (*ptd
->ptr
++ != ')') return -1;
301 if (DEBUG_PTS_ReadNum(ptd
, subnr
) == -1) return -1;
306 static int DEBUG_PTS_ReadRange(struct ParseTypedefData
* ptd
, struct datatype
** dt
,
309 /* type ';' <int> ';' <int> ';' */
310 if (DEBUG_PTS_ReadTypedef(ptd
, NULL
, dt
) == -1) return -1;
311 if (*ptd
->ptr
++ != ';') return -1; /* ';' */
312 if (DEBUG_PTS_ReadNum(ptd
, lo
) == -1) return -1;
313 if (*ptd
->ptr
++ != ';') return -1; /* ';' */
314 if (DEBUG_PTS_ReadNum(ptd
, hi
) == -1) return -1;
315 if (*ptd
->ptr
++ != ';') return -1; /* ';' */
319 static inline int DEBUG_PTS_ReadAggregate(struct ParseTypedefData
* ptd
, struct datatype
* sdt
)
323 struct datatype
* adt
;
327 sz
= strtol(ptd
->ptr
, &last
, 10);
328 if (last
== ptd
->ptr
) return -1;
331 doadd
= DEBUG_SetStructSize(sdt
, sz
);
332 /* if the structure has already been filled, just redo the parsing
333 * but don't store results into the struct
334 * FIXME: there's a quite ugly memory leak in there...
337 /* Now parse the individual elements of the structure/union. */
338 while (*ptd
->ptr
!= ';') {
339 /* agg_name : type ',' <int:offset> ',' <int:size> */
341 if (DEBUG_PTS_ReadID(ptd
) == -1) return -1;
343 if (DEBUG_PTS_ReadTypedef(ptd
, NULL
, &adt
) == -1) return -1;
346 if (*ptd
->ptr
++ != ',') return -1;
347 if (DEBUG_PTS_ReadNum(ptd
, &ofs
) == -1) return -1;
348 if (*ptd
->ptr
++ != ',') return -1;
349 if (DEBUG_PTS_ReadNum(ptd
, &sz
) == -1) return -1;
350 if (*ptd
->ptr
++ != ';') return -1;
352 if (doadd
) DEBUG_AddStructElement(sdt
, ptd
->buf
+ idx
, adt
, ofs
, sz
);
355 ptd
->ptr
++; /* ';' */
359 static inline int DEBUG_PTS_ReadEnum(struct ParseTypedefData
* ptd
, struct datatype
* edt
)
364 while (*ptd
->ptr
!= ';') {
366 if (DEBUG_PTS_ReadID(ptd
) == -1) return -1;
367 if (DEBUG_PTS_ReadNum(ptd
, &ofs
) == -1) return -1;
368 if (*ptd
->ptr
++ != ',') return -1;
369 DEBUG_AddStructElement(edt
, ptd
->buf
+ idx
, NULL
, ofs
, 0);
376 static inline int DEBUG_PTS_ReadArray(struct ParseTypedefData
* ptd
, struct datatype
* adt
)
379 struct datatype
* rdt
;
381 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
383 if (*ptd
->ptr
++ != 'r') return -1;
384 /* FIXME: range type is lost, always assume int */
385 if (DEBUG_PTS_ReadRange(ptd
, &rdt
, &lo
, &hi
) == -1) return -1;
386 if (DEBUG_PTS_ReadTypedef(ptd
, NULL
, &rdt
) == -1) return -1;
388 DEBUG_SetArrayParams(adt
, lo
, hi
, rdt
);
392 static int DEBUG_PTS_ReadTypedef(struct ParseTypedefData
* ptd
, const char* typename
,
393 struct datatype
** ret_dt
)
396 struct datatype
* new_dt
= NULL
; /* newly created data type */
397 struct datatype
* ref_dt
; /* referenced data type (pointer...) */
398 struct datatype
* dt1
; /* intermediate data type (scope is limited) */
399 struct datatype
* dt2
; /* intermediate data type: t1=t2=new_dt */
401 int filenr2
= 0, subnr2
= 0;
403 /* things are a bit complicated because of the way the typedefs are stored inside
404 * the file (we cannot keep the struct datatype** around, because address can
405 * change when realloc is done, so we must call over and over
406 * DEBUG_FileSubNr2StabEnum to keep the correct values around
407 * (however, keeping struct datatype* is valid
409 if (DEBUG_PTS_ReadTypeReference(ptd
, &filenr1
, &subnr1
) == -1) return -1;
411 while (*ptd
->ptr
== '=') {
414 DEBUG_Printf(DBG_CHN_MESG
, "Bad recursion (1) in typedef\n");
417 /* first handle attribute if any */
420 if (*++ptd
->ptr
== 's') {
422 if (DEBUG_PTS_ReadNum(ptd
, &lo
) == -1) {
423 DEBUG_Printf(DBG_CHN_MESG
, "Not an attribute... NIY\n");
427 if (*ptd
->ptr
++ != ';') return -1;
431 /* then the real definitions */
432 switch (*ptd
->ptr
++) {
434 new_dt
= DEBUG_NewDataType(DT_POINTER
, NULL
);
435 if (DEBUG_PTS_ReadTypedef(ptd
, NULL
, &ref_dt
) == -1) return -1;
436 DEBUG_SetPointerType(new_dt
, ref_dt
);
440 /* doit a two level by hand, otherwise we'd need a stack */
441 if (filenr2
|| subnr2
) {
442 DEBUG_Printf(DBG_CHN_MESG
, "Bad recursion (2) in typedef\n");
445 if (DEBUG_PTS_ReadTypeReference(ptd
, &filenr2
, &subnr2
) == -1) return -1;
447 dt1
= *DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
);
448 dt2
= *DEBUG_FileSubNr2StabEnum(filenr2
, subnr2
);
452 filenr2
= subnr2
= 0;
453 } else if (!dt1
&& !dt2
) {
456 DEBUG_Printf(DBG_CHN_MESG
, "Unknown condition %08lx %08lx (%s)\n",
457 (unsigned long)dt1
, (unsigned long)dt2
, ptd
->ptr
);
462 new_dt
= DEBUG_NewDataType(DT_ARRAY
, NULL
);
463 if (DEBUG_PTS_ReadArray(ptd
, new_dt
) == -1) return -1;
466 new_dt
= DEBUG_NewDataType(DT_BASIC
, typename
);
467 assert(!*DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
));
468 *DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
) = new_dt
;
469 if (DEBUG_PTS_ReadRange(ptd
, &ref_dt
, &lo
, &hi
) == -1) return -1;
470 /* should perhaps do more here... */
473 new_dt
= DEBUG_NewDataType(DT_FUNC
, NULL
);
474 if (DEBUG_PTS_ReadTypedef(ptd
, NULL
, &ref_dt
) == -1) return -1;
475 DEBUG_SetPointerType(new_dt
, ref_dt
);
478 new_dt
= DEBUG_NewDataType(DT_ENUM
, NULL
);
479 if (DEBUG_PTS_ReadEnum(ptd
, new_dt
) == -1) return -1;
483 /* dt1 can have been already defined in a forward definition */
484 dt1
= *DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
);
485 dt2
= DEBUG_TypeCast(DT_STRUCT
, typename
);
487 new_dt
= DEBUG_NewDataType(DT_STRUCT
, typename
);
488 /* we need to set it here, because a struct can hold a pointer
491 *DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
) = new_dt
;
493 if (DEBUG_GetType(dt1
) != DT_STRUCT
) {
494 DEBUG_Printf(DBG_CHN_MESG
,
495 "Forward declaration is not an aggregate\n");
499 /* should check typename is the same too */
502 if (DEBUG_PTS_ReadAggregate(ptd
, new_dt
) == -1) return -1;
505 switch (*ptd
->ptr
++) {
506 case 'e': lo
= DT_ENUM
; break;
507 case 's': case 'u': lo
= DT_STRUCT
; break;
512 if (DEBUG_PTS_ReadID(ptd
) == -1) return -1;
513 new_dt
= DEBUG_NewDataType(lo
, ptd
->buf
+ idx
);
517 DEBUG_Printf(DBG_CHN_MESG
, "Unknown type '%c'\n", *ptd
->ptr
);
522 if ((filenr2
|| subnr2
) && !*DEBUG_FileSubNr2StabEnum(filenr2
, subnr2
)) {
524 /* this should be a basic type, define it, or even void */
525 new_dt
= DEBUG_NewDataType(DT_BASIC
, typename
);
527 *DEBUG_FileSubNr2StabEnum(filenr2
, subnr2
) = new_dt
;
531 dt1
= *DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
);
533 DEBUG_Printf(DBG_CHN_MESG
, "Nothing has been defined <%s>\n", ptd
->ptr
);
540 *DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
) = *ret_dt
= new_dt
;
544 DEBUG_Printf(DBG_CHN_MESG
, "Adding (%d,%d) %s => ", filenr1
, subnr1
, typename
);
545 DEBUG_PrintTypeCast(new_dt
);
546 DEBUG_Printf(DBG_CHN_MESG
, "\n");
553 static int DEBUG_ParseTypedefStab(char* ptr
, const char* typename
)
555 struct ParseTypedefData ptd
;
559 /* check for already existing definition */
562 if ((ptd
.ptr
= strchr(ptr
, ':'))) {
564 if (*ptd
.ptr
!= '(') ptd
.ptr
++;
565 ret
= DEBUG_PTS_ReadTypedef(&ptd
, typename
, &dt
);
568 if (ret
== -1 || *ptd
.ptr
) {
569 DEBUG_Printf(DBG_CHN_MESG
, "failure on %s at %s\n", ptr
, ptd
.ptr
);
576 static struct datatype
*
577 DEBUG_ParseStabType(const char * stab
)
582 * Look through the stab definition, and figure out what datatype
583 * this represents. If we have something we know about, assign the
586 c
= strchr(stab
, ':');
592 * The next character says more about the type (i.e. data, function, etc)
593 * of symbol. Skip it.
598 * The next is either an integer or a (integer,integer).
599 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
601 return *DEBUG_ReadTypeEnum(&c
);
604 enum DbgInfoLoad
DEBUG_ParseStabs(char * addr
, unsigned int load_offset
,
605 unsigned int staboff
, int stablen
,
606 unsigned int strtaboff
, int strtablen
)
608 struct name_hash
* curr_func
= NULL
;
609 struct wine_locals
* curr_loc
= NULL
;
610 struct name_hash
* curr_sym
= NULL
;
611 char currpath
[PATH_MAX
];
613 int in_external_file
= FALSE
;
620 unsigned int stabbufflen
;
621 struct stab_nlist
* stab_ptr
;
624 char * subpath
= NULL
;
627 nstab
= stablen
/ sizeof(struct stab_nlist
);
628 stab_ptr
= (struct stab_nlist
*) (addr
+ staboff
);
629 strs
= (char *) (addr
+ strtaboff
);
631 memset(currpath
, 0, sizeof(currpath
));
634 * Allocate a buffer into which we can build stab strings for cases
635 * where the stab is continued over multiple lines.
638 stabbuff
= (char *) DBG_alloc(stabbufflen
);
642 for(i
=0; i
< nstab
; i
++, stab_ptr
++ )
644 ptr
= strs
+ (unsigned int) stab_ptr
->n_un
.n_name
;
645 if( ptr
[strlen(ptr
) - 1] == '\\' )
648 * Indicates continuation. Append this to the buffer, and go onto the
649 * next record. Repeat the process until we find a stab without the
650 * '/' character, as this indicates we have the whole thing.
653 if( strlen(stabbuff
) + len
> stabbufflen
)
655 stabbufflen
+= 65536;
656 stabbuff
= (char *) DBG_realloc(stabbuff
, stabbufflen
);
658 strncat(stabbuff
, ptr
, len
- 1);
661 else if( stabbuff
[0] != '\0' )
663 strcat( stabbuff
, ptr
);
667 if( strchr(ptr
, '=') != NULL
)
670 * The stabs aren't in writable memory, so copy it over so we are
671 * sure we can scribble on it.
673 if( ptr
!= stabbuff
)
675 strcpy(stabbuff
, ptr
);
678 stab_strcpy(symname
, sizeof(symname
), ptr
);
679 if (!DEBUG_ParseTypedefStab(ptr
, symname
)) {
680 /* skip this definition */
686 switch(stab_ptr
->n_type
)
690 * These are useless with ELF. They have no value, and you have to
691 * read the normal symbol table to get the address. Thus we
692 * ignore them, and when we process the normal symbol table
693 * we should do the right thing.
695 * With a.out or mingw, they actually do make some amount of sense.
697 new_value
.addr
.seg
= 0;
698 new_value
.type
= DEBUG_ParseStabType(ptr
);
699 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
700 new_value
.cookie
= DV_TARGET
;
702 stab_strcpy(symname
, sizeof(symname
), ptr
);
704 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
705 SYM_WINE
| SYM_DATA
| SYM_INVALID
);
707 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
708 SYM_WINE
| SYM_DATA
);
714 * We need to keep track of these so we get symbol scoping
715 * right for local variables. For now, we just ignore them.
716 * The hooks are already there for dealing with this however,
717 * so all we need to do is to keep count of the nesting level,
718 * and find the RBRAC for each matching LBRAC.
724 * These are static symbols and BSS symbols.
726 new_value
.addr
.seg
= 0;
727 new_value
.type
= DEBUG_ParseStabType(ptr
);
728 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
729 new_value
.cookie
= DV_TARGET
;
731 stab_strcpy(symname
, sizeof(symname
), ptr
);
732 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
733 SYM_WINE
| SYM_DATA
);
737 * These are function parameters.
739 if( curr_func
!= NULL
&& !in_external_file
)
741 stab_strcpy(symname
, sizeof(symname
), ptr
);
742 curr_loc
= DEBUG_AddLocal( curr_func
, 0,
743 stab_ptr
->n_value
, 0, 0, symname
);
744 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
748 if( curr_func
!= NULL
&& !in_external_file
)
750 stab_strcpy(symname
, sizeof(symname
), ptr
);
751 curr_loc
= DEBUG_AddLocal( curr_func
, stab_ptr
->n_value
+ 1,
753 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
757 if( curr_func
!= NULL
&& !in_external_file
)
759 stab_strcpy(symname
, sizeof(symname
), ptr
);
760 curr_loc
= DEBUG_AddLocal( curr_func
, 0,
761 stab_ptr
->n_value
, 0, 0, symname
);
762 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
767 * This is a line number. These are always relative to the start
768 * of the function (N_FUN), and this makes the lookup easier.
770 if( curr_func
!= NULL
&& !in_external_file
)
773 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
778 * This isn't right. The order of the stabs is different under
779 * a.out, and as a result we would end up attaching the line
780 * number to the wrong function.
782 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
783 stab_ptr
->n_value
- curr_func
->addr
.off
);
790 * First, clean up the previous function we were working on.
792 DEBUG_Normalize(curr_func
);
795 * For now, just declare the various functions. Later
796 * on, we will add the line number information and the
799 if( !in_external_file
)
801 stab_strcpy(symname
, sizeof(symname
), ptr
);
804 new_value
.addr
.seg
= 0;
805 new_value
.type
= DEBUG_ParseStabType(ptr
);
806 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
807 new_value
.cookie
= DV_TARGET
;
809 * Copy the string to a temp buffer so we
810 * can kill everything after the ':'. We do
811 * it this way because otherwise we end up dirtying
812 * all of the pages related to the stabs, and that
813 * sucks up swap space like crazy.
816 curr_func
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
817 SYM_WINE
| SYM_FUNC
| SYM_INVALID
);
819 curr_func
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
820 SYM_WINE
| SYM_FUNC
);
825 /* some GCC seem to use a N_FUN "" to mark the end of a function */
832 * Don't add line number information for this function
840 * This indicates a new source file. Append the records
841 * together, to build the correct path name.
845 * With a.out, there is no NULL string N_SO entry at the end of
846 * the file. Thus when we find non-consecutive entries,
847 * we consider that a new file is started.
852 DEBUG_Normalize(curr_func
);
863 DEBUG_Normalize(curr_func
);
869 strcat(currpath
, ptr
);
871 strcpy(currpath
, ptr
);
873 DEBUG_ResetIncludes();
879 * This indicates we are including stuff from an include file.
880 * If this is the main source, enable the debug stuff, otherwise
883 in_external_file
= !(subpath
== NULL
|| strcmp(ptr
, subpath
) == 0);
887 strtabinc
= stab_ptr
->n_value
;
888 DEBUG_Normalize(curr_func
);
893 * Ignore this. We don't care what it points to.
897 DEBUG_AddInclude(DEBUG_CreateInclude(ptr
, stab_ptr
->n_value
));
902 DEBUG_AddInclude(DEBUG_FindInclude(ptr
, stab_ptr
->n_value
));
906 * Always ignore these. GCC doesn't even generate them.
910 DEBUG_Printf(DBG_CHN_MESG
, "Unknown stab type 0x%02x\n", stab_ptr
->n_type
);
917 DEBUG_Printf(DBG_CHN_MESG
, "%d %x %s\n", stab_ptr
->n_type
,
918 (unsigned int) stab_ptr
->n_value
,
919 strs
+ (unsigned int) stab_ptr
->n_un
.n_name
);
923 DEBUG_FreeIncludes();
931 * Walk through the entire symbol table and add any symbols we find there.
932 * This can be used in cases where we have stripped ELF shared libraries,
933 * or it can be used in cases where we have data symbols for which the address
934 * isn't encoded in the stabs.
936 * This is all really quite easy, since we don't have to worry about line
937 * numbers or local data variables.
939 static int DEBUG_ProcessElfSymtab(DBG_MODULE
* module
, char* addr
,
940 u_long load_addr
, Elf32_Shdr
* symtab
,
943 char * curfile
= NULL
;
944 struct name_hash
* curr_sym
= NULL
;
953 symp
= (Elf32_Sym
*) (addr
+ symtab
->sh_offset
);
954 nsym
= symtab
->sh_size
/ sizeof(*symp
);
955 strp
= (char *) (addr
+ strtab
->sh_offset
);
957 for(i
=0; i
< nsym
; i
++, symp
++)
960 * Ignore certain types of entries which really aren't of that much
963 if( ELF32_ST_TYPE(symp
->st_info
) == STT_SECTION
||
964 symp
->st_shndx
== STN_UNDEF
)
969 symname
= strp
+ symp
->st_name
;
972 * Save the name of the current file, so we have a way of tracking
973 * static functions/data.
975 if( ELF32_ST_TYPE(symp
->st_info
) == STT_FILE
)
982 * See if we already have something for this symbol.
983 * If so, ignore this entry, because it would have come from the
984 * stabs or from a previous symbol. If the value is different,
985 * we will have to keep the darned thing, because there can be
986 * multiple local symbols by the same name.
988 if( (DEBUG_GetSymbolValue(symname
, -1, &new_value
, FALSE
) == TRUE
)
989 && (new_value
.addr
.off
== (load_addr
+ symp
->st_value
)) )
992 new_value
.addr
.seg
= 0;
993 new_value
.type
= NULL
;
994 new_value
.addr
.off
= load_addr
+ symp
->st_value
;
995 new_value
.cookie
= DV_TARGET
;
996 flags
= SYM_WINE
| ((ELF32_ST_TYPE(symp
->st_info
) == STT_FUNC
)
997 ? SYM_FUNC
: SYM_DATA
);
998 if( ELF32_ST_BIND(symp
->st_info
) == STB_GLOBAL
)
999 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, NULL
, flags
);
1001 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, curfile
, flags
);
1004 * Record the size of the symbol. This can come in handy in
1005 * some cases. Not really used yet, however.
1007 if( symp
->st_size
!= 0 )
1008 DEBUG_SetSymbolSize(curr_sym
, symp
->st_size
);
1015 * Loads the symbolic information from ELF module stored in 'filename'
1016 * the module has been loaded at 'load_offset' address, so symbols' address
1017 * relocation is performed
1019 * -1 if the file cannot be found/opened
1020 * 0 if the file doesn't contain symbolic info (or this info cannot be
1024 enum DbgInfoLoad
DEBUG_LoadElfStabs(DBG_MODULE
* module
)
1026 enum DbgInfoLoad dil
= DIL_ERROR
;
1027 char* addr
= (char*)0xffffffff;
1029 struct stat statbuf
;
1037 if (module
->type
!= DMT_ELF
|| ! module
->elf_info
) {
1038 DEBUG_Printf(DBG_CHN_ERR
, "Bad elf module '%s'\n", module
->module_name
);
1042 /* check that the file exists, and that the module hasn't been loaded yet */
1043 if (stat(module
->module_name
, &statbuf
) == -1) goto leave
;
1044 if (S_ISDIR(statbuf
.st_mode
)) goto leave
;
1047 * Now open the file, so that we can mmap() it.
1049 if ((fd
= open(module
->module_name
, O_RDONLY
)) == -1) goto leave
;
1053 * Now mmap() the file.
1055 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1056 if (addr
== (char*)0xffffffff) goto leave
;
1059 * Next, we need to find a few of the internal ELF headers within
1060 * this thing. We need the main executable header, and the section
1063 ehptr
= (Elf32_Ehdr
*) addr
;
1064 spnt
= (Elf32_Shdr
*) (addr
+ ehptr
->e_shoff
);
1065 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
1067 stabsect
= stabstrsect
= -1;
1069 for (i
= 0; i
< ehptr
->e_shnum
; i
++) {
1070 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stab") == 0)
1073 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stabstr") == 0)
1077 if (stabsect
== -1 || stabstrsect
== -1) {
1078 DEBUG_Printf(DBG_CHN_WARN
, "no .stab section\n");
1083 * OK, now just parse all of the stabs.
1085 if (DEBUG_ParseStabs(addr
,
1086 module
->elf_info
->elf_addr
,
1087 spnt
[stabsect
].sh_offset
,
1088 spnt
[stabsect
].sh_size
,
1089 spnt
[stabstrsect
].sh_offset
,
1090 spnt
[stabstrsect
].sh_size
)) {
1094 DEBUG_Printf(DBG_CHN_WARN
, "bad stabs\n");
1098 for (i
= 0; i
< ehptr
->e_shnum
; i
++) {
1099 if ( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".symtab") == 0)
1100 && (spnt
[i
].sh_type
== SHT_SYMTAB
))
1101 DEBUG_ProcessElfSymtab(module
, addr
, module
->elf_info
->elf_addr
,
1102 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
1104 if ( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynsym") == 0)
1105 && (spnt
[i
].sh_type
== SHT_DYNSYM
))
1106 DEBUG_ProcessElfSymtab(module
, addr
, module
->elf_info
->elf_addr
,
1107 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
1111 if (addr
!= (char*)0xffffffff) munmap(addr
, statbuf
.st_size
);
1112 if (fd
!= -1) close(fd
);
1118 * Loads the information for ELF module stored in 'filename'
1119 * the module has been loaded at 'load_offset' address
1121 * -1 if the file cannot be found/opened
1122 * 0 if the file doesn't contain symbolic info (or this info cannot be
1126 static enum DbgInfoLoad
DEBUG_ProcessElfFile(const char* filename
,
1127 unsigned int load_offset
,
1128 unsigned int* dyn_addr
)
1130 enum DbgInfoLoad dil
= DIL_ERROR
;
1131 char* addr
= (char*)0xffffffff;
1133 struct stat statbuf
;
1139 DBG_MODULE
* module
= NULL
;
1143 DEBUG_Printf(DBG_CHN_TRACE
, "Processing elf file '%s'\n", filename
);
1145 /* check that the file exists, and that the module hasn't been loaded yet */
1146 if (stat(filename
, &statbuf
) == -1) goto leave
;
1149 * Now open the file, so that we can mmap() it.
1151 if ((fd
= open(filename
, O_RDONLY
)) == -1) goto leave
;
1154 * Now mmap() the file.
1156 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1157 if (addr
== (char*)0xffffffff) goto leave
;
1162 * Next, we need to find a few of the internal ELF headers within
1163 * this thing. We need the main executable header, and the section
1166 ehptr
= (Elf32_Ehdr
*) addr
;
1167 spnt
= (Elf32_Shdr
*) (addr
+ ehptr
->e_shoff
);
1168 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
1170 /* if non relocatable ELF, then remove fixed address from computation
1171 * otherwise, all addresses are zero based
1173 delta
= (load_offset
== 0) ? ehptr
->e_entry
: 0;
1175 /* grab size of module once loaded in memory */
1176 ppnt
= (Elf32_Phdr
*) (addr
+ ehptr
->e_phoff
);
1178 for (i
= 0; i
< ehptr
->e_phnum
; i
++) {
1179 if (ppnt
[i
].p_type
!= PT_LOAD
) continue;
1180 if (size
< ppnt
[i
].p_vaddr
- delta
+ ppnt
[i
].p_memsz
)
1181 size
= ppnt
[i
].p_vaddr
- delta
+ ppnt
[i
].p_memsz
;
1184 for (i
= 0; i
< ehptr
->e_shnum
; i
++) {
1185 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".bss") == 0 &&
1186 spnt
[i
].sh_type
== SHT_NOBITS
) {
1187 if (size
< spnt
[i
].sh_addr
- delta
+ spnt
[i
].sh_size
)
1188 size
= spnt
[i
].sh_addr
- delta
+ spnt
[i
].sh_size
;
1190 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynamic") == 0 &&
1191 spnt
[i
].sh_type
== SHT_DYNAMIC
) {
1192 if (dyn_addr
) *dyn_addr
= spnt
[i
].sh_addr
;
1196 module
= DEBUG_RegisterELFModule((load_offset
== 0) ? ehptr
->e_entry
: load_offset
,
1203 if ((module
->elf_info
= DBG_alloc(sizeof(ELF_DBG_INFO
))) == NULL
) {
1204 DEBUG_Printf(DBG_CHN_ERR
, "OOM\n");
1208 module
->elf_info
->elf_addr
= load_offset
;
1209 dil
= DEBUG_LoadElfStabs(module
);
1212 if (addr
!= (char*)0xffffffff) munmap(addr
, statbuf
.st_size
);
1213 if (fd
!= -1) close(fd
);
1214 if (module
) module
->dil
= dil
;
1219 static enum DbgInfoLoad
DEBUG_ProcessElfFileFromPath(const char * filename
,
1220 unsigned int load_offset
,
1221 unsigned int* dyn_addr
,
1224 enum DbgInfoLoad dil
= DIL_ERROR
;
1228 if (!path
) return -1;
1230 for (s
= paths
= DBG_strdup(path
); s
&& *s
; s
= (t
) ? (t
+1) : NULL
) {
1233 fn
= (char*)DBG_alloc(strlen(filename
) + 1 + strlen(s
) + 1);
1237 strcat(fn
, filename
);
1238 dil
= DEBUG_ProcessElfFile(fn
, load_offset
, dyn_addr
);
1240 if (dil
!= DIL_ERROR
) break;
1241 s
= (t
) ? (t
+1) : NULL
;
1248 static enum DbgInfoLoad
DEBUG_ProcessElfObject(const char* filename
,
1249 unsigned int load_offset
,
1250 unsigned int* dyn_addr
)
1252 enum DbgInfoLoad dil
= DIL_ERROR
;
1254 if (filename
== NULL
) return DIL_ERROR
;
1255 if (DEBUG_FindModuleByName(filename
, DMT_ELF
)) return DIL_LOADED
;
1257 dil
= DEBUG_ProcessElfFile(filename
, load_offset
, dyn_addr
);
1259 /* if relative pathname, try some absolute base dirs */
1260 if (dil
== DIL_ERROR
&& !strchr(filename
, '/')) {
1261 dil
= DEBUG_ProcessElfFileFromPath(filename
, load_offset
, dyn_addr
, getenv("PATH"));
1262 if (dil
== DIL_ERROR
)
1263 dil
= DEBUG_ProcessElfFileFromPath(filename
, load_offset
, dyn_addr
, getenv("LD_LIBRARY_PATH"));
1266 DEBUG_ReportDIL(dil
, "ELF", filename
, load_offset
);
1271 static BOOL
DEBUG_WalkList(struct r_debug
* dbg_hdr
)
1279 * Now walk the linked list. In all known ELF implementations,
1280 * the dynamic loader maintains this linked list for us. In some
1281 * cases the first entry doesn't appear with a name, in other cases it
1284 for (lm_addr
= (u_long
)dbg_hdr
->r_map
; lm_addr
; lm_addr
= (u_long
)lm
.l_next
) {
1285 if (!DEBUG_READ_MEM_VERBOSE((void*)lm_addr
, &lm
, sizeof(lm
)))
1287 if (lm
.l_addr
!= 0 &&
1288 DEBUG_READ_MEM_VERBOSE((void*)lm
.l_addr
, &ehdr
, sizeof(ehdr
)) &&
1289 ehdr
.e_type
== ET_DYN
&& /* only look at dynamic modules */
1290 lm
.l_name
!= NULL
&&
1291 DEBUG_READ_MEM_VERBOSE((void*)lm
.l_name
, bufstr
, sizeof(bufstr
))) {
1292 bufstr
[sizeof(bufstr
) - 1] = '\0';
1293 DEBUG_ProcessElfObject(bufstr
, (unsigned)lm
.l_addr
, NULL
);
1300 static BOOL
DEBUG_RescanElf(void)
1302 struct r_debug dbg_hdr
;
1304 if (!DEBUG_CurrProcess
||
1305 !DEBUG_READ_MEM_VERBOSE((void*)DEBUG_CurrProcess
->dbg_hdr_addr
, &dbg_hdr
, sizeof(dbg_hdr
)))
1308 switch (dbg_hdr
.r_state
) {
1310 DEBUG_WalkList(&dbg_hdr
);
1315 /* FIXME: this is not currently handled, would need some kind of mark&sweep algo */
1321 enum DbgInfoLoad
DEBUG_ReadExecutableDbgInfo(const char* exe_name
)
1324 struct r_debug dbg_hdr
;
1325 enum DbgInfoLoad dil
= DIL_NOINFO
;
1326 unsigned int dyn_addr
;
1329 * Make sure we can stat and open this file.
1331 if (exe_name
== NULL
) goto leave
;
1332 DEBUG_ProcessElfObject(exe_name
, 0, &dyn_addr
);
1335 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn_addr
, &dyn
, sizeof(dyn
)))
1337 dyn_addr
+= sizeof(dyn
);
1338 } while (dyn
.d_tag
!= DT_DEBUG
&& dyn
.d_tag
!= DT_NULL
);
1339 if (dyn
.d_tag
== DT_NULL
) goto leave
;
1342 * OK, now dig into the actual tables themselves.
1344 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn
.d_un
.d_ptr
, &dbg_hdr
, sizeof(dbg_hdr
)))
1347 assert(!DEBUG_CurrProcess
->dbg_hdr_addr
);
1348 DEBUG_CurrProcess
->dbg_hdr_addr
= (u_long
)dyn
.d_un
.d_ptr
;
1350 if (dbg_hdr
.r_brk
) {
1353 DEBUG_Printf(DBG_CHN_TRACE
, "Setting up a breakpoint on r_brk(%lx)\n",
1354 (unsigned long)dbg_hdr
.r_brk
);
1356 DEBUG_SetBreakpoints(FALSE
);
1358 value
.cookie
= DV_TARGET
;
1360 value
.addr
.off
= (DWORD
)dbg_hdr
.r_brk
;
1361 DEBUG_AddBreakpoint(&value
, DEBUG_RescanElf
);
1362 DEBUG_SetBreakpoints(TRUE
);
1365 dil
= DEBUG_WalkList(&dbg_hdr
);
1371 #else /* !__ELF__ */
1373 enum DbgInfoLoad
DEBUG_ReadExecutableDbgInfo(const char* exe_name
)
1378 #endif /* __ELF__ */