In OSS_RawOpenDevice, always retrieve the device format and store it
[wine/multimedia.git] / programs / winedbg / stabs.c
blob79b6cd677080bc49547bd63cea73011121b0204f
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
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Maintenance Information
25 * -----------------------
27 * For documentation on the stabs format see for example
28 * The "stabs" debug format
29 * by Julia Menapace, Jim Kingdon, David Mackenzie
30 * of Cygnus Support
31 * available (hopefully) from http:\\sources.redhat.com\gdb\onlinedocs
34 #include "config.h"
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <sys/stat.h>
39 #ifdef HAVE_SYS_MMAN_H
40 #include <sys/mman.h>
41 #endif
42 #include <limits.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #ifndef PATH_MAX
47 #define PATH_MAX MAX_PATH
48 #endif
50 #include "debugger.h"
52 #if defined(__svr4__) || defined(__sun)
53 #define __ELF__
54 #endif
56 #ifdef __ELF__
57 #ifdef HAVE_ELF_H
58 # include <elf.h>
59 #endif
60 #ifdef HAVE_LINK_H
61 # include <link.h>
62 #endif
63 #ifdef HAVE_SYS_LINK_H
64 # include <sys/link.h>
65 #endif
66 #endif
68 #ifndef N_UNDF
69 #define N_UNDF 0x00
70 #endif
72 #ifndef STN_UNDEF
73 # define STN_UNDEF 0
74 #endif
76 #define N_GSYM 0x20
77 #define N_FUN 0x24
78 #define N_STSYM 0x26
79 #define N_LCSYM 0x28
80 #define N_MAIN 0x2a
81 #define N_ROSYM 0x2c
82 #define N_OPT 0x3c
83 #define N_RSYM 0x40
84 #define N_SLINE 0x44
85 #define N_SO 0x64
86 #define N_LSYM 0x80
87 #define N_BINCL 0x82
88 #define N_SOL 0x84
89 #define N_PSYM 0xa0
90 #define N_EINCL 0xa2
91 #define N_LBRAC 0xc0
92 #define N_EXCL 0xc2
93 #define N_RBRAC 0xe0
95 typedef struct tagELF_DBG_INFO
97 unsigned long elf_addr;
98 } ELF_DBG_INFO;
100 struct stab_nlist {
101 union {
102 char *n_name;
103 struct stab_nlist *n_next;
104 long n_strx;
105 } n_un;
106 unsigned char n_type;
107 char n_other;
108 short n_desc;
109 unsigned long n_value;
112 static void stab_strcpy(char * dest, int sz, const char * source)
115 * A strcpy routine that stops when we hit the ':' character.
116 * Faster than copying the whole thing, and then nuking the
117 * ':'.
119 while(*source != '\0' && *source != ':' && sz-- > 0)
120 *dest++ = *source++;
121 *dest = '\0';
122 assert(sz > 0);
125 typedef struct {
126 char* name;
127 unsigned long value;
128 int idx;
129 struct datatype** vector;
130 int nrofentries;
131 } include_def;
133 #define MAX_INCLUDES 5120
135 static include_def* include_defs = NULL;
136 static int num_include_def = 0;
137 static int num_alloc_include_def = 0;
138 static int cu_include_stack[MAX_INCLUDES];
139 static int cu_include_stk_idx = 0;
140 static struct datatype** cu_vector = NULL;
141 static int cu_nrofentries = 0;
143 static
145 DEBUG_CreateInclude(const char* file, unsigned long val)
147 if (num_include_def == num_alloc_include_def)
149 num_alloc_include_def += 256;
150 include_defs = DBG_realloc(include_defs, sizeof(include_defs[0])*num_alloc_include_def);
151 memset(include_defs+num_include_def, 0, sizeof(include_defs[0])*256);
153 include_defs[num_include_def].name = DBG_strdup(file);
154 include_defs[num_include_def].value = val;
155 include_defs[num_include_def].vector = NULL;
156 include_defs[num_include_def].nrofentries = 0;
158 return num_include_def++;
161 static
163 DEBUG_FindInclude(const char* file, unsigned long val)
165 int i;
167 for (i = 0; i < num_include_def; i++)
169 if (val == include_defs[i].value &&
170 strcmp(file, include_defs[i].name) == 0)
171 return i;
173 return -1;
176 static
178 DEBUG_AddInclude(int idx)
180 ++cu_include_stk_idx;
182 /* is this happen, just bump MAX_INCLUDES */
183 /* we could also handle this as another dynarray */
184 assert(cu_include_stk_idx < MAX_INCLUDES);
186 cu_include_stack[cu_include_stk_idx] = idx;
187 return cu_include_stk_idx;
190 static
191 void
192 DEBUG_ResetIncludes(void)
195 * The datatypes that we would need to use are reset when
196 * we start a new file. (at least the ones in filenr == 0
198 cu_include_stk_idx = 0;/* keep 0 as index for the .c file itself */
199 memset(cu_vector, 0, sizeof(cu_vector[0]) * cu_nrofentries);
202 static
203 void
204 DEBUG_FreeIncludes(void)
206 int i;
208 DEBUG_ResetIncludes();
210 for (i = 0; i < num_include_def; i++)
212 DBG_free(include_defs[i].name);
213 DBG_free(include_defs[i].vector);
215 DBG_free(include_defs);
216 include_defs = NULL;
217 num_include_def = 0;
218 num_alloc_include_def = 0;
219 DBG_free(cu_vector);
220 cu_vector = NULL;
221 cu_nrofentries = 0;
224 static
225 struct datatype**
226 DEBUG_FileSubNr2StabEnum(int filenr, int subnr)
228 struct datatype** ret;
230 /* DEBUG_Printf(DBG_CHN_MESG, "creating type id for (%d,%d)\n", filenr, subnr); */
232 /* FIXME: I could perhaps create a dummy include_def for each compilation
233 * unit which would allow not to handle those two cases separately
235 if (filenr == 0)
237 if (cu_nrofentries <= subnr)
239 cu_vector = DBG_realloc(cu_vector, sizeof(cu_vector[0])*(subnr+1));
240 memset(cu_vector+cu_nrofentries, 0, sizeof(cu_vector[0])*(subnr+1-cu_nrofentries));
241 cu_nrofentries = subnr + 1;
243 ret = &cu_vector[subnr];
245 else
247 include_def* idef;
249 assert(filenr <= cu_include_stk_idx);
251 idef = &include_defs[cu_include_stack[filenr]];
253 if (idef->nrofentries <= subnr)
255 idef->vector = DBG_realloc(idef->vector, sizeof(idef->vector[0])*(subnr+1));
256 memset(idef->vector + idef->nrofentries, 0, sizeof(idef->vector[0])*(subnr+1-idef->nrofentries));
257 idef->nrofentries = subnr + 1;
259 ret = &idef->vector[subnr];
261 /* DEBUG_Printf(DBG_CHN_MESG,"(%d,%d) is %d\n",filenr,subnr,ret); */
262 return ret;
265 static
266 struct datatype**
267 DEBUG_ReadTypeEnum(char **x) {
268 int filenr,subnr;
270 if (**x=='(') {
271 (*x)++; /* '(' */
272 filenr=strtol(*x,x,10); /* <int> */
273 (*x)++; /* ',' */
274 subnr=strtol(*x,x,10); /* <int> */
275 (*x)++; /* ')' */
276 } else {
277 filenr = 0;
278 subnr = strtol(*x,x,10); /* <int> */
280 return DEBUG_FileSubNr2StabEnum(filenr,subnr);
283 struct ParseTypedefData {
284 char* ptr;
285 char buf[1024];
286 int idx;
289 static int DEBUG_PTS_ReadTypedef(struct ParseTypedefData* ptd, const char* typename,
290 struct datatype** dt);
292 static int DEBUG_PTS_ReadID(struct ParseTypedefData* ptd)
294 char* first = ptd->ptr;
295 unsigned int len;
297 if ((ptd->ptr = strchr(ptd->ptr, ':')) == NULL) return -1;
298 len = ptd->ptr - first;
299 if (len >= sizeof(ptd->buf) - ptd->idx) return -1;
300 memcpy(ptd->buf + ptd->idx, first, len);
301 ptd->buf[ptd->idx + len] = '\0';
302 ptd->idx += len + 1;
303 ptd->ptr++; /* ':' */
304 return 0;
307 static int DEBUG_PTS_ReadNum(struct ParseTypedefData* ptd, int* v)
309 char* last;
311 *v = strtol(ptd->ptr, &last, 10);
312 if (last == ptd->ptr) return -1;
313 ptd->ptr = last;
314 return 0;
317 static int DEBUG_PTS_ReadTypeReference(struct ParseTypedefData* ptd,
318 int* filenr, int* subnr)
320 if (*ptd->ptr == '(') {
321 /* '(' <int> ',' <int> ')' */
322 ptd->ptr++;
323 if (DEBUG_PTS_ReadNum(ptd, filenr) == -1) return -1;
324 if (*ptd->ptr++ != ',') return -1;
325 if (DEBUG_PTS_ReadNum(ptd, subnr) == -1) return -1;
326 if (*ptd->ptr++ != ')') return -1;
327 } else {
328 *filenr = 0;
329 if (DEBUG_PTS_ReadNum(ptd, subnr) == -1) return -1;
331 return 0;
334 static int DEBUG_PTS_ReadRange(struct ParseTypedefData* ptd, struct datatype** dt,
335 int* lo, int* hi)
337 /* type ';' <int> ';' <int> ';' */
338 if (DEBUG_PTS_ReadTypedef(ptd, NULL, dt) == -1) return -1;
339 if (*ptd->ptr++ != ';') return -1; /* ';' */
340 if (DEBUG_PTS_ReadNum(ptd, lo) == -1) return -1;
341 if (*ptd->ptr++ != ';') return -1; /* ';' */
342 if (DEBUG_PTS_ReadNum(ptd, hi) == -1) return -1;
343 if (*ptd->ptr++ != ';') return -1; /* ';' */
344 return 0;
347 static inline int DEBUG_PTS_ReadAggregate(struct ParseTypedefData* ptd, struct datatype* sdt)
349 int sz, ofs;
350 char* last;
351 struct datatype* adt;
352 int idx;
353 int doadd;
355 sz = strtol(ptd->ptr, &last, 10);
356 if (last == ptd->ptr) return -1;
357 ptd->ptr = last;
359 doadd = DEBUG_SetStructSize(sdt, sz);
360 /* if the structure has already been filled, just redo the parsing
361 * but don't store results into the struct
362 * FIXME: there's a quite ugly memory leak in there...
365 /* Now parse the individual elements of the structure/union. */
366 while (*ptd->ptr != ';') {
367 /* agg_name : type ',' <int:offset> ',' <int:size> */
368 idx = ptd->idx;
369 if (DEBUG_PTS_ReadID(ptd) == -1) return -1;
370 /* Ref. TSDF R2.130 Section 7.4. When the field name is a method name
371 * it is followed by two colons rather than one.
373 if (*ptd->ptr == ':') ptd->ptr++;
375 if (DEBUG_PTS_ReadTypedef(ptd, NULL, &adt) == -1) return -1;
376 if (!adt) return -1;
378 if (*ptd->ptr++ != ',') return -1;
379 if (DEBUG_PTS_ReadNum(ptd, &ofs) == -1) return -1;
380 if (*ptd->ptr++ != ',') return -1;
381 if (DEBUG_PTS_ReadNum(ptd, &sz) == -1) return -1;
382 if (*ptd->ptr++ != ';') return -1;
384 if (doadd) DEBUG_AddStructElement(sdt, ptd->buf + idx, adt, ofs, sz);
385 ptd->idx = idx;
387 ptd->ptr++; /* ';' */
388 return 0;
391 static inline int DEBUG_PTS_ReadEnum(struct ParseTypedefData* ptd, struct datatype* edt)
393 int ofs;
394 int idx;
396 while (*ptd->ptr != ';') {
397 idx = ptd->idx;
398 if (DEBUG_PTS_ReadID(ptd) == -1) return -1;
399 if (DEBUG_PTS_ReadNum(ptd, &ofs) == -1) return -1;
400 if (*ptd->ptr++ != ',') return -1;
401 DEBUG_AddStructElement(edt, ptd->buf + idx, NULL, ofs, 0);
402 ptd->idx = idx;
404 ptd->ptr++;
405 return 0;
408 static inline int DEBUG_PTS_ReadArray(struct ParseTypedefData* ptd, struct datatype* adt)
410 int lo, hi;
411 struct datatype* rdt;
413 /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo> */
415 if (*ptd->ptr++ != 'r') return -1;
416 /* FIXME: range type is lost, always assume int */
417 if (DEBUG_PTS_ReadRange(ptd, &rdt, &lo, &hi) == -1) return -1;
418 if (DEBUG_PTS_ReadTypedef(ptd, NULL, &rdt) == -1) return -1;
420 DEBUG_SetArrayParams(adt, lo, hi, rdt);
421 return 0;
424 static int DEBUG_PTS_ReadTypedef(struct ParseTypedefData* ptd, const char* typename,
425 struct datatype** ret_dt)
427 int idx, lo, hi, sz = -1;
428 struct datatype* new_dt = NULL; /* newly created data type */
429 struct datatype* ref_dt; /* referenced data type (pointer...) */
430 struct datatype* dt1; /* intermediate data type (scope is limited) */
431 struct datatype* dt2; /* intermediate data type: t1=t2=new_dt */
432 int filenr1, subnr1;
433 int filenr2 = 0, subnr2 = 0;
435 /* things are a bit complicated because of the way the typedefs are stored inside
436 * the file (we cannot keep the struct datatype** around, because address can
437 * change when realloc is done, so we must call over and over
438 * DEBUG_FileSubNr2StabEnum to keep the correct values around
439 * (however, keeping struct datatype* is valid))
441 if (DEBUG_PTS_ReadTypeReference(ptd, &filenr1, &subnr1) == -1) return -1;
443 while (*ptd->ptr == '=') {
444 ptd->ptr++;
445 if (new_dt) {
446 DEBUG_Printf(DBG_CHN_MESG, "Bad recursion (1) in typedef\n");
447 return -1;
449 /* first handle attribute if any */
450 switch (*ptd->ptr) {
451 case '@':
452 if (*++ptd->ptr == 's') {
453 ptd->ptr++;
454 if (DEBUG_PTS_ReadNum(ptd, &sz) == -1) {
455 DEBUG_Printf(DBG_CHN_MESG, "Not an attribute... NIY\n");
456 ptd->ptr -= 2;
457 return -1;
459 if (*ptd->ptr++ != ';') return -1;
461 break;
463 /* then the real definitions */
464 switch (*ptd->ptr++) {
465 case '*':
466 new_dt = DEBUG_NewDataType(DT_POINTER, NULL);
467 if (DEBUG_PTS_ReadTypedef(ptd, NULL, &ref_dt) == -1) return -1;
468 DEBUG_SetPointerType(new_dt, ref_dt);
469 break;
470 case 'k': /* 'const' modifier */
471 case 'B': /* 'volatile' modifier */
472 /* just kinda ignore the modifier, I guess -gmt */
473 if (DEBUG_PTS_ReadTypedef(ptd, NULL, &new_dt) == -1) return -1;
474 break;
475 case '(':
476 ptd->ptr--;
477 /* doit a two level by hand, otherwise we'd need a stack */
478 if (filenr2 || subnr2) {
479 DEBUG_Printf(DBG_CHN_MESG, "Bad recursion (2) in typedef\n");
480 return -1;
482 if (DEBUG_PTS_ReadTypeReference(ptd, &filenr2, &subnr2) == -1) return -1;
484 dt1 = *DEBUG_FileSubNr2StabEnum(filenr1, subnr1);
485 dt2 = *DEBUG_FileSubNr2StabEnum(filenr2, subnr2);
487 if (!dt1 && dt2) {
488 new_dt = dt2;
489 filenr2 = subnr2 = 0;
490 } else if (!dt1 && !dt2) {
491 new_dt = NULL;
492 } else {
493 DEBUG_Printf(DBG_CHN_MESG, "Unknown condition %08lx %08lx (%s)\n",
494 (unsigned long)dt1, (unsigned long)dt2, ptd->ptr);
495 return -1;
497 break;
498 case 'a':
499 new_dt = DEBUG_NewDataType(DT_ARRAY, NULL);
500 if (DEBUG_PTS_ReadArray(ptd, new_dt) == -1) return -1;
501 break;
502 case 'r':
503 new_dt = DEBUG_NewDataType(DT_BASIC, typename);
504 assert(!*DEBUG_FileSubNr2StabEnum(filenr1, subnr1));
505 *DEBUG_FileSubNr2StabEnum(filenr1, subnr1) = new_dt;
506 if (DEBUG_PTS_ReadRange(ptd, &ref_dt, &lo, &hi) == -1) return -1;
507 /* should perhaps do more here... */
508 break;
509 case 'f':
510 new_dt = DEBUG_NewDataType(DT_FUNC, NULL);
511 if (DEBUG_PTS_ReadTypedef(ptd, NULL, &ref_dt) == -1) return -1;
512 DEBUG_SetPointerType(new_dt, ref_dt);
513 break;
514 case 'e':
515 new_dt = DEBUG_NewDataType(DT_ENUM, NULL);
516 if (DEBUG_PTS_ReadEnum(ptd, new_dt) == -1) return -1;
517 break;
518 case 's':
519 case 'u':
520 /* dt1 can have been already defined in a forward definition */
521 dt1 = *DEBUG_FileSubNr2StabEnum(filenr1, subnr1);
522 dt2 = DEBUG_TypeCast(DT_STRUCT, typename);
523 if (!dt1) {
524 new_dt = DEBUG_NewDataType(DT_STRUCT, typename);
525 /* we need to set it here, because a struct can hold a pointer
526 * to itself
528 *DEBUG_FileSubNr2StabEnum(filenr1, subnr1) = new_dt;
529 } else {
530 if (DEBUG_GetType(dt1) != DT_STRUCT) {
531 DEBUG_Printf(DBG_CHN_MESG,
532 "Forward declaration is not an aggregate\n");
533 return -1;
536 /* should check typename is the same too */
537 new_dt = dt1;
539 if (DEBUG_PTS_ReadAggregate(ptd, new_dt) == -1) return -1;
540 break;
541 case 'x':
542 switch (*ptd->ptr++) {
543 case 'e': lo = DT_ENUM; break;
544 case 's': case 'u': lo = DT_STRUCT; break;
545 default: return -1;
548 idx = ptd->idx;
549 if (DEBUG_PTS_ReadID(ptd) == -1) return -1;
550 new_dt = DEBUG_NewDataType(lo, ptd->buf + idx);
551 ptd->idx = idx;
552 break;
553 case '-':
554 if (DEBUG_PTS_ReadNum(ptd, &lo) == -1) {
555 DEBUG_Printf(DBG_CHN_MESG, "Should be a number (%s)...\n", ptd->ptr);
556 return -1;
557 } else {
558 enum debug_type_basic basic = DT_BASIC_LAST;
559 switch (lo)
561 case 1: basic = DT_BASIC_INT; break;
562 case 2: basic = DT_BASIC_CHAR; break;
563 case 3: basic = DT_BASIC_SHORTINT; break;
564 case 4: basic = DT_BASIC_LONGINT; break;
565 case 5: basic = DT_BASIC_UCHAR; break;
566 case 6: basic = DT_BASIC_SCHAR; break;
567 case 7: basic = DT_BASIC_USHORTINT; break;
568 case 8: basic = DT_BASIC_UINT; break;
569 /* case 9: basic = DT_BASIC_UINT"; */
570 case 10: basic = DT_BASIC_ULONGINT; break;
571 case 11: basic = DT_BASIC_VOID; break;
572 case 12: basic = DT_BASIC_FLOAT; break;
573 case 13: basic = DT_BASIC_DOUBLE; break;
574 case 14: basic = DT_BASIC_LONGDOUBLE; break;
575 /* case 15: basic = DT_BASIC_INT; break; */
576 case 16:
577 switch (sz) {
578 case 32: basic = DT_BASIC_BOOL1; break;
579 case 16: basic = DT_BASIC_BOOL2; break;
580 case 8: basic = DT_BASIC_BOOL4; break;
582 break;
583 /* case 17: basic = DT_BASIC_SHORT real; break; */
584 /* case 18: basic = DT_BASIC_REAL; break; */
585 case 25: basic = DT_BASIC_CMPLX_FLOAT; break;
586 case 26: basic = DT_BASIC_CMPLX_DOUBLE; break;
587 /* case 30: basic = DT_BASIC_wchar"; break; */
588 case 31: basic = DT_BASIC_LONGLONGINT; break;
589 case 32: basic = DT_BASIC_ULONGLONGINT; break;
590 default:
591 DEBUG_Printf(DBG_CHN_MESG, "Unsupported integral type (%d/%d)\n", lo, sz);
592 return -1;
594 if (!(new_dt = DEBUG_GetBasicType(basic))) {
595 DEBUG_Printf(DBG_CHN_MESG, "Basic type %d not found\n", basic);
596 return -1;
598 if (*ptd->ptr++ != ';') return -1;
600 break;
601 default:
602 DEBUG_Printf(DBG_CHN_MESG, "Unknown type '%c'\n", ptd->ptr[-1]);
603 return -1;
607 if ((filenr2 || subnr2) && !*DEBUG_FileSubNr2StabEnum(filenr2, subnr2)) {
608 if (!new_dt) {
609 /* this should be a basic type, define it, or even void */
610 new_dt = DEBUG_NewDataType(DT_BASIC, typename);
612 *DEBUG_FileSubNr2StabEnum(filenr2, subnr2) = new_dt;
615 if (!new_dt) {
616 dt1 = *DEBUG_FileSubNr2StabEnum(filenr1, subnr1);
617 if (!dt1) {
618 DEBUG_Printf(DBG_CHN_MESG, "Nothing has been defined <%s>\n", ptd->ptr);
619 return -1;
621 *ret_dt = dt1;
622 return 0;
625 *DEBUG_FileSubNr2StabEnum(filenr1, subnr1) = *ret_dt = new_dt;
627 #if 0
628 if (typename) {
629 DEBUG_Printf(DBG_CHN_MESG, "Adding (%d,%d) %s => ", filenr1, subnr1, typename);
630 DEBUG_PrintTypeCast(new_dt);
631 DEBUG_Printf(DBG_CHN_MESG, "\n");
633 #endif
635 return 0;
638 static int DEBUG_ParseTypedefStab(char* ptr, const char* typename)
640 struct ParseTypedefData ptd;
641 struct datatype* dt;
642 int ret = -1;
644 /* check for already existing definition */
646 ptd.idx = 0;
647 if ((ptd.ptr = strchr(ptr, ':'))) {
648 ptd.ptr++;
649 if (*ptd.ptr != '(') ptd.ptr++;
650 ret = DEBUG_PTS_ReadTypedef(&ptd, typename, &dt);
653 if (ret == -1 || *ptd.ptr) {
654 DEBUG_Printf(DBG_CHN_MESG, "failure on %s at %s\n", ptr, ptd.ptr);
655 return FALSE;
658 return TRUE;
661 static struct datatype *
662 DEBUG_ParseStabType(const char * stab)
664 char * c;
667 * Look through the stab definition, and figure out what datatype
668 * this represents. If we have something we know about, assign the
669 * type.
670 * According to "The \"stabs\" debug format" (Rev 2.130) the name may be
671 * a C++ name and contain double colons e.g. foo::bar::baz:t5=*6. Not
672 * yet implemented.
674 c = strchr(stab, ':');
675 if( c == NULL )
676 return NULL;
678 c++;
680 * The next characters say more about the type (i.e. data, function, etc)
681 * of symbol. Skip them. (C++ for example may have Tt).
682 * Actually this is a very weak description; I think Tt is the only
683 * multiple combination we should see.
685 while (*c && *c != '(' && !isdigit(*c))
686 c++;
688 * The next is either an integer or a (integer,integer).
689 * The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
691 return *DEBUG_ReadTypeEnum(&c);
694 enum DbgInfoLoad DEBUG_ParseStabs(char * addr, unsigned int load_offset,
695 unsigned int staboff, int stablen,
696 unsigned int strtaboff, int strtablen)
698 struct name_hash * curr_func = NULL;
699 struct wine_locals * curr_loc = NULL;
700 struct name_hash * curr_sym = NULL;
701 char currpath[PATH_MAX];
702 int i;
703 int in_external_file = FALSE;
704 int last_nso = -1;
705 unsigned int len;
706 DBG_VALUE new_value;
707 int nstab;
708 char * ptr;
709 char * stabbuff;
710 unsigned int stabbufflen;
711 struct stab_nlist * stab_ptr;
712 char * strs;
713 int strtabinc;
714 char * subpath = NULL;
715 char symname[4096];
717 nstab = stablen / sizeof(struct stab_nlist);
718 stab_ptr = (struct stab_nlist *) (addr + staboff);
719 strs = (char *) (addr + strtaboff);
721 memset(currpath, 0, sizeof(currpath));
724 * Allocate a buffer into which we can build stab strings for cases
725 * where the stab is continued over multiple lines.
727 stabbufflen = 65536;
728 stabbuff = (char *) DBG_alloc(stabbufflen);
730 strtabinc = 0;
731 stabbuff[0] = '\0';
732 for(i=0; i < nstab; i++, stab_ptr++ )
734 ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
735 if( ptr[strlen(ptr) - 1] == '\\' )
738 * Indicates continuation. Append this to the buffer, and go onto the
739 * next record. Repeat the process until we find a stab without the
740 * '/' character, as this indicates we have the whole thing.
742 len = strlen(ptr);
743 if( strlen(stabbuff) + len > stabbufflen )
745 stabbufflen += 65536;
746 stabbuff = (char *) DBG_realloc(stabbuff, stabbufflen);
748 strncat(stabbuff, ptr, len - 1);
749 continue;
751 else if( stabbuff[0] != '\0' )
753 strcat( stabbuff, ptr);
754 ptr = stabbuff;
757 if( strchr(ptr, '=') != NULL )
760 * The stabs aren't in writable memory, so copy it over so we are
761 * sure we can scribble on it.
763 if( ptr != stabbuff )
765 strcpy(stabbuff, ptr);
766 ptr = stabbuff;
768 stab_strcpy(symname, sizeof(symname), ptr);
769 if (!DEBUG_ParseTypedefStab(ptr, symname)) {
770 /* skip this definition */
771 stabbuff[0] = '\0';
772 continue;
776 switch(stab_ptr->n_type)
778 case N_GSYM:
780 * These are useless with ELF. They have no value, and you have to
781 * read the normal symbol table to get the address. Thus we
782 * ignore them, and when we process the normal symbol table
783 * we should do the right thing.
785 * With a.out or mingw, they actually do make some amount of sense.
787 new_value.addr.seg = 0;
788 new_value.type = DEBUG_ParseStabType(ptr);
789 new_value.addr.off = load_offset + stab_ptr->n_value;
790 new_value.cookie = DV_TARGET;
792 stab_strcpy(symname, sizeof(symname), ptr);
793 #ifdef __ELF__
794 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
795 SYM_WINE | SYM_DATA | SYM_INVALID );
796 #else
797 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
798 SYM_WINE | SYM_DATA );
799 #endif
800 break;
801 case N_RBRAC:
802 case N_LBRAC:
804 * We need to keep track of these so we get symbol scoping
805 * right for local variables. For now, we just ignore them.
806 * The hooks are already there for dealing with this however,
807 * so all we need to do is to keep count of the nesting level,
808 * and find the RBRAC for each matching LBRAC.
810 break;
811 case N_LCSYM:
812 case N_STSYM:
814 * These are static symbols and BSS symbols.
816 new_value.addr.seg = 0;
817 new_value.type = DEBUG_ParseStabType(ptr);
818 new_value.addr.off = load_offset + stab_ptr->n_value;
819 new_value.cookie = DV_TARGET;
821 stab_strcpy(symname, sizeof(symname), ptr);
822 curr_sym = DEBUG_AddSymbol( symname, &new_value, currpath,
823 SYM_WINE | SYM_DATA );
824 break;
825 case N_PSYM:
827 * These are function parameters.
829 if( curr_func != NULL && !in_external_file )
831 stab_strcpy(symname, sizeof(symname), ptr);
832 curr_loc = DEBUG_AddLocal( curr_func, 0,
833 stab_ptr->n_value, 0, 0, symname );
834 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
836 break;
837 case N_RSYM:
838 if( curr_func != NULL && !in_external_file )
840 stab_strcpy(symname, sizeof(symname), ptr);
841 curr_loc = DEBUG_AddLocal( curr_func, stab_ptr->n_value + 1,
842 0, 0, 0, symname );
843 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
845 break;
846 case N_LSYM:
847 if( curr_func != NULL && !in_external_file )
849 stab_strcpy(symname, sizeof(symname), ptr);
850 curr_loc = DEBUG_AddLocal( curr_func, 0,
851 stab_ptr->n_value, 0, 0, symname );
852 DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr) );
854 break;
855 case N_SLINE:
857 * This is a line number. These are always relative to the start
858 * of the function (N_FUN), and this makes the lookup easier.
860 if( curr_func != NULL && !in_external_file )
862 #ifdef __ELF__
863 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
864 stab_ptr->n_value);
865 #else
866 #if 0
868 * This isn't right. The order of the stabs is different under
869 * a.out, and as a result we would end up attaching the line
870 * number to the wrong function.
872 DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
873 stab_ptr->n_value - curr_func->addr.off);
874 #endif
875 #endif
877 break;
878 case N_FUN:
880 * First, clean up the previous function we were working on.
882 DEBUG_Normalize(curr_func);
885 * For now, just declare the various functions. Later
886 * on, we will add the line number information and the
887 * local symbols.
889 if( !in_external_file)
891 stab_strcpy(symname, sizeof(symname), ptr);
892 if (*symname)
894 new_value.addr.seg = 0;
895 new_value.type = DEBUG_ParseStabType(ptr);
896 new_value.addr.off = load_offset + stab_ptr->n_value;
897 new_value.cookie = DV_TARGET;
899 * Copy the string to a temp buffer so we
900 * can kill everything after the ':'. We do
901 * it this way because otherwise we end up dirtying
902 * all of the pages related to the stabs, and that
903 * sucks up swap space like crazy.
905 #ifdef __ELF__
906 curr_func = DEBUG_AddSymbol( symname, &new_value, currpath,
907 SYM_WINE | SYM_FUNC | SYM_INVALID );
908 #else
909 curr_func = DEBUG_AddSymbol( symname, &new_value, currpath,
910 SYM_WINE | SYM_FUNC );
911 #endif
913 else
915 /* some GCC seem to use a N_FUN "" to mark the end of a function */
916 curr_func = NULL;
919 else
922 * Don't add line number information for this function
923 * any more.
925 curr_func = NULL;
927 break;
928 case N_SO:
930 * This indicates a new source file. Append the records
931 * together, to build the correct path name.
933 #ifndef __ELF__
935 * With a.out, there is no NULL string N_SO entry at the end of
936 * the file. Thus when we find non-consecutive entries,
937 * we consider that a new file is started.
939 if( last_nso < i-1 )
941 currpath[0] = '\0';
942 DEBUG_Normalize(curr_func);
943 curr_func = NULL;
945 #endif
947 if( *ptr == '\0' )
950 * Nuke old path.
952 currpath[0] = '\0';
953 DEBUG_Normalize(curr_func);
954 curr_func = NULL;
956 else
958 if (*ptr != '/')
959 strcat(currpath, ptr);
960 else
961 strcpy(currpath, ptr);
962 subpath = ptr;
963 DEBUG_ResetIncludes();
965 last_nso = i;
966 break;
967 case N_SOL:
969 * This indicates we are including stuff from an include file.
970 * If this is the main source, enable the debug stuff, otherwise
971 * ignore it.
973 in_external_file = !(subpath == NULL || strcmp(ptr, subpath) == 0);
974 break;
975 case N_UNDF:
976 strs += strtabinc;
977 strtabinc = stab_ptr->n_value;
978 DEBUG_Normalize(curr_func);
979 curr_func = NULL;
980 break;
981 case N_OPT:
983 * Ignore this. We don't care what it points to.
985 break;
986 case N_BINCL:
987 DEBUG_AddInclude(DEBUG_CreateInclude(ptr, stab_ptr->n_value));
988 break;
989 case N_EINCL:
990 break;
991 case N_EXCL:
992 DEBUG_AddInclude(DEBUG_FindInclude(ptr, stab_ptr->n_value));
993 break;
994 case N_MAIN:
996 * Always ignore these. GCC doesn't even generate them.
998 break;
999 default:
1000 DEBUG_Printf(DBG_CHN_MESG, "Unknown stab type 0x%02x\n", stab_ptr->n_type);
1001 break;
1004 stabbuff[0] = '\0';
1006 #if 0
1007 DEBUG_Printf(DBG_CHN_MESG, "%d %x %s\n", stab_ptr->n_type,
1008 (unsigned int) stab_ptr->n_value,
1009 strs + (unsigned int) stab_ptr->n_un.n_name);
1010 #endif
1013 DEBUG_FreeIncludes();
1015 return DIL_LOADED;
1018 #ifdef __ELF__
1021 * Walk through the entire symbol table and add any symbols we find there.
1022 * This can be used in cases where we have stripped ELF shared libraries,
1023 * or it can be used in cases where we have data symbols for which the address
1024 * isn't encoded in the stabs.
1026 * This is all really quite easy, since we don't have to worry about line
1027 * numbers or local data variables.
1029 static int DEBUG_ProcessElfSymtab(DBG_MODULE* module, char* addr,
1030 u_long load_addr, Elf32_Shdr* symtab,
1031 Elf32_Shdr* strtab)
1033 char * curfile = NULL;
1034 struct name_hash * curr_sym = NULL;
1035 int flags;
1036 int i;
1037 DBG_VALUE new_value;
1038 int nsym;
1039 char * strp;
1040 char * symname;
1041 Elf32_Sym * symp;
1043 symp = (Elf32_Sym *) (addr + symtab->sh_offset);
1044 nsym = symtab->sh_size / sizeof(*symp);
1045 strp = (char *) (addr + strtab->sh_offset);
1047 for(i=0; i < nsym; i++, symp++)
1050 * Ignore certain types of entries which really aren't of that much
1051 * interest.
1053 if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION ||
1054 symp->st_shndx == STN_UNDEF )
1056 continue;
1059 symname = strp + symp->st_name;
1062 * Save the name of the current file, so we have a way of tracking
1063 * static functions/data.
1065 if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
1067 curfile = symname;
1068 continue;
1072 * See if we already have something for this symbol.
1073 * If so, ignore this entry, because it would have come from the
1074 * stabs or from a previous symbol. If the value is different,
1075 * we will have to keep the darned thing, because there can be
1076 * multiple local symbols by the same name.
1078 if( (DEBUG_GetSymbolValue(symname, -1, &new_value, FALSE ) == gsv_found)
1079 && (new_value.addr.off == (load_addr + symp->st_value)) )
1080 continue;
1082 new_value.addr.seg = 0;
1083 new_value.type = NULL;
1084 new_value.addr.off = load_addr + symp->st_value;
1085 new_value.cookie = DV_TARGET;
1086 flags = SYM_WINE | ((ELF32_ST_TYPE(symp->st_info) == STT_FUNC)
1087 ? SYM_FUNC : SYM_DATA);
1088 if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
1089 curr_sym = DEBUG_AddSymbol( symname, &new_value, NULL, flags );
1090 else
1091 curr_sym = DEBUG_AddSymbol( symname, &new_value, curfile, flags );
1094 * Record the size of the symbol. This can come in handy in
1095 * some cases. Not really used yet, however.
1097 if( symp->st_size != 0 )
1098 DEBUG_SetSymbolSize(curr_sym, symp->st_size);
1101 return TRUE;
1105 * Loads the symbolic information from ELF module stored in 'filename'
1106 * the module has been loaded at 'load_offset' address, so symbols' address
1107 * relocation is performed
1108 * returns
1109 * -1 if the file cannot be found/opened
1110 * 0 if the file doesn't contain symbolic info (or this info cannot be
1111 * read or parsed)
1112 * 1 on success
1114 enum DbgInfoLoad DEBUG_LoadElfStabs(DBG_MODULE* module)
1116 enum DbgInfoLoad dil = DIL_ERROR;
1117 char* addr = (char*)0xffffffff;
1118 int fd = -1;
1119 struct stat statbuf;
1120 Elf32_Ehdr* ehptr;
1121 Elf32_Shdr* spnt;
1122 char* shstrtab;
1123 int i;
1124 int stabsect;
1125 int stabstrsect;
1127 if (module->type != DMT_ELF || !module->elf_info) {
1128 DEBUG_Printf(DBG_CHN_ERR, "Bad elf module '%s'\n", module->module_name);
1129 return DIL_ERROR;
1132 /* check that the file exists, and that the module hasn't been loaded yet */
1133 if (stat(module->module_name, &statbuf) == -1) goto leave;
1134 if (S_ISDIR(statbuf.st_mode)) goto leave;
1137 * Now open the file, so that we can mmap() it.
1139 if ((fd = open(module->module_name, O_RDONLY)) == -1) goto leave;
1141 dil = DIL_NOINFO;
1143 * Now mmap() the file.
1145 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1146 if (addr == (char*)0xffffffff) goto leave;
1149 * Next, we need to find a few of the internal ELF headers within
1150 * this thing. We need the main executable header, and the section
1151 * table.
1153 ehptr = (Elf32_Ehdr*) addr;
1154 spnt = (Elf32_Shdr*) (addr + ehptr->e_shoff);
1155 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1157 stabsect = stabstrsect = -1;
1159 for (i = 0; i < ehptr->e_shnum; i++) {
1160 if (strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0)
1161 stabsect = i;
1163 if (strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0)
1164 stabstrsect = i;
1167 if (stabsect == -1 || stabstrsect == -1) {
1168 DEBUG_Printf(DBG_CHN_WARN, "no .stab section\n");
1169 goto leave;
1173 * OK, now just parse all of the stabs.
1175 if (DEBUG_ParseStabs(addr,
1176 module->elf_info->elf_addr,
1177 spnt[stabsect].sh_offset,
1178 spnt[stabsect].sh_size,
1179 spnt[stabstrsect].sh_offset,
1180 spnt[stabstrsect].sh_size)) {
1181 dil = DIL_LOADED;
1182 } else {
1183 dil = DIL_ERROR;
1184 DEBUG_Printf(DBG_CHN_WARN, "bad stabs\n");
1185 goto leave;
1188 for (i = 0; i < ehptr->e_shnum; i++) {
1189 if ( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
1190 && (spnt[i].sh_type == SHT_SYMTAB))
1191 DEBUG_ProcessElfSymtab(module, addr, module->elf_info->elf_addr,
1192 spnt + i, spnt + spnt[i].sh_link);
1194 if ( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
1195 && (spnt[i].sh_type == SHT_DYNSYM))
1196 DEBUG_ProcessElfSymtab(module, addr, module->elf_info->elf_addr,
1197 spnt + i, spnt + spnt[i].sh_link);
1200 leave:
1201 if (addr != (char*)0xffffffff) munmap(addr, statbuf.st_size);
1202 if (fd != -1) close(fd);
1204 return dil;
1208 * Loads the information for ELF module stored in 'filename'
1209 * the module has been loaded at 'load_offset' address
1210 * returns
1211 * -1 if the file cannot be found/opened
1212 * 0 if the file doesn't contain symbolic info (or this info cannot be
1213 * read or parsed)
1214 * 1 on success
1216 static enum DbgInfoLoad DEBUG_ProcessElfFile(const char* filename,
1217 unsigned int load_offset,
1218 unsigned int* dyn_addr)
1220 static const unsigned char elf_signature[4] = { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3 };
1221 enum DbgInfoLoad dil = DIL_ERROR;
1222 char* addr = (char*)0xffffffff;
1223 int fd = -1;
1224 struct stat statbuf;
1225 Elf32_Ehdr* ehptr;
1226 Elf32_Shdr* spnt;
1227 Elf32_Phdr* ppnt;
1228 char * shstrtab;
1229 int i;
1230 DBG_MODULE* module = NULL;
1231 DWORD size;
1232 DWORD delta;
1234 DEBUG_Printf(DBG_CHN_TRACE, "Processing elf file '%s'\n", filename);
1236 /* check that the file exists, and that the module hasn't been loaded yet */
1237 if (stat(filename, &statbuf) == -1) goto leave;
1240 * Now open the file, so that we can mmap() it.
1242 if ((fd = open(filename, O_RDONLY)) == -1) goto leave;
1245 * Now mmap() the file.
1247 addr = mmap(0, statbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
1248 if (addr == (char*)0xffffffff) goto leave;
1250 dil = DIL_NOINFO;
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
1255 * table.
1257 ehptr = (Elf32_Ehdr*) addr;
1258 if (memcmp( ehptr->e_ident, elf_signature, sizeof(elf_signature) )) goto leave;
1260 spnt = (Elf32_Shdr*) (addr + ehptr->e_shoff);
1261 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1263 /* if non relocatable ELF, then remove fixed address from computation
1264 * otherwise, all addresses are zero based
1266 delta = (load_offset == 0) ? ehptr->e_entry : 0;
1268 /* grab size of module once loaded in memory */
1269 ppnt = (Elf32_Phdr*) (addr + ehptr->e_phoff);
1270 size = 0;
1271 for (i = 0; i < ehptr->e_phnum; i++) {
1272 if (ppnt[i].p_type != PT_LOAD) continue;
1273 if (size < ppnt[i].p_vaddr - delta + ppnt[i].p_memsz)
1274 size = ppnt[i].p_vaddr - delta + ppnt[i].p_memsz;
1277 for (i = 0; i < ehptr->e_shnum; i++)
1279 if (strcmp(shstrtab + spnt[i].sh_name, ".bss") == 0 &&
1280 spnt[i].sh_type == SHT_NOBITS)
1282 if (size < spnt[i].sh_addr - delta + spnt[i].sh_size)
1283 size = spnt[i].sh_addr - delta + spnt[i].sh_size;
1285 if (strcmp(shstrtab + spnt[i].sh_name, ".dynamic") == 0 &&
1286 spnt[i].sh_type == SHT_DYNAMIC)
1288 if (dyn_addr) *dyn_addr = spnt[i].sh_addr;
1292 module = DEBUG_RegisterELFModule((load_offset == 0) ? ehptr->e_entry : load_offset,
1293 size, filename);
1294 if (!module) {
1295 dil = DIL_ERROR;
1296 goto leave;
1299 if ((module->elf_info = DBG_alloc(sizeof(ELF_DBG_INFO))) == NULL) {
1300 DEBUG_Printf(DBG_CHN_ERR, "OOM\n");
1301 exit(0);
1304 module->elf_info->elf_addr = load_offset;
1305 dil = DEBUG_LoadElfStabs(module);
1307 leave:
1308 if (addr != (char*)0xffffffff) munmap(addr, statbuf.st_size);
1309 if (fd != -1) close(fd);
1310 if (module) module->dil = dil;
1312 return dil;
1315 static enum DbgInfoLoad DEBUG_ProcessElfFileFromPath(const char * filename,
1316 unsigned int load_offset,
1317 unsigned int* dyn_addr,
1318 const char* path)
1320 enum DbgInfoLoad dil = DIL_ERROR;
1321 char *s, *t, *fn;
1322 char* paths = NULL;
1324 if (!path) return -1;
1326 for (s = paths = DBG_strdup(path); s && *s; s = (t) ? (t+1) : NULL) {
1327 t = strchr(s, ':');
1328 if (t) *t = '\0';
1329 fn = (char*)DBG_alloc(strlen(filename) + 1 + strlen(s) + 1);
1330 if (!fn) break;
1331 strcpy(fn, s );
1332 strcat(fn, "/");
1333 strcat(fn, filename);
1334 dil = DEBUG_ProcessElfFile(fn, load_offset, dyn_addr);
1335 DBG_free(fn);
1336 if (dil != DIL_ERROR) break;
1337 s = (t) ? (t+1) : NULL;
1340 DBG_free(paths);
1341 return dil;
1344 static enum DbgInfoLoad DEBUG_ProcessElfObject(const char* filename,
1345 unsigned int load_offset,
1346 unsigned int* dyn_addr)
1348 enum DbgInfoLoad dil = DIL_ERROR;
1350 if (filename == NULL) return DIL_ERROR;
1351 if (DEBUG_FindModuleByName(filename, DMT_ELF)) return DIL_LOADED;
1353 if (strstr (filename, "libstdc++")) return DIL_ERROR; /* We know we can't do it */
1354 dil = DEBUG_ProcessElfFile(filename, load_offset, dyn_addr);
1356 /* if relative pathname, try some absolute base dirs */
1357 if (dil == DIL_ERROR && !strchr(filename, '/')) {
1358 dil = DEBUG_ProcessElfFileFromPath(filename, load_offset, dyn_addr, getenv("PATH"));
1359 if (dil == DIL_ERROR)
1360 dil = DEBUG_ProcessElfFileFromPath(filename, load_offset, dyn_addr, getenv("LD_LIBRARY_PATH"));
1361 if (dil == DIL_ERROR)
1362 dil = DEBUG_ProcessElfFileFromPath(filename, load_offset, dyn_addr, getenv("WINEDLLPATH"));
1365 DEBUG_ReportDIL(dil, "ELF", filename, load_offset);
1367 return dil;
1370 static BOOL DEBUG_WalkList(struct r_debug* dbg_hdr)
1372 u_long lm_addr;
1373 struct link_map lm;
1374 Elf32_Ehdr ehdr;
1375 char bufstr[256];
1378 * Now walk the linked list. In all known ELF implementations,
1379 * the dynamic loader maintains this linked list for us. In some
1380 * cases the first entry doesn't appear with a name, in other cases it
1381 * does.
1383 for (lm_addr = (u_long)dbg_hdr->r_map; lm_addr; lm_addr = (u_long)lm.l_next) {
1384 if (!DEBUG_READ_MEM_VERBOSE((void*)lm_addr, &lm, sizeof(lm)))
1385 return FALSE;
1386 if (lm.l_addr != 0 &&
1387 DEBUG_READ_MEM_VERBOSE((void*)lm.l_addr, &ehdr, sizeof(ehdr)) &&
1388 ehdr.e_type == ET_DYN && /* only look at dynamic modules */
1389 lm.l_name != NULL &&
1390 DEBUG_READ_MEM_VERBOSE((void*)lm.l_name, bufstr, sizeof(bufstr))) {
1391 bufstr[sizeof(bufstr) - 1] = '\0';
1392 DEBUG_ProcessElfObject(bufstr, (unsigned)lm.l_addr, NULL);
1396 return TRUE;
1399 static BOOL DEBUG_RescanElf(void)
1401 struct r_debug dbg_hdr;
1403 if (!DEBUG_CurrProcess ||
1404 !DEBUG_READ_MEM_VERBOSE((void*)DEBUG_CurrProcess->dbg_hdr_addr, &dbg_hdr, sizeof(dbg_hdr)))
1405 return FALSE;
1407 switch (dbg_hdr.r_state) {
1408 case RT_CONSISTENT:
1409 DEBUG_WalkList(&dbg_hdr);
1410 DEBUG_CheckDelayedBP();
1411 break;
1412 case RT_ADD:
1413 break;
1414 case RT_DELETE:
1415 /* FIXME: this is not currently handled, would need some kind of mark&sweep algo */
1416 break;
1418 return FALSE;
1421 enum DbgInfoLoad DEBUG_ReadExecutableDbgInfo(const char* exe_name)
1423 Elf32_Dyn dyn;
1424 struct r_debug dbg_hdr;
1425 enum DbgInfoLoad dil = DIL_NOINFO;
1426 unsigned int dyn_addr;
1429 * Make sure we can stat and open this file.
1431 if (exe_name == NULL) goto leave;
1432 DEBUG_ProcessElfObject(exe_name, 0, &dyn_addr);
1434 do {
1435 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn_addr, &dyn, sizeof(dyn)))
1436 goto leave;
1437 dyn_addr += sizeof(dyn);
1438 } while (dyn.d_tag != DT_DEBUG && dyn.d_tag != DT_NULL);
1439 if (dyn.d_tag == DT_NULL) goto leave;
1442 * OK, now dig into the actual tables themselves.
1444 if (!DEBUG_READ_MEM_VERBOSE((void*)dyn.d_un.d_ptr, &dbg_hdr, sizeof(dbg_hdr)))
1445 goto leave;
1447 assert(!DEBUG_CurrProcess->dbg_hdr_addr);
1448 DEBUG_CurrProcess->dbg_hdr_addr = (u_long)dyn.d_un.d_ptr;
1450 if (dbg_hdr.r_brk) {
1451 DBG_VALUE value;
1453 DEBUG_Printf(DBG_CHN_TRACE, "Setting up a breakpoint on r_brk(%lx)\n",
1454 (unsigned long)dbg_hdr.r_brk);
1456 DEBUG_SetBreakpoints(FALSE);
1457 value.type = NULL;
1458 value.cookie = DV_TARGET;
1459 value.addr.seg = 0;
1460 value.addr.off = (DWORD)dbg_hdr.r_brk;
1461 DEBUG_AddBreakpoint(&value, DEBUG_RescanElf, TRUE);
1462 DEBUG_SetBreakpoints(TRUE);
1465 dil = DEBUG_WalkList(&dbg_hdr);
1467 leave:
1468 return dil;
1471 /* FIXME: merge with some of the routines above */
1472 int read_elf_info(const char* filename, unsigned long tab[])
1474 static const unsigned char elf_signature[4] = { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3 };
1475 char* addr;
1476 Elf32_Ehdr* ehptr;
1477 Elf32_Shdr* spnt;
1478 char* shstrtab;
1479 int i;
1480 int ret = 0;
1481 HANDLE hFile;
1482 HANDLE hMap = 0;
1484 addr = NULL;
1485 hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
1486 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1487 if (hFile == INVALID_HANDLE_VALUE) goto leave;
1488 hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
1489 if (hMap == 0) goto leave;
1490 addr = MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
1491 if (addr == NULL) goto leave;
1493 ehptr = (Elf32_Ehdr*) addr;
1494 if (memcmp(ehptr->e_ident, elf_signature, sizeof(elf_signature))) goto leave;
1496 spnt = (Elf32_Shdr*) (addr + ehptr->e_shoff);
1497 shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
1499 tab[0] = tab[1] = tab[2] = 0;
1500 for (i = 0; i < ehptr->e_shnum; i++)
1503 ret = 1;
1504 leave:
1505 if (addr != NULL) UnmapViewOfFile(addr);
1506 if (hMap != 0) CloseHandle(hMap);
1507 if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile);
1508 return ret;
1511 #else /* !__ELF__ */
1513 enum DbgInfoLoad DEBUG_ReadExecutableDbgInfo(const char* exe_name)
1515 return FALSE;
1518 #endif /* __ELF__ */