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 program is free software but WITHOUT ANY WARRANTY
13 #include <io.h> /* for mktemp */
16 /* #include "ar-elf.h" */
19 typedef unsigned short int Elf32_Half
;
20 typedef int Elf32_Sword
;
21 typedef unsigned int Elf32_Word
;
22 typedef unsigned int Elf32_Addr
;
23 typedef unsigned int Elf32_Off
;
24 typedef unsigned short int Elf32_Section
;
28 unsigned char e_ident
[EI_NIDENT
];
37 Elf32_Half e_phentsize
;
39 Elf32_Half e_shentsize
;
41 Elf32_Half e_shstrndx
;
53 Elf32_Word sh_addralign
;
54 Elf32_Word sh_entsize
;
58 #define SHT_PROGBITS 1
74 unsigned char st_info
;
75 unsigned char st_other
;
79 #define ELF32_ST_BIND(i) ((i)>>4)
80 #define ELF32_ST_TYPE(i) ((i)&0xf)
81 #define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
107 /* "ar-elf.h" ends */
109 #define ARMAG "!<arch>\n"
112 typedef struct ArHdr
{
123 unsigned long le2belong(unsigned long ul
) {
124 return ((ul
& 0xFF0000)>>8)+((ul
& 0xFF000000)>>24) +
125 ((ul
& 0xFF)<<24)+((ul
& 0xFF00)<<8);
148 int main(int argc
, char **argv
)
155 char *buf
, *shstr
, *symtab
= NULL
, *strtab
= NULL
;
156 int symtabsize
= 0, strtabsize
= 0;
159 int istrlen
, strpos
= 0, fpos
= 0, funccnt
= 0, funcmax
, hofs
;
160 char afile
[260], tfile
[260], stmp
[20];
163 strcpy(afile
, "ar_test.a");
168 printf("usage: tiny_libmaker [lib] file...\n");
171 for (i
=1; i
<argc
; i
++) {
172 istrlen
= strlen(argv
[i
]);
173 if (argv
[i
][istrlen
-2] == '.') {
174 if(argv
[i
][istrlen
-1] == 'a')
175 strcpy(afile
, argv
[i
]);
176 else if(argv
[i
][istrlen
-1] == 'o') {
183 strcpy(tfile
, "./XXXXXX");
184 if (!mktemp(tfile
) || (fo
= fopen(tfile
, "wb+")) == NULL
)
186 fprintf(stderr
, "Can't open temporary file %s\n", tfile
);
190 if ((fh
= fopen(afile
, "wb")) == NULL
)
192 fprintf(stderr
, "Can't open file %s \n", afile
);
198 afpos
= realloc(NULL
, funcmax
* sizeof *afpos
); // 250 func
199 memcpy(&arhdro
.ar_mode
, "100666", 6);
204 if (!strcmp(argv
[iarg
], "rcs")) {
208 if ((fi
= fopen(argv
[iarg
], "rb")) == NULL
)
210 fprintf(stderr
, "Can't open file %s \n", argv
[iarg
]);
214 fseek(fi
, 0, SEEK_END
);
216 fseek(fi
, 0, SEEK_SET
);
217 buf
= malloc(fsize
+ 1);
218 fread(buf
, fsize
, 1, fi
);
221 printf("%s:\n", argv
[iarg
]);
223 ehdr
= (Elf32_Ehdr
*)buf
;
224 shdr
= (Elf32_Shdr
*) (buf
+ ehdr
->e_shoff
+ ehdr
->e_shstrndx
* ehdr
->e_shentsize
);
225 shstr
= (char *)(buf
+ shdr
->sh_offset
);
226 for (i
= 0; i
< ehdr
->e_shnum
; i
++)
228 shdr
= (Elf32_Shdr
*) (buf
+ ehdr
->e_shoff
+ i
* ehdr
->e_shentsize
);
229 if (!shdr
->sh_offset
) continue;
230 if (shdr
->sh_type
== SHT_SYMTAB
)
232 symtab
= (char *)(buf
+ shdr
->sh_offset
);
233 symtabsize
= shdr
->sh_size
;
235 if (shdr
->sh_type
== SHT_STRTAB
)
237 if (!strcmp(shstr
+ shdr
->sh_name
, ".strtab"))
239 strtab
= (char *)(buf
+ shdr
->sh_offset
);
240 strtabsize
= shdr
->sh_size
;
245 if (symtab
&& symtabsize
)
247 int nsym
= symtabsize
/ sizeof(Elf32_Sym
);
248 //printf("symtab: info size shndx name\n");
249 for (i
= 1; i
< nsym
; i
++)
251 sym
= (Elf32_Sym
*) (symtab
+ i
* sizeof(Elf32_Sym
));
252 if (sym
->st_shndx
&& (sym
->st_info
== 0x11 || sym
->st_info
== 0x12)) {
253 //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
254 istrlen
= strlen(strtab
+ sym
->st_name
)+1;
255 anames
= realloc(anames
, strpos
+istrlen
);
256 strcpy(anames
+ strpos
, strtab
+ sym
->st_name
);
258 if (++funccnt
>= funcmax
) {
260 afpos
= realloc(afpos
, funcmax
* sizeof *afpos
); // 250 func more
262 afpos
[funccnt
] = fpos
;
266 memset(&arhdro
.ar_name
, ' ', sizeof(arhdr
.ar_name
));
267 strcpy(arhdro
.ar_name
, argv
[iarg
]);
268 arhdro
.ar_name
[strlen(argv
[iarg
])] = '/';
269 sprintf(stmp
, "%-10d", fsize
);
270 memcpy(&arhdro
.ar_size
, stmp
, 10);
271 fwrite(&arhdro
, sizeof(arhdro
), 1, fo
);
272 fwrite(buf
, fsize
, 1, fo
);
275 fpos
+= (fsize
+ sizeof(arhdro
));
277 hofs
= 8 + sizeof(arhdr
) + strpos
+ (funccnt
+1) * sizeof(int);
278 if ((hofs
& 1)) { // align
283 fwrite("!<arch>\n", 8, 1, fh
);
284 sprintf(stmp
, "%-10d", strpos
+ (funccnt
+1) * sizeof(int));
285 memcpy(&arhdr
.ar_size
, stmp
, 10);
286 fwrite(&arhdr
, sizeof(arhdr
), 1, fh
);
287 afpos
[0] = le2belong(funccnt
);
288 for (i
=1; i
<=funccnt
; i
++) {
289 afpos
[i
] = le2belong(afpos
[i
] + hofs
);
291 fwrite(afpos
, (funccnt
+1) * sizeof(int), 1, fh
);
292 fwrite(anames
, strpos
, 1, fh
);
293 if (fpos
) fwrite("", 1, 1, fh
);
295 fseek(fo
, 0, SEEK_END
);
297 fseek(fo
, 0, SEEK_SET
);
298 buf
= malloc(fsize
+ 1);
299 fread(buf
, fsize
, 1, fo
);
301 fwrite(buf
, fsize
, 1, fh
);