Unified · Split
| @@ -19,8 +19,10 @@ ifeq ($(GCC_MAJOR),2) · 18 unmodified lines | ||
| 19 | 19 | CFLAGS+=-m386 -malign-functions=0 |
| 20 | 20 | else |
| 21 | 21 | CFLAGS+=-march=i386 -falign-functions=0 -fno-strict-aliasing |
| 22 | +ifneq ($(GCC_MAJOR),3) | |
| 22 | 23 | CFLAGS+=-Wno-pointer-sign -Wno-sign-compare |
| 23 | 24 | endif |
| 25 | +endif | |
| 24 | 26 | |
| 25 | 27 | DISAS=objdump -d |
| 26 | 28 | INSTALL=install |
| @@ -30,7 +32,7 @@ PROGS=tcc$(EXESUF) · 3 unmodified lines | ||
| 30 | 32 | ifdef CONFIG_CROSS |
| 31 | 33 | PROGS+=c67-tcc$(EXESUF) arm-tcc$(EXESUF) |
| 32 | 34 | endif |
| 33 | −PROGS+=tiny_impdef$(EXESUF) | |
| 35 | +PROGS+=tiny_impdef$(EXESUF) tiny_libmaker$(EXESUF) | |
| 34 | 36 | else |
| 35 | 37 | ifeq ($(ARCH),i386) |
| 36 | 38 | PROGS=tcc$(EXESUF) |
| @@ -168,7 +170,9 @@ i386-win32-tcc$(EXESUF): tcc.c i386-gen.c tccelf.c tccasm.c i386-asm.c tcctok.h · 131 unmodified lines | ||
| 168 | 170 | $(CC) $(CFLAGS) -DTCC_TARGET_PE -o $@ $< $(LIBS) |
| 169 | 171 | |
| 170 | 172 | # windows utilities |
| 171 | −tiny_impdef$(EXESUF): tiny_impdef.c | |
| 173 | +tiny_impdef$(EXESUF): win32/tools/tiny_impdef.c | |
| 174 | + $(CC) $(CFLAGS) -o $@ $< -lkernel32 | |
| 175 | +tiny_libmaker$(EXESUF): win32/tools/tiny_libmaker.c | |
| 172 | 176 | $(CC) $(CFLAGS) -o $@ $< -lkernel32 |
| 173 | 177 | |
| 174 | 178 | # TinyCC runtime libraries |
| @@ -181,7 +181,20 @@ EOF · 180 unmodified lines | ||
| 181 | 181 | |
| 182 | 182 | gcc_major="2" |
| 183 | 183 | if $cc -o $TMPO $TMPC 2> /dev/null ; then |
| 184 | − gcc_major="3" | |
| 184 | + gcc_major="3" | |
| 185 | +fi | |
| 186 | +cat > $TMPC <<EOF | |
| 187 | +int main(void) { | |
| 188 | +#if __GNUC__ >= 4 | |
| 189 | +return 0; | |
| 190 | +#else | |
| 191 | +#error gcc < 4 | |
| 192 | +#endif | |
| 193 | +} | |
| 194 | +EOF | |
| 195 | + | |
| 196 | +if $cc -o $TMPO $TMPC 2> /dev/null ; then | |
| 197 | + gcc_major="4" | |
| 185 | 198 | fi |
| 186 | 199 | |
| 187 | 200 | if test x"$1" = x"-h" -o x"$1" = x"--help" ; then |
| @@ -2,17 +2,14 @@ · 1 unmodified line | ||
| 2 | 2 | @rem batch file to build tcc using gcc and ar from mingw |
| 3 | 3 | @rem ---------------------------------------------------- |
| 4 | 4 | : |
| 5 | −@if exist ..\config.h goto configready | |
| 6 | −: | |
| 7 | −@echo>..\config.h #define TCC_VERSION "0.9.24pre" | |
| 5 | +@echo>..\config.h #define TCC_VERSION "0.9.24" | |
| 8 | 6 | @echo>>..\config.h #define TCC_TARGET_PE 1 |
| 9 | 7 | @echo>>..\config.h #define CONFIG_TCCDIR NULL |
| 10 | 8 | : |
| 11 | −:configready | |
| 12 | −: | |
| 13 | 9 | gcc -Os -fno-strict-aliasing ../tcc.c -o tcc.exe -s |
| 14 | 10 | gcc -Os -fno-strict-aliasing ../tcc.c -D LIBTCC -c -o libtcc.o |
| 15 | −gcc -Os -fno-strict-aliasing ../tiny_impdef.c -o tiny_impdef.exe -s | |
| 11 | +gcc -Os tools/tiny_impdef.c -o tiny_impdef.exe -s | |
| 12 | +gcc -Os tools/tiny_libmaker.c -o tiny_libmaker.exe -s | |
| 16 | 13 | mkdir libtcc |
| 17 | 14 | ar rcs libtcc/libtcc.a libtcc.o |
| 18 | 15 | del libtcc.o |
| @@ -23,22 +23,21 @@ · 22 unmodified lines | ||
| 23 | 23 | #define __int64 long long |
| 24 | 24 | #define __int32 long |
| 25 | 25 | #define __int16 short |
| 26 | −#define __int8 char | |
| 27 | −#define __cdecl __attribute__((__cdecl__)) | |
| 28 | −#define __stdcall __attribute__((__stdcall__)) | |
| 29 | −#define __declspec(x) __attribute__((x)) | |
| 26 | +#define __int8 char | |
| 27 | +#define __cdecl __attribute__((__cdecl__)) | |
| 28 | +#define __stdcall __attribute__((__stdcall__)) | |
| 29 | +#define __declspec(x) __attribute__((x)) | |
| 30 | 30 | |
| 31 | 31 | #define __MINGW32_VERSION 2.0 |
| 32 | 32 | #define __MINGW32_MAJOR_VERSION 2 |
| 33 | 33 | #define __MINGW32_MINOR_VERSION 0 |
| 34 | 34 | |
| 35 | −#define __MSVCRT__ | |
| 36 | −#define __MINGW_IMPORT extern | |
| 35 | +#define __MSVCRT__ 1 | |
| 36 | +#define __MINGW_IMPORT extern | |
| 37 | 37 | #define _CRTIMP |
| 38 | −#define __CRT_INLINE extern __inline__ | |
| 38 | +#define __CRT_INLINE extern __inline__ | |
| 39 | 39 | |
| 40 | −#define _WIN32 | |
| 41 | −#define WIN32 | |
| 40 | +#define WIN32 1 | |
| 42 | 41 | |
| 43 | 42 | #ifndef _WINT_T |
| 44 | 43 | #define _WINT_T |
| @@ -46,7 +45,8 @@ typedef unsigned int wint_t; · 1 unmodified line | ||
| 46 | 45 | #endif |
| 47 | 46 | |
| 48 | 47 | /* for winapi */ |
| 49 | −#define NONAMELESSUNION | |
| 48 | +#define _ANONYMOUS_UNION | |
| 49 | +#define _ANONYMOUS_STRUCT | |
| 50 | 50 | #define DECLSPEC_NORETURN |
| 51 | 51 | #define WIN32_LEAN_AND_MEAN |
| 52 | 52 | #define DECLARE_STDCALL_P(type) __stdcall type |
| @@ -1,37 +1,34 @@ | ||
| 1 | 1 | |
| 2 | − TinyCC-PE | |
| 3 | − --------- | |
| 4 | − | |
| 5 | − TinyCC (aka TCC) is a small but hyperfast C compiler, | |
| 6 | − written by Fabrice Bellard, | |
| 7 | − | |
| 2 | + TinyCC | |
| 3 | + ====== | |
| 8 | 4 | |
| 9 | − TinyCC-PE is the TinyCC compiler with an extension to | |
| 10 | − write PE executables for MS-Windows. | |
| 5 | + This file contains some additional information for usage of TinyCC | |
| 6 | + under MS-Windows: | |
| 11 | 7 | |
| 12 | 8 | |
| 13 | − Features: | |
| 14 | − --------- | |
| 9 | + Overview: | |
| 10 | + --------- | |
| 11 | + TinyCC (aka TCC) is a small but hyperfast C compiler, written by | |
| 12 | + Fabrice Bellard. | |
| 15 | 13 | |
| 16 | − TinyCC-PE can produce console applications, native windows | |
| 17 | − GUI programs and DLL's. | |
| 14 | + TinyCC for MS-Windows can produce console applications, native | |
| 15 | + windows GUI programs and DLL's. | |
| 18 | 16 | |
| 19 | − Most of the features pointed out by Fabrice Bellard for the | |
| 20 | − original version are still valid, i.e: | |
| 17 | + The package with under 300kb includes a complete C-compiler with | |
| 18 | + header files and basic system library support. | |
| 21 | 19 | |
| 22 | − - SMALL! The package with ~400kb includes a complete C-compiler | |
| 23 | − with header files for console and GUI applications. | |
| 20 | + With the -run switch you can run C-sources without any linking | |
| 21 | + directly from the command line. | |
| 24 | 22 | |
| 25 | − - With the -run switch you can run C-sources without any | |
| 26 | − linking directly from the command line. | |
| 23 | + TinyCC can be used as dynamic code generator library in your own | |
| 24 | + program. | |
| 27 | 25 | |
| 28 | − - TCC can of course compile itself. | |
| 26 | + TinyCC can of course compile itself. | |
| 29 | 27 | |
| 30 | 28 | |
| 31 | 29 | Compilation: (omit that if you use the binary ZIP package) |
| 32 | 30 | ------------ |
| 33 | − | |
| 34 | − You must use the MinGW and MSYS tools available at | |
| 31 | + You can use the MinGW and MSYS tools available at | |
| 35 | 32 | http://www.mingw.org to compile TCC for Windows. Untar the TCC |
| 36 | 33 | archive and type in the MSYS shell: |
| 37 | 34 | |
| @@ -48,13 +45,14 @@ · 10 unmodified lines | ||
| 48 | 45 | |
| 49 | 46 | Installation: (from the binary ZIP package) |
| 50 | 47 | ------------- |
| 51 | − | |
| 52 | 48 | Just unzip the package to a directory anywhere on your computer. |
| 49 | + | |
| 50 | + The binary package does not include libtcc. If you want tcc as | |
| 51 | + dynamic code generator, please use the source code distribution. | |
| 53 | 52 | |
| 54 | 53 | |
| 55 | 54 | Examples: |
| 56 | 55 | --------- |
| 57 | − | |
| 58 | 56 | For the 'Fibonacci' console example type from the command line: |
| 59 | 57 | |
| 60 | 58 | tcc examples\fib.c |
| @@ -69,25 +67,22 @@ · 8 unmodified lines | ||
| 69 | 67 | tcc examples\hello_dll.c examples\dll.def |
| 70 | 68 | |
| 71 | 69 | |
| 72 | − Import Definitions: | |
| 73 | − ------------------- | |
| 74 | − | |
| 75 | − TinyCC-PE searches and reads import definition files similar | |
| 76 | − to libraries. | |
| 70 | + Import Definition Files: | |
| 71 | + ------------------------ | |
| 72 | + To link with Windows system DLLs, TinyCC uses import definition | |
| 73 | + files (.def) instead of libraries. | |
| 77 | 74 | |
| 78 | − The included 'tiny_impdef' program may be used to make .def files | |
| 79 | − for any DLL, e.g for an 'opengl32.def': | |
| 75 | + The included 'tiny_impdef' program may be used to make additional | |
| 76 | + .def files for any DLL. For example: | |
| 80 | 77 | |
| 81 | 78 | tiny_impdef.exe opengl32.dll |
| 82 | 79 | |
| 83 | − or to the same effect: | |
| 84 | − | |
| 85 | − tcc -run tiny_impdef.c opengl32.dll | |
| 80 | + To use it, put the opengl32.def file into the tcc/lib directory, | |
| 81 | + and specify -lopengl32 at the tcc commandline. | |
| 86 | 82 | |
| 87 | 83 | |
| 88 | 84 | Resource Files: |
| 89 | 85 | --------------- |
| 90 | − | |
| 91 | 86 | TinyCC-PE can now link windows resources in coff format as generated |
| 92 | 87 | by MINGW's windres.exe. For example: |
| 93 | 88 | |
| @@ -95,32 +90,25 @@ · 1 unmodified line | ||
| 95 | 90 | tcc app.c appres.o -o app.exe |
| 96 | 91 | |
| 97 | 92 | |
| 98 | − Header Files: | |
| 99 | − ------------- | |
| 100 | − | |
| 101 | − The system header files, except '_mingw.h', are from the | |
| 102 | − 3.7 mingw distribution. See also: http://www.mingw.org/ | |
| 103 | − | |
| 104 | − | |
| 105 | − Compile TCC: | |
| 106 | − ------------ | |
| 93 | + Tiny Libmaker: | |
| 94 | + -------------- | |
| 95 | + The included tiny_libmaker tool by Timovj Lahde can be used as | |
| 96 | + 'ar' replacement to make a library from several object files. | |
| 107 | 97 | |
| 108 | − With TCC itself just say: | |
| 109 | 98 | |
| 110 | − tcc src\tcc.c -o tcc.new.exe | |
| 111 | − | |
| 112 | − Other compilers like mingw-gcc or msvc work as well. | |
| 113 | − To make libtcc1.a, you need 'ar' from the mingw binutils. | |
| 99 | + Header Files: | |
| 100 | + ------------- | |
| 101 | + The system header files (except _mingw.h) are from the mingw | |
| 102 | + distribution (http://www.mingw.org/). | |
| 114 | 103 | |
| 115 | 104 | |
| 116 | 105 | Documentation and License: |
| 117 | 106 | -------------------------- |
| 118 | − | |
| 119 | 107 | TCC is distributed under the GNU Lesser General Public License |
| 120 | 108 | (see COPYING file). |
| 121 | 109 | |
| 122 | − Please read the original tcc-doc.html to have all the features | |
| 123 | − of TCC. Also visit: http://fabrice.bellard.free.fr/tcc/ | |
| 110 | + Please read tcc-doc.html to have all the features of TCC. Also | |
| 111 | + visit: http://fabrice.bellard.free.fr/tcc/ | |
| 124 | 112 | |
| 125 | − -- | |
| 126 | − grischka@users.sourceforge.net | |
| 113 | + | |
| 114 | + -- grischka@users.sourceforge.net | |
File renamed without changes.
| @@ -0,0 +1,319 @@ | ||
| 1 | +/* | |
| 2 | + * This program is for making libtcc1.a without ar | |
| 3 | + * tiny_libmaker - tiny elf lib maker | |
| 4 | + * usage: tiny_libmaker [lib] files... | |
| 5 | + * Copyright (c) 2007 Timppa | |
| 6 | + * | |
| 7 | + * This program is free software but WITHOUT ANY WARRANTY | |
| 8 | + */ | |
| 9 | +#include <stdio.h> | |
| 10 | +#include <stdlib.h> | |
| 11 | +#include <string.h> | |
| 12 | + | |
| 13 | +/* #include "ar-elf.h" */ | |
| 14 | +/* "ar-elf.h" */ | |
| 15 | +/* ELF_v1.2.pdf */ | |
| 16 | +typedef unsigned short int Elf32_Half; | |
| 17 | +typedef int Elf32_Sword; | |
| 18 | +typedef unsigned int Elf32_Word; | |
| 19 | +typedef unsigned int Elf32_Addr; | |
| 20 | +typedef unsigned int Elf32_Off; | |
| 21 | +typedef unsigned short int Elf32_Section; | |
| 22 | + | |
| 23 | +#define EI_NIDENT 16 | |
| 24 | +typedef struct { | |
| 25 | + unsigned char e_ident[EI_NIDENT]; | |
| 26 | + Elf32_Half e_type; | |
| 27 | + Elf32_Half e_machine; | |
| 28 | + Elf32_Word e_version; | |
| 29 | + Elf32_Addr e_entry; | |
| 30 | + Elf32_Off e_phoff; | |
| 31 | + Elf32_Off e_shoff; | |
| 32 | + Elf32_Word e_flags; | |
| 33 | + Elf32_Half e_ehsize; | |
| 34 | + Elf32_Half e_phentsize; | |
| 35 | + Elf32_Half e_phnum; | |
| 36 | + Elf32_Half e_shentsize; | |
| 37 | + Elf32_Half e_shnum; | |
| 38 | + Elf32_Half e_shstrndx; | |
| 39 | +} Elf32_Ehdr; | |
| 40 | + | |
| 41 | +typedef struct { | |
| 42 | + Elf32_Word sh_name; | |
| 43 | + Elf32_Word sh_type; | |
| 44 | + Elf32_Word sh_flags; | |
| 45 | + Elf32_Addr sh_addr; | |
| 46 | + Elf32_Off sh_offset; | |
| 47 | + Elf32_Word sh_size; | |
| 48 | + Elf32_Word sh_link; | |
| 49 | + Elf32_Word sh_info; | |
| 50 | + Elf32_Word sh_addralign; | |
| 51 | + Elf32_Word sh_entsize; | |
| 52 | +} Elf32_Shdr; | |
| 53 | + | |
| 54 | +#define SHT_NULL 0 | |
| 55 | +#define SHT_PROGBITS 1 | |
| 56 | +#define SHT_SYMTAB 2 | |
| 57 | +#define SHT_STRTAB 3 | |
| 58 | +#define SHT_RELA 4 | |
| 59 | +#define SHT_HASH 5 | |
| 60 | +#define SHT_DYNAMIC 6 | |
| 61 | +#define SHT_NOTE 7 | |
| 62 | +#define SHT_NOBITS 8 | |
| 63 | +#define SHT_REL 9 | |
| 64 | +#define SHT_SHLIB 10 | |
| 65 | +#define SHT_DYNSYM 11 | |
| 66 | + | |
| 67 | +typedef struct { | |
| 68 | + Elf32_Word st_name; | |
| 69 | + Elf32_Addr st_value; | |
| 70 | + Elf32_Word st_size; | |
| 71 | + unsigned char st_info; | |
| 72 | + unsigned char st_other; | |
| 73 | + Elf32_Half st_shndx; | |
| 74 | +} Elf32_Sym; | |
| 75 | + | |
| 76 | +#define ELF32_ST_BIND(i) ((i)>>4) | |
| 77 | +#define ELF32_ST_TYPE(i) ((i)&0xf) | |
| 78 | +#define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf)) | |
| 79 | + | |
| 80 | +#define STT_NOTYPE 0 | |
| 81 | +#define STT_OBJECT 1 | |
| 82 | +#define STT_FUNC 2 | |
| 83 | +#define STT_SECTION 3 | |
| 84 | +#define STT_FILE 4 | |
| 85 | +#define STT_LOPROC 13 | |
| 86 | +#define STT_HIPROC 15 | |
| 87 | + | |
| 88 | +#define STB_LOCAL 0 | |
| 89 | +#define STB_GLOBAL 1 | |
| 90 | +#define STB_WEAK 2 | |
| 91 | +#define STB_LOPROC 13 | |
| 92 | +#define STB_HIPROC 15 | |
| 93 | + | |
| 94 | +typedef struct { | |
| 95 | + Elf32_Word p_type; | |
| 96 | + Elf32_Off p_offset; | |
| 97 | + Elf32_Addr p_vaddr; | |
| 98 | + Elf32_Addr p_paddr; | |
| 99 | + Elf32_Word p_filesz; | |
| 100 | + Elf32_Word p_memsz; | |
| 101 | + Elf32_Word p_flags; | |
| 102 | + Elf32_Word p_align; | |
| 103 | +} Elf32_Phdr; | |
| 104 | +/* "ar-elf.h" ends */ | |
| 105 | + | |
| 106 | +#define ARMAG "!<arch>\n" | |
| 107 | +#define ARFMAG "`\n" | |
| 108 | + | |
| 109 | +typedef struct ArHdr { | |
| 110 | + char ar_name[16]; | |
| 111 | + char ar_date[12]; | |
| 112 | + char ar_uid[6]; | |
| 113 | + char ar_gid[6]; | |
| 114 | + char ar_mode[8]; | |
| 115 | + char ar_size[10]; | |
| 116 | + char ar_fmag[2]; | |
| 117 | +} ArHdr; | |
| 118 | + | |
| 119 | + | |
| 120 | +unsigned long le2belong(unsigned long ul) { | |
| 121 | + return ((ul & 0xFF0000)>>8)+((ul & 0xFF000000)>>24) + | |
| 122 | + ((ul & 0xFF)<<24)+((ul & 0xFF00)<<8); | |
| 123 | +} | |
| 124 | + | |
| 125 | +ArHdr arhdr = { | |
| 126 | + "/ ", | |
| 127 | + " ", | |
| 128 | + "0 ", | |
| 129 | + "0 ", | |
| 130 | + "0 ", | |
| 131 | + " ", | |
| 132 | + ARFMAG | |
| 133 | + }; | |
| 134 | + | |
| 135 | +ArHdr arhdro = { | |
| 136 | + " ", | |
| 137 | + " ", | |
| 138 | + "0 ", | |
| 139 | + "0 ", | |
| 140 | + "0 ", | |
| 141 | + " ", | |
| 142 | + ARFMAG | |
| 143 | + }; | |
| 144 | + | |
| 145 | +int main(int argc, char **argv) | |
| 146 | +{ | |
| 147 | + FILE *fi, *fh, *fo; | |
| 148 | + Elf32_Ehdr *ehdr; | |
| 149 | + Elf32_Shdr *shdr; | |
| 150 | + Elf32_Sym *sym; | |
| 151 | + int i, fsize, iarg; | |
| 152 | + char *buf, *shstr, *symtab = NULL, *strtab = NULL; | |
| 153 | + int symtabsize = 0, strtabsize = 0; | |
| 154 | + char *anames = NULL; | |
| 155 | + int *afpos = NULL; | |
| 156 | + int istrlen, strpos = 0, fpos = 0, funccnt = 0, funcmax, hofs; | |
| 157 | + char afile[260], tfile[260], stmp[20]; | |
| 158 | + | |
| 159 | + | |
| 160 | + strcpy(afile, "ar_test.a"); | |
| 161 | + iarg = 1; | |
| 162 | + | |
| 163 | + if (argc < 2) | |
| 164 | + { | |
| 165 | + printf("usage: tiny_libmaker [lib] file...\n"); | |
| 166 | + return 1; | |
| 167 | + } | |
| 168 | + for (i=1; i<argc; i++) { | |
| 169 | + istrlen = strlen(argv[i]); | |
| 170 | + if (argv[i][istrlen-2] == '.') { | |
| 171 | + if(argv[i][istrlen-1] == 'a') | |
| 172 | + strcpy(afile, argv[i]); | |
| 173 | + else if(argv[i][istrlen-1] == 'o') { | |
| 174 | + iarg = i; | |
| 175 | + break; | |
| 176 | + } | |
| 177 | + } | |
| 178 | + } | |
| 179 | + | |
| 180 | + //tfile[0] = '.'; tfile[1] = '/'; | |
| 181 | + //if (tmpnam(&tfile[2])) { | |
| 182 | + strcpy(tfile, "./XXXXXX"); | |
| 183 | + if (mktemp(tfile)) | |
| 184 | + { | |
| 185 | + if ((fo = fopen(tfile, "wb+")) == NULL) | |
| 186 | + { | |
| 187 | + fprintf(stderr, "Can't open file %s \n", tfile); | |
| 188 | + return 2; | |
| 189 | + } | |
| 190 | + } | |
| 191 | + | |
| 192 | +/* | |
| 193 | + if ((fo = tmpfile()) == NULL) | |
| 194 | + { | |
| 195 | + fprintf(stderr, "Can't open temporary file \n"); | |
| 196 | + return 2; | |
| 197 | + } | |
| 198 | +*/ | |
| 199 | + if ((fh = fopen(afile, "wb")) == NULL) | |
| 200 | + { | |
| 201 | + fprintf(stderr, "Can't open file %s \n", afile); | |
| 202 | + remove(tfile); | |
| 203 | + return 2; | |
| 204 | + } | |
| 205 | + | |
| 206 | + funcmax = 1000; | |
| 207 | + afpos = realloc(NULL, funcmax); // 250 func | |
| 208 | + memcpy(&arhdro.ar_mode, "100666", 6); | |
| 209 | + | |
| 210 | + //iarg = 1; | |
| 211 | + while (iarg < argc) | |
| 212 | + { | |
| 213 | + if (!strcmp(argv[iarg], "rcs")) { | |
| 214 | + iarg++; | |
| 215 | + continue; | |
| 216 | + } | |
| 217 | + if ((fi = fopen(argv[iarg], "rb")) == NULL) | |
| 218 | + { | |
| 219 | + fprintf(stderr, "Can't open file %s \n", argv[iarg]); | |
| 220 | + remove(tfile); | |
| 221 | + return 2; | |
| 222 | + } | |
| 223 | + fseek(fi, 0, SEEK_END); | |
| 224 | + fsize = ftell(fi); | |
| 225 | + fseek(fi, 0, SEEK_SET); | |
| 226 | + buf = malloc(fsize + 1); | |
| 227 | + fread(buf, fsize, 1, fi); | |
| 228 | + fclose(fi); | |
| 229 | + | |
| 230 | + printf("%s:\n", argv[iarg]); | |
| 231 | + // elf header | |
| 232 | + ehdr = (Elf32_Ehdr *)buf; | |
| 233 | + shdr = (Elf32_Shdr *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize); | |
| 234 | + shstr = (char *)(buf + shdr->sh_offset); | |
| 235 | + for (i = 0; i < ehdr->e_shnum; i++) | |
| 236 | + { | |
| 237 | + shdr = (Elf32_Shdr *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize); | |
| 238 | + if (!shdr->sh_offset) continue; | |
| 239 | + if (shdr->sh_type == SHT_SYMTAB) | |
| 240 | + { | |
| 241 | + symtab = (char *)(buf + shdr->sh_offset); | |
| 242 | + symtabsize = shdr->sh_size; | |
| 243 | + } | |
| 244 | + if (shdr->sh_type == SHT_STRTAB) | |
| 245 | + { | |
| 246 | + if (!strcmp(shstr + shdr->sh_name, ".strtab")) | |
| 247 | + { | |
| 248 | + strtab = (char *)(buf + shdr->sh_offset); | |
| 249 | + strtabsize = shdr->sh_size; | |
| 250 | + } | |
| 251 | + } | |
| 252 | + } | |
| 253 | + | |
| 254 | + if (symtab && symtabsize) | |
| 255 | + { | |
| 256 | + int nsym = symtabsize / sizeof(Elf32_Sym); | |
| 257 | + //printf("symtab: info size shndx name\n"); | |
| 258 | + for (i = 1; i < nsym; i++) | |
| 259 | + { | |
| 260 | + sym = (Elf32_Sym *) (symtab + i * sizeof(Elf32_Sym)); | |
| 261 | + if (sym->st_shndx && (sym->st_info == 0x11 || sym->st_info == 0x12)) { | |
| 262 | + //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name); | |
| 263 | + istrlen = strlen(strtab + sym->st_name)+1; | |
| 264 | + anames = realloc(anames, strpos+istrlen); | |
| 265 | + strcpy(anames + strpos, strtab + sym->st_name); | |
| 266 | + strpos += istrlen; | |
| 267 | + if (funccnt >= funcmax) { | |
| 268 | + afpos = realloc(NULL, funcmax+1000); // 250 func more | |
| 269 | + funcmax += 1000; | |
| 270 | + } | |
| 271 | + afpos[++funccnt] = fpos; | |
| 272 | + } | |
| 273 | + } | |
| 274 | + } | |
| 275 | + memset(&arhdro.ar_name, ' ', sizeof(arhdr.ar_name)); | |
| 276 | + strcpy(arhdro.ar_name, argv[iarg]); | |
| 277 | + arhdro.ar_name[strlen(argv[iarg])] = '/'; | |
| 278 | + sprintf(stmp, "%-10d", fsize); | |
| 279 | + memcpy(&arhdro.ar_size, stmp, 10); | |
| 280 | + fwrite(&arhdro, sizeof(arhdro), 1, fo); | |
| 281 | + fwrite(buf, fsize, 1, fo); | |
| 282 | + free(buf); | |
| 283 | + iarg++; | |
| 284 | + fpos += (fsize + sizeof(arhdro)); | |
| 285 | + } | |
| 286 | + hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int); | |
| 287 | + if ((hofs & 1)) { // align | |
| 288 | + hofs++; | |
| 289 | + fpos = 1; | |
| 290 | + } else fpos = 0; | |
| 291 | + // write header | |
| 292 | + fwrite("!<arch>\n", 8, 1, fh); | |
| 293 | + sprintf(stmp, "%-10d", strpos + (funccnt+1) * sizeof(int)); | |
| 294 | + memcpy(&arhdr.ar_size, stmp, 10); | |
| 295 | + fwrite(&arhdr, sizeof(arhdr), 1, fh); | |
| 296 | + afpos[0] = le2belong(funccnt); | |
| 297 | + for (i=1; i<=funccnt; i++) { | |
| 298 | + afpos[i] = le2belong(afpos[i] + hofs); | |
| 299 | + } | |
| 300 | + fwrite(afpos, (funccnt+1) * sizeof(int), 1, fh); | |
| 301 | + fwrite(anames, strpos, 1, fh); | |
| 302 | + if (fpos) fwrite("", 1, 1, fh); | |
| 303 | + // write objects | |
| 304 | + fseek(fo, 0, SEEK_END); | |
| 305 | + fsize = ftell(fo); | |
| 306 | + fseek(fo, 0, SEEK_SET); | |
| 307 | + buf = malloc(fsize + 1); | |
| 308 | + fread(buf, fsize, 1, fo); | |
| 309 | + fclose(fo); | |
| 310 | + fwrite(buf, fsize, 1, fh); | |
| 311 | + fclose(fh); | |
| 312 | + free(buf); | |
| 313 | + if (anames) | |
| 314 | + free(anames); | |
| 315 | + if (afpos) | |
| 316 | + free(afpos); | |
| 317 | + remove(tfile); | |
| 318 | + return 0; | |
| 319 | +} | |