File: elfextract.c

package info (click to toggle)
yaboot 1.3.6-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,192 kB
  • ctags: 2,696
  • sloc: ansic: 9,436; sh: 2,578; asm: 343; makefile: 231
file content (121 lines) | stat: -rw-r--r-- 2,717 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 *  elfextract.c - Extract the loadable program segment from an elf file.
 *
 *  Copyright 1996 Paul Mackerras.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <stdio.h>
#include <linux/elf.h>

FILE *fi, *fo;
char *ni, *no;
char buf[65536];

void
rd(void *buf, int len)
{
    int nr;

    nr = fread(buf, 1, len, fi);
    if (nr == len)
	return;
    if (ferror(fi))
	fprintf(stderr, "%s: read error\n", ni);
    else
	fprintf(stderr, "%s: short file\n", ni);
    exit(1);
}

int
main(int ac, char **av)
{
    unsigned nb, len, i;
    Elf32_Ehdr eh;
    Elf32_Phdr ph;
    unsigned long phoffset, phsize, prevaddr;

    if (ac > 3 || (ac > 1 && av[1][0] == '-')) {
	fprintf(stderr, "Usage: %s [elf-file [image-file]]\n", av[0]);
	exit(0);
    }

    fi = stdin;
    ni = "(stdin)";
    fo = stdout;
    no = "(stdout)";

    if (ac > 1) {
	ni = av[1];
	fi = fopen(ni, "rb");
	if (fi == NULL) {
	    perror(ni);
	    exit(1);
	}
    }

    rd(&eh, sizeof(eh));
    if (eh.e_ident[EI_MAG0] != ELFMAG0
	|| eh.e_ident[EI_MAG1] != ELFMAG1
	|| eh.e_ident[EI_MAG2] != ELFMAG2
	|| eh.e_ident[EI_MAG3] != ELFMAG3) {
	fprintf(stderr, "%s: not an ELF file\n", ni);
	exit(1);
    }

    fseek(fi, eh.e_phoff, 0);
    phsize = 0;
    for (i = 0; i < eh.e_phnum; ++i) {
	rd(&ph, sizeof(ph));
	if (ph.p_type != PT_LOAD)
	    continue;
	if (phsize == 0 || prevaddr == 0) {
	    phoffset = ph.p_offset;
	    phsize = ph.p_filesz;
	} else
	    phsize = ph.p_offset + ph.p_filesz - phoffset;
	prevaddr = ph.p_vaddr;
    }
    if (phsize == 0) {
	fprintf(stderr, "%s: doesn't have a loadable segment\n", ni);
	exit(1);
    }

    if (ac > 2) {
	no = av[2];
	fo = fopen(no, "wb");
	if (fo == NULL) {
	    perror(no);
	    exit(1);
	}
    }

    fseek(fi, phoffset, 0);
    for (len = phsize; len != 0; len -= nb) {
	nb = len;
	if (nb > sizeof(buf))
	    nb = sizeof(buf);
	rd(buf, nb);
	if (fwrite(buf, 1, nb, fo) != nb) {
	    fprintf(stderr, "%s: write error\n", no);
	    exit(1);
	}
    }

    fclose(fo);
    fclose(fi);
    exit(0);
}