Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 Google, Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 15 | * Foundation, Inc. |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #ifndef TOOL_RMODULE_H |
| 19 | #define TOOL_RMODULE_H |
| 20 | |
Aaron Durbin | b39a974 | 2015-09-08 17:24:04 -0500 | [diff] [blame^] | 21 | #include "elfparsing.h" |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 22 | #include "common.h" |
| 23 | |
Aaron Durbin | b39a974 | 2015-09-08 17:24:04 -0500 | [diff] [blame^] | 24 | struct arch_ops { |
| 25 | int arch; |
| 26 | /* Determine if relocation is a valid type for the architecture. */ |
| 27 | int (*valid_type)(Elf64_Rela *rel); |
| 28 | /* Determine if relocation should be emitted. */ |
| 29 | int (*should_emit)(Elf64_Rela *rel); |
| 30 | }; |
| 31 | |
| 32 | /* |
| 33 | * The fields in rmod_context are read-only to the user. These are |
| 34 | * exposed for easy shareability. |
| 35 | */ |
| 36 | struct rmod_context { |
| 37 | /* Ops to process relocations. */ |
| 38 | const struct arch_ops *ops; |
| 39 | |
| 40 | /* endian conversion ops */ |
| 41 | struct xdr *xdr; |
| 42 | |
| 43 | /* Parsed ELF sturcture. */ |
| 44 | struct parsed_elf pelf; |
| 45 | /* Program segment. */ |
| 46 | Elf64_Phdr *phdr; |
| 47 | |
| 48 | /* Collection of relocation addresses fixup in the module. */ |
| 49 | Elf64_Xword nrelocs; |
| 50 | Elf64_Addr *emitted_relocs; |
| 51 | |
| 52 | /* The following fields are addresses within the linked program. */ |
| 53 | Elf64_Addr parameters_begin; |
| 54 | Elf64_Addr parameters_end; |
| 55 | Elf64_Addr bss_begin; |
| 56 | Elf64_Addr bss_end; |
| 57 | }; |
| 58 | |
| 59 | struct reloc_filter { |
| 60 | /* Return < 0 on error. 0 to ignore relocation and 1 to include |
| 61 | * relocation. */ |
| 62 | int (*filter)(struct reloc_filter *f, const Elf64_Rela *r); |
| 63 | /* Pointer for filter provides */ |
| 64 | void *context; |
| 65 | }; |
| 66 | |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 67 | /* |
| 68 | * Parse an ELF file within the elfin buffer and fill in the elfout buffer |
| 69 | * with a created rmodule in ELF format. Return 0 on success, < 0 on error. |
| 70 | */ |
| 71 | int rmodule_create(const struct buffer *elfin, struct buffer *elfout); |
| 72 | |
Aaron Durbin | b39a974 | 2015-09-08 17:24:04 -0500 | [diff] [blame^] | 73 | /* |
| 74 | * Initialize an rmodule context from an ELF buffer. Returns 0 on scucess, < 0 |
| 75 | * on error. |
| 76 | */ |
| 77 | int rmodule_init(struct rmod_context *ctx, const struct buffer *elfin); |
| 78 | |
| 79 | /* |
| 80 | * Collect all the relocations that apply to the program in |
| 81 | * nrelocs/emitted_relocs. One can optionally provide a reloc_filter object |
| 82 | * to help in relocation filtering. The filter function will be called twice: |
| 83 | * once for counting and once for emitting. The same response should be |
| 84 | * provided for each call. Returns 0 on success, < 0 on error. |
| 85 | */ |
| 86 | int rmodule_collect_relocations(struct rmod_context *c, struct reloc_filter *f); |
| 87 | |
| 88 | /* Clean up the memory consumed by the rmdoule context. */ |
| 89 | void rmodule_cleanup(struct rmod_context *ctx); |
| 90 | |
Aaron Durbin | 4fde5a6 | 2014-03-07 15:11:53 -0600 | [diff] [blame] | 91 | #endif /* TOOL_RMODULE_H */ |