This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / scripts / modsign / mod-extract.c
1
2
3
4 #include <errno.h>
5 #include <error.h>
6 #include <fcntl.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <bfd.h>
11
12 /* This should be set in a Makefile somehow */
13 #define TARGET "i686-pc-linux-gnu"
14
15 static FILE *out;
16
17 static void dump_data(bfd *abfd)
18 {
19         asection *section;
20         bfd_byte *data = 0;
21         bfd_size_type size;
22         bfd_size_type addr_offset;
23         bfd_size_type stop_offset;
24         unsigned int opb = bfd_octets_per_byte(abfd);
25         unsigned int cksum;
26
27         for (section = abfd->sections; section != NULL; section = section->next) {
28                 if (section->flags & SEC_HAS_CONTENTS) {
29                         if (bfd_section_size(abfd, section) == 0)
30                                 continue;
31
32                         /* We only care about sections with "text" or "data" in their names */
33                         if ((strstr(section->name, "text") == NULL) &&
34                             (strstr(section->name, "data") == NULL))
35                                 continue;
36
37                         cksum = 0;
38
39                         size = bfd_section_size(abfd, section) / opb;
40
41                         printf("Contents of section %s size %lu", section->name, size);
42
43                         data = (bfd_byte *) malloc(size);
44
45                         bfd_get_section_contents(abfd, section, (PTR) data, 0, size);
46
47                         stop_offset = size;
48
49                         printf(" idata %02x%02x%02x%02x", data[0], data[1], data[2], data[3]);
50
51                         for (addr_offset = 0; addr_offset < stop_offset; ++addr_offset) {
52                                 cksum += (unsigned int) data[addr_offset];
53                                 fputc(data[addr_offset], out);
54                         }
55                         free (data);
56
57                         printf(" checksum %08x\n", cksum);
58                 }
59         }
60 }
61
62 void set_default_bfd_target(void)
63 {
64         const char *target = TARGET;
65
66         if (!bfd_set_default_target(target))
67                 fprintf(stderr, "can't set BFD default target to `%s': %s", target, bfd_errmsg (bfd_get_error ()));
68 }
69
70 int main (int argc, char *argv[])
71 {
72         char *in_file;
73         char *out_file;
74         bfd *file;
75         char **matching;
76
77         if (argc != 3) {
78                 fprintf(stderr, "%s [infile] [outfile]\n", argv[0]);
79                 exit(1);
80         }
81
82         in_file = argv[1];
83         out_file = argv[2];
84
85         bfd_init();
86         set_default_bfd_target();
87
88
89 //      file = bfd_openr(in_file, "elf32-i386");
90         file = bfd_openr(in_file, NULL);
91         if (file == NULL) {
92                 fprintf(stderr, "error \"%s\" trying to open %s\n", strerror(errno), in_file);
93                 exit(1);
94         }
95
96         out = fopen(out_file, "w");
97         if (out == NULL) {
98                 fprintf(stderr, "error \"%s\" trying to create %s\n", strerror(errno), out_file);
99                 exit(1);
100         }
101
102         if (bfd_check_format_matches(file, bfd_object, &matching)) {
103                 dump_data (file);
104         }
105
106         fclose(out);
107
108         return 0;
109 }