Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | Copyright (c) 2012 Eliot Dresselhaus |
| 17 | |
| 18 | Permission is hereby granted, free of charge, to any person obtaining |
| 19 | a copy of this software and associated documentation files (the |
| 20 | "Software"), to deal in the Software without restriction, including |
| 21 | without limitation the rights to use, copy, modify, merge, publish, |
| 22 | distribute, sublicense, and/or sell copies of the Software, and to |
| 23 | permit persons to whom the Software is furnished to do so, subject to |
| 24 | the following conditions: |
| 25 | |
| 26 | The above copyright notice and this permission notice shall be |
| 27 | included in all copies or substantial portions of the Software. |
| 28 | |
| 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 32 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 33 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 34 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | */ |
| 37 | |
| 38 | #ifndef included_clib_elf_self_h |
| 39 | #define included_clib_elf_self_h |
| 40 | |
| 41 | #include <vppinfra/elf.h> |
| 42 | #include <vppinfra/hash.h> |
| 43 | |
| 44 | #define CLIB_ELF_SECTION_DATA_ALIGN 32 |
| 45 | |
| 46 | #define CLIB_ELF_SECTION_ADD_PREFIX(n) "clib_elf_section_" n |
| 47 | |
| 48 | /* Attribute used is so that static registrations work even if |
| 49 | variable is not referenced. */ |
| 50 | #define CLIB_ELF_SECTION(SECTION) \ |
| 51 | __attribute__ ((used, \ |
| 52 | aligned (CLIB_ELF_SECTION_DATA_ALIGN), \ |
| 53 | section (CLIB_ELF_SECTION_ADD_PREFIX (SECTION)))) |
| 54 | |
| 55 | /* Given pointer to previous data A get next pointer. EXTRA gives extra |
| 56 | space beyond A + 1 used in object. */ |
| 57 | #define clib_elf_section_data_next(a,extra) \ |
| 58 | uword_to_pointer (round_pow2 (pointer_to_uword (a + 1) + (extra), \ |
| 59 | CLIB_ELF_SECTION_DATA_ALIGN), \ |
| 60 | void *) |
| 61 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 62 | typedef struct |
| 63 | { |
| 64 | void *lo, *hi; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | } clib_elf_section_bounds_t; |
| 66 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 67 | typedef struct |
| 68 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 69 | /* Vector of bounds for this section. Multiple shared objects may have instances |
| 70 | of the same sections. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 71 | clib_elf_section_bounds_t *bounds; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 72 | |
| 73 | /* Name of ELF section (e.g. .text). */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 74 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | } clib_elf_section_t; |
| 76 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 77 | typedef struct |
| 78 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | /* Vector of sections. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 80 | clib_elf_section_t *sections; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | |
| 82 | /* Hash map of name to section index. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 83 | uword *section_by_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | |
| 85 | /* Unix path that we were exec()ed with. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 86 | char *exec_path; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 88 | elf_main_t *elf_mains; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | } clib_elf_main_t; |
| 90 | |
| 91 | always_inline void |
| 92 | clib_elf_main_free (clib_elf_main_t * m) |
| 93 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 94 | clib_elf_section_t *s; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | vec_foreach (s, m->sections) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 96 | { |
| 97 | vec_free (s->bounds); |
| 98 | vec_free (s->name); |
| 99 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | vec_free (m->sections); |
| 101 | hash_free (m->section_by_name); |
| 102 | |
| 103 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 104 | elf_main_t *em; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | vec_foreach (em, m->elf_mains) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 106 | { |
| 107 | elf_main_free (em); |
| 108 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | vec_free (m->elf_mains); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* Call with exec_path equal to argv[0] from C main. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 114 | void clib_elf_main_init (char *exec_path); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 115 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 116 | clib_elf_section_bounds_t *clib_elf_get_section_bounds (char *name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 118 | typedef struct |
| 119 | { |
| 120 | /* The symbol. */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | elf64_symbol_t symbol; |
| 122 | |
| 123 | /* elf_main_t where symbol came from. */ |
| 124 | u32 elf_main_index; |
| 125 | |
| 126 | /* Symbol table in elf_main_t where this symbol came from. */ |
| 127 | u32 symbol_table_index; |
| 128 | } clib_elf_symbol_t; |
| 129 | |
| 130 | /* Returns 1 if found; otherwise zero. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 131 | uword clib_elf_symbol_by_name (char *name, clib_elf_symbol_t * result); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 132 | uword clib_elf_symbol_by_address (uword address, clib_elf_symbol_t * result); |
| 133 | |
Xiaoming Jiang | 5b55526 | 2023-03-08 06:49:19 +0000 | [diff] [blame] | 134 | const char *clib_elf_symbol_name (clib_elf_symbol_t *result); |
| 135 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | format_function_t format_clib_elf_symbol, format_clib_elf_symbol_with_address; |
| 137 | |
| 138 | #endif /* included_clib_elf_self_h */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * fd.io coding-style-patch-verification: ON |
| 142 | * |
| 143 | * Local Variables: |
| 144 | * eval: (c-set-style "gnu") |
| 145 | * End: |
| 146 | */ |