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