2 * File stabs.c - read stabs information from the wine executable itself.
4 * Copyright (C) 1996, Eric Youngdale.
12 #ifdef HAVE_SYS_MMAN_H
21 #define PATH_MAX _MAX_PATH
26 #if defined(__svr4__) || defined(__sun)
35 #ifdef HAVE_SYS_MMAN_H
38 #elif defined(__EMX__)
68 * This is how we translate stab types into our internal representations
71 static struct datatype
** stab_types
= NULL
;
72 static int num_stab_types
= 0;
75 * Set so that we know the main executable name and path.
82 struct stab_nlist
*n_next
;
88 unsigned long n_value
;
92 * This is used to keep track of known datatypes so that we don't redefine
93 * them over and over again. It sucks up lots of memory otherwise.
97 struct known_typedef
* next
;
100 struct datatype
* types
[1];
103 #define NR_STAB_HASH 521
105 struct known_typedef
* ktd_head
[NR_STAB_HASH
] = {NULL
,};
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
, 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
!= ':')
141 #define MAX_TD_NESTING 128
143 static int **typenums
;
144 static int *nroftypenums
=NULL
;
145 static int nrofnroftypenums
=0;
146 static int curtypenum
= 0;
150 DEBUG_FileSubNr2StabEnum(int filenr
,int subnr
) {
151 if (nrofnroftypenums
<=filenr
) {
152 nroftypenums
= DBG_realloc(nroftypenums
,sizeof(nroftypenums
[0])*(filenr
+1));
153 memset(nroftypenums
+nrofnroftypenums
,0,(filenr
+1-nrofnroftypenums
)*sizeof(nroftypenums
[0]));
154 typenums
= DBG_realloc(typenums
,sizeof(typenums
[0])*(filenr
+1));
155 memset(typenums
+nrofnroftypenums
,0,sizeof(typenums
[0])*(filenr
+1-nrofnroftypenums
));
156 nrofnroftypenums
=filenr
+1;
158 if (nroftypenums
[filenr
]<=subnr
) {
159 typenums
[filenr
] = DBG_realloc(typenums
[filenr
],sizeof(typenums
[0][0])*(subnr
+1));
160 memset(typenums
[filenr
]+nroftypenums
[filenr
],0,sizeof(typenums
[0][0])*(subnr
+1-nroftypenums
[filenr
]));
161 nroftypenums
[filenr
] = subnr
+1;
163 if (!typenums
[filenr
][subnr
])
164 typenums
[filenr
][subnr
]=++curtypenum
;
166 if( num_stab_types
<= curtypenum
) {
167 num_stab_types
= curtypenum
+ 256;
168 stab_types
= (struct datatype
**) DBG_realloc(stab_types
,
169 num_stab_types
* sizeof(struct datatype
*)
171 memset( stab_types
+ curtypenum
, 0, sizeof(struct datatype
*) * (num_stab_types
- curtypenum
) );
173 /*fprintf(stderr,"(%d,%d) is %d\n",filenr,subnr,typenums[filenr][subnr]); */
174 return typenums
[filenr
][subnr
];
179 DEBUG_ReadTypeEnumBackwards(char*x
) {
186 filenr
=strtol(x
,&x
,10); /* <int> */
188 subnr
=strtol(x
,&x
,10); /* <int> */
191 while ((*x
>='0') && (*x
<='9'))
196 return DEBUG_FileSubNr2StabEnum(filenr
,subnr
);
201 DEBUG_ReadTypeEnum(char **x
) {
206 filenr
=strtol(*x
,x
,10); /* <int> */
208 subnr
=strtol(*x
,x
,10); /* <int> */
212 subnr
= strtol(*x
,x
,10); /* <int> */
214 return DEBUG_FileSubNr2StabEnum(filenr
,subnr
);
219 DEBUG_RegisterTypedef(const char * name
, struct datatype
** types
, int ndef
)
222 struct known_typedef
* ktd
;
227 ktd
= (struct known_typedef
*) DBG_alloc(sizeof(struct known_typedef
)
228 + (ndef
- 1) * sizeof(struct datatype
*));
230 hash
= stab_hash(name
);
232 ktd
->name
= DBG_strdup(name
);
234 memcpy(&ktd
->types
[0], types
, ndef
* sizeof(struct datatype
*));
235 ktd
->next
= ktd_head
[hash
];
236 ktd_head
[hash
] = ktd
;
243 DEBUG_HandlePreviousTypedef(const char * name
, const char * stab
)
246 enum debug_type expect
;
248 struct known_typedef
* ktd
;
251 hash
= stab_hash(name
);
253 for(ktd
= ktd_head
[hash
]; ktd
; ktd
= ktd
->next
)
254 if ((ktd
->name
[0] == name
[0]) && (strcmp(name
, ktd
->name
) == 0) )
258 * Didn't find it. This must be a new one.
264 * Examine the stab to make sure it has the same number of definitions.
267 for(ptr
= strchr(stab
, '='); ptr
; ptr
= strchr(ptr
+1, '='))
269 if( count
>= ktd
->ndefs
)
273 * Make sure the types of all of the objects is consistent with
274 * what we have already parsed.
303 fprintf(stderr
, "Unknown type (%c).\n",ptr
[1]);
306 if( expect
!= DEBUG_GetType(ktd
->types
[count
]) )
311 if( ktd
->ndefs
!= count
)
315 * Go through, dig out all of the type numbers, and substitute the
316 * appropriate things.
319 for(ptr
= strchr(stab
, '='); ptr
; ptr
= strchr(ptr
+1, '='))
320 stab_types
[DEBUG_ReadTypeEnumBackwards(ptr
-1)] = ktd
->types
[count
++];
325 static int DEBUG_FreeRegisteredTypedefs()
329 struct known_typedef
* ktd
;
330 struct known_typedef
* next
;
333 for(j
=0; j
< NR_STAB_HASH
; j
++ )
335 for(ktd
= ktd_head
[j
]; ktd
; ktd
= next
)
351 DEBUG_ParseTypedefStab(char * ptr
, const char * typename
)
356 struct datatype
* curr_type
;
357 struct datatype
* datatype
;
358 struct datatype
* curr_types
[MAX_TD_NESTING
];
359 char element_name
[1024];
362 const char * orig_typename
;
368 orig_typename
= typename
;
370 if( DEBUG_HandlePreviousTypedef(typename
, ptr
) )
374 * Go from back to front. First we go through and figure out what
375 * type numbers we need, and register those types. Then we go in
376 * and fill the details.
379 for( c
= strchr(ptr
, '='); c
!= NULL
; c
= strchr(c
+ 1, '=') )
382 * Back up until we get to a non-numeric character. This is the type
385 typenum
= DEBUG_ReadTypeEnumBackwards(c
-1);
387 if( ntypes
>= MAX_TD_NESTING
)
390 * If this ever happens, just bump the counter.
392 fprintf(stderr
, "Typedef nesting overflow\n");
399 stab_types
[typenum
] = DEBUG_NewDataType(DT_POINTER
, NULL
);
400 curr_types
[ntypes
++] = stab_types
[typenum
];
404 stab_types
[typenum
] = DEBUG_NewDataType(DT_STRUCT
, typename
);
405 curr_types
[ntypes
++] = stab_types
[typenum
];
408 stab_types
[typenum
] = DEBUG_NewDataType(DT_ARRAY
, NULL
);
409 curr_types
[ntypes
++] = stab_types
[typenum
];
414 stab_types
[typenum
] = DEBUG_NewDataType(DT_BASIC
, typename
);
415 curr_types
[ntypes
++] = stab_types
[typenum
];
418 stab_strcpy(element_name
, c
+ 3);
419 stab_types
[typenum
] = DEBUG_NewDataType(DT_STRUCT
, element_name
);
420 curr_types
[ntypes
++] = stab_types
[typenum
];
423 stab_types
[typenum
] = DEBUG_NewDataType(DT_ENUM
, NULL
);
424 curr_types
[ntypes
++] = stab_types
[typenum
];
427 stab_types
[typenum
] = DEBUG_NewDataType(DT_FUNC
, NULL
);
428 curr_types
[ntypes
++] = stab_types
[typenum
];
431 fprintf(stderr
, "Unknown type (%c).\n",c
[1]);
437 * Now register the type so that if we encounter it again, we will know
440 DEBUG_RegisterTypedef(orig_typename
, curr_types
, ntypes
);
443 * OK, now take a second sweep through. Now we will be digging
444 * out the definitions of the various components, and storing
445 * them in the skeletons that we have already allocated. We take
446 * a right-to left search as this is much easier to parse.
448 for( c
= strrchr(ptr
, '='); c
!= NULL
; c
= strrchr(ptr
, '=') )
450 int typenum
= DEBUG_ReadTypeEnumBackwards(c
-1);
451 curr_type
= stab_types
[typenum
];
468 datatype
= stab_types
[DEBUG_ReadTypeEnum(&tc
)];
469 DEBUG_SetPointerType(curr_type
, datatype
);
479 * We have already handled these above.
484 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */
488 DEBUG_ReadTypeEnum(&tc
);
490 arrmin
= strtol(tc
, &tc
, 10); /* <int> */
492 arrmax
= strtol(tc
, &tc
, 10); /* <int> */
494 datatype
= stab_types
[DEBUG_ReadTypeEnum(&tc
)]; /* <typeinfo> */
499 DEBUG_SetArrayParams(curr_type
, arrmin
, arrmax
, datatype
);
506 if( DEBUG_SetStructSize(curr_type
, strtol(tc
, &tc
, 10)) == FALSE
)
509 * We have already filled out this structure. Nothing to do,
510 * so just skip forward to the end of the definition.
512 while( tc
[0] != ';' && tc
[1] != ';' )
525 * Now parse the individual elements of the structure/union.
536 datatype
= stab_types
[DEBUG_ReadTypeEnum(&tc
)];
539 offset
= strtol(tc
, &tc
, 10);
541 size
= strtol(tc
, &tc
, 10);
544 DEBUG_AddStructElement(curr_type
, element_name
, datatype
, offset
, size
);
547 /* ... but proceed parsing to the end of the stab */
552 /* if we had a undeclared value this one is undeclared too.
553 * remove it from the stab_types.
554 * I just set it to NULL to detect bugs in my thoughtprocess.
555 * FIXME: leaks the memory for the structure elements.
556 * FIXME: such structures should have been optimized away
559 stab_types
[typenum
] = NULL
;
570 * Now parse the individual elements of the structure/union.
579 offset
= strtol(tc
, &tc
, 10);
581 DEBUG_AddStructElement(curr_type
, element_name
, NULL
, offset
, 0);
589 fprintf(stderr
, "Unknown type (%c).\n",c
[1]);
598 static struct datatype
*
599 DEBUG_ParseStabType(const char * stab
)
604 * Look through the stab definition, and figure out what datatype
605 * this represents. If we have something we know about, assign the
608 c
= strchr(stab
, ':');
614 * The next character says more about the type (i.e. data, function, etc)
615 * of symbol. Skip it.
619 * The next is either an integer or a (integer,integer).
620 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
622 return stab_types
[DEBUG_ReadTypeEnum(&c
)];
626 DEBUG_ParseStabs(char * addr
, unsigned int load_offset
,
627 unsigned int staboff
, int stablen
,
628 unsigned int strtaboff
, int strtablen
)
630 struct name_hash
* curr_func
= NULL
;
631 struct wine_locals
* curr_loc
= NULL
;
632 struct name_hash
* curr_sym
= NULL
;
633 char currpath
[PATH_MAX
];
643 struct stab_nlist
* stab_ptr
;
646 char * subpath
= NULL
;
649 nstab
= stablen
/ sizeof(struct stab_nlist
);
650 stab_ptr
= (struct stab_nlist
*) (addr
+ staboff
);
651 strs
= (char *) (addr
+ strtaboff
);
653 memset(currpath
, 0, sizeof(currpath
));
656 * Allocate a buffer into which we can build stab strings for cases
657 * where the stab is continued over multiple lines.
660 stabbuff
= (char *) DBG_alloc(stabbufflen
);
664 for(i
=0; i
< nstab
; i
++, stab_ptr
++ )
666 ptr
= strs
+ (unsigned int) stab_ptr
->n_un
.n_name
;
667 if( ptr
[strlen(ptr
) - 1] == '\\' )
670 * Indicates continuation. Append this to the buffer, and go onto the
671 * next record. Repeat the process until we find a stab without the
672 * '/' character, as this indicates we have the whole thing.
675 if( strlen(stabbuff
) + len
> stabbufflen
)
677 stabbufflen
+= 65536;
678 stabbuff
= (char *) DBG_realloc(stabbuff
, stabbufflen
);
680 strncat(stabbuff
, ptr
, len
- 1);
683 else if( stabbuff
[0] != '\0' )
685 strcat( stabbuff
, ptr
);
689 if( strchr(ptr
, '=') != NULL
)
692 * The stabs aren't in writable memory, so copy it over so we are
693 * sure we can scribble on it.
695 if( ptr
!= stabbuff
)
697 strcpy(stabbuff
, ptr
);
700 stab_strcpy(symname
, ptr
);
701 DEBUG_ParseTypedefStab(ptr
, symname
);
704 switch(stab_ptr
->n_type
)
708 * These are useless with ELF. They have no value, and you have to
709 * read the normal symbol table to get the address. Thus we
710 * ignore them, and when we process the normal symbol table
711 * we should do the right thing.
713 * With a.out, they actually do make some amount of sense.
716 new_addr
.type
= DEBUG_ParseStabType(ptr
);
717 new_addr
.off
= load_offset
+ stab_ptr
->n_value
;
719 stab_strcpy(symname
, ptr
);
721 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, currpath
,
722 SYM_WINE
| SYM_DATA
| SYM_INVALID
);
724 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, currpath
,
725 SYM_WINE
| SYM_DATA
);
731 * We need to keep track of these so we get symbol scoping
732 * right for local variables. For now, we just ignore them.
733 * The hooks are already there for dealing with this however,
734 * so all we need to do is to keep count of the nesting level,
735 * and find the RBRAC for each matching LBRAC.
741 * These are static symbols and BSS symbols.
744 new_addr
.type
= DEBUG_ParseStabType(ptr
);
745 new_addr
.off
= load_offset
+ stab_ptr
->n_value
;
747 stab_strcpy(symname
, ptr
);
748 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, currpath
,
749 SYM_WINE
| SYM_DATA
);
753 * These are function parameters.
755 if( (curr_func
!= NULL
)
756 && (stab_ptr
->n_value
!= 0) )
758 stab_strcpy(symname
, ptr
);
759 curr_loc
= DEBUG_AddLocal(curr_func
, 0,
760 stab_ptr
->n_value
, 0, 0, symname
);
761 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
));
765 if( curr_func
!= NULL
)
767 stab_strcpy(symname
, ptr
);
768 curr_loc
= DEBUG_AddLocal(curr_func
, stab_ptr
->n_value
, 0, 0, 0, symname
);
769 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
));
773 if( (curr_func
!= NULL
)
774 && (stab_ptr
->n_value
!= 0) )
776 stab_strcpy(symname
, ptr
);
777 DEBUG_AddLocal(curr_func
, 0,
778 stab_ptr
->n_value
, 0, 0, symname
);
780 else if (curr_func
== NULL
)
782 stab_strcpy(symname
, ptr
);
787 * This is a line number. These are always relative to the start
788 * of the function (N_FUN), and this makes the lookup easier.
790 if( curr_func
!= NULL
)
793 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
798 * This isn't right. The order of the stabs is different under
799 * a.out, and as a result we would end up attaching the line
800 * number to the wrong function.
802 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
803 stab_ptr
->n_value
- curr_func
->addr
.off
);
810 * First, clean up the previous function we were working on.
812 DEBUG_Normalize(curr_func
);
815 * For now, just declare the various functions. Later
816 * on, we will add the line number information and the
822 new_addr
.type
= DEBUG_ParseStabType(ptr
);
823 new_addr
.off
= load_offset
+ stab_ptr
->n_value
;
825 * Copy the string to a temp buffer so we
826 * can kill everything after the ':'. We do
827 * it this way because otherwise we end up dirtying
828 * all of the pages related to the stabs, and that
829 * sucks up swap space like crazy.
831 stab_strcpy(symname
, ptr
);
832 curr_func
= DEBUG_AddSymbol( symname
, &new_addr
, currpath
,
833 SYM_WINE
| SYM_FUNC
);
838 * Don't add line number information for this function
846 * This indicates a new source file. Append the records
847 * together, to build the correct path name.
851 * With a.out, there is no NULL string N_SO entry at the end of
852 * the file. Thus when we find non-consecutive entries,
853 * we consider that a new file is started.
858 DEBUG_Normalize(curr_func
);
869 DEBUG_Normalize(curr_func
);
872 * The datatypes that we would need to use are reset when
873 * we start a new file.
875 memset(stab_types
, 0, num_stab_types
* sizeof(stab_types
[0]));
877 for (i=0;i<nrofnroftypenums;i++)
878 memset(typenums[i],0,sizeof(typenums[i][0])*nroftypenums[i]);
884 strcat(currpath
, ptr
);
886 strcpy(currpath
, ptr
);
893 * This indicates we are including stuff from an include file.
894 * If this is the main source, enable the debug stuff, otherwise
897 if( subpath
== NULL
|| strcmp(ptr
, subpath
) == 0 )
904 DEBUG_Normalize(curr_func
);
910 strtabinc
= stab_ptr
->n_value
;
911 DEBUG_Normalize(curr_func
);
916 * Ignore this. We don't care what it points to.
923 * Always ignore these. GCC doesn't even generate them.
933 fprintf(stderr
, "%d %x %s\n", stab_ptr
->n_type
,
934 (unsigned int) stab_ptr
->n_value
,
935 strs
+ (unsigned int) stab_ptr
->n_un
.n_name
);
939 if( stab_types
!= NULL
)
941 DBG_free(stab_types
);
947 DEBUG_FreeRegisteredTypedefs();
955 * Walk through the entire symbol table and add any symbols we find there.
956 * This can be used in cases where we have stripped ELF shared libraries,
957 * or it can be used in cases where we have data symbols for which the address
958 * isn't encoded in the stabs.
960 * This is all really quite easy, since we don't have to worry about line
961 * numbers or local data variables.
965 DEBUG_ProcessElfSymtab(char * addr
, unsigned int load_offset
,
966 Elf32_Shdr
* symtab
, Elf32_Shdr
* strtab
)
968 char * curfile
= NULL
;
969 struct name_hash
* curr_sym
= NULL
;
979 symp
= (Elf32_Sym
*) (addr
+ symtab
->sh_offset
);
980 nsym
= symtab
->sh_size
/ sizeof(*symp
);
981 strp
= (char *) (addr
+ strtab
->sh_offset
);
983 for(i
=0; i
< nsym
; i
++, symp
++)
986 * Ignore certain types of entries which really aren't of that much
989 if( ELF32_ST_TYPE(symp
->st_info
) == STT_SECTION
)
994 symname
= strp
+ symp
->st_name
;
997 * Save the name of the current file, so we have a way of tracking
998 * static functions/data.
1000 if( ELF32_ST_TYPE(symp
->st_info
) == STT_FILE
)
1008 * See if we already have something for this symbol.
1009 * If so, ignore this entry, because it would have come from the
1010 * stabs or from a previous symbol. If the value is different,
1011 * we will have to keep the darned thing, because there can be
1012 * multiple local symbols by the same name.
1014 if( (DEBUG_GetSymbolValue(symname
, -1, &new_addr
, FALSE
) == TRUE
)
1015 && (new_addr
.off
== (load_offset
+ symp
->st_value
)) )
1019 new_addr
.type
= NULL
;
1020 new_addr
.off
= load_offset
+ symp
->st_value
;
1021 flags
= SYM_WINE
| (ELF32_ST_BIND(symp
->st_info
) == STT_FUNC
1022 ? SYM_FUNC
: SYM_DATA
);
1023 if( ELF32_ST_BIND(symp
->st_info
) == STB_GLOBAL
)
1024 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, NULL
, flags
);
1026 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, curfile
, flags
);
1029 * Record the size of the symbol. This can come in handy in
1030 * some cases. Not really used yet, however.
1032 if( symp
->st_size
!= 0 )
1033 DEBUG_SetSymbolSize(curr_sym
, symp
->st_size
);
1041 DEBUG_ProcessElfObject(char * filename
, unsigned int load_offset
)
1044 struct stat statbuf
;
1047 char * addr
= (char *) 0xffffffff;
1058 * Make sure we can stat and open this file.
1060 if( filename
== NULL
)
1063 status
= stat(filename
, &statbuf
);
1066 char *s
,*t
,*fn
,*paths
;
1067 if (strchr(filename
,'/'))
1069 paths
= DBG_strdup(getenv("PATH"));
1074 fn
= (char*)DBG_alloc(strlen(filename
)+1+strlen(s
)+1);
1077 strcat(fn
,filename
);
1078 if ((rtn
= DEBUG_ProcessElfObject(fn
,load_offset
))) {
1084 if (t
) s
= t
+1; else break;
1086 if (!s
|| !*s
) fprintf(stderr
," not found");
1092 * Now open the file, so that we can mmap() it.
1094 fd
= open(filename
, O_RDONLY
);
1100 * Now mmap() the file.
1102 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
1103 MAP_PRIVATE
, fd
, 0);
1104 if( addr
== (char *) 0xffffffff )
1108 * Next, we need to find a few of the internal ELF headers within
1109 * this thing. We need the main executable header, and the section
1112 ehptr
= (Elf32_Ehdr
*) addr
;
1114 if( load_offset
== 0 )
1115 DEBUG_RegisterELFDebugInfo(ehptr
->e_entry
, statbuf
.st_size
, filename
);
1117 DEBUG_RegisterELFDebugInfo(load_offset
, statbuf
.st_size
, filename
);
1119 spnt
= (Elf32_Shdr
*) (addr
+ ehptr
->e_shoff
);
1120 nsect
= ehptr
->e_shnum
;
1121 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
1123 stabsect
= stabstrsect
= -1;
1125 for(i
=0; i
< nsect
; i
++)
1127 if( strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stab") == 0 )
1130 if( strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stabstr") == 0 )
1134 if( stabsect
== -1 || stabstrsect
== -1 )
1138 * OK, now just parse all of the stabs.
1140 rtn
= DEBUG_ParseStabs(addr
, load_offset
,
1141 spnt
[stabsect
].sh_offset
,
1142 spnt
[stabsect
].sh_size
,
1143 spnt
[stabstrsect
].sh_offset
,
1144 spnt
[stabstrsect
].sh_size
);
1149 for(i
=0; i
< nsect
; i
++)
1151 if( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".symtab") == 0)
1152 && (spnt
[i
].sh_type
== SHT_SYMTAB
) )
1153 DEBUG_ProcessElfSymtab(addr
, load_offset
,
1154 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
1156 if( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynsym") == 0)
1157 && (spnt
[i
].sh_type
== SHT_DYNSYM
) )
1158 DEBUG_ProcessElfSymtab(addr
, load_offset
,
1159 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
1164 if( addr
!= (char *) 0xffffffff )
1165 munmap(addr
, statbuf
.st_size
);
1175 DEBUG_ReadExecutableDbgInfo(void)
1180 struct r_debug
* dbg_hdr
;
1181 struct link_map
* lpnt
= NULL
;
1183 extern Elf32_Dyn _DYNAMIC
[] __attribute__ ((weak
));
1185 extern Elf32_Dyn _DYNAMIC
[];
1190 exe_name
= DEBUG_argv0
;
1193 * Make sure we can stat and open this file.
1195 if( exe_name
== NULL
)
1198 fprintf( stderr
, "Loading symbols: %s", exe_name
);
1199 rowcount
= 17 + strlen(exe_name
);
1200 DEBUG_ProcessElfObject(exe_name
, 0);
1203 * Finally walk the tables that the dynamic loader maintains to find all
1204 * of the other shared libraries which might be loaded. Perform the
1205 * same step for all of these.
1207 if( (&_DYNAMIC
== NULL
) || (_DYNAMIC
== NULL
) )
1213 * Now walk the dynamic section (of the executable, looking for a DT_DEBUG
1216 for(; dynpnt
->d_tag
!= DT_NULL
; dynpnt
++)
1217 if( dynpnt
->d_tag
== DT_DEBUG
)
1220 if( (dynpnt
->d_tag
!= DT_DEBUG
)
1221 || (dynpnt
->d_un
.d_ptr
== 0) )
1225 * OK, now dig into the actual tables themselves.
1227 dbg_hdr
= (struct r_debug
*) dynpnt
->d_un
.d_ptr
;
1228 lpnt
= dbg_hdr
->r_map
;
1231 * Now walk the linked list. In all known ELF implementations,
1232 * the dynamic loader maintains this linked list for us. In some
1233 * cases the first entry doesn't appear with a name, in other cases it
1236 for(; lpnt
; lpnt
= lpnt
->l_next
)
1239 * We already got the stuff for the executable using the
1240 * argv[0] entry above. Here we only need to concentrate on any
1241 * shared libraries which may be loaded.
1243 ehdr
= (Elf32_Ehdr
*) lpnt
->l_addr
;
1244 if( (lpnt
->l_addr
== 0) || (ehdr
->e_type
!= ET_DYN
) )
1247 if( lpnt
->l_name
!= NULL
)
1249 if (rowcount
+ strlen(lpnt
->l_name
) > 76)
1251 fprintf( stderr
, "\n " );
1254 fprintf( stderr
, " %s", lpnt
->l_name
);
1255 rowcount
+= strlen(lpnt
->l_name
) + 1;
1256 DEBUG_ProcessElfObject(lpnt
->l_name
, lpnt
->l_addr
);
1263 fprintf( stderr
, "\n" );
1268 #else /* !__ELF__ */
1275 DEBUG_ReadExecutableDbgInfo(void)
1277 char * addr
= (char *) 0xffffffff;
1282 unsigned int staboff
;
1283 struct stat statbuf
;
1285 unsigned int stroff
;
1287 exe_name
= DEBUG_argv0
;
1290 * Make sure we can stat and open this file.
1292 if( exe_name
== NULL
)
1295 status
= stat(exe_name
, &statbuf
);
1300 * Now open the file, so that we can mmap() it.
1302 fd
= open(exe_name
, O_RDONLY
);
1308 * Now mmap() the file.
1310 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
1311 MAP_PRIVATE
, fd
, 0);
1312 if( addr
== (char *) 0xffffffff )
1315 ahdr
= (struct exec
*) addr
;
1317 staboff
= N_SYMOFF(*ahdr
);
1318 stroff
= N_STROFF(*ahdr
);
1319 rtn
= DEBUG_ParseStabs(addr
, 0,
1323 statbuf
.st_size
- stroff
);
1326 * Give a nice status message here...
1328 fprintf( stderr
, "Loading symbols: %s", exe_name
);
1334 if( addr
!= (char *) 0xffffffff )
1335 munmap(addr
, statbuf
.st_size
);
1345 * Non-linux, non-ELF platforms.
1348 DEBUG_ReadExecutableDbgInfo(void)
1354 #endif /* __ELF__ */