blob: ec0971e27ee27b32f0d0290f3d176a75b4c54fba [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
3#ifndef TOOL_RMODULE_H
4#define TOOL_RMODULE_H
5
Aaron Durbinb39a9742015-09-08 17:24:04 -05006#include "elfparsing.h"
Aaron Durbin4fde5a62014-03-07 15:11:53 -06007#include "common.h"
8
Aaron Durbinb39a9742015-09-08 17:24:04 -05009struct arch_ops {
10 int arch;
11 /* Determine if relocation is a valid type for the architecture. */
12 int (*valid_type)(Elf64_Rela *rel);
13 /* Determine if relocation should be emitted. */
14 int (*should_emit)(Elf64_Rela *rel);
15};
16
17/*
18 * The fields in rmod_context are read-only to the user. These are
19 * exposed for easy shareability.
20 */
21struct rmod_context {
22 /* Ops to process relocations. */
23 const struct arch_ops *ops;
24
25 /* endian conversion ops */
26 struct xdr *xdr;
27
Elyes HAOUAS3db01982018-08-23 18:08:20 +020028 /* Parsed ELF structure. */
Aaron Durbinb39a9742015-09-08 17:24:04 -050029 struct parsed_elf pelf;
30 /* Program segment. */
31 Elf64_Phdr *phdr;
Julius Werner84446e62021-02-12 17:37:27 -080032 /* Symbol string table. */
33 char *strtab;
Aaron Durbinb39a9742015-09-08 17:24:04 -050034
35 /* Collection of relocation addresses fixup in the module. */
36 Elf64_Xword nrelocs;
37 Elf64_Addr *emitted_relocs;
38
39 /* The following fields are addresses within the linked program. */
40 Elf64_Addr parameters_begin;
41 Elf64_Addr parameters_end;
42 Elf64_Addr bss_begin;
43 Elf64_Addr bss_end;
44};
45
46struct reloc_filter {
47 /* Return < 0 on error. 0 to ignore relocation and 1 to include
48 * relocation. */
49 int (*filter)(struct reloc_filter *f, const Elf64_Rela *r);
50 /* Pointer for filter provides */
51 void *context;
52};
53
Aaron Durbin4fde5a62014-03-07 15:11:53 -060054/*
55 * Parse an ELF file within the elfin buffer and fill in the elfout buffer
56 * with a created rmodule in ELF format. Return 0 on success, < 0 on error.
57 */
58int rmodule_create(const struct buffer *elfin, struct buffer *elfout);
59
Aaron Durbinb39a9742015-09-08 17:24:04 -050060/*
61 * Initialize an rmodule context from an ELF buffer. Returns 0 on scucess, < 0
62 * on error.
63 */
64int rmodule_init(struct rmod_context *ctx, const struct buffer *elfin);
65
66/*
67 * Collect all the relocations that apply to the program in
68 * nrelocs/emitted_relocs. One can optionally provide a reloc_filter object
69 * to help in relocation filtering. The filter function will be called twice:
70 * once for counting and once for emitting. The same response should be
71 * provided for each call. Returns 0 on success, < 0 on error.
72 */
73int rmodule_collect_relocations(struct rmod_context *c, struct reloc_filter *f);
74
Frans Hendriks166cbde2018-11-22 14:21:12 +010075/* Clean up the memory consumed by the rmodule context. */
Aaron Durbinb39a9742015-09-08 17:24:04 -050076void rmodule_cleanup(struct rmod_context *ctx);
77
Aaron Durbin694fd132015-10-28 11:39:34 -050078/*
79 * Create an ELF file from the passed in rmodule in the buffer. The buffer
80 * contents will be replaced with an ELF file. Returns 1 if buff doesn't
81 * contain an rmodule and < 0 on failure, 0 on success.
82 */
83int rmodule_stage_to_elf(Elf64_Ehdr *ehdr, struct buffer *buff);
84
Aaron Durbin4fde5a62014-03-07 15:11:53 -060085#endif /* TOOL_RMODULE_H */