blob: f270e3ec8feb727c542981174b4175ff6e6e2c4d [file] [log] [blame]
Aaron Durbin4fde5a62014-03-07 15:11:53 -06001/*
Frans Hendriks166cbde2018-11-22 14:21:12 +01002 * Copyright (C) 2014 Google, Inc.
3 * Copyright (C) 2018 Eltan B.V.
Aaron Durbin4fde5a62014-03-07 15:11:53 -06004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Aaron Durbin4fde5a62014-03-07 15:11:53 -060013 */
14
Sol Boucher0e539312015-03-05 15:38:03 -080015#include <inttypes.h>
Aaron Durbin4fde5a62014-03-07 15:11:53 -060016#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19
20#include "elfparsing.h"
21#include "rmodule.h"
Aaron Durbindc9f5cd2015-09-08 13:34:43 -050022#include <commonlib/rmodule-defs.h>
Aaron Durbin4fde5a62014-03-07 15:11:53 -060023
Aaron Durbin4fde5a62014-03-07 15:11:53 -060024/*
25 * Architecture specific support operations.
26 */
Sol Boucher0e539312015-03-05 15:38:03 -080027static int valid_reloc_386(Elf64_Rela *rel)
Aaron Durbin4fde5a62014-03-07 15:11:53 -060028{
29 int type;
30
31 type = ELF64_R_TYPE(rel->r_info);
32
33 /* Only these 2 relocations are expected to be found. */
34 return (type == R_386_32 || type == R_386_PC32);
35}
36
Sol Boucher0e539312015-03-05 15:38:03 -080037static int should_emit_386(Elf64_Rela *rel)
Aaron Durbin4fde5a62014-03-07 15:11:53 -060038{
39 int type;
40
41 type = ELF64_R_TYPE(rel->r_info);
42
43 /* R_386_32 relocations are absolute. Must emit these. */
44 return (type == R_386_32);
45}
46
Patrick Rudolph565bebe2018-11-26 15:54:21 +010047static int valid_reloc_amd64(Elf64_Rela *rel)
48{
49 int type;
50
51 type = ELF64_R_TYPE(rel->r_info);
52
53 /* Only these 5 relocations are expected to be found. */
54 return (type == R_AMD64_64 ||
55 type == R_AMD64_PC64 ||
56 type == R_AMD64_32S ||
57 type == R_AMD64_32 ||
58 type == R_AMD64_PC32);
59}
60
61static int should_emit_amd64(Elf64_Rela *rel)
62{
63 int type;
64
65 type = ELF64_R_TYPE(rel->r_info);
66
67 /* Only emit absolute relocations */
68 return (type == R_AMD64_64 ||
69 type == R_AMD64_PC64 ||
70 type == R_AMD64_32S ||
71 type == R_AMD64_32);
72}
73
Sol Boucher0e539312015-03-05 15:38:03 -080074static int valid_reloc_arm(Elf64_Rela *rel)
Aaron Durbin785e47b2014-03-20 11:08:02 -050075{
76 int type;
77
78 type = ELF64_R_TYPE(rel->r_info);
79
Furquan Shaikhc4f08f72014-07-23 13:42:22 -070080 /* Only these 6 relocations are expected to be found. */
Aaron Durbin785e47b2014-03-20 11:08:02 -050081 return (type == R_ARM_ABS32 || type == R_ARM_THM_PC22 ||
Furquan Shaikhc4f08f72014-07-23 13:42:22 -070082 type == R_ARM_THM_JUMP24 || type == R_ARM_V4BX ||
83 type == R_ARM_CALL || type == R_ARM_JUMP24);
Aaron Durbin785e47b2014-03-20 11:08:02 -050084}
85
Sol Boucher0e539312015-03-05 15:38:03 -080086static int should_emit_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
92 /* R_ARM_ABS32 relocations are absolute. Must emit these. */
93 return (type == R_ARM_ABS32);
94}
95
Sol Boucher0e539312015-03-05 15:38:03 -080096static int valid_reloc_aarch64(Elf64_Rela *rel)
Furquan Shaikhd2338ba2014-08-26 15:21:15 -070097{
98 int type;
99
100 type = ELF64_R_TYPE(rel->r_info);
101
102 return (type == R_AARCH64_ADR_PREL_PG_HI21 ||
103 type == R_AARCH64_ADD_ABS_LO12_NC ||
Furquan Shaikhde77e6a2014-11-21 15:41:10 -0800104 type == R_AARCH64_LDST8_ABS_LO12_NC ||
Furquan Shaikh16c0a412015-06-08 11:58:04 -0700105 type == R_AARCH64_CONDBR19 ||
Furquan Shaikhd2338ba2014-08-26 15:21:15 -0700106 type == R_AARCH64_JUMP26 ||
107 type == R_AARCH64_LDST32_ABS_LO12_NC ||
Aaron Durbina47898e2014-09-18 13:39:16 -0500108 type == R_AARCH64_LDST64_ABS_LO12_NC ||
Furquan Shaikhd2338ba2014-08-26 15:21:15 -0700109 type == R_AARCH64_CALL26 ||
110 type == R_AARCH64_ABS64 ||
111 type == R_AARCH64_LD_PREL_LO19 ||
112 type == R_AARCH64_ADR_PREL_LO21);
113}
114
Sol Boucher0e539312015-03-05 15:38:03 -0800115static int should_emit_aarch64(Elf64_Rela *rel)
Furquan Shaikhd2338ba2014-08-26 15:21:15 -0700116{
117 int type;
118
119 type = ELF64_R_TYPE(rel->r_info);
120
121 return (type == R_AARCH64_ABS64);
122}
123
Aaron Durbinb39a9742015-09-08 17:24:04 -0500124static const struct arch_ops reloc_ops[] = {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600125 {
126 .arch = EM_386,
127 .valid_type = valid_reloc_386,
128 .should_emit = should_emit_386,
129 },
Aaron Durbin785e47b2014-03-20 11:08:02 -0500130 {
Patrick Rudolph565bebe2018-11-26 15:54:21 +0100131 .arch = EM_X86_64,
132 .valid_type = valid_reloc_amd64,
133 .should_emit = should_emit_amd64,
134 },
135 {
Aaron Durbin785e47b2014-03-20 11:08:02 -0500136 .arch = EM_ARM,
137 .valid_type = valid_reloc_arm,
138 .should_emit = should_emit_arm,
139 },
Furquan Shaikhd2338ba2014-08-26 15:21:15 -0700140 {
141 .arch = EM_AARCH64,
142 .valid_type = valid_reloc_aarch64,
143 .should_emit = should_emit_aarch64,
144 },
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600145};
146
147/*
148 * Relocation processing loops.
149 */
150
Aaron Durbinb39a9742015-09-08 17:24:04 -0500151static int for_each_reloc(struct rmod_context *ctx, struct reloc_filter *f,
152 int do_emit)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600153{
154 Elf64_Half i;
155 struct parsed_elf *pelf = &ctx->pelf;
156
157 for (i = 0; i < pelf->ehdr.e_shnum; i++) {
158 Elf64_Shdr *shdr;
159 Elf64_Rela *relocs;
160 Elf64_Xword nrelocs;
161 Elf64_Xword j;
162
163 relocs = pelf->relocs[i];
164
165 /* No relocations in this section. */
166 if (relocs == NULL)
167 continue;
168
169 shdr = &pelf->shdr[i];
170 nrelocs = shdr->sh_size / shdr->sh_entsize;
171
172 for (j = 0; j < nrelocs; j++) {
Aaron Durbinb39a9742015-09-08 17:24:04 -0500173 int filter_emit = 1;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600174 Elf64_Rela *r = &relocs[j];
175
Sol Boucher0e539312015-03-05 15:38:03 -0800176 if (!ctx->ops->valid_type(r)) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600177 ERROR("Invalid reloc type: %u\n",
178 (unsigned int)ELF64_R_TYPE(r->r_info));
179 return -1;
180 }
181
Aaron Durbinb39a9742015-09-08 17:24:04 -0500182 /* Allow the provided filter to have precedence. */
183 if (f != NULL) {
184 filter_emit = f->filter(f, r);
185
186 if (filter_emit < 0)
187 return filter_emit;
188 }
189
190 if (filter_emit && ctx->ops->should_emit(r)) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600191 int n = ctx->nrelocs;
192 if (do_emit)
193 ctx->emitted_relocs[n] = r->r_offset;
194 ctx->nrelocs++;
195 }
196 }
197 }
198
199 return 0;
200}
201
202static int find_program_segment(struct rmod_context *ctx)
203{
204 int i;
205 int nsegments;
206 struct parsed_elf *pelf;
Anatol Pomozov8cce7012015-07-10 17:30:01 -0700207 Elf64_Phdr *phdr = NULL;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600208
209 pelf = &ctx->pelf;
210
211 /* There should only be a single loadable segment. */
212 nsegments = 0;
213 for (i = 0; i < pelf->ehdr.e_phnum; i++) {
214 if (pelf->phdr[i].p_type != PT_LOAD)
215 continue;
216 phdr = &pelf->phdr[i];
217 nsegments++;
218 }
219
220 if (nsegments != 1) {
221 ERROR("Unexepcted number of loadable segments: %d.\n",
222 nsegments);
223 return -1;
224 }
225
226 INFO("Segment at 0x%0llx, file size 0x%0llx, mem size 0x%0llx.\n",
227 (long long)phdr->p_vaddr, (long long)phdr->p_filesz,
228 (long long)phdr->p_memsz);
229
230 ctx->phdr = phdr;
231
232 return 0;
233}
234
235static int
236filter_relocation_sections(struct rmod_context *ctx)
237{
238 int i;
239 const char *shstrtab;
240 struct parsed_elf *pelf;
241 const Elf64_Phdr *phdr;
242
243 pelf = &ctx->pelf;
244 phdr = ctx->phdr;
245 shstrtab = buffer_get(pelf->strtabs[pelf->ehdr.e_shstrndx]);
246
247 /*
248 * Find all relocation sections that contain relocation entries
249 * for sections that fall within the bounds of the segment. For
250 * easier processing the pointer to the relocation array for the
251 * sections that don't fall within the loadable program are NULL'd
252 * out.
253 */
254 for (i = 0; i < pelf->ehdr.e_shnum; i++) {
255 Elf64_Shdr *shdr;
256 Elf64_Word sh_info;
257 const char *section_name;
258
259 shdr = &pelf->shdr[i];
260
261 /* Ignore non-relocation sections. */
262 if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
263 continue;
264
265 /* Obtain section which relocations apply. */
266 sh_info = shdr->sh_info;
267 shdr = &pelf->shdr[sh_info];
268
269 section_name = &shstrtab[shdr->sh_name];
270 DEBUG("Relocation section found for '%s' section.\n",
271 section_name);
272
273 /* Do not process relocations for debug sections. */
274 if (strstr(section_name, ".debug") != NULL) {
275 pelf->relocs[i] = NULL;
276 continue;
277 }
278
279 /*
280 * If relocations apply to a non program section ignore the
281 * relocations for future processing.
282 */
283 if (shdr->sh_type != SHT_PROGBITS) {
284 pelf->relocs[i] = NULL;
285 continue;
286 }
287
288 if (shdr->sh_addr < phdr->p_vaddr ||
289 ((shdr->sh_addr + shdr->sh_size) >
290 (phdr->p_vaddr + phdr->p_memsz))) {
291 ERROR("Relocations being applied to section %d not "
292 "within segment region.\n", sh_info);
293 return -1;
294 }
295 }
296
297 return 0;
298}
299
300static int vaddr_cmp(const void *a, const void *b)
301{
302 const Elf64_Addr *pa = a;
303 const Elf64_Addr *pb = b;
304
305 if (*pa < *pb)
306 return -1;
307 if (*pa > *pb)
308 return 1;
309 return 0;
310}
311
Aaron Durbinb39a9742015-09-08 17:24:04 -0500312int rmodule_collect_relocations(struct rmod_context *ctx,
313 struct reloc_filter *f)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600314{
Sol Boucher0e539312015-03-05 15:38:03 -0800315 Elf64_Xword nrelocs;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600316
317 /*
318 * The relocs array in the pelf should only contain relocations that
319 * apply to the program. Count the number relocations. Then collect
320 * them into the allocated buffer.
321 */
Aaron Durbinb39a9742015-09-08 17:24:04 -0500322 if (for_each_reloc(ctx, f, 0))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600323 return -1;
324
325 nrelocs = ctx->nrelocs;
Sol Boucher0e539312015-03-05 15:38:03 -0800326 INFO("%" PRIu64 " relocations to be emitted.\n", nrelocs);
Furquan Shaikhb237c102014-08-26 14:59:36 -0700327 if (!nrelocs)
328 return 0;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600329
330 /* Reset the counter for indexing into the array. */
331 ctx->nrelocs = 0;
332 ctx->emitted_relocs = calloc(nrelocs, sizeof(Elf64_Addr));
333 /* Write out the relocations into the emitted_relocs array. */
Aaron Durbinb39a9742015-09-08 17:24:04 -0500334 if (for_each_reloc(ctx, f, 1))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600335 return -1;
336
337 if (ctx->nrelocs != nrelocs) {
338 ERROR("Mismatch counted and emitted relocations: %zu vs %zu.\n",
339 (size_t)nrelocs, (size_t)ctx->nrelocs);
340 return -1;
341 }
342
343 /* Sort the relocations by their address. */
344 qsort(ctx->emitted_relocs, nrelocs, sizeof(Elf64_Addr), vaddr_cmp);
345
346 return 0;
347}
348
349static int
350populate_sym(struct rmod_context *ctx, const char *sym_name, Elf64_Addr *addr,
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500351 int nsyms, const char *strtab, int optional)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600352{
353 int i;
354 Elf64_Sym *syms;
355
356 syms = ctx->pelf.syms;
357
358 for (i = 0; i < nsyms; i++) {
359 if (syms[i].st_name == 0)
360 continue;
361 if (strcmp(sym_name, &strtab[syms[i].st_name]))
362 continue;
363 DEBUG("%s -> 0x%llx\n", sym_name, (long long)syms[i].st_value);
364 *addr = syms[i].st_value;
365 return 0;
366 }
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500367
368 if (optional) {
369 DEBUG("optional symbol '%s' not found.\n", sym_name);
370 *addr = 0;
371 return 0;
372 }
373
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600374 ERROR("symbol '%s' not found.\n", sym_name);
375 return -1;
376}
377
Aaron Durbin051a1812015-09-08 15:52:01 -0500378static int populate_rmodule_info(struct rmod_context *ctx)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600379{
380 int i;
381 const char *strtab;
382 struct parsed_elf *pelf;
383 Elf64_Ehdr *ehdr;
384 int nsyms;
385
386 pelf = &ctx->pelf;
387 ehdr = &pelf->ehdr;
388
389 /* Obtain the string table. */
390 strtab = NULL;
391 for (i = 0; i < ehdr->e_shnum; i++) {
392 if (ctx->pelf.strtabs[i] == NULL)
393 continue;
394 /* Don't use the section headers' string table. */
395 if (i == ehdr->e_shstrndx)
396 continue;
397 strtab = buffer_get(ctx->pelf.strtabs[i]);
398 break;
399 }
400
401 if (strtab == NULL) {
402 ERROR("No string table found.\n");
403 return -1;
404 }
405
406 /* Determine number of symbols. */
407 nsyms = 0;
408 for (i = 0; i < ehdr->e_shnum; i++) {
409 if (pelf->shdr[i].sh_type != SHT_SYMTAB)
410 continue;
411
412 nsyms = pelf->shdr[i].sh_size / pelf->shdr[i].sh_entsize;
413 break;
414 }
415
Aaron Durbindde76292015-09-05 12:59:26 -0500416 if (populate_sym(ctx, "_rmodule_params", &ctx->parameters_begin,
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500417 nsyms, strtab, 1))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600418 return -1;
419
Aaron Durbindde76292015-09-05 12:59:26 -0500420 if (populate_sym(ctx, "_ermodule_params", &ctx->parameters_end,
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500421 nsyms, strtab, 1))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600422 return -1;
423
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500424 if (populate_sym(ctx, "_bss", &ctx->bss_begin, nsyms, strtab, 0))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600425 return -1;
426
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500427 if (populate_sym(ctx, "_ebss", &ctx->bss_end, nsyms, strtab, 0))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600428 return -1;
429
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600430 return 0;
431}
432
433static int
434add_section(struct elf_writer *ew, struct buffer *data, const char *name,
435 Elf64_Addr addr, Elf64_Word size)
436{
437 Elf64_Shdr shdr;
438 int ret;
439
440 memset(&shdr, 0, sizeof(shdr));
441 if (data != NULL) {
442 shdr.sh_type = SHT_PROGBITS;
443 shdr.sh_flags = SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR;
444 } else {
445 shdr.sh_type = SHT_NOBITS;
446 shdr.sh_flags = SHF_ALLOC;
447 }
448 shdr.sh_addr = addr;
449 shdr.sh_offset = addr;
450 shdr.sh_size = size;
451
452 ret = elf_writer_add_section(ew, &shdr, data, name);
453
454 if (ret)
455 ERROR("Could not add '%s' section.\n", name);
456
457 return ret;
458}
459
460static int
461write_elf(const struct rmod_context *ctx, const struct buffer *in,
462 struct buffer *out)
463{
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600464 int ret;
465 int bit64;
466 size_t loc;
467 size_t rmod_data_size;
468 struct elf_writer *ew;
469 struct buffer rmod_data;
470 struct buffer rmod_header;
471 struct buffer program;
472 struct buffer relocs;
473 Elf64_Xword total_size;
474 Elf64_Addr addr;
475 Elf64_Ehdr ehdr;
476
477 bit64 = ctx->pelf.ehdr.e_ident[EI_CLASS] == ELFCLASS64;
478
479 /*
480 * 3 sections will be added to the ELF file.
481 * +------------------+
482 * | rmodule header |
483 * +------------------+
484 * | program |
485 * +------------------+
486 * | relocations |
487 * +------------------+
488 */
489
490 /* Create buffer for header and relocations. */
491 rmod_data_size = sizeof(struct rmodule_header);
492 if (bit64)
493 rmod_data_size += ctx->nrelocs * sizeof(Elf64_Addr);
494 else
495 rmod_data_size += ctx->nrelocs * sizeof(Elf32_Addr);
496
497 if (buffer_create(&rmod_data, rmod_data_size, "rmod"))
498 return -1;
499
500 buffer_splice(&rmod_header, &rmod_data,
501 0, sizeof(struct rmodule_header));
502 buffer_clone(&relocs, &rmod_data);
503 buffer_seek(&relocs, sizeof(struct rmodule_header));
504
505 /* Reset current location. */
506 buffer_set_size(&rmod_header, 0);
507 buffer_set_size(&relocs, 0);
508
509 /* Program contents. */
510 buffer_splice(&program, in, ctx->phdr->p_offset, ctx->phdr->p_filesz);
511
512 /* Create ELF writer with modified entry point. */
513 memcpy(&ehdr, &ctx->pelf.ehdr, sizeof(ehdr));
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600514 ew = elf_writer_init(&ehdr);
515
516 if (ew == NULL) {
517 ERROR("Failed to create ELF writer.\n");
518 buffer_delete(&rmod_data);
519 return -1;
520 }
521
522 /* Write out rmodule_header. */
523 ctx->xdr->put16(&rmod_header, RMODULE_MAGIC);
524 ctx->xdr->put8(&rmod_header, RMODULE_VERSION_1);
525 ctx->xdr->put8(&rmod_header, 0);
526 /* payload_begin_offset */
527 loc = sizeof(struct rmodule_header);
528 ctx->xdr->put32(&rmod_header, loc);
529 /* payload_end_offset */
530 loc += ctx->phdr->p_filesz;
531 ctx->xdr->put32(&rmod_header, loc);
532 /* relocations_begin_offset */
533 ctx->xdr->put32(&rmod_header, loc);
534 /* relocations_end_offset */
535 if (bit64)
536 loc += ctx->nrelocs * sizeof(Elf64_Addr);
537 else
538 loc += ctx->nrelocs * sizeof(Elf32_Addr);
539 ctx->xdr->put32(&rmod_header, loc);
540 /* module_link_start_address */
Aaron Durbin051a1812015-09-08 15:52:01 -0500541 ctx->xdr->put32(&rmod_header, ctx->phdr->p_vaddr);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600542 /* module_program_size */
Aaron Durbin051a1812015-09-08 15:52:01 -0500543 ctx->xdr->put32(&rmod_header, ctx->phdr->p_memsz);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600544 /* module_entry_point */
Aaron Durbin051a1812015-09-08 15:52:01 -0500545 ctx->xdr->put32(&rmod_header, ctx->pelf.ehdr.e_entry);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600546 /* parameters_begin */
547 ctx->xdr->put32(&rmod_header, ctx->parameters_begin);
548 /* parameters_end */
549 ctx->xdr->put32(&rmod_header, ctx->parameters_end);
550 /* bss_begin */
551 ctx->xdr->put32(&rmod_header, ctx->bss_begin);
552 /* bss_end */
553 ctx->xdr->put32(&rmod_header, ctx->bss_end);
554 /* padding[4] */
555 ctx->xdr->put32(&rmod_header, 0);
556 ctx->xdr->put32(&rmod_header, 0);
557 ctx->xdr->put32(&rmod_header, 0);
558 ctx->xdr->put32(&rmod_header, 0);
559
560 /* Write the relocations. */
Sol Boucher0e539312015-03-05 15:38:03 -0800561 for (unsigned i = 0; i < ctx->nrelocs; i++) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600562 if (bit64)
563 ctx->xdr->put64(&relocs, ctx->emitted_relocs[i]);
564 else
565 ctx->xdr->put32(&relocs, ctx->emitted_relocs[i]);
566 }
567
568 total_size = 0;
569 addr = 0;
570
571 /*
572 * There are 2 cases to deal with. The program has a large NOBITS
573 * section and the relocations can fit entirely within occupied memory
574 * region for the program. The other is that the relocations increase
575 * the memory footprint of the program if it was loaded directly into
Frans Hendriks166cbde2018-11-22 14:21:12 +0100576 * the region it would run. The rmodule header is a fixed cost that
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600577 * is considered a part of the program.
578 */
579 total_size += buffer_size(&rmod_header);
Aaron Durbin518a3222014-08-26 13:52:30 -0500580 if (buffer_size(&relocs) + ctx->phdr->p_filesz > ctx->phdr->p_memsz) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600581 total_size += buffer_size(&relocs);
582 total_size += ctx->phdr->p_filesz;
Aaron Durbin518a3222014-08-26 13:52:30 -0500583 } else {
584 total_size += ctx->phdr->p_memsz;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600585 }
586
587 ret = add_section(ew, &rmod_header, ".header", addr,
588 buffer_size(&rmod_header));
589 if (ret < 0)
590 goto out;
591 addr += buffer_size(&rmod_header);
592
593 ret = add_section(ew, &program, ".program", addr, ctx->phdr->p_filesz);
594 if (ret < 0)
595 goto out;
596 addr += ctx->phdr->p_filesz;
597
Furquan Shaikhb237c102014-08-26 14:59:36 -0700598 if (ctx->nrelocs) {
599 ret = add_section(ew, &relocs, ".relocs", addr,
600 buffer_size(&relocs));
601 if (ret < 0)
602 goto out;
603 addr += buffer_size(&relocs);
604 }
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600605
606 if (total_size != addr) {
607 ret = add_section(ew, NULL, ".empty", addr, total_size - addr);
608 if (ret < 0)
609 goto out;
610 }
611
612 /*
613 * Ensure last section has a memory usage that meets the required
614 * total size of the program in memory.
615 */
616
617 ret = elf_writer_serialize(ew, out);
618 if (ret < 0)
619 ERROR("Failed to serialize ELF to buffer.\n");
620
621out:
622 buffer_delete(&rmod_data);
623 elf_writer_destroy(ew);
624
625 return ret;
626}
627
Aaron Durbinb39a9742015-09-08 17:24:04 -0500628int rmodule_init(struct rmod_context *ctx, const struct buffer *elfin)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600629{
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600630 struct parsed_elf *pelf;
Furquan Shaikh161d2332016-05-26 14:41:02 -0700631 size_t i;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600632 int ret;
633
634 ret = -1;
Aaron Durbin051a1812015-09-08 15:52:01 -0500635 memset(ctx, 0, sizeof(*ctx));
636 pelf = &ctx->pelf;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600637
638 if (parse_elf(elfin, pelf, ELF_PARSE_ALL)) {
639 ERROR("Couldn't parse ELF!\n");
640 return -1;
641 }
642
643 /* Only allow executables to be turned into rmodules. */
644 if (pelf->ehdr.e_type != ET_EXEC) {
645 ERROR("ELF is not an executable: %u.\n", pelf->ehdr.e_type);
646 goto out;
647 }
648
649 /* Determine if architecture is supported. */
650 for (i = 0; i < ARRAY_SIZE(reloc_ops); i++) {
651 if (reloc_ops[i].arch == pelf->ehdr.e_machine) {
Aaron Durbin051a1812015-09-08 15:52:01 -0500652 ctx->ops = &reloc_ops[i];
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600653 break;
654 }
655 }
656
Aaron Durbin051a1812015-09-08 15:52:01 -0500657 if (ctx->ops == NULL) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600658 ERROR("ELF is unsupported arch: %u.\n", pelf->ehdr.e_machine);
659 goto out;
660 }
661
662 /* Set the endian ops. */
Aaron Durbin051a1812015-09-08 15:52:01 -0500663 if (ctx->pelf.ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
664 ctx->xdr = &xdr_be;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600665 else
Aaron Durbin051a1812015-09-08 15:52:01 -0500666 ctx->xdr = &xdr_le;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600667
Aaron Durbin051a1812015-09-08 15:52:01 -0500668 if (find_program_segment(ctx))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600669 goto out;
670
Aaron Durbin051a1812015-09-08 15:52:01 -0500671 if (filter_relocation_sections(ctx))
672 goto out;
673
674 ret = 0;
675
676out:
677 return ret;
678}
679
Aaron Durbinb39a9742015-09-08 17:24:04 -0500680void rmodule_cleanup(struct rmod_context *ctx)
Aaron Durbin051a1812015-09-08 15:52:01 -0500681{
682 free(ctx->emitted_relocs);
683 parsed_elf_destroy(&ctx->pelf);
684}
685
686int rmodule_create(const struct buffer *elfin, struct buffer *elfout)
687{
688 struct rmod_context ctx;
689 int ret = -1;
690
691 if (rmodule_init(&ctx, elfin))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600692 goto out;
693
Aaron Durbinb39a9742015-09-08 17:24:04 -0500694 if (rmodule_collect_relocations(&ctx, NULL))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600695 goto out;
696
Aaron Durbin051a1812015-09-08 15:52:01 -0500697 if (populate_rmodule_info(&ctx))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600698 goto out;
699
700 if (write_elf(&ctx, elfin, elfout))
701 goto out;
702
703 ret = 0;
704
705out:
Aaron Durbin051a1812015-09-08 15:52:01 -0500706 rmodule_cleanup(&ctx);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600707 return ret;
708}
Aaron Durbin694fd132015-10-28 11:39:34 -0500709
710static void rmod_deserialize(struct rmodule_header *rmod, struct buffer *buff,
711 struct xdr *xdr)
712{
713 rmod->magic = xdr->get16(buff);
714 rmod->version = xdr->get8(buff);
715 rmod->type = xdr->get8(buff);
716 rmod->payload_begin_offset = xdr->get32(buff);
717 rmod->payload_end_offset = xdr->get32(buff);
718 rmod->relocations_begin_offset = xdr->get32(buff);
719 rmod->relocations_end_offset = xdr->get32(buff);
720 rmod->module_link_start_address = xdr->get32(buff);
721 rmod->module_program_size = xdr->get32(buff);
722 rmod->module_entry_point = xdr->get32(buff);
723 rmod->parameters_begin = xdr->get32(buff);
724 rmod->parameters_end = xdr->get32(buff);
725 rmod->bss_begin = xdr->get32(buff);
726 rmod->bss_end = xdr->get32(buff);
727 rmod->padding[0] = xdr->get32(buff);
728 rmod->padding[1] = xdr->get32(buff);
729 rmod->padding[2] = xdr->get32(buff);
730 rmod->padding[3] = xdr->get32(buff);
731}
732
733int rmodule_stage_to_elf(Elf64_Ehdr *ehdr, struct buffer *buff)
734{
735 struct buffer reader;
736 struct buffer elf_out;
737 struct rmodule_header rmod;
738 struct xdr *xdr;
739 struct elf_writer *ew;
740 Elf64_Shdr shdr;
741 int bit64;
742 size_t payload_sz;
743 const char *section_name = ".program";
744 const size_t input_sz = buffer_size(buff);
745
746 buffer_clone(&reader, buff);
747
748 xdr = (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) ? &xdr_be : &xdr_le;
749 bit64 = ehdr->e_ident[EI_CLASS] == ELFCLASS64;
750
751 rmod_deserialize(&rmod, &reader, xdr);
752
753 /* Indicate that file is not an rmodule if initial checks fail. */
754 if (rmod.magic != RMODULE_MAGIC)
755 return 1;
756 if (rmod.version != RMODULE_VERSION_1)
757 return 1;
758
759 if (rmod.payload_begin_offset > input_sz ||
760 rmod.payload_end_offset > input_sz ||
761 rmod.relocations_begin_offset > input_sz ||
762 rmod.relocations_end_offset > input_sz) {
763 ERROR("Rmodule fields out of bounds.\n");
764 return -1;
765 }
766
767 ehdr->e_entry = rmod.module_entry_point;
768 ew = elf_writer_init(ehdr);
769
770 if (ew == NULL)
771 return -1;
772
773 payload_sz = rmod.payload_end_offset - rmod.payload_begin_offset;
774 memset(&shdr, 0, sizeof(shdr));
775 shdr.sh_type = SHT_PROGBITS;
776 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
777 shdr.sh_addr = rmod.module_link_start_address;
778 shdr.sh_size = payload_sz;
779 buffer_splice(&reader, buff, rmod.payload_begin_offset, payload_sz);
780
781 if (elf_writer_add_section(ew, &shdr, &reader, section_name)) {
782 ERROR("Unable to add ELF section: %s\n", section_name);
783 elf_writer_destroy(ew);
784 return -1;
785 }
786
787 if (payload_sz != rmod.module_program_size) {
788 struct buffer b;
789
790 buffer_init(&b, NULL, NULL, 0);
791 memset(&shdr, 0, sizeof(shdr));
792 shdr.sh_type = SHT_NOBITS;
793 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
794 shdr.sh_addr = rmod.module_link_start_address + payload_sz;
795 shdr.sh_size = rmod.module_program_size - payload_sz;
796 if (elf_writer_add_section(ew, &shdr, &b, ".empty")) {
797 ERROR("Unable to add ELF section: .empty\n");
798 elf_writer_destroy(ew);
799 return -1;
800 }
801 }
802
803 /* Provide a section symbol so the relcoations can reference that. */
804 if (elf_writer_add_symbol(ew, section_name, section_name, shdr.sh_addr,
805 0, STB_LOCAL, STT_SECTION)) {
806 ERROR("Unable to add section symbol to ELF.\n");
807 elf_writer_destroy(ew);
808 return -1;
809 }
810
811 /* Add symbols for the parameters if they are non-zero. */
812 if (rmod.parameters_begin != rmod.parameters_end) {
813 int ret = 0;
814
815 ret |= elf_writer_add_symbol(ew, "_rmodule_params",
816 section_name,
817 rmod.parameters_begin, 0,
818 STB_GLOBAL, STT_NOTYPE);
819 ret |= elf_writer_add_symbol(ew, "_ermodule_params",
820 section_name,
821 rmod.parameters_end, 0,
822 STB_GLOBAL, STT_NOTYPE);
823
824 if (ret != 0) {
825 ERROR("Unable to add module params symbols to ELF\n");
826 elf_writer_destroy(ew);
827 return -1;
828 }
829 }
830
831 if (elf_writer_add_symbol(ew, "_bss", section_name, rmod.bss_begin, 0,
832 STB_GLOBAL, STT_NOTYPE) ||
833 elf_writer_add_symbol(ew, "_ebss", section_name, rmod.bss_end, 0,
834 STB_GLOBAL, STT_NOTYPE)) {
835 ERROR("Unable to add bss symbols to ELF\n");
836 elf_writer_destroy(ew);
837 return -1;
838 }
839
840 ssize_t relocs_sz = rmod.relocations_end_offset;
841 relocs_sz -= rmod.relocations_begin_offset;
842 buffer_splice(&reader, buff, rmod.relocations_begin_offset, relocs_sz);
843 while (relocs_sz > 0) {
844 Elf64_Addr addr;
845
846 if (bit64) {
847 relocs_sz -= sizeof(Elf64_Addr);
848 addr = xdr->get64(&reader);
849 } else {
850 relocs_sz -= sizeof(Elf32_Addr);
851 addr = xdr->get32(&reader);
852 }
853
854 /* Skip any relocations that are below the link address. */
855 if (addr < rmod.module_link_start_address)
856 continue;
857
858 if (elf_writer_add_rel(ew, section_name, addr)) {
859 ERROR("Relocation addition failure.\n");
860 elf_writer_destroy(ew);
861 return -1;
862 }
863 }
864
865 if (elf_writer_serialize(ew, &elf_out)) {
866 ERROR("ELF writer serialize failure.\n");
867 elf_writer_destroy(ew);
868 return -1;
869 }
870
871 elf_writer_destroy(ew);
872
873 /* Flip buffer with the created ELF one. */
874 buffer_delete(buff);
875 *buff = elf_out;
876
877 return 0;
878}