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