Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 1 | /* |
| 2 | ;* Copyright (C) 2014 Google, Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 15 | * Foundation, Inc. |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 16 | */ |
| 17 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 18 | #include <inttypes.h> |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | #include "elfparsing.h" |
| 24 | #include "rmodule.h" |
| 25 | #include "../../src/include/rmodule-defs.h" |
| 26 | |
| 27 | struct rmod_context; |
| 28 | |
| 29 | struct arch_ops { |
| 30 | int arch; |
| 31 | /* Determine if relocation is a valid type for the architecture. */ |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 32 | int (*valid_type)(Elf64_Rela *rel); |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 33 | /* Determine if relocation should be emitted. */ |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 34 | int (*should_emit)(Elf64_Rela *rel); |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | struct rmod_context { |
| 38 | /* Ops to process relocations. */ |
| 39 | struct arch_ops *ops; |
| 40 | |
| 41 | /* endian conversion ops */ |
| 42 | struct xdr *xdr; |
| 43 | |
| 44 | /* Parsed ELF sturcture. */ |
| 45 | struct parsed_elf pelf; |
| 46 | /* Program segment. */ |
| 47 | Elf64_Phdr *phdr; |
| 48 | |
| 49 | /* Collection of relocation addresses fixup in the module. */ |
| 50 | Elf64_Xword nrelocs; |
| 51 | Elf64_Addr *emitted_relocs; |
| 52 | |
| 53 | /* The following fields are addresses within the linked program. */ |
| 54 | Elf64_Addr link_addr; |
| 55 | Elf64_Addr entry; |
| 56 | Elf64_Addr parameters_begin; |
| 57 | Elf64_Addr parameters_end; |
| 58 | Elf64_Addr bss_begin; |
| 59 | Elf64_Addr bss_end; |
| 60 | Elf64_Xword size; |
| 61 | }; |
| 62 | |
| 63 | /* |
| 64 | * Architecture specific support operations. |
| 65 | */ |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 66 | static int valid_reloc_386(Elf64_Rela *rel) |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 67 | { |
| 68 | int type; |
| 69 | |
| 70 | type = ELF64_R_TYPE(rel->r_info); |
| 71 | |
| 72 | /* Only these 2 relocations are expected to be found. */ |
| 73 | return (type == R_386_32 || type == R_386_PC32); |
| 74 | } |
| 75 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 76 | static int should_emit_386(Elf64_Rela *rel) |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 77 | { |
| 78 | int type; |
| 79 | |
| 80 | type = ELF64_R_TYPE(rel->r_info); |
| 81 | |
| 82 | /* R_386_32 relocations are absolute. Must emit these. */ |
| 83 | return (type == R_386_32); |
| 84 | } |
| 85 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 86 | static int valid_reloc_arm(Elf64_Rela *rel) |
Aaron Durbin | 785e47b | 2014-03-20 11:08:02 -0500 | [diff] [blame] | 87 | { |
| 88 | int type; |
| 89 | |
| 90 | type = ELF64_R_TYPE(rel->r_info); |
| 91 | |
Furquan Shaikh | c4f08f7 | 2014-07-23 13:42:22 -0700 | [diff] [blame] | 92 | /* Only these 6 relocations are expected to be found. */ |
Aaron Durbin | 785e47b | 2014-03-20 11:08:02 -0500 | [diff] [blame] | 93 | return (type == R_ARM_ABS32 || type == R_ARM_THM_PC22 || |
Furquan Shaikh | c4f08f7 | 2014-07-23 13:42:22 -0700 | [diff] [blame] | 94 | type == R_ARM_THM_JUMP24 || type == R_ARM_V4BX || |
| 95 | type == R_ARM_CALL || type == R_ARM_JUMP24); |
Aaron Durbin | 785e47b | 2014-03-20 11:08:02 -0500 | [diff] [blame] | 96 | } |
| 97 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 98 | static int should_emit_arm(Elf64_Rela *rel) |
Aaron Durbin | 785e47b | 2014-03-20 11:08:02 -0500 | [diff] [blame] | 99 | { |
| 100 | int type; |
| 101 | |
| 102 | type = ELF64_R_TYPE(rel->r_info); |
| 103 | |
| 104 | /* R_ARM_ABS32 relocations are absolute. Must emit these. */ |
| 105 | return (type == R_ARM_ABS32); |
| 106 | } |
| 107 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 108 | static int valid_reloc_aarch64(Elf64_Rela *rel) |
Furquan Shaikh | d2338ba | 2014-08-26 15:21:15 -0700 | [diff] [blame] | 109 | { |
| 110 | int type; |
| 111 | |
| 112 | type = ELF64_R_TYPE(rel->r_info); |
| 113 | |
| 114 | return (type == R_AARCH64_ADR_PREL_PG_HI21 || |
| 115 | type == R_AARCH64_ADD_ABS_LO12_NC || |
Furquan Shaikh | de77e6a | 2014-11-21 15:41:10 -0800 | [diff] [blame] | 116 | type == R_AARCH64_LDST8_ABS_LO12_NC || |
Furquan Shaikh | 16c0a41 | 2015-06-08 11:58:04 -0700 | [diff] [blame] | 117 | type == R_AARCH64_CONDBR19 || |
Furquan Shaikh | d2338ba | 2014-08-26 15:21:15 -0700 | [diff] [blame] | 118 | type == R_AARCH64_JUMP26 || |
| 119 | type == R_AARCH64_LDST32_ABS_LO12_NC || |
Aaron Durbin | a47898e | 2014-09-18 13:39:16 -0500 | [diff] [blame] | 120 | type == R_AARCH64_LDST64_ABS_LO12_NC || |
Furquan Shaikh | d2338ba | 2014-08-26 15:21:15 -0700 | [diff] [blame] | 121 | type == R_AARCH64_CALL26 || |
| 122 | type == R_AARCH64_ABS64 || |
| 123 | type == R_AARCH64_LD_PREL_LO19 || |
| 124 | type == R_AARCH64_ADR_PREL_LO21); |
| 125 | } |
| 126 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 127 | static int should_emit_aarch64(Elf64_Rela *rel) |
Furquan Shaikh | d2338ba | 2014-08-26 15:21:15 -0700 | [diff] [blame] | 128 | { |
| 129 | int type; |
| 130 | |
| 131 | type = ELF64_R_TYPE(rel->r_info); |
| 132 | |
| 133 | return (type == R_AARCH64_ABS64); |
| 134 | } |
| 135 | |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 136 | static struct arch_ops reloc_ops[] = { |
| 137 | { |
| 138 | .arch = EM_386, |
| 139 | .valid_type = valid_reloc_386, |
| 140 | .should_emit = should_emit_386, |
| 141 | }, |
Aaron Durbin | 785e47b | 2014-03-20 11:08:02 -0500 | [diff] [blame] | 142 | { |
| 143 | .arch = EM_ARM, |
| 144 | .valid_type = valid_reloc_arm, |
| 145 | .should_emit = should_emit_arm, |
| 146 | }, |
Furquan Shaikh | d2338ba | 2014-08-26 15:21:15 -0700 | [diff] [blame] | 147 | { |
| 148 | .arch = EM_AARCH64, |
| 149 | .valid_type = valid_reloc_aarch64, |
| 150 | .should_emit = should_emit_aarch64, |
| 151 | }, |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | /* |
| 155 | * Relocation processing loops. |
| 156 | */ |
| 157 | |
| 158 | static int for_each_reloc(struct rmod_context *ctx, int do_emit) |
| 159 | { |
| 160 | Elf64_Half i; |
| 161 | struct parsed_elf *pelf = &ctx->pelf; |
| 162 | |
| 163 | for (i = 0; i < pelf->ehdr.e_shnum; i++) { |
| 164 | Elf64_Shdr *shdr; |
| 165 | Elf64_Rela *relocs; |
| 166 | Elf64_Xword nrelocs; |
| 167 | Elf64_Xword j; |
| 168 | |
| 169 | relocs = pelf->relocs[i]; |
| 170 | |
| 171 | /* No relocations in this section. */ |
| 172 | if (relocs == NULL) |
| 173 | continue; |
| 174 | |
| 175 | shdr = &pelf->shdr[i]; |
| 176 | nrelocs = shdr->sh_size / shdr->sh_entsize; |
| 177 | |
| 178 | for (j = 0; j < nrelocs; j++) { |
| 179 | Elf64_Rela *r = &relocs[j]; |
| 180 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 181 | if (!ctx->ops->valid_type(r)) { |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 182 | ERROR("Invalid reloc type: %u\n", |
| 183 | (unsigned int)ELF64_R_TYPE(r->r_info)); |
| 184 | return -1; |
| 185 | } |
| 186 | |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 187 | if (ctx->ops->should_emit(r)) { |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 188 | int n = ctx->nrelocs; |
| 189 | if (do_emit) |
| 190 | ctx->emitted_relocs[n] = r->r_offset; |
| 191 | ctx->nrelocs++; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int find_program_segment(struct rmod_context *ctx) |
| 200 | { |
| 201 | int i; |
| 202 | int nsegments; |
| 203 | struct parsed_elf *pelf; |
| 204 | Elf64_Phdr *phdr; |
| 205 | |
| 206 | pelf = &ctx->pelf; |
| 207 | |
| 208 | /* There should only be a single loadable segment. */ |
| 209 | nsegments = 0; |
| 210 | for (i = 0; i < pelf->ehdr.e_phnum; i++) { |
| 211 | if (pelf->phdr[i].p_type != PT_LOAD) |
| 212 | continue; |
| 213 | phdr = &pelf->phdr[i]; |
| 214 | nsegments++; |
| 215 | } |
| 216 | |
| 217 | if (nsegments != 1) { |
| 218 | ERROR("Unexepcted number of loadable segments: %d.\n", |
| 219 | nsegments); |
| 220 | return -1; |
| 221 | } |
| 222 | |
| 223 | INFO("Segment at 0x%0llx, file size 0x%0llx, mem size 0x%0llx.\n", |
| 224 | (long long)phdr->p_vaddr, (long long)phdr->p_filesz, |
| 225 | (long long)phdr->p_memsz); |
| 226 | |
| 227 | ctx->phdr = phdr; |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static int |
| 233 | filter_relocation_sections(struct rmod_context *ctx) |
| 234 | { |
| 235 | int i; |
| 236 | const char *shstrtab; |
| 237 | struct parsed_elf *pelf; |
| 238 | const Elf64_Phdr *phdr; |
| 239 | |
| 240 | pelf = &ctx->pelf; |
| 241 | phdr = ctx->phdr; |
| 242 | shstrtab = buffer_get(pelf->strtabs[pelf->ehdr.e_shstrndx]); |
| 243 | |
| 244 | /* |
| 245 | * Find all relocation sections that contain relocation entries |
| 246 | * for sections that fall within the bounds of the segment. For |
| 247 | * easier processing the pointer to the relocation array for the |
| 248 | * sections that don't fall within the loadable program are NULL'd |
| 249 | * out. |
| 250 | */ |
| 251 | for (i = 0; i < pelf->ehdr.e_shnum; i++) { |
| 252 | Elf64_Shdr *shdr; |
| 253 | Elf64_Word sh_info; |
| 254 | const char *section_name; |
| 255 | |
| 256 | shdr = &pelf->shdr[i]; |
| 257 | |
| 258 | /* Ignore non-relocation sections. */ |
| 259 | if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL) |
| 260 | continue; |
| 261 | |
| 262 | /* Obtain section which relocations apply. */ |
| 263 | sh_info = shdr->sh_info; |
| 264 | shdr = &pelf->shdr[sh_info]; |
| 265 | |
| 266 | section_name = &shstrtab[shdr->sh_name]; |
| 267 | DEBUG("Relocation section found for '%s' section.\n", |
| 268 | section_name); |
| 269 | |
| 270 | /* Do not process relocations for debug sections. */ |
| 271 | if (strstr(section_name, ".debug") != NULL) { |
| 272 | pelf->relocs[i] = NULL; |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * If relocations apply to a non program section ignore the |
| 278 | * relocations for future processing. |
| 279 | */ |
| 280 | if (shdr->sh_type != SHT_PROGBITS) { |
| 281 | pelf->relocs[i] = NULL; |
| 282 | continue; |
| 283 | } |
| 284 | |
| 285 | if (shdr->sh_addr < phdr->p_vaddr || |
| 286 | ((shdr->sh_addr + shdr->sh_size) > |
| 287 | (phdr->p_vaddr + phdr->p_memsz))) { |
| 288 | ERROR("Relocations being applied to section %d not " |
| 289 | "within segment region.\n", sh_info); |
| 290 | return -1; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int vaddr_cmp(const void *a, const void *b) |
| 298 | { |
| 299 | const Elf64_Addr *pa = a; |
| 300 | const Elf64_Addr *pb = b; |
| 301 | |
| 302 | if (*pa < *pb) |
| 303 | return -1; |
| 304 | if (*pa > *pb) |
| 305 | return 1; |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | static int collect_relocations(struct rmod_context *ctx) |
| 310 | { |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 311 | Elf64_Xword nrelocs; |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 312 | |
| 313 | /* |
| 314 | * The relocs array in the pelf should only contain relocations that |
| 315 | * apply to the program. Count the number relocations. Then collect |
| 316 | * them into the allocated buffer. |
| 317 | */ |
| 318 | if (for_each_reloc(ctx, 0)) |
| 319 | return -1; |
| 320 | |
| 321 | nrelocs = ctx->nrelocs; |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 322 | INFO("%" PRIu64 " relocations to be emitted.\n", nrelocs); |
Furquan Shaikh | b237c10 | 2014-08-26 14:59:36 -0700 | [diff] [blame] | 323 | if (!nrelocs) |
| 324 | return 0; |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 325 | |
| 326 | /* Reset the counter for indexing into the array. */ |
| 327 | ctx->nrelocs = 0; |
| 328 | ctx->emitted_relocs = calloc(nrelocs, sizeof(Elf64_Addr)); |
| 329 | /* Write out the relocations into the emitted_relocs array. */ |
| 330 | if (for_each_reloc(ctx, 1)) |
| 331 | return -1; |
| 332 | |
| 333 | if (ctx->nrelocs != nrelocs) { |
| 334 | ERROR("Mismatch counted and emitted relocations: %zu vs %zu.\n", |
| 335 | (size_t)nrelocs, (size_t)ctx->nrelocs); |
| 336 | return -1; |
| 337 | } |
| 338 | |
| 339 | /* Sort the relocations by their address. */ |
| 340 | qsort(ctx->emitted_relocs, nrelocs, sizeof(Elf64_Addr), vaddr_cmp); |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | static int |
| 346 | populate_sym(struct rmod_context *ctx, const char *sym_name, Elf64_Addr *addr, |
| 347 | int nsyms, const char *strtab) |
| 348 | { |
| 349 | int i; |
| 350 | Elf64_Sym *syms; |
| 351 | |
| 352 | syms = ctx->pelf.syms; |
| 353 | |
| 354 | for (i = 0; i < nsyms; i++) { |
| 355 | if (syms[i].st_name == 0) |
| 356 | continue; |
| 357 | if (strcmp(sym_name, &strtab[syms[i].st_name])) |
| 358 | continue; |
| 359 | DEBUG("%s -> 0x%llx\n", sym_name, (long long)syms[i].st_value); |
| 360 | *addr = syms[i].st_value; |
| 361 | return 0; |
| 362 | } |
| 363 | ERROR("symbol '%s' not found.\n", sym_name); |
| 364 | return -1; |
| 365 | } |
| 366 | |
| 367 | static int populate_program_info(struct rmod_context *ctx) |
| 368 | { |
| 369 | int i; |
| 370 | const char *strtab; |
| 371 | struct parsed_elf *pelf; |
| 372 | Elf64_Ehdr *ehdr; |
| 373 | int nsyms; |
| 374 | |
| 375 | pelf = &ctx->pelf; |
| 376 | ehdr = &pelf->ehdr; |
| 377 | |
| 378 | /* Obtain the string table. */ |
| 379 | strtab = NULL; |
| 380 | for (i = 0; i < ehdr->e_shnum; i++) { |
| 381 | if (ctx->pelf.strtabs[i] == NULL) |
| 382 | continue; |
| 383 | /* Don't use the section headers' string table. */ |
| 384 | if (i == ehdr->e_shstrndx) |
| 385 | continue; |
| 386 | strtab = buffer_get(ctx->pelf.strtabs[i]); |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | if (strtab == NULL) { |
| 391 | ERROR("No string table found.\n"); |
| 392 | return -1; |
| 393 | } |
| 394 | |
| 395 | /* Determine number of symbols. */ |
| 396 | nsyms = 0; |
| 397 | for (i = 0; i < ehdr->e_shnum; i++) { |
| 398 | if (pelf->shdr[i].sh_type != SHT_SYMTAB) |
| 399 | continue; |
| 400 | |
| 401 | nsyms = pelf->shdr[i].sh_size / pelf->shdr[i].sh_entsize; |
| 402 | break; |
| 403 | } |
| 404 | |
| 405 | if (populate_sym(ctx, "_module_params_begin", &ctx->parameters_begin, |
| 406 | nsyms, strtab)) |
| 407 | return -1; |
| 408 | |
| 409 | if (populate_sym(ctx, "_module_params_end", &ctx->parameters_end, |
| 410 | nsyms, strtab)) |
| 411 | return -1; |
| 412 | |
| 413 | if (populate_sym(ctx, "_bss", &ctx->bss_begin, nsyms, strtab)) |
| 414 | return -1; |
| 415 | |
| 416 | if (populate_sym(ctx, "_ebss", &ctx->bss_end, nsyms, strtab)) |
| 417 | return -1; |
| 418 | |
| 419 | if (populate_sym(ctx, "__rmodule_entry", &ctx->entry, nsyms, strtab)) |
| 420 | return -1; |
| 421 | |
| 422 | /* Link address is the virtual address of the program segment. */ |
| 423 | ctx->link_addr = ctx->phdr->p_vaddr; |
| 424 | |
| 425 | /* The program size is the memsz of the program segment. */ |
| 426 | ctx->size = ctx->phdr->p_memsz; |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | static int |
| 432 | add_section(struct elf_writer *ew, struct buffer *data, const char *name, |
| 433 | Elf64_Addr addr, Elf64_Word size) |
| 434 | { |
| 435 | Elf64_Shdr shdr; |
| 436 | int ret; |
| 437 | |
| 438 | memset(&shdr, 0, sizeof(shdr)); |
| 439 | if (data != NULL) { |
| 440 | shdr.sh_type = SHT_PROGBITS; |
| 441 | shdr.sh_flags = SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR; |
| 442 | } else { |
| 443 | shdr.sh_type = SHT_NOBITS; |
| 444 | shdr.sh_flags = SHF_ALLOC; |
| 445 | } |
| 446 | shdr.sh_addr = addr; |
| 447 | shdr.sh_offset = addr; |
| 448 | shdr.sh_size = size; |
| 449 | |
| 450 | ret = elf_writer_add_section(ew, &shdr, data, name); |
| 451 | |
| 452 | if (ret) |
| 453 | ERROR("Could not add '%s' section.\n", name); |
| 454 | |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | static int |
| 459 | write_elf(const struct rmod_context *ctx, const struct buffer *in, |
| 460 | struct buffer *out) |
| 461 | { |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 462 | int ret; |
| 463 | int bit64; |
| 464 | size_t loc; |
| 465 | size_t rmod_data_size; |
| 466 | struct elf_writer *ew; |
| 467 | struct buffer rmod_data; |
| 468 | struct buffer rmod_header; |
| 469 | struct buffer program; |
| 470 | struct buffer relocs; |
| 471 | Elf64_Xword total_size; |
| 472 | Elf64_Addr addr; |
| 473 | Elf64_Ehdr ehdr; |
| 474 | |
| 475 | bit64 = ctx->pelf.ehdr.e_ident[EI_CLASS] == ELFCLASS64; |
| 476 | |
| 477 | /* |
| 478 | * 3 sections will be added to the ELF file. |
| 479 | * +------------------+ |
| 480 | * | rmodule header | |
| 481 | * +------------------+ |
| 482 | * | program | |
| 483 | * +------------------+ |
| 484 | * | relocations | |
| 485 | * +------------------+ |
| 486 | */ |
| 487 | |
| 488 | /* Create buffer for header and relocations. */ |
| 489 | rmod_data_size = sizeof(struct rmodule_header); |
| 490 | if (bit64) |
| 491 | rmod_data_size += ctx->nrelocs * sizeof(Elf64_Addr); |
| 492 | else |
| 493 | rmod_data_size += ctx->nrelocs * sizeof(Elf32_Addr); |
| 494 | |
| 495 | if (buffer_create(&rmod_data, rmod_data_size, "rmod")) |
| 496 | return -1; |
| 497 | |
| 498 | buffer_splice(&rmod_header, &rmod_data, |
| 499 | 0, sizeof(struct rmodule_header)); |
| 500 | buffer_clone(&relocs, &rmod_data); |
| 501 | buffer_seek(&relocs, sizeof(struct rmodule_header)); |
| 502 | |
| 503 | /* Reset current location. */ |
| 504 | buffer_set_size(&rmod_header, 0); |
| 505 | buffer_set_size(&relocs, 0); |
| 506 | |
| 507 | /* Program contents. */ |
| 508 | buffer_splice(&program, in, ctx->phdr->p_offset, ctx->phdr->p_filesz); |
| 509 | |
| 510 | /* Create ELF writer with modified entry point. */ |
| 511 | memcpy(&ehdr, &ctx->pelf.ehdr, sizeof(ehdr)); |
| 512 | ehdr.e_entry = ctx->entry; |
| 513 | ew = elf_writer_init(&ehdr); |
| 514 | |
| 515 | if (ew == NULL) { |
| 516 | ERROR("Failed to create ELF writer.\n"); |
| 517 | buffer_delete(&rmod_data); |
| 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | /* Write out rmodule_header. */ |
| 522 | ctx->xdr->put16(&rmod_header, RMODULE_MAGIC); |
| 523 | ctx->xdr->put8(&rmod_header, RMODULE_VERSION_1); |
| 524 | ctx->xdr->put8(&rmod_header, 0); |
| 525 | /* payload_begin_offset */ |
| 526 | loc = sizeof(struct rmodule_header); |
| 527 | ctx->xdr->put32(&rmod_header, loc); |
| 528 | /* payload_end_offset */ |
| 529 | loc += ctx->phdr->p_filesz; |
| 530 | ctx->xdr->put32(&rmod_header, loc); |
| 531 | /* relocations_begin_offset */ |
| 532 | ctx->xdr->put32(&rmod_header, loc); |
| 533 | /* relocations_end_offset */ |
| 534 | if (bit64) |
| 535 | loc += ctx->nrelocs * sizeof(Elf64_Addr); |
| 536 | else |
| 537 | loc += ctx->nrelocs * sizeof(Elf32_Addr); |
| 538 | ctx->xdr->put32(&rmod_header, loc); |
| 539 | /* module_link_start_address */ |
| 540 | ctx->xdr->put32(&rmod_header, ctx->link_addr); |
| 541 | /* module_program_size */ |
| 542 | ctx->xdr->put32(&rmod_header, ctx->size); |
| 543 | /* module_entry_point */ |
| 544 | ctx->xdr->put32(&rmod_header, ctx->entry); |
| 545 | /* parameters_begin */ |
| 546 | ctx->xdr->put32(&rmod_header, ctx->parameters_begin); |
| 547 | /* parameters_end */ |
| 548 | ctx->xdr->put32(&rmod_header, ctx->parameters_end); |
| 549 | /* bss_begin */ |
| 550 | ctx->xdr->put32(&rmod_header, ctx->bss_begin); |
| 551 | /* bss_end */ |
| 552 | ctx->xdr->put32(&rmod_header, ctx->bss_end); |
| 553 | /* padding[4] */ |
| 554 | ctx->xdr->put32(&rmod_header, 0); |
| 555 | ctx->xdr->put32(&rmod_header, 0); |
| 556 | ctx->xdr->put32(&rmod_header, 0); |
| 557 | ctx->xdr->put32(&rmod_header, 0); |
| 558 | |
| 559 | /* Write the relocations. */ |
Sol Boucher | 0e53931 | 2015-03-05 15:38:03 -0800 | [diff] [blame] | 560 | for (unsigned i = 0; i < ctx->nrelocs; i++) { |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 561 | if (bit64) |
| 562 | ctx->xdr->put64(&relocs, ctx->emitted_relocs[i]); |
| 563 | else |
| 564 | ctx->xdr->put32(&relocs, ctx->emitted_relocs[i]); |
| 565 | } |
| 566 | |
| 567 | total_size = 0; |
| 568 | addr = 0; |
| 569 | |
| 570 | /* |
| 571 | * There are 2 cases to deal with. The program has a large NOBITS |
| 572 | * section and the relocations can fit entirely within occupied memory |
| 573 | * region for the program. The other is that the relocations increase |
| 574 | * the memory footprint of the program if it was loaded directly into |
| 575 | * the region it would run. The rmdoule header is a fixed cost that |
| 576 | * is considered a part of the program. |
| 577 | */ |
| 578 | total_size += buffer_size(&rmod_header); |
Aaron Durbin | 518a322 | 2014-08-26 13:52:30 -0500 | [diff] [blame] | 579 | if (buffer_size(&relocs) + ctx->phdr->p_filesz > ctx->phdr->p_memsz) { |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 580 | total_size += buffer_size(&relocs); |
| 581 | total_size += ctx->phdr->p_filesz; |
Aaron Durbin | 518a322 | 2014-08-26 13:52:30 -0500 | [diff] [blame] | 582 | } else { |
| 583 | total_size += ctx->phdr->p_memsz; |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | ret = add_section(ew, &rmod_header, ".header", addr, |
| 587 | buffer_size(&rmod_header)); |
| 588 | if (ret < 0) |
| 589 | goto out; |
| 590 | addr += buffer_size(&rmod_header); |
| 591 | |
| 592 | ret = add_section(ew, &program, ".program", addr, ctx->phdr->p_filesz); |
| 593 | if (ret < 0) |
| 594 | goto out; |
| 595 | addr += ctx->phdr->p_filesz; |
| 596 | |
Furquan Shaikh | b237c10 | 2014-08-26 14:59:36 -0700 | [diff] [blame] | 597 | if (ctx->nrelocs) { |
| 598 | ret = add_section(ew, &relocs, ".relocs", addr, |
| 599 | buffer_size(&relocs)); |
| 600 | if (ret < 0) |
| 601 | goto out; |
| 602 | addr += buffer_size(&relocs); |
| 603 | } |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 604 | |
| 605 | if (total_size != addr) { |
| 606 | ret = add_section(ew, NULL, ".empty", addr, total_size - addr); |
| 607 | if (ret < 0) |
| 608 | goto out; |
| 609 | } |
| 610 | |
| 611 | /* |
| 612 | * Ensure last section has a memory usage that meets the required |
| 613 | * total size of the program in memory. |
| 614 | */ |
| 615 | |
| 616 | ret = elf_writer_serialize(ew, out); |
| 617 | if (ret < 0) |
| 618 | ERROR("Failed to serialize ELF to buffer.\n"); |
| 619 | |
| 620 | out: |
| 621 | buffer_delete(&rmod_data); |
| 622 | elf_writer_destroy(ew); |
| 623 | |
| 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | int rmodule_create(const struct buffer *elfin, struct buffer *elfout) |
| 628 | { |
| 629 | struct rmod_context ctx; |
| 630 | struct parsed_elf *pelf; |
| 631 | int i; |
| 632 | int ret; |
| 633 | |
| 634 | ret = -1; |
| 635 | memset(&ctx, 0, sizeof(ctx)); |
| 636 | pelf = &ctx.pelf; |
| 637 | |
| 638 | if (parse_elf(elfin, pelf, ELF_PARSE_ALL)) { |
| 639 | ERROR("Couldn't parse ELF!\n"); |
| 640 | return -1; |
| 641 | } |
| 642 | |
| 643 | /* Only allow executables to be turned into rmodules. */ |
| 644 | if (pelf->ehdr.e_type != ET_EXEC) { |
| 645 | ERROR("ELF is not an executable: %u.\n", pelf->ehdr.e_type); |
| 646 | goto out; |
| 647 | } |
| 648 | |
| 649 | /* Determine if architecture is supported. */ |
| 650 | for (i = 0; i < ARRAY_SIZE(reloc_ops); i++) { |
| 651 | if (reloc_ops[i].arch == pelf->ehdr.e_machine) { |
| 652 | ctx.ops = &reloc_ops[i]; |
| 653 | break; |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | if (ctx.ops == NULL) { |
| 658 | ERROR("ELF is unsupported arch: %u.\n", pelf->ehdr.e_machine); |
| 659 | goto out; |
| 660 | } |
| 661 | |
| 662 | /* Set the endian ops. */ |
| 663 | if (ctx.pelf.ehdr.e_ident[EI_DATA] == ELFDATA2MSB) |
| 664 | ctx.xdr = &xdr_be; |
| 665 | else |
| 666 | ctx.xdr = &xdr_le; |
| 667 | |
| 668 | if (find_program_segment(&ctx)) |
| 669 | goto out; |
| 670 | |
| 671 | if (filter_relocation_sections(&ctx)) |
| 672 | goto out; |
| 673 | |
| 674 | if (collect_relocations(&ctx)) |
| 675 | goto out; |
| 676 | |
| 677 | if (populate_program_info(&ctx)) |
| 678 | goto out; |
| 679 | |
| 680 | if (write_elf(&ctx, elfin, elfout)) |
| 681 | goto out; |
| 682 | |
| 683 | ret = 0; |
| 684 | |
| 685 | out: |
| 686 | free(ctx.emitted_relocs); |
| 687 | parsed_elf_destroy(pelf); |
| 688 | return ret; |
| 689 | } |