Release 961222
[wine/multimedia.git] / debugger / stabs.c
blob2c238bef89ee58eb9c975fa9f82559b18b073438
1 /*
2 * File stabs.c - read stabs information from the wine executable itself.
4 * Copyright (C) 1996, Eric Youngdale.
5 */
7 #include <sys/mman.h>
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 #include <limits.h>
11 #include <stdio.h>
12 #include <strings.h>
13 #include <unistd.h>
15 #include "win.h"
16 #include "debugger.h"
18 #ifdef __ELF__
19 #include <elf.h>
20 #endif
22 #define N_UNDF 0x00
23 #define N_GSYM 0x20
24 #define N_FUN 0x24
25 #define N_STSYM 0x26
26 #define N_LCSYM 0x28
27 #define N_MAIN 0x2a
28 #define N_ROSYM 0x2c
29 #define N_OPT 0x3c
30 #define N_RSYM 0x40
31 #define N_SLINE 0x44
32 #define N_SO 0x64
33 #define N_LSYM 0x80
34 #define N_BINCL 0x82
35 #define N_SOL 0x84
36 #define N_PSYM 0xa0
37 #define N_EINCL 0xa2
38 #define N_LBRAC 0xc0
39 #define N_RBRAC 0xe0
43 * Set so that we know the main executable name and path.
45 char * DEBUG_argv0;
47 struct stab_nlist {
48 union {
49 char *n_name;
50 struct stab_nlist *n_next;
51 long n_strx;
52 } n_un;
53 unsigned char n_type;
54 char n_other;
55 short n_desc;
56 unsigned long n_value;
59 #ifdef __ELF__
61 int
62 DEBUG_ParseStabs(char * addr, Elf32_Shdr * stabsect, Elf32_Shdr * stabstr)
64 int i;
65 int ignore = FALSE;
66 int nstab;
67 struct stab_nlist * stab_ptr;
68 char * strs;
69 char * ptr;
70 char * xptr;
71 char currpath[PATH_MAX];
72 char symname[4096];
73 char * subpath = NULL;
74 DBG_ADDR new_addr;
75 struct name_hash * curr_func = NULL;
76 int strtabinc;
78 nstab = stabsect->sh_size / sizeof(struct stab_nlist);
79 stab_ptr = (struct stab_nlist *) (addr + stabsect->sh_offset);
80 strs = (char *) (addr + stabstr->sh_offset);
82 memset(currpath, 0, sizeof(currpath));
84 strtabinc = 0;
85 for(i=0; i < nstab; i++, stab_ptr++ )
87 ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
88 switch(stab_ptr->n_type)
90 case N_GSYM:
92 * These are useless. They have no value, and you have to
93 * read the normal symbol table to get the address. Thus we
94 * ignore them, and when we process the normal symbol table
95 * we should do the right thing.
97 case N_RBRAC:
98 case N_LBRAC:
100 * We need to keep track of these so we get symbol scoping
101 * right for local variables. For now, we just ignore them.
102 * The hooks are already there for dealing with this however,
103 * so all we need to do is to keep count of the nesting level,
104 * and find the RBRAC for each matching LBRAC.
106 break;
107 case N_LCSYM:
108 case N_STSYM:
110 * These are static symbols and BSS symbols.
112 new_addr.seg = 0;
113 new_addr.off = stab_ptr->n_value;
115 strcpy(symname, ptr);
116 xptr = strchr(symname, ':');
117 if( xptr != NULL )
119 *xptr = '\0';
121 DEBUG_AddSymbol( symname, &new_addr, currpath );
122 break;
123 case N_PSYM:
125 * These are function parameters.
127 if( (curr_func != NULL)
128 && (stab_ptr->n_value != 0) )
130 strcpy(symname, ptr);
131 xptr = strchr(symname, ':');
132 if( xptr != NULL )
134 *xptr = '\0';
136 DEBUG_AddLocal(curr_func, 0,
137 stab_ptr->n_value, 0, 0, symname);
139 break;
140 case N_RSYM:
141 if( curr_func != NULL )
143 strcpy(symname, ptr);
144 xptr = strchr(symname, ':');
145 if( xptr != NULL )
147 *xptr = '\0';
149 DEBUG_AddLocal(curr_func, stab_ptr->n_value, 0, 0, 0, symname);
151 break;
152 case N_LSYM:
153 if( (curr_func != NULL)
154 && (stab_ptr->n_value != 0) )
156 strcpy(symname, ptr);
157 xptr = strchr(symname, ':');
158 if( xptr != NULL )
160 *xptr = '\0';
162 DEBUG_AddLocal(curr_func, 0,
163 stab_ptr->n_value, 0, 0, symname);
165 break;
166 case N_SLINE:
168 * This is a line number. These are always relative to the start
169 * of the function (N_FUN), and this makes the lookup easier.
171 if( curr_func != NULL )
173 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
174 stab_ptr->n_value);
176 break;
177 case N_FUN:
179 * For now, just declare the various functions. Later
180 * on, we will add the line number information and the
181 * local symbols.
183 if( !ignore )
185 new_addr.seg = 0;
186 new_addr.off = stab_ptr->n_value;
188 * Copy the string to a temp buffer so we
189 * can kill everything after the ':'. We do
190 * it this way because otherwise we end up dirtying
191 * all of the pages related to the stabs, and that
192 * sucks up swap space like crazy.
194 strcpy(symname, ptr);
195 xptr = strchr(symname, ':');
196 if( xptr != NULL )
198 *xptr = '\0';
200 curr_func = DEBUG_AddSymbol( symname, &new_addr, currpath );
202 else
205 * Don't add line number information for this function
206 * any more.
208 curr_func = NULL;
210 break;
211 case N_SO:
213 * This indicates a new source file. Append the records
214 * together, to build the correct path name.
216 if( *ptr == '\0' )
219 * Nuke old path.
221 currpath[0] = '\0';
222 curr_func = NULL;
224 else
226 strcat(currpath, ptr);
227 subpath = ptr;
229 break;
230 case N_SOL:
232 * This indicates we are including stuff from an include file.
233 * If this is the main source, enable the debug stuff, otherwise
234 * ignore it.
236 if( subpath == NULL || strcmp(ptr, subpath) == 0 )
238 ignore = FALSE;
240 else
242 ignore = TRUE;
243 curr_func = NULL;
245 break;
246 case N_UNDF:
247 strs += strtabinc;
248 strtabinc = stab_ptr->n_value;
249 curr_func = NULL;
250 break;
251 case N_OPT:
253 * Ignore this. We don't care what it points to.
255 break;
256 case N_BINCL:
257 case N_EINCL:
258 case N_MAIN:
260 * Always ignore these. GCC doesn't even generate them.
262 break;
263 default:
264 break;
266 #if 0
267 fprintf(stderr, "%d %x %s\n", stab_ptr->n_type,
268 (unsigned int) stab_ptr->n_value,
269 strs + (unsigned int) stab_ptr->n_un.n_name);
270 #endif
272 return TRUE;
276 DEBUG_ReadExecutableDbgInfo(void)
278 int rtn = FALSE;
279 char * exe_name;
280 struct stat statbuf;
281 int fd = -1;
282 int status;
283 char * addr = (char *) 0xffffffff;
284 Elf32_Ehdr * ehptr;
285 Elf32_Shdr * spnt;
286 char * shstrtab;
287 int nsect;
288 int i;
289 int stabsect;
290 int stabstrsect;
292 exe_name = DEBUG_argv0;
295 * Make sure we can stat and open this file.
297 if( exe_name == NULL )
299 goto leave;
302 status = stat(exe_name, &statbuf);
303 if( status == -1 )
305 goto leave;
309 * Now open the file, so that we can mmap() it.
311 fd = open(exe_name, O_RDONLY);
312 if( fd == -1 )
314 goto leave;
319 * Now mmap() the file.
321 addr = mmap(0, statbuf.st_size, PROT_READ,
322 MAP_PRIVATE, fd, 0);
325 * Next, we need to find a few of the internal ELF headers within
326 * this thing. We need the main executable header, and the section
327 * table.
329 ehptr = (Elf32_Ehdr *) addr;
330 spnt = (Elf32_Shdr *) (addr + ehptr->e_shoff);
331 nsect = ehptr->e_shnum;
332 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
334 stabsect = stabstrsect = -1;
336 for(i=0; i < nsect; i++)
338 if( strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0 )
340 stabsect = i;
343 if( strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0 )
345 stabstrsect = i;
349 if( stabsect == -1 || stabstrsect == -1 )
351 goto leave;
355 * OK, now just parse all of the stabs.
357 rtn = DEBUG_ParseStabs(addr, spnt + stabsect, spnt + stabstrsect);
359 leave:
361 if( addr != (char *) 0xffffffff )
363 munmap(addr, statbuf.st_size);
366 if( fd != -1 )
368 close(fd);
371 return (rtn);
375 #endif /* __ELF__ */