Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Google, Inc. |
Frans Hendriks | 166cbde | 2018-11-22 14:21:12 +0100 | [diff] [blame] | 3 | * Copyright (C) 2018 Eltan B.V. |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 4 | * |
| 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 Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #ifndef TOOL_RMODULE_H |
| 16 | #define TOOL_RMODULE_H |
| 17 | |
Aaron Durbin | b39a974 | 2015-09-08 17:24:04 -0500 | [diff] [blame] | 18 | #include "elfparsing.h" |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 19 | #include "common.h" |
| 20 | |
Aaron Durbin | b39a974 | 2015-09-08 17:24:04 -0500 | [diff] [blame] | 21 | struct arch_ops { |
| 22 | int arch; |
| 23 | /* Determine if relocation is a valid type for the architecture. */ |
| 24 | int (*valid_type)(Elf64_Rela *rel); |
| 25 | /* Determine if relocation should be emitted. */ |
| 26 | int (*should_emit)(Elf64_Rela *rel); |
| 27 | }; |
| 28 | |
| 29 | /* |
| 30 | * The fields in rmod_context are read-only to the user. These are |
| 31 | * exposed for easy shareability. |
| 32 | */ |
| 33 | struct rmod_context { |
| 34 | /* Ops to process relocations. */ |
| 35 | const struct arch_ops *ops; |
| 36 | |
| 37 | /* endian conversion ops */ |
| 38 | struct xdr *xdr; |
| 39 | |
Elyes HAOUAS | 3db0198 | 2018-08-23 18:08:20 +0200 | [diff] [blame] | 40 | /* Parsed ELF structure. */ |
Aaron Durbin | b39a974 | 2015-09-08 17:24:04 -0500 | [diff] [blame] | 41 | struct parsed_elf pelf; |
| 42 | /* Program segment. */ |
| 43 | Elf64_Phdr *phdr; |
| 44 | |
| 45 | /* Collection of relocation addresses fixup in the module. */ |
| 46 | Elf64_Xword nrelocs; |
| 47 | Elf64_Addr *emitted_relocs; |
| 48 | |
| 49 | /* The following fields are addresses within the linked program. */ |
| 50 | Elf64_Addr parameters_begin; |
| 51 | Elf64_Addr parameters_end; |
| 52 | Elf64_Addr bss_begin; |
| 53 | Elf64_Addr bss_end; |
| 54 | }; |
| 55 | |
| 56 | struct reloc_filter { |
| 57 | /* Return < 0 on error. 0 to ignore relocation and 1 to include |
| 58 | * relocation. */ |
| 59 | int (*filter)(struct reloc_filter *f, const Elf64_Rela *r); |
| 60 | /* Pointer for filter provides */ |
| 61 | void *context; |
| 62 | }; |
| 63 | |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 64 | /* |
| 65 | * Parse an ELF file within the elfin buffer and fill in the elfout buffer |
| 66 | * with a created rmodule in ELF format. Return 0 on success, < 0 on error. |
| 67 | */ |
| 68 | int rmodule_create(const struct buffer *elfin, struct buffer *elfout); |
| 69 | |
Aaron Durbin | b39a974 | 2015-09-08 17:24:04 -0500 | [diff] [blame] | 70 | /* |
| 71 | * Initialize an rmodule context from an ELF buffer. Returns 0 on scucess, < 0 |
| 72 | * on error. |
| 73 | */ |
| 74 | int rmodule_init(struct rmod_context *ctx, const struct buffer *elfin); |
| 75 | |
| 76 | /* |
| 77 | * Collect all the relocations that apply to the program in |
| 78 | * nrelocs/emitted_relocs. One can optionally provide a reloc_filter object |
| 79 | * to help in relocation filtering. The filter function will be called twice: |
| 80 | * once for counting and once for emitting. The same response should be |
| 81 | * provided for each call. Returns 0 on success, < 0 on error. |
| 82 | */ |
| 83 | int rmodule_collect_relocations(struct rmod_context *c, struct reloc_filter *f); |
| 84 | |
Frans Hendriks | 166cbde | 2018-11-22 14:21:12 +0100 | [diff] [blame] | 85 | /* Clean up the memory consumed by the rmodule context. */ |
Aaron Durbin | b39a974 | 2015-09-08 17:24:04 -0500 | [diff] [blame] | 86 | void rmodule_cleanup(struct rmod_context *ctx); |
| 87 | |
Aaron Durbin | 694fd13 | 2015-10-28 11:39:34 -0500 | [diff] [blame] | 88 | /* |
| 89 | * Create an ELF file from the passed in rmodule in the buffer. The buffer |
| 90 | * contents will be replaced with an ELF file. Returns 1 if buff doesn't |
| 91 | * contain an rmodule and < 0 on failure, 0 on success. |
| 92 | */ |
| 93 | int rmodule_stage_to_elf(Elf64_Ehdr *ehdr, struct buffer *buff); |
| 94 | |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 95 | #endif /* TOOL_RMODULE_H */ |