forked from rosslagerwall/livepatch-build-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prelink.c
195 lines (165 loc) · 4.7 KB
/
prelink.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright (C) 2015 Ross Lagerwall <[email protected]>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
/*
* This tool takes a generated patch and a xen-syms file and fills in the
* undefined symbols (i.e. it does static partial linking).
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <argp.h>
#include <error.h>
#include <unistd.h>
#include <gelf.h>
#include "list.h"
#include "lookup.h"
#include "asm/insn.h"
#include "common.h"
char *childobj;
enum loglevel loglevel = NORMAL;
/* Resolve symbols using xen-syms */
void livepatch_resolve_symbols(struct kpatch_elf *kelf,
struct lookup_table *table)
{
struct symbol *sym;
struct lookup_result result;
char *curfile = NULL;
list_for_each_entry(sym, &kelf->symbols, list) {
/* ignore NULL symbol */
if (!strlen(sym->name))
continue;
if (sym->type == STT_FILE) {
curfile = sym->name;
log_debug("Local file is %s\n", curfile);
}
if (sym->sec)
continue;
if (sym->sym.st_shndx != SHN_UNDEF)
continue;
if (sym->bind == STB_LOCAL) {
/*
* Symbols are mangled to match Xen's runtime symbol
* mangling. However, the ELF symbol table in xen-syms
* is normal, so we need to unmangle the local symbols.
*/
char *s = strchr(sym->name, '#');
if (s)
s++;
else
s = sym->name;
if (lookup_local_symbol(table, s, curfile, &result))
ERROR("lookup_local_symbol %s (%s)",
s, curfile);
} else {
if (lookup_global_symbol(table, sym->name,
&result))
ERROR("lookup_global_symbol %s",
sym->name);
}
log_debug("lookup for %s @ 0x%016lx len %lu\n",
sym->name, result.value, result.size);
sym->sym.st_value = result.value;
sym->sym.st_shndx = SHN_ABS;
}
}
struct arguments {
char *args[3];
int debug;
};
static char args_doc[] = "original.o resolved.o xen-syms";
static struct argp_option options[] = {
{"debug", 'd', 0, 0, "Show debug output" },
{ 0 }
};
static error_t parse_opt (int key, char *arg, struct argp_state *state)
{
/* Get the input argument from argp_parse, which we
know is a pointer to our arguments structure. */
struct arguments *arguments = state->input;
switch (key)
{
case 'd':
arguments->debug = 1;
break;
case ARGP_KEY_ARG:
if (state->arg_num >= 3)
/* Too many arguments. */
argp_usage (state);
arguments->args[state->arg_num] = arg;
break;
case ARGP_KEY_END:
if (state->arg_num < 3)
/* Not enough arguments. */
argp_usage (state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, args_doc, 0 };
int main(int argc, char *argv[])
{
struct kpatch_elf *kelf;
struct arguments arguments;
struct lookup_table *lookup;
struct section *sec, *symtab;
arguments.debug = 0;
argp_parse (&argp, argc, argv, 0, 0, &arguments);
if (arguments.debug)
loglevel = DEBUG;
elf_version(EV_CURRENT);
childobj = basename(arguments.args[0]);
log_debug("Open elf\n");
kelf = kpatch_elf_open(arguments.args[0]);
/* create symbol lookup table */
log_debug("Lookup xen-syms\n");
lookup = lookup_open(arguments.args[2]);
log_debug("Resolve symbols\n");
livepatch_resolve_symbols(kelf, lookup);
/*
* Update rela section headers and rebuild the rela section data
* buffers from the relas lists.
*/
symtab = find_section_by_name(&kelf->sections, ".symtab");
list_for_each_entry(sec, &kelf->sections, list) {
if (!is_rela_section(sec))
continue;
sec->sh.sh_link = symtab->index;
sec->sh.sh_info = sec->base->index;
log_debug("Rebuild rela section data for %s\n", sec->name);
kpatch_rebuild_rela_section_data(sec);
}
log_debug("Create shstrtab\n");
kpatch_create_shstrtab(kelf);
log_debug("Create strtab\n");
kpatch_create_strtab(kelf);
log_debug("Create symtab\n");
kpatch_create_symtab(kelf);
log_debug("Dump elf status\n");
kpatch_dump_kelf(kelf);
log_debug("Write out elf\n");
kpatch_write_output_elf(kelf, kelf->elf, arguments.args[1]);
log_debug("Elf teardown\n");
kpatch_elf_teardown(kelf);
log_debug("Elf free\n");
kpatch_elf_free(kelf);
return 0;
}