From 368260c0d5a31b414577a41a0a3e01f4e149ff0a Mon Sep 17 00:00:00 2001 From: David McCullough Date: Tue, 7 Jul 2009 04:39:42 +0000 Subject: [PATCH] Unify the duplicated windows and other system fallback logic in stubs.h and add some fatal() helper functions to standardize the error output when falling over. This way we don't end up with obscure error messages with no idea what util they are coming from. Signed-off-by: Mike Frysinger --- Makefile.in | 2 +- compress.c | 13 ++-- elf2flt.c | 179 +++++++++++----------------------------------------- flthdr.c | 29 +++------ stubs.c | 41 +++++++++++- stubs.h | 38 +++++++++++ 6 files changed, 130 insertions(+), 172 deletions(-) create mode 100644 stubs.h diff --git a/Makefile.in b/Makefile.in index 8b82809..fc00851 100644 --- a/Makefile.in +++ b/Makefile.in @@ -55,7 +55,7 @@ link = $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS) $(PROG_ELF2FLT): elf2flt.o compress.o stubs.o $(link) -$(PROG_FLTHDR): flthdr.o compress.o +$(PROG_FLTHDR): flthdr.o compress.o stubs.o $(link) ld-elf2flt: $(srcdir)/ld-elf2flt.in diff --git a/compress.c b/compress.c index e5d4f17..144c96f 100644 --- a/compress.c +++ b/compress.c @@ -13,6 +13,7 @@ #include #include "compress.h" +#include "stubs.h" /* Open an (uncompressed) file as a stream. Return 0 on success, 1 on error. @@ -187,15 +188,11 @@ transfer(stream *ifp, stream *ofp, int count) n = fread_stream(cmd, 1, num, ifp); if (n == 0) break; - if (fwrite_stream(cmd, n, 1, ofp) != 1) { - fprintf(stderr, "Write failed :-(\n"); - exit(1); - } + if (fwrite_stream(cmd, n, 1, ofp) != 1) + fatal_perror("Write failed :-(\n"); if (count != -1) count -= n; } - if (count > 0) { - fprintf(stderr, "Failed to transfer %d bytes\n", count); - exit(1); - } + if (count > 0) + fatal("Failed to transfer %d bytes\n", count); } diff --git a/elf2flt.c b/elf2flt.c index 0ae3ad1..5e9c698 100644 --- a/elf2flt.c +++ b/elf2flt.c @@ -44,18 +44,14 @@ #include /* Userland prototypes of the Unix std system calls */ #include /* Flag value for file handling functions */ #include -#ifndef WIN32 -#include /* Consts and structs defined by the internet system */ -#define BINARY_FILE_OPTS -#else -#include -#define BINARY_FILE_OPTS "b" -#endif /* from $(INSTALLDIR)/include */ #include /* Main header file for the BFD library */ #include +#include "stubs.h" +const char *elf2flt_progname; + #if defined(TARGET_h8300) #include /* TARGET_* ELF support for the BFD library */ #elif defined(__CYGWIN__) || defined(__MINGW32__) || defined(TARGET_nios) || defined(TARGET_nios2) @@ -153,75 +149,6 @@ int use_resolved = 0; /* If true, get the value of symbol references from */ /* Set if the text section contains any relocations. If it does, we must set the load_to_ram flag. */ int text_has_relocs = 0; -const char *progname, *filename; -int lineno; - -int nerrors = 0; -int nwarnings = 0; - -static char where[200]; - -enum { - /* Use exactly one of these: */ - E_NOFILE = 0, /* "progname: " */ - E_FILE = 1, /* "filename: " */ - E_FILELINE = 2, /* "filename:lineno: " */ - E_FILEWHERE = 3, /* "filename:%s: " -- set %s with ewhere() */ - - /* Add in any of these with |': */ - E_WARNING = 0x10, - E_PERROR = 0x20 -}; - -void ewhere (const char *format, ...); -void einfo (int type, const char *format, ...); - - -void -ewhere (const char *format, ...) { - va_list args; - va_start (args, format); - vsprintf (where, format, args); - va_end (args); -} - - -void -einfo (int type, const char *format, ...) { - va_list args; - - switch (type & 0x0f) { - case E_NOFILE: - fprintf (stderr, "%s: ", progname); - break; - case E_FILE: - fprintf (stderr, "%s: ", filename); - break; - case E_FILELINE: - ewhere ("%d", lineno); - /* fall-through */ - case E_FILEWHERE: - fprintf (stderr, "%s:%s: ", filename, where); - break; - } - - if (type & E_WARNING) { - fprintf (stderr, "warning: "); - nwarnings++; - } else { - nerrors++; - } - - va_start (args, format); - vfprintf (stderr, format, args); - va_end (args); - - if (type & E_PERROR) - perror (""); - else - fprintf (stderr, "\n"); -} - asymbol** get_symbols (bfd *abfd, long *num) @@ -443,11 +370,9 @@ dump_symbols(symbols, number_of_symbols); printf("GOT table contains %d entries (%d bytes)\n", got_size/sizeof(uint32_t), got_size); #ifdef TARGET_m68k - if (got_size > GOT_LIMIT) { - fprintf(stderr, "GOT too large: %d bytes (limit = %d bytes)\n", - got_size, GOT_LIMIT); - exit(1); - } + if (got_size > GOT_LIMIT) + fatal("GOT too large: %d bytes (limit = %d bytes)", + got_size, GOT_LIMIT); #endif } @@ -1645,10 +1570,8 @@ printf("%s(%d): symbol name=%s address=0x%x section=%s -> RELOC=0x%x\n", -static char * program; - static void usage(void) -{ +{ fprintf(stderr, "Usage: %s [vrzd] [-p ] [-s stack-size] " "[-o ] \n\n" " -v : verbose operation\n" @@ -1663,7 +1586,7 @@ static void usage(void) " -p abs-pic-file : GOT/PIC processing with files\n" " -s stacksize : set application stack size\n" " -o output-file : output file name\n\n", - program); + elf2flt_progname); fprintf(stderr, "Compiled for " ARCH " architecture\n\n"); exit(2); } @@ -1716,23 +1639,20 @@ int main(int argc, char *argv[]) void *text; void *data; uint32_t *reloc; - + struct flat_hdr hdr; - program = argv[0]; - progname = argv[0]; - xmalloc_set_program_name(program); + elf2flt_progname = argv[0]; + xmalloc_set_program_name(elf2flt_progname); if (argc < 2) usage(); - - if (sizeof(hdr) != 64) { - fprintf(stderr, + + if (sizeof(hdr) != 64) + fatal( "Potential flat header incompatibility detected\n" - "header size should be 64 but is %d\n", + "header size should be 64 but is %d", sizeof(hdr)); - exit(64); - } #ifndef TARGET_e1 stack = 4096; @@ -1789,7 +1709,7 @@ int main(int argc, char *argv[]) if (!load_to_ram && !pfile) load_to_ram = 1; - filename = fname = argv[argc-1]; + fname = argv[argc-1]; if (pfile) { pic_with_got = 1; @@ -1800,43 +1720,30 @@ int main(int argc, char *argv[]) if (! rel_file) rel_file = fname; - if (!(rel_bfd = bfd_openr(rel_file, 0))) { - fprintf(stderr, "Can't open %s\n", rel_file); - exit(1); - } + if (!(rel_bfd = bfd_openr(rel_file, 0))) + fatal_perror("Can't open '%s'", rel_file); - if (bfd_check_format (rel_bfd, bfd_object) == 0) { - fprintf(stderr, "File is not an object file\n"); - exit(2); - } + if (bfd_check_format (rel_bfd, bfd_object) == 0) + fatal("File is not an object file"); if (abs_file == rel_file) abs_bfd = rel_bfd; /* one file does all */ else { - if (!(abs_bfd = bfd_openr(abs_file, 0))) { - fprintf(stderr, "Can't open %s\n", abs_file); - exit(1); - } + if (!(abs_bfd = bfd_openr(abs_file, 0))) + fatal_perror("Can't open '%s'", abs_file); - if (bfd_check_format (abs_bfd, bfd_object) == 0) { - fprintf(stderr, "File is not an object file\n"); - exit(2); - } + if (bfd_check_format (abs_bfd, bfd_object) == 0) + fatal("File is not an object file"); } - if (! (bfd_get_file_flags(rel_bfd) & HAS_RELOC)) { - fprintf (stderr, "%s: Input file contains no relocation info\n", rel_file); - exit (2); - } + if (! (bfd_get_file_flags(rel_bfd) & HAS_RELOC)) + fatal("%s: Input file contains no relocation info", rel_file); - if (use_resolved && !(bfd_get_file_flags(abs_bfd) & EXEC_P)) { + if (use_resolved && !(bfd_get_file_flags(abs_bfd) & EXEC_P)) /* `Absolute' file is not absolute, so neither are address contained therein. */ - fprintf (stderr, - "%s: `-a' option specified with non-fully-resolved input file\n", + fatal("%s: `-a' option specified with non-fully-resolved input file", bfd_get_filename (abs_bfd)); - exit (2); - } symbol_table = get_symbols(abs_bfd, &number_of_symbols); @@ -1871,10 +1778,8 @@ int main(int argc, char *argv[]) *len = sec_vma + sec_size - *vma; } - if (text_len == 0) { - fprintf (stderr, "%s: no .text section", abs_file); - exit (2); - } + if (text_len == 0) + fatal("%s: no .text section", abs_file); text = xmalloc(text_len); @@ -1888,14 +1793,11 @@ int main(int argc, char *argv[]) text + (s->vma - text_vma), 0, bfd_section_size(abs_bfd, s))) { - fprintf(stderr, "read error section %s\n", s->name); - exit(2); + fatal("read error section %s", s->name); } - if (data_len == 0) { - fprintf (stderr, "%s: no .data section", abs_file); - exit (2); - } + if (data_len == 0) + fatal("%s: no .data section", abs_file); data = xmalloc(data_len); if (verbose) @@ -1919,8 +1821,7 @@ int main(int argc, char *argv[]) data + (s->vma - data_vma), 0, bfd_section_size(abs_bfd, s))) { - fprintf(stderr, "read error section %s\n", s->name); - exit(2); + fatal("read error section %s", s->name); } if (bss_vma == ~0) @@ -1988,18 +1889,14 @@ int main(int argc, char *argv[]) strcat(ofile, ".bflt"); } - if ((fd = open (ofile, O_WRONLY|O_BINARY|O_CREAT|O_TRUNC, 0744)) < 0) { - fprintf (stderr, "Can't open output file %s\n", ofile); - exit(4); - } + if ((fd = open (ofile, O_WRONLY|O_BINARY|O_CREAT|O_TRUNC, 0744)) < 0) + fatal_perror("Can't open output file %s", ofile); write(fd, &hdr, sizeof(hdr)); close(fd); - if (fopen_stream_u(&gf, ofile, "a" BINARY_FILE_OPTS)) { - fprintf(stderr, "Can't open file %s for writing\n", ofile); - exit(4); - } + if (fopen_stream_u(&gf, ofile, "a" BINARY_FILE_OPTS)) + fatal_perror("Can't open file %s for writing", ofile); if (docompress == 1) reopen_stream_compressed(&gf); diff --git a/flthdr.c b/flthdr.c index 90f9aa3..0485107 100644 --- a/flthdr.c +++ b/flthdr.c @@ -18,18 +18,12 @@ #include #include -/* macros for conversion between host and (internet) network byte order */ -#ifndef WIN32 -#include /* Consts and structs defined by the internet system */ -#define BINARY_FILE_OPTS -#else -#include -#define BINARY_FILE_OPTS "b" -#endif - #include "compress.h" #include +#include "stubs.h" +const char *elf2flt_progname; + /* from uClinux-x.x.x/include/linux */ #include "flat.h" /* Binary flat header description */ @@ -48,8 +42,6 @@ /****************************************************************************/ -char *program_name; - static int print = 0, print_relocs = 0, docompress = 0, ramload = 0, stacksize = 0, ktrace = 0, l1stack = 0; @@ -239,16 +231,14 @@ process_file(char *ifile, char *ofile) tfile = make_temp_file("flthdr"); if (fopen_stream_u(&ofp, tfile, "w" BINARY_FILE_OPTS)) { - fprintf(stderr, "Failed to open %s for writing\n", tfile); unlink(tfile); - exit(1); + fatal("Failed to open %s for writing\n", tfile); } /* Copy header (always uncompressed). */ if (fwrite_stream(&new_hdr, sizeof(new_hdr), 1, &ofp) != 1) { - fprintf(stderr, "Failed to write to %s\n", tfile); unlink(tfile); - exit(1); + fatal("Failed to write to %s\n", tfile); } /* Whole input file (including text) is compressed: start decompressing @@ -283,11 +273,10 @@ process_file(char *ifile, char *ofile) output_error = ferror_stream(&ofp); if (input_error || output_error) { - fprintf(stderr, "Error on file pointer%s%s\n", + unlink(tfile); + fatal("Error on file pointer%s%s\n", input_error ? " input" : "", output_error ? " output" : ""); - unlink(tfile); - exit(1); } fclose_stream(&ifp); @@ -313,7 +302,7 @@ usage(char *s) { if (s) fprintf(stderr, "%s\n", s); - fprintf(stderr, "usage: %s [options] flat-file\n", program_name); + fprintf(stderr, "usage: %s [options] flat-file\n", elf2flt_progname); fprintf(stderr, " Allows you to change an existing flat file\n\n"); fprintf(stderr, " -p : print current settings\n"); fprintf(stderr, " -P : print relocations\n"); @@ -340,7 +329,7 @@ main(int argc, char *argv[]) int c; char *ofile = NULL, *ifile; - program_name = argv[0]; + elf2flt_progname = argv[0]; while ((c = getopt(argc, argv, "pPdzZrRuUkKs:o:")) != EOF) { switch (c) { diff --git a/stubs.c b/stubs.c index 59760a3..53a93aa 100644 --- a/stubs.c +++ b/stubs.c @@ -1,13 +1,50 @@ +#include +#include +#include +#include +#include + +#include "libiberty.h" + +#include "stubs.h" + #ifndef HAVE_DCGETTEXT -const char *dcgettext (const char *domain, const char *msg, int category) +const char *dcgettext(const char *domain, const char *msg, int category) { return msg; } #endif /* !HAVE_DCGETTEXT */ #ifndef HAVE_LIBINTL_DGETTEXT -const char *libintl_dgettext (const char *domain, const char *msg) +const char *libintl_dgettext(const char *domain, const char *msg) { return msg; } #endif /* !HAVE_LIBINTL_DGETTEXT */ + +/* fatal error & exit */ +void fatal(const char *format, ...) +{ + va_list args; + + va_start(args, format); + fprintf(stderr, "%s: ", elf2flt_progname); + vfprintf(stderr, format, args); + fprintf(stderr, "\n"); + va_end(args); + exit(1); +} + +/* fatal error, perror & exit */ +void fatal_perror(const char *format, ...) +{ + int e = errno; + va_list args; + + va_start(args, format); + fprintf(stderr, "%s: ", elf2flt_progname); + vfprintf(stderr, format, args); + fprintf(stderr, ": %s\n", strerror(e)); + va_end(args); + exit(1); +} diff --git a/stubs.h b/stubs.h new file mode 100644 index 0000000..493ff73 --- /dev/null +++ b/stubs.h @@ -0,0 +1,38 @@ +/* macros for conversion between host and (internet) network byte order */ +#ifndef WIN32 +# include /* Consts and structs defined by the internet system */ +# define BINARY_FILE_OPTS +#else +# include +# define BINARY_FILE_OPTS "b" +#endif + +#ifndef __WIN32 +# include +#endif +#ifndef WIFSIGNALED +# define WIFSIGNALED(S) (((S) & 0xff) != 0 && ((S) & 0xff) != 0x7f) +#endif +#ifndef WTERMSIG +# define WTERMSIG(S) ((S) & 0x7f) +#endif +#ifndef WIFEXITED +# define WIFEXITED(S) (((S) & 0xff) == 0) +#endif +#ifndef WEXITSTATUS +# define WEXITSTATUS(S) (((S) & 0xff00) >> 8) +#endif +#ifndef WCOREDUMP +# define WCOREDUMP(S) ((S) & WCOREFLG) +#endif +#ifndef WCOREFLG +# define WCOREFLG 0200 +#endif +#ifndef HAVE_STRSIGNAL +# define strsignal(sig) "SIG???" +#endif + +extern const char *elf2flt_progname; + +void fatal(const char *, ...); +void fatal_perror(const char *, ...); -- 2.39.5