blob: 4ac2951f72ca0048c244326e12b8c0e34a5212aa [file] [log] [blame]
Patrick Georgi7333a112020-05-08 20:48:04 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin4fde5a62014-03-07 15:11:53 -06002
Sol Boucher0e539312015-03-05 15:38:03 -08003#include <inttypes.h>
Aaron Durbin4fde5a62014-03-07 15:11:53 -06004#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
8#include "elfparsing.h"
9#include "rmodule.h"
Aaron Durbindc9f5cd2015-09-08 13:34:43 -050010#include <commonlib/rmodule-defs.h>
Aaron Durbin4fde5a62014-03-07 15:11:53 -060011
Aaron Durbin4fde5a62014-03-07 15:11:53 -060012/*
13 * Architecture specific support operations.
14 */
Sol Boucher0e539312015-03-05 15:38:03 -080015static int valid_reloc_386(Elf64_Rela *rel)
Aaron Durbin4fde5a62014-03-07 15:11:53 -060016{
17 int type;
18
19 type = ELF64_R_TYPE(rel->r_info);
20
21 /* Only these 2 relocations are expected to be found. */
22 return (type == R_386_32 || type == R_386_PC32);
23}
24
Sol Boucher0e539312015-03-05 15:38:03 -080025static int should_emit_386(Elf64_Rela *rel)
Aaron Durbin4fde5a62014-03-07 15:11:53 -060026{
27 int type;
28
29 type = ELF64_R_TYPE(rel->r_info);
30
31 /* R_386_32 relocations are absolute. Must emit these. */
32 return (type == R_386_32);
33}
34
Patrick Rudolph565bebe2018-11-26 15:54:21 +010035static int valid_reloc_amd64(Elf64_Rela *rel)
36{
37 int type;
38
39 type = ELF64_R_TYPE(rel->r_info);
40
Patrick Rudolph44b4ec72019-02-15 14:41:20 +010041 /* Only these 6 relocations are expected to be found. */
Patrick Rudolph565bebe2018-11-26 15:54:21 +010042 return (type == R_AMD64_64 ||
43 type == R_AMD64_PC64 ||
44 type == R_AMD64_32S ||
45 type == R_AMD64_32 ||
Patrick Rudolph44b4ec72019-02-15 14:41:20 +010046 type == R_AMD64_PC32 ||
47 /*
48 * binutils 2.31 introduced R_AMD64_PLT32 for non local
49 * functions. As we don't care about procedure linkage
50 * table entries handle it as R_X86_64_PC32.
51 */
52 type == R_AMD64_PLT32);
Patrick Rudolph565bebe2018-11-26 15:54:21 +010053}
54
55static int should_emit_amd64(Elf64_Rela *rel)
56{
57 int type;
58
59 type = ELF64_R_TYPE(rel->r_info);
60
61 /* Only emit absolute relocations */
62 return (type == R_AMD64_64 ||
Patrick Rudolph565bebe2018-11-26 15:54:21 +010063 type == R_AMD64_32S ||
64 type == R_AMD64_32);
65}
66
Sol Boucher0e539312015-03-05 15:38:03 -080067static int valid_reloc_arm(Elf64_Rela *rel)
Aaron Durbin785e47b2014-03-20 11:08:02 -050068{
69 int type;
70
71 type = ELF64_R_TYPE(rel->r_info);
72
Furquan Shaikhc4f08f72014-07-23 13:42:22 -070073 /* Only these 6 relocations are expected to be found. */
Aaron Durbin785e47b2014-03-20 11:08:02 -050074 return (type == R_ARM_ABS32 || type == R_ARM_THM_PC22 ||
Julius Werner84446e62021-02-12 17:37:27 -080075 type == R_ARM_THM_JUMP24 || type == R_ARM_V4BX ||
Furquan Shaikhc4f08f72014-07-23 13:42:22 -070076 type == R_ARM_CALL || type == R_ARM_JUMP24);
Aaron Durbin785e47b2014-03-20 11:08:02 -050077}
78
Sol Boucher0e539312015-03-05 15:38:03 -080079static int should_emit_arm(Elf64_Rela *rel)
Aaron Durbin785e47b2014-03-20 11:08:02 -050080{
81 int type;
82
83 type = ELF64_R_TYPE(rel->r_info);
84
85 /* R_ARM_ABS32 relocations are absolute. Must emit these. */
86 return (type == R_ARM_ABS32);
87}
88
Sol Boucher0e539312015-03-05 15:38:03 -080089static int valid_reloc_aarch64(Elf64_Rela *rel)
Furquan Shaikhd2338ba2014-08-26 15:21:15 -070090{
91 int type;
92
93 type = ELF64_R_TYPE(rel->r_info);
94
95 return (type == R_AARCH64_ADR_PREL_PG_HI21 ||
96 type == R_AARCH64_ADD_ABS_LO12_NC ||
Furquan Shaikhde77e6a2014-11-21 15:41:10 -080097 type == R_AARCH64_LDST8_ABS_LO12_NC ||
Furquan Shaikh16c0a412015-06-08 11:58:04 -070098 type == R_AARCH64_CONDBR19 ||
Furquan Shaikhd2338ba2014-08-26 15:21:15 -070099 type == R_AARCH64_JUMP26 ||
100 type == R_AARCH64_LDST32_ABS_LO12_NC ||
Aaron Durbina47898e2014-09-18 13:39:16 -0500101 type == R_AARCH64_LDST64_ABS_LO12_NC ||
Furquan Shaikhd2338ba2014-08-26 15:21:15 -0700102 type == R_AARCH64_CALL26 ||
103 type == R_AARCH64_ABS64 ||
104 type == R_AARCH64_LD_PREL_LO19 ||
105 type == R_AARCH64_ADR_PREL_LO21);
106}
107
Sol Boucher0e539312015-03-05 15:38:03 -0800108static int should_emit_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_ABS64);
115}
116
Aaron Durbinb39a9742015-09-08 17:24:04 -0500117static const struct arch_ops reloc_ops[] = {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600118 {
119 .arch = EM_386,
120 .valid_type = valid_reloc_386,
121 .should_emit = should_emit_386,
122 },
Aaron Durbin785e47b2014-03-20 11:08:02 -0500123 {
Patrick Rudolph565bebe2018-11-26 15:54:21 +0100124 .arch = EM_X86_64,
125 .valid_type = valid_reloc_amd64,
126 .should_emit = should_emit_amd64,
127 },
128 {
Aaron Durbin785e47b2014-03-20 11:08:02 -0500129 .arch = EM_ARM,
130 .valid_type = valid_reloc_arm,
131 .should_emit = should_emit_arm,
132 },
Furquan Shaikhd2338ba2014-08-26 15:21:15 -0700133 {
134 .arch = EM_AARCH64,
135 .valid_type = valid_reloc_aarch64,
136 .should_emit = should_emit_aarch64,
137 },
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600138};
139
Julius Werner84446e62021-02-12 17:37:27 -0800140static int relocation_for_absolute_symbol(struct rmod_context *ctx, Elf64_Rela *r)
141{
142 Elf64_Sym *s = &ctx->pelf.syms[ELF64_R_SYM(r->r_info)];
143
144 if (s->st_shndx == SHN_ABS) {
145 DEBUG("Omitting relocation for absolute symbol: %s\n",
146 &ctx->strtab[s->st_name]);
147 return 1;
148 }
149
150 return 0;
151}
152
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600153/*
154 * Relocation processing loops.
155 */
156
Aaron Durbinb39a9742015-09-08 17:24:04 -0500157static int for_each_reloc(struct rmod_context *ctx, struct reloc_filter *f,
158 int do_emit)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600159{
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++) {
Aaron Durbinb39a9742015-09-08 17:24:04 -0500179 int filter_emit = 1;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600180 Elf64_Rela *r = &relocs[j];
181
Sol Boucher0e539312015-03-05 15:38:03 -0800182 if (!ctx->ops->valid_type(r)) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600183 ERROR("Invalid reloc type: %u\n",
184 (unsigned int)ELF64_R_TYPE(r->r_info));
185 return -1;
186 }
187
Julius Werner84446e62021-02-12 17:37:27 -0800188 if (relocation_for_absolute_symbol(ctx, r))
189 continue;
190
Aaron Durbinb39a9742015-09-08 17:24:04 -0500191 /* Allow the provided filter to have precedence. */
192 if (f != NULL) {
193 filter_emit = f->filter(f, r);
194
195 if (filter_emit < 0)
196 return filter_emit;
197 }
198
199 if (filter_emit && ctx->ops->should_emit(r)) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600200 int n = ctx->nrelocs;
201 if (do_emit)
202 ctx->emitted_relocs[n] = r->r_offset;
203 ctx->nrelocs++;
204 }
205 }
206 }
207
208 return 0;
209}
210
211static int find_program_segment(struct rmod_context *ctx)
212{
213 int i;
214 int nsegments;
215 struct parsed_elf *pelf;
Anatol Pomozov8cce7012015-07-10 17:30:01 -0700216 Elf64_Phdr *phdr = NULL;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600217
218 pelf = &ctx->pelf;
219
220 /* There should only be a single loadable segment. */
221 nsegments = 0;
222 for (i = 0; i < pelf->ehdr.e_phnum; i++) {
223 if (pelf->phdr[i].p_type != PT_LOAD)
224 continue;
225 phdr = &pelf->phdr[i];
226 nsegments++;
227 }
228
229 if (nsegments != 1) {
Patrick Georgi01cfecc2020-01-29 13:31:16 +0100230 ERROR("Unexpected number of loadable segments: %d.\n",
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600231 nsegments);
232 return -1;
233 }
234
235 INFO("Segment at 0x%0llx, file size 0x%0llx, mem size 0x%0llx.\n",
236 (long long)phdr->p_vaddr, (long long)phdr->p_filesz,
237 (long long)phdr->p_memsz);
238
239 ctx->phdr = phdr;
240
241 return 0;
242}
243
244static int
245filter_relocation_sections(struct rmod_context *ctx)
246{
247 int i;
248 const char *shstrtab;
249 struct parsed_elf *pelf;
250 const Elf64_Phdr *phdr;
251
252 pelf = &ctx->pelf;
253 phdr = ctx->phdr;
254 shstrtab = buffer_get(pelf->strtabs[pelf->ehdr.e_shstrndx]);
255
256 /*
257 * Find all relocation sections that contain relocation entries
258 * for sections that fall within the bounds of the segment. For
259 * easier processing the pointer to the relocation array for the
260 * sections that don't fall within the loadable program are NULL'd
261 * out.
262 */
263 for (i = 0; i < pelf->ehdr.e_shnum; i++) {
264 Elf64_Shdr *shdr;
265 Elf64_Word sh_info;
266 const char *section_name;
267
268 shdr = &pelf->shdr[i];
269
270 /* Ignore non-relocation sections. */
271 if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL)
272 continue;
273
274 /* Obtain section which relocations apply. */
275 sh_info = shdr->sh_info;
276 shdr = &pelf->shdr[sh_info];
277
278 section_name = &shstrtab[shdr->sh_name];
279 DEBUG("Relocation section found for '%s' section.\n",
280 section_name);
281
282 /* Do not process relocations for debug sections. */
283 if (strstr(section_name, ".debug") != NULL) {
284 pelf->relocs[i] = NULL;
285 continue;
286 }
287
288 /*
289 * If relocations apply to a non program section ignore the
290 * relocations for future processing.
291 */
292 if (shdr->sh_type != SHT_PROGBITS) {
293 pelf->relocs[i] = NULL;
294 continue;
295 }
296
297 if (shdr->sh_addr < phdr->p_vaddr ||
298 ((shdr->sh_addr + shdr->sh_size) >
299 (phdr->p_vaddr + phdr->p_memsz))) {
300 ERROR("Relocations being applied to section %d not "
301 "within segment region.\n", sh_info);
302 return -1;
303 }
304 }
305
306 return 0;
307}
308
309static int vaddr_cmp(const void *a, const void *b)
310{
311 const Elf64_Addr *pa = a;
312 const Elf64_Addr *pb = b;
313
314 if (*pa < *pb)
315 return -1;
316 if (*pa > *pb)
317 return 1;
318 return 0;
319}
320
Aaron Durbinb39a9742015-09-08 17:24:04 -0500321int rmodule_collect_relocations(struct rmod_context *ctx,
322 struct reloc_filter *f)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600323{
Sol Boucher0e539312015-03-05 15:38:03 -0800324 Elf64_Xword nrelocs;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600325
326 /*
327 * The relocs array in the pelf should only contain relocations that
328 * apply to the program. Count the number relocations. Then collect
329 * them into the allocated buffer.
330 */
Aaron Durbinb39a9742015-09-08 17:24:04 -0500331 if (for_each_reloc(ctx, f, 0))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600332 return -1;
333
334 nrelocs = ctx->nrelocs;
Sol Boucher0e539312015-03-05 15:38:03 -0800335 INFO("%" PRIu64 " relocations to be emitted.\n", nrelocs);
Furquan Shaikhb237c102014-08-26 14:59:36 -0700336 if (!nrelocs)
337 return 0;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600338
339 /* Reset the counter for indexing into the array. */
340 ctx->nrelocs = 0;
341 ctx->emitted_relocs = calloc(nrelocs, sizeof(Elf64_Addr));
342 /* Write out the relocations into the emitted_relocs array. */
Aaron Durbinb39a9742015-09-08 17:24:04 -0500343 if (for_each_reloc(ctx, f, 1))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600344 return -1;
345
346 if (ctx->nrelocs != nrelocs) {
347 ERROR("Mismatch counted and emitted relocations: %zu vs %zu.\n",
348 (size_t)nrelocs, (size_t)ctx->nrelocs);
349 return -1;
350 }
351
352 /* Sort the relocations by their address. */
353 qsort(ctx->emitted_relocs, nrelocs, sizeof(Elf64_Addr), vaddr_cmp);
354
355 return 0;
356}
357
358static int
359populate_sym(struct rmod_context *ctx, const char *sym_name, Elf64_Addr *addr,
Julius Werner84446e62021-02-12 17:37:27 -0800360 int nsyms, int optional)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600361{
362 int i;
363 Elf64_Sym *syms;
364
365 syms = ctx->pelf.syms;
366
367 for (i = 0; i < nsyms; i++) {
368 if (syms[i].st_name == 0)
369 continue;
Julius Werner84446e62021-02-12 17:37:27 -0800370 if (strcmp(sym_name, &ctx->strtab[syms[i].st_name]))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600371 continue;
372 DEBUG("%s -> 0x%llx\n", sym_name, (long long)syms[i].st_value);
373 *addr = syms[i].st_value;
374 return 0;
375 }
Aaron Durbinc9b053d2015-09-06 10:39:10 -0500376
377 if (optional) {
378 DEBUG("optional symbol '%s' not found.\n", sym_name);
379 *addr = 0;
380 return 0;
381 }
382
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600383 ERROR("symbol '%s' not found.\n", sym_name);
384 return -1;
385}
386
Aaron Durbin051a1812015-09-08 15:52:01 -0500387static int populate_rmodule_info(struct rmod_context *ctx)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600388{
389 int i;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600390 struct parsed_elf *pelf;
391 Elf64_Ehdr *ehdr;
392 int nsyms;
393
394 pelf = &ctx->pelf;
395 ehdr = &pelf->ehdr;
396
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600397 /* Determine number of symbols. */
398 nsyms = 0;
399 for (i = 0; i < ehdr->e_shnum; i++) {
400 if (pelf->shdr[i].sh_type != SHT_SYMTAB)
401 continue;
402
403 nsyms = pelf->shdr[i].sh_size / pelf->shdr[i].sh_entsize;
404 break;
405 }
406
Julius Werner84446e62021-02-12 17:37:27 -0800407 if (populate_sym(ctx, "_rmodule_params", &ctx->parameters_begin, nsyms, 1))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600408 return -1;
409
Julius Werner84446e62021-02-12 17:37:27 -0800410 if (populate_sym(ctx, "_ermodule_params", &ctx->parameters_end, nsyms, 1))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600411 return -1;
412
Julius Werner84446e62021-02-12 17:37:27 -0800413 if (populate_sym(ctx, "_bss", &ctx->bss_begin, nsyms, 0))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600414 return -1;
415
Julius Werner84446e62021-02-12 17:37:27 -0800416 if (populate_sym(ctx, "_ebss", &ctx->bss_end, nsyms, 0))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600417 return -1;
418
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600419 return 0;
420}
421
422static int
423add_section(struct elf_writer *ew, struct buffer *data, const char *name,
Julius Werner84446e62021-02-12 17:37:27 -0800424 Elf64_Addr addr, Elf64_Word size)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600425{
426 Elf64_Shdr shdr;
427 int ret;
428
429 memset(&shdr, 0, sizeof(shdr));
430 if (data != NULL) {
431 shdr.sh_type = SHT_PROGBITS;
432 shdr.sh_flags = SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR;
433 } else {
434 shdr.sh_type = SHT_NOBITS;
435 shdr.sh_flags = SHF_ALLOC;
436 }
437 shdr.sh_addr = addr;
438 shdr.sh_offset = addr;
439 shdr.sh_size = size;
440
441 ret = elf_writer_add_section(ew, &shdr, data, name);
442
443 if (ret)
444 ERROR("Could not add '%s' section.\n", name);
445
446 return ret;
447}
448
449static int
450write_elf(const struct rmod_context *ctx, const struct buffer *in,
Julius Werner84446e62021-02-12 17:37:27 -0800451 struct buffer *out)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600452{
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600453 int ret;
454 int bit64;
455 size_t loc;
456 size_t rmod_data_size;
457 struct elf_writer *ew;
458 struct buffer rmod_data;
459 struct buffer rmod_header;
460 struct buffer program;
461 struct buffer relocs;
462 Elf64_Xword total_size;
463 Elf64_Addr addr;
464 Elf64_Ehdr ehdr;
465
466 bit64 = ctx->pelf.ehdr.e_ident[EI_CLASS] == ELFCLASS64;
467
468 /*
469 * 3 sections will be added to the ELF file.
470 * +------------------+
471 * | rmodule header |
472 * +------------------+
473 * | program |
474 * +------------------+
475 * | relocations |
476 * +------------------+
477 */
478
479 /* Create buffer for header and relocations. */
480 rmod_data_size = sizeof(struct rmodule_header);
481 if (bit64)
482 rmod_data_size += ctx->nrelocs * sizeof(Elf64_Addr);
483 else
484 rmod_data_size += ctx->nrelocs * sizeof(Elf32_Addr);
485
486 if (buffer_create(&rmod_data, rmod_data_size, "rmod"))
487 return -1;
488
489 buffer_splice(&rmod_header, &rmod_data,
490 0, sizeof(struct rmodule_header));
491 buffer_clone(&relocs, &rmod_data);
492 buffer_seek(&relocs, sizeof(struct rmodule_header));
493
494 /* Reset current location. */
495 buffer_set_size(&rmod_header, 0);
496 buffer_set_size(&relocs, 0);
497
498 /* Program contents. */
499 buffer_splice(&program, in, ctx->phdr->p_offset, ctx->phdr->p_filesz);
500
Julius Werner81dc20e2020-10-15 17:37:57 -0700501 /* Create ELF writer. Set entry point to 0 to match section offsets. */
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600502 memcpy(&ehdr, &ctx->pelf.ehdr, sizeof(ehdr));
Julius Werner81dc20e2020-10-15 17:37:57 -0700503 ehdr.e_entry = 0;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600504 ew = elf_writer_init(&ehdr);
505
506 if (ew == NULL) {
507 ERROR("Failed to create ELF writer.\n");
508 buffer_delete(&rmod_data);
509 return -1;
510 }
511
512 /* Write out rmodule_header. */
513 ctx->xdr->put16(&rmod_header, RMODULE_MAGIC);
514 ctx->xdr->put8(&rmod_header, RMODULE_VERSION_1);
515 ctx->xdr->put8(&rmod_header, 0);
516 /* payload_begin_offset */
517 loc = sizeof(struct rmodule_header);
518 ctx->xdr->put32(&rmod_header, loc);
519 /* payload_end_offset */
520 loc += ctx->phdr->p_filesz;
521 ctx->xdr->put32(&rmod_header, loc);
522 /* relocations_begin_offset */
523 ctx->xdr->put32(&rmod_header, loc);
524 /* relocations_end_offset */
525 if (bit64)
526 loc += ctx->nrelocs * sizeof(Elf64_Addr);
527 else
528 loc += ctx->nrelocs * sizeof(Elf32_Addr);
529 ctx->xdr->put32(&rmod_header, loc);
530 /* module_link_start_address */
Aaron Durbin051a1812015-09-08 15:52:01 -0500531 ctx->xdr->put32(&rmod_header, ctx->phdr->p_vaddr);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600532 /* module_program_size */
Aaron Durbin051a1812015-09-08 15:52:01 -0500533 ctx->xdr->put32(&rmod_header, ctx->phdr->p_memsz);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600534 /* module_entry_point */
Aaron Durbin051a1812015-09-08 15:52:01 -0500535 ctx->xdr->put32(&rmod_header, ctx->pelf.ehdr.e_entry);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600536 /* parameters_begin */
537 ctx->xdr->put32(&rmod_header, ctx->parameters_begin);
538 /* parameters_end */
539 ctx->xdr->put32(&rmod_header, ctx->parameters_end);
540 /* bss_begin */
541 ctx->xdr->put32(&rmod_header, ctx->bss_begin);
542 /* bss_end */
543 ctx->xdr->put32(&rmod_header, ctx->bss_end);
544 /* padding[4] */
545 ctx->xdr->put32(&rmod_header, 0);
546 ctx->xdr->put32(&rmod_header, 0);
547 ctx->xdr->put32(&rmod_header, 0);
548 ctx->xdr->put32(&rmod_header, 0);
549
550 /* Write the relocations. */
Sol Boucher0e539312015-03-05 15:38:03 -0800551 for (unsigned i = 0; i < ctx->nrelocs; i++) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600552 if (bit64)
553 ctx->xdr->put64(&relocs, ctx->emitted_relocs[i]);
554 else
555 ctx->xdr->put32(&relocs, ctx->emitted_relocs[i]);
556 }
557
558 total_size = 0;
559 addr = 0;
560
561 /*
562 * There are 2 cases to deal with. The program has a large NOBITS
563 * section and the relocations can fit entirely within occupied memory
564 * region for the program. The other is that the relocations increase
565 * the memory footprint of the program if it was loaded directly into
Frans Hendriks166cbde2018-11-22 14:21:12 +0100566 * the region it would run. The rmodule header is a fixed cost that
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600567 * is considered a part of the program.
568 */
569 total_size += buffer_size(&rmod_header);
Aaron Durbin518a3222014-08-26 13:52:30 -0500570 if (buffer_size(&relocs) + ctx->phdr->p_filesz > ctx->phdr->p_memsz) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600571 total_size += buffer_size(&relocs);
572 total_size += ctx->phdr->p_filesz;
Aaron Durbin518a3222014-08-26 13:52:30 -0500573 } else {
574 total_size += ctx->phdr->p_memsz;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600575 }
576
577 ret = add_section(ew, &rmod_header, ".header", addr,
578 buffer_size(&rmod_header));
579 if (ret < 0)
580 goto out;
581 addr += buffer_size(&rmod_header);
582
583 ret = add_section(ew, &program, ".program", addr, ctx->phdr->p_filesz);
584 if (ret < 0)
585 goto out;
586 addr += ctx->phdr->p_filesz;
587
Furquan Shaikhb237c102014-08-26 14:59:36 -0700588 if (ctx->nrelocs) {
589 ret = add_section(ew, &relocs, ".relocs", addr,
590 buffer_size(&relocs));
591 if (ret < 0)
592 goto out;
593 addr += buffer_size(&relocs);
594 }
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600595
596 if (total_size != addr) {
597 ret = add_section(ew, NULL, ".empty", addr, total_size - addr);
598 if (ret < 0)
599 goto out;
600 }
601
602 /*
603 * Ensure last section has a memory usage that meets the required
604 * total size of the program in memory.
605 */
606
607 ret = elf_writer_serialize(ew, out);
608 if (ret < 0)
609 ERROR("Failed to serialize ELF to buffer.\n");
610
611out:
612 buffer_delete(&rmod_data);
613 elf_writer_destroy(ew);
614
615 return ret;
616}
617
Aaron Durbinb39a9742015-09-08 17:24:04 -0500618int rmodule_init(struct rmod_context *ctx, const struct buffer *elfin)
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600619{
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600620 struct parsed_elf *pelf;
Furquan Shaikh161d2332016-05-26 14:41:02 -0700621 size_t i;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600622 int ret;
623
624 ret = -1;
Aaron Durbin051a1812015-09-08 15:52:01 -0500625 memset(ctx, 0, sizeof(*ctx));
626 pelf = &ctx->pelf;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600627
628 if (parse_elf(elfin, pelf, ELF_PARSE_ALL)) {
629 ERROR("Couldn't parse ELF!\n");
630 return -1;
631 }
632
633 /* Only allow executables to be turned into rmodules. */
634 if (pelf->ehdr.e_type != ET_EXEC) {
635 ERROR("ELF is not an executable: %u.\n", pelf->ehdr.e_type);
636 goto out;
637 }
638
639 /* Determine if architecture is supported. */
640 for (i = 0; i < ARRAY_SIZE(reloc_ops); i++) {
641 if (reloc_ops[i].arch == pelf->ehdr.e_machine) {
Aaron Durbin051a1812015-09-08 15:52:01 -0500642 ctx->ops = &reloc_ops[i];
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600643 break;
644 }
645 }
646
Aaron Durbin051a1812015-09-08 15:52:01 -0500647 if (ctx->ops == NULL) {
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600648 ERROR("ELF is unsupported arch: %u.\n", pelf->ehdr.e_machine);
649 goto out;
650 }
651
652 /* Set the endian ops. */
Aaron Durbin051a1812015-09-08 15:52:01 -0500653 if (ctx->pelf.ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
654 ctx->xdr = &xdr_be;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600655 else
Aaron Durbin051a1812015-09-08 15:52:01 -0500656 ctx->xdr = &xdr_le;
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600657
Julius Werner84446e62021-02-12 17:37:27 -0800658 /* Obtain the string table. */
659 for (i = 0; i < pelf->ehdr.e_shnum; i++) {
660 if (pelf->strtabs[i] == NULL)
661 continue;
662 /* Don't use the section headers' string table. */
663 if (i == pelf->ehdr.e_shstrndx)
664 continue;
665 ctx->strtab = buffer_get(pelf->strtabs[i]);
666 break;
667 }
668
669 if (ctx->strtab == NULL) {
670 ERROR("No string table found.\n");
671 return -1;
672 }
673
Aaron Durbin051a1812015-09-08 15:52:01 -0500674 if (find_program_segment(ctx))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600675 goto out;
676
Aaron Durbin051a1812015-09-08 15:52:01 -0500677 if (filter_relocation_sections(ctx))
678 goto out;
679
680 ret = 0;
681
682out:
683 return ret;
684}
685
Aaron Durbinb39a9742015-09-08 17:24:04 -0500686void rmodule_cleanup(struct rmod_context *ctx)
Aaron Durbin051a1812015-09-08 15:52:01 -0500687{
688 free(ctx->emitted_relocs);
689 parsed_elf_destroy(&ctx->pelf);
690}
691
692int rmodule_create(const struct buffer *elfin, struct buffer *elfout)
693{
694 struct rmod_context ctx;
695 int ret = -1;
696
697 if (rmodule_init(&ctx, elfin))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600698 goto out;
699
Aaron Durbinb39a9742015-09-08 17:24:04 -0500700 if (rmodule_collect_relocations(&ctx, NULL))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600701 goto out;
702
Aaron Durbin051a1812015-09-08 15:52:01 -0500703 if (populate_rmodule_info(&ctx))
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600704 goto out;
705
706 if (write_elf(&ctx, elfin, elfout))
707 goto out;
708
709 ret = 0;
710
711out:
Aaron Durbin051a1812015-09-08 15:52:01 -0500712 rmodule_cleanup(&ctx);
Aaron Durbin4fde5a62014-03-07 15:11:53 -0600713 return ret;
714}
Aaron Durbin694fd132015-10-28 11:39:34 -0500715
716static void rmod_deserialize(struct rmodule_header *rmod, struct buffer *buff,
717 struct xdr *xdr)
718{
719 rmod->magic = xdr->get16(buff);
720 rmod->version = xdr->get8(buff);
721 rmod->type = xdr->get8(buff);
722 rmod->payload_begin_offset = xdr->get32(buff);
723 rmod->payload_end_offset = xdr->get32(buff);
724 rmod->relocations_begin_offset = xdr->get32(buff);
725 rmod->relocations_end_offset = xdr->get32(buff);
726 rmod->module_link_start_address = xdr->get32(buff);
727 rmod->module_program_size = xdr->get32(buff);
728 rmod->module_entry_point = xdr->get32(buff);
729 rmod->parameters_begin = xdr->get32(buff);
730 rmod->parameters_end = xdr->get32(buff);
731 rmod->bss_begin = xdr->get32(buff);
732 rmod->bss_end = xdr->get32(buff);
733 rmod->padding[0] = xdr->get32(buff);
734 rmod->padding[1] = xdr->get32(buff);
735 rmod->padding[2] = xdr->get32(buff);
736 rmod->padding[3] = xdr->get32(buff);
737}
738
739int rmodule_stage_to_elf(Elf64_Ehdr *ehdr, struct buffer *buff)
740{
741 struct buffer reader;
742 struct buffer elf_out;
743 struct rmodule_header rmod;
744 struct xdr *xdr;
745 struct elf_writer *ew;
746 Elf64_Shdr shdr;
747 int bit64;
748 size_t payload_sz;
749 const char *section_name = ".program";
750 const size_t input_sz = buffer_size(buff);
751
752 buffer_clone(&reader, buff);
753
754 xdr = (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) ? &xdr_be : &xdr_le;
755 bit64 = ehdr->e_ident[EI_CLASS] == ELFCLASS64;
756
757 rmod_deserialize(&rmod, &reader, xdr);
758
759 /* Indicate that file is not an rmodule if initial checks fail. */
760 if (rmod.magic != RMODULE_MAGIC)
761 return 1;
762 if (rmod.version != RMODULE_VERSION_1)
763 return 1;
764
765 if (rmod.payload_begin_offset > input_sz ||
766 rmod.payload_end_offset > input_sz ||
767 rmod.relocations_begin_offset > input_sz ||
768 rmod.relocations_end_offset > input_sz) {
769 ERROR("Rmodule fields out of bounds.\n");
770 return -1;
771 }
772
773 ehdr->e_entry = rmod.module_entry_point;
774 ew = elf_writer_init(ehdr);
775
776 if (ew == NULL)
777 return -1;
778
779 payload_sz = rmod.payload_end_offset - rmod.payload_begin_offset;
780 memset(&shdr, 0, sizeof(shdr));
781 shdr.sh_type = SHT_PROGBITS;
782 shdr.sh_flags = SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR;
783 shdr.sh_addr = rmod.module_link_start_address;
784 shdr.sh_size = payload_sz;
785 buffer_splice(&reader, buff, rmod.payload_begin_offset, payload_sz);
786
787 if (elf_writer_add_section(ew, &shdr, &reader, section_name)) {
788 ERROR("Unable to add ELF section: %s\n", section_name);
789 elf_writer_destroy(ew);
790 return -1;
791 }
792
793 if (payload_sz != rmod.module_program_size) {
794 struct buffer b;
795
796 buffer_init(&b, NULL, NULL, 0);
797 memset(&shdr, 0, sizeof(shdr));
798 shdr.sh_type = SHT_NOBITS;
799 shdr.sh_flags = SHF_WRITE | SHF_ALLOC;
800 shdr.sh_addr = rmod.module_link_start_address + payload_sz;
801 shdr.sh_size = rmod.module_program_size - payload_sz;
802 if (elf_writer_add_section(ew, &shdr, &b, ".empty")) {
803 ERROR("Unable to add ELF section: .empty\n");
804 elf_writer_destroy(ew);
805 return -1;
806 }
807 }
808
809 /* Provide a section symbol so the relcoations can reference that. */
810 if (elf_writer_add_symbol(ew, section_name, section_name, shdr.sh_addr,
811 0, STB_LOCAL, STT_SECTION)) {
812 ERROR("Unable to add section symbol to ELF.\n");
813 elf_writer_destroy(ew);
814 return -1;
815 }
816
817 /* Add symbols for the parameters if they are non-zero. */
818 if (rmod.parameters_begin != rmod.parameters_end) {
819 int ret = 0;
820
821 ret |= elf_writer_add_symbol(ew, "_rmodule_params",
822 section_name,
823 rmod.parameters_begin, 0,
824 STB_GLOBAL, STT_NOTYPE);
825 ret |= elf_writer_add_symbol(ew, "_ermodule_params",
826 section_name,
827 rmod.parameters_end, 0,
828 STB_GLOBAL, STT_NOTYPE);
829
830 if (ret != 0) {
831 ERROR("Unable to add module params symbols to ELF\n");
832 elf_writer_destroy(ew);
833 return -1;
834 }
835 }
836
837 if (elf_writer_add_symbol(ew, "_bss", section_name, rmod.bss_begin, 0,
838 STB_GLOBAL, STT_NOTYPE) ||
839 elf_writer_add_symbol(ew, "_ebss", section_name, rmod.bss_end, 0,
840 STB_GLOBAL, STT_NOTYPE)) {
841 ERROR("Unable to add bss symbols to ELF\n");
842 elf_writer_destroy(ew);
843 return -1;
844 }
845
846 ssize_t relocs_sz = rmod.relocations_end_offset;
847 relocs_sz -= rmod.relocations_begin_offset;
848 buffer_splice(&reader, buff, rmod.relocations_begin_offset, relocs_sz);
849 while (relocs_sz > 0) {
850 Elf64_Addr addr;
851
852 if (bit64) {
853 relocs_sz -= sizeof(Elf64_Addr);
854 addr = xdr->get64(&reader);
855 } else {
856 relocs_sz -= sizeof(Elf32_Addr);
857 addr = xdr->get32(&reader);
858 }
859
860 /* Skip any relocations that are below the link address. */
861 if (addr < rmod.module_link_start_address)
862 continue;
863
864 if (elf_writer_add_rel(ew, section_name, addr)) {
865 ERROR("Relocation addition failure.\n");
866 elf_writer_destroy(ew);
867 return -1;
868 }
869 }
870
871 if (elf_writer_serialize(ew, &elf_out)) {
872 ERROR("ELF writer serialize failure.\n");
873 elf_writer_destroy(ew);
874 return -1;
875 }
876
877 elf_writer_destroy(ew);
878
879 /* Flip buffer with the created ELF one. */
880 buffer_delete(buff);
881 *buff = elf_out;
882
883 return 0;
884}