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>
26 path_search_free (path_search_t * p)
29 for (i = 0; i < vec_len (p->path); i++)
30 vec_free (p->path[i]);
35 split_string (char * string, u8 delimiter)
38 char * p, * start, * s;
44 while (*p != 0 && *p != delimiter)
47 vec_add (s, start, p - start);
59 file_exists_and_is_executable (char * dir, char * file)
61 char * path = (char *) format (0, "%s/%s%c", dir, file, 0);
65 yes = (stat (path, &s) >= 0
66 && S_ISREG (s.st_mode)
67 && 0 != (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)));
75 path_search (char * file)
81 /* Relative or absolute path. */
82 if (file[0] == '.' || file[0] == '/')
85 ps.path = split_string (getenv ("PATH"), ':');
87 for (i = 0; i < vec_len (ps.path); i++)
88 if (file_exists_and_is_executable (ps.path[i], file))
92 if (i < vec_len (ps.path))
93 result = (char *) format (0, "%s/%s%c", ps.path[i], file);
95 path_search_free (&ps);
100 static clib_error_t *
101 clib_elf_parse_file (clib_elf_main_t * cem,
109 uword mmap_length = 0;
111 clib_error_t * error = 0;
113 vec_add2 (cem->elf_mains, em, 1);
115 fd = open (file_name, 0);
118 error = clib_error_return_unix (0, "open `%s'", file_name);
122 if (fstat (fd, &fd_stat) < 0)
124 error = clib_error_return_unix (0, "fstat `%s'", file_name);
127 mmap_length = fd_stat.st_size;
129 data = mmap (0, mmap_length, PROT_READ, MAP_SHARED, fd, /* offset */ 0);
130 if (~pointer_to_uword (data) == 0)
132 error = clib_error_return_unix (0, "mmap `%s'", file_name);
136 error = elf_parse (em, data, mmap_length);
140 /* Look for CLIB special sections. */
142 char * section_name_start = CLIB_ELF_SECTION_ADD_PREFIX ();
143 uword section_name_start_len = strlen (section_name_start);
145 vec_foreach (s, em->sections)
147 u8 * name = elf_section_name (em, s);
149 clib_elf_section_t * vs;
150 clib_elf_section_bounds_t * b;
152 /* Section name must begin with CLIB_ELF_SECTION key. */
153 if (memcmp (name, section_name_start, section_name_start_len))
156 name += section_name_start_len;
157 p = hash_get_mem (cem->section_by_name, name);
159 vs = vec_elt_at_index (cem->sections, p[0]);
162 name = format (0, "%s%c", name, 0);
163 if (! cem->section_by_name)
164 cem->section_by_name = hash_create_string (0, sizeof (uword));
165 hash_set_mem (cem->section_by_name, name, vec_len (cem->sections));
166 vec_add2 (cem->sections, vs, 1);
170 vec_add2 (vs->bounds, b, 1);
171 b->lo = link_address + s->header.exec_address;
172 b->hi = b->lo + s->header.file_size;
176 /* Parse symbols for this file. */
178 elf_symbol_table_t * t;
181 elf_parse_symbols (em);
182 vec_foreach (t, em->symbol_tables)
184 vec_foreach (s, t->symbols)
186 s->value += pointer_to_uword (link_address);
191 /* No need to keep section contents around. */
194 vec_foreach (s, em->sections)
196 if (s->header.type != ELF_SECTION_STRING_TABLE)
197 vec_free (s->contents);
207 munmap (data, mmap_length);
215 add_section (struct dl_phdr_info * info, size_t size, void * opaque)
217 clib_elf_main_t * cem = opaque;
218 clib_error_t * error;
219 char * name = (char *) info->dlpi_name;
220 void * addr = (void *) info->dlpi_addr;
223 is_main = strlen (name) == 0;
228 /* Only do main program once. */
232 name = path_search (cem->exec_path);
234 clib_error ("failed to find %s on PATH", cem->exec_path);
238 error = clib_elf_parse_file (cem, name, addr);
240 clib_error_report (error);
242 if (is_main && name != cem->exec_path)
248 static clib_elf_main_t clib_elf_main;
250 void clib_elf_main_init (char * exec_path)
252 clib_elf_main_t * cem = &clib_elf_main;
254 cem->exec_path = exec_path;
256 dl_iterate_phdr (add_section, cem);
259 clib_elf_section_bounds_t *
260 clib_elf_get_section_bounds (char * name)
262 clib_elf_main_t * em = &clib_elf_main;
263 uword * p = hash_get (em->section_by_name, name);
264 return p ? vec_elt_at_index (em->sections, p[0])->bounds : 0;
268 symbol_by_address_or_name (char * by_name,
270 clib_elf_symbol_t * s)
272 clib_elf_main_t * cem = &clib_elf_main;
275 vec_foreach (em, cem->elf_mains)
277 elf_symbol_table_t * t;
278 s->elf_main_index = em - cem->elf_mains;
279 vec_foreach (t, em->symbol_tables)
281 s->symbol_table_index = t - em->symbol_tables;
284 uword * p = hash_get (t->symbol_by_name, by_name);
287 s->symbol = vec_elt (t->symbols, p[0]);
294 /* FIXME linear search. */
295 vec_foreach (x, t->symbols)
297 if (by_address >= x->value && by_address < x->value + x->size)
310 uword clib_elf_symbol_by_name (char * by_name, clib_elf_symbol_t * s)
311 { return symbol_by_address_or_name (by_name, /* by_address */ 0, s); }
313 uword clib_elf_symbol_by_address (uword by_address, clib_elf_symbol_t * s)
314 { return symbol_by_address_or_name (/* by_name */ 0, by_address, s); }
316 u8 * format_clib_elf_symbol (u8 * s, va_list * args)
318 clib_elf_main_t * cem = &clib_elf_main;
319 clib_elf_symbol_t * sym = va_arg (*args, clib_elf_symbol_t *);
321 elf_symbol_table_t * t;
324 /* Just print table headings. */
325 return format (s, "%U", format_elf_symbol, 0, 0, 0);
329 em = vec_elt_at_index (cem->elf_mains, sym->elf_main_index);
330 t = vec_elt_at_index (em->symbol_tables, sym->symbol_table_index);
331 return format (s, "%U", format_elf_symbol, em, t, &sym->symbol);
335 u8 * format_clib_elf_symbol_with_address (u8 * s, va_list * args)
337 uword address = va_arg (*args, uword);
338 clib_elf_main_t * cem = &clib_elf_main;
339 clib_elf_symbol_t sym;
341 elf_symbol_table_t * t;
343 if (clib_elf_symbol_by_address (address, &sym))
345 em = vec_elt_at_index (cem->elf_mains, sym.elf_main_index);
346 t = vec_elt_at_index (em->symbol_tables, sym.symbol_table_index);
347 s = format (s, "%s + 0x%wx",
348 elf_symbol_name (t, &sym.symbol),
349 address - sym.symbol.value);
352 s = format (s, "0x%wx", address);