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.
24 #include "../../elf.h"
26 #ifdef TCC_TARGET_X86_64
27 # define ELFCLASSW ELFCLASS64
28 # define ElfW(type) Elf##64##_##type
29 # define ELFW(type) ELF##64##_##type
31 # define ELFCLASSW ELFCLASS32
32 # define ElfW(type) Elf##32##_##type
33 # define ELFW(type) ELF##32##_##type
36 #define ARMAG "!<arch>\n"
39 typedef struct ArHdr
{
49 unsigned long le2belong(unsigned long ul
) {
50 return ((ul
& 0xFF0000)>>8)+((ul
& 0xFF000000)>>24) +
51 ((ul
& 0xFF)<<24)+((ul
& 0xFF00)<<8);
74 int main(int argc
, char **argv
)
76 FILE *fi
, *fh
= NULL
, *fo
= NULL
;
81 char *buf
, *shstr
, *symtab
= NULL
, *strtab
= NULL
;
82 int symtabsize
= 0;//, strtabsize = 0;
85 int istrlen
, strpos
= 0, fpos
= 0, funccnt
= 0, funcmax
, hofs
;
86 char afile
[260], tfile
[260], stmp
[20];
91 strcpy(afile
, "ar_test.a");
96 printf("usage: tiny_libmaker [lib] file...\n");
99 for (i
=1; i
<argc
; i
++) {
100 istrlen
= strlen(argv
[i
]);
101 if (argv
[i
][istrlen
-2] == '.') {
102 if(argv
[i
][istrlen
-1] == 'a')
103 strcpy(afile
, argv
[i
]);
104 else if(argv
[i
][istrlen
-1] == 'o') {
111 if ((fh
= fopen(afile
, "wb")) == NULL
)
113 fprintf(stderr
, "Can't open file %s \n", afile
);
117 sprintf(tfile
, "%s.tmp", afile
);
118 if ((fo
= fopen(tfile
, "wb+")) == NULL
)
120 fprintf(stderr
, "Can't create temporary file %s\n", tfile
);
125 afpos
= realloc(NULL
, funcmax
* sizeof *afpos
); // 250 func
126 memcpy(&arhdro
.ar_mode
, "100666", 6);
131 if (!strcmp(argv
[iarg
], "rcs")) {
135 if ((fi
= fopen(argv
[iarg
], "rb")) == NULL
)
137 fprintf(stderr
, "Can't open file %s \n", argv
[iarg
]);
140 fseek(fi
, 0, SEEK_END
);
142 fseek(fi
, 0, SEEK_SET
);
143 buf
= malloc(fsize
+ 1);
144 fread(buf
, fsize
, 1, fi
);
147 //printf("%s:\n", argv[iarg]);
150 ehdr
= (ElfW(Ehdr
) *)buf
;
151 if (ehdr
->e_ident
[4] != ELFCLASSW
)
153 fprintf(stderr
, "Unsupported Elf Class: %s\n", argv
[iarg
]);
157 shdr
= (ElfW(Shdr
) *) (buf
+ ehdr
->e_shoff
+ ehdr
->e_shstrndx
* ehdr
->e_shentsize
);
158 shstr
= (char *)(buf
+ shdr
->sh_offset
);
159 for (i
= 0; i
< ehdr
->e_shnum
; i
++)
161 shdr
= (ElfW(Shdr
) *) (buf
+ ehdr
->e_shoff
+ i
* ehdr
->e_shentsize
);
162 if (!shdr
->sh_offset
)
164 if (shdr
->sh_type
== SHT_SYMTAB
)
166 symtab
= (char *)(buf
+ shdr
->sh_offset
);
167 symtabsize
= shdr
->sh_size
;
169 if (shdr
->sh_type
== SHT_STRTAB
)
171 if (!strcmp(shstr
+ shdr
->sh_name
, ".strtab"))
173 strtab
= (char *)(buf
+ shdr
->sh_offset
);
174 //strtabsize = shdr->sh_size;
179 if (symtab
&& symtabsize
)
181 int nsym
= symtabsize
/ sizeof(ElfW(Sym
));
182 //printf("symtab: info size shndx name\n");
183 for (i
= 1; i
< nsym
; i
++)
185 sym
= (ElfW(Sym
) *) (symtab
+ i
* sizeof(ElfW(Sym
)));
187 (sym
->st_info
== 0x10
188 || sym
->st_info
== 0x11
189 || sym
->st_info
== 0x12
191 //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
192 istrlen
= strlen(strtab
+ sym
->st_name
)+1;
193 anames
= realloc(anames
, strpos
+istrlen
);
194 strcpy(anames
+ strpos
, strtab
+ sym
->st_name
);
196 if (++funccnt
>= funcmax
) {
198 afpos
= realloc(afpos
, funcmax
* sizeof *afpos
); // 250 func more
200 afpos
[funccnt
] = fpos
;
206 for (name
= strchr(file
, 0);
207 name
> file
&& name
[-1] != '/' && name
[-1] != '\\';
209 istrlen
= strlen(name
);
210 if (istrlen
>= sizeof(arhdro
.ar_name
))
211 istrlen
= sizeof(arhdro
.ar_name
) - 1;
212 memset(arhdro
.ar_name
, ' ', sizeof(arhdro
.ar_name
));
213 memcpy(arhdro
.ar_name
, name
, istrlen
);
214 arhdro
.ar_name
[istrlen
] = '/';
215 sprintf(stmp
, "%-10d", fsize
);
216 memcpy(&arhdro
.ar_size
, stmp
, 10);
217 fwrite(&arhdro
, sizeof(arhdro
), 1, fo
);
218 fwrite(buf
, fsize
, 1, fo
);
221 fpos
+= (fsize
+ sizeof(arhdro
));
223 hofs
= 8 + sizeof(arhdr
) + strpos
+ (funccnt
+1) * sizeof(int);
225 if ((hofs
& 1)) // align
228 fwrite("!<arch>\n", 8, 1, fh
);
229 sprintf(stmp
, "%-10d", (int)(strpos
+ (funccnt
+1) * sizeof(int)));
230 memcpy(&arhdr
.ar_size
, stmp
, 10);
231 fwrite(&arhdr
, sizeof(arhdr
), 1, fh
);
232 afpos
[0] = le2belong(funccnt
);
233 for (i
=1; i
<=funccnt
; i
++)
234 afpos
[i
] = le2belong(afpos
[i
] + hofs
);
235 fwrite(afpos
, (funccnt
+1) * sizeof(int), 1, fh
);
236 fwrite(anames
, strpos
, 1, fh
);
238 fwrite("", 1, 1, fh
);
240 fseek(fo
, 0, SEEK_END
);
242 fseek(fo
, 0, SEEK_SET
);
243 buf
= malloc(fsize
+ 1);
244 fread(buf
, fsize
, 1, fo
);
245 fwrite(buf
, fsize
, 1, fh
);
256 fclose(fo
), remove(tfile
);