2008-03-08 22:55:47 +03:00
|
|
|
/*
|
|
|
|
* This program is for making libtcc1.a without ar
|
|
|
|
* tiny_libmaker - tiny elf lib maker
|
|
|
|
* usage: tiny_libmaker [lib] files...
|
|
|
|
* Copyright (c) 2007 Timppa
|
|
|
|
*
|
2010-10-07 12:04:25 +04:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
2008-03-08 22:55:47 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2015-07-29 23:53:57 +03:00
|
|
|
#include "../../elf.h"
|
2008-03-08 22:55:47 +03:00
|
|
|
|
2011-07-14 21:23:04 +04:00
|
|
|
#ifdef TCC_TARGET_X86_64
|
|
|
|
# define ELFCLASSW ELFCLASS64
|
|
|
|
# define ElfW(type) Elf##64##_##type
|
|
|
|
# define ELFW(type) ELF##64##_##type
|
|
|
|
#else
|
|
|
|
# define ELFCLASSW ELFCLASS32
|
|
|
|
# define ElfW(type) Elf##32##_##type
|
|
|
|
# define ELFW(type) ELF##32##_##type
|
|
|
|
#endif
|
|
|
|
|
2008-03-08 22:55:47 +03:00
|
|
|
#define ARMAG "!<arch>\n"
|
|
|
|
#define ARFMAG "`\n"
|
|
|
|
|
|
|
|
typedef struct ArHdr {
|
|
|
|
char ar_name[16];
|
|
|
|
char ar_date[12];
|
|
|
|
char ar_uid[6];
|
|
|
|
char ar_gid[6];
|
|
|
|
char ar_mode[8];
|
|
|
|
char ar_size[10];
|
|
|
|
char ar_fmag[2];
|
|
|
|
} ArHdr;
|
|
|
|
|
|
|
|
unsigned long le2belong(unsigned long ul) {
|
|
|
|
return ((ul & 0xFF0000)>>8)+((ul & 0xFF000000)>>24) +
|
|
|
|
((ul & 0xFF)<<24)+((ul & 0xFF00)<<8);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArHdr arhdr = {
|
|
|
|
"/ ",
|
|
|
|
" ",
|
|
|
|
"0 ",
|
|
|
|
"0 ",
|
|
|
|
"0 ",
|
|
|
|
" ",
|
|
|
|
ARFMAG
|
|
|
|
};
|
|
|
|
|
|
|
|
ArHdr arhdro = {
|
|
|
|
" ",
|
|
|
|
" ",
|
|
|
|
"0 ",
|
|
|
|
"0 ",
|
|
|
|
"0 ",
|
|
|
|
" ",
|
|
|
|
ARFMAG
|
|
|
|
};
|
|
|
|
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
/* Returns 1 if s contains any of the chars of list, else 0 */
|
|
|
|
int contains_any(const char *s, const char *list) {
|
|
|
|
const char *l;
|
|
|
|
for (; *s; s++) {
|
|
|
|
for (l = list; *l; l++) {
|
|
|
|
if (*s == *l)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usage(int ret) {
|
|
|
|
fprintf(stderr, "usage: tiny_libmaker [rcsv] lib file...\n");
|
|
|
|
fprintf(stderr, "Always creates a new lib. [abdioptxN] are explicitly rejected.\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-03-08 22:55:47 +03:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2013-02-10 03:38:40 +04:00
|
|
|
FILE *fi, *fh = NULL, *fo = NULL;
|
2009-07-19 00:06:14 +04:00
|
|
|
ElfW(Ehdr) *ehdr;
|
|
|
|
ElfW(Shdr) *shdr;
|
|
|
|
ElfW(Sym) *sym;
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
int i, fsize, i_lib, i_obj;
|
2008-03-08 22:55:47 +03:00
|
|
|
char *buf, *shstr, *symtab = NULL, *strtab = NULL;
|
2013-02-12 22:13:28 +04:00
|
|
|
int symtabsize = 0;//, strtabsize = 0;
|
2008-03-08 22:55:47 +03:00
|
|
|
char *anames = NULL;
|
|
|
|
int *afpos = NULL;
|
|
|
|
int istrlen, strpos = 0, fpos = 0, funccnt = 0, funcmax, hofs;
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
char tfile[260], stmp[20];
|
2010-12-04 18:56:58 +03:00
|
|
|
char *file, *name;
|
2013-02-10 03:38:40 +04:00
|
|
|
int ret = 2;
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
char *ops_conflict = "habdioptxN"; // unsupported but destructive if ignored.
|
|
|
|
int verbose = 0;
|
|
|
|
|
|
|
|
i_lib = 0; i_obj = 0; // will hold the index of the lib and first obj
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
const char *a = argv[i];
|
|
|
|
if (*a == '-' && strstr(a, "."))
|
|
|
|
return usage(1); // -x.y is always invalid (same as gnu ar)
|
|
|
|
if ((*a == '-') || (i == 1 && !strstr(a, "."))) { // options argument
|
|
|
|
if (contains_any(a, ops_conflict))
|
|
|
|
return usage(1);
|
|
|
|
if (strstr(a, "v"))
|
|
|
|
verbose = 1;
|
|
|
|
} else { // lib or obj files: don't abort - keep validating all args.
|
|
|
|
if (!i_lib) // first file is the lib
|
|
|
|
i_lib = i;
|
|
|
|
else if (!i_obj) // second file is the first obj
|
|
|
|
i_obj = i;
|
2008-03-08 22:55:47 +03:00
|
|
|
}
|
|
|
|
}
|
2016-06-19 22:19:06 +03:00
|
|
|
if (!i_obj) // i_obj implies also i_lib. we require both.
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
return usage(1);
|
2008-03-08 22:55:47 +03:00
|
|
|
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
if ((fh = fopen(argv[i_lib], "wb")) == NULL)
|
2008-03-08 22:55:47 +03:00
|
|
|
{
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
fprintf(stderr, "Can't open file %s \n", argv[i_lib]);
|
2013-02-10 03:38:40 +04:00
|
|
|
goto the_end;
|
2008-03-08 22:55:47 +03:00
|
|
|
}
|
2009-05-11 20:53:52 +04:00
|
|
|
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
sprintf(tfile, "%s.tmp", argv[i_lib]);
|
2013-02-10 03:38:40 +04:00
|
|
|
if ((fo = fopen(tfile, "wb+")) == NULL)
|
2008-03-08 22:55:47 +03:00
|
|
|
{
|
2013-02-10 03:38:40 +04:00
|
|
|
fprintf(stderr, "Can't create temporary file %s\n", tfile);
|
|
|
|
goto the_end;
|
2008-03-08 22:55:47 +03:00
|
|
|
}
|
|
|
|
|
2009-04-03 22:52:24 +04:00
|
|
|
funcmax = 250;
|
|
|
|
afpos = realloc(NULL, funcmax * sizeof *afpos); // 250 func
|
2008-03-08 22:55:47 +03:00
|
|
|
memcpy(&arhdro.ar_mode, "100666", 6);
|
|
|
|
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
// i_obj = first input object file
|
|
|
|
while (i_obj < argc)
|
2008-03-08 22:55:47 +03:00
|
|
|
{
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
if (*argv[i_obj] == '-') { // by now, all options start with '-'
|
|
|
|
i_obj++;
|
2008-03-08 22:55:47 +03:00
|
|
|
continue;
|
|
|
|
}
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
if (verbose)
|
|
|
|
printf("a - %s\n", argv[i_obj]);
|
|
|
|
|
|
|
|
if ((fi = fopen(argv[i_obj], "rb")) == NULL)
|
2008-03-08 22:55:47 +03:00
|
|
|
{
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
fprintf(stderr, "Can't open file %s \n", argv[i_obj]);
|
2013-02-10 03:38:40 +04:00
|
|
|
goto the_end;
|
2008-03-08 22:55:47 +03:00
|
|
|
}
|
|
|
|
fseek(fi, 0, SEEK_END);
|
|
|
|
fsize = ftell(fi);
|
|
|
|
fseek(fi, 0, SEEK_SET);
|
|
|
|
buf = malloc(fsize + 1);
|
|
|
|
fread(buf, fsize, 1, fi);
|
|
|
|
fclose(fi);
|
|
|
|
|
|
|
|
// elf header
|
2009-07-19 00:06:14 +04:00
|
|
|
ehdr = (ElfW(Ehdr) *)buf;
|
2011-07-14 21:23:04 +04:00
|
|
|
if (ehdr->e_ident[4] != ELFCLASSW)
|
2009-07-19 00:06:14 +04:00
|
|
|
{
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
fprintf(stderr, "Unsupported Elf Class: %s\n", argv[i_obj]);
|
2013-02-10 03:38:40 +04:00
|
|
|
goto the_end;
|
2009-07-19 00:06:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + ehdr->e_shstrndx * ehdr->e_shentsize);
|
2008-03-08 22:55:47 +03:00
|
|
|
shstr = (char *)(buf + shdr->sh_offset);
|
|
|
|
for (i = 0; i < ehdr->e_shnum; i++)
|
|
|
|
{
|
2009-07-19 00:06:14 +04:00
|
|
|
shdr = (ElfW(Shdr) *) (buf + ehdr->e_shoff + i * ehdr->e_shentsize);
|
2013-02-10 03:38:40 +04:00
|
|
|
if (!shdr->sh_offset)
|
|
|
|
continue;
|
2008-03-08 22:55:47 +03:00
|
|
|
if (shdr->sh_type == SHT_SYMTAB)
|
|
|
|
{
|
|
|
|
symtab = (char *)(buf + shdr->sh_offset);
|
|
|
|
symtabsize = shdr->sh_size;
|
|
|
|
}
|
|
|
|
if (shdr->sh_type == SHT_STRTAB)
|
|
|
|
{
|
|
|
|
if (!strcmp(shstr + shdr->sh_name, ".strtab"))
|
|
|
|
{
|
|
|
|
strtab = (char *)(buf + shdr->sh_offset);
|
2013-02-12 22:13:28 +04:00
|
|
|
//strtabsize = shdr->sh_size;
|
2008-03-08 22:55:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (symtab && symtabsize)
|
|
|
|
{
|
2009-07-19 00:06:14 +04:00
|
|
|
int nsym = symtabsize / sizeof(ElfW(Sym));
|
2008-03-08 22:55:47 +03:00
|
|
|
//printf("symtab: info size shndx name\n");
|
|
|
|
for (i = 1; i < nsym; i++)
|
|
|
|
{
|
2009-07-19 00:06:14 +04:00
|
|
|
sym = (ElfW(Sym) *) (symtab + i * sizeof(ElfW(Sym)));
|
|
|
|
if (sym->st_shndx &&
|
|
|
|
(sym->st_info == 0x10
|
|
|
|
|| sym->st_info == 0x11
|
|
|
|
|| sym->st_info == 0x12
|
|
|
|
)) {
|
2008-03-08 22:55:47 +03:00
|
|
|
//printf("symtab: %2Xh %4Xh %2Xh %s\n", sym->st_info, sym->st_size, sym->st_shndx, strtab + sym->st_name);
|
|
|
|
istrlen = strlen(strtab + sym->st_name)+1;
|
|
|
|
anames = realloc(anames, strpos+istrlen);
|
|
|
|
strcpy(anames + strpos, strtab + sym->st_name);
|
|
|
|
strpos += istrlen;
|
2009-04-03 22:52:24 +04:00
|
|
|
if (++funccnt >= funcmax) {
|
|
|
|
funcmax += 250;
|
|
|
|
afpos = realloc(afpos, funcmax * sizeof *afpos); // 250 func more
|
2008-03-08 22:55:47 +03:00
|
|
|
}
|
2009-04-03 22:52:24 +04:00
|
|
|
afpos[funccnt] = fpos;
|
2008-03-08 22:55:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-04 18:56:58 +03:00
|
|
|
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
file = argv[i_obj];
|
2013-02-10 03:38:40 +04:00
|
|
|
for (name = strchr(file, 0);
|
2010-12-04 18:56:58 +03:00
|
|
|
name > file && name[-1] != '/' && name[-1] != '\\';
|
|
|
|
--name);
|
|
|
|
istrlen = strlen(name);
|
|
|
|
if (istrlen >= sizeof(arhdro.ar_name))
|
|
|
|
istrlen = sizeof(arhdro.ar_name) - 1;
|
|
|
|
memset(arhdro.ar_name, ' ', sizeof(arhdro.ar_name));
|
|
|
|
memcpy(arhdro.ar_name, name, istrlen);
|
|
|
|
arhdro.ar_name[istrlen] = '/';
|
2008-03-08 22:55:47 +03:00
|
|
|
sprintf(stmp, "%-10d", fsize);
|
|
|
|
memcpy(&arhdro.ar_size, stmp, 10);
|
|
|
|
fwrite(&arhdro, sizeof(arhdro), 1, fo);
|
|
|
|
fwrite(buf, fsize, 1, fo);
|
|
|
|
free(buf);
|
tiny_libmaker: more robust arguments interpretation
- Syntax is now much closer to gnu ar, but still supports whatever was
supported before, with the following exceptions (which gnu ar has too):
- lib is now mandatory (was optional and defaulted to ar_test.a before).
- Path cannot start with '-' (but ./-myfile.o is OK).
- Unlike gnu ar, modes are still optional (as before).
- Now supports also (like gnu ar):
- First argument as options doesn't have to start with '-', later options do.
- Now supports mode v (verbose) with same output format as gnu ar.
- Any names for lib/objs (were limited to .a/.o - broke cmake on windows).
- Now explicitly fail on options which would be destructive for the user.
- Now doesn't get confused by options between file arguments.
- Still ignores other unknown options - as before.
- Now doesn't read out-of-bounds if an option is one char.
- As a result, cmake for windows can now use tiny_libmaker as ar, and
configure can also detect tiny_libmaker as a valid ar (both couldn't before).
Ignoring all options could previously cause to misinterpret the mode in a
destructive way, e.g. if the user wanted to do something with an existing
archive (such as p - print, or x - extract, etc), then it would instead just
delete (re-create) the archive.
Modes which can be destructive if ignored now explicitly fail. These include
[habdioptxN]. Note that 'h' can be ignored, but this way we also implicitly
print the usage for -h/--help.
The .a/.o name limitations previously resulted in complete failure on some
cases, such as cmake on windows which uses <filename>.obj and <libname>.lib .
Fixed: e.g. 'tiny_libmaker r x.a x.o' was reading out of bounds [-1] for 'r'.
2016-06-12 14:51:29 +03:00
|
|
|
i_obj++;
|
2008-03-08 22:55:47 +03:00
|
|
|
fpos += (fsize + sizeof(arhdro));
|
|
|
|
}
|
|
|
|
hofs = 8 + sizeof(arhdr) + strpos + (funccnt+1) * sizeof(int);
|
2013-02-10 03:38:40 +04:00
|
|
|
fpos = 0;
|
|
|
|
if ((hofs & 1)) // align
|
|
|
|
hofs++, fpos = 1;
|
2008-03-08 22:55:47 +03:00
|
|
|
// write header
|
|
|
|
fwrite("!<arch>\n", 8, 1, fh);
|
2009-07-19 00:06:14 +04:00
|
|
|
sprintf(stmp, "%-10d", (int)(strpos + (funccnt+1) * sizeof(int)));
|
2008-03-08 22:55:47 +03:00
|
|
|
memcpy(&arhdr.ar_size, stmp, 10);
|
|
|
|
fwrite(&arhdr, sizeof(arhdr), 1, fh);
|
|
|
|
afpos[0] = le2belong(funccnt);
|
2013-02-10 03:38:40 +04:00
|
|
|
for (i=1; i<=funccnt; i++)
|
2008-03-08 22:55:47 +03:00
|
|
|
afpos[i] = le2belong(afpos[i] + hofs);
|
|
|
|
fwrite(afpos, (funccnt+1) * sizeof(int), 1, fh);
|
|
|
|
fwrite(anames, strpos, 1, fh);
|
2013-02-10 03:38:40 +04:00
|
|
|
if (fpos)
|
|
|
|
fwrite("", 1, 1, fh);
|
2008-03-08 22:55:47 +03:00
|
|
|
// write objects
|
|
|
|
fseek(fo, 0, SEEK_END);
|
|
|
|
fsize = ftell(fo);
|
|
|
|
fseek(fo, 0, SEEK_SET);
|
|
|
|
buf = malloc(fsize + 1);
|
|
|
|
fread(buf, fsize, 1, fo);
|
|
|
|
fwrite(buf, fsize, 1, fh);
|
|
|
|
free(buf);
|
2013-02-10 03:38:40 +04:00
|
|
|
ret = 0;
|
|
|
|
the_end:
|
2008-03-08 22:55:47 +03:00
|
|
|
if (anames)
|
|
|
|
free(anames);
|
|
|
|
if (afpos)
|
|
|
|
free(afpos);
|
2013-02-10 03:38:40 +04:00
|
|
|
if (fh)
|
|
|
|
fclose(fh);
|
|
|
|
if (fo)
|
|
|
|
fclose(fo), remove(tfile);
|
|
|
|
return ret;
|
2008-03-08 22:55:47 +03:00
|
|
|
}
|