2 * File stabs.c - read stabs information from the wine executable itself.
4 * Copyright (C) 1996, Eric Youngdale.
55 * This is how we translate stab types into our internal representations
58 static struct datatype
** stab_types
= NULL
;
59 static int num_stab_types
= 0;
62 * Set so that we know the main executable name and path.
69 struct stab_nlist
*n_next
;
75 unsigned long n_value
;
78 static void stab_strcpy(char * dest
, const char * source
)
81 * A strcpy routine that stops when we hit the ':' character.
82 * Faster than copying the whole thing, and then nuking the
85 while(*source
!= '\0' && *source
!= ':')
95 DEBUG_ParseTypedefStab(char * ptr
, const char * typename
)
100 struct datatype
* curr_type
;
101 struct datatype
* datatype
;
102 char element_name
[1024];
111 * Go from back to front. First we go through and figure out what type numbers
112 * we need, and register those types. Then we go in and fill the details.
114 for( c
= strchr(ptr
, '='); c
!= NULL
; c
= strchr(c
+ 1, '=') )
117 * Back up until we get to a non-numeric character. This is the type
121 while( *tc
>= '0' && *tc
<= '9' )
125 typenum
= atol(tc
+ 1);
126 if( num_stab_types
<= typenum
)
128 num_stab_types
= typenum
+ 32;
129 stab_types
= (struct datatype
**) xrealloc(stab_types
,
130 num_stab_types
* sizeof(struct datatype
*));
131 if( stab_types
== NULL
)
140 stab_types
[typenum
] = DEBUG_NewDataType(POINTER
, NULL
);
144 stab_types
[typenum
] = DEBUG_NewDataType(STRUCT
, typename
);
147 stab_types
[typenum
] = DEBUG_NewDataType(ARRAY
, NULL
);
151 stab_types
[typenum
] = DEBUG_NewDataType(BASIC
, typename
);
154 stab_strcpy(element_name
, c
+ 3);
155 stab_types
[typenum
] = DEBUG_NewDataType(STRUCT
, element_name
);
158 stab_types
[typenum
] = DEBUG_NewDataType(ENUM
, NULL
);
161 stab_types
[typenum
] = DEBUG_NewDataType(FUNC
, NULL
);
164 fprintf(stderr
, "Unknown type.\n");
170 * OK, now take a second sweep through. Now we will be digging out the definitions
171 * of the various components, and storing them in the skeletons that we have already
172 * allocated. We take a right-to left search as this is much easier to parse.
174 for( c
= strrchr(ptr
, '='); c
!= NULL
; c
= strrchr(ptr
, '=') )
177 * Back up until we get to a non-numeric character. This is the type
181 while( *tc
>= '0' && *tc
<= '9' )
185 typenum
= atol(tc
+ 1);
186 curr_type
= stab_types
[typenum
];
210 datatype
= stab_types
[strtol(tc
, &tc
, 10)];
211 DEBUG_SetPointerType(curr_type
, datatype
);
224 * We have already handled these above.
230 arrmin
= strtol(tc
, &tc
, 10);
232 arrmax
= strtol(tc
, &tc
, 10);
234 datatype
= stab_types
[strtol(tc
, &tc
, 10)];
244 DEBUG_SetArrayParams(curr_type
, arrmin
, arrmax
, datatype
);
249 DEBUG_SetStructSize(curr_type
, strtol(tc
, &tc
, 10));
251 * Now parse the individual elements of the structure/union.
262 datatype
= stab_types
[strtol(tc
, &tc
, 10)];
264 offset
= strtol(tc
, &tc
, 10);
266 size
= strtol(tc
, &tc
, 10);
268 DEBUG_AddStructElement(curr_type
, element_name
, datatype
, offset
, size
);
282 * Now parse the individual elements of the structure/union.
293 offset
= strtol(tc
, &tc
, 10);
295 DEBUG_AddStructElement(curr_type
, element_name
, NULL
, offset
, 0);
307 fprintf(stderr
, "Unknown type.\n");
320 static struct datatype
*
321 DEBUG_ParseStabType(const char * stab
)
327 * Look through the stab definition, and figure out what datatype
328 * this represents. If we have something we know about, assign the
331 c
= strchr(stab
, ':');
339 * The next character says more about the type (i.e. data, function, etc)
340 * of symbol. Skip it.
346 if( typenum
< num_stab_types
&& stab_types
[typenum
] != NULL
)
348 return stab_types
[typenum
];
356 DEBUG_ParseStabs(char * addr
, unsigned int load_offset
,
357 unsigned int staboff
, int stablen
,
358 unsigned int strtaboff
, int strtablen
)
360 struct name_hash
* curr_func
= NULL
;
361 struct wine_locals
* curr_loc
= NULL
;
362 struct name_hash
* curr_sym
= NULL
;
363 char currpath
[PATH_MAX
];
373 struct stab_nlist
* stab_ptr
;
376 char * subpath
= NULL
;
379 nstab
= stablen
/ sizeof(struct stab_nlist
);
380 stab_ptr
= (struct stab_nlist
*) (addr
+ staboff
);
381 strs
= (char *) (addr
+ strtaboff
);
383 memset(currpath
, 0, sizeof(currpath
));
386 * Allocate a buffer into which we can build stab strings for cases
387 * where the stab is continued over multiple lines.
390 stabbuff
= (char *) xmalloc(stabbufflen
);
391 if( stabbuff
== NULL
)
398 for(i
=0; i
< nstab
; i
++, stab_ptr
++ )
400 ptr
= strs
+ (unsigned int) stab_ptr
->n_un
.n_name
;
401 if( ptr
[strlen(ptr
) - 1] == '\\' )
404 * Indicates continuation. Append this to the buffer, and go onto the
405 * next record. Repeat the process until we find a stab without the
406 * '/' character, as this indicates we have the whole thing.
409 if( strlen(stabbuff
) + len
> stabbufflen
)
411 stabbufflen
+= 65536;
412 stabbuff
= (char *) xrealloc(stabbuff
, stabbufflen
);
413 if( stabbuff
== NULL
)
418 strncat(stabbuff
, ptr
, len
- 1);
421 else if( stabbuff
[0] != '\0' )
423 strcat( stabbuff
, ptr
);
427 if( strchr(ptr
, '=') != NULL
)
430 * The stabs aren't in writable memory, so copy it over so we are
431 * sure we can scribble on it.
433 if( ptr
!= stabbuff
)
435 strcpy(stabbuff
, ptr
);
438 stab_strcpy(symname
, ptr
);
439 DEBUG_ParseTypedefStab(ptr
, symname
);
442 switch(stab_ptr
->n_type
)
446 * These are useless with ELF. They have no value, and you have to
447 * read the normal symbol table to get the address. Thus we
448 * ignore them, and when we process the normal symbol table
449 * we should do the right thing.
451 * With a.out, they actually do make some amount of sense.
454 new_addr
.type
= DEBUG_ParseStabType(ptr
);
455 new_addr
.off
= load_offset
+ stab_ptr
->n_value
;
457 stab_strcpy(symname
, ptr
);
459 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, currpath
,
460 SYM_WINE
| SYM_DATA
| SYM_INVALID
);
462 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, currpath
,
463 SYM_WINE
| SYM_DATA
);
469 * We need to keep track of these so we get symbol scoping
470 * right for local variables. For now, we just ignore them.
471 * The hooks are already there for dealing with this however,
472 * so all we need to do is to keep count of the nesting level,
473 * and find the RBRAC for each matching LBRAC.
479 * These are static symbols and BSS symbols.
482 new_addr
.type
= DEBUG_ParseStabType(ptr
);
483 new_addr
.off
= load_offset
+ stab_ptr
->n_value
;
485 stab_strcpy(symname
, ptr
);
486 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, currpath
,
487 SYM_WINE
| SYM_DATA
);
491 * These are function parameters.
493 if( (curr_func
!= NULL
)
494 && (stab_ptr
->n_value
!= 0) )
496 stab_strcpy(symname
, ptr
);
497 curr_loc
= DEBUG_AddLocal(curr_func
, 0,
498 stab_ptr
->n_value
, 0, 0, symname
);
499 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
));
503 if( curr_func
!= NULL
)
505 stab_strcpy(symname
, ptr
);
506 curr_loc
= DEBUG_AddLocal(curr_func
, stab_ptr
->n_value
, 0, 0, 0, symname
);
507 DEBUG_SetLocalSymbolType( curr_loc
, DEBUG_ParseStabType(ptr
));
511 if( (curr_func
!= NULL
)
512 && (stab_ptr
->n_value
!= 0) )
514 stab_strcpy(symname
, ptr
);
515 DEBUG_AddLocal(curr_func
, 0,
516 stab_ptr
->n_value
, 0, 0, symname
);
518 else if (curr_func
== NULL
)
520 stab_strcpy(symname
, ptr
);
525 * This is a line number. These are always relative to the start
526 * of the function (N_FUN), and this makes the lookup easier.
528 if( curr_func
!= NULL
)
531 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
536 * This isn't right. The order of the stabs is different under
537 * a.out, and as a result we would end up attaching the line
538 * number to the wrong function.
540 DEBUG_AddLineNumber(curr_func
, stab_ptr
->n_desc
,
541 stab_ptr
->n_value
- curr_func
->addr
.off
);
548 * First, clean up the previous function we were working on.
550 DEBUG_Normalize(curr_func
);
553 * For now, just declare the various functions. Later
554 * on, we will add the line number information and the
560 new_addr
.type
= DEBUG_ParseStabType(ptr
);
561 new_addr
.off
= load_offset
+ stab_ptr
->n_value
;
563 * Copy the string to a temp buffer so we
564 * can kill everything after the ':'. We do
565 * it this way because otherwise we end up dirtying
566 * all of the pages related to the stabs, and that
567 * sucks up swap space like crazy.
569 stab_strcpy(symname
, ptr
);
570 curr_func
= DEBUG_AddSymbol( symname
, &new_addr
, currpath
,
571 SYM_WINE
| SYM_FUNC
);
576 * Don't add line number information for this function
584 * This indicates a new source file. Append the records
585 * together, to build the correct path name.
589 * With a.out, there is no NULL string N_SO entry at the end of
590 * the file. Thus when we find non-consecutive entries,
591 * we consider that a new file is started.
596 DEBUG_Normalize(curr_func
);
607 DEBUG_Normalize(curr_func
);
610 * The datatypes that we would need to use are reset when
611 * we start a new file.
613 memset(stab_types
, 0, num_stab_types
* sizeof(stab_types
));
617 strcat(currpath
, ptr
);
624 * This indicates we are including stuff from an include file.
625 * If this is the main source, enable the debug stuff, otherwise
628 if( subpath
== NULL
|| strcmp(ptr
, subpath
) == 0 )
635 DEBUG_Normalize(curr_func
);
641 strtabinc
= stab_ptr
->n_value
;
642 DEBUG_Normalize(curr_func
);
647 * Ignore this. We don't care what it points to.
654 * Always ignore these. GCC doesn't even generate them.
664 fprintf(stderr
, "%d %x %s\n", stab_ptr
->n_type
,
665 (unsigned int) stab_ptr
->n_value
,
666 strs
+ (unsigned int) stab_ptr
->n_un
.n_name
);
672 if( stab_types
!= NULL
)
685 * Walk through the entire symbol table and add any symbols we find there.
686 * This can be used in cases where we have stripped ELF shared libraries,
687 * or it can be used in cases where we have data symbols for which the address
688 * isn't encoded in the stabs.
690 * This is all really quite easy, since we don't have to worry about line
691 * numbers or local data variables.
695 DEBUG_ProcessElfSymtab(char * addr
, unsigned int load_offset
,
696 Elf32_Shdr
* symtab
, Elf32_Shdr
* strtab
)
698 char * curfile
= NULL
;
699 struct name_hash
* curr_sym
= NULL
;
709 symp
= (Elf32_Sym
*) (addr
+ symtab
->sh_offset
);
710 nsym
= symtab
->sh_size
/ sizeof(*symp
);
711 strp
= (char *) (addr
+ strtab
->sh_offset
);
713 for(i
=0; i
< nsym
; i
++, symp
++)
716 * Ignore certain types of entries which really aren't of that much
719 if( ELF32_ST_TYPE(symp
->st_info
) == STT_SECTION
)
724 symname
= strp
+ symp
->st_name
;
727 * Save the name of the current file, so we have a way of tracking
728 * static functions/data.
730 if( ELF32_ST_TYPE(symp
->st_info
) == STT_FILE
)
738 * See if we already have something for this symbol.
739 * If so, ignore this entry, because it would have come from the
740 * stabs or from a previous symbol. If the value is different,
741 * we will have to keep the darned thing, because there can be
742 * multiple local symbols by the same name.
744 if( (DEBUG_GetSymbolValue(symname
, -1, &new_addr
, FALSE
) == TRUE
)
745 && (new_addr
.off
== (load_offset
+ symp
->st_value
)) )
751 new_addr
.type
= NULL
;
752 new_addr
.off
= load_offset
+ symp
->st_value
;
753 flags
= SYM_WINE
| (ELF32_ST_BIND(symp
->st_info
) == STT_FUNC
754 ? SYM_FUNC
: SYM_DATA
);
755 if( ELF32_ST_BIND(symp
->st_info
) == STB_GLOBAL
)
757 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, NULL
, flags
);
761 curr_sym
= DEBUG_AddSymbol( symname
, &new_addr
, curfile
, flags
);
765 * Record the size of the symbol. This can come in handy in
766 * some cases. Not really used yet, however.
768 if( symp
->st_size
!= 0 )
770 DEBUG_SetSymbolSize(curr_sym
, symp
->st_size
);
779 DEBUG_ProcessElfObject(char * filename
, unsigned int load_offset
)
785 char * addr
= (char *) 0xffffffff;
796 * Make sure we can stat and open this file.
798 if( filename
== NULL
)
803 status
= stat(filename
, &statbuf
);
810 * Now open the file, so that we can mmap() it.
812 fd
= open(filename
, O_RDONLY
);
820 * Now mmap() the file.
822 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
826 * Give a nice status message here...
828 fprintf(stderr
, "Loading symbols from ELF file %s...\n", filename
);
831 * Next, we need to find a few of the internal ELF headers within
832 * this thing. We need the main executable header, and the section
835 ehptr
= (Elf32_Ehdr
*) addr
;
837 if( load_offset
== NULL
)
839 DEBUG_RegisterELFDebugInfo(ehptr
->e_entry
, statbuf
.st_size
, filename
);
843 DEBUG_RegisterELFDebugInfo(load_offset
, statbuf
.st_size
, filename
);
846 spnt
= (Elf32_Shdr
*) (addr
+ ehptr
->e_shoff
);
847 nsect
= ehptr
->e_shnum
;
848 shstrtab
= (addr
+ spnt
[ehptr
->e_shstrndx
].sh_offset
);
850 stabsect
= stabstrsect
= -1;
852 for(i
=0; i
< nsect
; i
++)
854 if( strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stab") == 0 )
859 if( strcmp(shstrtab
+ spnt
[i
].sh_name
, ".stabstr") == 0 )
865 if( stabsect
== -1 || stabstrsect
== -1 )
871 * OK, now just parse all of the stabs.
873 rtn
= DEBUG_ParseStabs(addr
, load_offset
,
874 spnt
[stabsect
].sh_offset
,
875 spnt
[stabsect
].sh_size
,
876 spnt
[stabstrsect
].sh_offset
,
877 spnt
[stabstrsect
].sh_size
);
884 for(i
=0; i
< nsect
; i
++)
886 if( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".symtab") == 0)
887 && (spnt
[i
].sh_type
== SHT_SYMTAB
) )
889 DEBUG_ProcessElfSymtab(addr
, load_offset
,
890 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
893 if( (strcmp(shstrtab
+ spnt
[i
].sh_name
, ".dynsym") == 0)
894 && (spnt
[i
].sh_type
== SHT_DYNSYM
) )
896 DEBUG_ProcessElfSymtab(addr
, load_offset
,
897 spnt
+ i
, spnt
+ spnt
[i
].sh_link
);
903 if( addr
!= (char *) 0xffffffff )
905 munmap(addr
, statbuf
.st_size
);
918 DEBUG_ReadExecutableDbgInfo(void)
923 struct r_debug
* dbg_hdr
;
924 struct link_map
* lpnt
= NULL
;
925 extern Elf32_Dyn _DYNAMIC
[];
928 exe_name
= DEBUG_argv0
;
931 * Make sure we can stat and open this file.
933 if( exe_name
== NULL
)
938 DEBUG_ProcessElfObject(exe_name
, 0);
941 * Finally walk the tables that the dynamic loader maintains to find all
942 * of the other shared libraries which might be loaded. Perform the
943 * same step for all of these.
952 * Now walk the dynamic section (of the executable, looking for a DT_DEBUG
955 for(; dynpnt
->d_tag
!= DT_NULL
; dynpnt
++)
957 if( dynpnt
->d_tag
== DT_DEBUG
)
963 if( (dynpnt
->d_tag
!= DT_DEBUG
)
964 || (dynpnt
->d_un
.d_ptr
== NULL
) )
970 * OK, now dig into the actual tables themselves.
972 dbg_hdr
= (struct r_debug
*) dynpnt
->d_un
.d_ptr
;
973 lpnt
= dbg_hdr
->r_map
;
976 * Now walk the linked list. In all known ELF implementations,
977 * the dynamic loader maintains this linked list for us. In some
978 * cases the first entry doesn't appear with a name, in other cases it
981 for(; lpnt
; lpnt
= lpnt
->l_next
)
984 * We already got the stuff for the executable using the
985 * argv[0] entry above. Here we only need to concentrate on any
986 * shared libraries which may be loaded.
988 ehdr
= (Elf32_Ehdr
*) lpnt
->l_addr
;
989 if( (lpnt
->l_addr
== NULL
) || (ehdr
->e_type
!= ET_DYN
) )
994 if( lpnt
->l_name
!= NULL
)
996 DEBUG_ProcessElfObject(lpnt
->l_name
, lpnt
->l_addr
);
1008 #else /* !__ELF__ */
1015 DEBUG_ReadExecutableDbgInfo(void)
1017 char * addr
= (char *) 0xffffffff;
1022 unsigned int staboff
;
1023 struct stat statbuf
;
1025 unsigned int stroff
;
1027 exe_name
= DEBUG_argv0
;
1030 * Make sure we can stat and open this file.
1032 if( exe_name
== NULL
)
1037 status
= stat(exe_name
, &statbuf
);
1044 * Now open the file, so that we can mmap() it.
1046 fd
= open(exe_name
, O_RDONLY
);
1054 * Now mmap() the file.
1056 addr
= mmap(0, statbuf
.st_size
, PROT_READ
,
1057 MAP_PRIVATE
, fd
, 0);
1059 ahdr
= (struct exec
*) addr
;
1061 staboff
= N_SYMOFF(*ahdr
);
1062 stroff
= N_STROFF(*ahdr
);
1063 rtn
= DEBUG_ParseStabs(addr
, 0,
1067 statbuf
.st_size
- stroff
);
1070 * Give a nice status message here...
1072 fprintf(stderr
, "Loading symbols from a.out file %s...\n", exe_name
);
1078 if( addr
!= (char *) 0xffffffff )
1080 munmap(addr
, statbuf
.st_size
);
1093 * Non-linux, non-ELF platforms.
1096 DEBUG_ReadExecutableDbgInfo(void)
1102 #endif /* __ELF__ */