win64: update tiny_impdef, tiny_libmaker (Elf64)

This commit is contained in:
grischka 2009-07-18 22:06:14 +02:00
parent 09ac9faf59
commit 06aed3d171
2 changed files with 149 additions and 388 deletions

View File

@ -23,201 +23,92 @@
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#include <windows.h> #include <windows.h>
#include <stdio.h> #include <stdio.h>
#include <malloc.h>
#include "../../config.h"
/* Offset to PE file signature */ int read_mem(FILE *fp, unsigned offset, void *buffer, unsigned len)
#define NTSIGNATURE(a) ((LPVOID)((BYTE *)a + \
((PIMAGE_DOS_HEADER)a)->e_lfanew))
/* MS-OS header identifies the NT PEFile signature dword;
the PEFILE header exists just after that dword. */
#define PEFHDROFFSET(a) ((LPVOID)((BYTE *)a + \
((PIMAGE_DOS_HEADER)a)->e_lfanew + \
SIZE_OF_NT_SIGNATURE))
/* PE optional header is immediately after PEFile header. */
#define OPTHDROFFSET(a) ((LPVOID)((BYTE *)a + \
((PIMAGE_DOS_HEADER)a)->e_lfanew + \
SIZE_OF_NT_SIGNATURE + \
sizeof (IMAGE_FILE_HEADER)))
/* Section headers are immediately after PE optional header. */
#define SECHDROFFSET(a) ((LPVOID)((BYTE *)a + \
((PIMAGE_DOS_HEADER)a)->e_lfanew + \
SIZE_OF_NT_SIGNATURE + \
sizeof (IMAGE_FILE_HEADER) + \
sizeof (IMAGE_OPTIONAL_HEADER)))
#define SIZE_OF_NT_SIGNATURE 4
/* -------------------------------------------------------------- */
int WINAPI NumOfSections (
LPVOID lpFile)
{ {
/* Number of sections is indicated in file header. */ fseek(fp, offset, 0);
return (int) return len == fread(buffer, 1, len, fp);
((PIMAGE_FILE_HEADER)
PEFHDROFFSET(lpFile))->NumberOfSections;
} }
char *get_export_names(FILE *fp)
/* -------------------------------------------------------------- */
LPVOID WINAPI ImageDirectoryOffset (
LPVOID lpFile,
DWORD dwIMAGE_DIRECTORY)
{ {
PIMAGE_OPTIONAL_HEADER poh; int l, i, n, n0;
PIMAGE_SECTION_HEADER psh; char *p;
int nSections = NumOfSections (lpFile);
int i = 0;
LPVOID VAImageDir;
/* Retrieve offsets to optional and section headers. */ IMAGE_SECTION_HEADER ish;
poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile); IMAGE_EXPORT_DIRECTORY ied;
psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile); IMAGE_DOS_HEADER dh;
IMAGE_FILE_HEADER ih;
DWORD sig, ref, addr, ptr, namep;
#ifdef TCC_TARGET_X86_64
IMAGE_OPTIONAL_HEADER64 oh;
const int MACHINE = 0x8664;
#else
IMAGE_OPTIONAL_HEADER32 oh;
const int MACHINE = 0x014C;
#endif
int pef_hdroffset, opt_hdroffset, sec_hdroffset;
/* Must be 0 thru (NumberOfRvaAndSizes-1). */ n = n0 = 0;
if (dwIMAGE_DIRECTORY >= poh->NumberOfRvaAndSizes) p = NULL;
return NULL;
/* Locate image directory's relative virtual address. */ if (!read_mem(fp, 0, &dh, sizeof dh))
VAImageDir = (LPVOID)poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress; goto the_end;
if (!read_mem(fp, dh.e_lfanew, &sig, sizeof sig))
goto the_end;
if (sig != 0x00004550)
goto the_end;
pef_hdroffset = dh.e_lfanew + sizeof sig;
if (!read_mem(fp, pef_hdroffset, &ih, sizeof ih))
goto the_end;
if (MACHINE != ih.Machine)
goto the_end;
opt_hdroffset = pef_hdroffset + sizeof ih;
sec_hdroffset = opt_hdroffset + sizeof oh;
if (!read_mem(fp, opt_hdroffset, &oh, sizeof oh))
goto the_end;
/* Locate section containing image directory. */ if (IMAGE_DIRECTORY_ENTRY_EXPORT >= oh.NumberOfRvaAndSizes)
while (i++<nSections) goto the_end;
{
if (psh->VirtualAddress <= (DWORD)VAImageDir addr = oh.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
&& psh->VirtualAddress + psh->SizeOfRawData > (DWORD)VAImageDir) //printf("addr: %08x\n", addr);
break; for (i = 0; i < ih.NumberOfSections; ++i) {
psh++; if (!read_mem(fp, sec_hdroffset + i * sizeof ish, &ish, sizeof ish))
goto the_end;
//printf("vaddr: %08x\n", ish.VirtualAddress);
if (addr >= ish.VirtualAddress && addr < ish.VirtualAddress + ish.SizeOfRawData)
goto found;
} }
goto the_end;
if (i > nSections) found:
return NULL; ref = ish.VirtualAddress - ish.PointerToRawData;
if (!read_mem(fp, addr - ref, &ied, sizeof ied))
goto the_end;
/* Return image import directory offset. */ namep = ied.AddressOfNames - ref;
return (LPVOID)(((int)lpFile + for (i = 0; i < ied.NumberOfNames; ++i) {
(int)VAImageDir - psh->VirtualAddress) + if (!read_mem(fp, namep, &ptr, sizeof ptr))
(int)psh->PointerToRawData); goto the_end;
} namep += sizeof ptr;
for (l = 0;;) {
/* -------------------------------------------------------------- */ if (n+1 >= n0)
p = realloc(p, n0 = n0 ? n0 * 2 : 256);
BOOL WINAPI GetSectionHdrByName ( if (!read_mem(fp, ptr - ref + l, p + n, 1) || ++l >= 80) {
LPVOID lpFile, free(p), p = NULL;
IMAGE_SECTION_HEADER *sh, goto the_end;
char *szSection)
{
PIMAGE_SECTION_HEADER psh;
int nSections = NumOfSections (lpFile);
int i;
if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL)
{
/* find the section by name */
for (i=0; i<nSections; i++)
{
if (!strcmp (psh->Name, szSection))
{
/* copy data to header */
memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
return TRUE;
} }
else if (p[n++] == 0)
psh++; break;
} }
} }
return FALSE; if (p)
} p[n] = 0;
the_end:
/* -------------------------------------------------------------- */ return p;
BOOL WINAPI GetSectionHdrByAddress (
LPVOID lpFile,
IMAGE_SECTION_HEADER *sh,
DWORD addr)
{
PIMAGE_SECTION_HEADER psh;
int nSections = NumOfSections (lpFile);
int i;
if ((psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile)) != NULL)
{
/* find the section by name */
for (i=0; i<nSections; i++)
{
if (addr >= psh->VirtualAddress
&& addr < psh->VirtualAddress + psh->SizeOfRawData)
{
/* copy data to header */
memcpy ((LPVOID)sh, (LPVOID)psh, sizeof (IMAGE_SECTION_HEADER));
return TRUE;
}
else
psh++;
}
}
return FALSE;
}
/* -------------------------------------------------------------- */
int WINAPI GetExportFunctionNames (
LPVOID lpFile,
HANDLE hHeap,
char **pszFunctions)
{
IMAGE_SECTION_HEADER sh;
PIMAGE_EXPORT_DIRECTORY ped;
int *pNames, *pCnt;
char *pSrc, *pDest;
int i, nCnt;
DWORD VAImageDir;
PIMAGE_OPTIONAL_HEADER poh;
char *pOffset;
/* Get section header and pointer to data directory
for .edata section. */
if (NULL == (ped = (PIMAGE_EXPORT_DIRECTORY)
ImageDirectoryOffset (lpFile, IMAGE_DIRECTORY_ENTRY_EXPORT)))
return 0;
poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile);
VAImageDir = poh->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress;
if (FALSE == GetSectionHdrByAddress (lpFile, &sh, VAImageDir))
return 0;
pOffset = (char *)lpFile + (sh.PointerToRawData - sh.VirtualAddress);
pNames = (int *)(pOffset + (DWORD)ped->AddressOfNames);
/* Figure out how much memory to allocate for all strings. */
nCnt = 1;
for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++)
{
pSrc = (pOffset + *pCnt++);
if (pSrc)
nCnt += strlen(pSrc)+1;
}
/* Allocate memory off heap for function names. */
pDest = *pszFunctions = HeapAlloc (hHeap, HEAP_ZERO_MEMORY, nCnt);
/* Copy all strings to buffer. */
for (i=0, pCnt = pNames; i<(int)ped->NumberOfNames; i++)
{
pSrc = (pOffset + *pCnt++);
if (pSrc) {
strcpy(pDest, pSrc);
pDest += strlen(pSrc)+1;
}
}
*pDest = 0;
return ped->NumberOfNames;
} }
/* -------------------------------------------------------------- */ /* -------------------------------------------------------------- */
@ -238,61 +129,49 @@ static char *file_basename(const char *name)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
HANDLE hHeap; int ret, v, i;
HANDLE hFile;
HANDLE hMapObject;
VOID *pMem;
int nCnt, ret, n;
char *pNames;
char infile[MAX_PATH]; char infile[MAX_PATH];
char buffer[MAX_PATH];
char outfile[MAX_PATH]; char outfile[MAX_PATH];
FILE *op;
char *p;
hHeap = NULL; static const char *ext[] = { ".dll", ".exe", NULL };
hFile = NULL; const char *file, **pp;
hMapObject = NULL; char path[MAX_PATH], *p, *q;
pMem = NULL; FILE *fp, *op;
infile[0] = 0; infile[0] = 0;
outfile[0] = 0; outfile[0] = 0;
fp = op = NULL;
v = 0;
ret = 1; ret = 1;
for (n = 1; n < argc; ++n) for (i = 1; i < argc; ++i) {
{ const char *a = argv[i];
const char *a = argv[n];
if ('-' == a[0]) { if ('-' == a[0]) {
if (0 == strcmp(a, "-o")) { if (0 == strcmp(a, "-v")) {
if (++n == argc) v = 1;
} else if (0 == strcmp(a, "-o")) {
if (++i == argc)
goto usage; goto usage;
strcpy(outfile, argv[n]); strcpy(outfile, argv[i]);
} } else
else
goto usage; goto usage;
} else if (0 == infile[0]) } else if (0 == infile[0])
strcpy(infile, a); strcpy(infile, a);
else else
goto usage; goto usage;
} }
if (0 == infile[0]) if (0 == infile[0]) {
{
usage: usage:
fprintf(stderr, fprintf(stderr,
"tiny_impdef creates an export definition file (.def) from a dll\n" "tiny_impdef: create export definition file (.def) from a dll\n"
"Usage: tiny_impdef library.dll [-o outputfile]\n" "Usage: tiny_impdef library.dll [-o outputfile]\n"
); );
goto the_end; goto the_end;
} }
if (SearchPath(NULL, infile, ".dll", sizeof buffer, buffer, NULL))
strcpy(infile, buffer);
if (0 == outfile[0]) if (0 == outfile[0])
{ {
char *p;
strcpy(outfile, file_basename(infile)); strcpy(outfile, file_basename(infile));
p = strrchr(outfile, '.'); p = strrchr(outfile, '.');
if (NULL == p) if (NULL == p)
@ -300,93 +179,55 @@ usage:
strcpy(p, ".def"); strcpy(p, ".def");
} }
hFile = CreateFile( file = infile;
infile, #ifdef _WIN32
GENERIC_READ, for (pp = ext; *pp; ++pp)
FILE_SHARE_READ, if (SearchPath(NULL, file, *pp, sizeof path, path, NULL)) {
NULL, file = path;
OPEN_EXISTING, break;
0, }
NULL #endif
);
if (hFile == INVALID_HANDLE_VALUE) fp = fopen(file, "rb");
{ if (NULL == fp) {
fprintf(stderr, "No such file: %s\n", infile); fprintf(stderr, "tiny_impdef: no such file: %s\n", infile);
goto the_end; goto the_end;
} }
hMapObject = CreateFileMapping(
hFile,
NULL,
PAGE_READONLY,
0, 0,
NULL
);
if (NULL == hMapObject)
{
fprintf(stderr, "Could not create file mapping: %s\n", infile);
goto the_end;
}
pMem = MapViewOfFile(
hMapObject, // object to map view of
FILE_MAP_READ, // read access
0, // high offset: map from
0, // low offset: beginning
0); // default: map entire file
if (NULL == pMem)
{
fprintf(stderr, "Could not map view of file: %s\n", infile);
goto the_end;
}
if (0 != strncmp(NTSIGNATURE(pMem), "PE", 2))
{
fprintf(stderr, "Not a PE file: %s\n", infile);
goto the_end;
}
hHeap = GetProcessHeap();
nCnt = GetExportFunctionNames(pMem, hHeap, &pNames);
if (0 == nCnt) {
fprintf(stderr, "Could not get exported function names: %s\n", infile);
goto the_end;
}
printf("--> %s\n", infile);
op = fopen(outfile, "w"); op = fopen(outfile, "w");
if (NULL == op) if (NULL == op) {
{ fprintf(stderr, "tiny_impdef: could not create output file: %s\n", outfile);
fprintf(stderr, "Could not create file: %s\n", outfile);
goto the_end; goto the_end;
} }
printf("<-- %s\n", outfile); if (v)
printf("--> %s\n", infile);
fprintf(op, "LIBRARY %s\n\nEXPORTS\n", file_basename(infile)); fprintf(op, "LIBRARY %s\n\nEXPORTS\n", file_basename(infile));
for (n = 0, p = pNames; n < nCnt; ++n) p = get_export_names(fp);
{ if (NULL == p) {
fprintf(op, "%s\n", p); fprintf(stderr, "tiny_impdef: could not get exported function names.\n");
while (*p++); goto the_end;
} }
for (q = p, i = 0; *q; ++i) {
fprintf(op, "%s\n", q);
q += strlen(q) + 1;
}
free(p);
if (v) {
printf("<-- %s\n", outfile);
printf("%d symbol(s) found\n", i);
}
ret = 0; ret = 0;
the_end: the_end:
if (pMem) if (fp)
UnmapViewOfFile(pMem); fclose(fp);
if (op)
if (hMapObject) fclose(op);
CloseHandle(hMapObject);
if (hFile)
CloseHandle(hFile);
return ret; return ret;
} }

View File

@ -13,98 +13,8 @@
#include <io.h> /* for mktemp */ #include <io.h> /* for mktemp */
#endif #endif
/* #include "ar-elf.h" */ #include "../../config.h"
/* "ar-elf.h" */ #include "../../elf.h"
/* ELF_v1.2.pdf */
typedef unsigned short int Elf32_Half;
typedef int Elf32_Sword;
typedef unsigned int Elf32_Word;
typedef unsigned int Elf32_Addr;
typedef unsigned int Elf32_Off;
typedef unsigned short int Elf32_Section;
#define EI_NIDENT 16
typedef struct {
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
} Elf32_Ehdr;
typedef struct {
Elf32_Word sh_name;
Elf32_Word sh_type;
Elf32_Word sh_flags;
Elf32_Addr sh_addr;
Elf32_Off sh_offset;
Elf32_Word sh_size;
Elf32_Word sh_link;
Elf32_Word sh_info;
Elf32_Word sh_addralign;
Elf32_Word sh_entsize;
} Elf32_Shdr;
#define SHT_NULL 0
#define SHT_PROGBITS 1
#define SHT_SYMTAB 2
#define SHT_STRTAB 3
#define SHT_RELA 4
#define SHT_HASH 5
#define SHT_DYNAMIC 6
#define SHT_NOTE 7
#define SHT_NOBITS 8
#define SHT_REL 9
#define SHT_SHLIB 10
#define SHT_DYNSYM 11
typedef struct {
Elf32_Word st_name;
Elf32_Addr st_value;
Elf32_Word st_size;
unsigned char st_info;
unsigned char st_other;
Elf32_Half st_shndx;
} Elf32_Sym;
#define ELF32_ST_BIND(i) ((i)>>4)
#define ELF32_ST_TYPE(i) ((i)&0xf)
#define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
#define STT_NOTYPE 0
#define STT_OBJECT 1
#define STT_FUNC 2
#define STT_SECTION 3
#define STT_FILE 4
#define STT_LOPROC 13
#define STT_HIPROC 15
#define STB_LOCAL 0
#define STB_GLOBAL 1
#define STB_WEAK 2
#define STB_LOPROC 13
#define STB_HIPROC 15
typedef struct {
Elf32_Word p_type;
Elf32_Off p_offset;
Elf32_Addr p_vaddr;
Elf32_Addr p_paddr;
Elf32_Word p_filesz;
Elf32_Word p_memsz;
Elf32_Word p_flags;
Elf32_Word p_align;
} Elf32_Phdr;
/* "ar-elf.h" ends */
#define ARMAG "!<arch>\n" #define ARMAG "!<arch>\n"
#define ARFMAG "`\n" #define ARFMAG "`\n"
@ -119,7 +29,6 @@ typedef struct ArHdr {
char ar_fmag[2]; char ar_fmag[2];
} ArHdr; } ArHdr;
unsigned long le2belong(unsigned long ul) { unsigned long le2belong(unsigned long ul) {
return ((ul & 0xFF0000)>>8)+((ul & 0xFF000000)>>24) + return ((ul & 0xFF0000)>>8)+((ul & 0xFF000000)>>24) +
((ul & 0xFF)<<24)+((ul & 0xFF00)<<8); ((ul & 0xFF)<<24)+((ul & 0xFF00)<<8);
@ -148,9 +57,9 @@ ArHdr arhdro = {
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
FILE *fi, *fh, *fo; FILE *fi, *fh, *fo;
Elf32_Ehdr *ehdr; ElfW(Ehdr) *ehdr;
Elf32_Shdr *shdr; ElfW(Shdr) *shdr;
Elf32_Sym *sym; ElfW(Sym) *sym;
int i, fsize, iarg; int i, fsize, iarg;
char *buf, *shstr, *symtab = NULL, *strtab = NULL; char *buf, *shstr, *symtab = NULL, *strtab = NULL;
int symtabsize = 0, strtabsize = 0; int symtabsize = 0, strtabsize = 0;
@ -207,7 +116,7 @@ int main(int argc, char **argv)
} }
if ((fi = fopen(argv[iarg], "rb")) == NULL) if ((fi = fopen(argv[iarg], "rb")) == NULL)
{ {
fprintf(stderr, "Can't open file %s \n", argv[iarg]); fprintf(stderr, "Can't open file %s \n", argv[iarg]);
remove(tfile); remove(tfile);
return 2; return 2;
} }
@ -218,14 +127,21 @@ int main(int argc, char **argv)
fread(buf, fsize, 1, fi); fread(buf, fsize, 1, fi);
fclose(fi); fclose(fi);
printf("%s:\n", argv[iarg]); //printf("%s:\n", argv[iarg]);
// elf header // elf header
ehdr = (Elf32_Ehdr *)buf; ehdr = (ElfW(Ehdr) *)buf;
shdr = (Elf32_Shdr *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize); if (ehdr->e_ident[4] != TCC_ELFCLASS)
{
fprintf(stderr, "Unsupported Elf Class: %s\n", argv[iarg]);
remove(tfile);
return 2;
}
shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);
shstr = (char *)(buf + shdr->sh_offset); shstr = (char *)(buf + shdr->sh_offset);
for (i = 0; i < ehdr->e_shnum; i++) for (i = 0; i < ehdr->e_shnum; i++)
{ {
shdr = (Elf32_Shdr *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize); shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);
if (!shdr->sh_offset) continue; if (!shdr->sh_offset) continue;
if (shdr->sh_type == SHT_SYMTAB) if (shdr->sh_type == SHT_SYMTAB)
{ {
@ -244,12 +160,16 @@ int main(int argc, char **argv)
if (symtab && symtabsize) if (symtab && symtabsize)
{ {
int nsym = symtabsize / sizeof(Elf32_Sym); int nsym = symtabsize / sizeof(ElfW(Sym));
//printf("symtab: info size shndx name\n"); //printf("symtab: info size shndx name\n");
for (i = 1; i < nsym; i++) for (i = 1; i < nsym; i++)
{ {
sym = (Elf32_Sym *) (symtab + i * sizeof(Elf32_Sym)); sym = (ElfW(Sym) *) (symtab + i * sizeof(ElfW(Sym)));
if (sym->st_shndx && (sym->st_info == 0x11 || sym->st_info == 0x12)) { if (sym->st_shndx &&
(sym->st_info == 0x10
|| sym->st_info == 0x11
|| sym->st_info == 0x12
)) {
//printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name); //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
istrlen = strlen(strtab + sym->st_name)+1; istrlen = strlen(strtab + sym->st_name)+1;
anames = realloc(anames, strpos+istrlen); anames = realloc(anames, strpos+istrlen);
@ -281,7 +201,7 @@ int main(int argc, char **argv)
} else fpos = 0; } else fpos = 0;
// write header // write header
fwrite("!<arch>\n", 8, 1, fh); fwrite("!<arch>\n", 8, 1, fh);
sprintf(stmp, "%-10d", strpos + (funccnt+1) * sizeof(int)); sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int)));
memcpy(&arhdr.ar_size, stmp, 10); memcpy(&arhdr.ar_size, stmp, 10);
fwrite(&arhdr, sizeof(arhdr), 1, fh); fwrite(&arhdr, sizeof(arhdr), 1, fh);
afpos[0] = le2belong(funccnt); afpos[0] = le2belong(funccnt);