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 "ar-elf.h" */
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
;
25 unsigned char e_ident
[EI_NIDENT
];
34 Elf32_Half e_phentsize
;
36 Elf32_Half e_shentsize
;
38 Elf32_Half e_shstrndx
;
50 Elf32_Word sh_addralign
;
51 Elf32_Word sh_entsize
;
55 #define SHT_PROGBITS 1
71 unsigned char st_info
;
72 unsigned char st_other
;
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))
104 /* "ar-elf.h" ends */
106 #define ARMAG "!<arch>\n"
109 typedef struct ArHdr
{
120 unsigned long le2belong(unsigned long ul
) {
121 return ((ul
& 0xFF0000)>>8)+((ul
& 0xFF000000)>>24) +
122 ((ul
& 0xFF)<<24)+((ul
& 0xFF00)<<8);
145 int main(int argc
, char **argv
)
152 char *buf
, *shstr
, *symtab
= NULL
, *strtab
= NULL
;
153 int symtabsize
= 0, strtabsize
= 0;
156 int istrlen
, strpos
= 0, fpos
= 0, funccnt
= 0, funcmax
, hofs
;
157 char afile
[260], tfile
[260], stmp
[20];
160 strcpy(afile
, "ar_test.a");
165 printf("usage: tiny_libmaker [lib] file...\n");
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') {
180 //tfile[0] = '.'; tfile[1] = '/';
181 //if (tmpnam(&tfile[2])) {
182 strcpy(tfile
, "./XXXXXX");
185 if ((fo
= fopen(tfile
, "wb+")) == NULL
)
187 fprintf(stderr
, "Can't open file %s \n", tfile
);
193 if ((fo = tmpfile()) == NULL)
195 fprintf(stderr, "Can't open temporary file \n");
199 if ((fh
= fopen(afile
, "wb")) == NULL
)
201 fprintf(stderr
, "Can't open file %s \n", afile
);
207 afpos
= realloc(NULL
, funcmax
); // 250 func
208 memcpy(&arhdro
.ar_mode
, "100666", 6);
213 if (!strcmp(argv
[iarg
], "rcs")) {
217 if ((fi
= fopen(argv
[iarg
], "rb")) == NULL
)
219 fprintf(stderr
, "Can't open file %s \n", argv
[iarg
]);
223 fseek(fi
, 0, SEEK_END
);
225 fseek(fi
, 0, SEEK_SET
);
226 buf
= malloc(fsize
+ 1);
227 fread(buf
, fsize
, 1, fi
);
230 printf("%s:\n", argv
[iarg
]);
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
++)
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
)
241 symtab
= (char *)(buf
+ shdr
->sh_offset
);
242 symtabsize
= shdr
->sh_size
;
244 if (shdr
->sh_type
== SHT_STRTAB
)
246 if (!strcmp(shstr
+ shdr
->sh_name
, ".strtab"))
248 strtab
= (char *)(buf
+ shdr
->sh_offset
);
249 strtabsize
= shdr
->sh_size
;
254 if (symtab
&& symtabsize
)
256 int nsym
= symtabsize
/ sizeof(Elf32_Sym
);
257 //printf("symtab: info size shndx name\n");
258 for (i
= 1; i
< nsym
; i
++)
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
);
267 if (funccnt
>= funcmax
) {
268 afpos
= realloc(NULL
, funcmax
+1000); // 250 func more
271 afpos
[++funccnt
] = fpos
;
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
);
284 fpos
+= (fsize
+ sizeof(arhdro
));
286 hofs
= 8 + sizeof(arhdr
) + strpos
+ (funccnt
+1) * sizeof(int);
287 if ((hofs
& 1)) { // align
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
);
300 fwrite(afpos
, (funccnt
+1) * sizeof(int), 1, fh
);
301 fwrite(anames
, strpos
, 1, fh
);
302 if (fpos
) fwrite("", 1, 1, fh
);
304 fseek(fo
, 0, SEEK_END
);
306 fseek(fo
, 0, SEEK_SET
);
307 buf
= malloc(fsize
+ 1);
308 fread(buf
, fsize
, 1, fo
);
310 fwrite(buf
, fsize
, 1, fh
);