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
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <io.h> /* for mktemp */
28 #include "../../elf.h"
30 #ifdef TCC_TARGET_X86_64
31 # define ELFCLASSW ELFCLASS64
32 # define ElfW(type) Elf##64##_##type
33 # define ELFW(type) ELF##64##_##type
35 # define ELFCLASSW ELFCLASS32
36 # define ElfW(type) Elf##32##_##type
37 # define ELFW(type) ELF##32##_##type
40 #define ARMAG "!<arch>\n"
43 typedef struct ArHdr
{
53 unsigned long le2belong(unsigned long ul
) {
54 return ((ul
& 0xFF0000)>>8)+((ul
& 0xFF000000)>>24) +
55 ((ul
& 0xFF)<<24)+((ul
& 0xFF00)<<8);
78 int main(int argc
, char **argv
)
85 char *buf
, *shstr
, *symtab
= NULL
, *strtab
= NULL
;
86 int symtabsize
= 0, strtabsize
= 0;
89 int istrlen
, strpos
= 0, fpos
= 0, funccnt
= 0, funcmax
, hofs
;
90 char afile
[260], tfile
[260], stmp
[20];
94 strcpy(afile
, "ar_test.a");
99 printf("usage: tiny_libmaker [lib] file...\n");
102 for (i
=1; i
<argc
; i
++) {
103 istrlen
= strlen(argv
[i
]);
104 if (argv
[i
][istrlen
-2] == '.') {
105 if(argv
[i
][istrlen
-1] == 'a')
106 strcpy(afile
, argv
[i
]);
107 else if(argv
[i
][istrlen
-1] == 'o') {
114 strcpy(tfile
, "./XXXXXX");
115 if (!mktemp(tfile
) || (fo
= fopen(tfile
, "wb+")) == NULL
)
117 fprintf(stderr
, "Can't open temporary file %s\n", tfile
);
121 if ((fh
= fopen(afile
, "wb")) == NULL
)
123 fprintf(stderr
, "Can't open file %s \n", afile
);
130 afpos
= realloc(NULL
, funcmax
* sizeof *afpos
); // 250 func
131 memcpy(&arhdro
.ar_mode
, "100666", 6);
136 if (!strcmp(argv
[iarg
], "rcs")) {
140 if ((fi
= fopen(argv
[iarg
], "rb")) == NULL
)
142 fprintf(stderr
, "Can't open file %s \n", argv
[iarg
]);
147 fseek(fi
, 0, SEEK_END
);
149 fseek(fi
, 0, SEEK_SET
);
150 buf
= malloc(fsize
+ 1);
151 fread(buf
, fsize
, 1, fi
);
154 //printf("%s:\n", argv[iarg]);
156 ehdr
= (ElfW(Ehdr
) *)buf
;
157 if (ehdr
->e_ident
[4] != ELFCLASSW
)
159 fprintf(stderr
, "Unsupported Elf Class: %s\n", argv
[iarg
]);
165 shdr
= (ElfW(Shdr
) *) (buf
+ ehdr
->e_shoff
+ ehdr
->e_shstrndx
* ehdr
->e_shentsize
);
166 shstr
= (char *)(buf
+ shdr
->sh_offset
);
167 for (i
= 0; i
< ehdr
->e_shnum
; i
++)
169 shdr
= (ElfW(Shdr
) *) (buf
+ ehdr
->e_shoff
+ i
* ehdr
->e_shentsize
);
170 if (!shdr
->sh_offset
) continue;
171 if (shdr
->sh_type
== SHT_SYMTAB
)
173 symtab
= (char *)(buf
+ shdr
->sh_offset
);
174 symtabsize
= shdr
->sh_size
;
176 if (shdr
->sh_type
== SHT_STRTAB
)
178 if (!strcmp(shstr
+ shdr
->sh_name
, ".strtab"))
180 strtab
= (char *)(buf
+ shdr
->sh_offset
);
181 strtabsize
= shdr
->sh_size
;
186 if (symtab
&& symtabsize
)
188 int nsym
= symtabsize
/ sizeof(ElfW(Sym
));
189 //printf("symtab: info size shndx name\n");
190 for (i
= 1; i
< nsym
; i
++)
192 sym
= (ElfW(Sym
) *) (symtab
+ i
* sizeof(ElfW(Sym
)));
194 (sym
->st_info
== 0x10
195 || sym
->st_info
== 0x11
196 || sym
->st_info
== 0x12
198 //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
199 istrlen
= strlen(strtab
+ sym
->st_name
)+1;
200 anames
= realloc(anames
, strpos
+istrlen
);
201 strcpy(anames
+ strpos
, strtab
+ sym
->st_name
);
203 if (++funccnt
>= funcmax
) {
205 afpos
= realloc(afpos
, funcmax
* sizeof *afpos
); // 250 func more
207 afpos
[funccnt
] = fpos
;
213 for (name
= strchr(file
, 0);
214 name
> file
&& name
[-1] != '/' && name
[-1] != '\\';
216 istrlen
= strlen(name
);
217 if (istrlen
>= sizeof(arhdro
.ar_name
))
218 istrlen
= sizeof(arhdro
.ar_name
) - 1;
219 memset(arhdro
.ar_name
, ' ', sizeof(arhdro
.ar_name
));
220 memcpy(arhdro
.ar_name
, name
, istrlen
);
221 arhdro
.ar_name
[istrlen
] = '/';
223 sprintf(stmp
, "%-10d", fsize
);
224 memcpy(&arhdro
.ar_size
, stmp
, 10);
225 fwrite(&arhdro
, sizeof(arhdro
), 1, fo
);
226 fwrite(buf
, fsize
, 1, fo
);
229 fpos
+= (fsize
+ sizeof(arhdro
));
231 hofs
= 8 + sizeof(arhdr
) + strpos
+ (funccnt
+1) * sizeof(int);
232 if ((hofs
& 1)) { // align
237 fwrite("!<arch>\n", 8, 1, fh
);
238 sprintf(stmp
, "%-10d", (int)(strpos
+ (funccnt
+1) * sizeof(int)));
239 memcpy(&arhdr
.ar_size
, stmp
, 10);
240 fwrite(&arhdr
, sizeof(arhdr
), 1, fh
);
241 afpos
[0] = le2belong(funccnt
);
242 for (i
=1; i
<=funccnt
; i
++) {
243 afpos
[i
] = le2belong(afpos
[i
] + hofs
);
245 fwrite(afpos
, (funccnt
+1) * sizeof(int), 1, fh
);
246 fwrite(anames
, strpos
, 1, fh
);
247 if (fpos
) fwrite("", 1, 1, fh
);
249 fseek(fo
, 0, SEEK_END
);
251 fseek(fo
, 0, SEEK_SET
);
252 buf
= malloc(fsize
+ 1);
253 fread(buf
, fsize
, 1, fo
);
255 fwrite(buf
, fsize
, 1, fh
);