Fixed WM_GETTEXTLENGTH handling.
[wine/multimedia.git] / debugger / stabs.c
blobb862bf9c81b0041440f061e2ee69ce9c365f82a6
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * File stabs.c - read stabs information from the wine executable itself.
6 * Copyright (C) 1996, Eric Youngdale.
7 * 1999, 2000 Eric Pouech
8 */
10 #include "config.h"
12 #include <sys/types.h>
13 #include <fcntl.h>
14 #include <sys/stat.h>
15 #ifdef HAVE_SYS_MMAN_H
16 #include <sys/mman.h>
17 #endif
18 #include <limits.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #ifndef PATH_MAX
23 #define PATH_MAX _MAX_PATH
24 #endif
26 #include "debugger.h"
28 #if defined(__svr4__) || defined(__sun)
29 #define __ELF__
30 #endif
32 #ifdef __ELF__
33 #ifdef HAVE_ELF_H
34 # include <elf.h>
35 #endif
36 #ifdef HAVE_LINK_H
37 # include <link.h>
38 #endif
39 #endif
41 #ifndef N_UNDF
42 #define N_UNDF 0x00
43 #endif
45 #ifndef STN_UNDEF
46 # define STN_UNDEF 0
47 #endif
49 #define N_GSYM 0x20
50 #define N_FUN 0x24
51 #define N_STSYM 0x26
52 #define N_LCSYM 0x28
53 #define N_MAIN 0x2a
54 #define N_ROSYM 0x2c
55 #define N_OPT 0x3c
56 #define N_RSYM 0x40
57 #define N_SLINE 0x44
58 #define N_SO 0x64
59 #define N_LSYM 0x80
60 #define N_BINCL 0x82
61 #define N_SOL 0x84
62 #define N_PSYM 0xa0
63 #define N_EINCL 0xa2
64 #define N_LBRAC 0xc0
65 #define N_EXCL 0xc2
66 #define N_RBRAC 0xe0
68 typedef struct tagELF_DBG_INFO {
69 unsigned long elf_addr;
70 } ELF_DBG_INFO;
72 struct stab_nlist {
73 union {
74 char *n_name;
75 struct stab_nlist *n_next;
76 long n_strx;
77 } n_un;
78 unsigned char n_type;
79 char n_other;
80 short n_desc;
81 unsigned long n_value;
84 static void stab_strcpy(char * dest, int sz, const char * source)
87 * A strcpy routine that stops when we hit the ':' character.
88 * Faster than copying the whole thing, and then nuking the
89 * ':'.
91 while(*source != '\0' && *source != ':' && sz-- > 0)
92 *dest++ = *source++;
93 *dest = '\0';
94 assert(sz > 0);
97 typedef struct {
98 char* name;
99 unsigned long value;
100 int idx;
101 struct datatype** vector;
102 int nrofentries;
103 } include_def;
105 #define MAX_INCLUDES 512
107 static include_def* include_defs = NULL;
108 static int num_include_def = 0;
109 static int num_alloc_include_def = 0;
110 static int cu_include_stack[MAX_INCLUDES];
111 static int cu_include_stk_idx = 0;
112 static struct datatype** cu_vector = NULL;
113 static int cu_nrofentries = 0;
115 static
116 int
117 DEBUG_CreateInclude(const char* file, unsigned long val)
119 if (num_include_def == num_alloc_include_def)
121 num_alloc_include_def += 256;
122 include_defs = DBG_realloc(include_defs, sizeof(include_defs[0])*num_alloc_include_def);
123 memset(include_defs+num_include_def, 0, sizeof(include_defs[0])*256);
125 include_defs[num_include_def].name = DBG_strdup(file);
126 include_defs[num_include_def].value = val;
127 include_defs[num_include_def].vector = NULL;
128 include_defs[num_include_def].nrofentries = 0;
130 return num_include_def++;
133 static
134 int
135 DEBUG_FindInclude(const char* file, unsigned long val)
137 int i;
139 for (i = 0; i < num_include_def; i++)
141 if (val == include_defs[i].value &&
142 strcmp(file, include_defs[i].name) == 0)
143 return i;
145 return -1;
148 static
150 DEBUG_AddInclude(int idx)
152 ++cu_include_stk_idx;
154 /* is this happen, just bump MAX_INCLUDES */
155 /* we could also handle this as another dynarray */
156 assert(cu_include_stk_idx < MAX_INCLUDES);
158 cu_include_stack[cu_include_stk_idx] = idx;
159 return cu_include_stk_idx;
162 static
163 void
164 DEBUG_ResetIncludes(void)
167 * The datatypes that we would need to use are reset when
168 * we start a new file. (at least the ones in filenr == 0
170 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
171 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
174 static
175 void
176 DEBUG_FreeIncludes(void)
178 int i;
180 DEBUG_ResetIncludes();
182 for (i = 0; i < num_include_def; i++)
184 DBG_free(include_defs[i].name);
185 DBG_free(include_defs[i].vector);
187 DBG_free(include_defs);
188 include_defs = NULL;
189 num_include_def = 0;
190 num_alloc_include_def = 0;
191 DBG_free(cu_vector);
192 cu_vector = NULL;
193 cu_nrofentries = 0;
196 static
197 struct datatype**
198 DEBUG_FileSubNr2StabEnum(int filenr, int subnr)
200 struct datatype** ret;
202 /* DEBUG_Printf(DBG_CHN_MESG, "creating type id for (%d,%d)\n", filenr, subnr); */
204 /* FIXME: I could perhaps create a dummy include_def for each compilation
205 * unit which would allow not to handle those two cases separately
207 if (filenr == 0)
209 if (cu_nrofentries <= subnr)
211 cu_vector = DBG_realloc(cu_vector, sizeof(cu_vector[0])*(subnr+1));
212 memset(cu_vector+cu_nrofentries, 0, sizeof(cu_vector[0])*(subnr+1-cu_nrofentries));
213 cu_nrofentries = subnr + 1;
215 ret = &cu_vector[subnr];
217 else
219 include_def* idef;
221 assert(filenr <= cu_include_stk_idx);
223 idef = &include_defs[cu_include_stack[filenr]];
225 if (idef->nrofentries <= subnr)
227 idef->vector = DBG_realloc(idef->vector, sizeof(idef->vector[0])*(subnr+1));
228 memset(idef->vector + idef->nrofentries, 0, sizeof(idef->vector[0])*(subnr+1-idef->nrofentries));
229 idef->nrofentries = subnr + 1;
231 ret = &idef->vector[subnr];
233 /* DEBUG_Printf(DBG_CHN_MESG,"(%d,%d) is %d\n",filenr,subnr,ret); */
234 return ret;
237 static
238 struct datatype**
239 DEBUG_ReadTypeEnum(char **x) {
240 int filenr,subnr;
242 if (**x=='(') {
243 (*x)++; /* '(' */
244 filenr=strtol(*x,x,10); /* <int> */
245 (*x)++; /* ',' */
246 subnr=strtol(*x,x,10); /* <int> */
247 (*x)++; /* ')' */
248 } else {
249 filenr = 0;
250 subnr = strtol(*x,x,10); /* <int> */
252 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
255 struct ParseTypedefData {
256 char* ptr;
257 char buf[1024];
258 int idx;
261 static int DEBUG_PTS_ReadTypedef(struct ParseTypedefData* ptd, const char* typename,
262 struct datatype** dt);
264 static int DEBUG_PTS_ReadID(struct ParseTypedefData* ptd)
266 char* first = ptd->ptr;
267 unsigned int len;
269 if ((ptd->ptr = strchr(ptd->ptr, ':')) == NULL) return -1;
270 len = ptd->ptr - first;
271 if (len >= sizeof(ptd->buf) - ptd->idx) return -1;
272 memcpy(ptd->buf + ptd->idx, first, len);
273 ptd->buf[ptd->idx + len] = '\0';
274 ptd->idx += len + 1;
275 ptd->ptr++; /* ':' */
276 return 0;
279 static int DEBUG_PTS_ReadNum(struct ParseTypedefData* ptd, int* v)
281 char* last;
283 *v = strtol(ptd->ptr, &last, 10);
284 if (last == ptd->ptr) return -1;
285 ptd->ptr = last;
286 return 0;
289 static int DEBUG_PTS_ReadTypeReference(struct ParseTypedefData* ptd,
290 int* filenr, int* subnr)
292 if (*ptd->ptr == '(') {
293 /* '(' <int> ',' <int> ')' */
294 ptd->ptr++;
295 if (DEBUG_PTS_ReadNum(ptd, filenr) == -1) return -1;
296 if (*ptd->ptr++ != ',') return -1;
297 if (DEBUG_PTS_ReadNum(ptd, subnr) == -1) return -1;
298 if (*ptd->ptr++ != ')') return -1;
299 } else {
300 *filenr = 0;
301 if (DEBUG_PTS_ReadNum(ptd, subnr) == -1) return -1;
303 return 0;
306 static int DEBUG_PTS_ReadRange(struct ParseTypedefData* ptd, struct datatype** dt,
307 int* lo, int* hi)
309 /* type ';' <int> ';' <int> ';' */
310 if (DEBUG_PTS_ReadTypedef(ptd, NULL, dt) == -1) return -1;
311 if (*ptd->ptr++ != ';') return -1; /* ';' */
312 if (DEBUG_PTS_ReadNum(ptd, lo) == -1) return -1;
313 if (*ptd->ptr++ != ';') return -1; /* ';' */
314 if (DEBUG_PTS_ReadNum(ptd, hi) == -1) return -1;
315 if (*ptd->ptr++ != ';') return -1; /* ';' */
316 return 0;
319 static inline int DEBUG_PTS_ReadAggregate(struct ParseTypedefData* ptd, struct datatype* sdt)
321 int sz, ofs;
322 char* last;
323 struct datatype* adt;
324 int idx;
325 int doadd;
327 sz = strtol(ptd->ptr, &last, 10);
328 if (last == ptd->ptr) return -1;
329 ptd->ptr = last;
331 doadd = DEBUG_SetStructSize(sdt, sz);
332 /* if the structure has already been filled, just redo the parsing
333 * but don't store results into the struct
334 * FIXME: there's a quite ugly memory leak in there...
337 /* Now parse the individual elements of the structure/union. */
338 while (*ptd->ptr != ';') {
339 /* agg_name : type ',' <int:offset> ',' <int:size> */
340 idx = ptd->idx;
341 if (DEBUG_PTS_ReadID(ptd) == -1) return -1;
343 if (DEBUG_PTS_ReadTypedef(ptd, NULL, &adt) == -1) return -1;
344 if (!adt) return -1;
346 if (*ptd->ptr++ != ',') return -1;
347 if (DEBUG_PTS_ReadNum(ptd, &ofs) == -1) return -1;
348 if (*ptd->ptr++ != ',') return -1;
349 if (DEBUG_PTS_ReadNum(ptd, &sz) == -1) return -1;
350 if (*ptd->ptr++ != ';') return -1;
352 if (doadd) DEBUG_AddStructElement(sdt, ptd->buf + idx, adt, ofs, sz);
353 ptd->idx = idx;
355 ptd->ptr++; /* ';' */
356 return 0;
359 static inline int DEBUG_PTS_ReadEnum(struct ParseTypedefData* ptd, struct datatype* edt)
361 int ofs;
362 int idx;
364 while (*ptd->ptr != ';') {
365 idx = ptd->idx;
366 if (DEBUG_PTS_ReadID(ptd) == -1) return -1;
367 if (DEBUG_PTS_ReadNum(ptd, &ofs) == -1) return -1;
368 if (*ptd->ptr++ != ',') return -1;
369 DEBUG_AddStructElement(edt, ptd->buf + idx, NULL, ofs, 0);
370 ptd->idx = idx;
372 ptd->ptr++;
373 return 0;
376 static inline int DEBUG_PTS_ReadArray(struct ParseTypedefData* ptd, struct datatype* adt)
378 int lo, hi;
379 struct datatype* rdt;
381 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
383 if (*ptd->ptr++ != 'r') return -1;
384 /* FIXME: range type is lost, always assume int */
385 if (DEBUG_PTS_ReadRange(ptd, &rdt, &lo, &hi) == -1) return -1;
386 if (DEBUG_PTS_ReadTypedef(ptd, NULL, &rdt) == -1) return -1;
388 DEBUG_SetArrayParams(adt, lo, hi, rdt);
389 return 0;
392 static int DEBUG_PTS_ReadTypedef(struct ParseTypedefData* ptd, const char* typename,
393 struct datatype** ret_dt)
395 int idx, lo, hi;
396 struct datatype* new_dt = NULL; /* newly created data type */
397 struct datatype* ref_dt; /* referenced data type (pointer...) */
398 struct datatype* dt1; /* intermediate data type (scope is limited) */
399 struct datatype* dt2; /* intermediate data type: t1=t2=new_dt */
400 int filenr1, subnr1;
401 int filenr2 = 0, subnr2 = 0;
403 /* things are a bit complicated because of the way the typedefs are stored inside
404 * the file (we cannot keep the struct datatype** around, because address can
405 * change when realloc is done, so we must call over and over
406 * DEBUG_FileSubNr2StabEnum to keep the correct values around
407 * (however, keeping struct datatype* is valid
409 if (DEBUG_PTS_ReadTypeReference(ptd, &filenr1, &subnr1) == -1) return -1;
411 while (*ptd->ptr == '=') {
412 ptd->ptr++;
413 if (new_dt) {
414 DEBUG_Printf(DBG_CHN_MESG, "Bad recursion (1) in typedef\n");
415 return -1;
417 /* first handle attribute if any */
418 switch (*ptd->ptr) {
419 case '@':
420 if (*++ptd->ptr == 's') {
421 ptd->ptr++;
422 if (DEBUG_PTS_ReadNum(ptd, &lo) == -1) {
423 DEBUG_Printf(DBG_CHN_MESG, "Not an attribute... NIY\n");
424 ptd->ptr -= 2;
425 return -1;
427 if (*ptd->ptr++ != ';') return -1;
429 break;
431 /* then the real definitions */
432 switch (*ptd->ptr++) {
433 case '*':
434 new_dt = DEBUG_NewDataType(DT_POINTER, NULL);
435 if (DEBUG_PTS_ReadTypedef(ptd, NULL, &ref_dt) == -1) return -1;
436 DEBUG_SetPointerType(new_dt, ref_dt);
437 break;
438 case '(':
439 ptd->ptr--;
440 /* doit a two level by hand, otherwise we'd need a stack */
441 if (filenr2 || subnr2) {
442 DEBUG_Printf(DBG_CHN_MESG, "Bad recursion (2) in typedef\n");
443 return -1;
445 if (DEBUG_PTS_ReadTypeReference(ptd, &filenr2, &subnr2) == -1) return -1;
447 dt1 = *DEBUG_FileSubNr2StabEnum(filenr1, subnr1);
448 dt2 = *DEBUG_FileSubNr2StabEnum(filenr2, subnr2);
450 if (!dt1 && dt2) {
451 new_dt = dt2;
452 filenr2 = subnr2 = 0;
453 } else if (!dt1 && !dt2) {
454 new_dt = NULL;
455 } else {
456 DEBUG_Printf(DBG_CHN_MESG, "Unknown condition %08lx %08lx (%s)\n",
457 (unsigned long)dt1, (unsigned long)dt2, ptd->ptr);
458 return -1;
460 break;
461 case 'a':
462 new_dt = DEBUG_NewDataType(DT_ARRAY, NULL);
463 if (DEBUG_PTS_ReadArray(ptd, new_dt) == -1) return -1;
464 break;
465 case 'r':
466 new_dt = DEBUG_NewDataType(DT_BASIC, typename);
467 assert(!*DEBUG_FileSubNr2StabEnum(filenr1, subnr1));
468 *DEBUG_FileSubNr2StabEnum(filenr1, subnr1) = new_dt;
469 if (DEBUG_PTS_ReadRange(ptd, &ref_dt, &lo, &hi) == -1) return -1;
470 /* should perhaps do more here... */
471 break;
472 case 'f':
473 new_dt = DEBUG_NewDataType(DT_FUNC, NULL);
474 if (DEBUG_PTS_ReadTypedef(ptd, NULL, &ref_dt) == -1) return -1;
475 DEBUG_SetPointerType(new_dt, ref_dt);
476 break;
477 case 'e':
478 new_dt = DEBUG_NewDataType(DT_ENUM, NULL);
479 if (DEBUG_PTS_ReadEnum(ptd, new_dt) == -1) return -1;
480 break;
481 case 's':
482 case 'u':
483 /* dt1 can have been already defined in a forward definition */
484 dt1 = *DEBUG_FileSubNr2StabEnum(filenr1, subnr1);
485 dt2 = DEBUG_TypeCast(DT_STRUCT, typename);
486 if (!dt1) {
487 new_dt = DEBUG_NewDataType(DT_STRUCT, typename);
488 /* we need to set it here, because a struct can hold a pointer
489 * to itself
491 *DEBUG_FileSubNr2StabEnum(filenr1, subnr1) = new_dt;
492 } else {
493 if (DEBUG_GetType(dt1) != DT_STRUCT) {
494 DEBUG_Printf(DBG_CHN_MESG,
495 "Forward declaration is not an aggregate\n");
496 return -1;
499 /* should check typename is the same too */
500 new_dt = dt1;
502 if (DEBUG_PTS_ReadAggregate(ptd, new_dt) == -1) return -1;
503 break;
504 case 'x':
505 switch (*ptd->ptr++) {
506 case 'e': lo = DT_ENUM; break;
507 case 's': case 'u': lo = DT_STRUCT; break;
508 default: return -1;
511 idx = ptd->idx;
512 if (DEBUG_PTS_ReadID(ptd) == -1) return -1;
513 new_dt = DEBUG_NewDataType(lo, ptd->buf + idx);
514 ptd->idx = idx;
515 break;
516 default:
517 DEBUG_Printf(DBG_CHN_MESG, "Unknown type '%c'\n", *ptd->ptr);
518 return -1;
522 if ((filenr2 || subnr2) && !*DEBUG_FileSubNr2StabEnum(filenr2, subnr2)) {
523 if (!new_dt) {
524 /* this should be a basic type, define it, or even void */
525 new_dt = DEBUG_NewDataType(DT_BASIC, typename);
527 *DEBUG_FileSubNr2StabEnum(filenr2, subnr2) = new_dt;
530 if (!new_dt) {
531 dt1 = *DEBUG_FileSubNr2StabEnum(filenr1, subnr1);
532 if (!dt1) {
533 DEBUG_Printf(DBG_CHN_MESG, "Nothing has been defined <%s>\n", ptd->ptr);
534 return -1;
536 *ret_dt = dt1;
537 return 0;
540 *DEBUG_FileSubNr2StabEnum(filenr1, subnr1) = *ret_dt = new_dt;
542 #if 0
543 if (typename) {
544 DEBUG_Printf(DBG_CHN_MESG, "Adding (%d,%d) %s => ", filenr1, subnr1, typename);
545 DEBUG_PrintTypeCast(new_dt);
546 DEBUG_Printf(DBG_CHN_MESG, "\n");
548 #endif
550 return 0;
553 static int DEBUG_ParseTypedefStab(char* ptr, const char* typename)
555 struct ParseTypedefData ptd;
556 struct datatype* dt;
557 int ret = -1;
559 /* check for already existing definition */
561 ptd.idx = 0;
562 if ((ptd.ptr = strchr(ptr, ':'))) {
563 ptd.ptr++;
564 if (*ptd.ptr != '(') ptd.ptr++;
565 ret = DEBUG_PTS_ReadTypedef(&ptd, typename, &dt);
568 if (ret == -1 || *ptd.ptr) {
569 DEBUG_Printf(DBG_CHN_MESG, "failure on %s at %s\n", ptr, ptd.ptr);
570 return FALSE;
573 return TRUE;
576 static struct datatype *
577 DEBUG_ParseStabType(const char * stab)
579 char * c;
582 * Look through the stab definition, and figure out what datatype
583 * this represents. If we have something we know about, assign the
584 * type.
586 c = strchr(stab, ':');
587 if( c == NULL )
588 return NULL;
590 c++;
592 * The next character says more about the type (i.e. data, function, etc)
593 * of symbol. Skip it.
595 if (*c != '(')
596 c++;
598 * The next is either an integer or a (integer,integer).
599 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
601 return *DEBUG_ReadTypeEnum(&c);
604 enum DbgInfoLoad DEBUG_ParseStabs(char * addr, unsigned int load_offset,
605 unsigned int staboff, int stablen,
606 unsigned int strtaboff, int strtablen)
608 struct name_hash * curr_func = NULL;
609 struct wine_locals * curr_loc = NULL;
610 struct name_hash * curr_sym = NULL;
611 char currpath[PATH_MAX];
612 int i;
613 int in_external_file = FALSE;
614 int last_nso = -1;
615 unsigned int len;
616 DBG_VALUE new_value;
617 int nstab;
618 char * ptr;
619 char * stabbuff;
620 unsigned int stabbufflen;
621 struct stab_nlist * stab_ptr;
622 char * strs;
623 int strtabinc;
624 char * subpath = NULL;
625 char symname[4096];
627 nstab = stablen / sizeof(struct stab_nlist);
628 stab_ptr = (struct stab_nlist *) (addr + staboff);
629 strs = (char *) (addr + strtaboff);
631 memset(currpath, 0, sizeof(currpath));
634 * Allocate a buffer into which we can build stab strings for cases
635 * where the stab is continued over multiple lines.
637 stabbufflen = 65536;
638 stabbuff = (char *) DBG_alloc(stabbufflen);
640 strtabinc = 0;
641 stabbuff[0] = '\0';
642 for(i=0; i < nstab; i++, stab_ptr++ )
644 ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
645 if( ptr[strlen(ptr) - 1] == '\\' )
648 * Indicates continuation. Append this to the buffer, and go onto the
649 * next record. Repeat the process until we find a stab without the
650 * '/' character, as this indicates we have the whole thing.
652 len = strlen(ptr);
653 if( strlen(stabbuff) + len > stabbufflen )
655 stabbufflen += 65536;
656 stabbuff = (char *) DBG_realloc(stabbuff, stabbufflen);
658 strncat(stabbuff, ptr, len - 1);
659 continue;
661 else if( stabbuff[0] != '\0' )
663 strcat( stabbuff, ptr);
664 ptr = stabbuff;
667 if( strchr(ptr, '=') != NULL )
670 * The stabs aren't in writable memory, so copy it over so we are
671 * sure we can scribble on it.
673 if( ptr != stabbuff )
675 strcpy(stabbuff, ptr);
676 ptr = stabbuff;
678 stab_strcpy(symname, sizeof(symname), ptr);
679 if (!DEBUG_ParseTypedefStab(ptr, symname)) {
680 /* skip this definition */
681 stabbuff[0] = '\0';
682 continue;
686 switch(stab_ptr->n_type)
688 case N_GSYM:
690 * These are useless with ELF. They have no value, and you have to
691 * read the normal symbol table to get the address. Thus we
692 * ignore them, and when we process the normal symbol table
693 * we should do the right thing.
695 * With a.out or mingw, they actually do make some amount of sense.
697 new_value.addr.seg = 0;
698 new_value.type = DEBUG_ParseStabType(ptr);
699 new_value.addr.off = load_offset + stab_ptr->n_value;
700 new_value.cookie = DV_TARGET;
702 stab_strcpy(symname, sizeof(symname), ptr);
703 #ifdef __ELF__
704 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
705 SYM_WINE | SYM_DATA | SYM_INVALID );
706 #else
707 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
708 SYM_WINE | SYM_DATA );
709 #endif
710 break;
711 case N_RBRAC:
712 case N_LBRAC:
714 * We need to keep track of these so we get symbol scoping
715 * right for local variables. For now, we just ignore them.
716 * The hooks are already there for dealing with this however,
717 * so all we need to do is to keep count of the nesting level,
718 * and find the RBRAC for each matching LBRAC.
720 break;
721 case N_LCSYM:
722 case N_STSYM:
724 * These are static symbols and BSS symbols.
726 new_value.addr.seg = 0;
727 new_value.type = DEBUG_ParseStabType(ptr);
728 new_value.addr.off = load_offset + stab_ptr->n_value;
729 new_value.cookie = DV_TARGET;
731 stab_strcpy(symname, sizeof(symname), ptr);
732 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
733 SYM_WINE | SYM_DATA );
734 break;
735 case N_PSYM:
737 * These are function parameters.
739 if( curr_func != NULL && !in_external_file )
741 stab_strcpy(symname, sizeof(symname), ptr);
742 curr_loc = DEBUG_AddLocal( curr_func, 0,
743 stab_ptr->n_value, 0, 0, symname );
744 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
746 break;
747 case N_RSYM:
748 if( curr_func != NULL && !in_external_file )
750 stab_strcpy(symname, sizeof(symname), ptr);
751 curr_loc = DEBUG_AddLocal( curr_func, stab_ptr->n_value + 1,
752 0, 0, 0, symname );
753 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
755 break;
756 case N_LSYM:
757 if( curr_func != NULL && !in_external_file )
759 stab_strcpy(symname, sizeof(symname), ptr);
760 curr_loc = DEBUG_AddLocal( curr_func, 0,
761 stab_ptr->n_value, 0, 0, symname );
762 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
764 break;
765 case N_SLINE:
767 * This is a line number. These are always relative to the start
768 * of the function (N_FUN), and this makes the lookup easier.
770 if( curr_func != NULL && !in_external_file )
772 #ifdef __ELF__
773 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
774 stab_ptr->n_value);
775 #else
776 #if 0
778 * This isn't right. The order of the stabs is different under
779 * a.out, and as a result we would end up attaching the line
780 * number to the wrong function.
782 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
783 stab_ptr->n_value - curr_func->addr.off);
784 #endif
785 #endif
787 break;
788 case N_FUN:
790 * First, clean up the previous function we were working on.
792 DEBUG_Normalize(curr_func);
795 * For now, just declare the various functions. Later
796 * on, we will add the line number information and the
797 * local symbols.
799 if( !in_external_file)
801 stab_strcpy(symname, sizeof(symname), ptr);
802 if (*symname)
804 new_value.addr.seg = 0;
805 new_value.type = DEBUG_ParseStabType(ptr);
806 new_value.addr.off = load_offset + stab_ptr->n_value;
807 new_value.cookie = DV_TARGET;
809 * Copy the string to a temp buffer so we
810 * can kill everything after the ':'. We do
811 * it this way because otherwise we end up dirtying
812 * all of the pages related to the stabs, and that
813 * sucks up swap space like crazy.
815 #ifdef __ELF__
816 curr_func = DEBUG_AddSymbol( symname, &new_value, currpath,
817 SYM_WINE | SYM_FUNC | SYM_INVALID );
818 #else
819 curr_func = DEBUG_AddSymbol( symname, &new_value, currpath,
820 SYM_WINE | SYM_FUNC );
821 #endif
823 else
825 /* some GCC seem to use a N_FUN "" to mark the end of a function */
826 curr_func = NULL;
829 else
832 * Don't add line number information for this function
833 * any more.
835 curr_func = NULL;
837 break;
838 case N_SO:
840 * This indicates a new source file. Append the records
841 * together, to build the correct path name.
843 #ifndef __ELF__
845 * With a.out, there is no NULL string N_SO entry at the end of
846 * the file. Thus when we find non-consecutive entries,
847 * we consider that a new file is started.
849 if( last_nso < i-1 )
851 currpath[0] = '\0';
852 DEBUG_Normalize(curr_func);
853 curr_func = NULL;
855 #endif
857 if( *ptr == '\0' )
860 * Nuke old path.
862 currpath[0] = '\0';
863 DEBUG_Normalize(curr_func);
864 curr_func = NULL;
866 else
868 if (*ptr != '/')
869 strcat(currpath, ptr);
870 else
871 strcpy(currpath, ptr);
872 subpath = ptr;
873 DEBUG_ResetIncludes();
875 last_nso = i;
876 break;
877 case N_SOL:
879 * This indicates we are including stuff from an include file.
880 * If this is the main source, enable the debug stuff, otherwise
881 * ignore it.
883 in_external_file = !(subpath == NULL || strcmp(ptr, subpath) == 0);
884 break;
885 case N_UNDF:
886 strs += strtabinc;
887 strtabinc = stab_ptr->n_value;
888 DEBUG_Normalize(curr_func);
889 curr_func = NULL;
890 break;
891 case N_OPT:
893 * Ignore this. We don't care what it points to.
895 break;
896 case N_BINCL:
897 DEBUG_AddInclude(DEBUG_CreateInclude(ptr, stab_ptr->n_value));
898 break;
899 case N_EINCL:
900 break;
901 case N_EXCL:
902 DEBUG_AddInclude(DEBUG_FindInclude(ptr, stab_ptr->n_value));
903 break;
904 case N_MAIN:
906 * Always ignore these. GCC doesn't even generate them.
908 break;
909 default:
910 DEBUG_Printf(DBG_CHN_MESG, "Unknown stab type 0x%02x\n", stab_ptr->n_type);
911 break;
914 stabbuff[0] = '\0';
916 #if 0
917 DEBUG_Printf(DBG_CHN_MESG, "%d %x %s\n", stab_ptr->n_type,
918 (unsigned int) stab_ptr->n_value,
919 strs + (unsigned int) stab_ptr->n_un.n_name);
920 #endif
923 DEBUG_FreeIncludes();
925 return DIL_LOADED;
928 #ifdef __ELF__
931 * Walk through the entire symbol table and add any symbols we find there.
932 * This can be used in cases where we have stripped ELF shared libraries,
933 * or it can be used in cases where we have data symbols for which the address
934 * isn't encoded in the stabs.
936 * This is all really quite easy, since we don't have to worry about line
937 * numbers or local data variables.
939 static int DEBUG_ProcessElfSymtab(DBG_MODULE* module, char* addr,
940 u_long load_addr, Elf32_Shdr* symtab,
941 Elf32_Shdr* strtab)
943 char * curfile = NULL;
944 struct name_hash * curr_sym = NULL;
945 int flags;
946 int i;
947 DBG_VALUE new_value;
948 int nsym;
949 char * strp;
950 char * symname;
951 Elf32_Sym * symp;
953 symp = (Elf32_Sym *) (addr + symtab->sh_offset);
954 nsym = symtab->sh_size / sizeof(*symp);
955 strp = (char *) (addr + strtab->sh_offset);
957 for(i=0; i < nsym; i++, symp++)
960 * Ignore certain types of entries which really aren't of that much
961 * interest.
963 if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION ||
964 symp->st_shndx == STN_UNDEF )
966 continue;
969 symname = strp + symp->st_name;
972 * Save the name of the current file, so we have a way of tracking
973 * static functions/data.
975 if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
977 curfile = symname;
978 continue;
982 * See if we already have something for this symbol.
983 * If so, ignore this entry, because it would have come from the
984 * stabs or from a previous symbol. If the value is different,
985 * we will have to keep the darned thing, because there can be
986 * multiple local symbols by the same name.
988 if( (DEBUG_GetSymbolValue(symname, -1, &new_value, FALSE ) == TRUE)
989 && (new_value.addr.off == (load_addr + symp->st_value)) )
990 continue;
992 new_value.addr.seg = 0;
993 new_value.type = NULL;
994 new_value.addr.off = load_addr + symp->st_value;
995 new_value.cookie = DV_TARGET;
996 flags = SYM_WINE | ((ELF32_ST_TYPE(symp->st_info) == STT_FUNC)
997 ? SYM_FUNC : SYM_DATA);
998 if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
999 curr_sym = DEBUG_AddSymbol( symname, &new_value, NULL, flags );
1000 else
1001 curr_sym = DEBUG_AddSymbol( symname, &new_value, curfile, flags );
1004 * Record the size of the symbol. This can come in handy in
1005 * some cases. Not really used yet, however.
1007 if( symp->st_size != 0 )
1008 DEBUG_SetSymbolSize(curr_sym, symp->st_size);
1011 return TRUE;
1015 * Loads the symbolic information from ELF module stored in 'filename'
1016 * the module has been loaded at 'load_offset' address, so symbols' address
1017 * relocation is performed
1018 * returns
1019 * -1 if the file cannot be found/opened
1020 * 0 if the file doesn't contain symbolic info (or this info cannot be
1021 * read or parsed)
1022 * 1 on success
1024 enum DbgInfoLoad DEBUG_LoadElfStabs(DBG_MODULE* module)
1026 enum DbgInfoLoad dil = DIL_ERROR;
1027 char* addr = (char*)0xffffffff;
1028 int fd = -1;
1029 struct stat statbuf;
1030 Elf32_Ehdr* ehptr;
1031 Elf32_Shdr* spnt;
1032 char* shstrtab;
1033 int i;
1034 int stabsect;
1035 int stabstrsect;
1037 if (module->type != DMT_ELF || ! module->elf_info) {
1038 DEBUG_Printf(DBG_CHN_ERR, "Bad elf module '%s'\n", module->module_name);
1039 return DIL_ERROR;
1042 /* check that the file exists, and that the module hasn't been loaded yet */
1043 if (stat(module->module_name, &statbuf) == -1) goto leave;
1044 if (S_ISDIR(statbuf.st_mode)) goto leave;
1047 * Now open the file, so that we can mmap() it.
1049 if ((fd = open(module->module_name, O_RDONLY)) == -1) goto leave;
1051 dil = DIL_NOINFO;
1053 * Now mmap() the file.
1055 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1056 if (addr == (char*)0xffffffff) goto leave;
1059 * Next, we need to find a few of the internal ELF headers within
1060 * this thing. We need the main executable header, and the section
1061 * table.
1063 ehptr = (Elf32_Ehdr*) addr;
1064 spnt = (Elf32_Shdr*) (addr + ehptr->e_shoff);
1065 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1067 stabsect = stabstrsect = -1;
1069 for (i = 0; i < ehptr->e_shnum; i++) {
1070 if (strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0)
1071 stabsect = i;
1073 if (strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0)
1074 stabstrsect = i;
1077 if (stabsect == -1 || stabstrsect == -1) {
1078 DEBUG_Printf(DBG_CHN_WARN, "no .stab section\n");
1079 goto leave;
1083 * OK, now just parse all of the stabs.
1085 if (DEBUG_ParseStabs(addr,
1086 module->elf_info->elf_addr,
1087 spnt[stabsect].sh_offset,
1088 spnt[stabsect].sh_size,
1089 spnt[stabstrsect].sh_offset,
1090 spnt[stabstrsect].sh_size)) {
1091 dil = DIL_LOADED;
1092 } else {
1093 dil = DIL_ERROR;
1094 DEBUG_Printf(DBG_CHN_WARN, "bad stabs\n");
1095 goto leave;
1098 for (i = 0; i < ehptr->e_shnum; i++) {
1099 if ( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1100 && (spnt[i].sh_type == SHT_SYMTAB))
1101 DEBUG_ProcessElfSymtab(module, addr, module->elf_info->elf_addr,
1102 spnt + i, spnt + spnt[i].sh_link);
1104 if ( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1105 && (spnt[i].sh_type == SHT_DYNSYM))
1106 DEBUG_ProcessElfSymtab(module, addr, module->elf_info->elf_addr,
1107 spnt + i, spnt + spnt[i].sh_link);
1110 leave:
1111 if (addr != (char*)0xffffffff) munmap(addr, statbuf.st_size);
1112 if (fd != -1) close(fd);
1114 return dil;
1118 * Loads the information for ELF module stored in 'filename'
1119 * the module has been loaded at 'load_offset' address
1120 * returns
1121 * -1 if the file cannot be found/opened
1122 * 0 if the file doesn't contain symbolic info (or this info cannot be
1123 * read or parsed)
1124 * 1 on success
1126 static enum DbgInfoLoad DEBUG_ProcessElfFile(const char* filename,
1127 unsigned int load_offset,
1128 unsigned int* dyn_addr)
1130 enum DbgInfoLoad dil = DIL_ERROR;
1131 char* addr = (char*)0xffffffff;
1132 int fd = -1;
1133 struct stat statbuf;
1134 Elf32_Ehdr* ehptr;
1135 Elf32_Shdr* spnt;
1136 Elf32_Phdr* ppnt;
1137 char * shstrtab;
1138 int i;
1139 DBG_MODULE* module = NULL;
1140 DWORD size;
1141 DWORD delta;
1143 DEBUG_Printf(DBG_CHN_TRACE, "Processing elf file '%s'\n", filename);
1145 /* check that the file exists, and that the module hasn't been loaded yet */
1146 if (stat(filename, &statbuf) == -1) goto leave;
1149 * Now open the file, so that we can mmap() it.
1151 if ((fd = open(filename, O_RDONLY)) == -1) goto leave;
1154 * Now mmap() the file.
1156 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1157 if (addr == (char*)0xffffffff) goto leave;
1159 dil = DIL_NOINFO;
1162 * Next, we need to find a few of the internal ELF headers within
1163 * this thing. We need the main executable header, and the section
1164 * table.
1166 ehptr = (Elf32_Ehdr*) addr;
1167 spnt = (Elf32_Shdr*) (addr + ehptr->e_shoff);
1168 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1170 /* if non relocatable ELF, then remove fixed address from computation
1171 * otherwise, all addresses are zero based
1173 delta = (load_offset == 0) ? ehptr->e_entry : 0;
1175 /* grab size of module once loaded in memory */
1176 ppnt = (Elf32_Phdr*) (addr + ehptr->e_phoff);
1177 size = 0;
1178 for (i = 0; i < ehptr->e_phnum; i++) {
1179 if (ppnt[i].p_type != PT_LOAD) continue;
1180 if (size < ppnt[i].p_vaddr - delta + ppnt[i].p_memsz)
1181 size = ppnt[i].p_vaddr - delta + ppnt[i].p_memsz;
1184 for (i = 0; i < ehptr->e_shnum; i++) {
1185 if (strcmp(shstrtab + spnt[i].sh_name, ".bss") == 0 &&
1186 spnt[i].sh_type == SHT_NOBITS) {
1187 if (size < spnt[i].sh_addr - delta + spnt[i].sh_size)
1188 size = spnt[i].sh_addr - delta + spnt[i].sh_size;
1190 if (strcmp(shstrtab + spnt[i].sh_name, ".dynamic") == 0 &&
1191 spnt[i].sh_type == SHT_DYNAMIC) {
1192 if (dyn_addr) *dyn_addr = spnt[i].sh_addr;
1196 module = DEBUG_RegisterELFModule((load_offset == 0) ? ehptr->e_entry : load_offset,
1197 size, filename);
1198 if (!module) {
1199 dil = DIL_ERROR;
1200 goto leave;
1203 if ((module->elf_info = DBG_alloc(sizeof(ELF_DBG_INFO))) == NULL) {
1204 DEBUG_Printf(DBG_CHN_ERR, "OOM\n");
1205 exit(0);
1208 module->elf_info->elf_addr = load_offset;
1209 dil = DEBUG_LoadElfStabs(module);
1211 leave:
1212 if (addr != (char*)0xffffffff) munmap(addr, statbuf.st_size);
1213 if (fd != -1) close(fd);
1214 if (module) module->dil = dil;
1216 return dil;
1219 static enum DbgInfoLoad DEBUG_ProcessElfFileFromPath(const char * filename,
1220 unsigned int load_offset,
1221 unsigned int* dyn_addr,
1222 const char* path)
1224 enum DbgInfoLoad dil = DIL_ERROR;
1225 char *s, *t, *fn;
1226 char* paths = NULL;
1228 if (!path) return -1;
1230 for (s = paths = DBG_strdup(path); s && *s; s = (t) ? (t+1) : NULL) {
1231 t = strchr(s, ':');
1232 if (t) *t = '\0';
1233 fn = (char*)DBG_alloc(strlen(filename) + 1 + strlen(s) + 1);
1234 if (!fn) break;
1235 strcpy(fn, s );
1236 strcat(fn, "/");
1237 strcat(fn, filename);
1238 dil = DEBUG_ProcessElfFile(fn, load_offset, dyn_addr);
1239 DBG_free(fn);
1240 if (dil != DIL_ERROR) break;
1241 s = (t) ? (t+1) : NULL;
1244 DBG_free(paths);
1245 return dil;
1248 static enum DbgInfoLoad DEBUG_ProcessElfObject(const char* filename,
1249 unsigned int load_offset,
1250 unsigned int* dyn_addr)
1252 enum DbgInfoLoad dil = DIL_ERROR;
1254 if (filename == NULL) return DIL_ERROR;
1255 if (DEBUG_FindModuleByName(filename, DMT_ELF)) return DIL_LOADED;
1257 dil = DEBUG_ProcessElfFile(filename, load_offset, dyn_addr);
1259 /* if relative pathname, try some absolute base dirs */
1260 if (dil == DIL_ERROR && !strchr(filename, '/')) {
1261 dil = DEBUG_ProcessElfFileFromPath(filename, load_offset, dyn_addr, getenv("PATH"));
1262 if (dil == DIL_ERROR)
1263 dil = DEBUG_ProcessElfFileFromPath(filename, load_offset, dyn_addr, getenv("LD_LIBRARY_PATH"));
1266 DEBUG_ReportDIL(dil, "ELF", filename, load_offset);
1268 return dil;
1271 static BOOL DEBUG_WalkList(struct r_debug* dbg_hdr)
1273 u_long lm_addr;
1274 struct link_map lm;
1275 Elf32_Ehdr ehdr;
1276 char bufstr[256];
1279 * Now walk the linked list. In all known ELF implementations,
1280 * the dynamic loader maintains this linked list for us. In some
1281 * cases the first entry doesn't appear with a name, in other cases it
1282 * does.
1284 for (lm_addr = (u_long)dbg_hdr->r_map; lm_addr; lm_addr = (u_long)lm.l_next) {
1285 if (!DEBUG_READ_MEM_VERBOSE((void*)lm_addr, &lm, sizeof(lm)))
1286 return FALSE;
1287 if (lm.l_addr != 0 &&
1288 DEBUG_READ_MEM_VERBOSE((void*)lm.l_addr, &ehdr, sizeof(ehdr)) &&
1289 ehdr.e_type == ET_DYN && /* only look at dynamic modules */
1290 lm.l_name != NULL &&
1291 DEBUG_READ_MEM_VERBOSE((void*)lm.l_name, bufstr, sizeof(bufstr))) {
1292 bufstr[sizeof(bufstr) - 1] = '\0';
1293 DEBUG_ProcessElfObject(bufstr, (unsigned)lm.l_addr, NULL);
1297 return TRUE;
1300 static BOOL DEBUG_RescanElf(void)
1302 struct r_debug dbg_hdr;
1304 if (!DEBUG_CurrProcess ||
1305 !DEBUG_READ_MEM_VERBOSE((void*)DEBUG_CurrProcess->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)))
1306 return FALSE;
1308 switch (dbg_hdr.r_state) {
1309 case RT_CONSISTENT:
1310 DEBUG_WalkList(&dbg_hdr);
1311 break;
1312 case RT_ADD:
1313 break;
1314 case RT_DELETE:
1315 /* FIXME: this is not currently handled, would need some kind of mark&sweep algo */
1316 break;
1318 return FALSE;
1321 enum DbgInfoLoad DEBUG_ReadExecutableDbgInfo(const char* exe_name)
1323 Elf32_Dyn dyn;
1324 struct r_debug dbg_hdr;
1325 enum DbgInfoLoad dil = DIL_NOINFO;
1326 unsigned int dyn_addr;
1329 * Make sure we can stat and open this file.
1331 if (exe_name == NULL) goto leave;
1332 DEBUG_ProcessElfObject(exe_name, 0, &dyn_addr);
1334 do {
1335 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn_addr, &dyn, sizeof(dyn)))
1336 goto leave;
1337 dyn_addr += sizeof(dyn);
1338 } while (dyn.d_tag != DT_DEBUG && dyn.d_tag != DT_NULL);
1339 if (dyn.d_tag == DT_NULL) goto leave;
1342 * OK, now dig into the actual tables themselves.
1344 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn.d_un.d_ptr, &dbg_hdr, sizeof(dbg_hdr)))
1345 goto leave;
1347 assert(!DEBUG_CurrProcess->dbg_hdr_addr);
1348 DEBUG_CurrProcess->dbg_hdr_addr = (u_long)dyn.d_un.d_ptr;
1350 if (dbg_hdr.r_brk) {
1351 DBG_VALUE value;
1353 DEBUG_Printf(DBG_CHN_TRACE, "Setting up a breakpoint on r_brk(%lx)\n",
1354 (unsigned long)dbg_hdr.r_brk);
1356 DEBUG_SetBreakpoints(FALSE);
1357 value.type = NULL;
1358 value.cookie = DV_TARGET;
1359 value.addr.seg = 0;
1360 value.addr.off = (DWORD)dbg_hdr.r_brk;
1361 DEBUG_AddBreakpoint(&value, DEBUG_RescanElf);
1362 DEBUG_SetBreakpoints(TRUE);
1365 dil = DEBUG_WalkList(&dbg_hdr);
1367 leave:
1368 return dil;
1371 #else /* !__ELF__ */
1373 enum DbgInfoLoad DEBUG_ReadExecutableDbgInfo(const char* exe_name)
1375 return FALSE;
1378 #endif /* __ELF__ */