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