Porting pice. Added pe parsing in loader.

svn path=/trunk/; revision=2431
This commit is contained in:
Eugene Ingerman 2001-12-15 23:50:07 +00:00
parent a7d526433d
commit 3edb1beb95
5 changed files with 303 additions and 316 deletions

View file

@ -33,11 +33,6 @@ Copyright notice:
// includes // includes
#include "stdinc.h" #include "stdinc.h"
#include <linux/errno.h>
#include <asm/ioctl.h>
#include <sys/stat.h>
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
// constant defines // constant defines
@ -47,7 +42,7 @@ Copyright notice:
char SrcFileNames[2048][2048]; char SrcFileNames[2048][2048];
ULONG ulCurrentSrcFile = 0; ULONG ulCurrentSrcFile = 0;
int debugger_file; HANDLE debugger_file;
ULONG ulGlobalVerbose = 0; ULONG ulGlobalVerbose = 0;
@ -59,8 +54,8 @@ ULONG ulGlobalVerbose = 0;
void process_stabs( void process_stabs(
char* pExeName, // name of exe char* pExeName, // name of exe
int fileout, // symbol file handle int fileout, // symbol file handle
Elf32_Shdr* pSHdr, PIMAGE_SECTION_HEADER section, //Elf32_Shdr* pSHdr,
int nSHdrSize, int sectionHeadersSize, //int nSHdrSize,
void* p, // ptr to memory where whole exe was read void* p, // ptr to memory where whole exe was read
PSTAB_ENTRY pStab, // ptr to stabs PSTAB_ENTRY pStab, // ptr to stabs
int nStabLen, // size of stabs int nStabLen, // size of stabs
@ -71,7 +66,7 @@ void process_stabs(
char* pGlobalsStr, // ptr to global strings char* pGlobalsStr, // ptr to global strings
int nGlobalStrLen) // size of global strings int nGlobalStrLen) // size of global strings
{ {
int i,strLen; unsigned i,strLen;
int nOffset=0,nNextOffset=0; int nOffset=0,nNextOffset=0;
PSTAB_ENTRY pStabCopy = pStab; PSTAB_ENTRY pStabCopy = pStab;
char* pName,szCurrentPath[2048]; char* pName,szCurrentPath[2048];
@ -82,10 +77,11 @@ void process_stabs(
//printf("LOADER: enter process_stabs()\n"); //printf("LOADER: enter process_stabs()\n");
PICE_memset((void*)&SymbolFileHeader,0,sizeof(SymbolFileHeader)); //get the name of the executable file
memset((void*)&SymbolFileHeader,0,sizeof(SymbolFileHeader));
SymbolFileHeader.magic = PICE_MAGIC; SymbolFileHeader.magic = PICE_MAGIC;
PICE_strcpy(temp,pExeName); strcpy(temp,pExeName);
pSlash = strrchr(temp,'/'); pSlash = strrchr(temp,'\\');
pDot = strrchr(temp,'.'); pDot = strrchr(temp,'.');
if(pDot) if(pDot)
{ {
@ -95,7 +91,7 @@ void process_stabs(
{ {
pCopyExeName = pSlash+1; pCopyExeName = pSlash+1;
} }
PICE_strcpy(SymbolFileHeader.name,pCopyExeName); strcpy(SymbolFileHeader.name,pCopyExeName);
for(i=0;i<(nStabLen/sizeof(STAB_ENTRY));i++) for(i=0;i<(nStabLen/sizeof(STAB_ENTRY));i++)
{ {
@ -119,27 +115,27 @@ void process_stabs(
//printf("LOADER: changing string offset %x %x\n",nOffset,nNextOffset); //printf("LOADER: changing string offset %x %x\n",nOffset,nNextOffset);
break; break;
case N_SO: case N_SO:
if((strLen = PICE_strlen(pName))) if((strLen = strlen(pName)))
{ {
if(pName[strLen-1]!='/') if(pName[strLen-1]!='/')
{ {
if(strlen(szCurrentPath)) if(strlen(szCurrentPath))
{ {
//printf("LOADER: ###########################################################################\n"); //printf("LOADER: ###########################################################################\n");
PICE_strcat(szCurrentPath,pName); strcat(szCurrentPath,pName);
//printf("LOADER: changing source file %s\n",szCurrentPath); //printf("LOADER: changing source file %s\n",szCurrentPath);
PICE_strcpy(SrcFileNames[ulCurrentSrcFile++],szCurrentPath); strcpy(SrcFileNames[ulCurrentSrcFile++],szCurrentPath);
szCurrentPath[0]=0; szCurrentPath[0]=0;
} }
else else
{ {
//printf("LOADER: ###########################################################################\n"); //printf("LOADER: ###########################################################################\n");
//printf("LOADER: changing source file %s\n",pName); //printf("LOADER: changing source file %s\n",pName);
PICE_strcpy(SrcFileNames[ulCurrentSrcFile++],pName); strcpy(SrcFileNames[ulCurrentSrcFile++],pName);
} }
} }
else else
PICE_strcpy(szCurrentPath,pName); strcpy(szCurrentPath,pName);
} }
else else
{ {
@ -211,21 +207,20 @@ void process_stabs(
//printf("LOADER: SymbolFileHeader.ulSizeOfStabsStrings = %x (%x)\n",nStrLen,(LPSTR)pStr-(LPSTR)p); //printf("LOADER: SymbolFileHeader.ulSizeOfStabsStrings = %x (%x)\n",nStrLen,(LPSTR)pStr-(LPSTR)p);
SymbolFileHeader.ulOffsetToHeaders = sizeof(PICE_SYMBOLFILE_HEADER); SymbolFileHeader.ulOffsetToHeaders = sizeof(PICE_SYMBOLFILE_HEADER);
SymbolFileHeader.ulSizeOfHeader = nSHdrSize; SymbolFileHeader.ulSizeOfHeader = sectionHeadersSize;
SymbolFileHeader.ulOffsetToGlobals = sizeof(PICE_SYMBOLFILE_HEADER)+nSHdrSize; SymbolFileHeader.ulOffsetToGlobals = sizeof(PICE_SYMBOLFILE_HEADER)+sectionHeadersSize;
SymbolFileHeader.ulSizeOfGlobals = nGlobalLen; SymbolFileHeader.ulSizeOfGlobals = nGlobalLen;
SymbolFileHeader.ulOffsetToGlobalsStrings = sizeof(PICE_SYMBOLFILE_HEADER)+nSHdrSize+nGlobalLen; SymbolFileHeader.ulOffsetToGlobalsStrings = sizeof(PICE_SYMBOLFILE_HEADER)+sectionHeadersSize+nGlobalLen;
SymbolFileHeader.ulSizeOfGlobalsStrings = nGlobalStrLen; SymbolFileHeader.ulSizeOfGlobalsStrings = nGlobalStrLen;
SymbolFileHeader.ulOffsetToStabs = sizeof(PICE_SYMBOLFILE_HEADER)+nSHdrSize+nGlobalLen+nGlobalStrLen; SymbolFileHeader.ulOffsetToStabs = sizeof(PICE_SYMBOLFILE_HEADER)+sectionHeadersSize+nGlobalLen+nGlobalStrLen;
SymbolFileHeader.ulSizeOfStabs = nStabLen; SymbolFileHeader.ulSizeOfStabs = nStabLen;
SymbolFileHeader.ulOffsetToStabsStrings = sizeof(PICE_SYMBOLFILE_HEADER)+nSHdrSize+nGlobalLen+nGlobalStrLen+nStabLen; SymbolFileHeader.ulOffsetToStabsStrings = sizeof(PICE_SYMBOLFILE_HEADER)+sectionHeadersSize+nGlobalLen+nGlobalStrLen+nStabLen;
SymbolFileHeader.ulSizeOfStabsStrings = nStrLen; SymbolFileHeader.ulSizeOfStabsStrings = nStrLen;
SymbolFileHeader.ulOffsetToSrcFiles = sizeof(PICE_SYMBOLFILE_HEADER)+nSHdrSize+nGlobalLen+nGlobalStrLen+nStabLen+nStrLen; SymbolFileHeader.ulOffsetToSrcFiles = sizeof(PICE_SYMBOLFILE_HEADER)+sectionHeadersSize+nGlobalLen+nGlobalStrLen+nStabLen+nStrLen;
SymbolFileHeader.ulNumberOfSrcFiles = ulCurrentSrcFile; SymbolFileHeader.ulNumberOfSrcFiles = ulCurrentSrcFile;
write(fileout,&SymbolFileHeader,sizeof(SymbolFileHeader)); write(fileout,&SymbolFileHeader,sizeof(SymbolFileHeader));
write(fileout,section,sectionHeadersSize);
write(fileout,pSHdr,nSHdrSize);
write(fileout,pGlobals,nGlobalLen); write(fileout,pGlobals,nGlobalLen);
write(fileout,pGlobalsStr,nGlobalStrLen); write(fileout,pGlobalsStr,nGlobalStrLen);
write(fileout,pStab,nStabLen); write(fileout,pStab,nStabLen);
@ -238,17 +233,30 @@ void process_stabs(
PVOID pFile; PVOID pFile;
PICE_SYMBOLFILE_SOURCE pss; PICE_SYMBOLFILE_SOURCE pss;
file = open(SrcFileNames[i],O_RDONLY); file = _open(SrcFileNames[i],O_RDONLY);
if( file <= 0 ){
//let's try win format drive:/file
char srctmp[2048];
strcpy(srctmp, SrcFileNames[i] );
if(strncmp(srctmp,"//",2)==0){
*(srctmp) = *(srctmp+2);
*(srctmp+1) = ':';
*(srctmp+2) = '/';
file = _open(srctmp,O_RDONLY);
if( file <= 0 )
printf("Can't open file: %s\n", srctmp );
}
}
if(file>0) if(file>0)
{ {
//printf("LOADER: [%u] opened %s as FD %x\n",i,SrcFileNames[i],file); //printf("LOADER: [%u] opened %s as FD %x\n",i,SrcFileNames[i],file);
len = lseek(file,0,SEEK_END); len = _lseek(file,0,SEEK_END);
//printf("LOADER: length = %x\n",(int)len); //printf("LOADER: length = %x\n",(int)len);
lseek(file,0,SEEK_SET); _lseek(file,0,SEEK_SET);
PICE_strcpy(pss.filename,SrcFileNames[i]); strcpy(pss.filename,SrcFileNames[i]);
pss.ulOffsetToNext = len+sizeof(PICE_SYMBOLFILE_SOURCE); pss.ulOffsetToNext = len+sizeof(PICE_SYMBOLFILE_SOURCE);
pFile = malloc(len); pFile = malloc(len);
@ -256,15 +264,15 @@ void process_stabs(
if(pFile) if(pFile)
{ {
//printf("LOADER: reading file...\n"); //printf("LOADER: reading file...\n");
read(file,pFile,len); _read(file,pFile,len);
write(fileout,&pss,sizeof(PICE_SYMBOLFILE_SOURCE)); _write(fileout,&pss,sizeof(PICE_SYMBOLFILE_SOURCE));
//printf("LOADER: writing file...\n"); //printf("LOADER: writing file...\n");
write(fileout,pFile,len); _write(fileout,pFile,len);
free(pFile); free(pFile);
} }
close(file); _close(file);
} }
} }
@ -276,29 +284,28 @@ void process_stabs(
// find_stab_sections() // find_stab_sections()
// //
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
void find_stab_sections(void* p,Elf32_Shdr* pSHdr,PSTAB_ENTRY* ppStab,int* pLen,char** ppStr,int* pnStabStrLen,int num,int index) void find_stab_sections(void* p,PIMAGE_SECTION_HEADER section, unsigned cSections,
PSTAB_ENTRY* ppStab,int* pLen,char** ppStr,int* pnStabStrLen)
{ {
int i; unsigned i;
char* pStr = (char*)((int)p + pSHdr[index].sh_offset);
//printf("LOADER: enter find_stab_sections()\n"); //printf("LOADER: enter find_stab_sections()\n");
*ppStab = 0; *ppStab = 0;
*ppStr = 0; *ppStr = 0;
for(i=0;i<num;i++,pSHdr++)
for ( i=1; i <= cSections; i++, section++ )
{ {
int sh_name = pSHdr->sh_name;
//printf("LOADER: [%u] %32s %8x %8x %8x %8x %8x\n",i,&pStr[sh_name],pSHdr->sh_offset,pSHdr->sh_size,pSHdr->sh_addr,pSHdr->sh_type,pSHdr->sh_link); if(strcmp(section->Name,".stab") == 0)
if(PICE_strcmp(&pStr[sh_name],".stab") == 0)
{ {
*ppStab = (PSTAB_ENTRY)((int)p + pSHdr->sh_offset); *ppStab = (PSTAB_ENTRY)((int)p + section->PointerToRawData);
*pLen = pSHdr->sh_size; *pLen = section->SizeOfRawData;
//printf("LOADER: .stab @ %x (offset %x) len = %x\n",*ppStab,pSHdr->sh_offset,pSHdr->sh_size); printf("LOADER: .stab @ %x (offset %x) len = %x\n",*ppStab,section->PointerToRawData,section->SizeOfRawData);
} }
else if(PICE_strcmp(&pStr[sh_name],".stabstr") == 0) else if(strncmp(section->Name,".stabstr",strlen(".stabstr")) == 0)
{ {
*ppStr = (char*)((int)p + pSHdr->sh_offset); *ppStr = (char*)((int)p + section->PointerToRawData);
*pnStabStrLen = pSHdr->sh_size; *pnStabStrLen = section->SizeOfRawData;
//printf("LOADER: .stabstr @ %x (offset %x size=%u)\n",*ppStr,pSHdr->sh_offset,pSHdr->sh_size); printf("LOADER: .stabstr @ %x (offset %x) len = %x\n",*ppStab,section->PointerToRawData,section->SizeOfRawData);
} }
} }
@ -306,165 +313,106 @@ char* pStr = (char*)((int)p + pSHdr[index].sh_offset);
} }
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
// find_symtab() // process_pe()
// //
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
Elf32_Sym* find_symtab(void* p,Elf32_Shdr* pSHdrOrig,int num,int index,int* pLen,LPSTR* ppStr,int *pnSymStrLen) int process_pe(char* filename,int file,void* p,int len)
{ {
int i;
Elf32_Sym* pSym = NULL,*pSymOrig = NULL;
// char* pStr = (char*)((int)p + pSHdrOrig[index].sh_offset);
LPSTR pName;
ULONG ulSymTabEntries = 0,link=-1;
Elf32_Shdr* pSHdr;
//printf("LOADER: enter find_symtab()\n"); PIMAGE_DOS_HEADER pDosHeader;
PIMAGE_NT_HEADERS pNTHeaders;
// find global symbol table
pSHdr = pSHdrOrig;
for(i=0;i<num;i++,pSHdr++)
{
//int sh_name = pSHdr->sh_name;
//printf("LOADER: [%u] %32s %8x %8x %8x %8x %8x\n",i,pStr,pSHdr->sh_offset,pSHdr->sh_size,pSHdr->sh_addr,pSHdr->sh_type,pSHdr->sh_link);
if(pSHdr->sh_type == SHT_SYMTAB)
{
pSym = (Elf32_Sym*)((int)p+pSHdr->sh_offset);
//printf("LOADER: symbol table %u %x %u\n",i,pSHdr->sh_offset,pSHdr->sh_link);
ulSymTabEntries = pSHdr->sh_size;
link = pSHdr->sh_link;
}
}
if(link != (-1))
{
// find global string table
pSHdr = pSHdrOrig;
for(i=0;i<num;i++,pSHdr++)
{
//int sh_name = pSHdr->sh_name;
//printf("LOADER: [%u] %32s %8x %8x %8x %8x %8x\n",i,pStr,pSHdr->sh_offset,pSHdr->sh_size,pSHdr->sh_addr,pSHdr->sh_type,pSHdr->sh_link);
if(pSHdr->sh_type == SHT_STRTAB && i==link)
{
*ppStr = (LPSTR)((int)p+pSHdr->sh_offset);
*pnSymStrLen = pSHdr->sh_size;
}
}
if(*ppStr && pSym)
{
LPSTR pStr = *ppStr;
pSymOrig = pSym;
for(i=0;i<ulSymTabEntries/sizeof(Elf32_Sym);i++)
{
pName = &pStr[pSym->st_name];
//printf("LOADER: [%u] %32s %x %x %x %x\n",i,pName,pSym->st_name,pSym->st_value,pSym->st_info,pSym->st_other);
pSym++;
}
}
*pLen = ulSymTabEntries;
}
else
{
pSymOrig= NULL;
}
//printf("LOADER: leave find_symtab()\n");
return pSymOrig;
}
///////////////////////////////////////////////////////////////////////////////////
// process_elf()
//
///////////////////////////////////////////////////////////////////////////////////
int process_elf(char* filename,int file,void* p,int len)
{
Elf32_Ehdr* pEHdr =(Elf32_Ehdr*)p;
Elf32_Shdr* pSHdr;
char* pStr; char* pStr;
PSTAB_ENTRY pStab; PSTAB_ENTRY pStab;
int nStabLen,nSym; DWORD nStabLen,nSym;
LPSTR pStrTab; char* pStrTab;
Elf32_Sym* pSymTab; char* pSymTab;
char szSymName[2048]; char szSymName[2048];
int fileout; int fileout;
int nSymStrLen,nStabStrLen; int nSymStrLen,nStabStrLen;
int iRetVal = 0; int iRetVal = 0;
//printf("LOADER: enter process_elf()\n"); pDosHeader = (PIMAGE_DOS_HEADER)p;
if(strncmp(pEHdr->e_ident,"\177ELF",4) == 0) // is ELF binary magic pNTHeaders = (PIMAGE_NT_HEADERS)((DWORD)p + pDosHeader->e_lfanew);
if ((pDosHeader->e_magic == IMAGE_DOS_SIGNATURE)
&& (pDosHeader->e_lfanew != 0L)
&& (pNTHeaders->Signature == IMAGE_NT_SIGNATURE))
{ {
pSHdr = (Elf32_Shdr*)((int)p+pEHdr->e_shoff); if( pNTHeaders->FileHeader.PointerToSymbolTable ){
//printf("LOADER: Section header @ %x (offset %x)\n",pSHdr,pEHdr->e_shoff);
//printf("LOADER: %u entries\n",pEHdr->e_shnum); pSymTab = (char*)((DWORD)p + pNTHeaders->FileHeader.PointerToSymbolTable);
//printf("LOADER: string table index %u\n",pEHdr->e_shstrndx); nSym = pNTHeaders->FileHeader.NumberOfSymbols;
if((pSymTab = find_symtab(p,pSHdr,pEHdr->e_shnum,pEHdr->e_shstrndx,&nSym,&pStrTab,&nSymStrLen)) != NULL ) //string table follows immediately after symbol table. first 4 bytes give the length of the table
{ //references to string table include the first 4 bytes.
find_stab_sections(p,pSHdr,&pStab,&nStabLen,&pStr,&nStabStrLen,pEHdr->e_shnum,pEHdr->e_shstrndx); pStrTab = (PIMAGE_SYMBOL)pSymTab + nSym;
nSymStrLen = *((DWORD*)pStrTab);
find_stab_sections(p,IMAGE_FIRST_SECTION(pNTHeaders),pNTHeaders->FileHeader.NumberOfSections,
&pStab,&nStabLen,&pStr,&nStabStrLen);
if(pStab && nStabLen && pStr && nStabStrLen) if(pStab && nStabLen && pStr && nStabStrLen)
{ {
LPSTR pDot; LPSTR pDot;
PICE_strcpy(szSymName,filename); strcpy(szSymName,filename);
//printf("LOADER: file name = %s\n",szSymName); //printf("LOADER: file name = %s\n",szSymName);
if((pDot = strrchr(szSymName,'.'))) if((pDot = strrchr(szSymName,'.')))
{ {
*pDot = 0; *pDot = 0;
PICE_strcat(pDot,".sym"); strcat(pDot,".pice");
} }
else else
{ {
PICE_strcat(szSymName,".sym"); strcat(szSymName,".pice");
} }
//printf("LOADER: symbol file name = %s\n",szSymName); //printf("LOADER: symbol file name = %s\n",szSymName);
printf("LOADER: creating symbol file %s for %s\n",szSymName,filename); printf("LOADER: creating symbol file %s for %s\n",szSymName,filename);
fileout = creat(szSymName,S_IRUSR|S_IWUSR); // make r/w for owner fileout = _creat(szSymName, _S_IREAD | _S_IWRITE ); // make r/w
if(fileout != -1) if(fileout != -1)
{ {
process_stabs(szSymName, process_stabs(szSymName,
fileout, fileout,
pSHdr, IMAGE_FIRST_SECTION(pNTHeaders),
pEHdr->e_shnum*sizeof(Elf32_Shdr), pNTHeaders->FileHeader.NumberOfSections*sizeof(PIMAGE_SECTION_HEADER),
p, p,
pStab, pStab,
nStabLen, nStabLen,
pStr, pStr,
nStabStrLen, nStabStrLen,
(LPSTR)pSymTab, (char*)pSymTab,
nSym, nSym,
pStrTab, pStrTab,
nSymStrLen); nSymStrLen);
close(fileout); close(fileout);
} }
else else
{ {
printf("LOADER: creation of symbol file %s failed\n",szSymName); printf("LOADER: creation of symbol file %s failed\n",szSymName);
iRetVal = 2; iRetVal = 2;
} }
} }
else else
{ {
printf("LOADER: file %s has no data inside symbol tables\n",filename); printf("LOADER: file %s has no data inside symbol tables\n",filename);
if( ulGlobalVerbose ) if( ulGlobalVerbose )
{ {
if( !pStab || !nStabLen ) if( !pStab || !nStabLen )
printf("LOADER: - symbol table is empty or not present\n"); printf("LOADER: - symbol table is empty or not present\n");
if( !pStr || !nStabStrLen ) if( !pStr || !nStabStrLen )
printf("LOADER: - string table is empty or not present\n"); printf("LOADER: - string table is empty or not present\n");
} }
iRetVal = 2; iRetVal = 2;
} }
} }
else else{
{
printf("LOADER: file %s does not have a symbol table\n",filename); printf("LOADER: file %s does not have a symbol table\n",filename);
iRetVal = 2; iRetVal = 2;
} }
} }
else else
{ {
@ -472,7 +420,7 @@ int process_elf(char* filename,int file,void* p,int len)
iRetVal = 1; iRetVal = 1;
} }
//printf("LOADER: leave process_elf()\n"); //printf("LOADER: leave process_pe()\n");
return iRetVal; return iRetVal;
} }
@ -488,30 +436,29 @@ int process_file(char* filename)
int iRetVal=0; int iRetVal=0;
//printf("LOADER: enter process_file()\n"); //printf("LOADER: enter process_file()\n");
file = open(filename,O_RDONLY); file = _open(filename,O_RDONLY|_O_BINARY);
if(file>0) if(file>0)
{ {
//printf("LOADER: opened %s as FD %x\n",filename,file); //printf("LOADER: opened %s as FD %x\n",filename,file);
len = lseek(file,0,SEEK_END); len = _lseek(file,0,SEEK_END);
printf("LOADER: file %s is %u bytes\n",filename,(int)len); printf("LOADER: file %s is %u bytes\n",filename,(int)len);
lseek(file,0,SEEK_SET); _lseek(file,0,SEEK_SET);
p = malloc(len+16); p = malloc(len+16);
if(p) if(p)
{ {
long count;
//printf("LOADER: malloc'd @ %x\n",p); //printf("LOADER: malloc'd @ %x\n",p);
PICE_memset(p,0,len+16); memset(p,0,len+16);
if(len == (count = _read(file,p,len)))
if(len == read(file,p,len))
{ {
//printf("LOADER: trying ELF format\n"); //printf("LOADER: trying ELF format\n");
iRetVal = process_elf(filename,file,p,len); iRetVal = process_pe(filename,file,p,len);
} }
} }
_close(file);
close(file);
} }
else else
{ {
@ -527,12 +474,12 @@ int process_file(char* filename)
// open_debugger() // open_debugger()
// //
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
int open_debugger(void) HANDLE open_debugger(void)
{ {
debugger_file = open("/dev/pice0",O_RDONLY); debugger_file = CreateFile("\\Device\\Pice",GENERIC_READ,0,NULL,OPEN_EXISTING,NULL,NULL);
if(debugger_file<0) if(debugger_file == INVALID_HANDLE_VALUE)
{ {
printf("LOADER: debugger is not loaded\n"); printf("LOADER: debugger is not loaded. Last Error: %ld\n", GetLastError());
} }
return debugger_file; return debugger_file;
@ -544,7 +491,24 @@ int open_debugger(void)
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
void close_debugger(void) void close_debugger(void)
{ {
close(debugger_file); if( !CloseHandle(debugger_file) ){
printf("Error closing debugger handle: %ld\n", GetLastError());
}
}
int ioctl( HANDLE device, DWORD ioctrlcode, PDEBUGGER_STATUS_BLOCK psb)
{
DEBUGGER_STATUS_BLOCK tsb;
DWORD bytesreturned;
if( !DeviceIoControl( device, ioctrlcode, psb, sizeof(DEBUGGER_STATUS_BLOCK),
&tsb, sizeof(DEBUGGER_STATUS_BLOCK),&bytesreturned, NULL) ){
printf("Error in DeviceIoControl: %ld\n", GetLastError());
return -EINVAL;
}
else{
memcpy( psb, &tsb, sizeof(DEBUGGER_STATUS_BLOCK) );
}
return 0;
} }
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
@ -576,28 +540,31 @@ void banner(void)
void change_symbols(int action,char* pfilename) void change_symbols(int action,char* pfilename)
{ {
int iRetVal = 0; int iRetVal = 0;
DEBUGGER_STATUS_BLOCK sb;
strcpy(sb.filename, pfilename);
switch(action) switch(action)
{ {
case ACTION_LOAD: case ACTION_LOAD:
printf("LOADER: loading symbols from %s\n",pfilename); printf("LOADER: loading symbols from %s\n",pfilename);
if(open_debugger()>=0) if(open_debugger() != INVALID_HANDLE_VALUE)
{ {
iRetVal = ioctl(debugger_file,PICE_IOCTL_LOAD,pfilename); iRetVal = ioctl(debugger_file,PICE_IOCTL_LOAD,&sb);
close_debugger(); close_debugger();
} }
break; break;
case ACTION_UNLOAD: case ACTION_UNLOAD:
printf("LOADER: unloading symbols from %s\n",pfilename); printf("LOADER: unloading symbols from %s\n",pfilename);
if(open_debugger()>=0) if(open_debugger() != INVALID_HANDLE_VALUE)
{ {
iRetVal = ioctl(debugger_file,PICE_IOCTL_UNLOAD,pfilename); iRetVal = ioctl(debugger_file,PICE_IOCTL_UNLOAD,&sb);
close_debugger(); close_debugger();
} }
break; break;
case ACTION_RELOAD: case ACTION_RELOAD:
printf("LOADER: reloading all symbols\n"); printf("LOADER: reloading all symbols\n");
if(open_debugger()>=0) if(open_debugger() != INVALID_HANDLE_VALUE)
{ {
ioctl(debugger_file,PICE_IOCTL_RELOAD,NULL); ioctl(debugger_file,PICE_IOCTL_RELOAD,NULL);
close_debugger(); close_debugger();
@ -621,6 +588,8 @@ void change_symbols(int action,char* pfilename)
} }
} }
// Dynamic install to be added later
#if 0
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
// tryinstall() // tryinstall()
// //
@ -634,7 +603,7 @@ int tryinstall(void)
banner(); banner();
printf("LOADER: trying to install debugger...\n"); printf("LOADER: trying to install debugger...\n");
if(!(open_debugger() < 0) ) if( open_debugger() != INVALID_HANDLE_VALUE )
{ {
printf("LOADER: debugger already installed...\n"); printf("LOADER: debugger already installed...\n");
close_debugger(); close_debugger();
@ -698,7 +667,7 @@ int tryuninstall(void)
printf("LOADER: trying to remove debugger...\n"); printf("LOADER: trying to remove debugger...\n");
// check for loaded debugger // check for loaded debugger
if(open_debugger() < 0) if(open_debugger() == INVALID_HANDLE_VALUE)
{ {
return -1; return -1;
} }
@ -747,6 +716,7 @@ int tryuninstall(void)
} }
return err; return err;
} }
#endif
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
// showstatus() // showstatus()
@ -757,12 +727,11 @@ void showstatus(void)
DEBUGGER_STATUS_BLOCK sb; DEBUGGER_STATUS_BLOCK sb;
int iRetVal; int iRetVal;
if(open_debugger()>=0) if(open_debugger() != INVALID_HANDLE_VALUE)
{ {
sb.Test = 0;
iRetVal = ioctl(debugger_file,PICE_IOCTL_STATUS,&sb); iRetVal = ioctl(debugger_file,PICE_IOCTL_STATUS,&sb);
printf("LOADER: Test = %X\n",sb.Test); //printf("LOADER: Test = %X\n",sb.Test);
close_debugger(); close_debugger();
} }
} }
@ -775,10 +744,9 @@ void dobreak(void)
{ {
int iRetVal; int iRetVal;
if(open_debugger()>=0) if(open_debugger() != INVALID_HANDLE_VALUE)
{ {
iRetVal = ioctl(debugger_file,PICE_IOCTL_BREAK,NULL); iRetVal = ioctl(debugger_file,PICE_IOCTL_BREAK,NULL);
close_debugger(); close_debugger();
} }
} }
@ -787,6 +755,7 @@ void dobreak(void)
// doterminal() // doterminal()
// //
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
#if 0
void doterminal(void) void doterminal(void)
{ {
if(SetupSerial(2,B115200)) if(SetupSerial(2,B115200))
@ -795,6 +764,7 @@ void doterminal(void)
CloseSerial(); CloseSerial();
} }
} }
#endif
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
// process_switches() // process_switches()
@ -818,44 +788,44 @@ int process_switches(int argc,char* argv[])
int new_action=ACTION_NONE; int new_action=ACTION_NONE;
parg++; parg++;
if(PICE_strcmp(parg,"load")==0 || PICE_strcmp(parg,"l")==0) if(strcmp(parg,"load")==0 || strcmp(parg,"l")==0)
{ {
new_action = ACTION_LOAD; new_action = ACTION_LOAD;
} }
else if(PICE_strcmp(parg,"unload")==0 || PICE_strcmp(parg,"u")==0) else if(strcmp(parg,"unload")==0 || strcmp(parg,"u")==0)
{ {
new_action = ACTION_UNLOAD; new_action = ACTION_UNLOAD;
} }
else if(PICE_strcmp(parg,"trans")==0 || PICE_strcmp(parg,"t")==0) else if(strcmp(parg,"trans")==0 || strcmp(parg,"t")==0)
{ {
new_action = ACTION_TRANS; new_action = ACTION_TRANS;
} }
else if(PICE_strcmp(parg,"reload")==0 || PICE_strcmp(parg,"r")==0) else if(strcmp(parg,"reload")==0 || strcmp(parg,"r")==0)
{ {
new_action = ACTION_RELOAD; new_action = ACTION_RELOAD;
} }
else if(PICE_strcmp(parg,"verbose")==0 || PICE_strcmp(parg,"v")==0) else if(strcmp(parg,"verbose")==0 || strcmp(parg,"v")==0)
{ {
if( ulGlobalVerbose+1 > ulGlobalVerbose ) if( ulGlobalVerbose+1 > ulGlobalVerbose )
ulGlobalVerbose++; ulGlobalVerbose++;
} }
else if(PICE_strcmp(parg,"install")==0 || PICE_strcmp(parg,"i")==0) else if(strcmp(parg,"install")==0 || strcmp(parg,"i")==0)
{ {
new_action = ACTION_INSTALL; new_action = ACTION_INSTALL;
} }
else if(PICE_strcmp(parg,"uninstall")==0 || PICE_strcmp(parg,"x")==0) else if(strcmp(parg,"uninstall")==0 || strcmp(parg,"x")==0)
{ {
new_action = ACTION_UNINSTALL; new_action = ACTION_UNINSTALL;
} }
else if(PICE_strcmp(parg,"status")==0 || PICE_strcmp(parg,"s")==0) else if(strcmp(parg,"status")==0 || strcmp(parg,"s")==0)
{ {
new_action = ACTION_STATUS; new_action = ACTION_STATUS;
} }
else if(PICE_strcmp(parg,"break")==0 || PICE_strcmp(parg,"b")==0) else if(strcmp(parg,"break")==0 || strcmp(parg,"b")==0)
{ {
new_action = ACTION_BREAK; new_action = ACTION_BREAK;
} }
else if(PICE_strcmp(parg,"serial")==0 || PICE_strcmp(parg,"ser")==0) else if(strcmp(parg,"serial")==0 || strcmp(parg,"ser")==0)
{ {
new_action = ACTION_TERMINAL; new_action = ACTION_TERMINAL;
} }
@ -907,6 +877,7 @@ int process_switches(int argc,char* argv[])
case ACTION_RELOAD : case ACTION_RELOAD :
/* filename parameter is optional */ /* filename parameter is optional */
break; break;
#if 0
case ACTION_UNINSTALL: case ACTION_UNINSTALL:
close_debugger(); close_debugger();
tryuninstall(); tryuninstall();
@ -914,15 +885,18 @@ int process_switches(int argc,char* argv[])
case ACTION_INSTALL: case ACTION_INSTALL:
tryinstall(); tryinstall();
break; break;
#endif
case ACTION_STATUS: case ACTION_STATUS:
showstatus(); showstatus();
break; break;
case ACTION_BREAK: case ACTION_BREAK:
dobreak(); dobreak();
break; break;
#if 0
case ACTION_TERMINAL: case ACTION_TERMINAL:
doterminal(); doterminal();
break; break;
#endif
case ACTION_NONE : case ACTION_NONE :
printf("LOADER: no action specified specifed on commandline\n"); printf("LOADER: no action specified specifed on commandline\n");
error = 1; error = 1;
@ -993,26 +967,12 @@ void showpermission(void)
/////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////
int main(int argc,char* argv[]) int main(int argc,char* argv[])
{ {
int uid;
// find out who's using us
// if it's not the superuser, bail!
if(argc==1 || argc>3) if(argc==1 || argc>3)
{ {
showhelp(); showhelp();
return 1; return 1;
} }
else
{
uid = getuid();
if(uid != 0)
{
showpermission();
return 0;
}
return process_switches(argc,argv); return process_switches(argc,argv);
}
} }

View file

@ -1,6 +1,7 @@
CC = gcc CC = gcc
CFLAGS := -Wall -fomit-frame-pointer -O2 -DLINUX CFLAGS := -Wall -fomit-frame-pointer -O2 -DLINUX
LDFLAGS := -dynamic -lncurses LDFLAGS := -dynamic
#-lncurses
OBJS = main.o terminal.o OBJS = main.o terminal.o

View file

@ -1,18 +1,25 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <malloc.h> #include <malloc.h>
#include <io.h>
//#include <ddk/winnt.h>
#include <windows.h>
#include <winnt.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> //#include <sys/wait.h>
#include <sys/ioctl.h> //#include <sys/ioctl.h>
#include <sys/signal.h> //#include <sys/signal.h>
#include <SYS/STAT.H>
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> //#include <unistd.h>
#include <linux/unistd.h> //#include <linux/unistd.h>
#include <elf.h>
//#include "../../../include/pe.h"
#include "stab_gnu.h" #include "stab_gnu.h"
#include "retypes.h" //#include "retypes.h"
#include "terminal.h" //#include "terminal.h"
#include <termios.h> //#include <termios.h>
#include "../shared/shared.h" #include "../shared/shared.h"

View file

@ -27,6 +27,7 @@ Copyright notice:
This file may be distributed under the terms of the GNU Public License. This file may be distributed under the terms of the GNU Public License.
--*/ --*/
#if 0 //ei not ready
#include "stdinc.h" #include "stdinc.h"
#include <curses.h> #include <curses.h>
@ -426,3 +427,4 @@ void DebuggerShell(void)
} }
} }
} }
#endif

View file

@ -35,7 +35,24 @@ Copyright notice:
--*/ --*/
#include <ddk/ntddk.h> //#include <ddk/ntddk.h>
#include <winnt.h>
//temporary
#ifndef CTL_CODE
#define CTL_CODE(Dev, Func, Meth, Acc) ( ((Dev)<<16) | ((Acc)<<14) | ((Func)<<2) | (Meth))
// IOCTL Parameter buffering methods
#define METHOD_BUFFERED 0
#define METHOD_IN_DIRECT 1
#define METHOD_OUT_DIRECT 2
#define METHOD_NEITHER 3
// IOCTL File access type
#define FILE_ANY_ACCESS 0
#define FILE_READ_ACCESS 1
#define FILE_WRITE_ACCESS 2
#endif
// define custom device type // define custom device type
#define PICE_DEVICE_DEBUGGER 64787 #define PICE_DEVICE_DEBUGGER 64787
@ -49,7 +66,7 @@ Copyright notice:
typedef struct _DEBUGGER_STATUS_BLOCK typedef struct _DEBUGGER_STATUS_BLOCK
{ {
ULONG Test; char filename[256];
}DEBUGGER_STATUS_BLOCK,*PDEBUGGER_STATUS_BLOCK; }DEBUGGER_STATUS_BLOCK,*PDEBUGGER_STATUS_BLOCK;
#define MAGIC_ULONG( ch0, ch1, ch2, ch3 ) \ #define MAGIC_ULONG( ch0, ch1, ch2, ch3 ) \