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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
29 #ifdef HAVE_SYS_MMAN_H
37 #define PATH_MAX MAX_PATH
42 #if defined(__svr4__) || defined(__sun)
53 #ifdef HAVE_SYS_LINK_H
54 # include <sys/link.h>
85 typedef struct tagELF_DBG_INFO
{
86 unsigned long elf_addr
;
92 struct stab_nlist
*n_next
;
98 unsigned long n_value
;
101 static void stab_strcpy(char * dest
, int sz
, const char * source
)
104 * A strcpy routine that stops when we hit the ':' character.
105 * Faster than copying the whole thing, and then nuking the
108 while(*source
!= '\0' && *source
!= ':' && sz
-- > 0)
118 struct datatype
** vector
;
122 #define MAX_INCLUDES 5120
124 static include_def
* include_defs
= NULL
;
125 static int num_include_def
= 0;
126 static int num_alloc_include_def
= 0;
127 static int cu_include_stack
[MAX_INCLUDES
];
128 static int cu_include_stk_idx
= 0;
129 static struct datatype
** cu_vector
= NULL
;
130 static int cu_nrofentries
= 0;
134 DEBUG_CreateInclude(const char* file
, unsigned long val
)
136 if (num_include_def
== num_alloc_include_def
)
138 num_alloc_include_def
+= 256;
139 include_defs
= DBG_realloc(include_defs
, sizeof(include_defs
[0])*num_alloc_include_def
);
140 memset(include_defs
+num_include_def
, 0, sizeof(include_defs
[0])*256);
142 include_defs
[num_include_def
].name
= DBG_strdup(file
);
143 include_defs
[num_include_def
].value
= val
;
144 include_defs
[num_include_def
].vector
= NULL
;
145 include_defs
[num_include_def
].nrofentries
= 0;
147 return num_include_def
++;
152 DEBUG_FindInclude(const char* file
, unsigned long val
)
156 for (i
= 0; i
< num_include_def
; i
++)
158 if (val
== include_defs
[i
].value
&&
159 strcmp(file
, include_defs
[i
].name
) == 0)
167 DEBUG_AddInclude(int idx
)
169 ++cu_include_stk_idx
;
171 /* is this happen, just bump MAX_INCLUDES */
172 /* we could also handle this as another dynarray */
173 assert(cu_include_stk_idx
< MAX_INCLUDES
);
175 cu_include_stack
[cu_include_stk_idx
] = idx
;
176 return cu_include_stk_idx
;
181 DEBUG_ResetIncludes(void)
184 * The datatypes that we would need to use are reset when
185 * we start a new file. (at least the ones in filenr == 0
187 cu_include_stk_idx
= 0;/* keep 0 as index for the .c file itself */
188 memset(cu_vector
, 0, sizeof(cu_vector
[0]) * cu_nrofentries
);
193 DEBUG_FreeIncludes(void)
197 DEBUG_ResetIncludes();
199 for (i
= 0; i
< num_include_def
; i
++)
201 DBG_free(include_defs
[i
].name
);
202 DBG_free(include_defs
[i
].vector
);
204 DBG_free(include_defs
);
207 num_alloc_include_def
= 0;
215 DEBUG_FileSubNr2StabEnum(int filenr
, int subnr
)
217 struct datatype
** ret
;
219 /* DEBUG_Printf(DBG_CHN_MESG, "creating type id for (%d,%d)\n", filenr, subnr); */
221 /* FIXME: I could perhaps create a dummy include_def for each compilation
222 * unit which would allow not to handle those two cases separately
226 if (cu_nrofentries
<= subnr
)
228 cu_vector
= DBG_realloc(cu_vector
, sizeof(cu_vector
[0])*(subnr
+1));
229 memset(cu_vector
+cu_nrofentries
, 0, sizeof(cu_vector
[0])*(subnr
+1-cu_nrofentries
));
230 cu_nrofentries
= subnr
+ 1;
232 ret
= &cu_vector
[subnr
];
238 assert(filenr
<= cu_include_stk_idx
);
240 idef
= &include_defs
[cu_include_stack
[filenr
]];
242 if (idef
->nrofentries
<= subnr
)
244 idef
->vector
= DBG_realloc(idef
->vector
, sizeof(idef
->vector
[0])*(subnr
+1));
245 memset(idef
->vector
+ idef
->nrofentries
, 0, sizeof(idef
->vector
[0])*(subnr
+1-idef
->nrofentries
));
246 idef
->nrofentries
= subnr
+ 1;
248 ret
= &idef
->vector
[subnr
];
250 /* DEBUG_Printf(DBG_CHN_MESG,"(%d,%d) is %d\n",filenr,subnr,ret); */
256 DEBUG_ReadTypeEnum(char **x
) {
261 filenr
=strtol(*x
,x
,10); /* <int> */
263 subnr
=strtol(*x
,x
,10); /* <int> */
267 subnr
= strtol(*x
,x
,10); /* <int> */
269 return DEBUG_FileSubNr2StabEnum(filenr
,subnr
);
272 struct ParseTypedefData
{
278 static int DEBUG_PTS_ReadTypedef(struct ParseTypedefData
* ptd
, const char* typename
,
279 struct datatype
** dt
);
281 static int DEBUG_PTS_ReadID(struct ParseTypedefData
* ptd
)
283 char* first
= ptd
->ptr
;
286 if ((ptd
->ptr
= strchr(ptd
->ptr
, ':')) == NULL
) return -1;
287 len
= ptd
->ptr
- first
;
288 if (len
>= sizeof(ptd
->buf
) - ptd
->idx
) return -1;
289 memcpy(ptd
->buf
+ ptd
->idx
, first
, len
);
290 ptd
->buf
[ptd
->idx
+ len
] = '\0';
292 ptd
->ptr
++; /* ':' */
296 static int DEBUG_PTS_ReadNum(struct ParseTypedefData
* ptd
, int* v
)
300 *v
= strtol(ptd
->ptr
, &last
, 10);
301 if (last
== ptd
->ptr
) return -1;
306 static int DEBUG_PTS_ReadTypeReference(struct ParseTypedefData
* ptd
,
307 int* filenr
, int* subnr
)
309 if (*ptd
->ptr
== '(') {
310 /* '(' <int> ',' <int> ')' */
312 if (DEBUG_PTS_ReadNum(ptd
, filenr
) == -1) return -1;
313 if (*ptd
->ptr
++ != ',') return -1;
314 if (DEBUG_PTS_ReadNum(ptd
, subnr
) == -1) return -1;
315 if (*ptd
->ptr
++ != ')') return -1;
318 if (DEBUG_PTS_ReadNum(ptd
, subnr
) == -1) return -1;
323 static int DEBUG_PTS_ReadRange(struct ParseTypedefData
* ptd
, struct datatype
** dt
,
326 /* type ';' <int> ';' <int> ';' */
327 if (DEBUG_PTS_ReadTypedef(ptd
, NULL
, dt
) == -1) return -1;
328 if (*ptd
->ptr
++ != ';') return -1; /* ';' */
329 if (DEBUG_PTS_ReadNum(ptd
, lo
) == -1) return -1;
330 if (*ptd
->ptr
++ != ';') return -1; /* ';' */
331 if (DEBUG_PTS_ReadNum(ptd
, hi
) == -1) return -1;
332 if (*ptd
->ptr
++ != ';') return -1; /* ';' */
336 static inline int DEBUG_PTS_ReadAggregate(struct ParseTypedefData
* ptd
, struct datatype
* sdt
)
340 struct datatype
* adt
;
344 sz
= strtol(ptd
->ptr
, &last
, 10);
345 if (last
== ptd
->ptr
) return -1;
348 doadd
= DEBUG_SetStructSize(sdt
, sz
);
349 /* if the structure has already been filled, just redo the parsing
350 * but don't store results into the struct
351 * FIXME: there's a quite ugly memory leak in there...
354 /* Now parse the individual elements of the structure/union. */
355 while (*ptd
->ptr
!= ';') {
356 /* agg_name : type ',' <int:offset> ',' <int:size> */
358 if (DEBUG_PTS_ReadID(ptd
) == -1) return -1;
360 if (DEBUG_PTS_ReadTypedef(ptd
, NULL
, &adt
) == -1) return -1;
363 if (*ptd
->ptr
++ != ',') return -1;
364 if (DEBUG_PTS_ReadNum(ptd
, &ofs
) == -1) return -1;
365 if (*ptd
->ptr
++ != ',') return -1;
366 if (DEBUG_PTS_ReadNum(ptd
, &sz
) == -1) return -1;
367 if (*ptd
->ptr
++ != ';') return -1;
369 if (doadd
) DEBUG_AddStructElement(sdt
, ptd
->buf
+ idx
, adt
, ofs
, sz
);
372 ptd
->ptr
++; /* ';' */
376 static inline int DEBUG_PTS_ReadEnum(struct ParseTypedefData
* ptd
, struct datatype
* edt
)
381 while (*ptd
->ptr
!= ';') {
383 if (DEBUG_PTS_ReadID(ptd
) == -1) return -1;
384 if (DEBUG_PTS_ReadNum(ptd
, &ofs
) == -1) return -1;
385 if (*ptd
->ptr
++ != ',') return -1;
386 DEBUG_AddStructElement(edt
, ptd
->buf
+ idx
, NULL
, ofs
, 0);
393 static inline int DEBUG_PTS_ReadArray(struct ParseTypedefData
* ptd
, struct datatype
* adt
)
396 struct datatype
* rdt
;
398 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
400 if (*ptd
->ptr
++ != 'r') return -1;
401 /* FIXME: range type is lost, always assume int */
402 if (DEBUG_PTS_ReadRange(ptd
, &rdt
, &lo
, &hi
) == -1) return -1;
403 if (DEBUG_PTS_ReadTypedef(ptd
, NULL
, &rdt
) == -1) return -1;
405 DEBUG_SetArrayParams(adt
, lo
, hi
, rdt
);
409 static int DEBUG_PTS_ReadTypedef(struct ParseTypedefData
* ptd
, const char* typename
,
410 struct datatype
** ret_dt
)
412 int idx
, lo
, hi
, sz
= -1;
413 struct datatype
* new_dt
= NULL
; /* newly created data type */
414 struct datatype
* ref_dt
; /* referenced data type (pointer...) */
415 struct datatype
* dt1
; /* intermediate data type (scope is limited) */
416 struct datatype
* dt2
; /* intermediate data type: t1=t2=new_dt */
418 int filenr2
= 0, subnr2
= 0;
420 /* things are a bit complicated because of the way the typedefs are stored inside
421 * the file (we cannot keep the struct datatype** around, because address can
422 * change when realloc is done, so we must call over and over
423 * DEBUG_FileSubNr2StabEnum to keep the correct values around
424 * (however, keeping struct datatype* is valid
426 if (DEBUG_PTS_ReadTypeReference(ptd
, &filenr1
, &subnr1
) == -1) return -1;
428 while (*ptd
->ptr
== '=') {
431 DEBUG_Printf(DBG_CHN_MESG
, "Bad recursion (1) in typedef\n");
434 /* first handle attribute if any */
437 if (*++ptd
->ptr
== 's') {
439 if (DEBUG_PTS_ReadNum(ptd
, &sz
) == -1) {
440 DEBUG_Printf(DBG_CHN_MESG
, "Not an attribute... NIY\n");
444 if (*ptd
->ptr
++ != ';') return -1;
448 /* then the real definitions */
449 switch (*ptd
->ptr
++) {
451 new_dt
= DEBUG_NewDataType(DT_POINTER
, NULL
);
452 if (DEBUG_PTS_ReadTypedef(ptd
, NULL
, &ref_dt
) == -1) return -1;
453 DEBUG_SetPointerType(new_dt
, ref_dt
);
457 /* doit a two level by hand, otherwise we'd need a stack */
458 if (filenr2
|| subnr2
) {
459 DEBUG_Printf(DBG_CHN_MESG
, "Bad recursion (2) in typedef\n");
462 if (DEBUG_PTS_ReadTypeReference(ptd
, &filenr2
, &subnr2
) == -1) return -1;
464 dt1
= *DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
);
465 dt2
= *DEBUG_FileSubNr2StabEnum(filenr2
, subnr2
);
469 filenr2
= subnr2
= 0;
470 } else if (!dt1
&& !dt2
) {
473 DEBUG_Printf(DBG_CHN_MESG
, "Unknown condition %08lx %08lx (%s)\n",
474 (unsigned long)dt1
, (unsigned long)dt2
, ptd
->ptr
);
479 new_dt
= DEBUG_NewDataType(DT_ARRAY
, NULL
);
480 if (DEBUG_PTS_ReadArray(ptd
, new_dt
) == -1) return -1;
483 new_dt
= DEBUG_NewDataType(DT_BASIC
, typename
);
484 assert(!*DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
));
485 *DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
) = new_dt
;
486 if (DEBUG_PTS_ReadRange(ptd
, &ref_dt
, &lo
, &hi
) == -1) return -1;
487 /* should perhaps do more here... */
490 new_dt
= DEBUG_NewDataType(DT_FUNC
, NULL
);
491 if (DEBUG_PTS_ReadTypedef(ptd
, NULL
, &ref_dt
) == -1) return -1;
492 DEBUG_SetPointerType(new_dt
, ref_dt
);
495 new_dt
= DEBUG_NewDataType(DT_ENUM
, NULL
);
496 if (DEBUG_PTS_ReadEnum(ptd
, new_dt
) == -1) return -1;
500 /* dt1 can have been already defined in a forward definition */
501 dt1
= *DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
);
502 dt2
= DEBUG_TypeCast(DT_STRUCT
, typename
);
504 new_dt
= DEBUG_NewDataType(DT_STRUCT
, typename
);
505 /* we need to set it here, because a struct can hold a pointer
508 *DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
) = new_dt
;
510 if (DEBUG_GetType(dt1
) != DT_STRUCT
) {
511 DEBUG_Printf(DBG_CHN_MESG
,
512 "Forward declaration is not an aggregate\n");
516 /* should check typename is the same too */
519 if (DEBUG_PTS_ReadAggregate(ptd
, new_dt
) == -1) return -1;
522 switch (*ptd
->ptr
++) {
523 case 'e': lo
= DT_ENUM
; break;
524 case 's': case 'u': lo
= DT_STRUCT
; break;
529 if (DEBUG_PTS_ReadID(ptd
) == -1) return -1;
530 new_dt
= DEBUG_NewDataType(lo
, ptd
->buf
+ idx
);
534 if (DEBUG_PTS_ReadNum(ptd
, &lo
) == -1) {
535 DEBUG_Printf(DBG_CHN_MESG
, "Should be a number (%s)...\n", ptd
->ptr
);
538 enum debug_type_basic basic
= DT_BASIC_LAST
;
541 case 1: basic
= DT_BASIC_INT
; break;
542 case 2: basic
= DT_BASIC_CHAR
; break;
543 case 3: basic
= DT_BASIC_SHORTINT
; break;
544 case 4: basic
= DT_BASIC_LONGINT
; break;
545 case 5: basic
= DT_BASIC_UCHAR
; break;
546 case 6: basic
= DT_BASIC_SCHAR
; break;
547 case 7: basic
= DT_BASIC_USHORTINT
; break;
548 case 8: basic
= DT_BASIC_UINT
; break;
549 /* case 9: basic = DT_BASIC_UINT"; */
550 case 10: basic
= DT_BASIC_ULONGINT
; break;
551 case 11: basic
= DT_BASIC_VOID
; break;
552 case 12: basic
= DT_BASIC_FLOAT
; break;
553 case 13: basic
= DT_BASIC_DOUBLE
; break;
554 case 14: basic
= DT_BASIC_LONGDOUBLE
; break;
555 /* case 15: basic = DT_BASIC_INT; break; */
558 case 32: basic
= DT_BASIC_BOOL1
; break;
559 case 16: basic
= DT_BASIC_BOOL2
; break;
560 case 8: basic
= DT_BASIC_BOOL4
; break;
563 /* case 17: basic = DT_BASIC_SHORT real; break; */
564 /* case 18: basic = DT_BASIC_REAL; break; */
565 case 25: basic
= DT_BASIC_CMPLX_FLOAT
; break;
566 case 26: basic
= DT_BASIC_CMPLX_DOUBLE
; break;
567 /* case 30: basic = DT_BASIC_wchar"; break; */
568 case 31: basic
= DT_BASIC_LONGLONGINT
; break;
569 case 32: basic
= DT_BASIC_ULONGLONGINT
; break;
571 DEBUG_Printf(DBG_CHN_MESG
, "Unsupported integral type (%d/%d)\n", lo
, sz
);
574 if (!(new_dt
= DEBUG_GetBasicType(basic
))) {
575 DEBUG_Printf(DBG_CHN_MESG
, "Basic type %d not found\n", basic
);
578 if (*ptd
->ptr
++ != ';') return -1;
582 DEBUG_Printf(DBG_CHN_MESG
, "Unknown type '%c'\n", ptd
->ptr
[-1]);
587 if ((filenr2
|| subnr2
) && !*DEBUG_FileSubNr2StabEnum(filenr2
, subnr2
)) {
589 /* this should be a basic type, define it, or even void */
590 new_dt
= DEBUG_NewDataType(DT_BASIC
, typename
);
592 *DEBUG_FileSubNr2StabEnum(filenr2
, subnr2
) = new_dt
;
596 dt1
= *DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
);
598 DEBUG_Printf(DBG_CHN_MESG
, "Nothing has been defined <%s>\n", ptd
->ptr
);
605 *DEBUG_FileSubNr2StabEnum(filenr1
, subnr1
) = *ret_dt
= new_dt
;
609 DEBUG_Printf(DBG_CHN_MESG
, "Adding (%d,%d) %s => ", filenr1
, subnr1
, typename
);
610 DEBUG_PrintTypeCast(new_dt
);
611 DEBUG_Printf(DBG_CHN_MESG
, "\n");
618 static int DEBUG_ParseTypedefStab(char* ptr
, const char* typename
)
620 struct ParseTypedefData ptd
;
624 /* check for already existing definition */
627 if ((ptd
.ptr
= strchr(ptr
, ':'))) {
629 if (*ptd
.ptr
!= '(') ptd
.ptr
++;
630 ret
= DEBUG_PTS_ReadTypedef(&ptd
, typename
, &dt
);
633 if (ret
== -1 || *ptd
.ptr
) {
634 DEBUG_Printf(DBG_CHN_MESG
, "failure on %s at %s\n", ptr
, ptd
.ptr
);
641 static struct datatype
*
642 DEBUG_ParseStabType(const char * stab
)
647 * Look through the stab definition, and figure out what datatype
648 * this represents. If we have something we know about, assign the
651 c
= strchr(stab
, ':');
657 * The next character says more about the type (i.e. data, function, etc)
658 * of symbol. Skip it.
663 * The next is either an integer or a (integer,integer).
664 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
666 return *DEBUG_ReadTypeEnum(&c
);
669 enum DbgInfoLoad
DEBUG_ParseStabs(char * addr
, unsigned int load_offset
,
670 unsigned int staboff
, int stablen
,
671 unsigned int strtaboff
, int strtablen
)
673 struct name_hash
* curr_func
= NULL
;
674 struct wine_locals
* curr_loc
= NULL
;
675 struct name_hash
* curr_sym
= NULL
;
676 char currpath
[PATH_MAX
];
678 int in_external_file
= FALSE
;
685 unsigned int stabbufflen
;
686 struct stab_nlist
* stab_ptr
;
689 char * subpath
= NULL
;
692 nstab
= stablen
/ sizeof(struct stab_nlist
);
693 stab_ptr
= (struct stab_nlist
*) (addr
+ staboff
);
694 strs
= (char *) (addr
+ strtaboff
);
696 memset(currpath
, 0, sizeof(currpath
));
699 * Allocate a buffer into which we can build stab strings for cases
700 * where the stab is continued over multiple lines.
703 stabbuff
= (char *) DBG_alloc(stabbufflen
);
707 for(i
=0; i
< nstab
; i
++, stab_ptr
++ )
709 ptr
= strs
+ (unsigned int) stab_ptr
->n_un
.n_name
;
710 if( ptr
[strlen(ptr
) - 1] == '\\' )
713 * Indicates continuation. Append this to the buffer, and go onto the
714 * next record. Repeat the process until we find a stab without the
715 * '/' character, as this indicates we have the whole thing.
718 if( strlen(stabbuff
) + len
> stabbufflen
)
720 stabbufflen
+= 65536;
721 stabbuff
= (char *) DBG_realloc(stabbuff
, stabbufflen
);
723 strncat(stabbuff
, ptr
, len
- 1);
726 else if( stabbuff
[0] != '\0' )
728 strcat( stabbuff
, ptr
);
732 if( strchr(ptr
, '=') != NULL
)
735 * The stabs aren't in writable memory, so copy it over so we are
736 * sure we can scribble on it.
738 if( ptr
!= stabbuff
)
740 strcpy(stabbuff
, ptr
);
743 stab_strcpy(symname
, sizeof(symname
), ptr
);
744 if (!DEBUG_ParseTypedefStab(ptr
, symname
)) {
745 /* skip this definition */
751 switch(stab_ptr
->n_type
)
755 * These are useless with ELF. They have no value, and you have to
756 * read the normal symbol table to get the address. Thus we
757 * ignore them, and when we process the normal symbol table
758 * we should do the right thing.
760 * With a.out or mingw, they actually do make some amount of sense.
762 new_value
.addr
.seg
= 0;
763 new_value
.type
= DEBUG_ParseStabType(ptr
);
764 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
765 new_value
.cookie
= DV_TARGET
;
767 stab_strcpy(symname
, sizeof(symname
), ptr
);
769 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
770 SYM_WINE
| SYM_DATA
| SYM_INVALID
);
772 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
773 SYM_WINE
| SYM_DATA
);
779 * We need to keep track of these so we get symbol scoping
780 * right for local variables. For now, we just ignore them.
781 * The hooks are already there for dealing with this however,
782 * so all we need to do is to keep count of the nesting level,
783 * and find the RBRAC for each matching LBRAC.
789 * These are static symbols and BSS symbols.
791 new_value
.addr
.seg
= 0;
792 new_value
.type
= DEBUG_ParseStabType(ptr
);
793 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
794 new_value
.cookie
= DV_TARGET
;
796 stab_strcpy(symname
, sizeof(symname
), ptr
);
797 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
798 SYM_WINE
| SYM_DATA
);
802 * These are function parameters.
804 if( curr_func
!= NULL
&& !in_external_file
)
806 stab_strcpy(symname
, sizeof(symname
), ptr
);
807 curr_loc
= DEBUG_AddLocal( curr_func
, 0,
808 stab_ptr
->n_value
, 0, 0, symname
);
809 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
813 if( curr_func
!= NULL
&& !in_external_file
)
815 stab_strcpy(symname
, sizeof(symname
), ptr
);
816 curr_loc
= DEBUG_AddLocal( curr_func
, stab_ptr
->n_value
+ 1,
818 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
822 if( curr_func
!= NULL
&& !in_external_file
)
824 stab_strcpy(symname
, sizeof(symname
), ptr
);
825 curr_loc
= DEBUG_AddLocal( curr_func
, 0,
826 stab_ptr
->n_value
, 0, 0, symname
);
827 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
832 * This is a line number. These are always relative to the start
833 * of the function (N_FUN), and this makes the lookup easier.
835 if( curr_func
!= NULL
&& !in_external_file
)
838 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
843 * This isn't right. The order of the stabs is different under
844 * a.out, and as a result we would end up attaching the line
845 * number to the wrong function.
847 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
848 stab_ptr
->n_value
- curr_func
->addr
.off
);
855 * First, clean up the previous function we were working on.
857 DEBUG_Normalize(curr_func
);
860 * For now, just declare the various functions. Later
861 * on, we will add the line number information and the
864 if( !in_external_file
)
866 stab_strcpy(symname
, sizeof(symname
), ptr
);
869 new_value
.addr
.seg
= 0;
870 new_value
.type
= DEBUG_ParseStabType(ptr
);
871 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
872 new_value
.cookie
= DV_TARGET
;
874 * Copy the string to a temp buffer so we
875 * can kill everything after the ':'. We do
876 * it this way because otherwise we end up dirtying
877 * all of the pages related to the stabs, and that
878 * sucks up swap space like crazy.
881 curr_func
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
882 SYM_WINE
| SYM_FUNC
| SYM_INVALID
);
884 curr_func
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
885 SYM_WINE
| SYM_FUNC
);
890 /* some GCC seem to use a N_FUN "" to mark the end of a function */
897 * Don't add line number information for this function
905 * This indicates a new source file. Append the records
906 * together, to build the correct path name.
910 * With a.out, there is no NULL string N_SO entry at the end of
911 * the file. Thus when we find non-consecutive entries,
912 * we consider that a new file is started.
917 DEBUG_Normalize(curr_func
);
928 DEBUG_Normalize(curr_func
);
934 strcat(currpath
, ptr
);
936 strcpy(currpath
, ptr
);
938 DEBUG_ResetIncludes();
944 * This indicates we are including stuff from an include file.
945 * If this is the main source, enable the debug stuff, otherwise
948 in_external_file
= !(subpath
== NULL
|| strcmp(ptr
, subpath
) == 0);
952 strtabinc
= stab_ptr
->n_value
;
953 DEBUG_Normalize(curr_func
);
958 * Ignore this. We don't care what it points to.
962 DEBUG_AddInclude(DEBUG_CreateInclude(ptr
, stab_ptr
->n_value
));
967 DEBUG_AddInclude(DEBUG_FindInclude(ptr
, stab_ptr
->n_value
));
971 * Always ignore these. GCC doesn't even generate them.
975 DEBUG_Printf(DBG_CHN_MESG
, "Unknown stab type 0x%02x\n", stab_ptr
->n_type
);
982 DEBUG_Printf(DBG_CHN_MESG
, "%d %x %s\n", stab_ptr
->n_type
,
983 (unsigned int) stab_ptr
->n_value
,
984 strs
+ (unsigned int) stab_ptr
->n_un
.n_name
);
988 DEBUG_FreeIncludes();
996 * Walk through the entire symbol table and add any symbols we find there.
997 * This can be used in cases where we have stripped ELF shared libraries,
998 * or it can be used in cases where we have data symbols for which the address
999 * isn't encoded in the stabs.
1001 * This is all really quite easy, since we don't have to worry about line
1002 * numbers or local data variables.
1004 static int DEBUG_ProcessElfSymtab(DBG_MODULE
* module
, char* addr
,
1005 u_long load_addr
, Elf32_Shdr
* symtab
,
1008 char * curfile
= NULL
;
1009 struct name_hash
* curr_sym
= NULL
;
1012 DBG_VALUE new_value
;
1018 symp
= (Elf32_Sym
*) (addr
+ symtab
->sh_offset
);
1019 nsym
= symtab
->sh_size
/ sizeof(*symp
);
1020 strp
= (char *) (addr
+ strtab
->sh_offset
);
1022 for(i
=0; i
< nsym
; i
++, symp
++)
1025 * Ignore certain types of entries which really aren't of that much
1028 if( ELF32_ST_TYPE(symp
->st_info
) == STT_SECTION
||
1029 symp
->st_shndx
== STN_UNDEF
)
1034 symname
= strp
+ symp
->st_name
;
1037 * Save the name of the current file, so we have a way of tracking
1038 * static functions/data.
1040 if( ELF32_ST_TYPE(symp
->st_info
) == STT_FILE
)
1047 * See if we already have something for this symbol.
1048 * If so, ignore this entry, because it would have come from the
1049 * stabs or from a previous symbol. If the value is different,
1050 * we will have to keep the darned thing, because there can be
1051 * multiple local symbols by the same name.
1053 if( (DEBUG_GetSymbolValue(symname
, -1, &new_value
, FALSE
) == TRUE
)
1054 && (new_value
.addr
.off
== (load_addr
+ symp
->st_value
)) )
1057 new_value
.addr
.seg
= 0;
1058 new_value
.type
= NULL
;
1059 new_value
.addr
.off
= load_addr
+ symp
->st_value
;
1060 new_value
.cookie
= DV_TARGET
;
1061 flags
= SYM_WINE
| ((ELF32_ST_TYPE(symp
->st_info
) == STT_FUNC
)
1062 ? SYM_FUNC
: SYM_DATA
);
1063 if( ELF32_ST_BIND(symp
->st_info
) == STB_GLOBAL
)
1064 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, NULL
, flags
);
1066 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, curfile
, flags
);
1069 * Record the size of the symbol. This can come in handy in
1070 * some cases. Not really used yet, however.
1072 if( symp
->st_size
!= 0 )
1073 DEBUG_SetSymbolSize(curr_sym
, symp
->st_size
);
1080 * Loads the symbolic information from ELF module stored in 'filename'
1081 * the module has been loaded at 'load_offset' address, so symbols' address
1082 * relocation is performed
1084 * -1 if the file cannot be found/opened
1085 * 0 if the file doesn't contain symbolic info (or this info cannot be
1089 enum DbgInfoLoad
DEBUG_LoadElfStabs(DBG_MODULE
* module
)
1091 enum DbgInfoLoad dil
= DIL_ERROR
;
1092 char* addr
= (char*)0xffffffff;
1094 struct stat statbuf
;
1102 if (module
->type
!= DMT_ELF
|| ! module
->elf_info
) {
1103 DEBUG_Printf(DBG_CHN_ERR
, "Bad elf module '%s'\n", module
->module_name
);
1107 /* check that the file exists, and that the module hasn't been loaded yet */
1108 if (stat(module
->module_name
, &statbuf
) == -1) goto leave
;
1109 if (S_ISDIR(statbuf
.st_mode
)) goto leave
;
1112 * Now open the file, so that we can mmap() it.
1114 if ((fd
= open(module
->module_name
, O_RDONLY
)) == -1) goto leave
;
1118 * Now mmap() the file.
1120 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1121 if (addr
== (char*)0xffffffff) goto leave
;
1124 * Next, we need to find a few of the internal ELF headers within
1125 * this thing. We need the main executable header, and the section
1128 ehptr
= (Elf32_Ehdr
*) addr
;
1129 spnt
= (Elf32_Shdr
*) (addr
+ ehptr
->e_shoff
);
1130 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
1132 stabsect
= stabstrsect
= -1;
1134 for (i
= 0; i
< ehptr
->e_shnum
; i
++) {
1135 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stab") == 0)
1138 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stabstr") == 0)
1142 if (stabsect
== -1 || stabstrsect
== -1) {
1143 DEBUG_Printf(DBG_CHN_WARN
, "no .stab section\n");
1148 * OK, now just parse all of the stabs.
1150 if (DEBUG_ParseStabs(addr
,
1151 module
->elf_info
->elf_addr
,
1152 spnt
[stabsect
].sh_offset
,
1153 spnt
[stabsect
].sh_size
,
1154 spnt
[stabstrsect
].sh_offset
,
1155 spnt
[stabstrsect
].sh_size
)) {
1159 DEBUG_Printf(DBG_CHN_WARN
, "bad stabs\n");
1163 for (i
= 0; i
< ehptr
->e_shnum
; i
++) {
1164 if ( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".symtab") == 0)
1165 && (spnt
[i
].sh_type
== SHT_SYMTAB
))
1166 DEBUG_ProcessElfSymtab(module
, addr
, module
->elf_info
->elf_addr
,
1167 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
1169 if ( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynsym") == 0)
1170 && (spnt
[i
].sh_type
== SHT_DYNSYM
))
1171 DEBUG_ProcessElfSymtab(module
, addr
, module
->elf_info
->elf_addr
,
1172 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
1176 if (addr
!= (char*)0xffffffff) munmap(addr
, statbuf
.st_size
);
1177 if (fd
!= -1) close(fd
);
1183 * Loads the information for ELF module stored in 'filename'
1184 * the module has been loaded at 'load_offset' address
1186 * -1 if the file cannot be found/opened
1187 * 0 if the file doesn't contain symbolic info (or this info cannot be
1191 static enum DbgInfoLoad
DEBUG_ProcessElfFile(const char* filename
,
1192 unsigned int load_offset
,
1193 unsigned int* dyn_addr
)
1195 static const unsigned char elf_signature
[4] = { 0x7f, 'E', 'L', 'F' };
1196 enum DbgInfoLoad dil
= DIL_ERROR
;
1197 char* addr
= (char*)0xffffffff;
1199 struct stat statbuf
;
1205 DBG_MODULE
* module
= NULL
;
1209 DEBUG_Printf(DBG_CHN_TRACE
, "Processing elf file '%s'\n", filename
);
1211 /* check that the file exists, and that the module hasn't been loaded yet */
1212 if (stat(filename
, &statbuf
) == -1) goto leave
;
1215 * Now open the file, so that we can mmap() it.
1217 if ((fd
= open(filename
, O_RDONLY
)) == -1) goto leave
;
1220 * Now mmap() the file.
1222 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1223 if (addr
== (char*)0xffffffff) goto leave
;
1228 * Next, we need to find a few of the internal ELF headers within
1229 * this thing. We need the main executable header, and the section
1232 ehptr
= (Elf32_Ehdr
*) addr
;
1233 if (memcmp( ehptr
->e_ident
, elf_signature
, sizeof(elf_signature
) )) goto leave
;
1235 spnt
= (Elf32_Shdr
*) (addr
+ ehptr
->e_shoff
);
1236 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
1238 /* if non relocatable ELF, then remove fixed address from computation
1239 * otherwise, all addresses are zero based
1241 delta
= (load_offset
== 0) ? ehptr
->e_entry
: 0;
1243 /* grab size of module once loaded in memory */
1244 ppnt
= (Elf32_Phdr
*) (addr
+ ehptr
->e_phoff
);
1246 for (i
= 0; i
< ehptr
->e_phnum
; i
++) {
1247 if (ppnt
[i
].p_type
!= PT_LOAD
) continue;
1248 if (size
< ppnt
[i
].p_vaddr
- delta
+ ppnt
[i
].p_memsz
)
1249 size
= ppnt
[i
].p_vaddr
- delta
+ ppnt
[i
].p_memsz
;
1252 for (i
= 0; i
< ehptr
->e_shnum
; i
++) {
1253 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".bss") == 0 &&
1254 spnt
[i
].sh_type
== SHT_NOBITS
) {
1255 if (size
< spnt
[i
].sh_addr
- delta
+ spnt
[i
].sh_size
)
1256 size
= spnt
[i
].sh_addr
- delta
+ spnt
[i
].sh_size
;
1258 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynamic") == 0 &&
1259 spnt
[i
].sh_type
== SHT_DYNAMIC
) {
1260 if (dyn_addr
) *dyn_addr
= spnt
[i
].sh_addr
;
1264 module
= DEBUG_RegisterELFModule((load_offset
== 0) ? ehptr
->e_entry
: load_offset
,
1271 if ((module
->elf_info
= DBG_alloc(sizeof(ELF_DBG_INFO
))) == NULL
) {
1272 DEBUG_Printf(DBG_CHN_ERR
, "OOM\n");
1276 module
->elf_info
->elf_addr
= load_offset
;
1277 dil
= DEBUG_LoadElfStabs(module
);
1280 if (addr
!= (char*)0xffffffff) munmap(addr
, statbuf
.st_size
);
1281 if (fd
!= -1) close(fd
);
1282 if (module
) module
->dil
= dil
;
1287 static enum DbgInfoLoad
DEBUG_ProcessElfFileFromPath(const char * filename
,
1288 unsigned int load_offset
,
1289 unsigned int* dyn_addr
,
1292 enum DbgInfoLoad dil
= DIL_ERROR
;
1296 if (!path
) return -1;
1298 for (s
= paths
= DBG_strdup(path
); s
&& *s
; s
= (t
) ? (t
+1) : NULL
) {
1301 fn
= (char*)DBG_alloc(strlen(filename
) + 1 + strlen(s
) + 1);
1305 strcat(fn
, filename
);
1306 dil
= DEBUG_ProcessElfFile(fn
, load_offset
, dyn_addr
);
1308 if (dil
!= DIL_ERROR
) break;
1309 s
= (t
) ? (t
+1) : NULL
;
1316 static enum DbgInfoLoad
DEBUG_ProcessElfObject(const char* filename
,
1317 unsigned int load_offset
,
1318 unsigned int* dyn_addr
)
1320 enum DbgInfoLoad dil
= DIL_ERROR
;
1322 if (filename
== NULL
) return DIL_ERROR
;
1323 if (DEBUG_FindModuleByName(filename
, DMT_ELF
)) return DIL_LOADED
;
1325 dil
= DEBUG_ProcessElfFile(filename
, load_offset
, dyn_addr
);
1327 /* if relative pathname, try some absolute base dirs */
1328 if (dil
== DIL_ERROR
&& !strchr(filename
, '/')) {
1329 dil
= DEBUG_ProcessElfFileFromPath(filename
, load_offset
, dyn_addr
, getenv("PATH"));
1330 if (dil
== DIL_ERROR
)
1331 dil
= DEBUG_ProcessElfFileFromPath(filename
, load_offset
, dyn_addr
, getenv("LD_LIBRARY_PATH"));
1334 DEBUG_ReportDIL(dil
, "ELF", filename
, load_offset
);
1339 static BOOL
DEBUG_WalkList(struct r_debug
* dbg_hdr
)
1347 * Now walk the linked list. In all known ELF implementations,
1348 * the dynamic loader maintains this linked list for us. In some
1349 * cases the first entry doesn't appear with a name, in other cases it
1352 for (lm_addr
= (u_long
)dbg_hdr
->r_map
; lm_addr
; lm_addr
= (u_long
)lm
.l_next
) {
1353 if (!DEBUG_READ_MEM_VERBOSE((void*)lm_addr
, &lm
, sizeof(lm
)))
1355 if (lm
.l_addr
!= 0 &&
1356 DEBUG_READ_MEM_VERBOSE((void*)lm
.l_addr
, &ehdr
, sizeof(ehdr
)) &&
1357 ehdr
.e_type
== ET_DYN
&& /* only look at dynamic modules */
1358 lm
.l_name
!= NULL
&&
1359 DEBUG_READ_MEM_VERBOSE((void*)lm
.l_name
, bufstr
, sizeof(bufstr
))) {
1360 bufstr
[sizeof(bufstr
) - 1] = '\0';
1361 DEBUG_ProcessElfObject(bufstr
, (unsigned)lm
.l_addr
, NULL
);
1368 static BOOL
DEBUG_RescanElf(void)
1370 struct r_debug dbg_hdr
;
1372 if (!DEBUG_CurrProcess
||
1373 !DEBUG_READ_MEM_VERBOSE((void*)DEBUG_CurrProcess
->dbg_hdr_addr
, &dbg_hdr
, sizeof(dbg_hdr
)))
1376 switch (dbg_hdr
.r_state
) {
1378 DEBUG_WalkList(&dbg_hdr
);
1379 DEBUG_CheckDelayedBP();
1384 /* FIXME: this is not currently handled, would need some kind of mark&sweep algo */
1390 enum DbgInfoLoad
DEBUG_ReadExecutableDbgInfo(const char* exe_name
)
1393 struct r_debug dbg_hdr
;
1394 enum DbgInfoLoad dil
= DIL_NOINFO
;
1395 unsigned int dyn_addr
;
1398 * Make sure we can stat and open this file.
1400 if (exe_name
== NULL
) goto leave
;
1401 DEBUG_ProcessElfObject(exe_name
, 0, &dyn_addr
);
1404 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn_addr
, &dyn
, sizeof(dyn
)))
1406 dyn_addr
+= sizeof(dyn
);
1407 } while (dyn
.d_tag
!= DT_DEBUG
&& dyn
.d_tag
!= DT_NULL
);
1408 if (dyn
.d_tag
== DT_NULL
) goto leave
;
1411 * OK, now dig into the actual tables themselves.
1413 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn
.d_un
.d_ptr
, &dbg_hdr
, sizeof(dbg_hdr
)))
1416 assert(!DEBUG_CurrProcess
->dbg_hdr_addr
);
1417 DEBUG_CurrProcess
->dbg_hdr_addr
= (u_long
)dyn
.d_un
.d_ptr
;
1419 if (dbg_hdr
.r_brk
) {
1422 DEBUG_Printf(DBG_CHN_TRACE
, "Setting up a breakpoint on r_brk(%lx)\n",
1423 (unsigned long)dbg_hdr
.r_brk
);
1425 DEBUG_SetBreakpoints(FALSE
);
1427 value
.cookie
= DV_TARGET
;
1429 value
.addr
.off
= (DWORD
)dbg_hdr
.r_brk
;
1430 DEBUG_AddBreakpoint(&value
, DEBUG_RescanElf
);
1431 DEBUG_SetBreakpoints(TRUE
);
1434 dil
= DEBUG_WalkList(&dbg_hdr
);
1440 #else /* !__ELF__ */
1442 enum DbgInfoLoad
DEBUG_ReadExecutableDbgInfo(const char* exe_name
)
1447 #endif /* __ELF__ */