Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 2 | |
| 3 | #ifndef TOOL_RMODULE_H |
| 4 | #define TOOL_RMODULE_H |
| 5 | |
Aaron Durbin | b39a974 | 2015-09-08 17:24:04 -0500 | [diff] [blame] | 6 | #include "elfparsing.h" |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 7 | #include "common.h" |
| 8 | |
Aaron Durbin | b39a974 | 2015-09-08 17:24:04 -0500 | [diff] [blame] | 9 | struct 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 | */ |
| 21 | struct rmod_context { |
| 22 | /* Ops to process relocations. */ |
| 23 | const struct arch_ops *ops; |
| 24 | |
| 25 | /* endian conversion ops */ |
| 26 | struct xdr *xdr; |
| 27 | |
Elyes HAOUAS | 3db0198 | 2018-08-23 18:08:20 +0200 | [diff] [blame] | 28 | /* Parsed ELF structure. */ |
Aaron Durbin | b39a974 | 2015-09-08 17:24:04 -0500 | [diff] [blame] | 29 | struct parsed_elf pelf; |
| 30 | /* Program segment. */ |
| 31 | Elf64_Phdr *phdr; |
Jeremy Compostella | 79f2e1f | 2023-08-30 15:35:46 -0700 | [diff] [blame] | 32 | /* Number of loadable segments. */ |
| 33 | size_t nsegments; |
Julius Werner | 84446e6 | 2021-02-12 17:37:27 -0800 | [diff] [blame] | 34 | /* Symbol string table. */ |
| 35 | char *strtab; |
Aaron Durbin | b39a974 | 2015-09-08 17:24:04 -0500 | [diff] [blame] | 36 | |
| 37 | /* Collection of relocation addresses fixup in the module. */ |
| 38 | Elf64_Xword nrelocs; |
| 39 | Elf64_Addr *emitted_relocs; |
| 40 | |
| 41 | /* The following fields are addresses within the linked program. */ |
| 42 | Elf64_Addr parameters_begin; |
| 43 | Elf64_Addr parameters_end; |
| 44 | Elf64_Addr bss_begin; |
| 45 | Elf64_Addr bss_end; |
| 46 | }; |
| 47 | |
| 48 | struct reloc_filter { |
| 49 | /* Return < 0 on error. 0 to ignore relocation and 1 to include |
| 50 | * relocation. */ |
| 51 | int (*filter)(struct reloc_filter *f, const Elf64_Rela *r); |
| 52 | /* Pointer for filter provides */ |
| 53 | void *context; |
| 54 | }; |
| 55 | |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 56 | /* |
| 57 | * Parse an ELF file within the elfin buffer and fill in the elfout buffer |
| 58 | * with a created rmodule in ELF format. Return 0 on success, < 0 on error. |
| 59 | */ |
| 60 | int rmodule_create(const struct buffer *elfin, struct buffer *elfout); |
| 61 | |
Aaron Durbin | b39a974 | 2015-09-08 17:24:04 -0500 | [diff] [blame] | 62 | /* |
| 63 | * Initialize an rmodule context from an ELF buffer. Returns 0 on scucess, < 0 |
| 64 | * on error. |
| 65 | */ |
| 66 | int rmodule_init(struct rmod_context *ctx, const struct buffer *elfin); |
| 67 | |
| 68 | /* |
| 69 | * Collect all the relocations that apply to the program in |
| 70 | * nrelocs/emitted_relocs. One can optionally provide a reloc_filter object |
| 71 | * to help in relocation filtering. The filter function will be called twice: |
| 72 | * once for counting and once for emitting. The same response should be |
| 73 | * provided for each call. Returns 0 on success, < 0 on error. |
| 74 | */ |
| 75 | int rmodule_collect_relocations(struct rmod_context *c, struct reloc_filter *f); |
| 76 | |
Frans Hendriks | 166cbde | 2018-11-22 14:21:12 +0100 | [diff] [blame] | 77 | /* Clean up the memory consumed by the rmodule context. */ |
Aaron Durbin | b39a974 | 2015-09-08 17:24:04 -0500 | [diff] [blame] | 78 | void rmodule_cleanup(struct rmod_context *ctx); |
| 79 | |
Aaron Durbin | 694fd13 | 2015-10-28 11:39:34 -0500 | [diff] [blame] | 80 | /* |
| 81 | * Create an ELF file from the passed in rmodule in the buffer. The buffer |
| 82 | * contents will be replaced with an ELF file. Returns 1 if buff doesn't |
| 83 | * contain an rmodule and < 0 on failure, 0 on success. |
| 84 | */ |
| 85 | int rmodule_stage_to_elf(Elf64_Ehdr *ehdr, struct buffer *buff); |
| 86 | |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 87 | #endif /* TOOL_RMODULE_H */ |