-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathkpatch_make.c
123 lines (102 loc) · 2.82 KB
/
kpatch_make.c
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
122
123
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdarg.h>
#include <errno.h>
#include <time.h>
#include "kpatch_file.h"
#define ALIGN(x, align) ((x + align - 1) & (~(align - 1)))
static int verbose;
static void xerror(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
vfprintf(stderr, fmt, va);
va_end(va);
exit(1);
}
int make_file(int fdo, void *buf1, off_t size, const char *buildid, int coroutine_env_offset)
{
int res;
struct kpatch_file khdr;
memset(&khdr, 0, sizeof(khdr));
memcpy(khdr.magic, KPATCH_FILE_MAGIC1, sizeof(khdr.magic));
strncpy(khdr.uname, buildid, sizeof(khdr.uname));
khdr.build_time = (uint64_t)time(NULL);
khdr.csum = 0; /* FIXME */
khdr.nr_reloc = 0;
khdr.rel_offset = sizeof(khdr);
khdr.kpatch_offset = khdr.rel_offset;
size = ALIGN(size, 16);
khdr.total_size = khdr.kpatch_offset + size;
khdr.coroutine_env_offset = coroutine_env_offset;
res = write(fdo, &khdr, sizeof(khdr));
res += write(fdo, buf1, size);
if (res != sizeof(khdr) + size)
xerror("write error");
return 0;
}
static void usage(void)
{
printf("Usage: kpatch_make [-d] -n <modulename> [-v <version>] -e <entryaddr> [-o <output>] <input1> [input2]\n");
printf(" -b buildid = target buildid for patch\n");
printf(" -c offset = env offset in coroutine\n");
printf(" -d debug (verbose)\n");
printf("\n");
printf(" result is printed to output and is the following:\n");
printf(" header - struct kpatch_file\n");
printf(" .kpatch.* - sections with binary patch text/data and info\n");
exit(EXIT_FAILURE);
}
int main(int argc, char **argv)
{
int opt;
int fd1, fdo;
void *buf;
struct stat st;
char *buildid = NULL, *outputname = NULL;
int coroutine_env_offset = -1;
while ((opt = getopt(argc, argv, "db:o:v:s:c:")) != -1) {
switch (opt) {
case 'd':
verbose = 1;
break;
case 'b':
buildid = strdup(optarg);
break;
case 'o':
outputname = strdup(optarg);
break;
case 'c':
if (sscanf(optarg, "%d", &coroutine_env_offset) != 1) {
printf("Error: can't parse jmpbuf offset for coroutines.\n");
usage();
}
break;
default: /* '?' */
usage();
}
}
if (buildid == NULL)
usage();
fd1 = open(argv[optind], O_RDONLY);
if (fd1 == -1)
xerror("Can't open 1st input file '%s'", argv[optind]);
if (fstat(fd1, &st) == -1)
xerror("Can't stat file1");
buf = mmap(NULL, st.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd1, 0);
if (buf == MAP_FAILED)
xerror("mmap error %d", errno);
close(fd1);
fdo = 1;
if (outputname) {
fdo = open(outputname, O_CREAT | O_TRUNC | O_WRONLY, 0660);
if (fdo == -1)
xerror("Can't open output file '%s'", outputname);
}
return make_file(fdo, buf, st.st_size, buildid, coroutine_env_offset);
}