Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/elf2ecoff misc knf cleanup.



details:   https://anonhg.NetBSD.org/src/rev/978c8188b7ef
branches:  trunk
changeset: 768655:978c8188b7ef
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Aug 23 20:27:22 2011 +0000

description:
misc knf cleanup.

diffstat:

 usr.bin/elf2ecoff/elf2ecoff.c |  263 +++++++++++++++++------------------------
 1 files changed, 107 insertions(+), 156 deletions(-)

diffs (truncated from 526 to 300 lines):

diff -r 464fb9a03fd2 -r 978c8188b7ef usr.bin/elf2ecoff/elf2ecoff.c
--- a/usr.bin/elf2ecoff/elf2ecoff.c     Tue Aug 23 19:57:24 2011 +0000
+++ b/usr.bin/elf2ecoff/elf2ecoff.c     Tue Aug 23 20:27:22 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: elf2ecoff.c,v 1.27 2011/06/28 13:13:15 tsutsui Exp $   */
+/*     $NetBSD: elf2ecoff.c,v 1.28 2011/08/23 20:27:22 christos Exp $  */
 
 /*
  * Copyright (c) 1997 Jonathan Stone
@@ -74,45 +74,29 @@
        char   *stringtab;
 };
 
-int     debug = 0;
-
-int     phcmp(Elf32_Phdr * h1, Elf32_Phdr * h2);
-
-
-char   *saveRead(int file, off_t offset, off_t len, const char *name);
-void    safewrite(int outfile, const void *buf, off_t len, const char *msg);
-void    copy(int, int, off_t, off_t);
-void    combine(struct sect * base, struct sect * new, int paddable);
-void    translate_syms(struct elf_syms *, struct ecoff_syms *);
-void   elf_symbol_table_to_ecoff(int out, int in,
-           struct ecoff_exechdr * ep,
-           off_t symoff, off_t symsize,
-           off_t stroff, off_t strsize);
-
+static int     debug = 0;
+static int     needswap;
 
-int    make_ecoff_section_hdrs(struct ecoff_exechdr * ep,
-           struct ecoff_scnhdr * esecs);
-
-void   write_ecoff_symhdr(int outfile, struct ecoff_exechdr * ep,
-           struct ecoff_symhdr * symhdrp,
-           long nesyms, long extsymoff, long extstroff,
-           long strsize);
-
-void    pad16(int fd, int size, const char *msg);
-void   bswap32_region(int32_t* , int);
-
-int    *symTypeTable;
-int    needswap;
-
-
-
-
-void   elf_read_syms(struct elf_syms * elfsymsp, int infile,
-           off_t symoff, off_t symsize, off_t stroff, off_t strsize);
+static int     phcmp(Elf32_Phdr *, Elf32_Phdr *);
+static char   *saveRead(int, off_t, off_t, const char *);
+static void    safewrite(int, const void *, off_t, const char *);
+static void    copy(int, int, off_t, off_t);
+static void    combine(struct sect *, struct sect *, int);
+static void    translate_syms(struct elf_syms *, struct ecoff_syms *);
+static void    elf_symbol_table_to_ecoff(int, int, struct ecoff_exechdr *,
+    off_t, off_t, off_t, off_t);
+static int     make_ecoff_section_hdrs(struct ecoff_exechdr *,
+    struct ecoff_scnhdr *);
+static void    write_ecoff_symhdr(int, struct ecoff_exechdr *,
+    struct ecoff_symhdr *, long, long, long, long);
+static void    pad16(int, int, const char *);
+static void    bswap32_region(int32_t* , int);
+static void    elf_read_syms(struct elf_syms *, int, off_t, off_t, off_t,
+    off_t);
 
 
 int
-main(int argc, char **argv, char **envp)
+main(int argc, char **argv)
 {
        Elf32_Ehdr ex;
        Elf32_Phdr *ph;
@@ -145,7 +129,8 @@
        if (argc < 3 || argc > 4) {
 usage:
                fprintf(stderr,
-                   "usage: elf2ecoff <elf executable> <ECOFF executable> [-s]\n");
+                   "Usage: %s <elf executable> <ECOFF executable> [-s]\n",
+                   getprogname());
                exit(1);
        }
        if (argc == 4) {
@@ -154,27 +139,18 @@
                symflag = 1;
        }
        /* Try the input file... */
-       if ((infile = open(argv[1], O_RDONLY)) < 0) {
-               fprintf(stderr, "Can't open %s for read: %s\n",
-                   argv[1], strerror(errno));
-               exit(1);
-       }
+       if ((infile = open(argv[1], O_RDONLY)) < 0)
+               err(1, "Can't open %s for read", argv[1]);
        /* Read the header, which is at the beginning of the file... */
        i = read(infile, &ex, sizeof ex);
-       if (i != sizeof ex) {
-               fprintf(stderr, "ex: %s: %s.\n",
-                   argv[1], i ? strerror(errno) : "End of file reached");
-               exit(1);
-       }
+       if (i != sizeof ex)
+               err(1, "Short header read from %s", argv[1]);
        if (ex.e_ident[EI_DATA] == ELFDATA2LSB)
                mipsel = 1;
        else if (ex.e_ident[EI_DATA] == ELFDATA2MSB)
                mipsel = 0;
-       else {
-               fprintf(stderr, "invalid ELF byte order %d\n",
-                   ex.e_ident[EI_DATA]);
-               exit(1);
-       }
+       else
+               errx(1, "invalid ELF byte order %d", ex.e_ident[EI_DATA]);
 #if BYTE_ORDER == BIG_ENDIAN
        if (mipsel)
                needswap = 1;
@@ -235,11 +211,13 @@
 
        }
 
-       /* Figure out if we can cram the program header into an ECOFF
+       /*
+        * Figure out if we can cram the program header into an ECOFF
         * header...  Basically, we can't handle anything but loadable
         * segments, but we can ignore some kinds of segments.  We can't
         * handle holes in the address space.  Segments may be out of order,
-        * so we sort them first. */
+        * so we sort them first.
+        */
 
        qsort(ph, ex.e_phnum, sizeof(Elf32_Phdr),
            (int (*) (const void *, const void *)) phcmp);
@@ -251,18 +229,17 @@
                    ph[i].p_type == PT_MIPS_REGINFO) {
 
                        if (debug) {
-                               fprintf(stderr, "  skipping PH %zu type %d flags 0x%x\n",
+                               fprintf(stderr, "  skipping PH %zu type %d "
+                                   "flags 0x%x\n",
                                    i, ph[i].p_type, ph[i].p_flags);
                        }
                        continue;
                }
                /* Section types we can't handle... */
                else
-                       if (ph[i].p_type != PT_LOAD) {
-                               fprintf(stderr, "Program header %zu type %d can't be converted.\n",
-                                   i, ph[i].p_type);
-                               exit(1);
-                       }
+                       if (ph[i].p_type != PT_LOAD)
+                               errx(1, "Program header %zu type %d can't be "
+                                   "converted", i, ph[i].p_type);
                /* Writable (data) segment? */
                if (ph[i].p_flags & PF_W) {
                        struct sect ndata, nbss;
@@ -273,8 +250,10 @@
                        nbss.len = ph[i].p_memsz - ph[i].p_filesz;
 
                        if (debug) {
-                               fprintf(stderr,
-                                   "  combinining PH %zu type %d flags 0x%x with data, ndata = %ld, nbss =%ld\n", i, ph[i].p_type, ph[i].p_flags, ndata.len, nbss.len);
+                               fprintf(stderr, "  combinining PH %zu type %d "
+                                   "flags 0x%x with data, ndata = %ld, "
+                                   "nbss =%ld\n", i, ph[i].p_type,
+                                   ph[i].p_flags, ndata.len, nbss.len);
                        }
                        combine(&data, &ndata, 0);
                        combine(&bss, &nbss, 1);
@@ -284,9 +263,8 @@
                        ntxt.vaddr = ph[i].p_vaddr;
                        ntxt.len = ph[i].p_filesz;
                        if (debug) {
-
-                               fprintf(stderr,
-                                   "  combinining PH %zu type %d flags 0x%x with text, len = %ld\n",
+                               fprintf(stderr, "  combinining PH %zu type %d "
+                                   "flags 0x%x with text, len = %ld\n",
                                    i, ph[i].p_type, ph[i].p_flags, ntxt.len);
                        }
                        combine(&text, &ntxt, 0);
@@ -298,10 +276,9 @@
 
        /* Sections must be in order to be converted... */
        if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
-           text.vaddr + text.len > data.vaddr || data.vaddr + data.len > bss.vaddr) {
-               fprintf(stderr, "Sections ordering prevents a.out conversion.\n");
-               exit(1);
-       }
+           text.vaddr + text.len > data.vaddr ||
+           data.vaddr + data.len > bss.vaddr)
+               errx(1, "Sections ordering prevents a.out conversion");
        /* If there's a data section but no text section, then the loader
         * combined everything into one section.   That needs to be the text
         * section, so just make the data section zero length following text. */
@@ -385,25 +362,23 @@
        }
 
        /* Make the output file... */
-       if ((outfile = open(argv[2], O_WRONLY | O_CREAT, 0777)) < 0) {
-               fprintf(stderr, "Unable to create %s: %s\n", argv[2], strerror(errno));
-               exit(1);
-       }
+       if ((outfile = open(argv[2], O_WRONLY | O_CREAT, 0777)) < 0)
+               err(1, "Unable to create %s", argv[2]);
+
        /* Truncate file... */
        if (ftruncate(outfile, 0)) {
                warn("ftruncate %s", argv[2]);
        }
        /* Write the headers... */
-       safewrite(outfile, &ep.f, sizeof(ep.f), "ep.f: write: %s\n");
+       safewrite(outfile, &ep.f, sizeof(ep.f), "ep.f: write");
        if (debug)
                fprintf(stderr, "wrote %zu byte file header.\n", sizeof(ep.f));
 
-       safewrite(outfile, &ep.a, sizeof(ep.a), "ep.a: write: %s\n");
+       safewrite(outfile, &ep.a, sizeof(ep.a), "ep.a: write");
        if (debug)
                fprintf(stderr, "wrote %zu byte a.out header.\n", sizeof(ep.a));
 
-       safewrite(outfile, &esecs, sizeof(esecs[0]) * nsecs,
-           "esecs: write: %s\n");
+       safewrite(outfile, &esecs, sizeof(esecs[0]) * nsecs, "esecs: write");
        if (debug)
                fprintf(stderr, "wrote %zu bytes of section headers.\n",
                    sizeof(esecs[0]) * nsecs);
@@ -412,7 +387,7 @@
        pad = ((sizeof ep.f + sizeof ep.a + sizeof esecs) & 15);
        if (pad) {
                pad = 16 - pad;
-               pad16(outfile, pad, "ipad: write: %s\n");
+               pad16(outfile, pad, "ipad: write");
                if (debug)
                        fprintf(stderr, "wrote %d byte pad.\n", pad);
        }
@@ -426,27 +401,25 @@
                        if (cur_vma != ph[i].p_vaddr) {
                                unsigned long gap = ph[i].p_vaddr - cur_vma;
                                char    obuf[1024];
-                               if (gap > 65536) {
-                                       fprintf(stderr, "Intersegment gap (%ld bytes) too large.\n",
-                                           gap);
-                                       exit(1);
-                               }
+                               if (gap > 65536)
+                                       errx(1, "Intersegment gap (%ld bytes) "
+                                           "too large", gap);
                                if (debug)
-                                       fprintf(stderr, "Warning: %ld byte intersegment gap.\n", gap);
+                                       fprintf(stderr, "Warning: %ld byte "
+                                           "intersegment gap.\n", gap);
                                memset(obuf, 0, sizeof obuf);
                                while (gap) {
-                                       int     count = write(outfile, obuf, (gap > sizeof obuf
-                                               ? sizeof obuf : gap));
-                                       if (count < 0) {
-                                               fprintf(stderr, "Error writing gap: %s\n",
-                                                   strerror(errno));
-                                               exit(1);
-                                       }
+                                       int count = write(outfile, obuf,
+                                           (gap > sizeof obuf
+                                           ? sizeof obuf : gap));
+                                       if (count < 0)
+                                               err(1, "Error writing gap");
                                        gap -= count;
                                }
                        }
                        if (debug)
-                               fprintf(stderr, "writing %d bytes...\n", ph[i].p_filesz);
+                               fprintf(stderr, "writing %d bytes...\n",
+                                   ph[i].p_filesz);
                        copy(outfile, infile, ph[i].p_offset, ph[i].p_filesz);
                        cur_vma = ph[i].p_vaddr + ph[i].p_filesz;
                }
@@ -470,45 +443,38 @@
        {
                char    obuf[4096];
                memset(obuf, 0, sizeof obuf);
-               if (write(outfile, obuf, sizeof(obuf)) != sizeof(obuf)) {
-                       fprintf(stderr, "Error writing PROM padding: %s\n",
-                           strerror(errno));
-                       exit(1);
-               }
+               if (write(outfile, obuf, sizeof(obuf)) != sizeof(obuf))
+                       err(1, "Error writing PROM padding");
        }
 
        /* Looks like we won... */
-       exit(0);
+       return 0;
 }
 
-void
+static void
 copy(int out, int in, off_t offset, off_t size)
 {



Home | Main Index | Thread Index | Old Index