linux/arch/alpha/boot/tools/objstrip.c
<<
>>
Prefs
   1/*
   2 * arch/alpha/boot/tools/objstrip.c
   3 *
   4 * Strip the object file headers/trailers from an executable (ELF or ECOFF).
   5 *
   6 * Copyright (C) 1996 David Mosberger-Tang.
   7 */
   8/*
   9 * Converts an ECOFF or ELF object file into a bootable file.  The
  10 * object file must be a OMAGIC file (i.e., data and bss follow immediately
  11 * behind the text).  See DEC "Assembly Language Programmer's Guide"
  12 * documentation for details.  The SRM boot process is documented in
  13 * the Alpha AXP Architecture Reference Manual, Second Edition by
  14 * Richard L. Sites and Richard T. Witek.
  15 */
  16#include <stdio.h>
  17#include <string.h>
  18#include <stdlib.h>
  19#include <unistd.h>
  20
  21#include <sys/fcntl.h>
  22#include <sys/stat.h>
  23#include <sys/types.h>
  24
  25#include <linux/a.out.h>
  26#include <linux/coff.h>
  27#include <linux/param.h>
  28#ifdef __ELF__
  29# include <linux/elf.h>
  30# define elfhdr elf64_hdr
  31# define elf_phdr elf64_phdr
  32# define elf_check_arch(x) ((x)->e_machine == EM_ALPHA)
  33#endif
  34
  35/* bootfile size must be multiple of BLOCK_SIZE: */
  36#define BLOCK_SIZE      512
  37
  38const char * prog_name;
  39
  40
  41static void
  42usage (void)
  43{
  44    fprintf(stderr,
  45            "usage: %s [-v] -p file primary\n"
  46            "       %s [-vb] file [secondary]\n", prog_name, prog_name);
  47    exit(1);
  48}
  49
  50
  51int
  52main (int argc, char *argv[])
  53{
  54    size_t nwritten, tocopy, n, mem_size, fil_size, pad = 0;
  55    int fd, ofd, i, j, verbose = 0, primary = 0;
  56    char buf[8192], *inname;
  57    struct exec * aout;         /* includes file & aout header */
  58    long offset;
  59#ifdef __ELF__
  60    struct elfhdr *elf;
  61    struct elf_phdr *elf_phdr;  /* program header */
  62    unsigned long long e_entry;
  63#endif
  64
  65    prog_name = argv[0];
  66
  67    for (i = 1; i < argc && argv[i][0] == '-'; ++i) {
  68        for (j = 1; argv[i][j]; ++j) {
  69            switch (argv[i][j]) {
  70              case 'v':
  71                  verbose = ~verbose;
  72                  break;
  73
  74              case 'b':
  75                  pad = BLOCK_SIZE;
  76                  break;
  77
  78              case 'p':
  79                  primary = 1;          /* make primary bootblock */
  80                  break;
  81            }
  82        }
  83    }
  84
  85    if (i >= argc) {
  86        usage();
  87    }
  88    inname = argv[i++];
  89
  90    fd = open(inname, O_RDONLY);
  91    if (fd == -1) {
  92        perror("open");
  93        exit(1);
  94    }
  95
  96    ofd = 1;
  97    if (i < argc) {
  98        ofd = open(argv[i++], O_WRONLY | O_CREAT | O_TRUNC, 0666);
  99        if (ofd == -1) {
 100            perror("open");
 101            exit(1);
 102        }
 103    }
 104
 105    if (primary) {
 106        /* generate bootblock for primary loader */
 107        
 108        unsigned long bb[64], sum = 0;
 109        struct stat st;
 110        off_t size;
 111        int i;
 112
 113        if (ofd == 1) {
 114            usage();
 115        }
 116
 117        if (fstat(fd, &st) == -1) {
 118            perror("fstat");
 119            exit(1);
 120        }
 121
 122        size = (st.st_size + BLOCK_SIZE - 1) & ~(BLOCK_SIZE - 1);
 123        memset(bb, 0, sizeof(bb));
 124        strcpy((char *) bb, "Linux SRM bootblock");
 125        bb[60] = size / BLOCK_SIZE;     /* count */
 126        bb[61] = 1;                     /* starting sector # */
 127        bb[62] = 0;                     /* flags---must be 0 */
 128        for (i = 0; i < 63; ++i) {
 129            sum += bb[i];
 130        }
 131        bb[63] = sum;
 132        if (write(ofd, bb, sizeof(bb)) != sizeof(bb)) {
 133            perror("boot-block write");
 134            exit(1);
 135        }
 136        printf("%lu\n", size);
 137        return 0;
 138    }
 139
 140    /* read and inspect exec header: */
 141
 142    if (read(fd, buf, sizeof(buf)) < 0) {
 143        perror("read");
 144        exit(1);
 145    }
 146
 147#ifdef __ELF__
 148    elf = (struct elfhdr *) buf;
 149
 150    if (elf->e_ident[0] == 0x7f && strncmp((char *)elf->e_ident + 1, "ELF", 3) == 0) {
 151        if (elf->e_type != ET_EXEC) {
 152            fprintf(stderr, "%s: %s is not an ELF executable\n",
 153                    prog_name, inname);
 154            exit(1);
 155        }
 156        if (!elf_check_arch(elf)) {
 157            fprintf(stderr, "%s: is not for this processor (e_machine=%d)\n",
 158                    prog_name, elf->e_machine);
 159            exit(1);
 160        }
 161        if (elf->e_phnum != 1) {
 162            fprintf(stderr,
 163                    "%s: %d program headers (forgot to link with -N?)\n",
 164                    prog_name, elf->e_phnum);
 165        }
 166
 167        e_entry = elf->e_entry;
 168
 169        lseek(fd, elf->e_phoff, SEEK_SET);
 170        if (read(fd, buf, sizeof(*elf_phdr)) != sizeof(*elf_phdr)) {
 171            perror("read");
 172            exit(1);
 173        }
 174
 175        elf_phdr = (struct elf_phdr *) buf;
 176        offset   = elf_phdr->p_offset;
 177        mem_size = elf_phdr->p_memsz;
 178        fil_size = elf_phdr->p_filesz;
 179
 180        /* work around ELF bug: */
 181        if (elf_phdr->p_vaddr < e_entry) {
 182            unsigned long delta = e_entry - elf_phdr->p_vaddr;
 183            offset   += delta;
 184            mem_size -= delta;
 185            fil_size -= delta;
 186            elf_phdr->p_vaddr += delta;
 187        }
 188
 189        if (verbose) {
 190            fprintf(stderr, "%s: extracting %#016lx-%#016lx (at %lx)\n",
 191                    prog_name, (long) elf_phdr->p_vaddr,
 192                    elf_phdr->p_vaddr + fil_size, offset);
 193        }
 194    } else
 195#endif
 196    {
 197        aout = (struct exec *) buf;
 198
 199        if (!(aout->fh.f_flags & COFF_F_EXEC)) {
 200            fprintf(stderr, "%s: %s is not in executable format\n",
 201                    prog_name, inname);
 202            exit(1);
 203        }
 204
 205        if (aout->fh.f_opthdr != sizeof(aout->ah)) {
 206            fprintf(stderr, "%s: %s has unexpected optional header size\n",
 207                    prog_name, inname);
 208            exit(1);
 209        }
 210
 211        if (N_MAGIC(*aout) != OMAGIC) {
 212            fprintf(stderr, "%s: %s is not an OMAGIC file\n",
 213                    prog_name, inname);
 214            exit(1);
 215        }
 216        offset = N_TXTOFF(*aout);
 217        fil_size = aout->ah.tsize + aout->ah.dsize;
 218        mem_size = fil_size + aout->ah.bsize;
 219
 220        if (verbose) {
 221            fprintf(stderr, "%s: extracting %#016lx-%#016lx (at %lx)\n",
 222                    prog_name, aout->ah.text_start,
 223                    aout->ah.text_start + fil_size, offset);
 224        }
 225    }
 226
 227    if (lseek(fd, offset, SEEK_SET) != offset) {
 228        perror("lseek");
 229        exit(1);
 230    }
 231
 232    if (verbose) {
 233        fprintf(stderr, "%s: copying %lu byte from %s\n",
 234                prog_name, (unsigned long) fil_size, inname);
 235    }
 236
 237    tocopy = fil_size;
 238    while (tocopy > 0) {
 239        n = tocopy;
 240        if (n > sizeof(buf)) {
 241            n = sizeof(buf);
 242        }
 243        tocopy -= n;
 244        if ((size_t) read(fd, buf, n) != n) {
 245            perror("read");
 246            exit(1);
 247        }
 248        do {
 249            nwritten = write(ofd, buf, n);
 250            if ((ssize_t) nwritten == -1) {
 251                perror("write");
 252                exit(1);
 253            }
 254            n -= nwritten;
 255        } while (n > 0);
 256    }
 257
 258    if (pad) {
 259        mem_size = ((mem_size + pad - 1) / pad) * pad;
 260    }
 261
 262    tocopy = mem_size - fil_size;
 263    if (tocopy > 0) {
 264        fprintf(stderr,
 265                "%s: zero-filling bss and aligning to %lu with %lu bytes\n",
 266                prog_name, pad, (unsigned long) tocopy);
 267
 268        memset(buf, 0x00, sizeof(buf));
 269        do {
 270            n = tocopy;
 271            if (n > sizeof(buf)) {
 272                n = sizeof(buf);
 273            }
 274            nwritten = write(ofd, buf, n);
 275            if ((ssize_t) nwritten == -1) {
 276                perror("write");
 277                exit(1);
 278            }
 279            tocopy -= nwritten;
 280        } while (tocopy > 0);
 281    }
 282    return 0;
 283}
 284