blob: 03828f77c9bc11783cd600fa4864116d0bd887fc [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
Aaron Durbin051a1812015-09-08 15:52:01 -050053 /* The following fields are addresses within the linked program. */
Aaron Durbin4fde5a62014-03-07 15:11:53 -060054 Elf64_Addr parameters_begin;
55 Elf64_Addr parameters_end;
56 Elf64_Addr bss_begin;
57 Elf64_Addr bss_end;
Aaron Durbin4fde5a62014-03-07 15:11:53 -060058};
59
60/*
61 * Architecture specific support operations.
62 */
Sol Boucher0e539312015-03-05 15:38:03 -080063static int valid_reloc_386(Elf64_Rela *rel)
Aaron Durbin4fde5a62014-03-07 15:11:53 -060064{
65 int type;
66
67 type = ELF64_R_TYPE(rel->r_info);
68
69 /* Only these 2 relocations are expected to be found. */
70 return (type == R_386_32 || type == R_386_PC32);
71}
72
Sol Boucher0e539312015-03-05 15:38:03 -080073static int should_emit_386(Elf64_Rela *rel)
Aaron Durbin4fde5a62014-03-07 15:11:53 -060074{
75 int type;
76
77 type = ELF64_R_TYPE(rel->r_info);
78
79 /* R_386_32 relocations are absolute. Must emit these. */
80 return (type == R_386_32);
81}
82
Sol Boucher0e539312015-03-05 15:38:03 -080083static int valid_reloc_arm(Elf64_Rela *rel)
Aaron Durbin785e47b2014-03-20 11:08:02 -050084{
85 int type;
86
87 type = ELF64_R_TYPE(rel->r_info);
88
Furquan Shaikhc4f08f72014-07-23 13:42:22 -070089 /* Only these 6 relocations are expected to be found. */
Aaron Durbin785e47b2014-03-20 11:08:02 -050090 return (type == R_ARM_ABS32 || type == R_ARM_THM_PC22 ||
Furquan Shaikhc4f08f72014-07-23 13:42:22 -070091 type == R_ARM_THM_JUMP24 || type == R_ARM_V4BX ||
92 type == R_ARM_CALL || type == R_ARM_JUMP24);
Aaron Durbin785e47b2014-03-20 11:08:02 -050093}
94
Sol Boucher0e539312015-03-05 15:38:03 -080095static int should_emit_arm(Elf64_Rela *rel)
Aaron Durbin785e47b2014-03-20 11:08:02 -050096{
97 int type;
98
99 type = ELF64_R_TYPE(rel->r_info);
100
101 /* R_ARM_ABS32 relocations are absolute. Must emit these. */
102 return (type == R_ARM_ABS32);
103}
104
Sol Boucher0e539312015-03-05 15:38:03 -0800105static int valid_reloc_aarch64(Elf64_Rela *rel)
Furquan Shaikhd2338ba2014-08-26 15:21:15 -0700106{
107 int type;
108
109 type = ELF64_R_TYPE(rel->r_info);
110
111 return (type == R_AARCH64_ADR_PREL_PG_HI21 ||
112 type == R_AARCH64_ADD_ABS_LO12_NC ||
Furquan Shaikhde77e6a2014-11-21 15:41:10 -0800113 type == R_AARCH64_LDST8_ABS_LO12_NC ||
Furquan Shaikh16c0a412015-06-08 11:58:04 -0700114 type == R_AARCH64_CONDBR19 ||
Furquan Shaikhd2338ba2014-08-26 15:21:15 -0700115 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
Sol Boucher0e539312015-03-05 15:38:03 -0800124static int should_emit_aarch64(Elf64_Rela *rel)
Furquan Shaikhd2338ba2014-08-26 15:21:15 -0700125{
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
Sol Boucher0e539312015-03-05 15:38:03 -0800178 if (!ctx->ops->valid_type(r)) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600179 ERROR("Invalid reloc type: %u\n",
180 (unsigned int)ELF64_R_TYPE(r->r_info));
181 return -1;
182 }
183
Sol Boucher0e539312015-03-05 15:38:03 -0800184 if (ctx->ops->should_emit(r)) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600185 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;
Anatol Pomozov8cce7012015-07-10 17:30:01 -0700201 Elf64_Phdr *phdr = NULL;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600202
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{
Sol Boucher0e539312015-03-05 15:38:03 -0800308 Elf64_Xword nrelocs;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600309
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;
Sol Boucher0e539312015-03-05 15:38:03 -0800319 INFO("%" PRIu64 " 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,
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500344 int nsyms, const char *strtab, int optional)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600345{
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 }
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500360
361 if (optional) {
362 DEBUG("optional symbol '%s' not found.\n", sym_name);
363 *addr = 0;
364 return 0;
365 }
366
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600367 ERROR("symbol '%s' not found.\n", sym_name);
368 return -1;
369}
370
Aaron Durbin051a1812015-09-08 15:52:01 -0500371static int populate_rmodule_info(struct rmod_context *ctx)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600372{
373 int i;
374 const char *strtab;
375 struct parsed_elf *pelf;
376 Elf64_Ehdr *ehdr;
377 int nsyms;
378
379 pelf = &ctx->pelf;
380 ehdr = &pelf->ehdr;
381
382 /* Obtain the string table. */
383 strtab = NULL;
384 for (i = 0; i < ehdr->e_shnum; i++) {
385 if (ctx->pelf.strtabs[i] == NULL)
386 continue;
387 /* Don't use the section headers' string table. */
388 if (i == ehdr->e_shstrndx)
389 continue;
390 strtab = buffer_get(ctx->pelf.strtabs[i]);
391 break;
392 }
393
394 if (strtab == NULL) {
395 ERROR("No string table found.\n");
396 return -1;
397 }
398
399 /* Determine number of symbols. */
400 nsyms = 0;
401 for (i = 0; i < ehdr->e_shnum; i++) {
402 if (pelf->shdr[i].sh_type != SHT_SYMTAB)
403 continue;
404
405 nsyms = pelf->shdr[i].sh_size / pelf->shdr[i].sh_entsize;
406 break;
407 }
408
Aaron Durbindde76292015-09-05 12:59:26 -0500409 if (populate_sym(ctx, "_rmodule_params", &ctx->parameters_begin,
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500410 nsyms, strtab, 1))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600411 return -1;
412
Aaron Durbindde76292015-09-05 12:59:26 -0500413 if (populate_sym(ctx, "_ermodule_params", &ctx->parameters_end,
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500414 nsyms, strtab, 1))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600415 return -1;
416
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500417 if (populate_sym(ctx, "_bss", &ctx->bss_begin, nsyms, strtab, 0))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600418 return -1;
419
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500420 if (populate_sym(ctx, "_ebss", &ctx->bss_end, nsyms, strtab, 0))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600421 return -1;
422
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600423 return 0;
424}
425
426static int
427add_section(struct elf_writer *ew, struct buffer *data, const char *name,
428 Elf64_Addr addr, Elf64_Word size)
429{
430 Elf64_Shdr shdr;
431 int ret;
432
433 memset(&shdr, 0, sizeof(shdr));
434 if (data != NULL) {
435 shdr.sh_type = SHT_PROGBITS;
436 shdr.sh_flags = SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR;
437 } else {
438 shdr.sh_type = SHT_NOBITS;
439 shdr.sh_flags = SHF_ALLOC;
440 }
441 shdr.sh_addr = addr;
442 shdr.sh_offset = addr;
443 shdr.sh_size = size;
444
445 ret = elf_writer_add_section(ew, &shdr, data, name);
446
447 if (ret)
448 ERROR("Could not add '%s' section.\n", name);
449
450 return ret;
451}
452
453static int
454write_elf(const struct rmod_context *ctx, const struct buffer *in,
455 struct buffer *out)
456{
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600457 int ret;
458 int bit64;
459 size_t loc;
460 size_t rmod_data_size;
461 struct elf_writer *ew;
462 struct buffer rmod_data;
463 struct buffer rmod_header;
464 struct buffer program;
465 struct buffer relocs;
466 Elf64_Xword total_size;
467 Elf64_Addr addr;
468 Elf64_Ehdr ehdr;
469
470 bit64 = ctx->pelf.ehdr.e_ident[EI_CLASS] == ELFCLASS64;
471
472 /*
473 * 3 sections will be added to the ELF file.
474 * +------------------+
475 * | rmodule header |
476 * +------------------+
477 * | program |
478 * +------------------+
479 * | relocations |
480 * +------------------+
481 */
482
483 /* Create buffer for header and relocations. */
484 rmod_data_size = sizeof(struct rmodule_header);
485 if (bit64)
486 rmod_data_size += ctx->nrelocs * sizeof(Elf64_Addr);
487 else
488 rmod_data_size += ctx->nrelocs * sizeof(Elf32_Addr);
489
490 if (buffer_create(&rmod_data, rmod_data_size, "rmod"))
491 return -1;
492
493 buffer_splice(&rmod_header, &rmod_data,
494 0, sizeof(struct rmodule_header));
495 buffer_clone(&relocs, &rmod_data);
496 buffer_seek(&relocs, sizeof(struct rmodule_header));
497
498 /* Reset current location. */
499 buffer_set_size(&rmod_header, 0);
500 buffer_set_size(&relocs, 0);
501
502 /* Program contents. */
503 buffer_splice(&program, in, ctx->phdr->p_offset, ctx->phdr->p_filesz);
504
505 /* Create ELF writer with modified entry point. */
506 memcpy(&ehdr, &ctx->pelf.ehdr, sizeof(ehdr));
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600507 ew = elf_writer_init(&ehdr);
508
509 if (ew == NULL) {
510 ERROR("Failed to create ELF writer.\n");
511 buffer_delete(&rmod_data);
512 return -1;
513 }
514
515 /* Write out rmodule_header. */
516 ctx->xdr->put16(&rmod_header, RMODULE_MAGIC);
517 ctx->xdr->put8(&rmod_header, RMODULE_VERSION_1);
518 ctx->xdr->put8(&rmod_header, 0);
519 /* payload_begin_offset */
520 loc = sizeof(struct rmodule_header);
521 ctx->xdr->put32(&rmod_header, loc);
522 /* payload_end_offset */
523 loc += ctx->phdr->p_filesz;
524 ctx->xdr->put32(&rmod_header, loc);
525 /* relocations_begin_offset */
526 ctx->xdr->put32(&rmod_header, loc);
527 /* relocations_end_offset */
528 if (bit64)
529 loc += ctx->nrelocs * sizeof(Elf64_Addr);
530 else
531 loc += ctx->nrelocs * sizeof(Elf32_Addr);
532 ctx->xdr->put32(&rmod_header, loc);
533 /* module_link_start_address */
Aaron Durbin051a1812015-09-08 15:52:01 -0500534 ctx->xdr->put32(&rmod_header, ctx->phdr->p_vaddr);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600535 /* module_program_size */
Aaron Durbin051a1812015-09-08 15:52:01 -0500536 ctx->xdr->put32(&rmod_header, ctx->phdr->p_memsz);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600537 /* module_entry_point */
Aaron Durbin051a1812015-09-08 15:52:01 -0500538 ctx->xdr->put32(&rmod_header, ctx->pelf.ehdr.e_entry);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600539 /* parameters_begin */
540 ctx->xdr->put32(&rmod_header, ctx->parameters_begin);
541 /* parameters_end */
542 ctx->xdr->put32(&rmod_header, ctx->parameters_end);
543 /* bss_begin */
544 ctx->xdr->put32(&rmod_header, ctx->bss_begin);
545 /* bss_end */
546 ctx->xdr->put32(&rmod_header, ctx->bss_end);
547 /* padding[4] */
548 ctx->xdr->put32(&rmod_header, 0);
549 ctx->xdr->put32(&rmod_header, 0);
550 ctx->xdr->put32(&rmod_header, 0);
551 ctx->xdr->put32(&rmod_header, 0);
552
553 /* Write the relocations. */
Sol Boucher0e539312015-03-05 15:38:03 -0800554 for (unsigned i = 0; i < ctx->nrelocs; i++) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600555 if (bit64)
556 ctx->xdr->put64(&relocs, ctx->emitted_relocs[i]);
557 else
558 ctx->xdr->put32(&relocs, ctx->emitted_relocs[i]);
559 }
560
561 total_size = 0;
562 addr = 0;
563
564 /*
565 * There are 2 cases to deal with. The program has a large NOBITS
566 * section and the relocations can fit entirely within occupied memory
567 * region for the program. The other is that the relocations increase
568 * the memory footprint of the program if it was loaded directly into
569 * the region it would run. The rmdoule header is a fixed cost that
570 * is considered a part of the program.
571 */
572 total_size += buffer_size(&rmod_header);
Aaron Durbin518a3222014-08-26 13:52:30 -0500573 if (buffer_size(&relocs) + ctx->phdr->p_filesz > ctx->phdr->p_memsz) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600574 total_size += buffer_size(&relocs);
575 total_size += ctx->phdr->p_filesz;
Aaron Durbin518a3222014-08-26 13:52:30 -0500576 } else {
577 total_size += ctx->phdr->p_memsz;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600578 }
579
580 ret = add_section(ew, &rmod_header, ".header", addr,
581 buffer_size(&rmod_header));
582 if (ret < 0)
583 goto out;
584 addr += buffer_size(&rmod_header);
585
586 ret = add_section(ew, &program, ".program", addr, ctx->phdr->p_filesz);
587 if (ret < 0)
588 goto out;
589 addr += ctx->phdr->p_filesz;
590
Furquan Shaikhb237c102014-08-26 14:59:36 -0700591 if (ctx->nrelocs) {
592 ret = add_section(ew, &relocs, ".relocs", addr,
593 buffer_size(&relocs));
594 if (ret < 0)
595 goto out;
596 addr += buffer_size(&relocs);
597 }
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600598
599 if (total_size != addr) {
600 ret = add_section(ew, NULL, ".empty", addr, total_size - addr);
601 if (ret < 0)
602 goto out;
603 }
604
605 /*
606 * Ensure last section has a memory usage that meets the required
607 * total size of the program in memory.
608 */
609
610 ret = elf_writer_serialize(ew, out);
611 if (ret < 0)
612 ERROR("Failed to serialize ELF to buffer.\n");
613
614out:
615 buffer_delete(&rmod_data);
616 elf_writer_destroy(ew);
617
618 return ret;
619}
620
Aaron Durbin051a1812015-09-08 15:52:01 -0500621static int rmodule_init(struct rmod_context *ctx, const struct buffer *elfin)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600622{
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600623 struct parsed_elf *pelf;
624 int i;
625 int ret;
626
627 ret = -1;
Aaron Durbin051a1812015-09-08 15:52:01 -0500628 memset(ctx, 0, sizeof(*ctx));
629 pelf = &ctx->pelf;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600630
631 if (parse_elf(elfin, pelf, ELF_PARSE_ALL)) {
632 ERROR("Couldn't parse ELF!\n");
633 return -1;
634 }
635
636 /* Only allow executables to be turned into rmodules. */
637 if (pelf->ehdr.e_type != ET_EXEC) {
638 ERROR("ELF is not an executable: %u.\n", pelf->ehdr.e_type);
639 goto out;
640 }
641
642 /* Determine if architecture is supported. */
643 for (i = 0; i < ARRAY_SIZE(reloc_ops); i++) {
644 if (reloc_ops[i].arch == pelf->ehdr.e_machine) {
Aaron Durbin051a1812015-09-08 15:52:01 -0500645 ctx->ops = &reloc_ops[i];
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600646 break;
647 }
648 }
649
Aaron Durbin051a1812015-09-08 15:52:01 -0500650 if (ctx->ops == NULL) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600651 ERROR("ELF is unsupported arch: %u.\n", pelf->ehdr.e_machine);
652 goto out;
653 }
654
655 /* Set the endian ops. */
Aaron Durbin051a1812015-09-08 15:52:01 -0500656 if (ctx->pelf.ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
657 ctx->xdr = &xdr_be;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600658 else
Aaron Durbin051a1812015-09-08 15:52:01 -0500659 ctx->xdr = &xdr_le;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600660
Aaron Durbin051a1812015-09-08 15:52:01 -0500661 if (find_program_segment(ctx))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600662 goto out;
663
Aaron Durbin051a1812015-09-08 15:52:01 -0500664 if (filter_relocation_sections(ctx))
665 goto out;
666
667 ret = 0;
668
669out:
670 return ret;
671}
672
673static void rmodule_cleanup(struct rmod_context *ctx)
674{
675 free(ctx->emitted_relocs);
676 parsed_elf_destroy(&ctx->pelf);
677}
678
679int rmodule_create(const struct buffer *elfin, struct buffer *elfout)
680{
681 struct rmod_context ctx;
682 int ret = -1;
683
684 if (rmodule_init(&ctx, elfin))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600685 goto out;
686
687 if (collect_relocations(&ctx))
688 goto out;
689
Aaron Durbin051a1812015-09-08 15:52:01 -0500690 if (populate_rmodule_info(&ctx))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600691 goto out;
692
693 if (write_elf(&ctx, elfin, elfout))
694 goto out;
695
696 ret = 0;
697
698out:
Aaron Durbin051a1812015-09-08 15:52:01 -0500699 rmodule_cleanup(&ctx);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600700 return ret;
701}