Path: blob/master/arch/alpha/boot/tools/objstrip.c
17263 views
/*1* arch/alpha/boot/tools/objstrip.c2*3* Strip the object file headers/trailers from an executable (ELF or ECOFF).4*5* Copyright (C) 1996 David Mosberger-Tang.6*/7/*8* Converts an ECOFF or ELF object file into a bootable file. The9* object file must be a OMAGIC file (i.e., data and bss follow immediately10* behind the text). See DEC "Assembly Language Programmer's Guide"11* documentation for details. The SRM boot process is documented in12* the Alpha AXP Architecture Reference Manual, Second Edition by13* Richard L. Sites and Richard T. Witek.14*/15#include <stdio.h>16#include <string.h>17#include <stdlib.h>18#include <unistd.h>1920#include <sys/fcntl.h>21#include <sys/stat.h>22#include <sys/types.h>2324#include <linux/a.out.h>25#include <linux/coff.h>26#include <linux/param.h>27#ifdef __ELF__28# include <linux/elf.h>29#endif3031/* bootfile size must be multiple of BLOCK_SIZE: */32#define BLOCK_SIZE 5123334const char * prog_name;353637static void38usage (void)39{40fprintf(stderr,41"usage: %s [-v] -p file primary\n"42" %s [-vb] file [secondary]\n", prog_name, prog_name);43exit(1);44}454647int48main (int argc, char *argv[])49{50size_t nwritten, tocopy, n, mem_size, fil_size, pad = 0;51int fd, ofd, i, j, verbose = 0, primary = 0;52char buf[8192], *inname;53struct exec * aout; /* includes file & aout header */54long offset;55#ifdef __ELF__56struct elfhdr *elf;57struct elf_phdr *elf_phdr; /* program header */58unsigned long long e_entry;59#endif6061prog_name = argv[0];6263for (i = 1; i < argc && argv[i][0] == '-'; ++i) {64for (j = 1; argv[i][j]; ++j) {65switch (argv[i][j]) {66case 'v':67verbose = ~verbose;68break;6970case 'b':71pad = BLOCK_SIZE;72break;7374case 'p':75primary = 1; /* make primary bootblock */76break;77}78}79}8081if (i >= argc) {82usage();83}84inname = argv[i++];8586fd = open(inname, O_RDONLY);87if (fd == -1) {88perror("open");89exit(1);90}9192ofd = 1;93if (i < argc) {94ofd = open(argv[i++], O_WRONLY | O_CREAT | O_TRUNC, 0666);95if (ofd == -1) {96perror("open");97exit(1);98}99}100101if (primary) {102/* generate bootblock for primary loader */103104unsigned long bb[64], sum = 0;105struct stat st;106off_t size;107int i;108109if (ofd == 1) {110usage();111}112113if (fstat(fd, &st) == -1) {114perror("fstat");115exit(1);116}117118size = (st.st_size + BLOCK_SIZE - 1) & ~(BLOCK_SIZE - 1);119memset(bb, 0, sizeof(bb));120strcpy((char *) bb, "Linux SRM bootblock");121bb[60] = size / BLOCK_SIZE; /* count */122bb[61] = 1; /* starting sector # */123bb[62] = 0; /* flags---must be 0 */124for (i = 0; i < 63; ++i) {125sum += bb[i];126}127bb[63] = sum;128if (write(ofd, bb, sizeof(bb)) != sizeof(bb)) {129perror("boot-block write");130exit(1);131}132printf("%lu\n", size);133return 0;134}135136/* read and inspect exec header: */137138if (read(fd, buf, sizeof(buf)) < 0) {139perror("read");140exit(1);141}142143#ifdef __ELF__144elf = (struct elfhdr *) buf;145146if (elf->e_ident[0] == 0x7f && strncmp((char *)elf->e_ident + 1, "ELF", 3) == 0) {147if (elf->e_type != ET_EXEC) {148fprintf(stderr, "%s: %s is not an ELF executable\n",149prog_name, inname);150exit(1);151}152if (!elf_check_arch(elf)) {153fprintf(stderr, "%s: is not for this processor (e_machine=%d)\n",154prog_name, elf->e_machine);155exit(1);156}157if (elf->e_phnum != 1) {158fprintf(stderr,159"%s: %d program headers (forgot to link with -N?)\n",160prog_name, elf->e_phnum);161}162163e_entry = elf->e_entry;164165lseek(fd, elf->e_phoff, SEEK_SET);166if (read(fd, buf, sizeof(*elf_phdr)) != sizeof(*elf_phdr)) {167perror("read");168exit(1);169}170171elf_phdr = (struct elf_phdr *) buf;172offset = elf_phdr->p_offset;173mem_size = elf_phdr->p_memsz;174fil_size = elf_phdr->p_filesz;175176/* work around ELF bug: */177if (elf_phdr->p_vaddr < e_entry) {178unsigned long delta = e_entry - elf_phdr->p_vaddr;179offset += delta;180mem_size -= delta;181fil_size -= delta;182elf_phdr->p_vaddr += delta;183}184185if (verbose) {186fprintf(stderr, "%s: extracting %#016lx-%#016lx (at %lx)\n",187prog_name, (long) elf_phdr->p_vaddr,188elf_phdr->p_vaddr + fil_size, offset);189}190} else191#endif192{193aout = (struct exec *) buf;194195if (!(aout->fh.f_flags & COFF_F_EXEC)) {196fprintf(stderr, "%s: %s is not in executable format\n",197prog_name, inname);198exit(1);199}200201if (aout->fh.f_opthdr != sizeof(aout->ah)) {202fprintf(stderr, "%s: %s has unexpected optional header size\n",203prog_name, inname);204exit(1);205}206207if (N_MAGIC(*aout) != OMAGIC) {208fprintf(stderr, "%s: %s is not an OMAGIC file\n",209prog_name, inname);210exit(1);211}212offset = N_TXTOFF(*aout);213fil_size = aout->ah.tsize + aout->ah.dsize;214mem_size = fil_size + aout->ah.bsize;215216if (verbose) {217fprintf(stderr, "%s: extracting %#016lx-%#016lx (at %lx)\n",218prog_name, aout->ah.text_start,219aout->ah.text_start + fil_size, offset);220}221}222223if (lseek(fd, offset, SEEK_SET) != offset) {224perror("lseek");225exit(1);226}227228if (verbose) {229fprintf(stderr, "%s: copying %lu byte from %s\n",230prog_name, (unsigned long) fil_size, inname);231}232233tocopy = fil_size;234while (tocopy > 0) {235n = tocopy;236if (n > sizeof(buf)) {237n = sizeof(buf);238}239tocopy -= n;240if ((size_t) read(fd, buf, n) != n) {241perror("read");242exit(1);243}244do {245nwritten = write(ofd, buf, n);246if ((ssize_t) nwritten == -1) {247perror("write");248exit(1);249}250n -= nwritten;251} while (n > 0);252}253254if (pad) {255mem_size = ((mem_size + pad - 1) / pad) * pad;256}257258tocopy = mem_size - fil_size;259if (tocopy > 0) {260fprintf(stderr,261"%s: zero-filling bss and aligning to %lu with %lu bytes\n",262prog_name, pad, (unsigned long) tocopy);263264memset(buf, 0x00, sizeof(buf));265do {266n = tocopy;267if (n > sizeof(buf)) {268n = sizeof(buf);269}270nwritten = write(ofd, buf, n);271if ((ssize_t) nwritten == -1) {272perror("write");273exit(1);274}275tocopy -= nwritten;276} while (tocopy > 0);277}278return 0;279}280281282