1 /* -*- tab-width: 8; c-basic-offset: 2 -*- */
4 * File stabs.c - read stabs information from the wine executable itself.
6 * Copyright (C) 1996, Eric Youngdale.
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 #elif defined(__EMX__)
80 struct stab_nlist
*n_next
;
86 unsigned long n_value
;
90 * This is used to keep track of known datatypes so that we don't redefine
91 * them over and over again. It sucks up lots of memory otherwise.
95 struct known_typedef
* next
;
98 struct datatype
* types
[1];
101 #define NR_STAB_HASH 521
103 static struct known_typedef
* ktd_head
[NR_STAB_HASH
] = {NULL
,};
104 static struct datatype
** curr_types
= NULL
;
105 static int allocated_types
= 0;
107 static unsigned int stab_hash( const char * name
)
109 unsigned int hash
= 0;
117 hash
= (hash
<< 4) + *p
++;
119 if( (tmp
= (hash
& 0xf0000000)) )
125 return hash
% NR_STAB_HASH
;
129 static void stab_strcpy(char * dest
, int sz
, const char * source
)
132 * A strcpy routine that stops when we hit the ':' character.
133 * Faster than copying the whole thing, and then nuking the
136 while(*source
!= '\0' && *source
!= ':' && sz
-- > 0)
146 struct datatype
** vector
;
150 #define MAX_INCLUDES 256
152 static include_def
* include_defs
= NULL
;
153 static int num_include_def
= 0;
154 static int num_alloc_include_def
= 0;
155 static int cu_include_stack
[MAX_INCLUDES
];
156 static int cu_include_stk_idx
= 0;
157 static struct datatype
** cu_vector
= NULL
;
158 static int cu_nrofentries
= 0;
162 DEBUG_CreateInclude(const char* file
, unsigned long val
)
164 if (num_include_def
== num_alloc_include_def
)
166 num_alloc_include_def
+= 256;
167 include_defs
= DBG_realloc(include_defs
, sizeof(include_defs
[0])*num_alloc_include_def
);
168 memset(include_defs
+num_include_def
, 0, sizeof(include_defs
[0])*256);
170 include_defs
[num_include_def
].name
= DBG_strdup(file
);
171 include_defs
[num_include_def
].value
= val
;
172 include_defs
[num_include_def
].vector
= NULL
;
173 include_defs
[num_include_def
].nrofentries
= 0;
175 return num_include_def
++;
180 DEBUG_FindInclude(const char* file
, unsigned long val
)
184 for (i
= 0; i
< num_include_def
; i
++)
186 if (val
== include_defs
[i
].value
&&
187 strcmp(file
, include_defs
[i
].name
) == 0)
195 DEBUG_AddInclude(int idx
)
197 ++cu_include_stk_idx
;
199 /* is this happen, just bump MAX_INCLUDES */
200 /* we could also handle this as another dynarray */
201 assert(cu_include_stk_idx
< MAX_INCLUDES
);
203 cu_include_stack
[cu_include_stk_idx
] = idx
;
204 return cu_include_stk_idx
;
209 DEBUG_ResetIncludes(void)
212 * The datatypes that we would need to use are reset when
213 * we start a new file. (at least the ones in filenr == 0
215 cu_include_stk_idx
= 0;/* keep 0 as index for the .c file itself */
216 memset(cu_vector
, 0, sizeof(cu_vector
[0]) * cu_nrofentries
);
221 DEBUG_FreeIncludes(void)
225 DEBUG_ResetIncludes();
227 for (i
= 0; i
< num_include_def
; i
++)
229 DBG_free(include_defs
[i
].name
);
230 DBG_free(include_defs
[i
].vector
);
232 DBG_free(include_defs
);
235 num_alloc_include_def
= 0;
243 DEBUG_FileSubNr2StabEnum(int filenr
, int subnr
)
245 struct datatype
** ret
;
247 /* DEBUG_Printf(DBG_CHN_MESG, "creating type id for (%d,%d)\n", filenr, subnr); */
249 /* FIXME: I could perhaps create a dummy include_def for each compilation
250 * unit which would allow not to handle those two cases separately
254 if (cu_nrofentries
<= subnr
)
256 cu_vector
= DBG_realloc(cu_vector
, sizeof(cu_vector
[0])*(subnr
+1));
257 memset(cu_vector
+cu_nrofentries
, 0, sizeof(cu_vector
[0])*(subnr
+1-cu_nrofentries
));
258 cu_nrofentries
= subnr
+ 1;
260 ret
= &cu_vector
[subnr
];
266 assert(filenr
<= cu_include_stk_idx
);
268 idef
= &include_defs
[cu_include_stack
[filenr
]];
270 if (idef
->nrofentries
<= subnr
)
272 idef
->vector
= DBG_realloc(idef
->vector
, sizeof(idef
->vector
[0])*(subnr
+1));
273 memset(idef
->vector
+ idef
->nrofentries
, 0, sizeof(idef
->vector
[0])*(subnr
+1-idef
->nrofentries
));
274 idef
->nrofentries
= subnr
+ 1;
276 ret
= &idef
->vector
[subnr
];
278 /* DEBUG_Printf(DBG_CHN_MESG,"(%d,%d) is %d\n",filenr,subnr,ret); */
284 DEBUG_ReadTypeEnumBackwards(char*x
) {
291 filenr
=strtol(x
,&x
,10); /* <int> */
293 subnr
=strtol(x
,&x
,10); /* <int> */
296 while ((*x
>='0') && (*x
<='9'))
301 return DEBUG_FileSubNr2StabEnum(filenr
,subnr
);
306 DEBUG_ReadTypeEnum(char **x
) {
311 filenr
=strtol(*x
,x
,10); /* <int> */
313 subnr
=strtol(*x
,x
,10); /* <int> */
317 subnr
= strtol(*x
,x
,10); /* <int> */
319 return DEBUG_FileSubNr2StabEnum(filenr
,subnr
);
324 DEBUG_RegisterTypedef(const char * name
, struct datatype
** types
, int ndef
)
327 struct known_typedef
* ktd
;
332 ktd
= (struct known_typedef
*) DBG_alloc(sizeof(struct known_typedef
)
333 + (ndef
- 1) * sizeof(struct datatype
*));
335 hash
= stab_hash(name
);
337 ktd
->name
= DBG_strdup(name
);
339 memcpy(&ktd
->types
[0], types
, ndef
* sizeof(struct datatype
*));
340 ktd
->next
= ktd_head
[hash
];
341 ktd_head
[hash
] = ktd
;
348 DEBUG_HandlePreviousTypedef(const char * name
, const char * stab
)
351 enum debug_type expect
;
353 struct known_typedef
* ktd
;
356 hash
= stab_hash(name
);
358 for(ktd
= ktd_head
[hash
]; ktd
; ktd
= ktd
->next
)
359 if ((ktd
->name
[0] == name
[0]) && (strcmp(name
, ktd
->name
) == 0) )
363 * Didn't find it. This must be a new one.
369 * Examine the stab to make sure it has the same number of definitions.
372 for(ptr
= strchr(stab
, '='); ptr
; ptr
= strchr(ptr
+1, '='))
374 if( count
>= ktd
->ndefs
)
378 * Make sure the types of all of the objects is consistent with
379 * what we have already parsed.
393 case '(': /* it's mainly a ref to another typedef, skip it */
410 DEBUG_Printf(DBG_CHN_FIXME
, "Unknown type (%c).\n",ptr
[1]);
413 if( expect
!= -1 && expect
!= DEBUG_GetType(ktd
->types
[count
]) )
418 if( ktd
->ndefs
!= count
)
422 * Go through, dig out all of the type numbers, and substitute the
423 * appropriate things.
426 for(ptr
= strchr(stab
, '='); ptr
; ptr
= strchr(ptr
+1, '='))
427 *DEBUG_ReadTypeEnumBackwards(ptr
-1) = ktd
->types
[count
++];
432 static int DEBUG_FreeRegisteredTypedefs(void)
436 struct known_typedef
* ktd
;
437 struct known_typedef
* next
;
440 for(j
=0; j
< NR_STAB_HASH
; j
++ )
442 for(ktd
= ktd_head
[j
]; ktd
; ktd
= next
)
458 DEBUG_ParseTypedefStab(char * ptr
, const char * typename
)
463 struct datatype
* curr_type
;
464 struct datatype
* datatype
;
465 char element_name
[1024];
468 const char * orig_typename
;
474 orig_typename
= typename
;
476 if( DEBUG_HandlePreviousTypedef(typename
, ptr
) )
480 * Go from back to front. First we go through and figure out what
481 * type numbers we need, and register those types. Then we go in
482 * and fill the details.
485 for( c
= strchr(ptr
, '='); c
!= NULL
; c
= strchr(c
+ 1, '=') )
488 * Back up until we get to a non-numeric character, to get datatype
490 struct datatype
** dt
= DEBUG_ReadTypeEnumBackwards(c
-1);
492 if( ntypes
>= allocated_types
)
494 allocated_types
+= 64;
495 curr_types
= DBG_realloc(curr_types
, sizeof(struct datatype
*) * allocated_types
);
496 if (!curr_types
) return FALSE
;
502 *dt
= DEBUG_NewDataType(DT_POINTER
, NULL
);
503 curr_types
[ntypes
++] = *dt
;
507 *dt
= DEBUG_NewDataType(DT_STRUCT
, typename
);
508 curr_types
[ntypes
++] = *dt
;
511 *dt
= DEBUG_NewDataType(DT_ARRAY
, NULL
);
512 curr_types
[ntypes
++] = *dt
;
515 /* will be handled in next loop,
516 * just a ref to another type
518 curr_types
[ntypes
++] = NULL
;
522 *dt
= DEBUG_NewDataType(DT_BASIC
, typename
);
523 curr_types
[ntypes
++] = *dt
;
526 stab_strcpy(element_name
, sizeof(element_name
), c
+ 3);
527 *dt
= DEBUG_NewDataType(DT_STRUCT
, element_name
);
528 curr_types
[ntypes
++] = *dt
;
531 *dt
= DEBUG_NewDataType(DT_ENUM
, NULL
);
532 curr_types
[ntypes
++] = *dt
;
535 *dt
= DEBUG_NewDataType(DT_FUNC
, NULL
);
536 curr_types
[ntypes
++] = *dt
;
539 DEBUG_Printf(DBG_CHN_FIXME
, "Unknown type (%c).\n",c
[1]);
548 * OK, now take a second sweep through. Now we will be digging
549 * out the definitions of the various components, and storing
550 * them in the skeletons that we have already allocated. We take
551 * a right-to left search as this is much easier to parse.
553 for( c
= strrchr(ptr
, '='); c
!= NULL
; c
= strrchr(ptr
, '=') )
555 struct datatype
** dt
= DEBUG_ReadTypeEnumBackwards(c
-1);
556 struct datatype
** dt2
;
577 datatype
= *DEBUG_ReadTypeEnum(&tc
);
578 DEBUG_SetPointerType(curr_type
, datatype
);
586 dt2
= DEBUG_ReadTypeEnum(&tc
);
592 else if (!*dt
&& !*dt2
)
594 /* this should be a basic type, define it */
595 *dt2
= *dt
= DEBUG_NewDataType(DT_BASIC
, typename
);
599 DEBUG_Printf(DBG_CHN_MESG
, "Unknown condition %08lx %08lx (%s)\n",
600 (unsigned long)*dt
, (unsigned long)*dt2
, ptr
);
606 curr_types
[ntp
--] = *dt
;
612 * We have already handled these above.
618 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */
622 DEBUG_ReadTypeEnum(&tc
);
624 arrmin
= strtol(tc
, &tc
, 10); /* <int> */
626 arrmax
= strtol(tc
, &tc
, 10); /* <int> */
628 datatype
= *DEBUG_ReadTypeEnum(&tc
); /* <typeinfo> */
633 DEBUG_SetArrayParams(curr_type
, arrmin
, arrmax
, datatype
);
641 if( DEBUG_SetStructSize(curr_type
, strtol(tc
, &tc
, 10)) == FALSE
)
644 * We have already filled out this structure. Nothing to do,
645 * so just skip forward to the end of the definition.
647 while( tc
[0] != ';' && tc
[1] != ';' )
660 * Now parse the individual elements of the structure/union.
671 datatype
= *DEBUG_ReadTypeEnum(&tc
);
674 offset
= strtol(tc
, &tc
, 10);
676 size
= strtol(tc
, &tc
, 10);
679 DEBUG_AddStructElement(curr_type
, element_name
, datatype
,
684 /* ... but proceed parsing to the end of the stab */
685 DEBUG_Printf(DBG_CHN_MESG
, "failure on %s %s\n", ptr
, ti
);
692 /* if we had a undeclared value this one is undeclared too.
693 * remove it from the stab_types.
694 * I just set it to NULL to detect bugs in my thoughtprocess.
695 * FIXME: leaks the memory for the structure elements.
696 * FIXME: such structures should have been optimized away
710 * Now parse the individual elements of the structure/union.
719 offset
= strtol(tc
, &tc
, 10);
721 DEBUG_AddStructElement(curr_type
, element_name
, NULL
, offset
, 0);
729 DEBUG_Printf(DBG_CHN_FIXME
, "Unknown type (%c).\n",c
[1]);
734 * Now register the type so that if we encounter it again, we will know
737 DEBUG_RegisterTypedef(orig_typename
, curr_types
, ntypes
);
742 static struct datatype
*
743 DEBUG_ParseStabType(const char * stab
)
748 * Look through the stab definition, and figure out what datatype
749 * this represents. If we have something we know about, assign the
752 c
= strchr(stab
, ':');
758 * The next character says more about the type (i.e. data, function, etc)
759 * of symbol. Skip it.
764 * The next is either an integer or a (integer,integer).
765 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
767 return *DEBUG_ReadTypeEnum(&c
);
771 DEBUG_ParseStabs(char * addr
, unsigned int load_offset
,
772 unsigned int staboff
, int stablen
,
773 unsigned int strtaboff
, int strtablen
)
775 struct name_hash
* curr_func
= NULL
;
776 struct wine_locals
* curr_loc
= NULL
;
777 struct name_hash
* curr_sym
= NULL
;
778 char currpath
[PATH_MAX
];
780 int in_external_file
= FALSE
;
788 struct stab_nlist
* stab_ptr
;
791 char * subpath
= NULL
;
794 nstab
= stablen
/ sizeof(struct stab_nlist
);
795 stab_ptr
= (struct stab_nlist
*) (addr
+ staboff
);
796 strs
= (char *) (addr
+ strtaboff
);
798 memset(currpath
, 0, sizeof(currpath
));
801 * Allocate a buffer into which we can build stab strings for cases
802 * where the stab is continued over multiple lines.
805 stabbuff
= (char *) DBG_alloc(stabbufflen
);
809 for(i
=0; i
< nstab
; i
++, stab_ptr
++ )
811 ptr
= strs
+ (unsigned int) stab_ptr
->n_un
.n_name
;
812 if( ptr
[strlen(ptr
) - 1] == '\\' )
815 * Indicates continuation. Append this to the buffer, and go onto the
816 * next record. Repeat the process until we find a stab without the
817 * '/' character, as this indicates we have the whole thing.
820 if( strlen(stabbuff
) + len
> stabbufflen
)
822 stabbufflen
+= 65536;
823 stabbuff
= (char *) DBG_realloc(stabbuff
, stabbufflen
);
825 strncat(stabbuff
, ptr
, len
- 1);
828 else if( stabbuff
[0] != '\0' )
830 strcat( stabbuff
, ptr
);
834 if( strchr(ptr
, '=') != NULL
)
837 * The stabs aren't in writable memory, so copy it over so we are
838 * sure we can scribble on it.
840 if( ptr
!= stabbuff
)
842 strcpy(stabbuff
, ptr
);
845 stab_strcpy(symname
, sizeof(symname
), ptr
);
846 if (!DEBUG_ParseTypedefStab(ptr
, symname
)) {
847 /* skip this definition */
853 switch(stab_ptr
->n_type
)
857 * These are useless with ELF. They have no value, and you have to
858 * read the normal symbol table to get the address. Thus we
859 * ignore them, and when we process the normal symbol table
860 * we should do the right thing.
862 * With a.out or mingw, they actually do make some amount of sense.
864 new_value
.addr
.seg
= 0;
865 new_value
.type
= DEBUG_ParseStabType(ptr
);
866 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
867 new_value
.cookie
= DV_TARGET
;
869 stab_strcpy(symname
, sizeof(symname
), ptr
);
871 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
872 SYM_WINE
| SYM_DATA
| SYM_INVALID
);
874 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
875 SYM_WINE
| SYM_DATA
);
881 * We need to keep track of these so we get symbol scoping
882 * right for local variables. For now, we just ignore them.
883 * The hooks are already there for dealing with this however,
884 * so all we need to do is to keep count of the nesting level,
885 * and find the RBRAC for each matching LBRAC.
891 * These are static symbols and BSS symbols.
893 new_value
.addr
.seg
= 0;
894 new_value
.type
= DEBUG_ParseStabType(ptr
);
895 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
896 new_value
.cookie
= DV_TARGET
;
898 stab_strcpy(symname
, sizeof(symname
), ptr
);
899 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
900 SYM_WINE
| SYM_DATA
);
904 * These are function parameters.
906 if( curr_func
!= NULL
&& !in_external_file
)
908 stab_strcpy(symname
, sizeof(symname
), ptr
);
909 curr_loc
= DEBUG_AddLocal( curr_func
, 0,
910 stab_ptr
->n_value
, 0, 0, symname
);
911 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
915 if( curr_func
!= NULL
&& !in_external_file
)
917 stab_strcpy(symname
, sizeof(symname
), ptr
);
918 curr_loc
= DEBUG_AddLocal( curr_func
, stab_ptr
->n_value
+ 1,
920 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
924 if( curr_func
!= NULL
&& !in_external_file
)
926 stab_strcpy(symname
, sizeof(symname
), ptr
);
927 curr_loc
= DEBUG_AddLocal( curr_func
, 0,
928 stab_ptr
->n_value
, 0, 0, symname
);
929 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
934 * This is a line number. These are always relative to the start
935 * of the function (N_FUN), and this makes the lookup easier.
937 if( curr_func
!= NULL
&& !in_external_file
)
940 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
945 * This isn't right. The order of the stabs is different under
946 * a.out, and as a result we would end up attaching the line
947 * number to the wrong function.
949 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
950 stab_ptr
->n_value
- curr_func
->addr
.off
);
957 * First, clean up the previous function we were working on.
959 DEBUG_Normalize(curr_func
);
962 * For now, just declare the various functions. Later
963 * on, we will add the line number information and the
966 if( !in_external_file
)
968 stab_strcpy(symname
, sizeof(symname
), ptr
);
971 new_value
.addr
.seg
= 0;
972 new_value
.type
= DEBUG_ParseStabType(ptr
);
973 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
974 new_value
.cookie
= DV_TARGET
;
976 * Copy the string to a temp buffer so we
977 * can kill everything after the ':'. We do
978 * it this way because otherwise we end up dirtying
979 * all of the pages related to the stabs, and that
980 * sucks up swap space like crazy.
982 curr_func
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
983 SYM_WINE
| SYM_FUNC
| SYM_INVALID
);
987 /* some GCC seem to use a N_FUN "" to mark the end of a function */
994 * Don't add line number information for this function
1002 * This indicates a new source file. Append the records
1003 * together, to build the correct path name.
1007 * With a.out, there is no NULL string N_SO entry at the end of
1008 * the file. Thus when we find non-consecutive entries,
1009 * we consider that a new file is started.
1011 if( last_nso
< i
-1 )
1014 DEBUG_Normalize(curr_func
);
1025 DEBUG_Normalize(curr_func
);
1031 strcat(currpath
, ptr
);
1033 strcpy(currpath
, ptr
);
1035 DEBUG_ResetIncludes();
1041 * This indicates we are including stuff from an include file.
1042 * If this is the main source, enable the debug stuff, otherwise
1045 in_external_file
= !(subpath
== NULL
|| strcmp(ptr
, subpath
) == 0);
1049 strtabinc
= stab_ptr
->n_value
;
1050 DEBUG_Normalize(curr_func
);
1055 * Ignore this. We don't care what it points to.
1059 DEBUG_AddInclude(DEBUG_CreateInclude(ptr
, stab_ptr
->n_value
));
1064 DEBUG_AddInclude(DEBUG_FindInclude(ptr
, stab_ptr
->n_value
));
1068 * Always ignore these. GCC doesn't even generate them.
1072 DEBUG_Printf(DBG_CHN_MESG
, "Unkown stab type 0x%02x\n", stab_ptr
->n_type
);
1079 DEBUG_Printf(DBG_CHN_MESG
, "%d %x %s\n", stab_ptr
->n_type
,
1080 (unsigned int) stab_ptr
->n_value
,
1081 strs
+ (unsigned int) stab_ptr
->n_un
.n_name
);
1085 DEBUG_FreeRegisteredTypedefs();
1086 DEBUG_FreeIncludes();
1087 DBG_free(curr_types
);
1089 allocated_types
= 0;
1097 * Walk through the entire symbol table and add any symbols we find there.
1098 * This can be used in cases where we have stripped ELF shared libraries,
1099 * or it can be used in cases where we have data symbols for which the address
1100 * isn't encoded in the stabs.
1102 * This is all really quite easy, since we don't have to worry about line
1103 * numbers or local data variables.
1107 DEBUG_ProcessElfSymtab(char * addr
, unsigned int load_offset
,
1108 Elf32_Shdr
* symtab
, Elf32_Shdr
* strtab
)
1110 char * curfile
= NULL
;
1111 struct name_hash
* curr_sym
= NULL
;
1114 DBG_VALUE new_value
;
1121 symp
= (Elf32_Sym
*) (addr
+ symtab
->sh_offset
);
1122 nsym
= symtab
->sh_size
/ sizeof(*symp
);
1123 strp
= (char *) (addr
+ strtab
->sh_offset
);
1125 for(i
=0; i
< nsym
; i
++, symp
++)
1128 * Ignore certain types of entries which really aren't of that much
1131 if( ELF32_ST_TYPE(symp
->st_info
) == STT_SECTION
||
1132 symp
->st_shndx
== STN_UNDEF
)
1137 symname
= strp
+ symp
->st_name
;
1140 * Save the name of the current file, so we have a way of tracking
1141 * static functions/data.
1143 if( ELF32_ST_TYPE(symp
->st_info
) == STT_FILE
)
1150 * See if we already have something for this symbol.
1151 * If so, ignore this entry, because it would have come from the
1152 * stabs or from a previous symbol. If the value is different,
1153 * we will have to keep the darned thing, because there can be
1154 * multiple local symbols by the same name.
1156 if( (DEBUG_GetSymbolValue(symname
, -1, &new_value
, FALSE
) == TRUE
)
1157 && (new_value
.addr
.off
== (load_offset
+ symp
->st_value
)) )
1160 new_value
.addr
.seg
= 0;
1161 new_value
.type
= NULL
;
1162 new_value
.addr
.off
= load_offset
+ symp
->st_value
;
1163 new_value
.cookie
= DV_TARGET
;
1164 flags
= SYM_WINE
| ((ELF32_ST_TYPE(symp
->st_info
) == STT_FUNC
)
1165 ? SYM_FUNC
: SYM_DATA
);
1166 if( ELF32_ST_BIND(symp
->st_info
) == STB_GLOBAL
)
1167 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, NULL
, flags
);
1169 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, curfile
, flags
);
1172 * Record the size of the symbol. This can come in handy in
1173 * some cases. Not really used yet, however.
1175 if( symp
->st_size
!= 0 )
1176 DEBUG_SetSymbolSize(curr_sym
, symp
->st_size
);
1183 * Loads the symbolic information from ELF module stored in 'filename'
1184 * the module has been loaded at 'load_offset' address, so symbols' address
1185 * relocation is performed
1187 * -1 if the file cannot be found/opened
1188 * 0 if the file doesn't contain symbolic info (or this info cannot be
1192 static int DEBUG_ProcessElfFile(const char * filename
, unsigned int load_offset
)
1195 char * addr
= (char*)0xffffffff;
1197 struct stat statbuf
;
1206 /* check that the file exists, and that the module hasn't been loaded yet */
1207 if (stat(filename
, &statbuf
) == -1) goto leave
;
1210 * Now open the file, so that we can mmap() it.
1212 if ((fd
= open(filename
, O_RDONLY
)) == -1) goto leave
;
1216 * Now mmap() the file.
1218 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1219 if (addr
== (char*)0xffffffff) goto leave
;
1222 * Next, we need to find a few of the internal ELF headers within
1223 * this thing. We need the main executable header, and the section
1226 ehptr
= (Elf32_Ehdr
*) addr
;
1228 DEBUG_RegisterELFModule((load_offset
== 0) ? ehptr
->e_entry
: load_offset
, filename
);
1230 spnt
= (Elf32_Shdr
*) (addr
+ ehptr
->e_shoff
);
1231 nsect
= ehptr
->e_shnum
;
1232 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
1234 stabsect
= stabstrsect
= -1;
1236 for (i
= 0; i
< nsect
; i
++) {
1237 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stab") == 0)
1240 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stabstr") == 0)
1244 if (stabsect
== -1 || stabstrsect
== -1) {
1245 DEBUG_Printf(DBG_CHN_WARN
, "no .stab section\n");
1250 * OK, now just parse all of the stabs.
1252 if (!(rtn
= DEBUG_ParseStabs(addr
, load_offset
,
1253 spnt
[stabsect
].sh_offset
,
1254 spnt
[stabsect
].sh_size
,
1255 spnt
[stabstrsect
].sh_offset
,
1256 spnt
[stabstrsect
].sh_size
))) {
1257 DEBUG_Printf(DBG_CHN_WARN
, "bad stabs\n");
1261 for (i
= 0; i
< nsect
; i
++) {
1262 if ( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".symtab") == 0)
1263 && (spnt
[i
].sh_type
== SHT_SYMTAB
))
1264 DEBUG_ProcessElfSymtab(addr
, load_offset
,
1265 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
1267 if ( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynsym") == 0)
1268 && (spnt
[i
].sh_type
== SHT_DYNSYM
))
1269 DEBUG_ProcessElfSymtab(addr
, load_offset
,
1270 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
1274 if (addr
!= (char*)0xffffffff) munmap(addr
, statbuf
.st_size
);
1275 if (fd
!= -1) close(fd
);
1280 static int DEBUG_ProcessElfFileFromPath(const char * filename
,
1281 unsigned int load_offset
, const char* path
)
1287 if (!path
) return -1;
1289 for (s
= paths
= DBG_strdup(path
); s
&& *s
; s
= (t
) ? (t
+1) : NULL
) {
1292 fn
= (char*)DBG_alloc(strlen(filename
) + 1 + strlen(s
) + 1);
1296 strcat(fn
, filename
);
1297 rtn
= DEBUG_ProcessElfFile(fn
, load_offset
);
1299 if (rtn
>= 0) break;
1300 s
= (t
) ? (t
+1) : NULL
;
1307 static int DEBUG_ProcessElfObject(const char * filename
, unsigned int load_offset
)
1312 DEBUG_Printf(DBG_CHN_TRACE
, "Processing elf file '%s'\n", filename
);
1314 if (filename
== NULL
) return FALSE
;
1315 if (DEBUG_FindModuleByName(filename
, DM_TYPE_ELF
)) return TRUE
;
1317 rtn
= DEBUG_ProcessElfFile(filename
, load_offset
);
1319 /* if relative pathname, try some absolute base dirs */
1320 if (rtn
< 0 && !strchr(filename
, '/')) {
1321 rtn
= DEBUG_ProcessElfFileFromPath(filename
, load_offset
, getenv("PATH"));
1323 rtn
= DEBUG_ProcessElfFileFromPath(filename
, load_offset
, getenv("LD_LIBRARY_PATH"));
1327 case 1: fmt
= "Loaded stabs debug symbols from ELF '%s' (0x%08x)\n"; break;
1328 case 0: fmt
= "No stabs debug symbols in ELF '%s' (0x%08x)\n"; break;
1329 case -1:fmt
= "Can't find file for ELF '%s' (0x%08x)\n"; break;
1330 default: DEBUG_Printf(DBG_CHN_ERR
, "Oooocch (%d)\n", rtn
); return FALSE
;
1333 DEBUG_Printf(DBG_CHN_MESG
, fmt
, filename
, load_offset
);
1338 static BOOL
DEBUG_WalkList(struct r_debug
* dbg_hdr
)
1346 * Now walk the linked list. In all known ELF implementations,
1347 * the dynamic loader maintains this linked list for us. In some
1348 * cases the first entry doesn't appear with a name, in other cases it
1351 for (lm_addr
= (u_long
)dbg_hdr
->r_map
; lm_addr
; lm_addr
= (u_long
)lm
.l_next
) {
1352 if (!DEBUG_READ_MEM_VERBOSE((void*)lm_addr
, &lm
, sizeof(lm
)))
1354 if (lm
.l_addr
!= 0 &&
1355 DEBUG_READ_MEM_VERBOSE((void*)lm
.l_addr
, &ehdr
, sizeof(ehdr
)) &&
1356 ehdr
.e_type
== ET_DYN
&& /* only look at dynamic modules */
1357 lm
.l_name
!= NULL
&&
1358 DEBUG_READ_MEM_VERBOSE((void*)lm
.l_name
, bufstr
, sizeof(bufstr
))) {
1359 bufstr
[sizeof(bufstr
) - 1] = '\0';
1360 DEBUG_ProcessElfObject(bufstr
, (unsigned)lm
.l_addr
);
1367 static BOOL
DEBUG_RescanElf(void)
1369 struct r_debug dbg_hdr
;
1371 if (!DEBUG_CurrProcess
||
1372 !DEBUG_READ_MEM_VERBOSE((void*)DEBUG_CurrProcess
->dbg_hdr_addr
, &dbg_hdr
, sizeof(dbg_hdr
)))
1375 switch (dbg_hdr
.r_state
) {
1377 DEBUG_WalkList(&dbg_hdr
);
1382 /*FIXME: this is not currently handled, would need some kind of mark&sweep algo */
1389 DEBUG_ReadExecutableDbgInfo(const char* exe_name
)
1392 struct r_debug dbg_hdr
;
1397 * Make sure we can stat and open this file.
1399 if (exe_name
== NULL
) goto leave
;
1400 DEBUG_ProcessElfObject(exe_name
, 0);
1402 /* previous step should have loaded symbol _DYNAMIC if it exists inside
1403 * the main executable
1405 if (!DEBUG_GetSymbolValue("_DYNAMIC", -1, &val
, FALSE
)) {
1406 DEBUG_Printf(DBG_CHN_WARN
, "Can't find symbol _DYNAMIC\n");
1411 if (!DEBUG_READ_MEM_VERBOSE((void*)val
.addr
.off
, &dyn
, sizeof(dyn
)))
1413 val
.addr
.off
+= sizeof(dyn
);
1414 } while (dyn
.d_tag
!= DT_DEBUG
&& dyn
.d_tag
!= DT_NULL
);
1415 if (dyn
.d_tag
== DT_NULL
) goto leave
;
1418 * OK, now dig into the actual tables themselves.
1420 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn
.d_un
.d_ptr
, &dbg_hdr
, sizeof(dbg_hdr
)))
1423 assert(!DEBUG_CurrProcess
->dbg_hdr_addr
);
1424 DEBUG_CurrProcess
->dbg_hdr_addr
= (u_long
)dyn
.d_un
.d_ptr
;
1426 if (dbg_hdr
.r_brk
) {
1429 DEBUG_Printf(DBG_CHN_TRACE
, "Setting up a breakpoint on r_brk(%lx)\n",
1430 (unsigned long)dbg_hdr
.r_brk
);
1432 DEBUG_SetBreakpoints(FALSE
);
1434 value
.cookie
= DV_TARGET
;
1436 value
.addr
.off
= (DWORD
)dbg_hdr
.r_brk
;
1437 DEBUG_AddBreakpoint(&value
, DEBUG_RescanElf
);
1438 DEBUG_SetBreakpoints(TRUE
);
1441 rtn
= DEBUG_WalkList(&dbg_hdr
);
1447 #else /* !__ELF__ */
1450 DEBUG_ReadExecutableDbgInfo(const char* exe_name
)
1455 #endif /* __ELF__ */