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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include <vppinfra/elf_clib.h>
27 path_search_free (path_search_t * p)
30 for (i = 0; i < vec_len (p->path); i++)
31 vec_free (p->path[i]);
36 split_string (char *string, u8 delimiter)
45 while (*p != 0 && *p != delimiter)
48 vec_add (s, start, p - start);
60 file_exists_and_is_executable (char *dir, char *file)
62 char *path = (char *) format (0, "%s/%s%c", dir, file, 0);
66 yes = (stat (path, &s) >= 0
67 && S_ISREG (s.st_mode)
68 && 0 != (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)));
76 path_search (char *file)
82 /* Relative or absolute path. */
83 if (file[0] == '.' || file[0] == '/')
86 if (getenv ("PATH") == 0)
89 ps.path = split_string (getenv ("PATH"), ':');
91 for (i = 0; i < vec_len (ps.path); i++)
92 if (file_exists_and_is_executable (ps.path[i], file))
96 if (i < vec_len (ps.path))
97 result = (char *) format (0, "%s/%s%c", ps.path[i], file, 0);
99 path_search_free (&ps);
104 static clib_error_t *
105 clib_elf_parse_file (clib_elf_main_t * cem,
106 char *file_name, void *link_address)
112 uword mmap_length = 0;
114 clib_error_t *error = 0;
116 vec_add2 (cem->elf_mains, em, 1);
118 fd = open (file_name, 0);
121 error = clib_error_return_unix (0, "open `%s'", file_name);
125 if (fstat (fd, &fd_stat) < 0)
127 error = clib_error_return_unix (0, "fstat `%s'", file_name);
130 mmap_length = fd_stat.st_size;
132 data = mmap (0, mmap_length, PROT_READ, MAP_SHARED, fd, /* offset */ 0);
133 if (~pointer_to_uword (data) == 0)
135 error = clib_error_return_unix (0, "mmap `%s'", file_name);
139 error = elf_parse (em, data, mmap_length);
143 /* Look for CLIB special sections. */
145 char *section_name_start = CLIB_ELF_SECTION_ADD_PREFIX ();
146 uword section_name_start_len = strlen (section_name_start);
148 vec_foreach (s, em->sections)
150 u8 *name = elf_section_name (em, s);
152 clib_elf_section_t *vs;
153 clib_elf_section_bounds_t *b;
155 /* Section name must begin with CLIB_ELF_SECTION key. */
156 if (strcmp ((char *) name, section_name_start))
159 name += section_name_start_len;
160 p = hash_get_mem (cem->section_by_name, name);
162 vs = vec_elt_at_index (cem->sections, p[0]);
165 name = format (0, "%s%c", name, 0);
166 if (!cem->section_by_name)
167 cem->section_by_name = hash_create_string (0, sizeof (uword));
168 hash_set_mem (cem->section_by_name, name, vec_len (cem->sections));
169 vec_add2 (cem->sections, vs, 1);
173 vec_add2 (vs->bounds, b, 1);
174 b->lo = link_address + s->header.exec_address;
175 b->hi = b->lo + s->header.file_size;
179 /* Parse symbols for this file. */
181 elf_symbol_table_t *t;
184 elf_parse_symbols (em);
185 vec_foreach (t, em->symbol_tables)
187 vec_foreach (s, t->symbols)
189 s->value += pointer_to_uword (link_address);
194 /* No need to keep section contents around. */
197 vec_foreach (s, em->sections)
199 if (s->header.type != ELF_SECTION_STRING_TABLE)
200 vec_free (s->contents);
210 munmap (data, mmap_length);
218 add_section (struct dl_phdr_info *info, size_t size, void *opaque)
220 clib_elf_main_t *cem = opaque;
222 char *name = (char *) info->dlpi_name;
223 void *addr = (void *) info->dlpi_addr;
226 is_main = strlen (name) == 0;
231 /* Only do main program once. */
235 name = path_search (cem->exec_path);
238 clib_error ("failed to find %s on PATH", cem->exec_path);
244 error = clib_elf_parse_file (cem, name, addr);
247 /* Don't complain about 'linux-vdso.so.1' */
248 if (!is_main && name[0] != '/' && error->code == ENOENT)
249 clib_error_free (error);
251 clib_error_report (error);
254 if (is_main && name != cem->exec_path)
260 static clib_elf_main_t clib_elf_main;
263 clib_elf_main_init (char *exec_path)
265 clib_elf_main_t *cem = &clib_elf_main;
267 cem->exec_path = exec_path;
269 dl_iterate_phdr (add_section, cem);
272 clib_elf_section_bounds_t *
273 clib_elf_get_section_bounds (char *name)
275 clib_elf_main_t *em = &clib_elf_main;
276 uword *p = hash_get (em->section_by_name, name);
277 return p ? vec_elt_at_index (em->sections, p[0])->bounds : 0;
281 symbol_by_address_or_name (char *by_name,
282 uword by_address, clib_elf_symbol_t * s)
284 clib_elf_main_t *cem = &clib_elf_main;
287 vec_foreach (em, cem->elf_mains)
289 elf_symbol_table_t *t;
290 s->elf_main_index = em - cem->elf_mains;
291 vec_foreach (t, em->symbol_tables)
293 s->symbol_table_index = t - em->symbol_tables;
296 uword *p = hash_get (t->symbol_by_name, by_name);
299 s->symbol = vec_elt (t->symbols, p[0]);
306 /* FIXME linear search. */
307 vec_foreach (x, t->symbols)
309 if (by_address >= x->value && by_address < x->value + x->size)
323 clib_elf_symbol_by_name (char *by_name, clib_elf_symbol_t * s)
325 return symbol_by_address_or_name (by_name, /* by_address */ 0, s);
329 clib_elf_symbol_by_address (uword by_address, clib_elf_symbol_t * s)
331 return symbol_by_address_or_name ( /* by_name */ 0, by_address, s);
335 format_clib_elf_symbol (u8 * s, va_list * args)
337 clib_elf_main_t *cem = &clib_elf_main;
338 clib_elf_symbol_t *sym = va_arg (*args, clib_elf_symbol_t *);
340 elf_symbol_table_t *t;
343 /* Just print table headings. */
344 return format (s, "%U", format_elf_symbol, 0, 0, 0);
348 em = vec_elt_at_index (cem->elf_mains, sym->elf_main_index);
349 t = vec_elt_at_index (em->symbol_tables, sym->symbol_table_index);
350 return format (s, "%U", format_elf_symbol, em, t, &sym->symbol);
355 format_clib_elf_symbol_with_address (u8 * s, va_list * args)
357 uword address = va_arg (*args, uword);
358 clib_elf_main_t *cem = &clib_elf_main;
359 clib_elf_symbol_t sym;
361 elf_symbol_table_t *t;
363 if (clib_elf_symbol_by_address (address, &sym))
365 em = vec_elt_at_index (cem->elf_mains, sym.elf_main_index);
366 t = vec_elt_at_index (em->symbol_tables, sym.symbol_table_index);
367 s = format (s, "%s + 0x%wx",
368 elf_symbol_name (t, &sym.symbol),
369 address - sym.symbol.value);
372 s = format (s, "0x%wx", address);
378 * fd.io coding-style-patch-verification: ON
381 * eval: (c-set-style "gnu")