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 /* Returns 1 if s contains any of the chars of list, else 0 */
75 int contains_any(const char *s
, const char *list
) {
78 for (l
= list
; *l
; l
++) {
87 fprintf(stderr
, "usage: tiny_libmaker [rcsv] lib file...\n");
88 fprintf(stderr
, "Always creates a new lib. [abdioptxN] are explicitly rejected.\n");
92 int main(int argc
, char **argv
)
94 FILE *fi
, *fh
= NULL
, *fo
= NULL
;
98 int i
, fsize
, i_lib
, i_obj
;
99 char *buf
, *shstr
, *symtab
= NULL
, *strtab
= NULL
;
100 int symtabsize
= 0;//, strtabsize = 0;
103 int istrlen
, strpos
= 0, fpos
= 0, funccnt
= 0, funcmax
, hofs
;
104 char tfile
[260], stmp
[20];
107 char *ops_conflict
= "habdioptxN"; // unsupported but destructive if ignored.
110 i_lib
= 0; i_obj
= 0; // will hold the index of the lib and first obj
111 for (i
= 1; i
< argc
; i
++) {
112 const char *a
= argv
[i
];
113 if (*a
== '-' && strstr(a
, "."))
114 return usage(1); // -x.y is always invalid (same as gnu ar)
115 if ((*a
== '-') || (i
== 1 && !strstr(a
, "."))) { // options argument
116 if (contains_any(a
, ops_conflict
))
120 } else { // lib or obj files: don't abort - keep validating all args.
121 if (!i_lib
) // first file is the lib
123 else if (!i_obj
) // second file is the first obj
127 if (!i_obj
) // i_obj implies also i_lib. we require both.
130 if ((fh
= fopen(argv
[i_lib
], "wb")) == NULL
)
132 fprintf(stderr
, "Can't open file %s \n", argv
[i_lib
]);
136 sprintf(tfile
, "%s.tmp", argv
[i_lib
]);
137 if ((fo
= fopen(tfile
, "wb+")) == NULL
)
139 fprintf(stderr
, "Can't create temporary file %s\n", tfile
);
144 afpos
= realloc(NULL
, funcmax
* sizeof *afpos
); // 250 func
145 memcpy(&arhdro
.ar_mode
, "100666", 6);
147 // i_obj = first input object file
150 if (*argv
[i_obj
] == '-') { // by now, all options start with '-'
155 printf("a - %s\n", argv
[i_obj
]);
157 if ((fi
= fopen(argv
[i_obj
], "rb")) == NULL
)
159 fprintf(stderr
, "Can't open file %s \n", argv
[i_obj
]);
162 fseek(fi
, 0, SEEK_END
);
164 fseek(fi
, 0, SEEK_SET
);
165 buf
= malloc(fsize
+ 1);
166 fread(buf
, fsize
, 1, fi
);
170 ehdr
= (ElfW(Ehdr
) *)buf
;
171 if (ehdr
->e_ident
[4] != ELFCLASSW
)
173 fprintf(stderr
, "Unsupported Elf Class: %s\n", argv
[i_obj
]);
177 shdr
= (ElfW(Shdr
) *) (buf
+ ehdr
->e_shoff
+ ehdr
->e_shstrndx
* ehdr
->e_shentsize
);
178 shstr
= (char *)(buf
+ shdr
->sh_offset
);
179 for (i
= 0; i
< ehdr
->e_shnum
; i
++)
181 shdr
= (ElfW(Shdr
) *) (buf
+ ehdr
->e_shoff
+ i
* ehdr
->e_shentsize
);
182 if (!shdr
->sh_offset
)
184 if (shdr
->sh_type
== SHT_SYMTAB
)
186 symtab
= (char *)(buf
+ shdr
->sh_offset
);
187 symtabsize
= shdr
->sh_size
;
189 if (shdr
->sh_type
== SHT_STRTAB
)
191 if (!strcmp(shstr
+ shdr
->sh_name
, ".strtab"))
193 strtab
= (char *)(buf
+ shdr
->sh_offset
);
194 //strtabsize = shdr->sh_size;
199 if (symtab
&& symtabsize
)
201 int nsym
= symtabsize
/ sizeof(ElfW(Sym
));
202 //printf("symtab: info size shndx name\n");
203 for (i
= 1; i
< nsym
; i
++)
205 sym
= (ElfW(Sym
) *) (symtab
+ i
* sizeof(ElfW(Sym
)));
207 (sym
->st_info
== 0x10
208 || sym
->st_info
== 0x11
209 || sym
->st_info
== 0x12
211 //printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
212 istrlen
= strlen(strtab
+ sym
->st_name
)+1;
213 anames
= realloc(anames
, strpos
+istrlen
);
214 strcpy(anames
+ strpos
, strtab
+ sym
->st_name
);
216 if (++funccnt
>= funcmax
) {
218 afpos
= realloc(afpos
, funcmax
* sizeof *afpos
); // 250 func more
220 afpos
[funccnt
] = fpos
;
226 for (name
= strchr(file
, 0);
227 name
> file
&& name
[-1] != '/' && name
[-1] != '\\';
229 istrlen
= strlen(name
);
230 if (istrlen
>= sizeof(arhdro
.ar_name
))
231 istrlen
= sizeof(arhdro
.ar_name
) - 1;
232 memset(arhdro
.ar_name
, ' ', sizeof(arhdro
.ar_name
));
233 memcpy(arhdro
.ar_name
, name
, istrlen
);
234 arhdro
.ar_name
[istrlen
] = '/';
235 sprintf(stmp
, "%-10d", fsize
);
236 memcpy(&arhdro
.ar_size
, stmp
, 10);
237 fwrite(&arhdro
, sizeof(arhdro
), 1, fo
);
238 fwrite(buf
, fsize
, 1, fo
);
241 fpos
+= (fsize
+ sizeof(arhdro
));
243 hofs
= 8 + sizeof(arhdr
) + strpos
+ (funccnt
+1) * sizeof(int);
245 if ((hofs
& 1)) // align
248 fwrite("!<arch>\n", 8, 1, fh
);
249 sprintf(stmp
, "%-10d", (int)(strpos
+ (funccnt
+1) * sizeof(int)));
250 memcpy(&arhdr
.ar_size
, stmp
, 10);
251 fwrite(&arhdr
, sizeof(arhdr
), 1, fh
);
252 afpos
[0] = le2belong(funccnt
);
253 for (i
=1; i
<=funccnt
; i
++)
254 afpos
[i
] = le2belong(afpos
[i
] + hofs
);
255 fwrite(afpos
, (funccnt
+1) * sizeof(int), 1, fh
);
256 fwrite(anames
, strpos
, 1, fh
);
258 fwrite("", 1, 1, fh
);
260 fseek(fo
, 0, SEEK_END
);
262 fseek(fo
, 0, SEEK_SET
);
263 buf
= malloc(fsize
+ 1);
264 fread(buf
, fsize
, 1, fo
);
265 fwrite(buf
, fsize
, 1, fh
);
276 fclose(fo
), remove(tfile
);