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