blob: 986ba623dea1d4bb162fb4204d25c7b370c6e404 [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"
Aaron Durbindc9f5cd2015-09-08 13:34:43 -050025#include <commonlib/rmodule-defs.h>
Aaron Durbin4fde5a62014-03-07 15:11:53 -060026
Aaron Durbin4fde5a62014-03-07 15:11:53 -060027/*
28 * Architecture specific support operations.
29 */
Sol Boucher0e539312015-03-05 15:38:03 -080030static int valid_reloc_386(Elf64_Rela *rel)
Aaron Durbin4fde5a62014-03-07 15:11:53 -060031{
32 int type;
33
34 type = ELF64_R_TYPE(rel->r_info);
35
36 /* Only these 2 relocations are expected to be found. */
37 return (type == R_386_32 || type == R_386_PC32);
38}
39
Sol Boucher0e539312015-03-05 15:38:03 -080040static int should_emit_386(Elf64_Rela *rel)
Aaron Durbin4fde5a62014-03-07 15:11:53 -060041{
42 int type;
43
44 type = ELF64_R_TYPE(rel->r_info);
45
46 /* R_386_32 relocations are absolute. Must emit these. */
47 return (type == R_386_32);
48}
49
Sol Boucher0e539312015-03-05 15:38:03 -080050static int valid_reloc_arm(Elf64_Rela *rel)
Aaron Durbin785e47b2014-03-20 11:08:02 -050051{
52 int type;
53
54 type = ELF64_R_TYPE(rel->r_info);
55
Furquan Shaikhc4f08f72014-07-23 13:42:22 -070056 /* Only these 6 relocations are expected to be found. */
Aaron Durbin785e47b2014-03-20 11:08:02 -050057 return (type == R_ARM_ABS32 || type == R_ARM_THM_PC22 ||
Furquan Shaikhc4f08f72014-07-23 13:42:22 -070058 type == R_ARM_THM_JUMP24 || type == R_ARM_V4BX ||
59 type == R_ARM_CALL || type == R_ARM_JUMP24);
Aaron Durbin785e47b2014-03-20 11:08:02 -050060}
61
Sol Boucher0e539312015-03-05 15:38:03 -080062static int should_emit_arm(Elf64_Rela *rel)
Aaron Durbin785e47b2014-03-20 11:08:02 -050063{
64 int type;
65
66 type = ELF64_R_TYPE(rel->r_info);
67
68 /* R_ARM_ABS32 relocations are absolute. Must emit these. */
69 return (type == R_ARM_ABS32);
70}
71
Sol Boucher0e539312015-03-05 15:38:03 -080072static int valid_reloc_aarch64(Elf64_Rela *rel)
Furquan Shaikhd2338ba2014-08-26 15:21:15 -070073{
74 int type;
75
76 type = ELF64_R_TYPE(rel->r_info);
77
78 return (type == R_AARCH64_ADR_PREL_PG_HI21 ||
79 type == R_AARCH64_ADD_ABS_LO12_NC ||
Furquan Shaikhde77e6a2014-11-21 15:41:10 -080080 type == R_AARCH64_LDST8_ABS_LO12_NC ||
Furquan Shaikh16c0a412015-06-08 11:58:04 -070081 type == R_AARCH64_CONDBR19 ||
Furquan Shaikhd2338ba2014-08-26 15:21:15 -070082 type == R_AARCH64_JUMP26 ||
83 type == R_AARCH64_LDST32_ABS_LO12_NC ||
Aaron Durbina47898e2014-09-18 13:39:16 -050084 type == R_AARCH64_LDST64_ABS_LO12_NC ||
Furquan Shaikhd2338ba2014-08-26 15:21:15 -070085 type == R_AARCH64_CALL26 ||
86 type == R_AARCH64_ABS64 ||
87 type == R_AARCH64_LD_PREL_LO19 ||
88 type == R_AARCH64_ADR_PREL_LO21);
89}
90
Sol Boucher0e539312015-03-05 15:38:03 -080091static int should_emit_aarch64(Elf64_Rela *rel)
Furquan Shaikhd2338ba2014-08-26 15:21:15 -070092{
93 int type;
94
95 type = ELF64_R_TYPE(rel->r_info);
96
97 return (type == R_AARCH64_ABS64);
98}
99
Aaron Durbinb39a9742015-09-08 17:24:04 -0500100static const struct arch_ops reloc_ops[] = {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600101 {
102 .arch = EM_386,
103 .valid_type = valid_reloc_386,
104 .should_emit = should_emit_386,
105 },
Aaron Durbin785e47b2014-03-20 11:08:02 -0500106 {
107 .arch = EM_ARM,
108 .valid_type = valid_reloc_arm,
109 .should_emit = should_emit_arm,
110 },
Furquan Shaikhd2338ba2014-08-26 15:21:15 -0700111 {
112 .arch = EM_AARCH64,
113 .valid_type = valid_reloc_aarch64,
114 .should_emit = should_emit_aarch64,
115 },
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600116};
117
118/*
119 * Relocation processing loops.
120 */
121
Aaron Durbinb39a9742015-09-08 17:24:04 -0500122static int for_each_reloc(struct rmod_context *ctx, struct reloc_filter *f,
123 int do_emit)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600124{
125 Elf64_Half i;
126 struct parsed_elf *pelf = &ctx->pelf;
127
128 for (i = 0; i < pelf->ehdr.e_shnum; i++) {
129 Elf64_Shdr *shdr;
130 Elf64_Rela *relocs;
131 Elf64_Xword nrelocs;
132 Elf64_Xword j;
133
134 relocs = pelf->relocs[i];
135
136 /* No relocations in this section. */
137 if (relocs == NULL)
138 continue;
139
140 shdr = &pelf->shdr[i];
141 nrelocs = shdr->sh_size / shdr->sh_entsize;
142
143 for (j = 0; j < nrelocs; j++) {
Aaron Durbinb39a9742015-09-08 17:24:04 -0500144 int filter_emit = 1;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600145 Elf64_Rela *r = &relocs[j];
146
Sol Boucher0e539312015-03-05 15:38:03 -0800147 if (!ctx->ops->valid_type(r)) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600148 ERROR("Invalid reloc type: %u\n",
149 (unsigned int)ELF64_R_TYPE(r->r_info));
150 return -1;
151 }
152
Aaron Durbinb39a9742015-09-08 17:24:04 -0500153 /* Allow the provided filter to have precedence. */
154 if (f != NULL) {
155 filter_emit = f->filter(f, r);
156
157 if (filter_emit < 0)
158 return filter_emit;
159 }
160
161 if (filter_emit && ctx->ops->should_emit(r)) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600162 int n = ctx->nrelocs;
163 if (do_emit)
164 ctx->emitted_relocs[n] = r->r_offset;
165 ctx->nrelocs++;
166 }
167 }
168 }
169
170 return 0;
171}
172
173static int find_program_segment(struct rmod_context *ctx)
174{
175 int i;
176 int nsegments;
177 struct parsed_elf *pelf;
Anatol Pomozov8cce7012015-07-10 17:30:01 -0700178 Elf64_Phdr *phdr = NULL;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600179
180 pelf = &ctx->pelf;
181
182 /* There should only be a single loadable segment. */
183 nsegments = 0;
184 for (i = 0; i < pelf->ehdr.e_phnum; i++) {
185 if (pelf->phdr[i].p_type != PT_LOAD)
186 continue;
187 phdr = &pelf->phdr[i];
188 nsegments++;
189 }
190
191 if (nsegments != 1) {
192 ERROR("Unexepcted number of loadable segments: %d.\n",
193 nsegments);
194 return -1;
195 }
196
197 INFO("Segment at 0x%0llx, file size 0x%0llx, mem size 0x%0llx.\n",
198 (long long)phdr->p_vaddr, (long long)phdr->p_filesz,
199 (long long)phdr->p_memsz);
200
201 ctx->phdr = phdr;
202
203 return 0;
204}
205
206static int
207filter_relocation_sections(struct rmod_context *ctx)
208{
209 int i;
210 const char *shstrtab;
211 struct parsed_elf *pelf;
212 const Elf64_Phdr *phdr;
213
214 pelf = &ctx->pelf;
215 phdr = ctx->phdr;
216 shstrtab = buffer_get(pelf->strtabs[pelf->ehdr.e_shstrndx]);
217
218 /*
219 * Find all relocation sections that contain relocation entries
220 * for sections that fall within the bounds of the segment. For
221 * easier processing the pointer to the relocation array for the
222 * sections that don't fall within the loadable program are NULL'd
223 * out.
224 */
225 for (i = 0; i < pelf->ehdr.e_shnum; i++) {
226 Elf64_Shdr *shdr;
227 Elf64_Word sh_info;
228 const char *section_name;
229
230 shdr = &pelf->shdr[i];
231
232 /* Ignore non-relocation sections. */
233 if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
234 continue;
235
236 /* Obtain section which relocations apply. */
237 sh_info = shdr->sh_info;
238 shdr = &pelf->shdr[sh_info];
239
240 section_name = &shstrtab[shdr->sh_name];
241 DEBUG("Relocation section found for '%s' section.\n",
242 section_name);
243
244 /* Do not process relocations for debug sections. */
245 if (strstr(section_name, ".debug") != NULL) {
246 pelf->relocs[i] = NULL;
247 continue;
248 }
249
250 /*
251 * If relocations apply to a non program section ignore the
252 * relocations for future processing.
253 */
254 if (shdr->sh_type != SHT_PROGBITS) {
255 pelf->relocs[i] = NULL;
256 continue;
257 }
258
259 if (shdr->sh_addr < phdr->p_vaddr ||
260 ((shdr->sh_addr + shdr->sh_size) >
261 (phdr->p_vaddr + phdr->p_memsz))) {
262 ERROR("Relocations being applied to section %d not "
263 "within segment region.\n", sh_info);
264 return -1;
265 }
266 }
267
268 return 0;
269}
270
271static int vaddr_cmp(const void *a, const void *b)
272{
273 const Elf64_Addr *pa = a;
274 const Elf64_Addr *pb = b;
275
276 if (*pa < *pb)
277 return -1;
278 if (*pa > *pb)
279 return 1;
280 return 0;
281}
282
Aaron Durbinb39a9742015-09-08 17:24:04 -0500283int rmodule_collect_relocations(struct rmod_context *ctx,
284 struct reloc_filter *f)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600285{
Sol Boucher0e539312015-03-05 15:38:03 -0800286 Elf64_Xword nrelocs;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600287
288 /*
289 * The relocs array in the pelf should only contain relocations that
290 * apply to the program. Count the number relocations. Then collect
291 * them into the allocated buffer.
292 */
Aaron Durbinb39a9742015-09-08 17:24:04 -0500293 if (for_each_reloc(ctx, f, 0))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600294 return -1;
295
296 nrelocs = ctx->nrelocs;
Sol Boucher0e539312015-03-05 15:38:03 -0800297 INFO("%" PRIu64 " relocations to be emitted.\n", nrelocs);
Furquan Shaikhb237c102014-08-26 14:59:36 -0700298 if (!nrelocs)
299 return 0;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600300
301 /* Reset the counter for indexing into the array. */
302 ctx->nrelocs = 0;
303 ctx->emitted_relocs = calloc(nrelocs, sizeof(Elf64_Addr));
304 /* Write out the relocations into the emitted_relocs array. */
Aaron Durbinb39a9742015-09-08 17:24:04 -0500305 if (for_each_reloc(ctx, f, 1))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600306 return -1;
307
308 if (ctx->nrelocs != nrelocs) {
309 ERROR("Mismatch counted and emitted relocations: %zu vs %zu.\n",
310 (size_t)nrelocs, (size_t)ctx->nrelocs);
311 return -1;
312 }
313
314 /* Sort the relocations by their address. */
315 qsort(ctx->emitted_relocs, nrelocs, sizeof(Elf64_Addr), vaddr_cmp);
316
317 return 0;
318}
319
320static int
321populate_sym(struct rmod_context *ctx, const char *sym_name, Elf64_Addr *addr,
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500322 int nsyms, const char *strtab, int optional)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600323{
324 int i;
325 Elf64_Sym *syms;
326
327 syms = ctx->pelf.syms;
328
329 for (i = 0; i < nsyms; i++) {
330 if (syms[i].st_name == 0)
331 continue;
332 if (strcmp(sym_name, &strtab[syms[i].st_name]))
333 continue;
334 DEBUG("%s -> 0x%llx\n", sym_name, (long long)syms[i].st_value);
335 *addr = syms[i].st_value;
336 return 0;
337 }
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500338
339 if (optional) {
340 DEBUG("optional symbol '%s' not found.\n", sym_name);
341 *addr = 0;
342 return 0;
343 }
344
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600345 ERROR("symbol '%s' not found.\n", sym_name);
346 return -1;
347}
348
Aaron Durbin051a1812015-09-08 15:52:01 -0500349static int populate_rmodule_info(struct rmod_context *ctx)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600350{
351 int i;
352 const char *strtab;
353 struct parsed_elf *pelf;
354 Elf64_Ehdr *ehdr;
355 int nsyms;
356
357 pelf = &ctx->pelf;
358 ehdr = &pelf->ehdr;
359
360 /* Obtain the string table. */
361 strtab = NULL;
362 for (i = 0; i < ehdr->e_shnum; i++) {
363 if (ctx->pelf.strtabs[i] == NULL)
364 continue;
365 /* Don't use the section headers' string table. */
366 if (i == ehdr->e_shstrndx)
367 continue;
368 strtab = buffer_get(ctx->pelf.strtabs[i]);
369 break;
370 }
371
372 if (strtab == NULL) {
373 ERROR("No string table found.\n");
374 return -1;
375 }
376
377 /* Determine number of symbols. */
378 nsyms = 0;
379 for (i = 0; i < ehdr->e_shnum; i++) {
380 if (pelf->shdr[i].sh_type != SHT_SYMTAB)
381 continue;
382
383 nsyms = pelf->shdr[i].sh_size / pelf->shdr[i].sh_entsize;
384 break;
385 }
386
Aaron Durbindde76292015-09-05 12:59:26 -0500387 if (populate_sym(ctx, "_rmodule_params", &ctx->parameters_begin,
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500388 nsyms, strtab, 1))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600389 return -1;
390
Aaron Durbindde76292015-09-05 12:59:26 -0500391 if (populate_sym(ctx, "_ermodule_params", &ctx->parameters_end,
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500392 nsyms, strtab, 1))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600393 return -1;
394
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500395 if (populate_sym(ctx, "_bss", &ctx->bss_begin, nsyms, strtab, 0))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600396 return -1;
397
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500398 if (populate_sym(ctx, "_ebss", &ctx->bss_end, nsyms, strtab, 0))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600399 return -1;
400
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600401 return 0;
402}
403
404static int
405add_section(struct elf_writer *ew, struct buffer *data, const char *name,
406 Elf64_Addr addr, Elf64_Word size)
407{
408 Elf64_Shdr shdr;
409 int ret;
410
411 memset(&shdr, 0, sizeof(shdr));
412 if (data != NULL) {
413 shdr.sh_type = SHT_PROGBITS;
414 shdr.sh_flags = SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR;
415 } else {
416 shdr.sh_type = SHT_NOBITS;
417 shdr.sh_flags = SHF_ALLOC;
418 }
419 shdr.sh_addr = addr;
420 shdr.sh_offset = addr;
421 shdr.sh_size = size;
422
423 ret = elf_writer_add_section(ew, &shdr, data, name);
424
425 if (ret)
426 ERROR("Could not add '%s' section.\n", name);
427
428 return ret;
429}
430
431static int
432write_elf(const struct rmod_context *ctx, const struct buffer *in,
433 struct buffer *out)
434{
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600435 int ret;
436 int bit64;
437 size_t loc;
438 size_t rmod_data_size;
439 struct elf_writer *ew;
440 struct buffer rmod_data;
441 struct buffer rmod_header;
442 struct buffer program;
443 struct buffer relocs;
444 Elf64_Xword total_size;
445 Elf64_Addr addr;
446 Elf64_Ehdr ehdr;
447
448 bit64 = ctx->pelf.ehdr.e_ident[EI_CLASS] == ELFCLASS64;
449
450 /*
451 * 3 sections will be added to the ELF file.
452 * +------------------+
453 * | rmodule header |
454 * +------------------+
455 * | program |
456 * +------------------+
457 * | relocations |
458 * +------------------+
459 */
460
461 /* Create buffer for header and relocations. */
462 rmod_data_size = sizeof(struct rmodule_header);
463 if (bit64)
464 rmod_data_size += ctx->nrelocs * sizeof(Elf64_Addr);
465 else
466 rmod_data_size += ctx->nrelocs * sizeof(Elf32_Addr);
467
468 if (buffer_create(&rmod_data, rmod_data_size, "rmod"))
469 return -1;
470
471 buffer_splice(&rmod_header, &rmod_data,
472 0, sizeof(struct rmodule_header));
473 buffer_clone(&relocs, &rmod_data);
474 buffer_seek(&relocs, sizeof(struct rmodule_header));
475
476 /* Reset current location. */
477 buffer_set_size(&rmod_header, 0);
478 buffer_set_size(&relocs, 0);
479
480 /* Program contents. */
481 buffer_splice(&program, in, ctx->phdr->p_offset, ctx->phdr->p_filesz);
482
483 /* Create ELF writer with modified entry point. */
484 memcpy(&ehdr, &ctx->pelf.ehdr, sizeof(ehdr));
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600485 ew = elf_writer_init(&ehdr);
486
487 if (ew == NULL) {
488 ERROR("Failed to create ELF writer.\n");
489 buffer_delete(&rmod_data);
490 return -1;
491 }
492
493 /* Write out rmodule_header. */
494 ctx->xdr->put16(&rmod_header, RMODULE_MAGIC);
495 ctx->xdr->put8(&rmod_header, RMODULE_VERSION_1);
496 ctx->xdr->put8(&rmod_header, 0);
497 /* payload_begin_offset */
498 loc = sizeof(struct rmodule_header);
499 ctx->xdr->put32(&rmod_header, loc);
500 /* payload_end_offset */
501 loc += ctx->phdr->p_filesz;
502 ctx->xdr->put32(&rmod_header, loc);
503 /* relocations_begin_offset */
504 ctx->xdr->put32(&rmod_header, loc);
505 /* relocations_end_offset */
506 if (bit64)
507 loc += ctx->nrelocs * sizeof(Elf64_Addr);
508 else
509 loc += ctx->nrelocs * sizeof(Elf32_Addr);
510 ctx->xdr->put32(&rmod_header, loc);
511 /* module_link_start_address */
Aaron Durbin051a1812015-09-08 15:52:01 -0500512 ctx->xdr->put32(&rmod_header, ctx->phdr->p_vaddr);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600513 /* module_program_size */
Aaron Durbin051a1812015-09-08 15:52:01 -0500514 ctx->xdr->put32(&rmod_header, ctx->phdr->p_memsz);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600515 /* module_entry_point */
Aaron Durbin051a1812015-09-08 15:52:01 -0500516 ctx->xdr->put32(&rmod_header, ctx->pelf.ehdr.e_entry);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600517 /* parameters_begin */
518 ctx->xdr->put32(&rmod_header, ctx->parameters_begin);
519 /* parameters_end */
520 ctx->xdr->put32(&rmod_header, ctx->parameters_end);
521 /* bss_begin */
522 ctx->xdr->put32(&rmod_header, ctx->bss_begin);
523 /* bss_end */
524 ctx->xdr->put32(&rmod_header, ctx->bss_end);
525 /* padding[4] */
526 ctx->xdr->put32(&rmod_header, 0);
527 ctx->xdr->put32(&rmod_header, 0);
528 ctx->xdr->put32(&rmod_header, 0);
529 ctx->xdr->put32(&rmod_header, 0);
530
531 /* Write the relocations. */
Sol Boucher0e539312015-03-05 15:38:03 -0800532 for (unsigned i = 0; i < ctx->nrelocs; i++) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600533 if (bit64)
534 ctx->xdr->put64(&relocs, ctx->emitted_relocs[i]);
535 else
536 ctx->xdr->put32(&relocs, ctx->emitted_relocs[i]);
537 }
538
539 total_size = 0;
540 addr = 0;
541
542 /*
543 * There are 2 cases to deal with. The program has a large NOBITS
544 * section and the relocations can fit entirely within occupied memory
545 * region for the program. The other is that the relocations increase
546 * the memory footprint of the program if it was loaded directly into
547 * the region it would run. The rmdoule header is a fixed cost that
548 * is considered a part of the program.
549 */
550 total_size += buffer_size(&rmod_header);
Aaron Durbin518a3222014-08-26 13:52:30 -0500551 if (buffer_size(&relocs) + ctx->phdr->p_filesz > ctx->phdr->p_memsz) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600552 total_size += buffer_size(&relocs);
553 total_size += ctx->phdr->p_filesz;
Aaron Durbin518a3222014-08-26 13:52:30 -0500554 } else {
555 total_size += ctx->phdr->p_memsz;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600556 }
557
558 ret = add_section(ew, &rmod_header, ".header", addr,
559 buffer_size(&rmod_header));
560 if (ret < 0)
561 goto out;
562 addr += buffer_size(&rmod_header);
563
564 ret = add_section(ew, &program, ".program", addr, ctx->phdr->p_filesz);
565 if (ret < 0)
566 goto out;
567 addr += ctx->phdr->p_filesz;
568
Furquan Shaikhb237c102014-08-26 14:59:36 -0700569 if (ctx->nrelocs) {
570 ret = add_section(ew, &relocs, ".relocs", addr,
571 buffer_size(&relocs));
572 if (ret < 0)
573 goto out;
574 addr += buffer_size(&relocs);
575 }
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600576
577 if (total_size != addr) {
578 ret = add_section(ew, NULL, ".empty", addr, total_size - addr);
579 if (ret < 0)
580 goto out;
581 }
582
583 /*
584 * Ensure last section has a memory usage that meets the required
585 * total size of the program in memory.
586 */
587
588 ret = elf_writer_serialize(ew, out);
589 if (ret < 0)
590 ERROR("Failed to serialize ELF to buffer.\n");
591
592out:
593 buffer_delete(&rmod_data);
594 elf_writer_destroy(ew);
595
596 return ret;
597}
598
Aaron Durbinb39a9742015-09-08 17:24:04 -0500599int rmodule_init(struct rmod_context *ctx, const struct buffer *elfin)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600600{
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600601 struct parsed_elf *pelf;
602 int i;
603 int ret;
604
605 ret = -1;
Aaron Durbin051a1812015-09-08 15:52:01 -0500606 memset(ctx, 0, sizeof(*ctx));
607 pelf = &ctx->pelf;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600608
609 if (parse_elf(elfin, pelf, ELF_PARSE_ALL)) {
610 ERROR("Couldn't parse ELF!\n");
611 return -1;
612 }
613
614 /* Only allow executables to be turned into rmodules. */
615 if (pelf->ehdr.e_type != ET_EXEC) {
616 ERROR("ELF is not an executable: %u.\n", pelf->ehdr.e_type);
617 goto out;
618 }
619
620 /* Determine if architecture is supported. */
621 for (i = 0; i < ARRAY_SIZE(reloc_ops); i++) {
622 if (reloc_ops[i].arch == pelf->ehdr.e_machine) {
Aaron Durbin051a1812015-09-08 15:52:01 -0500623 ctx->ops = &reloc_ops[i];
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600624 break;
625 }
626 }
627
Aaron Durbin051a1812015-09-08 15:52:01 -0500628 if (ctx->ops == NULL) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600629 ERROR("ELF is unsupported arch: %u.\n", pelf->ehdr.e_machine);
630 goto out;
631 }
632
633 /* Set the endian ops. */
Aaron Durbin051a1812015-09-08 15:52:01 -0500634 if (ctx->pelf.ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
635 ctx->xdr = &xdr_be;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600636 else
Aaron Durbin051a1812015-09-08 15:52:01 -0500637 ctx->xdr = &xdr_le;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600638
Aaron Durbin051a1812015-09-08 15:52:01 -0500639 if (find_program_segment(ctx))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600640 goto out;
641
Aaron Durbin051a1812015-09-08 15:52:01 -0500642 if (filter_relocation_sections(ctx))
643 goto out;
644
645 ret = 0;
646
647out:
648 return ret;
649}
650
Aaron Durbinb39a9742015-09-08 17:24:04 -0500651void rmodule_cleanup(struct rmod_context *ctx)
Aaron Durbin051a1812015-09-08 15:52:01 -0500652{
653 free(ctx->emitted_relocs);
654 parsed_elf_destroy(&ctx->pelf);
655}
656
657int rmodule_create(const struct buffer *elfin, struct buffer *elfout)
658{
659 struct rmod_context ctx;
660 int ret = -1;
661
662 if (rmodule_init(&ctx, elfin))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600663 goto out;
664
Aaron Durbinb39a9742015-09-08 17:24:04 -0500665 if (rmodule_collect_relocations(&ctx, NULL))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600666 goto out;
667
Aaron Durbin051a1812015-09-08 15:52:01 -0500668 if (populate_rmodule_info(&ctx))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600669 goto out;
670
671 if (write_elf(&ctx, elfin, elfout))
672 goto out;
673
674 ret = 0;
675
676out:
Aaron Durbin051a1812015-09-08 15:52:01 -0500677 rmodule_cleanup(&ctx);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600678 return ret;
679}