diff options
Diffstat (limited to 'win32')
| -rw-r--r-- | win32/build-tcc.bat | 9 | ||||
| -rw-r--r-- | win32/include/_mingw.h | 20 | ||||
| -rw-r--r-- | win32/readme.txt | 94 | ||||
| -rw-r--r-- | win32/tools/tiny_impdef.c | 393 | ||||
| -rw-r--r-- | win32/tools/tiny_libmaker.c | 319 |
5 files changed, 766 insertions, 69 deletions
diff --git a/win32/build-tcc.bat b/win32/build-tcc.bat index 32cb5e1..e21fdeb 100644 --- a/win32/build-tcc.bat +++ b/win32/build-tcc.bat @@ -2,17 +2,14 @@ @rem batch file to build tcc using gcc and ar from mingw @rem ---------------------------------------------------- : -@if exist ..\config.h goto configready -: -@echo>..\config.h #define TCC_VERSION "0.9.24pre" +@echo>..\config.h #define TCC_VERSION "0.9.24" @echo>>..\config.h #define TCC_TARGET_PE 1 @echo>>..\config.h #define CONFIG_TCCDIR NULL : -:configready -: gcc -Os -fno-strict-aliasing ../tcc.c -o tcc.exe -s gcc -Os -fno-strict-aliasing ../tcc.c -D LIBTCC -c -o libtcc.o -gcc -Os -fno-strict-aliasing ../tiny_impdef.c -o tiny_impdef.exe -s +gcc -Os tools/tiny_impdef.c -o tiny_impdef.exe -s +gcc -Os tools/tiny_libmaker.c -o tiny_libmaker.exe -s mkdir libtcc ar rcs libtcc/libtcc.a libtcc.o del libtcc.o diff --git a/win32/include/_mingw.h b/win32/include/_mingw.h index 90a8b11..257c523 100644 --- a/win32/include/_mingw.h +++ b/win32/include/_mingw.h @@ -23,22 +23,21 @@ #define __int64 long long #define __int32 long #define __int16 short -#define __int8 char -#define __cdecl __attribute__((__cdecl__)) -#define __stdcall __attribute__((__stdcall__)) -#define __declspec(x) __attribute__((x)) +#define __int8 char +#define __cdecl __attribute__((__cdecl__)) +#define __stdcall __attribute__((__stdcall__)) +#define __declspec(x) __attribute__((x)) #define __MINGW32_VERSION 2.0 #define __MINGW32_MAJOR_VERSION 2 #define __MINGW32_MINOR_VERSION 0 -#define __MSVCRT__ -#define __MINGW_IMPORT extern +#define __MSVCRT__ 1 +#define __MINGW_IMPORT extern #define _CRTIMP -#define __CRT_INLINE extern __inline__ +#define __CRT_INLINE extern __inline__ -#define _WIN32 -#define WIN32 +#define WIN32 1 #ifndef _WINT_T #define _WINT_T @@ -46,7 +45,8 @@ typedef unsigned int wint_t; #endif /* for winapi */ -#define NONAMELESSUNION +#define _ANONYMOUS_UNION +#define _ANONYMOUS_STRUCT #define DECLSPEC_NORETURN #define WIN32_LEAN_AND_MEAN #define DECLARE_STDCALL_P(type) __stdcall type diff --git a/win32/readme.txt b/win32/readme.txt index c453a69..ee17687 100644 --- a/win32/readme.txt +++ b/win32/readme.txt @@ -1,37 +1,34 @@ - TinyCC-PE - --------- - - TinyCC (aka TCC) is a small but hyperfast C compiler, - written by Fabrice Bellard, - + TinyCC + ====== - TinyCC-PE is the TinyCC compiler with an extension to - write PE executables for MS-Windows. + This file contains some additional information for usage of TinyCC + under MS-Windows: - Features: - --------- + Overview: + --------- + TinyCC (aka TCC) is a small but hyperfast C compiler, written by + Fabrice Bellard. - TinyCC-PE can produce console applications, native windows - GUI programs and DLL's. + TinyCC for MS-Windows can produce console applications, native + windows GUI programs and DLL's. - Most of the features pointed out by Fabrice Bellard for the - original version are still valid, i.e: + The package with under 300kb includes a complete C-compiler with + header files and basic system library support. - - SMALL! The package with ~400kb includes a complete C-compiler - with header files for console and GUI applications. + With the -run switch you can run C-sources without any linking + directly from the command line. - - With the -run switch you can run C-sources without any - linking directly from the command line. + TinyCC can be used as dynamic code generator library in your own + program. - - TCC can of course compile itself. + TinyCC can of course compile itself. Compilation: (omit that if you use the binary ZIP package) ------------ - - You must use the MinGW and MSYS tools available at + You can use the MinGW and MSYS tools available at http://www.mingw.org to compile TCC for Windows. Untar the TCC archive and type in the MSYS shell: @@ -48,13 +45,14 @@ Installation: (from the binary ZIP package) ------------- - Just unzip the package to a directory anywhere on your computer. + + The binary package does not include libtcc. If you want tcc as + dynamic code generator, please use the source code distribution. Examples: --------- - For the 'Fibonacci' console example type from the command line: tcc examples\fib.c @@ -69,25 +67,22 @@ tcc examples\hello_dll.c examples\dll.def - Import Definitions: - ------------------- - - TinyCC-PE searches and reads import definition files similar - to libraries. + Import Definition Files: + ------------------------ + To link with Windows system DLLs, TinyCC uses import definition + files (.def) instead of libraries. - The included 'tiny_impdef' program may be used to make .def files - for any DLL, e.g for an 'opengl32.def': + The included 'tiny_impdef' program may be used to make additional + .def files for any DLL. For example: tiny_impdef.exe opengl32.dll - or to the same effect: - - tcc -run tiny_impdef.c opengl32.dll + To use it, put the opengl32.def file into the tcc/lib directory, + and specify -lopengl32 at the tcc commandline. Resource Files: --------------- - TinyCC-PE can now link windows resources in coff format as generated by MINGW's windres.exe. For example: @@ -95,32 +90,25 @@ tcc app.c appres.o -o app.exe - Header Files: - ------------- - - The system header files, except '_mingw.h', are from the - 3.7 mingw distribution. See also: http://www.mingw.org/ - - - Compile TCC: - ------------ + Tiny Libmaker: + -------------- + The included tiny_libmaker tool by Timovj Lahde can be used as + 'ar' replacement to make a library from several object files. - With TCC itself just say: - tcc src\tcc.c -o tcc.new.exe - - Other compilers like mingw-gcc or msvc work as well. - To make libtcc1.a, you need 'ar' from the mingw binutils. + Header Files: + ------------- + The system header files (except _mingw.h) are from the mingw + distribution (http://www.mingw.org/). Documentation and License: -------------------------- - TCC is distributed under the GNU Lesser General Public License (see COPYING file). - Please read the original tcc-doc.html to have all the features - of TCC. Also visit: http://fabrice.bellard.free.fr/tcc/ + Please read tcc-doc.html to have all the features of TCC. Also + visit: http://fabrice.bellard.free.fr/tcc/ - -- - grischka@users.sourceforge.net + + -- grischka@users.sourceforge.net diff --git a/win32/tools/tiny_impdef.c b/win32/tools/tiny_impdef.c new file mode 100644 index 0000000..040c53a --- /dev/null +++ b/win32/tools/tiny_impdef.c @@ -0,0 +1,393 @@ +/* -------------------------------------------------------------- */ +/* + * tiny_impdef creates an export definition file (.def) from a dll + * on MS-Windows. Usage: tiny_impdef library.dll [-o outputfile]" + * + * Copyright (c) 2005,2007 grischka + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +#include <stdio.h> + +/* Offset to PE file signature */ +#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. */ + return (int) + ((PIMAGE_FILE_HEADER) + PEFHDROFFSET(lpFile))->NumberOfSections; +} + + +/* -------------------------------------------------------------- */ + +LPVOID WINAPI ImageDirectoryOffset ( + LPVOID lpFile, + DWORD dwIMAGE_DIRECTORY) +{ + PIMAGE_OPTIONAL_HEADER poh; + PIMAGE_SECTION_HEADER psh; + int nSections = NumOfSections (lpFile); + int i = 0; + LPVOID VAImageDir; + + /* Retrieve offsets to optional and section headers. */ + poh = (PIMAGE_OPTIONAL_HEADER)OPTHDROFFSET (lpFile); + psh = (PIMAGE_SECTION_HEADER)SECHDROFFSET (lpFile); + + /* Must be 0 thru (NumberOfRvaAndSizes-1). */ + if (dwIMAGE_DIRECTORY >= poh->NumberOfRvaAndSizes) + return NULL; + + /* Locate image directory's relative virtual address. */ + VAImageDir = (LPVOID)poh->DataDirectory[dwIMAGE_DIRECTORY].VirtualAddress; + + /* Locate section containing image directory. */ + while (i++<nSections) + { + if (psh->VirtualAddress <= (DWORD)VAImageDir + && psh->VirtualAddress + psh->SizeOfRawData > (DWORD)VAImageDir) + break; + psh++; + } + + if (i > nSections) + return NULL; + + /* Return image import directory offset. */ + return (LPVOID)(((int)lpFile + + (int)VAImageDir - psh->VirtualAddress) + + (int)psh->PointerToRawData); +} + +/* -------------------------------------------------------------- */ + +BOOL WINAPI GetSectionHdrByName ( + LPVOID lpFile, + IMAGE_SECTION_HEADER *sh, + 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 + psh++; + } + } + return FALSE; +} + +/* -------------------------------------------------------------- */ + +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; +} + +/* -------------------------------------------------------------- */ +/* extract the basename of a file */ + +static char *file_basename(const char *name) +{ + const char *p = strchr(name, 0); + while (p > name + && p[-1] != '/' + && p[-1] != '\\' + ) + --p; + return (char*)p; +} + +/* -------------------------------------------------------------- */ + +int main(int argc, char **argv) +{ + HANDLE hHeap; + HANDLE hFile; + HANDLE hMapObject; + VOID *pMem; + + int nCnt, ret, n; + char *pNames; + char infile[MAX_PATH]; + char buffer[MAX_PATH]; + char outfile[MAX_PATH]; + FILE *op; + char *p; + + hHeap = NULL; + hFile = NULL; + hMapObject = NULL; + pMem = NULL; + infile[0] = 0; + outfile[0] = 0; + ret = 1; + + for (n = 1; n < argc; ++n) + { + const char *a = argv[n]; + if ('-' == a[0]) { + if (0 == strcmp(a, "-o")) { + if (++n == argc) + goto usage; + strcpy(outfile, argv[n]); + } + else + goto usage; + + } else if (0 == infile[0]) + strcpy(infile, a); + else + goto usage; + } + + if (0 == infile[0]) + { +usage: + fprintf(stderr, + "tiny_impdef creates an export definition file (.def) from a dll\n" + "Usage: tiny_impdef library.dll [-o outputfile]\n" + ); + goto the_end; + } + + if (SearchPath(NULL, infile, ".dll", sizeof buffer, buffer, NULL)) + strcpy(infile, buffer); + + if (0 == outfile[0]) + { + char *p; + strcpy(outfile, file_basename(infile)); + p = strrchr(outfile, '.'); + if (NULL == p) + p = strchr(outfile, 0); + strcpy(p, ".def"); + } + + hFile = CreateFile( + infile, + GENERIC_READ, + FILE_SHARE_READ, + NULL, + OPEN_EXISTING, + 0, + NULL + ); + + if (hFile == INVALID_HANDLE_VALUE) + { + fprintf(stderr, "No such file: %s\n", infile); + 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"); + if (NULL == op) + { + fprintf(stderr, "Could not create file: %s\n", outfile); + goto the_end; + } + + printf("<-- %s\n", outfile); + + fprintf(op, "LIBRARY %s\n\nEXPORTS\n", file_basename(infile)); + for (n = 0, p = pNames; n < nCnt; ++n) + { + fprintf(op, "%s\n", p); + while (*p++); + } + ret = 0; + +the_end: + if (pMem) + UnmapViewOfFile(pMem); + + if (hMapObject) + CloseHandle(hMapObject); + + if (hFile) + CloseHandle(hFile); + + return ret; +} + +/* -------------------------------------------------------------- */ diff --git a/win32/tools/tiny_libmaker.c b/win32/tools/tiny_libmaker.c new file mode 100644 index 0000000..efda78a --- /dev/null +++ b/win32/tools/tiny_libmaker.c @@ -0,0 +1,319 @@ +/* + * This program is for making libtcc1.a without ar + * tiny_libmaker - tiny elf lib maker + * usage: tiny_libmaker [lib] files... + * Copyright (c) 2007 Timppa + * + * This program is free software but WITHOUT ANY WARRANTY + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +/* #include "ar-elf.h" */ +/* "ar-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 ARFMAG "`\n" + +typedef struct ArHdr { + char ar_name[16]; + char ar_date[12]; + char ar_uid[6]; + char ar_gid[6]; + char ar_mode[8]; + char ar_size[10]; + char ar_fmag[2]; +} ArHdr; + + +unsigned long le2belong(unsigned long ul) { + return ((ul & 0xFF0000)>>8)+((ul & 0xFF000000)>>24) + + ((ul & 0xFF)<<24)+((ul & 0xFF00)<<8); +} + +ArHdr arhdr = { + "/ ", + " ", + "0 ", + "0 ", + "0 ", + " ", + ARFMAG + }; + +ArHdr arhdro = { + " ", + " ", + "0 ", + "0 ", + "0 ", + " ", + ARFMAG + }; + +int main(int argc, char **argv) +{ + FILE *fi, *fh, *fo; + Elf32_Ehdr *ehdr; + Elf32_Shdr *shdr; + Elf32_Sym *sym; + int i, fsize, iarg; + char *buf, *shstr, *symtab = NULL, *strtab = NULL; + int symtabsize = 0, strtabsize = 0; + char *anames = NULL; + int *afpos = NULL; + int istrlen, strpos = 0, fpos = 0, funccnt = 0, funcmax, hofs; + char afile[260], tfile[260], stmp[20]; + + + strcpy(afile, "ar_test.a"); + iarg = 1; + + if (argc < 2) + { + printf("usage: tiny_libmaker [lib] file...\n"); + return 1; + } + for (i=1; i<argc; i++) { + istrlen = strlen(argv[i]); + if (argv[i][istrlen-2] == '.') { + if(argv[i][istrlen-1] == 'a') + strcpy(afile, argv[i]); + else if(argv[i][istrlen-1] == 'o') { + iarg = i; + break; + } + } + } + + //tfile[0] = '.'; tfile[1] = '/'; + //if (tmpnam(&tfile[2])) { + strcpy(tfile, "./XXXXXX"); + if (mktemp(tfile)) + { + if ((fo = fopen(tfile, "wb+")) == NULL) + { + fprintf(stderr, "Can't open file %s \n", tfile); + return 2; + } + } + +/* + if ((fo = tmpfile()) == NULL) + { + fprintf(stderr, "Can't open temporary file \n"); + return 2; + } +*/ + if ((fh = fopen(afile, "wb")) == NULL) + { + fprintf(stderr, "Can't open file %s \n", afile); + remove(tfile); + return 2; + } + + funcmax = 1000; + afpos = realloc(NULL, funcmax); // 250 func + memcpy(&arhdro.ar_mode, "100666", 6); + + //iarg = 1; + while (iarg < argc) + { + if (!strcmp(argv[iarg], "rcs")) { + iarg++; + continue; + } + if ((fi = fopen(argv[iarg], "rb")) == NULL) + { + fprintf(stderr, "Can't open file %s \n", argv[iarg]); + remove(tfile); + return 2; + } + fseek(fi, 0, SEEK_END); + fsize = ftell(fi); + fseek(fi, 0, SEEK_SET); + buf = malloc(fsize + 1); + fread(buf, fsize, 1, fi); + fclose(fi); + + printf("%s:\n", argv[iarg]); + // elf header + ehdr = (Elf32_Ehdr *)buf; + shdr = (Elf32_Shdr *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize); + shstr = (char *)(buf + shdr->sh_offset); + for (i = 0; i < ehdr->e_shnum; i++) + { + shdr = (Elf32_Shdr *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize); + if (!shdr->sh_offset) continue; + if (shdr->sh_type == SHT_SYMTAB) + { + symtab = (char *)(buf + shdr->sh_offset); + symtabsize = shdr->sh_size; + } + if (shdr->sh_type == SHT_STRTAB) + { + if (!strcmp(shstr + shdr->sh_name, ".strtab")) + { + strtab = (char *)(buf + shdr->sh_offset); + strtabsize = shdr->sh_size; + } + } + } + + if (symtab && symtabsize) + { + int nsym = symtabsize / sizeof(Elf32_Sym); + //printf("symtab: info size shndx name\n"); + for (i = 1; i < nsym; i++) + { + sym = (Elf32_Sym *) (symtab + i * sizeof(Elf32_Sym)); + if (sym->st_shndx && (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); + istrlen = strlen(strtab + sym->st_name)+1; + anames = realloc(anames, strpos+istrlen); + strcpy(anames + strpos, strtab + sym->st_name); + strpos += istrlen; + if (funccnt >= funcmax) { + afpos = realloc(NULL, funcmax+1000); // 250 func more + funcmax += 1000; + } + afpos[++funccnt] = fpos; + } + } + } + memset(&arhdro.ar_name, ' ', sizeof(arhdr.ar_name)); + strcpy(arhdro.ar_name, argv[iarg]); + arhdro.ar_name[strlen(argv[iarg])] = '/'; + sprintf(stmp, "%-10d", fsize); + memcpy(&arhdro.ar_size, stmp, 10); + fwrite(&arhdro, sizeof(arhdro), 1, fo); + fwrite(buf, fsize, 1, fo); + free(buf); + iarg++; + fpos += (fsize + sizeof(arhdro)); + } + hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int); + if ((hofs & 1)) { // align + hofs++; + fpos = 1; + } else fpos = 0; + // write header + fwrite("!<arch>\n", 8, 1, fh); + sprintf(stmp, "%-10d", strpos + (funccnt+1) * sizeof(int)); + memcpy(&arhdr.ar_size, stmp, 10); + fwrite(&arhdr, sizeof(arhdr), 1, fh); + afpos[0] = le2belong(funccnt); + for (i=1; i<=funccnt; i++) { + afpos[i] = le2belong(afpos[i] + hofs); + } + fwrite(afpos, (funccnt+1) * sizeof(int), 1, fh); + fwrite(anames, strpos, 1, fh); + if (fpos) fwrite("", 1, 1, fh); + // write objects + fseek(fo, 0, SEEK_END); + fsize = ftell(fo); + fseek(fo, 0, SEEK_SET); + buf = malloc(fsize + 1); + fread(buf, fsize, 1, fo); + fclose(fo); + fwrite(buf, fsize, 1, fh); + fclose(fh); + free(buf); + if (anames) + free(anames); + if (afpos) + free(afpos); + remove(tfile); + return 0; +} |
