Module Name: src Committed By: christos Date: Tue Aug 23 20:27:22 UTC 2011
Modified Files: src/usr.bin/elf2ecoff: elf2ecoff.c Log Message: misc knf cleanup. To generate a diff of this commit: cvs rdiff -u -r1.27 -r1.28 src/usr.bin/elf2ecoff/elf2ecoff.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/elf2ecoff/elf2ecoff.c diff -u src/usr.bin/elf2ecoff/elf2ecoff.c:1.27 src/usr.bin/elf2ecoff/elf2ecoff.c:1.28 --- src/usr.bin/elf2ecoff/elf2ecoff.c:1.27 Tue Jun 28 09:13:15 2011 +++ src/usr.bin/elf2ecoff/elf2ecoff.c Tue Aug 23 16:27:22 2011 @@ -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; +static int debug = 0; +static int needswap; -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); - - -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) { char ibuf[4096]; size_t remaining, cur, count; /* Go to the start of the ELF symbol table... */ - if (lseek(in, offset, SEEK_SET) < 0) { - perror("copy: lseek"); - exit(1); - } + if (lseek(in, offset, SEEK_SET) < 0) + err(1, "copy: lseek"); remaining = size; while (remaining) { cur = remaining; if (cur > sizeof ibuf) cur = sizeof ibuf; remaining -= cur; - if ((count = read(in, ibuf, cur)) != cur) { - fprintf(stderr, "copy: read: %s\n", - count ? strerror(errno) : "premature end of file"); - exit(1); - } - safewrite(out, ibuf, cur, "copy: write: %s\n"); + if ((count = read(in, ibuf, cur)) != cur) + err(1, "copy: short read"); + safewrite(out, ibuf, cur, "copy: write"); } } + /* Combine two segments, which must be contiguous. If pad is true, it's okay for there to be padding between. */ -void +static void combine(struct sect *base, struct sect *new, int pad) { @@ -519,17 +485,15 @@ if (base->vaddr + base->len != new->vaddr) { if (pad) base->len = new->vaddr - base->vaddr; - else { - fprintf(stderr, - "Non-contiguous data can't be converted.\n"); - exit(1); - } + else + errx(1, "Non-contiguous data can't be " + "converted"); } base->len += new->len; } } -int +static int phcmp(Elf32_Phdr *h1, Elf32_Phdr *h2) { @@ -542,40 +506,31 @@ return 0; } -char * +static char * saveRead(int file, off_t offset, off_t len, const char *name) { char *tmp; int count; off_t off; - if ((off = lseek(file, offset, SEEK_SET)) < 0) { - fprintf(stderr, "%s: fseek: %s\n", name, strerror(errno)); - exit(1); - } - if ((tmp = malloc(len)) == NULL) { - fprintf(stderr, "%s: Can't allocate %ld bytes.\n", name, (long) len); - exit(1); - } + if ((off = lseek(file, offset, SEEK_SET)) < 0) + err(1, "%s: fseek", name); + if ((tmp = malloc(len)) == NULL) + err(1, "%s: Can't allocate %ld bytes", name, (long) len); count = read(file, tmp, len); - if (count != len) { - fprintf(stderr, "%s: read: %s.\n", - name, count ? strerror(errno) : "End of file reached"); - exit(1); - } + if (count != len) + err(1, "%s: short read", name); return tmp; } -void +static void safewrite(int outfile, const void *buf, off_t len, const char *msg) { - int written; + ssize_t written; written = write(outfile, buf, len); - if (written != len) { - fprintf(stderr, msg, strerror(errno)); - exit(1); - } + if (written != len) + err(1, "%s", msg); } @@ -583,7 +538,7 @@ * Output only three ECOFF sections, corresponding to ELF psecs * for text, data, and bss. */ -int +static int make_ecoff_section_hdrs(struct ecoff_exechdr *ep, struct ecoff_scnhdr *esecs) { @@ -631,14 +586,15 @@ * Guess at how big the symbol table will be. * Mark all symbols as EXTERN (for now). */ -void +static void write_ecoff_symhdr(int out, struct ecoff_exechdr *ep, struct ecoff_symhdr *symhdrp, long nesyms, long extsymoff, long extstroff, long strsize) { if (debug) - fprintf(stderr, "writing symhdr for %ld entries at offset 0x%lx\n", + fprintf(stderr, + "writing symhdr for %ld entries at offset 0x%lx\n", nesyms, (u_long) ep->f.f_symptr); ep->f.f_nsyms = sizeof(struct ecoff_symhdr); @@ -665,11 +621,11 @@ } safewrite(out, symhdrp, sizeof(*symhdrp), - "writing symbol header: %s\n"); + "writing symbol header"); } -void +static void elf_read_syms(struct elf_syms *elfsymsp, int in, off_t symoff, off_t symsize, off_t stroff, off_t strsize) { @@ -699,10 +655,7 @@ } -/* - * - */ -void +static 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) { @@ -743,9 +696,9 @@ /* Write out the string table... */ padding = ecoff_strsize - ecoffsymtab.stringsize; safewrite(out, ecoffsymtab.stringtab, ecoffsymtab.stringsize, - "string table: write: %s\n"); + "string table: write"); if (padding) - pad16(out, padding, "string table: padding: %s\n"); + pad16(out, padding, "string table: padding"); /* Write out the symbol table... */ @@ -760,9 +713,9 @@ } safewrite(out, ecoffsymtab.ecoff_syms, nsyms * sizeof(struct ecoff_extsym), - "symbol table: write: %s\n"); + "symbol table: write"); if (padding) - pad16(out, padding, "symbols: padding: %s\n"); + pad16(out, padding, "symbols: padding"); } @@ -770,7 +723,7 @@ /* * In-memory translation of ELF symbosl to ECOFF. */ -void +static void translate_syms(struct elf_syms *elfp, struct ecoff_syms *ecoffp) { @@ -795,10 +748,8 @@ newstrings = (char *) ecoffp->stringtab; nsp = (char *) ecoffp->stringtab; - if (newstrings == NULL) { - fprintf(stderr, "No memory for new string table!\n"); - exit(1); - } + if (newstrings == NULL) + errx(1, "No memory for new string table"); /* Copy and translate symbols... */ idx = 0; for (i = 0; i < nsyms; i++) { @@ -833,7 +784,7 @@ /* * pad to a 16-byte boundary */ -void +static void pad16(int fd, int size, const char *msg) { @@ -841,7 +792,7 @@ } /* swap a 32bit region */ -void +static void bswap32_region(int32_t* p, int len) { size_t i;