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.
12 #include <sys/types.h>
15 #ifdef HAVE_SYS_MMAN_H
24 #define PATH_MAX _MAX_PATH
29 #if defined(__svr4__) || defined(__sun)
40 #ifdef HAVE_SYS_MMAN_H
41 # include <sys/mman.h>
43 #elif defined(__EMX__)
77 * Set so that we know the main executable name and path.
84 struct stab_nlist
*n_next
;
90 unsigned long n_value
;
94 * This is used to keep track of known datatypes so that we don't redefine
95 * them over and over again. It sucks up lots of memory otherwise.
99 struct known_typedef
* next
;
102 struct datatype
* types
[1];
105 #define NR_STAB_HASH 521
107 struct known_typedef
* ktd_head
[NR_STAB_HASH
] = {NULL
,};
109 static unsigned int stab_hash( const char * name
)
111 unsigned int hash
= 0;
119 hash
= (hash
<< 4) + *p
++;
121 if( (tmp
= (hash
& 0xf0000000)) )
127 return hash
% NR_STAB_HASH
;
131 static void stab_strcpy(char * dest
, const char * source
)
134 * A strcpy routine that stops when we hit the ':' character.
135 * Faster than copying the whole thing, and then nuking the
138 while(*source
!= '\0' && *source
!= ':')
143 extern void DEBUG_PrintAType(struct datatype
*, int);
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;
244 #define MAX_TD_NESTING 128
248 DEBUG_FileSubNr2StabEnum(int filenr
, int subnr
)
250 struct datatype
** ret
;
252 /* fprintf(stderr, "creating type id for (%d,%d)\n", filenr, subnr); */
254 /* FIXME: I could perhaps create a dummy include_def for each compilation
255 * unit which would allow not to handle those two cases separately
259 if (cu_nrofentries
<= subnr
)
261 cu_vector
= DBG_realloc(cu_vector
, sizeof(cu_vector
[0])*(subnr
+1));
262 memset(cu_vector
+cu_nrofentries
, 0, sizeof(cu_vector
[0])*(subnr
+1-cu_nrofentries
));
263 cu_nrofentries
= subnr
+ 1;
265 ret
= &cu_vector
[subnr
];
271 assert(filenr
<= cu_include_stk_idx
);
273 idef
= &include_defs
[cu_include_stack
[filenr
]];
275 if (idef
->nrofentries
<= subnr
)
277 idef
->vector
= DBG_realloc(idef
->vector
, sizeof(idef
->vector
[0])*(subnr
+1));
278 memset(idef
->vector
+ idef
->nrofentries
, 0, sizeof(idef
->vector
[0])*(subnr
+1-idef
->nrofentries
));
279 idef
->nrofentries
= subnr
+ 1;
281 ret
= &idef
->vector
[subnr
];
283 /* fprintf(stderr,"(%d,%d) is %d\n",filenr,subnr,ret); */
289 DEBUG_ReadTypeEnumBackwards(char*x
) {
296 filenr
=strtol(x
,&x
,10); /* <int> */
298 subnr
=strtol(x
,&x
,10); /* <int> */
301 while ((*x
>='0') && (*x
<='9'))
306 return DEBUG_FileSubNr2StabEnum(filenr
,subnr
);
311 DEBUG_ReadTypeEnum(char **x
) {
316 filenr
=strtol(*x
,x
,10); /* <int> */
318 subnr
=strtol(*x
,x
,10); /* <int> */
322 subnr
= strtol(*x
,x
,10); /* <int> */
324 return DEBUG_FileSubNr2StabEnum(filenr
,subnr
);
329 DEBUG_RegisterTypedef(const char * name
, struct datatype
** types
, int ndef
)
332 struct known_typedef
* ktd
;
337 ktd
= (struct known_typedef
*) DBG_alloc(sizeof(struct known_typedef
)
338 + (ndef
- 1) * sizeof(struct datatype
*));
340 hash
= stab_hash(name
);
342 ktd
->name
= DBG_strdup(name
);
344 memcpy(&ktd
->types
[0], types
, ndef
* sizeof(struct datatype
*));
345 ktd
->next
= ktd_head
[hash
];
346 ktd_head
[hash
] = ktd
;
353 DEBUG_HandlePreviousTypedef(const char * name
, const char * stab
)
356 enum debug_type expect
;
358 struct known_typedef
* ktd
;
361 hash
= stab_hash(name
);
363 for(ktd
= ktd_head
[hash
]; ktd
; ktd
= ktd
->next
)
364 if ((ktd
->name
[0] == name
[0]) && (strcmp(name
, ktd
->name
) == 0) )
368 * Didn't find it. This must be a new one.
374 * Examine the stab to make sure it has the same number of definitions.
377 for(ptr
= strchr(stab
, '='); ptr
; ptr
= strchr(ptr
+1, '='))
379 if( count
>= ktd
->ndefs
)
383 * Make sure the types of all of the objects is consistent with
384 * what we have already parsed.
398 case '(': /* it's mainly a ref to another typedef, skip it */
415 fprintf(stderr
, "Unknown type (%c).\n",ptr
[1]);
418 if( expect
!= -1 && expect
!= DEBUG_GetType(ktd
->types
[count
]) )
423 if( ktd
->ndefs
!= count
)
427 * Go through, dig out all of the type numbers, and substitute the
428 * appropriate things.
431 for(ptr
= strchr(stab
, '='); ptr
; ptr
= strchr(ptr
+1, '='))
432 *DEBUG_ReadTypeEnumBackwards(ptr
-1) = ktd
->types
[count
++];
437 static int DEBUG_FreeRegisteredTypedefs()
441 struct known_typedef
* ktd
;
442 struct known_typedef
* next
;
445 for(j
=0; j
< NR_STAB_HASH
; j
++ )
447 for(ktd
= ktd_head
[j
]; ktd
; ktd
= next
)
463 DEBUG_ParseTypedefStab(char * ptr
, const char * typename
)
468 struct datatype
* curr_type
;
469 struct datatype
* datatype
;
470 struct datatype
* curr_types
[MAX_TD_NESTING
];
471 char element_name
[1024];
474 const char * orig_typename
;
480 orig_typename
= typename
;
482 if( DEBUG_HandlePreviousTypedef(typename
, ptr
) )
486 * Go from back to front. First we go through and figure out what
487 * type numbers we need, and register those types. Then we go in
488 * and fill the details.
491 for( c
= strchr(ptr
, '='); c
!= NULL
; c
= strchr(c
+ 1, '=') )
494 * Back up until we get to a non-numeric character, to get datatype
496 struct datatype
** dt
= DEBUG_ReadTypeEnumBackwards(c
-1);
498 if( ntypes
>= MAX_TD_NESTING
)
501 * If this ever happens, just bump the counter.
503 fprintf(stderr
, "Typedef nesting overflow\n");
510 *dt
= DEBUG_NewDataType(DT_POINTER
, NULL
);
511 curr_types
[ntypes
++] = *dt
;
515 *dt
= DEBUG_NewDataType(DT_STRUCT
, typename
);
516 curr_types
[ntypes
++] = *dt
;
519 *dt
= DEBUG_NewDataType(DT_ARRAY
, NULL
);
520 curr_types
[ntypes
++] = *dt
;
523 /* will be handled in next loop,
524 * just a ref to another type
526 curr_types
[ntypes
++] = NULL
;
530 *dt
= DEBUG_NewDataType(DT_BASIC
, typename
);
531 curr_types
[ntypes
++] = *dt
;
534 stab_strcpy(element_name
, c
+ 3);
535 *dt
= DEBUG_NewDataType(DT_STRUCT
, element_name
);
536 curr_types
[ntypes
++] = *dt
;
539 *dt
= DEBUG_NewDataType(DT_ENUM
, NULL
);
540 curr_types
[ntypes
++] = *dt
;
543 *dt
= DEBUG_NewDataType(DT_FUNC
, NULL
);
544 curr_types
[ntypes
++] = *dt
;
547 fprintf(stderr
, "Unknown type (%c).\n",c
[1]);
555 * OK, now take a second sweep through. Now we will be digging
556 * out the definitions of the various components, and storing
557 * them in the skeletons that we have already allocated. We take
558 * a right-to left search as this is much easier to parse.
560 for( c
= strrchr(ptr
, '='); c
!= NULL
; c
= strrchr(ptr
, '=') )
562 struct datatype
** dt
= DEBUG_ReadTypeEnumBackwards(c
-1);
563 struct datatype
** dt2
;
584 datatype
= *DEBUG_ReadTypeEnum(&tc
);
585 DEBUG_SetPointerType(curr_type
, datatype
);
593 dt2
= DEBUG_ReadTypeEnum(&tc
);
599 else if (!*dt
&& !*dt2
)
601 /* this should be a basic type, define it */
602 *dt2
= *dt
= DEBUG_NewDataType(DT_BASIC
, typename
);
606 fprintf(stderr
, "Unknown condition %p %p (%s)\n", *dt
, *dt2
, ptr
);
612 curr_types
[ntp
--] = *dt
;
618 * We have already handled these above.
624 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */
628 DEBUG_ReadTypeEnum(&tc
);
630 arrmin
= strtol(tc
, &tc
, 10); /* <int> */
632 arrmax
= strtol(tc
, &tc
, 10); /* <int> */
634 datatype
= *DEBUG_ReadTypeEnum(&tc
); /* <typeinfo> */
639 DEBUG_SetArrayParams(curr_type
, arrmin
, arrmax
, datatype
);
647 if( DEBUG_SetStructSize(curr_type
, strtol(tc
, &tc
, 10)) == FALSE
)
650 * We have already filled out this structure. Nothing to do,
651 * so just skip forward to the end of the definition.
653 while( tc
[0] != ';' && tc
[1] != ';' )
666 * Now parse the individual elements of the structure/union.
677 datatype
= *DEBUG_ReadTypeEnum(&tc
);
680 offset
= strtol(tc
, &tc
, 10);
682 size
= strtol(tc
, &tc
, 10);
685 DEBUG_AddStructElement(curr_type
, element_name
, datatype
,
690 /* ... but proceed parsing to the end of the stab */
691 fprintf(stderr
, "failure on %s %s\n", ptr
, ti
);
698 /* if we had a undeclared value this one is undeclared too.
699 * remove it from the stab_types.
700 * I just set it to NULL to detect bugs in my thoughtprocess.
701 * FIXME: leaks the memory for the structure elements.
702 * FIXME: such structures should have been optimized away
716 * Now parse the individual elements of the structure/union.
725 offset
= strtol(tc
, &tc
, 10);
727 DEBUG_AddStructElement(curr_type
, element_name
, NULL
, offset
, 0);
735 fprintf(stderr
, "Unknown type (%c).\n",c
[1]);
740 * Now register the type so that if we encounter it again, we will know
743 DEBUG_RegisterTypedef(orig_typename
, curr_types
, ntypes
);
748 static struct datatype
*
749 DEBUG_ParseStabType(const char * stab
)
754 * Look through the stab definition, and figure out what datatype
755 * this represents. If we have something we know about, assign the
758 c
= strchr(stab
, ':');
764 * The next character says more about the type (i.e. data, function, etc)
765 * of symbol. Skip it.
770 * The next is either an integer or a (integer,integer).
771 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
773 return *DEBUG_ReadTypeEnum(&c
);
777 DEBUG_ParseStabs(char * addr
, unsigned int load_offset
,
778 unsigned int staboff
, int stablen
,
779 unsigned int strtaboff
, int strtablen
)
781 struct name_hash
* curr_func
= NULL
;
782 struct wine_locals
* curr_loc
= NULL
;
783 struct name_hash
* curr_sym
= NULL
;
784 char currpath
[PATH_MAX
];
794 struct stab_nlist
* stab_ptr
;
797 char * subpath
= NULL
;
800 nstab
= stablen
/ sizeof(struct stab_nlist
);
801 stab_ptr
= (struct stab_nlist
*) (addr
+ staboff
);
802 strs
= (char *) (addr
+ strtaboff
);
804 memset(currpath
, 0, sizeof(currpath
));
807 * Allocate a buffer into which we can build stab strings for cases
808 * where the stab is continued over multiple lines.
811 stabbuff
= (char *) DBG_alloc(stabbufflen
);
815 for(i
=0; i
< nstab
; i
++, stab_ptr
++ )
817 ptr
= strs
+ (unsigned int) stab_ptr
->n_un
.n_name
;
818 if( ptr
[strlen(ptr
) - 1] == '\\' )
821 * Indicates continuation. Append this to the buffer, and go onto the
822 * next record. Repeat the process until we find a stab without the
823 * '/' character, as this indicates we have the whole thing.
826 if( strlen(stabbuff
) + len
> stabbufflen
)
828 stabbufflen
+= 65536;
829 stabbuff
= (char *) DBG_realloc(stabbuff
, stabbufflen
);
831 strncat(stabbuff
, ptr
, len
- 1);
834 else if( stabbuff
[0] != '\0' )
836 strcat( stabbuff
, ptr
);
840 if( strchr(ptr
, '=') != NULL
)
843 * The stabs aren't in writable memory, so copy it over so we are
844 * sure we can scribble on it.
846 if( ptr
!= stabbuff
)
848 strcpy(stabbuff
, ptr
);
851 stab_strcpy(symname
, ptr
);
852 DEBUG_ParseTypedefStab(ptr
, symname
);
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, they actually do make some amount of sense.
867 new_addr
.type
= DEBUG_ParseStabType(ptr
);
868 new_addr
.off
= load_offset
+ stab_ptr
->n_value
;
870 stab_strcpy(symname
, ptr
);
872 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, currpath
,
873 SYM_WINE
| SYM_DATA
| SYM_INVALID
);
875 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, currpath
,
876 SYM_WINE
| SYM_DATA
);
882 * We need to keep track of these so we get symbol scoping
883 * right for local variables. For now, we just ignore them.
884 * The hooks are already there for dealing with this however,
885 * so all we need to do is to keep count of the nesting level,
886 * and find the RBRAC for each matching LBRAC.
892 * These are static symbols and BSS symbols.
895 new_addr
.type
= DEBUG_ParseStabType(ptr
);
896 new_addr
.off
= load_offset
+ stab_ptr
->n_value
;
898 stab_strcpy(symname
, ptr
);
899 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, currpath
,
900 SYM_WINE
| SYM_DATA
);
904 * These are function parameters.
906 if( (curr_func
!= NULL
)
907 && (stab_ptr
->n_value
!= 0) )
909 stab_strcpy(symname
, ptr
);
910 curr_loc
= DEBUG_AddLocal( curr_func
, 0,
911 stab_ptr
->n_value
, 0, 0, symname
);
912 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
916 if( curr_func
!= NULL
)
918 stab_strcpy(symname
, ptr
);
919 curr_loc
= DEBUG_AddLocal( curr_func
, stab_ptr
->n_value
,
921 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
) );
925 if( (curr_func
!= NULL
)
926 && (stab_ptr
->n_value
!= 0) )
928 stab_strcpy(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
) );
933 else if (curr_func
== NULL
)
935 stab_strcpy(symname
, ptr
);
940 * This is a line number. These are always relative to the start
941 * of the function (N_FUN), and this makes the lookup easier.
943 if( curr_func
!= NULL
)
946 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
951 * This isn't right. The order of the stabs is different under
952 * a.out, and as a result we would end up attaching the line
953 * number to the wrong function.
955 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
956 stab_ptr
->n_value
- curr_func
->addr
.off
);
963 * First, clean up the previous function we were working on.
965 DEBUG_Normalize(curr_func
);
968 * For now, just declare the various functions. Later
969 * on, we will add the line number information and the
975 new_addr
.type
= DEBUG_ParseStabType(ptr
);
976 new_addr
.off
= load_offset
+ stab_ptr
->n_value
;
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.
984 stab_strcpy(symname
, ptr
);
985 curr_func
= DEBUG_AddSymbol( symname
, &new_addr
, currpath
,
986 SYM_WINE
| SYM_FUNC
);
991 * Don't add line number information for this function
999 * This indicates a new source file. Append the records
1000 * together, to build the correct path name.
1004 * With a.out, there is no NULL string N_SO entry at the end of
1005 * the file. Thus when we find non-consecutive entries,
1006 * we consider that a new file is started.
1008 if( last_nso
< i
-1 )
1011 DEBUG_Normalize(curr_func
);
1022 DEBUG_Normalize(curr_func
);
1028 strcat(currpath
, ptr
);
1030 strcpy(currpath
, ptr
);
1032 DEBUG_ResetIncludes();
1038 * This indicates we are including stuff from an include file.
1039 * If this is the main source, enable the debug stuff, otherwise
1042 if( subpath
== NULL
|| strcmp(ptr
, subpath
) == 0 )
1049 DEBUG_Normalize(curr_func
);
1055 strtabinc
= stab_ptr
->n_value
;
1056 DEBUG_Normalize(curr_func
);
1061 * Ignore this. We don't care what it points to.
1065 DEBUG_AddInclude(DEBUG_CreateInclude(ptr
, stab_ptr
->n_value
));
1070 DEBUG_AddInclude(DEBUG_FindInclude(ptr
, stab_ptr
->n_value
));
1074 * Always ignore these. GCC doesn't even generate them.
1078 fprintf(stderr
, "Unkown stab type 0x%02x\n", stab_ptr
->n_type
);
1085 fprintf(stderr
, "%d %x %s\n", stab_ptr
->n_type
,
1086 (unsigned int) stab_ptr
->n_value
,
1087 strs
+ (unsigned int) stab_ptr
->n_un
.n_name
);
1091 DEBUG_FreeRegisteredTypedefs();
1092 DEBUG_FreeIncludes();
1100 * Walk through the entire symbol table and add any symbols we find there.
1101 * This can be used in cases where we have stripped ELF shared libraries,
1102 * or it can be used in cases where we have data symbols for which the address
1103 * isn't encoded in the stabs.
1105 * This is all really quite easy, since we don't have to worry about line
1106 * numbers or local data variables.
1110 DEBUG_ProcessElfSymtab(char * addr
, unsigned int load_offset
,
1111 Elf32_Shdr
* symtab
, Elf32_Shdr
* strtab
)
1113 char * curfile
= NULL
;
1114 struct name_hash
* curr_sym
= NULL
;
1124 symp
= (Elf32_Sym
*) (addr
+ symtab
->sh_offset
);
1125 nsym
= symtab
->sh_size
/ sizeof(*symp
);
1126 strp
= (char *) (addr
+ strtab
->sh_offset
);
1128 for(i
=0; i
< nsym
; i
++, symp
++)
1131 * Ignore certain types of entries which really aren't of that much
1134 if( ELF32_ST_TYPE(symp
->st_info
) == STT_SECTION
)
1139 symname
= strp
+ symp
->st_name
;
1142 * Save the name of the current file, so we have a way of tracking
1143 * static functions/data.
1145 if( ELF32_ST_TYPE(symp
->st_info
) == STT_FILE
)
1153 * See if we already have something for this symbol.
1154 * If so, ignore this entry, because it would have come from the
1155 * stabs or from a previous symbol. If the value is different,
1156 * we will have to keep the darned thing, because there can be
1157 * multiple local symbols by the same name.
1159 if( (DEBUG_GetSymbolValue(symname
, -1, &new_addr
, FALSE
) == TRUE
)
1160 && (new_addr
.off
== (load_offset
+ symp
->st_value
)) )
1164 new_addr
.type
= NULL
;
1165 new_addr
.off
= load_offset
+ symp
->st_value
;
1166 flags
= SYM_WINE
| (ELF32_ST_BIND(symp
->st_info
) == STT_FUNC
1167 ? SYM_FUNC
: SYM_DATA
);
1168 if( ELF32_ST_BIND(symp
->st_info
) == STB_GLOBAL
)
1169 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, NULL
, flags
);
1171 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, curfile
, flags
);
1174 * Record the size of the symbol. This can come in handy in
1175 * some cases. Not really used yet, however.
1177 if( symp
->st_size
!= 0 )
1178 DEBUG_SetSymbolSize(curr_sym
, symp
->st_size
);
1186 DEBUG_ProcessElfObject(char * filename
, unsigned int load_offset
)
1189 struct stat statbuf
;
1192 char * addr
= (char *) 0xffffffff;
1203 * Make sure we can stat and open this file.
1205 if( filename
== NULL
)
1208 status
= stat(filename
, &statbuf
);
1211 char *s
,*t
,*fn
,*paths
;
1212 if (strchr(filename
,'/'))
1214 paths
= DBG_strdup(getenv("PATH"));
1219 fn
= (char*)DBG_alloc(strlen(filename
)+1+strlen(s
)+1);
1222 strcat(fn
,filename
);
1223 if ((rtn
= DEBUG_ProcessElfObject(fn
,load_offset
))) {
1229 if (t
) s
= t
+1; else break;
1231 if (!s
|| !*s
) fprintf(stderr
," not found");
1237 * Now open the file, so that we can mmap() it.
1239 fd
= open(filename
, O_RDONLY
);
1245 * Now mmap() the file.
1247 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
1248 MAP_PRIVATE
, fd
, 0);
1249 if( addr
== (char *) 0xffffffff )
1253 * Next, we need to find a few of the internal ELF headers within
1254 * this thing. We need the main executable header, and the section
1257 ehptr
= (Elf32_Ehdr
*) addr
;
1259 if( load_offset
== 0 )
1260 DEBUG_RegisterELFDebugInfo(ehptr
->e_entry
, statbuf
.st_size
, filename
);
1262 DEBUG_RegisterELFDebugInfo(load_offset
, statbuf
.st_size
, filename
);
1264 spnt
= (Elf32_Shdr
*) (addr
+ ehptr
->e_shoff
);
1265 nsect
= ehptr
->e_shnum
;
1266 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
1268 stabsect
= stabstrsect
= -1;
1270 for(i
=0; i
< nsect
; i
++)
1272 if( strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stab") == 0 )
1275 if( strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stabstr") == 0 )
1279 if( stabsect
== -1 || stabstrsect
== -1 )
1283 * OK, now just parse all of the stabs.
1285 rtn
= DEBUG_ParseStabs(addr
, load_offset
,
1286 spnt
[stabsect
].sh_offset
,
1287 spnt
[stabsect
].sh_size
,
1288 spnt
[stabstrsect
].sh_offset
,
1289 spnt
[stabstrsect
].sh_size
);
1294 for(i
=0; i
< nsect
; i
++)
1296 if( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".symtab") == 0)
1297 && (spnt
[i
].sh_type
== SHT_SYMTAB
) )
1298 DEBUG_ProcessElfSymtab(addr
, load_offset
,
1299 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
1301 if( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynsym") == 0)
1302 && (spnt
[i
].sh_type
== SHT_DYNSYM
) )
1303 DEBUG_ProcessElfSymtab(addr
, load_offset
,
1304 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
1309 if( addr
!= (char *) 0xffffffff )
1310 munmap(addr
, statbuf
.st_size
);
1320 DEBUG_ReadExecutableDbgInfo(void)
1325 struct r_debug
* dbg_hdr
;
1326 struct link_map
* lpnt
= NULL
;
1328 extern Elf32_Dyn _DYNAMIC
[] __attribute__ ((weak
));
1330 extern Elf32_Dyn _DYNAMIC
[];
1335 exe_name
= DEBUG_argv0
;
1338 * Make sure we can stat and open this file.
1340 if( exe_name
== NULL
)
1343 fprintf( stderr
, "Loading symbols: %s", exe_name
);
1344 rowcount
= 17 + strlen(exe_name
);
1345 DEBUG_ProcessElfObject(exe_name
, 0);
1348 * Finally walk the tables that the dynamic loader maintains to find all
1349 * of the other shared libraries which might be loaded. Perform the
1350 * same step for all of these.
1352 if( (&_DYNAMIC
== NULL
) || (_DYNAMIC
== NULL
) )
1358 * Now walk the dynamic section (of the executable, looking for a DT_DEBUG
1361 for(; dynpnt
->d_tag
!= DT_NULL
; dynpnt
++)
1362 if( dynpnt
->d_tag
== DT_DEBUG
)
1365 if( (dynpnt
->d_tag
!= DT_DEBUG
)
1366 || (dynpnt
->d_un
.d_ptr
== 0) )
1370 * OK, now dig into the actual tables themselves.
1372 dbg_hdr
= (struct r_debug
*) dynpnt
->d_un
.d_ptr
;
1373 lpnt
= dbg_hdr
->r_map
;
1376 * Now walk the linked list. In all known ELF implementations,
1377 * the dynamic loader maintains this linked list for us. In some
1378 * cases the first entry doesn't appear with a name, in other cases it
1381 for(; lpnt
; lpnt
= lpnt
->l_next
)
1384 * We already got the stuff for the executable using the
1385 * argv[0] entry above. Here we only need to concentrate on any
1386 * shared libraries which may be loaded.
1388 ehdr
= (Elf32_Ehdr
*) lpnt
->l_addr
;
1389 if( (lpnt
->l_addr
== 0) || (ehdr
->e_type
!= ET_DYN
) )
1392 if( lpnt
->l_name
!= NULL
)
1394 if (rowcount
+ strlen(lpnt
->l_name
) > 76)
1396 fprintf( stderr
, "\n " );
1399 fprintf( stderr
, " %s", lpnt
->l_name
);
1400 rowcount
+= strlen(lpnt
->l_name
) + 1;
1401 DEBUG_ProcessElfObject(lpnt
->l_name
, lpnt
->l_addr
);
1408 fprintf( stderr
, "\n" );
1413 #else /* !__ELF__ */
1420 DEBUG_ReadExecutableDbgInfo(void)
1422 char * addr
= (char *) 0xffffffff;
1427 unsigned int staboff
;
1428 struct stat statbuf
;
1430 unsigned int stroff
;
1432 exe_name
= DEBUG_argv0
;
1435 * Make sure we can stat and open this file.
1437 if( exe_name
== NULL
)
1440 status
= stat(exe_name
, &statbuf
);
1445 * Now open the file, so that we can mmap() it.
1447 fd
= open(exe_name
, O_RDONLY
);
1453 * Now mmap() the file.
1455 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
1456 MAP_PRIVATE
, fd
, 0);
1457 if( addr
== (char *) 0xffffffff )
1460 ahdr
= (struct exec
*) addr
;
1462 staboff
= N_SYMOFF(*ahdr
);
1463 stroff
= N_STROFF(*ahdr
);
1464 rtn
= DEBUG_ParseStabs(addr
, 0,
1468 statbuf
.st_size
- stroff
);
1471 * Give a nice status message here...
1473 fprintf( stderr
, "Loading symbols: %s", exe_name
);
1479 if( addr
!= (char *) 0xffffffff )
1480 munmap(addr
, statbuf
.st_size
);
1490 * Non-linux, non-ELF platforms.
1493 DEBUG_ReadExecutableDbgInfo(void)
1499 #endif /* __ELF__ */