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