wine/debugger/stabs.c
Alexandre Julliard a845b88ead Release 980601
Sun May 31 13:40:13 1998  Alexandre Julliard  <julliard@lrc.epfl.ch>

	* [if1632/signal.c]
	Added display of exception name.

	* [loader/task.c]
	Yet another attempt at fixing SwitchStackTo/SwitchStackBack.

	* [memory/selector.c] [relay32/builtin32.c] [tools/build.c]
	  [win32/kernel32.c]
	Generate an assembly stub for Win32 register functions to make
	their names available at link time.

	* [programs/*/Makefile.in]
	Added hacks to support old resource compiler.

Fri May 29 16:27:14 1998  Marcus Meissner <marcus@jet.franken.de>

	* [tools/testrun]
	Merge of my testscripts at home into one single perl program
	(tested with perl5). Works only on Linux due to 'ps' and 'ipcs'
	magic.

	* [controls/menu.c]
	Added some DefaultMenuItem stubs.

	* [debugger/stabs.c]
	Fixed stabs loading, now supports (int,int) typeinfo format used
	by gcc-2.8 and egcs-1. If it still crashes, please mail me.

	* [if1632/][relay32/]
	Added msvideo.dll (stubs only)
	Replaced some ptr by str for stringcases
	Added some new stubs (VxDCall, FindCloseNotif....)

	* [misc/network.c]
	Some argument fixes.

	* [misc/registry.c][misc/cpu.c]
	Registry initialization partially rewritten and enhanced.

	* [scheduler/*.c]
	Some additions so we don't do kill(0,SIGUSR1) (kill processgroup
	instead of targeted thread)
	Added SetThreadContext.

Thu May 28 23:59:59 1998  Bertho Stultiens <bertho@akhphd.au.dk>

	* [tools/wrc/*]
	New resource compiler version 1.0.0 (28-May-1998)

	* [Make.rules.in] [Makefile.in]
	Changed and added rc rules to point to tools/wrc/wrc.

	* [configure.in] [include/config.h.in]
	Added check for function 'stricmp'.

	* [include/resource.h]
	Commented out the old resource structure to catch references.
	It also includes wrc_rsc.h.

	* [include/wrc_rsc.h]
	New file. Definitions for the resources generated with wrc.

	* [include/windows.h]
	Added #ifdef RC_INVOKED to exclude stdarg.h.
	Added SS_NOTIFY flag.

	* [include/winnls.h]
	Added SUBLANG_* definitions and corrected some defaults.

	* [loader/libres.c]
	Changed the sysres load functions to support wrc generated
	resources.

	* [resource/sysres_*.rc]
	Added #include <windows.h>

	* [resource/sysres.c]
	Changed declarations to match wrc's output

	* [resource/Makefile.in]
	Changed rules to work with wrc.

	* [tools/makedep.c]
	Changed generation of .rc file dependencies to .s target.

Thu May 28 22:28:39 1998  Eric Kohl <ekohl@abo.rhein-zeitung.de>

	* [files/file.c][include/windows.c][relay32/kernel32.spec]
	Implemented GetFileAttributesEx32A/W.

	* [misc/imagelist.h][include/commctrl.h][relay32/comctl32.spec]
	Added ImageList_Read and ImageList_Write stubs.
	Added ImageList_AddIcon function.
	Added ImageList_LoadImage. It is the same as ImageList_LoadImage32A.

	* [controls/header.c]
	Fixed bitmap drawing bug.
	Added full bitmap support.

	* [include/commctrl.h]
	Added missing header macros.

	* [controls/toolbar.c][include/toolbar.h][include/commctrl.h]
	  [controls/commctrl.c] [relay32/comctl32.spec]
	First implementation of toolbar control.
	Implemented CreateToolbar, CreateToolbarEx and CreateMappedBitmap.

	* [controls/progress.c][controls/status.c]
	Some code cleanup.

	* [controls/commctrl.c][include/commctrl.h][relay32/comctl32.spec]
	Removed CreateStatusWindow16 and DrawStatusText16.
	CreateStatusWindow is the same as CreateStatusWindow32A.
	DrawStatusText is the same as DrawStatusText32A.

Thu May 28 16:01:28 1998  Matthew J. Francis  <asbel@dial.pipex.com>

	* [objects/bitmap.c] [objects/bitmap.h] [objects/oembitmap.c]
	  [objects/dc.c] [graphics/x11drv/bitblt.c]
	Added partial implementation of CreateDIBSection, with great thanks
	to Ulrich Weigand <weigand@informatik.uni-erlangen.de> for
	contributing the bulk of the patch.

Wed May 27 19:04:31 1998  Ulrich Weigand <weigand@informatik.uni-erlangen.de>

	* [win32/kernel32.c] [if1632/thunk.c] [include/flatthunk.h]
	ThunkConnect16 and related functions moved to emulator.

	* [loader/ne/segment.c]
	Call DllEntryPoint with correct arguments.

	* [relay32/builtin32.c]
	Bugfix: Relay debugging did not work for multiple processes.

	* [controls/menu.c]
	Bugfix: dwItemData was not set for MF_OWNERDRAW menus.

	* [if1632/relay.c] [relay32/relay386.c]
	Relay messages converted to use DPRINTF.

	* [controls/desktop.c] [relay32/user32.spec]
	Implemented PaintDesktop.

	* [files/profile.c] [if1632/kernel.spec] [misc/network.c]
	  [misc/printdrv.c] [relay32/winspool.spec] 
	  [win32/ordinals.c] [relay32/kernel32.spec]
	Some stubs added.

	* [relay32/mpr.spec]
	All ordinals were off by one.

Tue May 26 13:32:57 1998  Bill Hawes <whawes@star.net>

	* [misc/lstr.c] [include/casemap.h] [tools/unimap.pl]
	Added Unicode case conversion routines towupper/towlower,
	with mapping tables casemap.h created by tools/unimap.pl.

	* [misc/ntdll.c] [include/winnls.h] [relay32/ntdll.spec]
	  [relay32/advapi.spec]
	Minimal implementation of IsTextUnicode, just enough to get
	NT4 notepad to open ascii/unicode files.

	* [Make.rules.in] [resources/sysres_En.rc]
	Added include file dlgs.h for building resource files, so that
	resources can refer to defined values (e.g. pshHelp).

	* [misc/crtdll.c] [relay32/crtdll.spec]
	Use towupper/towlower for 32W case conversions.

	* [memory/string.c]
	Use towupper for 32W case conversions.

	* [ole/ole2nls.c]
	Use towupper for 32W case conversions; fix mem leak; minor cleanup

	* [controls/edit.c]
	Added soft break flag to edit state. Print unknown action values
	for WM_VSCROLL (action 190 occurs when running NT4 notepad.)

Mon May 25 22:42:40 1998  Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>

	* [files/file.c]
	Care for a pathological case in SetFilePointer.

	* [graphics/x11drv/xfont.c]
	Handle longer Font names in LFD_ComposeLFD and try to catch errors.

	* [loader/pe_image.c]
	Unload Dummymodule when PE_LoadLibraryEx32A fails with
	PE_LoadImage (makes Encarta 98 installer proceed).

	* [misc/registry.c]
	Move a check for a special case in RegCreateKeyEx32W after the
	check for existence.

Tue May 25 20:18:26 1998  Matthew Becker <mbecker@glasscity.net>

	* [misc/ntdll.c]
	Added some stubs, just guessing at the size of their param lists.

	* [misc/registry.c]
	Added stubs for RegUnLoadKey, RegSetKeySecurity, RegSaveKey,
	RegRestoreKey, and RegReplaceKey

	* [programs/regtest/regtest.c]
	Updated registry testing program.

Sun May 24 18:11:40 1998  Alex Priem <alexp@sci.kun.nl>

	* [file/profile.c]
	Added flag 'return_values' to PROFILE_GetSection.

Sun May 24 13:41:10 1998  James Juran <jrj120@psu.edu>

	* [misc/shell.c] [files/directory.c]
	Documentation/debugging info additions.

	* [*/*.c] [include/*.h]
	Moved many extern function definitions to appropriate header files.
	Cleaned up a few compile warnings.
	If #include "debug.h" is present, removed #include <stdio.h>.
	debug.h includes stdio.h, so it is not necessary to include both.

	* [graphics/*.c] [if1632/signal.c] [ipc/*.c] [scheduler/*.c]
	  [win32/*.c] [windows/*.c]
	Final patch to convert fprintf statements to new debugging interface.
	Some fprintfs are still left, especially in the debugger/ directory.
	However, IMHO, it's not worth the effort to change the rest.

Fri May 22 21:58:35 1998  Morten Welinder  <terra@diku.dk>

	* [windows/keyboard.c]
	Correct handling of keys "`-=[]\;',./".

Fri May 22 12:06:00 1998  Per Lindström <pelinstr@algonet.se>

	* [include/windows.h] [relay32/kernel32.spec] [win32/console.c]
	Added stub for ReadConsoleOutputCharacter32A.

Thu May 21 16:45:48 1998  Pascal Cuoq <pcuoq@ens-lyon.fr>

	* [ole/ole2nls.c]
	Began better implementation of LCMapString32A.
	Not very well tested yet, and still need improvements.

	* [controls/scroll.c]
	Documented functions.

Wed May 20 21:37:56 1998  Peter Hunnisett <hunnise@nortel.ca>

	* [include/windows.h][misc/main.c]
	Change SystemParameterInfo to support SPI_GETHIGHCONTRAST. Also
	include some missing SPI_ definitions.

	* [include/dsound.h][multimedia/dsound.c][relay32/dplayx.spec]
	Added stubs for DirectPlayLobbyCreate[AW]. Not sure if these
	should go into a new files dplayx.c? Anyone care?

	* [include/winnls.h]
	Added two missing flags for the CompareString32 functions.
1998-06-01 10:44:35 +00:00

1340 lines
30 KiB
C

/*
* File stabs.c - read stabs information from the wine executable itself.
*
* Copyright (C) 1996, Eric Youngdale.
*/
#include <sys/types.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifndef PATH_MAX
#define PATH_MAX _MAX_PATH
#endif
#include "win.h"
#include "debugger.h"
#include "xmalloc.h"
#ifdef __svr4__
#define __ELF__
#endif
#ifdef __ELF__
#include <elf.h>
#include <link.h>
#include <sys/mman.h>
#elif defined(__EMX__)
#include <a_out.h>
#else
#include <a.out.h>
#endif
#ifndef N_UNDF
#define N_UNDF 0x00
#endif
#define N_GSYM 0x20
#define N_FUN 0x24
#define N_STSYM 0x26
#define N_LCSYM 0x28
#define N_MAIN 0x2a
#define N_ROSYM 0x2c
#define N_OPT 0x3c
#define N_RSYM 0x40
#define N_SLINE 0x44
#define N_SO 0x64
#define N_LSYM 0x80
#define N_BINCL 0x82
#define N_SOL 0x84
#define N_PSYM 0xa0
#define N_EINCL 0xa2
#define N_LBRAC 0xc0
#define N_RBRAC 0xe0
/*
* This is how we translate stab types into our internal representations
* of datatypes.
*/
static struct datatype ** stab_types = NULL;
static int num_stab_types = 0;
/*
* Set so that we know the main executable name and path.
*/
char * DEBUG_argv0;
struct stab_nlist {
union {
char *n_name;
struct stab_nlist *n_next;
long n_strx;
} n_un;
unsigned char n_type;
char n_other;
short n_desc;
unsigned long n_value;
};
/*
* This is used to keep track of known datatypes so that we don't redefine
* them over and over again. It sucks up lots of memory otherwise.
*/
struct known_typedef
{
struct known_typedef * next;
char * name;
int ndefs;
struct datatype * types[0];
};
#define NR_STAB_HASH 521
struct known_typedef * ktd_head[NR_STAB_HASH] = {NULL,};
static unsigned int stab_hash( const char * name )
{
unsigned int hash = 0;
unsigned int tmp;
const char * p;
p = name;
while (*p)
{
hash = (hash << 4) + *p++;
if( (tmp = (hash & 0xf0000000)) )
{
hash ^= tmp >> 24;
}
hash &= ~tmp;
}
return hash % NR_STAB_HASH;
}
static void stab_strcpy(char * dest, const char * source)
{
/*
* A strcpy routine that stops when we hit the ':' character.
* Faster than copying the whole thing, and then nuking the
* ':'.
*/
while(*source != '\0' && *source != ':')
*dest++ = *source++;
*dest++ = '\0';
}
#define MAX_TD_NESTING 128
static int **typenums;
static int *nroftypenums=NULL;
static int nrofnroftypenums=0;
static int curtypenum = 0;
static
int
DEBUG_FileSubNr2StabEnum(int filenr,int subnr) {
if (nrofnroftypenums<=filenr) {
nroftypenums = xrealloc(nroftypenums,sizeof(nroftypenums[0])*(filenr+1));
memset(nroftypenums+nrofnroftypenums,0,(filenr+1-nrofnroftypenums)*sizeof(nroftypenums[0]));
typenums = xrealloc(typenums,sizeof(typenums[0])*(filenr+1));
memset(typenums+nrofnroftypenums,0,sizeof(typenums[0])*(filenr+1-nrofnroftypenums));
nrofnroftypenums=filenr+1;
}
if (nroftypenums[filenr]<=subnr) {
typenums[filenr] = xrealloc(typenums[filenr],sizeof(typenums[0][0])*(subnr+1));
memset(typenums[filenr]+nroftypenums[filenr],0,sizeof(typenums[0][0])*(subnr+1-nroftypenums[filenr]));
nroftypenums[filenr] = subnr+1;
}
if (!typenums[filenr][subnr])
typenums[filenr][subnr]=++curtypenum;
if( num_stab_types <= curtypenum ) {
num_stab_types = curtypenum + 256;
stab_types = (struct datatype **) xrealloc(stab_types,
num_stab_types * sizeof(struct datatype *)
);
}
//fprintf(stderr,"(%d,%d) is %d\n",filenr,subnr,typenums[filenr][subnr]);
return typenums[filenr][subnr];
}
static
int
DEBUG_ReadTypeEnumBackwards(char*x) {
int filenr,subnr;
if (*x==')') {
while (*x!='(')
x--;
x++; /* '(' */
filenr=strtol(x,&x,10); /* <int> */
assert(*(x++)==','); /* ',' */
subnr=strtol(x,&x,10); /* <int> */
assert(*(x++)==')'); /* ')' */
} else {
while ((*x>='0') && (*x<='9'))
x--;
filenr = 0;
subnr = atol(x+1);
}
return DEBUG_FileSubNr2StabEnum(filenr,subnr);
}
static
int
DEBUG_ReadTypeEnum(char **x) {
int filenr,subnr;
if (**x=='(') {
(*x)++; /* '(' */
filenr=strtol(*x,x,10); /* <int> */
(*x)++; /* ',' */
subnr=strtol(*x,x,10); /* <int> */
(*x)++; /* ')' */
} else {
filenr = 0;
subnr = strtol(*x,x,10); /* <int> */
}
return DEBUG_FileSubNr2StabEnum(filenr,subnr);
}
static
int
DEBUG_RegisterTypedef(const char * name, struct datatype ** types, int ndef)
{
int hash;
struct known_typedef * ktd;
if( ndef == 1 )
return TRUE;
ktd = (struct known_typedef *) xmalloc(sizeof(struct known_typedef)
+ ndef * sizeof(struct datatype *));
hash = stab_hash(name);
ktd->name = xstrdup(name);
ktd->ndefs = ndef;
memcpy(&ktd->types[0], types, ndef * sizeof(struct datatype *));
ktd->next = ktd_head[hash];
ktd_head[hash] = ktd;
return TRUE;
}
static
int
DEBUG_HandlePreviousTypedef(const char * name, const char * stab)
{
int count;
enum debug_type expect;
int hash;
struct known_typedef * ktd;
char * ptr;
hash = stab_hash(name);
for(ktd = ktd_head[hash]; ktd; ktd = ktd->next)
if ((ktd->name[0] == name[0]) && (strcmp(name, ktd->name) == 0) )
break;
/*
* Didn't find it. This must be a new one.
*/
if( ktd == NULL )
return FALSE;
/*
* Examine the stab to make sure it has the same number of definitions.
*/
count = 0;
for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
{
if( count >= ktd->ndefs )
return FALSE;
/*
* Make sure the types of all of the objects is consistent with
* what we have already parsed.
*/
switch(ptr[1])
{
case '*':
expect = POINTER;
break;
case 's':
case 'u':
expect = STRUCT;
break;
case 'a':
expect = ARRAY;
break;
case '1':
case '(':
case 'r':
expect = BASIC;
break;
case 'x':
expect = STRUCT;
break;
case 'e':
expect = ENUM;
break;
case 'f':
expect = FUNC;
break;
default:
fprintf(stderr, "Unknown type (%c).\n",ptr[1]);
return FALSE;
}
if( expect != DEBUG_GetType(ktd->types[count]) )
return FALSE;
count++;
}
if( ktd->ndefs != count )
return FALSE;
/*
* Go through, dig out all of the type numbers, and substitute the
* appropriate things.
*/
count = 0;
for(ptr = strchr(stab, '='); ptr; ptr = strchr(ptr+1, '='))
stab_types[DEBUG_ReadTypeEnumBackwards(ptr-1)] = ktd->types[count++];
return TRUE;
}
static int DEBUG_FreeRegisteredTypedefs()
{
int count;
int j;
struct known_typedef * ktd;
struct known_typedef * next;
count = 0;
for(j=0; j < NR_STAB_HASH; j++ )
{
for(ktd = ktd_head[j]; ktd; ktd = next)
{
count++;
next = ktd->next;
free(ktd->name);
free(ktd);
}
ktd_head[j] = NULL;
}
return TRUE;
}
static
int
DEBUG_ParseTypedefStab(char * ptr, const char * typename)
{
int arrmax;
int arrmin;
char * c;
struct datatype * curr_type;
struct datatype * datatype;
struct datatype * curr_types[MAX_TD_NESTING];
char element_name[1024];
int ntypes = 0;
int offset;
const char * orig_typename;
int size;
char * tc;
char * tc2;
int typenum;
orig_typename = typename;
if( DEBUG_HandlePreviousTypedef(typename, ptr) )
return TRUE;
/*
* Go from back to front. First we go through and figure out what
* type numbers we need, and register those types. Then we go in
* and fill the details.
*/
for( c = strchr(ptr, '='); c != NULL; c = strchr(c + 1, '=') )
{
/*
* Back up until we get to a non-numeric character. This is the type
* number.
*/
typenum = DEBUG_ReadTypeEnumBackwards(c-1);
if( ntypes >= MAX_TD_NESTING )
{
/*
* If this ever happens, just bump the counter.
*/
fprintf(stderr, "Typedef nesting overflow\n");
return FALSE;
}
switch(c[1])
{
case '*':
stab_types[typenum] = DEBUG_NewDataType(POINTER, NULL);
curr_types[ntypes++] = stab_types[typenum];
break;
case 's':
case 'u':
stab_types[typenum] = DEBUG_NewDataType(STRUCT, typename);
curr_types[ntypes++] = stab_types[typenum];
break;
case 'a':
stab_types[typenum] = DEBUG_NewDataType(ARRAY, NULL);
curr_types[ntypes++] = stab_types[typenum];
break;
case '(':
case '1':
case 'r':
stab_types[typenum] = DEBUG_NewDataType(BASIC, typename);
curr_types[ntypes++] = stab_types[typenum];
break;
case 'x':
stab_strcpy(element_name, c + 3);
stab_types[typenum] = DEBUG_NewDataType(STRUCT, element_name);
curr_types[ntypes++] = stab_types[typenum];
break;
case 'e':
stab_types[typenum] = DEBUG_NewDataType(ENUM, NULL);
curr_types[ntypes++] = stab_types[typenum];
break;
case 'f':
stab_types[typenum] = DEBUG_NewDataType(FUNC, NULL);
curr_types[ntypes++] = stab_types[typenum];
break;
default:
fprintf(stderr, "Unknown type (%c).\n",c[1]);
}
typename = NULL;
}
/*
* Now register the type so that if we encounter it again, we will know
* what to do.
*/
DEBUG_RegisterTypedef(orig_typename, curr_types, ntypes);
/*
* OK, now take a second sweep through. Now we will be digging
* out the definitions of the various components, and storing
* them in the skeletons that we have already allocated. We take
* a right-to left search as this is much easier to parse.
*/
for( c = strrchr(ptr, '='); c != NULL; c = strrchr(ptr, '=') )
{
int typenum = DEBUG_ReadTypeEnumBackwards(c-1);
curr_type = stab_types[typenum];
switch(c[1])
{
case 'x':
tc = c + 3;
while( *tc != ':' )
tc++;
tc++;
if( *tc == '\0' )
*c = '\0';
else
strcpy(c, tc);
break;
case '*':
case 'f':
tc = c + 2;
datatype = stab_types[DEBUG_ReadTypeEnum(&tc)];
DEBUG_SetPointerType(curr_type, datatype);
if( *tc == '\0' )
*c = '\0';
else
strcpy(c, tc);
break;
case '(':
case '1':
case 'r':
/*
* We have already handled these above.
*/
*c = '\0';
break;
case 'a':
/* ar<typeinfo_nodef>;<int>;<int>;<typeinfo>,<int>,<int>;; */
tc = c + 3;
assert(c[2]=='r');
DEBUG_ReadTypeEnum(&tc);
assert(*(tc++)==';'); /* ';' */
arrmin = strtol(tc, &tc, 10); /* <int> */
assert(*(tc++)==';'); /* ';' */
arrmax = strtol(tc, &tc, 10); /* <int> */
assert(*(tc++)==';'); /* ';' */
datatype = stab_types[DEBUG_ReadTypeEnum(&tc)]; /* <typeinfo> */
if( *tc == '\0' )
*c = '\0';
else
strcpy(c, tc);
DEBUG_SetArrayParams(curr_type, arrmin, arrmax, datatype);
break;
case 's':
case 'u': {
int failure = 0;
tc = c + 2;
if( DEBUG_SetStructSize(curr_type, strtol(tc, &tc, 10)) == FALSE )
{
/*
* We have already filled out this structure. Nothing to do,
* so just skip forward to the end of the definition.
*/
while( tc[0] != ';' && tc[1] != ';' )
tc++;
tc += 2;
if( *tc == '\0' )
*c = '\0';
else
strcpy(c, tc + 1);
continue;
}
/*
* Now parse the individual elements of the structure/union.
*/
while(*tc != ';')
{
char *ti;
tc2 = element_name;
while(*tc != ':')
*tc2++ = *tc++;
tc++;
*tc2++ = '\0';
ti=tc;
datatype = stab_types[DEBUG_ReadTypeEnum(&tc)];
*tc='\0';
tc++;
offset = strtol(tc, &tc, 10);
tc++;
size = strtol(tc, &tc, 10);
tc++;
if (datatype)
DEBUG_AddStructElement(curr_type, element_name, datatype, offset, size);
else {
failure = 1;
/* ... but proceed parsing to the end of the stab */
}
}
if (failure) {
/* if we had a undeclared value this one is undeclared too.
* remove it from the stab_types.
* I just set it to NULL to detect bugs in my thoughtprocess.
* FIXME: leaks the memory for the structure elements.
* FIXME: such structures should have been optimized away
* by ld.
*/
stab_types[typenum] = NULL;
}
if( *tc == '\0' )
*c = '\0';
else
strcpy(c, tc + 1);
break;
}
case 'e':
tc = c + 2;
/*
* Now parse the individual elements of the structure/union.
*/
while(*tc != ';')
{
tc2 = element_name;
while(*tc != ':')
*tc2++ = *tc++;
tc++;
*tc2++ = '\0';
offset = strtol(tc, &tc, 10);
tc++;
DEBUG_AddStructElement(curr_type, element_name, NULL, offset, 0);
}
if( *tc == '\0' )
*c = '\0';
else
strcpy(c, tc + 1);
break;
default:
fprintf(stderr, "Unknown type (%c).\n",c[1]);
break;
}
}
return TRUE;
}
static struct datatype *
DEBUG_ParseStabType(const char * stab)
{
char * c;
/*
* Look through the stab definition, and figure out what datatype
* this represents. If we have something we know about, assign the
* type.
*/
c = strchr(stab, ':');
if( c == NULL )
return NULL;
c++;
/*
* The next character says more about the type (i.e. data, function, etc)
* of symbol. Skip it.
*/
c++;
/*
* The next is either an integer or a (integer,integer).
* The DEBUG_ReadTypeEnum takes care that stab_types is large enough.
*/
return stab_types[DEBUG_ReadTypeEnum(&c)];
}
static
int
DEBUG_ParseStabs(char * addr, unsigned int load_offset,
unsigned int staboff, int stablen,
unsigned int strtaboff, int strtablen)
{
struct name_hash * curr_func = NULL;
struct wine_locals * curr_loc = NULL;
struct name_hash * curr_sym = NULL;
char currpath[PATH_MAX];
int i;
int ignore = FALSE;
int last_nso = -1;
int len;
DBG_ADDR new_addr;
int nstab;
char * ptr;
char * stabbuff;
int stabbufflen;
struct stab_nlist * stab_ptr;
char * strs;
int strtabinc;
char * subpath = NULL;
char symname[4096];
nstab = stablen / sizeof(struct stab_nlist);
stab_ptr = (struct stab_nlist *) (addr + staboff);
strs = (char *) (addr + strtaboff);
memset(currpath, 0, sizeof(currpath));
/*
* Allocate a buffer into which we can build stab strings for cases
* where the stab is continued over multiple lines.
*/
stabbufflen = 65536;
stabbuff = (char *) xmalloc(stabbufflen);
strtabinc = 0;
stabbuff[0] = '\0';
for(i=0; i < nstab; i++, stab_ptr++ )
{
ptr = strs + (unsigned int) stab_ptr->n_un.n_name;
if( ptr[strlen(ptr) - 1] == '\\' )
{
/*
* Indicates continuation. Append this to the buffer, and go onto the
* next record. Repeat the process until we find a stab without the
* '/' character, as this indicates we have the whole thing.
*/
len = strlen(ptr);
if( strlen(stabbuff) + len > stabbufflen )
{
stabbufflen += 65536;
stabbuff = (char *) xrealloc(stabbuff, stabbufflen);
}
strncat(stabbuff, ptr, len - 1);
continue;
}
else if( stabbuff[0] != '\0' )
{
strcat( stabbuff, ptr);
ptr = stabbuff;
}
if( strchr(ptr, '=') != NULL )
{
/*
* The stabs aren't in writable memory, so copy it over so we are
* sure we can scribble on it.
*/
if( ptr != stabbuff )
{
strcpy(stabbuff, ptr);
ptr = stabbuff;
}
stab_strcpy(symname, ptr);
DEBUG_ParseTypedefStab(ptr, symname);
}
switch(stab_ptr->n_type)
{
case N_GSYM:
/*
* These are useless with ELF. They have no value, and you have to
* read the normal symbol table to get the address. Thus we
* ignore them, and when we process the normal symbol table
* we should do the right thing.
*
* With a.out, they actually do make some amount of sense.
*/
new_addr.seg = 0;
new_addr.type = DEBUG_ParseStabType(ptr);
new_addr.off = load_offset + stab_ptr->n_value;
stab_strcpy(symname, ptr);
#ifdef __ELF__
curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
SYM_WINE | SYM_DATA | SYM_INVALID);
#else
curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
SYM_WINE | SYM_DATA );
#endif
break;
case N_RBRAC:
case N_LBRAC:
/*
* We need to keep track of these so we get symbol scoping
* right for local variables. For now, we just ignore them.
* The hooks are already there for dealing with this however,
* so all we need to do is to keep count of the nesting level,
* and find the RBRAC for each matching LBRAC.
*/
break;
case N_LCSYM:
case N_STSYM:
/*
* These are static symbols and BSS symbols.
*/
new_addr.seg = 0;
new_addr.type = DEBUG_ParseStabType(ptr);
new_addr.off = load_offset + stab_ptr->n_value;
stab_strcpy(symname, ptr);
curr_sym = DEBUG_AddSymbol( symname, &new_addr, currpath,
SYM_WINE | SYM_DATA );
break;
case N_PSYM:
/*
* These are function parameters.
*/
if( (curr_func != NULL)
&& (stab_ptr->n_value != 0) )
{
stab_strcpy(symname, ptr);
curr_loc = DEBUG_AddLocal(curr_func, 0,
stab_ptr->n_value, 0, 0, symname);
DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr));
}
break;
case N_RSYM:
if( curr_func != NULL )
{
stab_strcpy(symname, ptr);
curr_loc = DEBUG_AddLocal(curr_func, stab_ptr->n_value, 0, 0, 0, symname);
DEBUG_SetLocalSymbolType( curr_loc, DEBUG_ParseStabType(ptr));
}
break;
case N_LSYM:
if( (curr_func != NULL)
&& (stab_ptr->n_value != 0) )
{
stab_strcpy(symname, ptr);
DEBUG_AddLocal(curr_func, 0,
stab_ptr->n_value, 0, 0, symname);
}
else if (curr_func == NULL)
{
stab_strcpy(symname, ptr);
}
break;
case N_SLINE:
/*
* This is a line number. These are always relative to the start
* of the function (N_FUN), and this makes the lookup easier.
*/
if( curr_func != NULL )
{
#ifdef __ELF__
DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
stab_ptr->n_value);
#else
#if 0
/*
* This isn't right. The order of the stabs is different under
* a.out, and as a result we would end up attaching the line
* number to the wrong function.
*/
DEBUG_AddLineNumber(curr_func, stab_ptr->n_desc,
stab_ptr->n_value - curr_func->addr.off);
#endif
#endif
}
break;
case N_FUN:
/*
* First, clean up the previous function we were working on.
*/
DEBUG_Normalize(curr_func);
/*
* For now, just declare the various functions. Later
* on, we will add the line number information and the
* local symbols.
*/
if( !ignore )
{
new_addr.seg = 0;
new_addr.type = DEBUG_ParseStabType(ptr);
new_addr.off = load_offset + stab_ptr->n_value;
/*
* Copy the string to a temp buffer so we
* can kill everything after the ':'. We do
* it this way because otherwise we end up dirtying
* all of the pages related to the stabs, and that
* sucks up swap space like crazy.
*/
stab_strcpy(symname, ptr);
curr_func = DEBUG_AddSymbol( symname, &new_addr, currpath,
SYM_WINE | SYM_FUNC);
}
else
{
/*
* Don't add line number information for this function
* any more.
*/
curr_func = NULL;
}
break;
case N_SO:
/*
* This indicates a new source file. Append the records
* together, to build the correct path name.
*/
#ifndef __ELF__
/*
* With a.out, there is no NULL string N_SO entry at the end of
* the file. Thus when we find non-consecutive entries,
* we consider that a new file is started.
*/
if( last_nso < i-1 )
{
currpath[0] = '\0';
DEBUG_Normalize(curr_func);
curr_func = NULL;
}
#endif
if( *ptr == '\0' )
{
/*
* Nuke old path.
*/
currpath[0] = '\0';
DEBUG_Normalize(curr_func);
curr_func = NULL;
/*
* The datatypes that we would need to use are reset when
* we start a new file.
*/
memset(stab_types, 0, num_stab_types * sizeof(stab_types[0]));
/*
for (i=0;i<nrofnroftypenums;i++)
memset(typenums[i],0,sizeof(typenums[i][0])*nroftypenums[i]);
*/
}
else
{
if (*ptr != '/')
strcat(currpath, ptr);
else
strcpy(currpath, ptr);
subpath = ptr;
}
last_nso = i;
break;
case N_SOL:
/*
* This indicates we are including stuff from an include file.
* If this is the main source, enable the debug stuff, otherwise
* ignore it.
*/
if( subpath == NULL || strcmp(ptr, subpath) == 0 )
{
ignore = FALSE;
}
else
{
ignore = TRUE;
DEBUG_Normalize(curr_func);
curr_func = NULL;
}
break;
case N_UNDF:
strs += strtabinc;
strtabinc = stab_ptr->n_value;
DEBUG_Normalize(curr_func);
curr_func = NULL;
break;
case N_OPT:
/*
* Ignore this. We don't care what it points to.
*/
break;
case N_BINCL:
case N_EINCL:
case N_MAIN:
/*
* Always ignore these. GCC doesn't even generate them.
*/
break;
default:
break;
}
stabbuff[0] = '\0';
#if 0
fprintf(stderr, "%d %x %s\n", stab_ptr->n_type,
(unsigned int) stab_ptr->n_value,
strs + (unsigned int) stab_ptr->n_un.n_name);
#endif
}
if( stab_types != NULL )
{
free(stab_types);
stab_types = NULL;
num_stab_types = 0;
}
DEBUG_FreeRegisteredTypedefs();
return TRUE;
}
#ifdef __ELF__
/*
* Walk through the entire symbol table and add any symbols we find there.
* This can be used in cases where we have stripped ELF shared libraries,
* or it can be used in cases where we have data symbols for which the address
* isn't encoded in the stabs.
*
* This is all really quite easy, since we don't have to worry about line
* numbers or local data variables.
*/
static
int
DEBUG_ProcessElfSymtab(char * addr, unsigned int load_offset,
Elf32_Shdr * symtab, Elf32_Shdr * strtab)
{
char * curfile = NULL;
struct name_hash * curr_sym = NULL;
int flags;
int i;
DBG_ADDR new_addr;
int nsym;
char * strp;
char * symname;
Elf32_Sym * symp;
symp = (Elf32_Sym *) (addr + symtab->sh_offset);
nsym = symtab->sh_size / sizeof(*symp);
strp = (char *) (addr + strtab->sh_offset);
for(i=0; i < nsym; i++, symp++)
{
/*
* Ignore certain types of entries which really aren't of that much
* interest.
*/
if( ELF32_ST_TYPE(symp->st_info) == STT_SECTION )
{
continue;
}
symname = strp + symp->st_name;
/*
* Save the name of the current file, so we have a way of tracking
* static functions/data.
*/
if( ELF32_ST_TYPE(symp->st_info) == STT_FILE )
{
curfile = symname;
continue;
}
/*
* See if we already have something for this symbol.
* If so, ignore this entry, because it would have come from the
* stabs or from a previous symbol. If the value is different,
* we will have to keep the darned thing, because there can be
* multiple local symbols by the same name.
*/
if( (DEBUG_GetSymbolValue(symname, -1, &new_addr, FALSE ) == TRUE)
&& (new_addr.off == (load_offset + symp->st_value)) )
continue;
new_addr.seg = 0;
new_addr.type = NULL;
new_addr.off = load_offset + symp->st_value;
flags = SYM_WINE | (ELF32_ST_BIND(symp->st_info) == STT_FUNC
? SYM_FUNC : SYM_DATA);
if( ELF32_ST_BIND(symp->st_info) == STB_GLOBAL )
curr_sym = DEBUG_AddSymbol( symname, &new_addr, NULL, flags );
else
curr_sym = DEBUG_AddSymbol( symname, &new_addr, curfile, flags );
/*
* Record the size of the symbol. This can come in handy in
* some cases. Not really used yet, however.
*/
if( symp->st_size != 0 )
DEBUG_SetSymbolSize(curr_sym, symp->st_size);
}
return TRUE;
}
static
int
DEBUG_ProcessElfObject(char * filename, unsigned int load_offset)
{
int rtn = FALSE;
struct stat statbuf;
int fd = -1;
int status;
char * addr = (char *) 0xffffffff;
Elf32_Ehdr * ehptr;
Elf32_Shdr * spnt;
char * shstrtab;
int nsect;
int i;
int stabsect;
int stabstrsect;
/*
* Make sure we can stat and open this file.
*/
if( filename == NULL )
goto leave;
status = stat(filename, &statbuf);
if( status == -1 )
{
char *s,*t,*fn,*paths;
if (strchr(filename,'/'))
goto leave;
paths = xstrdup(getenv("PATH"));
s = paths;
while (s && *s) {
t = strchr(s,':');
if (t) *t='\0';
fn = (char*)xmalloc(strlen(filename)+1+strlen(s)+1);
strcpy(fn,s);
strcat(fn,"/");
strcat(fn,filename);
if ((rtn = DEBUG_ProcessElfObject(fn,load_offset))) {
free(fn);
free(paths);
goto leave;
}
free(fn);
if (t) s = t+1;
}
if (!s || !*s)
fprintf(stderr," %s not found",filename);
free(paths);
goto leave;
}
/*
* Now open the file, so that we can mmap() it.
*/
fd = open(filename, O_RDONLY);
if( fd == -1 )
goto leave;
/*
* Now mmap() the file.
*/
addr = mmap(0, statbuf.st_size, PROT_READ,
MAP_PRIVATE, fd, 0);
if( addr == (char *) 0xffffffff )
goto leave;
/*
* Give a nice status message here...
* Well not, just print the name.
*/
fprintf(stderr, " %s", filename);
/*
* Next, we need to find a few of the internal ELF headers within
* this thing. We need the main executable header, and the section
* table.
*/
ehptr = (Elf32_Ehdr *) addr;
if( load_offset == 0 )
DEBUG_RegisterELFDebugInfo(ehptr->e_entry, statbuf.st_size, filename);
else
DEBUG_RegisterELFDebugInfo(load_offset, statbuf.st_size, filename);
spnt = (Elf32_Shdr *) (addr + ehptr->e_shoff);
nsect = ehptr->e_shnum;
shstrtab = (addr + spnt[ehptr->e_shstrndx].sh_offset);
stabsect = stabstrsect = -1;
for(i=0; i < nsect; i++)
{
if( strcmp(shstrtab + spnt[i].sh_name, ".stab") == 0 )
stabsect = i;
if( strcmp(shstrtab + spnt[i].sh_name, ".stabstr") == 0 )
stabstrsect = i;
}
if( stabsect == -1 || stabstrsect == -1 )
goto leave;
/*
* OK, now just parse all of the stabs.
*/
rtn = DEBUG_ParseStabs(addr, load_offset,
spnt[stabsect].sh_offset,
spnt[stabsect].sh_size,
spnt[stabstrsect].sh_offset,
spnt[stabstrsect].sh_size);
if( rtn != TRUE )
goto leave;
for(i=0; i < nsect; i++)
{
if( (strcmp(shstrtab + spnt[i].sh_name, ".symtab") == 0)
&& (spnt[i].sh_type == SHT_SYMTAB) )
DEBUG_ProcessElfSymtab(addr, load_offset,
spnt + i, spnt + spnt[i].sh_link);
if( (strcmp(shstrtab + spnt[i].sh_name, ".dynsym") == 0)
&& (spnt[i].sh_type == SHT_DYNSYM) )
DEBUG_ProcessElfSymtab(addr, load_offset,
spnt + i, spnt + spnt[i].sh_link);
}
leave:
if( addr != (char *) 0xffffffff )
munmap(addr, statbuf.st_size);
if( fd != -1 )
close(fd);
return (rtn);
}
int
DEBUG_ReadExecutableDbgInfo(void)
{
Elf32_Ehdr * ehdr;
char * exe_name;
Elf32_Dyn * dynpnt;
struct r_debug * dbg_hdr;
struct link_map * lpnt = NULL;
extern Elf32_Dyn _DYNAMIC[];
int rtn = FALSE;
exe_name = DEBUG_argv0;
/*
* Make sure we can stat and open this file.
*/
if( exe_name == NULL )
goto leave;
DEBUG_ProcessElfObject(exe_name, 0);
/*
* Finally walk the tables that the dynamic loader maintains to find all
* of the other shared libraries which might be loaded. Perform the
* same step for all of these.
*/
dynpnt = _DYNAMIC;
if( dynpnt == NULL )
goto leave;
/*
* Now walk the dynamic section (of the executable, looking for a DT_DEBUG
* entry.
*/
for(; dynpnt->d_tag != DT_NULL; dynpnt++)
if( dynpnt->d_tag == DT_DEBUG )
break;
if( (dynpnt->d_tag != DT_DEBUG)
|| (dynpnt->d_un.d_ptr == 0) )
goto leave;
/*
* OK, now dig into the actual tables themselves.
*/
dbg_hdr = (struct r_debug *) dynpnt->d_un.d_ptr;
lpnt = dbg_hdr->r_map;
/*
* Now walk the linked list. In all known ELF implementations,
* the dynamic loader maintains this linked list for us. In some
* cases the first entry doesn't appear with a name, in other cases it
* does.
*/
for(; lpnt; lpnt = lpnt->l_next )
{
/*
* We already got the stuff for the executable using the
* argv[0] entry above. Here we only need to concentrate on any
* shared libraries which may be loaded.
*/
ehdr = (Elf32_Ehdr *) lpnt->l_addr;
if( (lpnt->l_addr == 0) || (ehdr->e_type != ET_DYN) )
continue;
if( lpnt->l_name != NULL )
DEBUG_ProcessElfObject(lpnt->l_name, lpnt->l_addr);
}
rtn = TRUE;
leave:
return (rtn);
}
#else /* !__ELF__ */
#ifdef linux
/*
* a.out linux.
*/
int
DEBUG_ReadExecutableDbgInfo(void)
{
char * addr = (char *) 0xffffffff;
char * exe_name;
struct exec * ahdr;
int fd = -1;
int rtn = FALSE;
unsigned int staboff;
struct stat statbuf;
int status;
unsigned int stroff;
exe_name = DEBUG_argv0;
/*
* Make sure we can stat and open this file.
*/
if( exe_name == NULL )
goto leave;
status = stat(exe_name, &statbuf);
if( status == -1 )
goto leave;
/*
* Now open the file, so that we can mmap() it.
*/
fd = open(exe_name, O_RDONLY);
if( fd == -1 )
goto leave;
/*
* Now mmap() the file.
*/
addr = mmap(0, statbuf.st_size, PROT_READ,
MAP_PRIVATE, fd, 0);
if( addr == (char *) 0xffffffff )
goto leave;
ahdr = (struct exec *) addr;
staboff = N_SYMOFF(*ahdr);
stroff = N_STROFF(*ahdr);
rtn = DEBUG_ParseStabs(addr, 0,
staboff,
ahdr->a_syms,
stroff,
statbuf.st_size - stroff);
/*
* Give a nice status message here...
*/
fprintf(stderr, " %s", exe_name);
rtn = TRUE;
leave:
if( addr != (char *) 0xffffffff )
munmap(addr, statbuf.st_size);
if( fd != -1 )
close(fd);
return (rtn);
}
#else
/*
* Non-linux, non-ELF platforms.
*/
int
DEBUG_ReadExecutableDbgInfo(void)
{
return FALSE;
}
#endif
#endif /* __ELF__ */