Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * File stabs.c - read stabs information from the wine executable itself. |
| 3 | * |
| 4 | * Copyright (C) 1996, Eric Youngdale. |
| 5 | */ |
| 6 | |
Marcus Meissner | 592ba10 | 1999-01-20 14:18:55 +0000 | [diff] [blame] | 7 | #include "config.h" |
| 8 | |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 9 | #include <sys/types.h> |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 10 | #include <fcntl.h> |
| 11 | #include <sys/stat.h> |
Alexandre Julliard | f0cbfa0 | 1997-02-15 14:29:56 +0000 | [diff] [blame] | 12 | #include <sys/mman.h> |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 13 | #include <limits.h> |
| 14 | #include <stdio.h> |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 17 | #include <unistd.h> |
Alexandre Julliard | 349a953 | 1997-02-02 19:01:52 +0000 | [diff] [blame] | 18 | #ifndef PATH_MAX |
| 19 | #define PATH_MAX _MAX_PATH |
| 20 | #endif |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 21 | |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 22 | #include "debugger.h" |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 23 | |
Patrik Stridvall | a9a671d | 1999-04-25 19:01:52 +0000 | [diff] [blame] | 24 | #if defined(__svr4__) || defined(__sun) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 25 | #define __ELF__ |
| 26 | #endif |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 27 | |
| 28 | #ifdef __ELF__ |
Marcus Meissner | 592ba10 | 1999-01-20 14:18:55 +0000 | [diff] [blame] | 29 | #ifdef HAVE_ELF_H |
| 30 | # include <elf.h> |
| 31 | #endif |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 32 | #include <link.h> |
Alexandre Julliard | 349a953 | 1997-02-02 19:01:52 +0000 | [diff] [blame] | 33 | #include <sys/mman.h> |
| 34 | #elif defined(__EMX__) |
| 35 | #include <a_out.h> |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 36 | #else |
| 37 | #include <a.out.h> |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 40 | #ifndef N_UNDF |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 41 | #define N_UNDF 0x00 |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 44 | #define N_GSYM 0x20 |
| 45 | #define N_FUN 0x24 |
| 46 | #define N_STSYM 0x26 |
| 47 | #define N_LCSYM 0x28 |
| 48 | #define N_MAIN 0x2a |
| 49 | #define N_ROSYM 0x2c |
| 50 | #define N_OPT 0x3c |
| 51 | #define N_RSYM 0x40 |
| 52 | #define N_SLINE 0x44 |
| 53 | #define N_SO 0x64 |
| 54 | #define N_LSYM 0x80 |
| 55 | #define N_BINCL 0x82 |
| 56 | #define N_SOL 0x84 |
| 57 | #define N_PSYM 0xa0 |
| 58 | #define N_EINCL 0xa2 |
| 59 | #define N_LBRAC 0xc0 |
| 60 | #define N_RBRAC 0xe0 |
| 61 | |
| 62 | |
| 63 | /* |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 64 | * This is how we translate stab types into our internal representations |
| 65 | * of datatypes. |
| 66 | */ |
| 67 | static struct datatype ** stab_types = NULL; |
| 68 | static int num_stab_types = 0; |
| 69 | |
| 70 | /* |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 71 | * Set so that we know the main executable name and path. |
| 72 | */ |
| 73 | char * DEBUG_argv0; |
| 74 | |
| 75 | struct stab_nlist { |
| 76 | union { |
| 77 | char *n_name; |
| 78 | struct stab_nlist *n_next; |
| 79 | long n_strx; |
| 80 | } n_un; |
| 81 | unsigned char n_type; |
| 82 | char n_other; |
| 83 | short n_desc; |
| 84 | unsigned long n_value; |
| 85 | }; |
| 86 | |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 87 | /* |
| 88 | * This is used to keep track of known datatypes so that we don't redefine |
| 89 | * them over and over again. It sucks up lots of memory otherwise. |
| 90 | */ |
| 91 | struct known_typedef |
| 92 | { |
| 93 | struct known_typedef * next; |
| 94 | char * name; |
| 95 | int ndefs; |
| 96 | struct datatype * types[0]; |
| 97 | }; |
| 98 | |
| 99 | #define NR_STAB_HASH 521 |
| 100 | |
Alexandre Julliard | 60ce85c | 1998-02-01 18:33:27 +0000 | [diff] [blame] | 101 | struct known_typedef * ktd_head[NR_STAB_HASH] = {NULL,}; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 102 | |
| 103 | static unsigned int stab_hash( const char * name ) |
| 104 | { |
| 105 | unsigned int hash = 0; |
| 106 | unsigned int tmp; |
| 107 | const char * p; |
| 108 | |
| 109 | p = name; |
| 110 | |
| 111 | while (*p) |
| 112 | { |
| 113 | hash = (hash << 4) + *p++; |
| 114 | |
| 115 | if( (tmp = (hash & 0xf0000000)) ) |
| 116 | { |
| 117 | hash ^= tmp >> 24; |
| 118 | } |
| 119 | hash &= ~tmp; |
| 120 | } |
| 121 | return hash % NR_STAB_HASH; |
| 122 | } |
| 123 | |
| 124 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 125 | static void stab_strcpy(char * dest, const char * source) |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 126 | { |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 127 | /* |
| 128 | * A strcpy routine that stops when we hit the ':' character. |
| 129 | * Faster than copying the whole thing, and then nuking the |
| 130 | * ':'. |
| 131 | */ |
| 132 | while(*source != '\0' && *source != ':') |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 133 | *dest++ = *source++; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 134 | *dest++ = '\0'; |
| 135 | } |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 136 | |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 137 | #define MAX_TD_NESTING 128 |
| 138 | |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 139 | static int **typenums; |
| 140 | static int *nroftypenums=NULL; |
| 141 | static int nrofnroftypenums=0; |
| 142 | static int curtypenum = 0; |
| 143 | |
| 144 | static |
| 145 | int |
| 146 | DEBUG_FileSubNr2StabEnum(int filenr,int subnr) { |
| 147 | if (nrofnroftypenums<=filenr) { |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 148 | nroftypenums = DBG_realloc(nroftypenums,sizeof(nroftypenums[0])*(filenr+1)); |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 149 | memset(nroftypenums+nrofnroftypenums,0,(filenr+1-nrofnroftypenums)*sizeof(nroftypenums[0])); |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 150 | typenums = DBG_realloc(typenums,sizeof(typenums[0])*(filenr+1)); |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 151 | memset(typenums+nrofnroftypenums,0,sizeof(typenums[0])*(filenr+1-nrofnroftypenums)); |
| 152 | nrofnroftypenums=filenr+1; |
| 153 | } |
| 154 | if (nroftypenums[filenr]<=subnr) { |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 155 | typenums[filenr] = DBG_realloc(typenums[filenr],sizeof(typenums[0][0])*(subnr+1)); |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 156 | memset(typenums[filenr]+nroftypenums[filenr],0,sizeof(typenums[0][0])*(subnr+1-nroftypenums[filenr])); |
| 157 | nroftypenums[filenr] = subnr+1; |
| 158 | } |
| 159 | if (!typenums[filenr][subnr]) |
| 160 | typenums[filenr][subnr]=++curtypenum; |
| 161 | |
| 162 | if( num_stab_types <= curtypenum ) { |
| 163 | num_stab_types = curtypenum + 256; |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 164 | stab_types = (struct datatype **) DBG_realloc(stab_types, |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 165 | num_stab_types * sizeof(struct datatype *) |
| 166 | ); |
Ove Kaaven | 7e5b3b3 | 1998-10-11 12:13:42 +0000 | [diff] [blame] | 167 | memset( stab_types + curtypenum, 0, sizeof(struct datatype *) * (num_stab_types - curtypenum) ); |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 168 | } |
Marcus Meissner | 73458b0 | 1998-12-26 12:54:29 +0000 | [diff] [blame] | 169 | /*fprintf(stderr,"(%d,%d) is %d\n",filenr,subnr,typenums[filenr][subnr]); */ |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 170 | return typenums[filenr][subnr]; |
| 171 | } |
| 172 | |
| 173 | static |
| 174 | int |
| 175 | DEBUG_ReadTypeEnumBackwards(char*x) { |
| 176 | int filenr,subnr; |
| 177 | |
| 178 | if (*x==')') { |
| 179 | while (*x!='(') |
| 180 | x--; |
| 181 | x++; /* '(' */ |
| 182 | filenr=strtol(x,&x,10); /* <int> */ |
Alexandre Julliard | f90efa9 | 1998-06-14 15:24:15 +0000 | [diff] [blame] | 183 | x++; /* ',' */ |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 184 | subnr=strtol(x,&x,10); /* <int> */ |
Alexandre Julliard | f90efa9 | 1998-06-14 15:24:15 +0000 | [diff] [blame] | 185 | x++; /* ')' */ |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 186 | } else { |
| 187 | while ((*x>='0') && (*x<='9')) |
| 188 | x--; |
| 189 | filenr = 0; |
| 190 | subnr = atol(x+1); |
| 191 | } |
| 192 | return DEBUG_FileSubNr2StabEnum(filenr,subnr); |
| 193 | } |
| 194 | |
| 195 | static |
| 196 | int |
| 197 | DEBUG_ReadTypeEnum(char **x) { |
| 198 | int filenr,subnr; |
| 199 | |
| 200 | if (**x=='(') { |
| 201 | (*x)++; /* '(' */ |
| 202 | filenr=strtol(*x,x,10); /* <int> */ |
| 203 | (*x)++; /* ',' */ |
| 204 | subnr=strtol(*x,x,10); /* <int> */ |
| 205 | (*x)++; /* ')' */ |
| 206 | } else { |
| 207 | filenr = 0; |
| 208 | subnr = strtol(*x,x,10); /* <int> */ |
| 209 | } |
| 210 | return DEBUG_FileSubNr2StabEnum(filenr,subnr); |
| 211 | } |
| 212 | |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 213 | static |
| 214 | int |
| 215 | DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef) |
| 216 | { |
| 217 | int hash; |
| 218 | struct known_typedef * ktd; |
| 219 | |
| 220 | if( ndef == 1 ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 221 | return TRUE; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 222 | |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 223 | ktd = (struct known_typedef *) DBG_alloc(sizeof(struct known_typedef) |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 224 | + ndef * sizeof(struct datatype *)); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 225 | |
| 226 | hash = stab_hash(name); |
| 227 | |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 228 | ktd->name = DBG_strdup(name); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 229 | ktd->ndefs = ndef; |
| 230 | memcpy(&ktd->types[0], types, ndef * sizeof(struct datatype *)); |
| 231 | ktd->next = ktd_head[hash]; |
| 232 | ktd_head[hash] = ktd; |
| 233 | |
| 234 | return TRUE; |
| 235 | } |
| 236 | |
| 237 | static |
| 238 | int |
| 239 | DEBUG_HandlePreviousTypedef(const char * name, const char * stab) |
| 240 | { |
| 241 | int count; |
| 242 | enum debug_type expect; |
| 243 | int hash; |
| 244 | struct known_typedef * ktd; |
| 245 | char * ptr; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 246 | |
| 247 | hash = stab_hash(name); |
| 248 | |
| 249 | for(ktd = ktd_head[hash]; ktd; ktd = ktd->next) |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 250 | if ((ktd->name[0] == name[0]) && (strcmp(name, ktd->name) == 0) ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 251 | break; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 252 | |
| 253 | /* |
| 254 | * Didn't find it. This must be a new one. |
| 255 | */ |
| 256 | if( ktd == NULL ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 257 | return FALSE; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 258 | |
| 259 | /* |
| 260 | * Examine the stab to make sure it has the same number of definitions. |
| 261 | */ |
| 262 | count = 0; |
| 263 | for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '=')) |
| 264 | { |
| 265 | if( count >= ktd->ndefs ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 266 | return FALSE; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * Make sure the types of all of the objects is consistent with |
| 270 | * what we have already parsed. |
| 271 | */ |
| 272 | switch(ptr[1]) |
| 273 | { |
| 274 | case '*': |
François Gouget | 241c730 | 1998-10-28 10:47:09 +0000 | [diff] [blame] | 275 | expect = DT_POINTER; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 276 | break; |
| 277 | case 's': |
| 278 | case 'u': |
François Gouget | 241c730 | 1998-10-28 10:47:09 +0000 | [diff] [blame] | 279 | expect = DT_STRUCT; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 280 | break; |
| 281 | case 'a': |
François Gouget | 241c730 | 1998-10-28 10:47:09 +0000 | [diff] [blame] | 282 | expect = DT_ARRAY; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 283 | break; |
| 284 | case '1': |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 285 | case '(': |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 286 | case 'r': |
François Gouget | 241c730 | 1998-10-28 10:47:09 +0000 | [diff] [blame] | 287 | expect = DT_BASIC; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 288 | break; |
| 289 | case 'x': |
François Gouget | 241c730 | 1998-10-28 10:47:09 +0000 | [diff] [blame] | 290 | expect = DT_STRUCT; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 291 | break; |
| 292 | case 'e': |
François Gouget | 241c730 | 1998-10-28 10:47:09 +0000 | [diff] [blame] | 293 | expect = DT_ENUM; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 294 | break; |
| 295 | case 'f': |
François Gouget | 241c730 | 1998-10-28 10:47:09 +0000 | [diff] [blame] | 296 | expect = DT_FUNC; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 297 | break; |
| 298 | default: |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 299 | fprintf(stderr, "Unknown type (%c).\n",ptr[1]); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 300 | return FALSE; |
| 301 | } |
| 302 | if( expect != DEBUG_GetType(ktd->types[count]) ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 303 | return FALSE; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 304 | count++; |
| 305 | } |
| 306 | |
| 307 | if( ktd->ndefs != count ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 308 | return FALSE; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 309 | |
| 310 | /* |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 311 | * Go through, dig out all of the type numbers, and substitute the |
| 312 | * appropriate things. |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 313 | */ |
| 314 | count = 0; |
| 315 | for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '=')) |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 316 | stab_types[DEBUG_ReadTypeEnumBackwards(ptr-1)] = ktd->types[count++]; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 317 | |
| 318 | return TRUE; |
| 319 | } |
| 320 | |
| 321 | static int DEBUG_FreeRegisteredTypedefs() |
| 322 | { |
| 323 | int count; |
| 324 | int j; |
| 325 | struct known_typedef * ktd; |
| 326 | struct known_typedef * next; |
| 327 | |
| 328 | count = 0; |
| 329 | for(j=0; j < NR_STAB_HASH; j++ ) |
| 330 | { |
| 331 | for(ktd = ktd_head[j]; ktd; ktd = next) |
| 332 | { |
| 333 | count++; |
| 334 | next = ktd->next; |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 335 | DBG_free(ktd->name); |
| 336 | DBG_free(ktd); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 337 | } |
| 338 | ktd_head[j] = NULL; |
| 339 | } |
| 340 | |
| 341 | return TRUE; |
| 342 | |
| 343 | } |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 344 | |
| 345 | static |
| 346 | int |
| 347 | DEBUG_ParseTypedefStab(char * ptr, const char * typename) |
| 348 | { |
| 349 | int arrmax; |
| 350 | int arrmin; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 351 | char * c; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 352 | struct datatype * curr_type; |
| 353 | struct datatype * datatype; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 354 | struct datatype * curr_types[MAX_TD_NESTING]; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 355 | char element_name[1024]; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 356 | int ntypes = 0; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 357 | int offset; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 358 | const char * orig_typename; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 359 | int size; |
| 360 | char * tc; |
| 361 | char * tc2; |
| 362 | int typenum; |
| 363 | |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 364 | orig_typename = typename; |
| 365 | |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 366 | if( DEBUG_HandlePreviousTypedef(typename, ptr) ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 367 | return TRUE; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 368 | |
| 369 | /* |
| 370 | * Go from back to front. First we go through and figure out what |
| 371 | * type numbers we need, and register those types. Then we go in |
| 372 | * and fill the details. |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 373 | */ |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 374 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 375 | for( c = strchr(ptr, '='); c != NULL; c = strchr(c + 1, '=') ) |
| 376 | { |
| 377 | /* |
| 378 | * Back up until we get to a non-numeric character. This is the type |
| 379 | * number. |
| 380 | */ |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 381 | typenum = DEBUG_ReadTypeEnumBackwards(c-1); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 382 | |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 383 | if( ntypes >= MAX_TD_NESTING ) |
| 384 | { |
| 385 | /* |
| 386 | * If this ever happens, just bump the counter. |
| 387 | */ |
| 388 | fprintf(stderr, "Typedef nesting overflow\n"); |
| 389 | return FALSE; |
| 390 | } |
| 391 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 392 | switch(c[1]) |
| 393 | { |
| 394 | case '*': |
François Gouget | 241c730 | 1998-10-28 10:47:09 +0000 | [diff] [blame] | 395 | stab_types[typenum] = DEBUG_NewDataType(DT_POINTER, NULL); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 396 | curr_types[ntypes++] = stab_types[typenum]; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 397 | break; |
| 398 | case 's': |
| 399 | case 'u': |
François Gouget | 241c730 | 1998-10-28 10:47:09 +0000 | [diff] [blame] | 400 | stab_types[typenum] = DEBUG_NewDataType(DT_STRUCT, typename); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 401 | curr_types[ntypes++] = stab_types[typenum]; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 402 | break; |
| 403 | case 'a': |
François Gouget | 241c730 | 1998-10-28 10:47:09 +0000 | [diff] [blame] | 404 | stab_types[typenum] = DEBUG_NewDataType(DT_ARRAY, NULL); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 405 | curr_types[ntypes++] = stab_types[typenum]; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 406 | break; |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 407 | case '(': |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 408 | case '1': |
| 409 | case 'r': |
François Gouget | 241c730 | 1998-10-28 10:47:09 +0000 | [diff] [blame] | 410 | stab_types[typenum] = DEBUG_NewDataType(DT_BASIC, typename); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 411 | curr_types[ntypes++] = stab_types[typenum]; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 412 | break; |
| 413 | case 'x': |
| 414 | stab_strcpy(element_name, c + 3); |
François Gouget | 241c730 | 1998-10-28 10:47:09 +0000 | [diff] [blame] | 415 | stab_types[typenum] = DEBUG_NewDataType(DT_STRUCT, element_name); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 416 | curr_types[ntypes++] = stab_types[typenum]; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 417 | break; |
| 418 | case 'e': |
François Gouget | 241c730 | 1998-10-28 10:47:09 +0000 | [diff] [blame] | 419 | stab_types[typenum] = DEBUG_NewDataType(DT_ENUM, NULL); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 420 | curr_types[ntypes++] = stab_types[typenum]; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 421 | break; |
| 422 | case 'f': |
François Gouget | 241c730 | 1998-10-28 10:47:09 +0000 | [diff] [blame] | 423 | stab_types[typenum] = DEBUG_NewDataType(DT_FUNC, NULL); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 424 | curr_types[ntypes++] = stab_types[typenum]; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 425 | break; |
| 426 | default: |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 427 | fprintf(stderr, "Unknown type (%c).\n",c[1]); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 428 | } |
| 429 | typename = NULL; |
| 430 | } |
| 431 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 432 | /* |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 433 | * Now register the type so that if we encounter it again, we will know |
| 434 | * what to do. |
| 435 | */ |
| 436 | DEBUG_RegisterTypedef(orig_typename, curr_types, ntypes); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 437 | |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 438 | /* |
| 439 | * OK, now take a second sweep through. Now we will be digging |
| 440 | * out the definitions of the various components, and storing |
| 441 | * them in the skeletons that we have already allocated. We take |
| 442 | * a right-to left search as this is much easier to parse. |
| 443 | */ |
| 444 | for( c = strrchr(ptr, '='); c != NULL; c = strrchr(ptr, '=') ) |
| 445 | { |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 446 | int typenum = DEBUG_ReadTypeEnumBackwards(c-1); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 447 | curr_type = stab_types[typenum]; |
| 448 | |
| 449 | switch(c[1]) |
| 450 | { |
| 451 | case 'x': |
| 452 | tc = c + 3; |
| 453 | while( *tc != ':' ) |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 454 | tc++; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 455 | tc++; |
| 456 | if( *tc == '\0' ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 457 | *c = '\0'; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 458 | else |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 459 | strcpy(c, tc); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 460 | break; |
| 461 | case '*': |
| 462 | case 'f': |
| 463 | tc = c + 2; |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 464 | datatype = stab_types[DEBUG_ReadTypeEnum(&tc)]; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 465 | DEBUG_SetPointerType(curr_type, datatype); |
| 466 | if( *tc == '\0' ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 467 | *c = '\0'; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 468 | else |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 469 | strcpy(c, tc); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 470 | break; |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 471 | case '(': |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 472 | case '1': |
| 473 | case 'r': |
| 474 | /* |
| 475 | * We have already handled these above. |
| 476 | */ |
| 477 | *c = '\0'; |
| 478 | break; |
| 479 | case 'a': |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 480 | /* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */ |
| 481 | |
| 482 | tc = c + 3; |
Alexandre Julliard | f90efa9 | 1998-06-14 15:24:15 +0000 | [diff] [blame] | 483 | /* 'r' */ |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 484 | DEBUG_ReadTypeEnum(&tc); |
Alexandre Julliard | f90efa9 | 1998-06-14 15:24:15 +0000 | [diff] [blame] | 485 | tc++; /* ';' */ |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 486 | arrmin = strtol(tc, &tc, 10); /* <int> */ |
Alexandre Julliard | f90efa9 | 1998-06-14 15:24:15 +0000 | [diff] [blame] | 487 | tc++; /* ';' */ |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 488 | arrmax = strtol(tc, &tc, 10); /* <int> */ |
Alexandre Julliard | f90efa9 | 1998-06-14 15:24:15 +0000 | [diff] [blame] | 489 | tc++; /* ';' */ |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 490 | datatype = stab_types[DEBUG_ReadTypeEnum(&tc)]; /* <typeinfo> */ |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 491 | if( *tc == '\0' ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 492 | *c = '\0'; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 493 | else |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 494 | strcpy(c, tc); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 495 | DEBUG_SetArrayParams(curr_type, arrmin, arrmax, datatype); |
| 496 | break; |
| 497 | case 's': |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 498 | case 'u': { |
| 499 | int failure = 0; |
| 500 | |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 501 | tc = c + 2; |
| 502 | if( DEBUG_SetStructSize(curr_type, strtol(tc, &tc, 10)) == FALSE ) |
| 503 | { |
| 504 | /* |
| 505 | * We have already filled out this structure. Nothing to do, |
| 506 | * so just skip forward to the end of the definition. |
| 507 | */ |
| 508 | while( tc[0] != ';' && tc[1] != ';' ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 509 | tc++; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 510 | |
| 511 | tc += 2; |
| 512 | |
| 513 | if( *tc == '\0' ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 514 | *c = '\0'; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 515 | else |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 516 | strcpy(c, tc + 1); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 517 | continue; |
| 518 | } |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 519 | |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 520 | /* |
| 521 | * Now parse the individual elements of the structure/union. |
| 522 | */ |
| 523 | while(*tc != ';') |
| 524 | { |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 525 | char *ti; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 526 | tc2 = element_name; |
| 527 | while(*tc != ':') |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 528 | *tc2++ = *tc++; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 529 | tc++; |
| 530 | *tc2++ = '\0'; |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 531 | ti=tc; |
| 532 | datatype = stab_types[DEBUG_ReadTypeEnum(&tc)]; |
| 533 | *tc='\0'; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 534 | tc++; |
| 535 | offset = strtol(tc, &tc, 10); |
| 536 | tc++; |
| 537 | size = strtol(tc, &tc, 10); |
| 538 | tc++; |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 539 | if (datatype) |
| 540 | DEBUG_AddStructElement(curr_type, element_name, datatype, offset, size); |
| 541 | else { |
| 542 | failure = 1; |
| 543 | /* ... but proceed parsing to the end of the stab */ |
| 544 | } |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 545 | } |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 546 | |
| 547 | if (failure) { |
| 548 | /* if we had a undeclared value this one is undeclared too. |
| 549 | * remove it from the stab_types. |
| 550 | * I just set it to NULL to detect bugs in my thoughtprocess. |
| 551 | * FIXME: leaks the memory for the structure elements. |
| 552 | * FIXME: such structures should have been optimized away |
| 553 | * by ld. |
| 554 | */ |
| 555 | stab_types[typenum] = NULL; |
| 556 | } |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 557 | if( *tc == '\0' ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 558 | *c = '\0'; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 559 | else |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 560 | strcpy(c, tc + 1); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 561 | break; |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 562 | } |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 563 | case 'e': |
| 564 | tc = c + 2; |
| 565 | /* |
| 566 | * Now parse the individual elements of the structure/union. |
| 567 | */ |
| 568 | while(*tc != ';') |
| 569 | { |
| 570 | tc2 = element_name; |
| 571 | while(*tc != ':') |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 572 | *tc2++ = *tc++; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 573 | tc++; |
| 574 | *tc2++ = '\0'; |
| 575 | offset = strtol(tc, &tc, 10); |
| 576 | tc++; |
| 577 | DEBUG_AddStructElement(curr_type, element_name, NULL, offset, 0); |
| 578 | } |
| 579 | if( *tc == '\0' ) |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 580 | *c = '\0'; |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 581 | else |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 582 | strcpy(c, tc + 1); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 583 | break; |
| 584 | default: |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 585 | fprintf(stderr, "Unknown type (%c).\n",c[1]); |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 586 | break; |
| 587 | } |
| 588 | } |
| 589 | |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 590 | return TRUE; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 591 | |
| 592 | } |
| 593 | |
| 594 | static struct datatype * |
| 595 | DEBUG_ParseStabType(const char * stab) |
| 596 | { |
| 597 | char * c; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 598 | |
| 599 | /* |
| 600 | * Look through the stab definition, and figure out what datatype |
| 601 | * this represents. If we have something we know about, assign the |
| 602 | * type. |
| 603 | */ |
| 604 | c = strchr(stab, ':'); |
| 605 | if( c == NULL ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 606 | return NULL; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 607 | |
| 608 | c++; |
| 609 | /* |
| 610 | * The next character says more about the type (i.e. data, function, etc) |
| 611 | * of symbol. Skip it. |
| 612 | */ |
| 613 | c++; |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 614 | /* |
| 615 | * The next is either an integer or a (integer,integer). |
| 616 | * The DEBUG_ReadTypeEnum takes care that stab_types is large enough. |
| 617 | */ |
| 618 | return stab_types[DEBUG_ReadTypeEnum(&c)]; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 621 | int |
| 622 | DEBUG_ParseStabs(char * addr, unsigned int load_offset, |
| 623 | unsigned int staboff, int stablen, |
| 624 | unsigned int strtaboff, int strtablen) |
| 625 | { |
| 626 | struct name_hash * curr_func = NULL; |
| 627 | struct wine_locals * curr_loc = NULL; |
| 628 | struct name_hash * curr_sym = NULL; |
| 629 | char currpath[PATH_MAX]; |
| 630 | int i; |
| 631 | int ignore = FALSE; |
| 632 | int last_nso = -1; |
| 633 | int len; |
| 634 | DBG_ADDR new_addr; |
| 635 | int nstab; |
| 636 | char * ptr; |
| 637 | char * stabbuff; |
| 638 | int stabbufflen; |
| 639 | struct stab_nlist * stab_ptr; |
| 640 | char * strs; |
| 641 | int strtabinc; |
| 642 | char * subpath = NULL; |
| 643 | char symname[4096]; |
| 644 | |
| 645 | nstab = stablen / sizeof(struct stab_nlist); |
| 646 | stab_ptr = (struct stab_nlist *) (addr + staboff); |
| 647 | strs = (char *) (addr + strtaboff); |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 648 | |
| 649 | memset(currpath, 0, sizeof(currpath)); |
| 650 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 651 | /* |
| 652 | * Allocate a buffer into which we can build stab strings for cases |
| 653 | * where the stab is continued over multiple lines. |
| 654 | */ |
| 655 | stabbufflen = 65536; |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 656 | stabbuff = (char *) DBG_alloc(stabbufflen); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 657 | |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 658 | strtabinc = 0; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 659 | stabbuff[0] = '\0'; |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 660 | for(i=0; i < nstab; i++, stab_ptr++ ) |
| 661 | { |
| 662 | ptr = strs + (unsigned int) stab_ptr->n_un.n_name; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 663 | if( ptr[strlen(ptr) - 1] == '\\' ) |
| 664 | { |
| 665 | /* |
| 666 | * Indicates continuation. Append this to the buffer, and go onto the |
| 667 | * next record. Repeat the process until we find a stab without the |
| 668 | * '/' character, as this indicates we have the whole thing. |
| 669 | */ |
| 670 | len = strlen(ptr); |
| 671 | if( strlen(stabbuff) + len > stabbufflen ) |
| 672 | { |
| 673 | stabbufflen += 65536; |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 674 | stabbuff = (char *) DBG_realloc(stabbuff, stabbufflen); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 675 | } |
| 676 | strncat(stabbuff, ptr, len - 1); |
| 677 | continue; |
| 678 | } |
| 679 | else if( stabbuff[0] != '\0' ) |
| 680 | { |
| 681 | strcat( stabbuff, ptr); |
| 682 | ptr = stabbuff; |
| 683 | } |
| 684 | |
| 685 | if( strchr(ptr, '=') != NULL ) |
| 686 | { |
| 687 | /* |
| 688 | * The stabs aren't in writable memory, so copy it over so we are |
| 689 | * sure we can scribble on it. |
| 690 | */ |
| 691 | if( ptr != stabbuff ) |
| 692 | { |
| 693 | strcpy(stabbuff, ptr); |
| 694 | ptr = stabbuff; |
| 695 | } |
| 696 | stab_strcpy(symname, ptr); |
| 697 | DEBUG_ParseTypedefStab(ptr, symname); |
| 698 | } |
| 699 | |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 700 | switch(stab_ptr->n_type) |
| 701 | { |
| 702 | case N_GSYM: |
| 703 | /* |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 704 | * These are useless with ELF. They have no value, and you have to |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 705 | * read the normal symbol table to get the address. Thus we |
| 706 | * ignore them, and when we process the normal symbol table |
| 707 | * we should do the right thing. |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 708 | * |
| 709 | * With a.out, they actually do make some amount of sense. |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 710 | */ |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 711 | new_addr.seg = 0; |
| 712 | new_addr.type = DEBUG_ParseStabType(ptr); |
| 713 | new_addr.off = load_offset + stab_ptr->n_value; |
| 714 | |
| 715 | stab_strcpy(symname, ptr); |
| 716 | #ifdef __ELF__ |
| 717 | curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath, |
| 718 | SYM_WINE | SYM_DATA | SYM_INVALID); |
| 719 | #else |
| 720 | curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath, |
| 721 | SYM_WINE | SYM_DATA ); |
| 722 | #endif |
| 723 | break; |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 724 | case N_RBRAC: |
| 725 | case N_LBRAC: |
| 726 | /* |
| 727 | * We need to keep track of these so we get symbol scoping |
| 728 | * right for local variables. For now, we just ignore them. |
| 729 | * The hooks are already there for dealing with this however, |
| 730 | * so all we need to do is to keep count of the nesting level, |
| 731 | * and find the RBRAC for each matching LBRAC. |
| 732 | */ |
| 733 | break; |
| 734 | case N_LCSYM: |
| 735 | case N_STSYM: |
| 736 | /* |
| 737 | * These are static symbols and BSS symbols. |
| 738 | */ |
| 739 | new_addr.seg = 0; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 740 | new_addr.type = DEBUG_ParseStabType(ptr); |
| 741 | new_addr.off = load_offset + stab_ptr->n_value; |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 742 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 743 | stab_strcpy(symname, ptr); |
| 744 | curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath, |
| 745 | SYM_WINE | SYM_DATA ); |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 746 | break; |
| 747 | case N_PSYM: |
| 748 | /* |
| 749 | * These are function parameters. |
| 750 | */ |
| 751 | if( (curr_func != NULL) |
| 752 | && (stab_ptr->n_value != 0) ) |
| 753 | { |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 754 | stab_strcpy(symname, ptr); |
| 755 | curr_loc = DEBUG_AddLocal(curr_func, 0, |
| 756 | stab_ptr->n_value, 0, 0, symname); |
| 757 | DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr)); |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 758 | } |
| 759 | break; |
| 760 | case N_RSYM: |
| 761 | if( curr_func != NULL ) |
| 762 | { |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 763 | stab_strcpy(symname, ptr); |
| 764 | curr_loc = DEBUG_AddLocal(curr_func, stab_ptr->n_value, 0, 0, 0, symname); |
| 765 | DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr)); |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 766 | } |
| 767 | break; |
| 768 | case N_LSYM: |
| 769 | if( (curr_func != NULL) |
| 770 | && (stab_ptr->n_value != 0) ) |
| 771 | { |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 772 | stab_strcpy(symname, ptr); |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 773 | DEBUG_AddLocal(curr_func, 0, |
| 774 | stab_ptr->n_value, 0, 0, symname); |
| 775 | } |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 776 | else if (curr_func == NULL) |
| 777 | { |
| 778 | stab_strcpy(symname, ptr); |
| 779 | } |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 780 | break; |
| 781 | case N_SLINE: |
| 782 | /* |
| 783 | * This is a line number. These are always relative to the start |
| 784 | * of the function (N_FUN), and this makes the lookup easier. |
| 785 | */ |
| 786 | if( curr_func != NULL ) |
| 787 | { |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 788 | #ifdef __ELF__ |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 789 | DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc, |
| 790 | stab_ptr->n_value); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 791 | #else |
| 792 | #if 0 |
| 793 | /* |
| 794 | * This isn't right. The order of the stabs is different under |
| 795 | * a.out, and as a result we would end up attaching the line |
| 796 | * number to the wrong function. |
| 797 | */ |
| 798 | DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc, |
| 799 | stab_ptr->n_value - curr_func->addr.off); |
| 800 | #endif |
| 801 | #endif |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 802 | } |
| 803 | break; |
| 804 | case N_FUN: |
| 805 | /* |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 806 | * First, clean up the previous function we were working on. |
| 807 | */ |
| 808 | DEBUG_Normalize(curr_func); |
| 809 | |
| 810 | /* |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 811 | * For now, just declare the various functions. Later |
| 812 | * on, we will add the line number information and the |
| 813 | * local symbols. |
| 814 | */ |
| 815 | if( !ignore ) |
| 816 | { |
| 817 | new_addr.seg = 0; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 818 | new_addr.type = DEBUG_ParseStabType(ptr); |
| 819 | new_addr.off = load_offset + stab_ptr->n_value; |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 820 | /* |
| 821 | * Copy the string to a temp buffer so we |
| 822 | * can kill everything after the ':'. We do |
| 823 | * it this way because otherwise we end up dirtying |
| 824 | * all of the pages related to the stabs, and that |
| 825 | * sucks up swap space like crazy. |
| 826 | */ |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 827 | stab_strcpy(symname, ptr); |
| 828 | curr_func = DEBUG_AddSymbol( symname, &new_addr, currpath, |
| 829 | SYM_WINE | SYM_FUNC); |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 830 | } |
| 831 | else |
| 832 | { |
| 833 | /* |
| 834 | * Don't add line number information for this function |
| 835 | * any more. |
| 836 | */ |
| 837 | curr_func = NULL; |
| 838 | } |
| 839 | break; |
| 840 | case N_SO: |
| 841 | /* |
| 842 | * This indicates a new source file. Append the records |
| 843 | * together, to build the correct path name. |
| 844 | */ |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 845 | #ifndef __ELF__ |
| 846 | /* |
| 847 | * With a.out, there is no NULL string N_SO entry at the end of |
| 848 | * the file. Thus when we find non-consecutive entries, |
| 849 | * we consider that a new file is started. |
| 850 | */ |
| 851 | if( last_nso < i-1 ) |
| 852 | { |
| 853 | currpath[0] = '\0'; |
| 854 | DEBUG_Normalize(curr_func); |
| 855 | curr_func = NULL; |
| 856 | } |
| 857 | #endif |
| 858 | |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 859 | if( *ptr == '\0' ) |
| 860 | { |
| 861 | /* |
| 862 | * Nuke old path. |
| 863 | */ |
| 864 | currpath[0] = '\0'; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 865 | DEBUG_Normalize(curr_func); |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 866 | curr_func = NULL; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 867 | /* |
| 868 | * The datatypes that we would need to use are reset when |
| 869 | * we start a new file. |
| 870 | */ |
Alexandre Julliard | a845b88 | 1998-06-01 10:44:35 +0000 | [diff] [blame] | 871 | memset(stab_types, 0, num_stab_types * sizeof(stab_types[0])); |
| 872 | /* |
| 873 | for (i=0;i<nrofnroftypenums;i++) |
| 874 | memset(typenums[i],0,sizeof(typenums[i][0])*nroftypenums[i]); |
| 875 | */ |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 876 | } |
| 877 | else |
| 878 | { |
Alexandre Julliard | 349a953 | 1997-02-02 19:01:52 +0000 | [diff] [blame] | 879 | if (*ptr != '/') |
| 880 | strcat(currpath, ptr); |
| 881 | else |
| 882 | strcpy(currpath, ptr); |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 883 | subpath = ptr; |
| 884 | } |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 885 | last_nso = i; |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 886 | break; |
| 887 | case N_SOL: |
| 888 | /* |
| 889 | * This indicates we are including stuff from an include file. |
| 890 | * If this is the main source, enable the debug stuff, otherwise |
| 891 | * ignore it. |
| 892 | */ |
| 893 | if( subpath == NULL || strcmp(ptr, subpath) == 0 ) |
| 894 | { |
| 895 | ignore = FALSE; |
| 896 | } |
| 897 | else |
| 898 | { |
| 899 | ignore = TRUE; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 900 | DEBUG_Normalize(curr_func); |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 901 | curr_func = NULL; |
| 902 | } |
| 903 | break; |
| 904 | case N_UNDF: |
| 905 | strs += strtabinc; |
| 906 | strtabinc = stab_ptr->n_value; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 907 | DEBUG_Normalize(curr_func); |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 908 | curr_func = NULL; |
| 909 | break; |
| 910 | case N_OPT: |
| 911 | /* |
| 912 | * Ignore this. We don't care what it points to. |
| 913 | */ |
| 914 | break; |
| 915 | case N_BINCL: |
| 916 | case N_EINCL: |
| 917 | case N_MAIN: |
| 918 | /* |
| 919 | * Always ignore these. GCC doesn't even generate them. |
| 920 | */ |
| 921 | break; |
| 922 | default: |
| 923 | break; |
| 924 | } |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 925 | |
| 926 | stabbuff[0] = '\0'; |
| 927 | |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 928 | #if 0 |
| 929 | fprintf(stderr, "%d %x %s\n", stab_ptr->n_type, |
| 930 | (unsigned int) stab_ptr->n_value, |
| 931 | strs + (unsigned int) stab_ptr->n_un.n_name); |
| 932 | #endif |
| 933 | } |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 934 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 935 | if( stab_types != NULL ) |
| 936 | { |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 937 | DBG_free(stab_types); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 938 | stab_types = NULL; |
| 939 | num_stab_types = 0; |
| 940 | } |
| 941 | |
Alexandre Julliard | 01d6346 | 1997-01-20 19:43:45 +0000 | [diff] [blame] | 942 | |
| 943 | DEBUG_FreeRegisteredTypedefs(); |
| 944 | |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 945 | return TRUE; |
| 946 | } |
| 947 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 948 | #ifdef __ELF__ |
| 949 | |
| 950 | /* |
| 951 | * Walk through the entire symbol table and add any symbols we find there. |
| 952 | * This can be used in cases where we have stripped ELF shared libraries, |
| 953 | * or it can be used in cases where we have data symbols for which the address |
| 954 | * isn't encoded in the stabs. |
| 955 | * |
| 956 | * This is all really quite easy, since we don't have to worry about line |
| 957 | * numbers or local data variables. |
| 958 | */ |
| 959 | static |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 960 | int |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 961 | DEBUG_ProcessElfSymtab(char * addr, unsigned int load_offset, |
| 962 | Elf32_Shdr * symtab, Elf32_Shdr * strtab) |
| 963 | { |
| 964 | char * curfile = NULL; |
| 965 | struct name_hash * curr_sym = NULL; |
| 966 | int flags; |
| 967 | int i; |
| 968 | DBG_ADDR new_addr; |
| 969 | int nsym; |
| 970 | char * strp; |
| 971 | char * symname; |
| 972 | Elf32_Sym * symp; |
| 973 | |
| 974 | |
| 975 | symp = (Elf32_Sym *) (addr + symtab->sh_offset); |
| 976 | nsym = symtab->sh_size / sizeof(*symp); |
| 977 | strp = (char *) (addr + strtab->sh_offset); |
| 978 | |
| 979 | for(i=0; i < nsym; i++, symp++) |
| 980 | { |
| 981 | /* |
| 982 | * Ignore certain types of entries which really aren't of that much |
| 983 | * interest. |
| 984 | */ |
| 985 | if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION ) |
| 986 | { |
| 987 | continue; |
| 988 | } |
| 989 | |
| 990 | symname = strp + symp->st_name; |
| 991 | |
| 992 | /* |
| 993 | * Save the name of the current file, so we have a way of tracking |
| 994 | * static functions/data. |
| 995 | */ |
| 996 | if( ELF32_ST_TYPE(symp->st_info) == STT_FILE ) |
| 997 | { |
| 998 | curfile = symname; |
| 999 | continue; |
| 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | /* |
| 1004 | * See if we already have something for this symbol. |
| 1005 | * If so, ignore this entry, because it would have come from the |
| 1006 | * stabs or from a previous symbol. If the value is different, |
| 1007 | * we will have to keep the darned thing, because there can be |
| 1008 | * multiple local symbols by the same name. |
| 1009 | */ |
| 1010 | if( (DEBUG_GetSymbolValue(symname, -1, &new_addr, FALSE ) == TRUE) |
| 1011 | && (new_addr.off == (load_offset + symp->st_value)) ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1012 | continue; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1013 | |
| 1014 | new_addr.seg = 0; |
| 1015 | new_addr.type = NULL; |
| 1016 | new_addr.off = load_offset + symp->st_value; |
| 1017 | flags = SYM_WINE | (ELF32_ST_BIND(symp->st_info) == STT_FUNC |
| 1018 | ? SYM_FUNC : SYM_DATA); |
| 1019 | if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1020 | curr_sym = DEBUG_AddSymbol( symname, &new_addr, NULL, flags ); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1021 | else |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1022 | curr_sym = DEBUG_AddSymbol( symname, &new_addr, curfile, flags ); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1023 | |
| 1024 | /* |
| 1025 | * Record the size of the symbol. This can come in handy in |
| 1026 | * some cases. Not really used yet, however. |
| 1027 | */ |
| 1028 | if( symp->st_size != 0 ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1029 | DEBUG_SetSymbolSize(curr_sym, symp->st_size); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | return TRUE; |
| 1033 | } |
| 1034 | |
| 1035 | static |
| 1036 | int |
| 1037 | DEBUG_ProcessElfObject(char * filename, unsigned int load_offset) |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1038 | { |
| 1039 | int rtn = FALSE; |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1040 | struct stat statbuf; |
| 1041 | int fd = -1; |
| 1042 | int status; |
| 1043 | char * addr = (char *) 0xffffffff; |
| 1044 | Elf32_Ehdr * ehptr; |
| 1045 | Elf32_Shdr * spnt; |
| 1046 | char * shstrtab; |
| 1047 | int nsect; |
| 1048 | int i; |
| 1049 | int stabsect; |
| 1050 | int stabstrsect; |
| 1051 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1052 | |
| 1053 | /* |
| 1054 | * Make sure we can stat and open this file. |
| 1055 | */ |
| 1056 | if( filename == NULL ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1057 | goto leave; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1058 | |
| 1059 | status = stat(filename, &statbuf); |
| 1060 | if( status == -1 ) |
| 1061 | { |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1062 | char *s,*t,*fn,*paths; |
| 1063 | if (strchr(filename,'/')) |
| 1064 | goto leave; |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 1065 | paths = DBG_strdup(getenv("PATH")); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1066 | s = paths; |
| 1067 | while (s && *s) { |
| 1068 | t = strchr(s,':'); |
| 1069 | if (t) *t='\0'; |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 1070 | fn = (char*)DBG_alloc(strlen(filename)+1+strlen(s)+1); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1071 | strcpy(fn,s); |
| 1072 | strcat(fn,"/"); |
| 1073 | strcat(fn,filename); |
| 1074 | if ((rtn = DEBUG_ProcessElfObject(fn,load_offset))) { |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 1075 | DBG_free(fn); |
| 1076 | DBG_free(paths); |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1077 | goto leave; |
| 1078 | } |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 1079 | DBG_free(fn); |
Alexandre Julliard | 85ed45e | 1998-08-22 19:03:56 +0000 | [diff] [blame] | 1080 | if (t) s = t+1; else break; |
Alexandre Julliard | 77b9918 | 1997-09-14 17:17:23 +0000 | [diff] [blame] | 1081 | } |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1082 | if (!s || !*s) fprintf(stderr," not found"); |
Ove Kaaven | dda17c6 | 1999-04-25 12:24:42 +0000 | [diff] [blame] | 1083 | DBG_free(paths); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1084 | goto leave; |
| 1085 | } |
| 1086 | |
| 1087 | /* |
| 1088 | * Now open the file, so that we can mmap() it. |
| 1089 | */ |
| 1090 | fd = open(filename, O_RDONLY); |
| 1091 | if( fd == -1 ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1092 | goto leave; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1093 | |
| 1094 | |
| 1095 | /* |
| 1096 | * Now mmap() the file. |
| 1097 | */ |
| 1098 | addr = mmap(0, statbuf.st_size, PROT_READ, |
| 1099 | MAP_PRIVATE, fd, 0); |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 1100 | if( addr == (char *) 0xffffffff ) |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 1101 | goto leave; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1102 | |
| 1103 | /* |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1104 | * Next, we need to find a few of the internal ELF headers within |
| 1105 | * this thing. We need the main executable header, and the section |
| 1106 | * table. |
| 1107 | */ |
| 1108 | ehptr = (Elf32_Ehdr *) addr; |
| 1109 | |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 1110 | if( load_offset == 0 ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1111 | DEBUG_RegisterELFDebugInfo(ehptr->e_entry, statbuf.st_size, filename); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1112 | else |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1113 | DEBUG_RegisterELFDebugInfo(load_offset, statbuf.st_size, filename); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1114 | |
| 1115 | spnt = (Elf32_Shdr *) (addr + ehptr->e_shoff); |
| 1116 | nsect = ehptr->e_shnum; |
| 1117 | shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset); |
| 1118 | |
| 1119 | stabsect = stabstrsect = -1; |
| 1120 | |
| 1121 | for(i=0; i < nsect; i++) |
| 1122 | { |
| 1123 | if( strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0 ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1124 | stabsect = i; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1125 | |
| 1126 | if( strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0 ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1127 | stabstrsect = i; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | if( stabsect == -1 || stabstrsect == -1 ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1131 | goto leave; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1132 | |
| 1133 | /* |
| 1134 | * OK, now just parse all of the stabs. |
| 1135 | */ |
| 1136 | rtn = DEBUG_ParseStabs(addr, load_offset, |
| 1137 | spnt[stabsect].sh_offset, |
| 1138 | spnt[stabsect].sh_size, |
| 1139 | spnt[stabstrsect].sh_offset, |
| 1140 | spnt[stabstrsect].sh_size); |
| 1141 | |
| 1142 | if( rtn != TRUE ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1143 | goto leave; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1144 | |
| 1145 | for(i=0; i < nsect; i++) |
| 1146 | { |
| 1147 | if( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0) |
| 1148 | && (spnt[i].sh_type == SHT_SYMTAB) ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1149 | DEBUG_ProcessElfSymtab(addr, load_offset, |
| 1150 | spnt + i, spnt + spnt[i].sh_link); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1151 | |
| 1152 | if( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0) |
| 1153 | && (spnt[i].sh_type == SHT_DYNSYM) ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1154 | DEBUG_ProcessElfSymtab(addr, load_offset, |
| 1155 | spnt + i, spnt + spnt[i].sh_link); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | leave: |
| 1159 | |
| 1160 | if( addr != (char *) 0xffffffff ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1161 | munmap(addr, statbuf.st_size); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1162 | |
| 1163 | if( fd != -1 ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1164 | close(fd); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1165 | |
| 1166 | return (rtn); |
| 1167 | |
| 1168 | } |
| 1169 | |
| 1170 | int |
| 1171 | DEBUG_ReadExecutableDbgInfo(void) |
| 1172 | { |
| 1173 | Elf32_Ehdr * ehdr; |
| 1174 | char * exe_name; |
| 1175 | Elf32_Dyn * dynpnt; |
| 1176 | struct r_debug * dbg_hdr; |
| 1177 | struct link_map * lpnt = NULL; |
| 1178 | extern Elf32_Dyn _DYNAMIC[]; |
| 1179 | int rtn = FALSE; |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1180 | int rowcount; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1181 | |
| 1182 | exe_name = DEBUG_argv0; |
| 1183 | |
| 1184 | /* |
| 1185 | * Make sure we can stat and open this file. |
| 1186 | */ |
| 1187 | if( exe_name == NULL ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1188 | goto leave; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1189 | |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1190 | fprintf( stderr, "Loading symbols: %s", exe_name ); |
| 1191 | rowcount = 17 + strlen(exe_name); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1192 | DEBUG_ProcessElfObject(exe_name, 0); |
| 1193 | |
| 1194 | /* |
| 1195 | * Finally walk the tables that the dynamic loader maintains to find all |
| 1196 | * of the other shared libraries which might be loaded. Perform the |
| 1197 | * same step for all of these. |
| 1198 | */ |
| 1199 | dynpnt = _DYNAMIC; |
| 1200 | if( dynpnt == NULL ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1201 | goto leave; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1202 | |
| 1203 | /* |
| 1204 | * Now walk the dynamic section (of the executable, looking for a DT_DEBUG |
| 1205 | * entry. |
| 1206 | */ |
| 1207 | for(; dynpnt->d_tag != DT_NULL; dynpnt++) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1208 | if( dynpnt->d_tag == DT_DEBUG ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1209 | break; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1210 | |
| 1211 | if( (dynpnt->d_tag != DT_DEBUG) |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 1212 | || (dynpnt->d_un.d_ptr == 0) ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1213 | goto leave; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1214 | |
| 1215 | /* |
| 1216 | * OK, now dig into the actual tables themselves. |
| 1217 | */ |
| 1218 | dbg_hdr = (struct r_debug *) dynpnt->d_un.d_ptr; |
| 1219 | lpnt = dbg_hdr->r_map; |
| 1220 | |
| 1221 | /* |
| 1222 | * Now walk the linked list. In all known ELF implementations, |
| 1223 | * the dynamic loader maintains this linked list for us. In some |
| 1224 | * cases the first entry doesn't appear with a name, in other cases it |
| 1225 | * does. |
| 1226 | */ |
| 1227 | for(; lpnt; lpnt = lpnt->l_next ) |
| 1228 | { |
| 1229 | /* |
| 1230 | * We already got the stuff for the executable using the |
| 1231 | * argv[0] entry above. Here we only need to concentrate on any |
| 1232 | * shared libraries which may be loaded. |
| 1233 | */ |
| 1234 | ehdr = (Elf32_Ehdr *) lpnt->l_addr; |
Alexandre Julliard | dadf78f | 1998-05-17 17:13:43 +0000 | [diff] [blame] | 1235 | if( (lpnt->l_addr == 0) || (ehdr->e_type != ET_DYN) ) |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1236 | continue; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1237 | |
| 1238 | if( lpnt->l_name != NULL ) |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1239 | { |
| 1240 | if (rowcount + strlen(lpnt->l_name) > 76) |
| 1241 | { |
| 1242 | fprintf( stderr, "\n " ); |
| 1243 | rowcount = 3; |
| 1244 | } |
| 1245 | fprintf( stderr, " %s", lpnt->l_name ); |
| 1246 | rowcount += strlen(lpnt->l_name) + 1; |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1247 | DEBUG_ProcessElfObject(lpnt->l_name, lpnt->l_addr); |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1248 | } |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | rtn = TRUE; |
| 1252 | |
| 1253 | leave: |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1254 | fprintf( stderr, "\n" ); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1255 | return (rtn); |
| 1256 | |
| 1257 | } |
| 1258 | |
| 1259 | #else /* !__ELF__ */ |
| 1260 | |
| 1261 | #ifdef linux |
| 1262 | /* |
| 1263 | * a.out linux. |
| 1264 | */ |
| 1265 | int |
| 1266 | DEBUG_ReadExecutableDbgInfo(void) |
| 1267 | { |
| 1268 | char * addr = (char *) 0xffffffff; |
| 1269 | char * exe_name; |
| 1270 | struct exec * ahdr; |
| 1271 | int fd = -1; |
| 1272 | int rtn = FALSE; |
| 1273 | unsigned int staboff; |
| 1274 | struct stat statbuf; |
| 1275 | int status; |
| 1276 | unsigned int stroff; |
| 1277 | |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1278 | exe_name = DEBUG_argv0; |
| 1279 | |
| 1280 | /* |
| 1281 | * Make sure we can stat and open this file. |
| 1282 | */ |
| 1283 | if( exe_name == NULL ) |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1284 | goto leave; |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1285 | |
| 1286 | status = stat(exe_name, &statbuf); |
| 1287 | if( status == -1 ) |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1288 | goto leave; |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1289 | |
| 1290 | /* |
| 1291 | * Now open the file, so that we can mmap() it. |
| 1292 | */ |
| 1293 | fd = open(exe_name, O_RDONLY); |
| 1294 | if( fd == -1 ) |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1295 | goto leave; |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1296 | |
| 1297 | |
| 1298 | /* |
| 1299 | * Now mmap() the file. |
| 1300 | */ |
| 1301 | addr = mmap(0, statbuf.st_size, PROT_READ, |
| 1302 | MAP_PRIVATE, fd, 0); |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 1303 | if( addr == (char *) 0xffffffff ) |
Alexandre Julliard | df2673b | 1997-03-29 17:20:20 +0000 | [diff] [blame] | 1304 | goto leave; |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1305 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1306 | ahdr = (struct exec *) addr; |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1307 | |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1308 | staboff = N_SYMOFF(*ahdr); |
| 1309 | stroff = N_STROFF(*ahdr); |
| 1310 | rtn = DEBUG_ParseStabs(addr, 0, |
| 1311 | staboff, |
| 1312 | ahdr->a_syms, |
| 1313 | stroff, |
| 1314 | statbuf.st_size - stroff); |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1315 | |
| 1316 | /* |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1317 | * Give a nice status message here... |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1318 | */ |
Alexandre Julliard | 767e6f6 | 1998-08-09 12:47:43 +0000 | [diff] [blame] | 1319 | fprintf( stderr, "Loading symbols: %s", exe_name ); |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1320 | |
| 1321 | rtn = TRUE; |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1322 | |
| 1323 | leave: |
| 1324 | |
| 1325 | if( addr != (char *) 0xffffffff ) |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1326 | munmap(addr, statbuf.st_size); |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1327 | |
| 1328 | if( fd != -1 ) |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1329 | close(fd); |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1330 | |
| 1331 | return (rtn); |
| 1332 | |
| 1333 | } |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1334 | #else |
| 1335 | /* |
| 1336 | * Non-linux, non-ELF platforms. |
| 1337 | */ |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1338 | int |
| 1339 | DEBUG_ReadExecutableDbgInfo(void) |
| 1340 | { |
| 1341 | return FALSE; |
| 1342 | } |
Alexandre Julliard | c6c0944 | 1997-01-12 18:32:19 +0000 | [diff] [blame] | 1343 | #endif |
Alexandre Julliard | 9ea19e5 | 1997-01-01 17:29:55 +0000 | [diff] [blame] | 1344 | |
Alexandre Julliard | 7ebe1a4 | 1996-12-22 18:27:48 +0000 | [diff] [blame] | 1345 | #endif /* __ELF__ */ |