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