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) 2008 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 | #include <vppinfra/elf.h> |
| 39 | |
| 40 | #include <sys/types.h> |
| 41 | #include <sys/stat.h> |
| 42 | #include <fcntl.h> |
| 43 | |
| 44 | #ifndef CLIB_UNIX |
| 45 | #error "unix only" |
| 46 | #endif |
| 47 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 48 | static clib_error_t * |
| 49 | elf_set_interpreter (elf_main_t * em, char *interp) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 51 | elf_segment_t *g; |
| 52 | elf_section_t *s; |
| 53 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | |
| 55 | vec_foreach (g, em->segments) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 56 | { |
| 57 | if (g->header.type == ELF_SEGMENT_INTERP) |
| 58 | break; |
| 59 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | |
| 61 | if (g >= vec_end (em->segments)) |
| 62 | return clib_error_return (0, "interpreter not found"); |
| 63 | |
| 64 | if (g->header.memory_size < 1 + strlen (interp)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 65 | return clib_error_return (0, |
| 66 | "given interpreter does not fit; must be less than %d bytes (`%s' given)", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 67 | g->header.memory_size, interp); |
| 68 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 69 | error = |
| 70 | elf_get_section_by_start_address (em, g->header.virtual_address, &s); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | if (error) |
| 72 | return error; |
| 73 | |
| 74 | /* Put in new null terminated string. */ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 75 | clib_memset (s->contents, 0, vec_len (s->contents)); |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 76 | clib_memcpy (s->contents, interp, strlen (interp)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static void |
| 82 | delete_dynamic_rpath_entries_from_section (elf_main_t * em, elf_section_t * s) |
| 83 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 84 | elf64_dynamic_entry_t *e; |
| 85 | elf64_dynamic_entry_t *new_es = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | |
| 87 | vec_foreach (e, em->dynamic_entries) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 88 | { |
| 89 | switch (e->type) |
| 90 | { |
| 91 | case ELF_DYNAMIC_ENTRY_RPATH: |
| 92 | case ELF_DYNAMIC_ENTRY_RUN_PATH: |
| 93 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 94 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 95 | default: |
| 96 | vec_add1 (new_es, e[0]); |
| 97 | break; |
| 98 | } |
| 99 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | |
| 101 | /* Pad so as to keep section size constant. */ |
| 102 | { |
| 103 | elf64_dynamic_entry_t e_end; |
| 104 | e_end.type = ELF_DYNAMIC_ENTRY_END; |
| 105 | e_end.data = 0; |
| 106 | while (vec_len (new_es) < vec_len (em->dynamic_entries)) |
| 107 | vec_add1 (new_es, e_end); |
| 108 | } |
| 109 | |
| 110 | elf_set_dynamic_entries (em); |
| 111 | } |
| 112 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 113 | static void |
| 114 | elf_delete_dynamic_rpath_entries (elf_main_t * em) |
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 | elf_section_t *s; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 117 | |
| 118 | vec_foreach (s, em->sections) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 119 | { |
| 120 | switch (s->header.type) |
| 121 | { |
| 122 | case ELF_SECTION_DYNAMIC: |
| 123 | delete_dynamic_rpath_entries_from_section (em, s); |
| 124 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 126 | default: |
| 127 | break; |
| 128 | } |
| 129 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 132 | typedef struct |
| 133 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | elf_main_t elf_main; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 135 | char *input_file; |
| 136 | char *output_file; |
| 137 | char *set_interpreter; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | int verbose; |
| 139 | } elf_test_main_t; |
| 140 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 141 | int |
| 142 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 144 | elf_test_main_t _tm, *tm = &_tm; |
| 145 | elf_main_t *em = &tm->elf_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | unformat_input_t i; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 147 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 149 | clib_memset (tm, 0, sizeof (tm[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | |
| 151 | unformat_init_command_line (&i, argv); |
| 152 | while (unformat_check_input (&i) != UNFORMAT_END_OF_INPUT) |
| 153 | { |
| 154 | if (unformat (&i, "in %s", &tm->input_file)) |
| 155 | ; |
| 156 | else if (unformat (&i, "out %s", &tm->output_file)) |
| 157 | ; |
| 158 | else if (unformat (&i, "set-interpreter %s", &tm->set_interpreter)) |
| 159 | ; |
| 160 | else if (unformat (&i, "verbose")) |
| 161 | tm->verbose = ~0; |
| 162 | else if (unformat (&i, "verbose-symbols")) |
| 163 | tm->verbose |= FORMAT_ELF_MAIN_SYMBOLS; |
| 164 | else if (unformat (&i, "verbose-relocations")) |
| 165 | tm->verbose |= FORMAT_ELF_MAIN_RELOCATIONS; |
| 166 | else if (unformat (&i, "verbose-dynamic")) |
| 167 | tm->verbose |= FORMAT_ELF_MAIN_DYNAMIC; |
| 168 | else |
| 169 | { |
| 170 | error = unformat_parse_error (&i); |
| 171 | goto done; |
| 172 | } |
| 173 | } |
| 174 | |
Florin Coras | e10e372 | 2016-04-13 00:05:27 +0200 | [diff] [blame] | 175 | if (!tm->input_file) |
| 176 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 177 | clib_warning ("No input file! Using test_bihash_template"); |
Florin Coras | e10e372 | 2016-04-13 00:05:27 +0200 | [diff] [blame] | 178 | tm->input_file = "test_bihash_template"; |
| 179 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | |
| 181 | error = elf_read_file (em, tm->input_file); |
| 182 | if (error) |
| 183 | goto done; |
| 184 | |
| 185 | if (tm->set_interpreter) |
| 186 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 187 | clib_error_t *error = elf_set_interpreter (em, tm->set_interpreter); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | if (error) |
| 189 | goto done; |
| 190 | elf_delete_dynamic_rpath_entries (em); |
| 191 | } |
| 192 | |
| 193 | if (tm->verbose) |
| 194 | fformat (stdout, "%U", format_elf_main, em, tm->verbose); |
| 195 | |
| 196 | if (tm->output_file) |
| 197 | error = elf_write_file (em, tm->output_file); |
| 198 | |
| 199 | elf_main_free (em); |
| 200 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 201 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | if (error) |
| 203 | { |
| 204 | clib_error_report (error); |
| 205 | return 1; |
| 206 | } |
| 207 | else |
| 208 | return 0; |
| 209 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * fd.io coding-style-patch-verification: ON |
| 213 | * |
| 214 | * Local Variables: |
| 215 | * eval: (c-set-style "gnu") |
| 216 | * End: |
| 217 | */ |