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 #elif defined(__EMX__)
76 typedef struct tagELF_DBG_INFO
{
77 unsigned long elf_addr
;
83 struct stab_nlist
*n_next
;
89 unsigned long n_value
;
93 * This is used to keep track of known datatypes so that we don't redefine
94 * them over and over again. It sucks up lots of memory otherwise.
98 struct known_typedef
* next
;
101 struct datatype
* types
[1];
104 #define NR_STAB_HASH 521
106 static struct known_typedef
* ktd_head
[NR_STAB_HASH
] = {NULL
,};
107 static struct datatype
** curr_types
= NULL
;
108 static int allocated_types
= 0;
110 static unsigned int stab_hash( const char * name
)
112 unsigned int hash
= 0;
120 hash
= (hash
<< 4) + *p
++;
122 if( (tmp
= (hash
& 0xf0000000)) )
128 return hash
% NR_STAB_HASH
;
132 static void stab_strcpy(char * dest
, int sz
, const char * source
)
135 * A strcpy routine that stops when we hit the ':' character.
136 * Faster than copying the whole thing, and then nuking the
139 while(*source
!= '\0' && *source
!= ':' && sz
-- > 0)
149 struct datatype
** vector
;
153 #define MAX_INCLUDES 256
155 static include_def
* include_defs
= NULL
;
156 static int num_include_def
= 0;
157 static int num_alloc_include_def
= 0;
158 static int cu_include_stack
[MAX_INCLUDES
];
159 static int cu_include_stk_idx
= 0;
160 static struct datatype
** cu_vector
= NULL
;
161 static int cu_nrofentries
= 0;
165 DEBUG_CreateInclude(const char* file
, unsigned long val
)
167 if (num_include_def
== num_alloc_include_def
)
169 num_alloc_include_def
+= 256;
170 include_defs
= DBG_realloc(include_defs
, sizeof(include_defs
[0])*num_alloc_include_def
);
171 memset(include_defs
+num_include_def
, 0, sizeof(include_defs
[0])*256);
173 include_defs
[num_include_def
].name
= DBG_strdup(file
);
174 include_defs
[num_include_def
].value
= val
;
175 include_defs
[num_include_def
].vector
= NULL
;
176 include_defs
[num_include_def
].nrofentries
= 0;
178 return num_include_def
++;
183 DEBUG_FindInclude(const char* file
, unsigned long val
)
187 for (i
= 0; i
< num_include_def
; i
++)
189 if (val
== include_defs
[i
].value
&&
190 strcmp(file
, include_defs
[i
].name
) == 0)
198 DEBUG_AddInclude(int idx
)
200 ++cu_include_stk_idx
;
202 /* is this happen, just bump MAX_INCLUDES */
203 /* we could also handle this as another dynarray */
204 assert(cu_include_stk_idx
< MAX_INCLUDES
);
206 cu_include_stack
[cu_include_stk_idx
] = idx
;
207 return cu_include_stk_idx
;
212 DEBUG_ResetIncludes(void)
215 * The datatypes that we would need to use are reset when
216 * we start a new file. (at least the ones in filenr == 0
218 cu_include_stk_idx
= 0;/* keep 0 as index for the .c file itself */
219 memset(cu_vector
, 0, sizeof(cu_vector
[0]) * cu_nrofentries
);
224 DEBUG_FreeIncludes(void)
228 DEBUG_ResetIncludes();
230 for (i
= 0; i
< num_include_def
; i
++)
232 DBG_free(include_defs
[i
].name
);
233 DBG_free(include_defs
[i
].vector
);
235 DBG_free(include_defs
);
238 num_alloc_include_def
= 0;
246 DEBUG_FileSubNr2StabEnum(int filenr
, int subnr
)
248 struct datatype
** ret
;
250 /* DEBUG_Printf(DBG_CHN_MESG, "creating type id for (%d,%d)\n", filenr, subnr); */
252 /* FIXME: I could perhaps create a dummy include_def for each compilation
253 * unit which would allow not to handle those two cases separately
257 if (cu_nrofentries
<= subnr
)
259 cu_vector
= DBG_realloc(cu_vector
, sizeof(cu_vector
[0])*(subnr
+1));
260 memset(cu_vector
+cu_nrofentries
, 0, sizeof(cu_vector
[0])*(subnr
+1-cu_nrofentries
));
261 cu_nrofentries
= subnr
+ 1;
263 ret
= &cu_vector
[subnr
];
269 assert(filenr
<= cu_include_stk_idx
);
271 idef
= &include_defs
[cu_include_stack
[filenr
]];
273 if (idef
->nrofentries
<= subnr
)
275 idef
->vector
= DBG_realloc(idef
->vector
, sizeof(idef
->vector
[0])*(subnr
+1));
276 memset(idef
->vector
+ idef
->nrofentries
, 0, sizeof(idef
->vector
[0])*(subnr
+1-idef
->nrofentries
));
277 idef
->nrofentries
= subnr
+ 1;
279 ret
= &idef
->vector
[subnr
];
281 /* DEBUG_Printf(DBG_CHN_MESG,"(%d,%d) is %d\n",filenr,subnr,ret); */
287 DEBUG_ReadTypeEnumBackwards(char*x
) {
294 filenr
=strtol(x
,&x
,10); /* <int> */
296 subnr
=strtol(x
,&x
,10); /* <int> */
299 while ((*x
>='0') && (*x
<='9'))
304 return DEBUG_FileSubNr2StabEnum(filenr
,subnr
);
309 DEBUG_ReadTypeEnum(char **x
) {
314 filenr
=strtol(*x
,x
,10); /* <int> */
316 subnr
=strtol(*x
,x
,10); /* <int> */
320 subnr
= strtol(*x
,x
,10); /* <int> */
322 return DEBUG_FileSubNr2StabEnum(filenr
,subnr
);
327 DEBUG_RegisterTypedef(const char * name
, struct datatype
** types
, int ndef
)
330 struct known_typedef
* ktd
;
335 ktd
= (struct known_typedef
*) DBG_alloc(sizeof(struct known_typedef
)
336 + (ndef
- 1) * sizeof(struct datatype
*));
338 hash
= stab_hash(name
);
340 ktd
->name
= DBG_strdup(name
);
342 memcpy(&ktd
->types
[0], types
, ndef
* sizeof(struct datatype
*));
343 ktd
->next
= ktd_head
[hash
];
344 ktd_head
[hash
] = ktd
;
351 DEBUG_HandlePreviousTypedef(const char * name
, const char * stab
)
354 enum debug_type expect
;
356 struct known_typedef
* ktd
;
359 hash
= stab_hash(name
);
361 for(ktd
= ktd_head
[hash
]; ktd
; ktd
= ktd
->next
)
362 if ((ktd
->name
[0] == name
[0]) && (strcmp(name
, ktd
->name
) == 0) )
366 * Didn't find it. This must be a new one.
372 * Examine the stab to make sure it has the same number of definitions.
375 for(ptr
= strchr(stab
, '='); ptr
; ptr
= strchr(ptr
+1, '='))
377 if( count
>= ktd
->ndefs
)
381 * Make sure the types of all of the objects is consistent with
382 * what we have already parsed.
396 case '(': /* it's mainly a ref to another typedef, skip it */
413 DEBUG_Printf(DBG_CHN_FIXME
, "Unknown type (%c).\n",ptr
[1]);
416 if( expect
!= -1 && expect
!= DEBUG_GetType(ktd
->types
[count
]) )
421 if( ktd
->ndefs
!= count
)
425 * Go through, dig out all of the type numbers, and substitute the
426 * appropriate things.
429 for(ptr
= strchr(stab
, '='); ptr
; ptr
= strchr(ptr
+1, '='))
430 *DEBUG_ReadTypeEnumBackwards(ptr
-1) = ktd
->types
[count
++];
435 static int DEBUG_FreeRegisteredTypedefs(void)
439 struct known_typedef
* ktd
;
440 struct known_typedef
* next
;
443 for(j
=0; j
< NR_STAB_HASH
; j
++ )
445 for(ktd
= ktd_head
[j
]; ktd
; ktd
= next
)
461 DEBUG_ParseTypedefStab(char * ptr
, const char * typename
)
466 struct datatype
* curr_type
;
467 struct datatype
* datatype
;
468 char element_name
[1024];
471 const char * orig_typename
;
477 orig_typename
= typename
;
479 if( DEBUG_HandlePreviousTypedef(typename
, ptr
) )
483 * Go from back to front. First we go through and figure out what
484 * type numbers we need, and register those types. Then we go in
485 * and fill the details.
488 for( c
= strchr(ptr
, '='); c
!= NULL
; c
= strchr(c
+ 1, '=') )
491 * Back up until we get to a non-numeric character, to get datatype
493 struct datatype
** dt
= DEBUG_ReadTypeEnumBackwards(c
-1);
495 if( ntypes
>= allocated_types
)
497 allocated_types
+= 64;
498 curr_types
= DBG_realloc(curr_types
, sizeof(struct datatype
*) * allocated_types
);
499 if (!curr_types
) return FALSE
;
505 *dt
= DEBUG_NewDataType(DT_POINTER
, NULL
);
506 curr_types
[ntypes
++] = *dt
;
510 *dt
= DEBUG_NewDataType(DT_STRUCT
, typename
);
511 curr_types
[ntypes
++] = *dt
;
514 *dt
= DEBUG_NewDataType(DT_ARRAY
, NULL
);
515 curr_types
[ntypes
++] = *dt
;
518 /* will be handled in next loop,
519 * just a ref to another type
521 curr_types
[ntypes
++] = NULL
;
525 *dt
= DEBUG_NewDataType(DT_BASIC
, typename
);
526 curr_types
[ntypes
++] = *dt
;
529 stab_strcpy(element_name
, sizeof(element_name
), c
+ 3);
530 *dt
= DEBUG_NewDataType(DT_STRUCT
, element_name
);
531 curr_types
[ntypes
++] = *dt
;
534 *dt
= DEBUG_NewDataType(DT_ENUM
, NULL
);
535 curr_types
[ntypes
++] = *dt
;
538 *dt
= DEBUG_NewDataType(DT_FUNC
, NULL
);
539 curr_types
[ntypes
++] = *dt
;
542 DEBUG_Printf(DBG_CHN_FIXME
, "Unknown type (%c).\n",c
[1]);
551 * OK, now take a second sweep through. Now we will be digging
552 * out the definitions of the various components, and storing
553 * them in the skeletons that we have already allocated. We take
554 * a right-to left search as this is much easier to parse.
556 for( c
= strrchr(ptr
, '='); c
!= NULL
; c
= strrchr(ptr
, '=') )
558 struct datatype
** dt
= DEBUG_ReadTypeEnumBackwards(c
-1);
559 struct datatype
** dt2
;
580 datatype
= *DEBUG_ReadTypeEnum(&tc
);
581 DEBUG_SetPointerType(curr_type
, datatype
);
589 dt2
= DEBUG_ReadTypeEnum(&tc
);
595 else if (!*dt
&& !*dt2
)
597 /* this should be a basic type, define it */
598 *dt2
= *dt
= DEBUG_NewDataType(DT_BASIC
, typename
);
602 DEBUG_Printf(DBG_CHN_MESG
, "Unknown condition %08lx %08lx (%s)\n",
603 (unsigned long)*dt
, (unsigned long)*dt2
, ptr
);
609 curr_types
[ntp
--] = *dt
;
615 * We have already handled these above.
621 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */
625 DEBUG_ReadTypeEnum(&tc
);
627 arrmin
= strtol(tc
, &tc
, 10); /* <int> */
629 arrmax
= strtol(tc
, &tc
, 10); /* <int> */
631 datatype
= *DEBUG_ReadTypeEnum(&tc
); /* <typeinfo> */
636 DEBUG_SetArrayParams(curr_type
, arrmin
, arrmax
, datatype
);
644 if( DEBUG_SetStructSize(curr_type
, strtol(tc
, &tc
, 10)) == FALSE
)
647 * We have already filled out this structure. Nothing to do,
648 * so just skip forward to the end of the definition.
650 while( tc
[0] != ';' && tc
[1] != ';' )
663 * Now parse the individual elements of the structure/union.
674 datatype
= *DEBUG_ReadTypeEnum(&tc
);
677 offset
= strtol(tc
, &tc
, 10);
679 size
= strtol(tc
, &tc
, 10);
682 DEBUG_AddStructElement(curr_type
, element_name
, datatype
,
687 /* ... but proceed parsing to the end of the stab */
688 DEBUG_Printf(DBG_CHN_MESG
, "failure on %s %s\n", ptr
, ti
);
695 /* if we had a undeclared value this one is undeclared too.
696 * remove it from the stab_types.
697 * I just set it to NULL to detect bugs in my thoughtprocess.
698 * FIXME: leaks the memory for the structure elements.
699 * FIXME: such structures should have been optimized away
713 * Now parse the individual elements of the structure/union.
722 offset
= strtol(tc
, &tc
, 10);
724 DEBUG_AddStructElement(curr_type
, element_name
, NULL
, offset
, 0);
732 DEBUG_Printf(DBG_CHN_FIXME
, "Unknown type (%c).\n",c
[1]);
737 * Now register the type so that if we encounter it again, we will know
740 DEBUG_RegisterTypedef(orig_typename
, curr_types
, ntypes
);
745 static struct datatype
*
746 DEBUG_ParseStabType(const char * stab
)
751 * Look through the stab definition, and figure out what datatype
752 * this represents. If we have something we know about, assign the
755 c
= strchr(stab
, ':');
761 * The next character says more about the type (i.e. data, function, etc)
762 * of symbol. Skip it.
767 * The next is either an integer or a (integer,integer).
768 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
770 return *DEBUG_ReadTypeEnum(&c
);
773 enum DbgInfoLoad
DEBUG_ParseStabs(char * addr
, unsigned int load_offset
,
774 unsigned int staboff
, int stablen
,
775 unsigned int strtaboff
, int strtablen
)
777 struct name_hash
* curr_func
= NULL
;
778 struct wine_locals
* curr_loc
= NULL
;
779 struct name_hash
* curr_sym
= NULL
;
780 char currpath
[PATH_MAX
];
782 int in_external_file
= FALSE
;
790 struct stab_nlist
* stab_ptr
;
793 char * subpath
= NULL
;
796 nstab
= stablen
/ sizeof(struct stab_nlist
);
797 stab_ptr
= (struct stab_nlist
*) (addr
+ staboff
);
798 strs
= (char *) (addr
+ strtaboff
);
800 memset(currpath
, 0, sizeof(currpath
));
803 * Allocate a buffer into which we can build stab strings for cases
804 * where the stab is continued over multiple lines.
807 stabbuff
= (char *) DBG_alloc(stabbufflen
);
811 for(i
=0; i
< nstab
; i
++, stab_ptr
++ )
813 ptr
= strs
+ (unsigned int) stab_ptr
->n_un
.n_name
;
814 if( ptr
[strlen(ptr
) - 1] == '\\' )
817 * Indicates continuation. Append this to the buffer, and go onto the
818 * next record. Repeat the process until we find a stab without the
819 * '/' character, as this indicates we have the whole thing.
822 if( strlen(stabbuff
) + len
> stabbufflen
)
824 stabbufflen
+= 65536;
825 stabbuff
= (char *) DBG_realloc(stabbuff
, stabbufflen
);
827 strncat(stabbuff
, ptr
, len
- 1);
830 else if( stabbuff
[0] != '\0' )
832 strcat( stabbuff
, ptr
);
836 if( strchr(ptr
, '=') != NULL
)
839 * The stabs aren't in writable memory, so copy it over so we are
840 * sure we can scribble on it.
842 if( ptr
!= stabbuff
)
844 strcpy(stabbuff
, ptr
);
847 stab_strcpy(symname
, sizeof(symname
), ptr
);
848 if (!DEBUG_ParseTypedefStab(ptr
, symname
)) {
849 /* skip this definition */
855 switch(stab_ptr
->n_type
)
859 * These are useless with ELF. They have no value, and you have to
860 * read the normal symbol table to get the address. Thus we
861 * ignore them, and when we process the normal symbol table
862 * we should do the right thing.
864 * With a.out or mingw, they actually do make some amount of sense.
866 new_value
.addr
.seg
= 0;
867 new_value
.type
= DEBUG_ParseStabType(ptr
);
868 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
869 new_value
.cookie
= DV_TARGET
;
871 stab_strcpy(symname
, sizeof(symname
), ptr
);
873 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
874 SYM_WINE
| SYM_DATA
| SYM_INVALID
);
876 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
877 SYM_WINE
| SYM_DATA
);
883 * We need to keep track of these so we get symbol scoping
884 * right for local variables. For now, we just ignore them.
885 * The hooks are already there for dealing with this however,
886 * so all we need to do is to keep count of the nesting level,
887 * and find the RBRAC for each matching LBRAC.
893 * These are static symbols and BSS symbols.
895 new_value
.addr
.seg
= 0;
896 new_value
.type
= DEBUG_ParseStabType(ptr
);
897 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
898 new_value
.cookie
= DV_TARGET
;
900 stab_strcpy(symname
, sizeof(symname
), ptr
);
901 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
902 SYM_WINE
| SYM_DATA
);
906 * These are function parameters.
908 if( curr_func
!= NULL
&& !in_external_file
)
910 stab_strcpy(symname
, sizeof(symname
), ptr
);
911 curr_loc
= DEBUG_AddLocal( curr_func
, 0,
912 stab_ptr
->n_value
, 0, 0, symname
);
913 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
917 if( curr_func
!= NULL
&& !in_external_file
)
919 stab_strcpy(symname
, sizeof(symname
), ptr
);
920 curr_loc
= DEBUG_AddLocal( curr_func
, stab_ptr
->n_value
+ 1,
922 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
926 if( curr_func
!= NULL
&& !in_external_file
)
928 stab_strcpy(symname
, sizeof(symname
), ptr
);
929 curr_loc
= DEBUG_AddLocal( curr_func
, 0,
930 stab_ptr
->n_value
, 0, 0, symname
);
931 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
936 * This is a line number. These are always relative to the start
937 * of the function (N_FUN), and this makes the lookup easier.
939 if( curr_func
!= NULL
&& !in_external_file
)
942 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
947 * This isn't right. The order of the stabs is different under
948 * a.out, and as a result we would end up attaching the line
949 * number to the wrong function.
951 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
952 stab_ptr
->n_value
- curr_func
->addr
.off
);
959 * First, clean up the previous function we were working on.
961 DEBUG_Normalize(curr_func
);
964 * For now, just declare the various functions. Later
965 * on, we will add the line number information and the
968 if( !in_external_file
)
970 stab_strcpy(symname
, sizeof(symname
), ptr
);
973 new_value
.addr
.seg
= 0;
974 new_value
.type
= DEBUG_ParseStabType(ptr
);
975 new_value
.addr
.off
= load_offset
+ stab_ptr
->n_value
;
976 new_value
.cookie
= DV_TARGET
;
978 * Copy the string to a temp buffer so we
979 * can kill everything after the ':'. We do
980 * it this way because otherwise we end up dirtying
981 * all of the pages related to the stabs, and that
982 * sucks up swap space like crazy.
985 curr_func
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
986 SYM_WINE
| SYM_FUNC
| SYM_INVALID
);
988 curr_func
= DEBUG_AddSymbol( symname
, &new_value
, currpath
,
989 SYM_WINE
| SYM_FUNC
);
994 /* some GCC seem to use a N_FUN "" to mark the end of a function */
1001 * Don't add line number information for this function
1009 * This indicates a new source file. Append the records
1010 * together, to build the correct path name.
1014 * With a.out, there is no NULL string N_SO entry at the end of
1015 * the file. Thus when we find non-consecutive entries,
1016 * we consider that a new file is started.
1018 if( last_nso
< i
-1 )
1021 DEBUG_Normalize(curr_func
);
1032 DEBUG_Normalize(curr_func
);
1038 strcat(currpath
, ptr
);
1040 strcpy(currpath
, ptr
);
1042 DEBUG_ResetIncludes();
1048 * This indicates we are including stuff from an include file.
1049 * If this is the main source, enable the debug stuff, otherwise
1052 in_external_file
= !(subpath
== NULL
|| strcmp(ptr
, subpath
) == 0);
1056 strtabinc
= stab_ptr
->n_value
;
1057 DEBUG_Normalize(curr_func
);
1062 * Ignore this. We don't care what it points to.
1066 DEBUG_AddInclude(DEBUG_CreateInclude(ptr
, stab_ptr
->n_value
));
1071 DEBUG_AddInclude(DEBUG_FindInclude(ptr
, stab_ptr
->n_value
));
1075 * Always ignore these. GCC doesn't even generate them.
1079 DEBUG_Printf(DBG_CHN_MESG
, "Unknown stab type 0x%02x\n", stab_ptr
->n_type
);
1086 DEBUG_Printf(DBG_CHN_MESG
, "%d %x %s\n", stab_ptr
->n_type
,
1087 (unsigned int) stab_ptr
->n_value
,
1088 strs
+ (unsigned int) stab_ptr
->n_un
.n_name
);
1092 DEBUG_FreeRegisteredTypedefs();
1093 DEBUG_FreeIncludes();
1094 DBG_free(curr_types
);
1096 allocated_types
= 0;
1104 * Walk through the entire symbol table and add any symbols we find there.
1105 * This can be used in cases where we have stripped ELF shared libraries,
1106 * or it can be used in cases where we have data symbols for which the address
1107 * isn't encoded in the stabs.
1109 * This is all really quite easy, since we don't have to worry about line
1110 * numbers or local data variables.
1112 static int DEBUG_ProcessElfSymtab(DBG_MODULE
* module
, char* addr
,
1113 u_long load_addr
, Elf32_Shdr
* symtab
,
1116 char * curfile
= NULL
;
1117 struct name_hash
* curr_sym
= NULL
;
1120 DBG_VALUE new_value
;
1126 symp
= (Elf32_Sym
*) (addr
+ symtab
->sh_offset
);
1127 nsym
= symtab
->sh_size
/ sizeof(*symp
);
1128 strp
= (char *) (addr
+ strtab
->sh_offset
);
1130 for(i
=0; i
< nsym
; i
++, symp
++)
1133 * Ignore certain types of entries which really aren't of that much
1136 if( ELF32_ST_TYPE(symp
->st_info
) == STT_SECTION
||
1137 symp
->st_shndx
== STN_UNDEF
)
1142 symname
= strp
+ symp
->st_name
;
1145 * Save the name of the current file, so we have a way of tracking
1146 * static functions/data.
1148 if( ELF32_ST_TYPE(symp
->st_info
) == STT_FILE
)
1155 * See if we already have something for this symbol.
1156 * If so, ignore this entry, because it would have come from the
1157 * stabs or from a previous symbol. If the value is different,
1158 * we will have to keep the darned thing, because there can be
1159 * multiple local symbols by the same name.
1161 if( (DEBUG_GetSymbolValue(symname
, -1, &new_value
, FALSE
) == TRUE
)
1162 && (new_value
.addr
.off
== (load_addr
+ symp
->st_value
)) )
1165 new_value
.addr
.seg
= 0;
1166 new_value
.type
= NULL
;
1167 new_value
.addr
.off
= load_addr
+ symp
->st_value
;
1168 new_value
.cookie
= DV_TARGET
;
1169 flags
= SYM_WINE
| ((ELF32_ST_TYPE(symp
->st_info
) == STT_FUNC
)
1170 ? SYM_FUNC
: SYM_DATA
);
1171 if( ELF32_ST_BIND(symp
->st_info
) == STB_GLOBAL
)
1172 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, NULL
, flags
);
1174 curr_sym
= DEBUG_AddSymbol( symname
, &new_value
, curfile
, flags
);
1177 * Record the size of the symbol. This can come in handy in
1178 * some cases. Not really used yet, however.
1180 if( symp
->st_size
!= 0 )
1181 DEBUG_SetSymbolSize(curr_sym
, symp
->st_size
);
1188 * Loads the symbolic information from ELF module stored in 'filename'
1189 * the module has been loaded at 'load_offset' address, so symbols' address
1190 * relocation is performed
1192 * -1 if the file cannot be found/opened
1193 * 0 if the file doesn't contain symbolic info (or this info cannot be
1197 enum DbgInfoLoad
DEBUG_LoadElfStabs(DBG_MODULE
* module
)
1199 enum DbgInfoLoad dil
= DIL_ERROR
;
1200 char* addr
= (char*)0xffffffff;
1202 struct stat statbuf
;
1210 if (module
->type
!= DMT_ELF
|| ! module
->elf_info
) {
1211 DEBUG_Printf(DBG_CHN_ERR
, "Bad elf module '%s'\n", module
->module_name
);
1215 /* check that the file exists, and that the module hasn't been loaded yet */
1216 if (stat(module
->module_name
, &statbuf
) == -1) goto leave
;
1217 if (S_ISDIR(statbuf
.st_mode
)) goto leave
;
1220 * Now open the file, so that we can mmap() it.
1222 if ((fd
= open(module
->module_name
, O_RDONLY
)) == -1) goto leave
;
1226 * Now mmap() the file.
1228 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1229 if (addr
== (char*)0xffffffff) goto leave
;
1232 * Next, we need to find a few of the internal ELF headers within
1233 * this thing. We need the main executable header, and the section
1236 ehptr
= (Elf32_Ehdr
*) addr
;
1237 spnt
= (Elf32_Shdr
*) (addr
+ ehptr
->e_shoff
);
1238 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
1240 stabsect
= stabstrsect
= -1;
1242 for (i
= 0; i
< ehptr
->e_shnum
; i
++) {
1243 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stab") == 0)
1246 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stabstr") == 0)
1250 if (stabsect
== -1 || stabstrsect
== -1) {
1251 DEBUG_Printf(DBG_CHN_WARN
, "no .stab section\n");
1256 * OK, now just parse all of the stabs.
1258 if (DEBUG_ParseStabs(addr
,
1259 module
->elf_info
->elf_addr
,
1260 spnt
[stabsect
].sh_offset
,
1261 spnt
[stabsect
].sh_size
,
1262 spnt
[stabstrsect
].sh_offset
,
1263 spnt
[stabstrsect
].sh_size
)) {
1267 DEBUG_Printf(DBG_CHN_WARN
, "bad stabs\n");
1271 for (i
= 0; i
< ehptr
->e_shnum
; i
++) {
1272 if ( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".symtab") == 0)
1273 && (spnt
[i
].sh_type
== SHT_SYMTAB
))
1274 DEBUG_ProcessElfSymtab(module
, addr
, module
->elf_info
->elf_addr
,
1275 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
1277 if ( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynsym") == 0)
1278 && (spnt
[i
].sh_type
== SHT_DYNSYM
))
1279 DEBUG_ProcessElfSymtab(module
, addr
, module
->elf_info
->elf_addr
,
1280 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
1284 if (addr
!= (char*)0xffffffff) munmap(addr
, statbuf
.st_size
);
1285 if (fd
!= -1) close(fd
);
1291 * Loads the information for ELF module stored in 'filename'
1292 * the module has been loaded at 'load_offset' address
1294 * -1 if the file cannot be found/opened
1295 * 0 if the file doesn't contain symbolic info (or this info cannot be
1299 static enum DbgInfoLoad
DEBUG_ProcessElfFile(const char* filename
,
1300 unsigned int load_offset
,
1301 unsigned int* dyn_addr
)
1303 enum DbgInfoLoad dil
= DIL_ERROR
;
1304 char* addr
= (char*)0xffffffff;
1306 struct stat statbuf
;
1312 DBG_MODULE
* module
= NULL
;
1316 DEBUG_Printf(DBG_CHN_TRACE
, "Processing elf file '%s'\n", filename
);
1318 /* check that the file exists, and that the module hasn't been loaded yet */
1319 if (stat(filename
, &statbuf
) == -1) goto leave
;
1322 * Now open the file, so that we can mmap() it.
1324 if ((fd
= open(filename
, O_RDONLY
)) == -1) goto leave
;
1327 * Now mmap() the file.
1329 addr
= mmap(0, statbuf
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
1330 if (addr
== (char*)0xffffffff) goto leave
;
1335 * Next, we need to find a few of the internal ELF headers within
1336 * this thing. We need the main executable header, and the section
1339 ehptr
= (Elf32_Ehdr
*) addr
;
1340 spnt
= (Elf32_Shdr
*) (addr
+ ehptr
->e_shoff
);
1341 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
1343 /* if non relocatable ELF, then remove fixed address from computation
1344 * otherwise, all addresses are zero based
1346 delta
= (load_offset
== 0) ? ehptr
->e_entry
: 0;
1348 /* grab size of module once loaded in memory */
1349 ppnt
= (Elf32_Phdr
*) (addr
+ ehptr
->e_phoff
);
1351 for (i
= 0; i
< ehptr
->e_phnum
; i
++) {
1352 if (ppnt
[i
].p_type
!= PT_LOAD
) continue;
1353 if (size
< ppnt
[i
].p_vaddr
- delta
+ ppnt
[i
].p_memsz
)
1354 size
= ppnt
[i
].p_vaddr
- delta
+ ppnt
[i
].p_memsz
;
1357 for (i
= 0; i
< ehptr
->e_shnum
; i
++) {
1358 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".bss") == 0 &&
1359 spnt
[i
].sh_type
== SHT_NOBITS
) {
1360 if (size
< spnt
[i
].sh_addr
- delta
+ spnt
[i
].sh_size
)
1361 size
= spnt
[i
].sh_addr
- delta
+ spnt
[i
].sh_size
;
1363 if (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynamic") == 0 &&
1364 spnt
[i
].sh_type
== SHT_DYNAMIC
) {
1365 if (dyn_addr
) *dyn_addr
= spnt
[i
].sh_addr
;
1369 module
= DEBUG_RegisterELFModule((load_offset
== 0) ? ehptr
->e_entry
: load_offset
,
1376 if ((module
->elf_info
= DBG_alloc(sizeof(ELF_DBG_INFO
))) == NULL
) {
1377 DEBUG_Printf(DBG_CHN_ERR
, "OOM\n");
1381 module
->elf_info
->elf_addr
= load_offset
;
1382 dil
= DEBUG_LoadElfStabs(module
);
1385 if (addr
!= (char*)0xffffffff) munmap(addr
, statbuf
.st_size
);
1386 if (fd
!= -1) close(fd
);
1387 if (module
) module
->dil
= dil
;
1392 static enum DbgInfoLoad
DEBUG_ProcessElfFileFromPath(const char * filename
,
1393 unsigned int load_offset
,
1394 unsigned int* dyn_addr
,
1397 enum DbgInfoLoad dil
= DIL_ERROR
;
1401 if (!path
) return -1;
1403 for (s
= paths
= DBG_strdup(path
); s
&& *s
; s
= (t
) ? (t
+1) : NULL
) {
1406 fn
= (char*)DBG_alloc(strlen(filename
) + 1 + strlen(s
) + 1);
1410 strcat(fn
, filename
);
1411 dil
= DEBUG_ProcessElfFile(fn
, load_offset
, dyn_addr
);
1413 if (dil
!= DIL_ERROR
) break;
1414 s
= (t
) ? (t
+1) : NULL
;
1421 static enum DbgInfoLoad
DEBUG_ProcessElfObject(const char* filename
,
1422 unsigned int load_offset
,
1423 unsigned int* dyn_addr
)
1425 enum DbgInfoLoad dil
= DIL_ERROR
;
1427 if (filename
== NULL
) return DIL_ERROR
;
1428 if (DEBUG_FindModuleByName(filename
, DMT_ELF
)) return DIL_LOADED
;
1430 dil
= DEBUG_ProcessElfFile(filename
, load_offset
, dyn_addr
);
1432 /* if relative pathname, try some absolute base dirs */
1433 if (dil
== DIL_ERROR
&& !strchr(filename
, '/')) {
1434 dil
= DEBUG_ProcessElfFileFromPath(filename
, load_offset
, dyn_addr
, getenv("PATH"));
1435 if (dil
== DIL_ERROR
)
1436 dil
= DEBUG_ProcessElfFileFromPath(filename
, load_offset
, dyn_addr
, getenv("LD_LIBRARY_PATH"));
1439 DEBUG_ReportDIL(dil
, "ELF", filename
, load_offset
);
1444 static BOOL
DEBUG_WalkList(struct r_debug
* dbg_hdr
)
1452 * Now walk the linked list. In all known ELF implementations,
1453 * the dynamic loader maintains this linked list for us. In some
1454 * cases the first entry doesn't appear with a name, in other cases it
1457 for (lm_addr
= (u_long
)dbg_hdr
->r_map
; lm_addr
; lm_addr
= (u_long
)lm
.l_next
) {
1458 if (!DEBUG_READ_MEM_VERBOSE((void*)lm_addr
, &lm
, sizeof(lm
)))
1460 if (lm
.l_addr
!= 0 &&
1461 DEBUG_READ_MEM_VERBOSE((void*)lm
.l_addr
, &ehdr
, sizeof(ehdr
)) &&
1462 ehdr
.e_type
== ET_DYN
&& /* only look at dynamic modules */
1463 lm
.l_name
!= NULL
&&
1464 DEBUG_READ_MEM_VERBOSE((void*)lm
.l_name
, bufstr
, sizeof(bufstr
))) {
1465 bufstr
[sizeof(bufstr
) - 1] = '\0';
1466 DEBUG_ProcessElfObject(bufstr
, (unsigned)lm
.l_addr
, NULL
);
1473 static BOOL
DEBUG_RescanElf(void)
1475 struct r_debug dbg_hdr
;
1477 if (!DEBUG_CurrProcess
||
1478 !DEBUG_READ_MEM_VERBOSE((void*)DEBUG_CurrProcess
->dbg_hdr_addr
, &dbg_hdr
, sizeof(dbg_hdr
)))
1481 switch (dbg_hdr
.r_state
) {
1483 DEBUG_WalkList(&dbg_hdr
);
1488 /* FIXME: this is not currently handled, would need some kind of mark&sweep algo */
1494 enum DbgInfoLoad
DEBUG_ReadExecutableDbgInfo(const char* exe_name
)
1497 struct r_debug dbg_hdr
;
1498 enum DbgInfoLoad dil
= DIL_NOINFO
;
1499 unsigned int dyn_addr
;
1502 * Make sure we can stat and open this file.
1504 if (exe_name
== NULL
) goto leave
;
1505 DEBUG_ProcessElfObject(exe_name
, 0, &dyn_addr
);
1508 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn_addr
, &dyn
, sizeof(dyn
)))
1510 dyn_addr
+= sizeof(dyn
);
1511 } while (dyn
.d_tag
!= DT_DEBUG
&& dyn
.d_tag
!= DT_NULL
);
1512 if (dyn
.d_tag
== DT_NULL
) goto leave
;
1515 * OK, now dig into the actual tables themselves.
1517 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn
.d_un
.d_ptr
, &dbg_hdr
, sizeof(dbg_hdr
)))
1520 assert(!DEBUG_CurrProcess
->dbg_hdr_addr
);
1521 DEBUG_CurrProcess
->dbg_hdr_addr
= (u_long
)dyn
.d_un
.d_ptr
;
1523 if (dbg_hdr
.r_brk
) {
1526 DEBUG_Printf(DBG_CHN_TRACE
, "Setting up a breakpoint on r_brk(%lx)\n",
1527 (unsigned long)dbg_hdr
.r_brk
);
1529 DEBUG_SetBreakpoints(FALSE
);
1531 value
.cookie
= DV_TARGET
;
1533 value
.addr
.off
= (DWORD
)dbg_hdr
.r_brk
;
1534 DEBUG_AddBreakpoint(&value
, DEBUG_RescanElf
);
1535 DEBUG_SetBreakpoints(TRUE
);
1538 dil
= DEBUG_WalkList(&dbg_hdr
);
1544 #else /* !__ELF__ */
1546 int DEBUG_ReadExecutableDbgInfo(const char* exe_name
)
1551 #endif /* __ELF__ */