]> git.ozlabs.org Git - yaboot.git/blob - util/elfextract.c
Commit yaboot 1.3.0
[yaboot.git] / util / elfextract.c
1 /*
2  * Extract the loadable program segment from an elf file.
3  *
4  * Copyright 1996 Paul Mackerras.
5  */
6 #include <stdio.h>
7 #include <linux/elf.h>
8
9 FILE *fi, *fo;
10 char *ni, *no;
11 char buf[65536];
12
13 void
14 rd(void *buf, int len)
15 {
16     int nr;
17
18     nr = fread(buf, 1, len, fi);
19     if (nr == len)
20         return;
21     if (ferror(fi))
22         fprintf(stderr, "%s: read error\n", ni);
23     else
24         fprintf(stderr, "%s: short file\n", ni);
25     exit(1);
26 }
27
28 int
29 main(int ac, char **av)
30 {
31     unsigned nb, len, i;
32     Elf32_Ehdr eh;
33     Elf32_Phdr ph;
34     unsigned long phoffset, phsize, prevaddr;
35
36     if (ac > 3 || (ac > 1 && av[1][0] == '-')) {
37         fprintf(stderr, "Usage: %s [elf-file [image-file]]\n", av[0]);
38         exit(0);
39     }
40
41     fi = stdin;
42     ni = "(stdin)";
43     fo = stdout;
44     no = "(stdout)";
45
46     if (ac > 1) {
47         ni = av[1];
48         fi = fopen(ni, "rb");
49         if (fi == NULL) {
50             perror(ni);
51             exit(1);
52         }
53     }
54
55     rd(&eh, sizeof(eh));
56     if (eh.e_ident[EI_MAG0] != ELFMAG0
57         || eh.e_ident[EI_MAG1] != ELFMAG1
58         || eh.e_ident[EI_MAG2] != ELFMAG2
59         || eh.e_ident[EI_MAG3] != ELFMAG3) {
60         fprintf(stderr, "%s: not an ELF file\n", ni);
61         exit(1);
62     }
63
64     fseek(fi, eh.e_phoff, 0);
65     phsize = 0;
66     for (i = 0; i < eh.e_phnum; ++i) {
67         rd(&ph, sizeof(ph));
68         if (ph.p_type != PT_LOAD)
69             continue;
70         if (phsize == 0 || prevaddr == 0) {
71             phoffset = ph.p_offset;
72             phsize = ph.p_filesz;
73         } else
74             phsize = ph.p_offset + ph.p_filesz - phoffset;
75         prevaddr = ph.p_vaddr;
76     }
77     if (phsize == 0) {
78         fprintf(stderr, "%s: doesn't have a loadable segment\n", ni);
79         exit(1);
80     }
81
82     if (ac > 2) {
83         no = av[2];
84         fo = fopen(no, "wb");
85         if (fo == NULL) {
86             perror(no);
87             exit(1);
88         }
89     }
90
91     fseek(fi, phoffset, 0);
92     for (len = phsize; len != 0; len -= nb) {
93         nb = len;
94         if (nb > sizeof(buf))
95             nb = sizeof(buf);
96         rd(buf, nb);
97         if (fwrite(buf, 1, nb, fo) != nb) {
98             fprintf(stderr, "%s: write error\n", no);
99             exit(1);
100         }
101     }
102
103     fclose(fo);
104     fclose(fi);
105     exit(0);
106 }