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