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
|
#include <stdio.h>
#include "x86_aout.h"
#ifndef __OUT_OK
main()
{
fprintf(stderr, "Compile error: struct exec invalid\n");
exit(1);
}
#else
FILE * ifd;
struct exec header;
main(argc, argv)
int argc;
char ** argv;
{
FILE * ofd;
if( argc != 5 ) fatal("Usage: objchop a.out text.bin data.bin sizes.asm");
ifd = fopen(argv[1], "r");
if( ifd == 0 ) fatal("Cannot open input file");
if( fread(&header, A_MINHDR, 1, ifd) != 1 )
fatal("Incomplete executable header");
if( BADMAG(header) )
fatal("Input file has bad magic number");
if( fseek(ifd, A_TEXTPOS(header), 0) < 0 )
fatal("Cannot seek to start of text");
write_file(argv[2], header.a_text);
if( fseek(ifd, A_DATAPOS(header), 0) < 0 )
fatal("Cannot seek to start of data");
write_file(argv[3], header.a_data);
ofd = fopen(argv[4], "w");
if( ofd == 0 ) fatal("Cannot open output file");
fprintf(ofd, "TEXT_SIZE=%ld\nDATA_SIZE=%ld\nBSS_SIZE=%ld\nALLOC_SIZE=%ld\n",
header.a_text, header.a_data, header.a_bss, header.a_total);
fclose(ofd);
exit(0);
}
write_file(fname, bsize)
char * fname;
long bsize;
{
char buffer[1024];
int ssize;
FILE * ofd;
ofd = fopen(fname, "w");
if( ofd == 0 ) fatal("Cannot open output file");
while(bsize>0)
{
if( bsize > sizeof(buffer) ) ssize = sizeof(buffer);
else ssize = bsize;
if( (ssize=fread(buffer, 1, ssize, ifd)) <= 0 )
fatal("Error reading segment from executable");
if( fwrite(buffer, 1, ssize, ofd) != ssize )
fatal("Error writing output file");
bsize -= ssize;
}
fclose(ofd);
}
fatal(str)
char * str;
{
fprintf(stderr, "objchop: %s\n", str);
exit(2);
}
#endif
|