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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/*
* create-kpatch-module.c
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA,
* 02110-1301, USA.
*/
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#include <argp.h>
#include "log.h"
#include "kpatch-elf.h"
#include "kpatch-intermediate.h"
#include "kpatch-patch.h"
/* For log.h */
char *childobj;
enum loglevel loglevel = NORMAL;
/*
* Create .kpatch.dynrelas from .kpatch.relocations and .kpatch.symbols sections
*
* Iterate through .kpatch.relocations and fill in the corresponding dynrela
* entry using information from .kpatch.relocations and .kpatch.symbols
*/
static void create_dynamic_rela_sections(struct kpatch_elf *kelf, struct section *krelasec,
struct section *ksymsec, struct section *strsec)
{
struct kpatch_patch_dynrela *dynrelas;
struct kpatch_relocation *krelas;
struct kpatch_symbol *ksym, *ksyms;
struct section *dynsec;
struct symbol *sym;
struct rela *rela;
unsigned int index, nr, offset, dest_offset, objname_offset, name_offset;
unsigned int type;
long addend;
char *target_name;
ksyms = ksymsec->data->d_buf;
krelas = krelasec->data->d_buf;
nr = (unsigned int)(krelasec->data->d_size / sizeof(*krelas));
dynsec = create_section_pair(kelf, ".kpatch.dynrelas", sizeof(*dynrelas), nr);
dynrelas = dynsec->data->d_buf;
for (index = 0; index < nr; index++) {
offset = index * (unsigned int)sizeof(*krelas);
/*
* To fill in each dynrela entry, find dest location,
* objname offset, ksym, and symbol name offset
*/
/* Get dest location */
rela = find_rela_by_offset(krelasec->rela,
offset + offsetof(struct kpatch_relocation, dest));
if (!rela)
ERROR("find_rela_by_offset");
sym = rela->sym;
dest_offset = (unsigned int)rela->addend;
/* Get objname offset */
rela = find_rela_by_offset(krelasec->rela,
(unsigned int)(offset + offsetof(struct kpatch_relocation, objname)));
if (!rela)
ERROR("find_rela_by_offset");
objname_offset = (unsigned int)rela->addend;
/* Get ksym (.kpatch.symbols entry) and symbol name offset */
rela = find_rela_by_offset(krelasec->rela,
(unsigned int)(offset + offsetof(struct kpatch_relocation, ksym)));
if (!rela)
ERROR("find_rela_by_offset");
ksym = ksyms + (rela->addend / sizeof(*ksyms));
offset = (unsigned int )(index * sizeof(*ksyms));
rela = find_rela_by_offset(ksymsec->rela,
(unsigned int)(offset + offsetof(struct kpatch_symbol, name)));
if (!rela)
ERROR("find_rela_by_offset");
name_offset = (unsigned int)rela->addend;
/* Fill in dynrela entry */
type = krelas[index].type;
addend = krelas[index].addend;
if (type == R_X86_64_64 && (addend > INT_MAX || addend <= INT_MIN)) {
target_name = (char *)strsec->data->d_buf + name_offset;
ERROR("got R_X86_64_64 dynrela for '%s' with addend too large or too small for an int: %lx",
target_name, addend);
}
dynrelas[index].src = ksym->src;
dynrelas[index].addend = addend;
dynrelas[index].type = type;
dynrelas[index].external = krelas[index].external;
dynrelas[index].sympos = ksym->sympos;
/* dest */
ALLOC_LINK(rela, &dynsec->rela->relas);
rela->sym = sym;
rela->type = R_X86_64_64;
rela->addend = dest_offset;
rela->offset = (unsigned int)(index * sizeof(*dynrelas));
/* name */
ALLOC_LINK(rela, &dynsec->rela->relas);
rela->sym = strsec->secsym;
rela->type = R_X86_64_64;
rela->addend = name_offset;
rela->offset = (unsigned int)(index * sizeof(*dynrelas) + \
offsetof(struct kpatch_patch_dynrela, name));
/* objname */
ALLOC_LINK(rela, &dynsec->rela->relas);
rela->sym = strsec->secsym;
rela->type = R_X86_64_64;
rela->addend = objname_offset;
rela->offset = (unsigned int)(index * sizeof(*dynrelas) + \
offsetof(struct kpatch_patch_dynrela, objname));
}
}
static void remove_intermediate_sections(struct kpatch_elf *kelf)
{
size_t i;
char *intermediate_sections[] = {
".kpatch.symbols",
".rela.kpatch.symbols",
".kpatch.relocations",
".rela.kpatch.relocations",
".kpatch.arch",
".rela.kpatch.arch"
};
for (i = 0; i < sizeof(intermediate_sections)/sizeof(intermediate_sections[0]); i++)
kpatch_remove_and_free_section(kelf, intermediate_sections[i]);
}
struct arguments {
char *args[2];
int debug;
};
static char args_doc[] = "input.o output.o";
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 >= 2)
/* Too many arguments. */
argp_usage (state);
arguments->args[state->arg_num] = arg;
break;
case ARGP_KEY_END:
if (state->arg_num < 2)
/* 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 section *symtab, *sec;
struct section *ksymsec, *krelasec, *strsec;
struct arguments arguments;
unsigned int ksyms_nr, krelas_nr;
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]);
kelf = kpatch_elf_open(arguments.args[0]);
/*
* Sanity checks:
* - Make sure all the required sections exist
* - Make sure that the number of entries in
* .kpatch.{symbols,relocations} match
*/
strsec = find_section_by_name(&kelf->sections, ".kpatch.strings");
if (!strsec)
ERROR("missing .kpatch.strings");
ksymsec = find_section_by_name(&kelf->sections, ".kpatch.symbols");
if (!ksymsec)
ERROR("missing .kpatch.symbols section");
ksyms_nr = (unsigned int)(ksymsec->data->d_size / sizeof(struct kpatch_symbol));
krelasec = find_section_by_name(&kelf->sections, ".kpatch.relocations");
if (!krelasec)
ERROR("missing .kpatch.relocations section");
krelas_nr = (unsigned int)(krelasec->data->d_size / sizeof(struct kpatch_relocation));
if (krelas_nr != ksyms_nr)
ERROR("number of krelas and ksyms do not match");
/* Create dynrelas from .kpatch.{relocations,symbols} sections */
create_dynamic_rela_sections(kelf, krelasec, ksymsec, strsec);
remove_intermediate_sections(kelf);
kpatch_reindex_elements(kelf);
symtab = find_section_by_name(&kelf->sections, ".symtab");
if (!symtab)
ERROR("missing .symtab section");
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;
kpatch_rebuild_rela_section_data(sec);
}
kpatch_create_shstrtab(kelf);
kpatch_create_strtab(kelf);
kpatch_create_symtab(kelf);
kpatch_write_output_elf(kelf, kelf->elf, arguments.args[1], 0664);
kpatch_elf_teardown(kelf);
kpatch_elf_free(kelf);
return 0;
}
|