8c705488a4b6bb786a73c2b11cf041fb1123af76
[vpp.git] / vppinfra / vppinfra / elf_clib.c
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 #include <vppinfra/elf_clib.h>
16
17 #include <stdlib.h>
18 #include <fcntl.h>
19 #include <sys/stat.h>
20
21 typedef struct {
22   char ** path;
23 } path_search_t;
24
25 always_inline void
26 path_search_free (path_search_t * p)
27 {
28   uword i;
29   for (i = 0; i < vec_len (p->path); i++)
30     vec_free (p->path[i]);
31   vec_free (p->path);
32 }
33
34 static char **
35 split_string (char * string, u8 delimiter)
36 {
37   char ** result = 0;
38   char * p, * start, * s;
39   
40   p = string;
41   while (1)
42     {
43       start = p;
44       while (*p != 0 && *p != delimiter)
45         p++;
46       s = 0;
47       vec_add (s, start, p - start);
48       vec_add1 (s, 0);
49       vec_add1 (result, s);
50       if (*p == 0)
51         break;
52       p++;
53     }
54
55   return result;
56 }
57
58 static int
59 file_exists_and_is_executable (char * dir, char * file)
60 {
61   char * path = (char *) format (0, "%s/%s%c", dir, file, 0);
62   struct stat s;
63   uword yes;
64
65   yes = (stat (path, &s) >= 0
66          && S_ISREG (s.st_mode)
67          && 0 != (s.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)));
68
69   vec_free (path);
70
71   return yes;
72 }
73
74 static char *
75 path_search (char * file)
76 {
77   path_search_t ps;
78   uword i;
79   char * result;
80
81   /* Relative or absolute path. */
82   if (file[0] == '.' || file[0] == '/')
83     return file;
84
85   if (getenv("PATH") == 0)
86     return file;
87
88   ps.path = split_string (getenv ("PATH"), ':');
89
90   for (i = 0; i < vec_len (ps.path); i++)
91     if (file_exists_and_is_executable (ps.path[i], file))
92       break;
93
94   result = 0;
95   if (i < vec_len (ps.path))
96     result = (char *) format (0, "%s/%s%c", ps.path[i], file);
97
98   path_search_free (&ps);
99
100   return result;
101 }
102
103 static clib_error_t *
104 clib_elf_parse_file (clib_elf_main_t * cem,
105                      char * file_name,
106                      void * link_address)
107 {
108   elf_main_t * em;
109   elf_section_t * s;
110   int fd;
111   struct stat fd_stat;
112   uword mmap_length = 0;
113   void * data = 0;
114   clib_error_t * error = 0;
115
116   vec_add2 (cem->elf_mains, em, 1);
117
118   fd = open (file_name, 0);
119   if (fd < 0)
120     {
121       error = clib_error_return_unix (0, "open `%s'", file_name);
122       goto done;
123     }
124
125   if (fstat (fd, &fd_stat) < 0)
126     {
127       error = clib_error_return_unix (0, "fstat `%s'", file_name);
128       goto done;
129     }
130   mmap_length = fd_stat.st_size;
131
132   data = mmap (0, mmap_length, PROT_READ, MAP_SHARED, fd, /* offset */ 0);
133   if (~pointer_to_uword (data) == 0)
134     {
135       error = clib_error_return_unix (0, "mmap `%s'", file_name);
136       goto done;
137     }
138
139   error = elf_parse (em, data, mmap_length);
140   if (error)
141     goto done;
142
143   /* Look for CLIB special sections. */
144   {
145     char * section_name_start = CLIB_ELF_SECTION_ADD_PREFIX ();
146     uword section_name_start_len = strlen (section_name_start);
147
148     vec_foreach (s, em->sections)
149       {
150         u8 * name = elf_section_name (em, s);
151         uword * p;
152         clib_elf_section_t * vs;
153         clib_elf_section_bounds_t * b;
154
155         /* Section name must begin with CLIB_ELF_SECTION key. */
156         if (memcmp (name, section_name_start, section_name_start_len))
157           continue;
158
159         name += section_name_start_len;
160         p = hash_get_mem (cem->section_by_name, name);
161         if (p)
162           vs = vec_elt_at_index (cem->sections, p[0]);
163         else
164           {
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);
170             vs->name = name;
171           }
172
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;
176       }
177   }
178
179   /* Parse symbols for this file. */
180   {
181     elf_symbol_table_t * t;
182     elf64_symbol_t * s;
183
184     elf_parse_symbols (em);
185     vec_foreach (t, em->symbol_tables)
186       {
187         vec_foreach (s, t->symbols)
188           {
189             s->value += pointer_to_uword (link_address);
190           }
191       }
192   }
193
194   /* No need to keep section contents around. */
195   {
196     elf_section_t * s;
197     vec_foreach (s, em->sections)
198       {
199         if (s->header.type != ELF_SECTION_STRING_TABLE)
200           vec_free (s->contents);
201       }
202   }
203
204  done:
205   if (error)
206     elf_main_free (em);
207   if (fd >= 0)
208     close (fd);
209   if (data)
210     munmap (data, mmap_length);
211   return error;
212 }
213
214 #define __USE_GNU
215 #include <link.h>
216
217 static int
218 add_section (struct dl_phdr_info * info, size_t size, void * opaque)
219 {
220   clib_elf_main_t * cem = opaque;
221   clib_error_t * error;
222   char * name = (char *) info->dlpi_name;
223   void * addr = (void *) info->dlpi_addr;
224   uword is_main;
225
226   is_main = strlen (name) == 0;
227   if (is_main)
228     {
229       static int done;
230
231       /* Only do main program once. */
232       if (done++)
233         return 0;
234
235       name = path_search (cem->exec_path);
236       if (! name)
237         {
238           clib_error ("failed to find %s on PATH", cem->exec_path);
239           return 0;
240         }
241       addr = 0;
242     }
243
244   error = clib_elf_parse_file (cem, name, addr);
245   if (error)
246     clib_error_report (error);
247
248   if (is_main && name != cem->exec_path)
249     vec_free (name);
250
251   return 0;
252 }
253
254 static clib_elf_main_t clib_elf_main;
255
256 void clib_elf_main_init (char * exec_path)
257 {
258   clib_elf_main_t * cem = &clib_elf_main;
259
260   cem->exec_path = exec_path;
261
262   dl_iterate_phdr (add_section, cem);
263 }
264
265 clib_elf_section_bounds_t *
266 clib_elf_get_section_bounds (char * name)
267 {
268   clib_elf_main_t * em = &clib_elf_main;
269   uword * p = hash_get (em->section_by_name, name);
270   return p ? vec_elt_at_index (em->sections, p[0])->bounds : 0;
271 }
272
273 static uword
274 symbol_by_address_or_name (char * by_name,
275                            uword by_address,
276                            clib_elf_symbol_t * s)
277 {
278   clib_elf_main_t * cem = &clib_elf_main;
279   elf_main_t * em;
280
281   vec_foreach (em, cem->elf_mains)
282     {
283       elf_symbol_table_t * t;
284       s->elf_main_index = em - cem->elf_mains;
285       vec_foreach (t, em->symbol_tables)
286         {
287           s->symbol_table_index = t - em->symbol_tables;
288           if (by_name)
289             {
290               uword * p = hash_get (t->symbol_by_name, by_name);
291               if (p)
292                 {
293                   s->symbol = vec_elt (t->symbols, p[0]);
294                   return 1;
295                 }
296             }
297           else
298             {
299               elf64_symbol_t * x;
300               /* FIXME linear search. */
301               vec_foreach (x, t->symbols)
302                 {
303                   if (by_address >= x->value && by_address < x->value + x->size)
304                     {
305                       s->symbol = x[0];
306                       return 1;
307                     }
308                 }
309             }
310         }
311     }
312
313   return 0;
314 }
315
316 uword clib_elf_symbol_by_name (char * by_name, clib_elf_symbol_t * s)
317 { return symbol_by_address_or_name (by_name, /* by_address */ 0, s); }
318
319 uword clib_elf_symbol_by_address (uword by_address, clib_elf_symbol_t * s)
320 { return symbol_by_address_or_name (/* by_name */ 0, by_address, s); }
321
322 u8 * format_clib_elf_symbol (u8 * s, va_list * args)
323 {
324   clib_elf_main_t * cem = &clib_elf_main;
325   clib_elf_symbol_t * sym = va_arg (*args, clib_elf_symbol_t *);
326   elf_main_t * em;
327   elf_symbol_table_t * t;
328
329   if (! sym)
330     /* Just print table headings. */
331     return format (s, "%U", format_elf_symbol, 0, 0, 0);
332
333   else
334     {
335       em = vec_elt_at_index (cem->elf_mains, sym->elf_main_index);
336       t = vec_elt_at_index (em->symbol_tables, sym->symbol_table_index);
337       return format (s, "%U", format_elf_symbol, em, t, &sym->symbol);
338     }
339 }
340
341 u8 * format_clib_elf_symbol_with_address (u8 * s, va_list * args)
342 {
343   uword address = va_arg (*args, uword);
344   clib_elf_main_t * cem = &clib_elf_main;
345   clib_elf_symbol_t sym;
346   elf_main_t * em;
347   elf_symbol_table_t * t;
348
349   if (clib_elf_symbol_by_address (address, &sym))
350     {
351       em = vec_elt_at_index (cem->elf_mains, sym.elf_main_index);
352       t = vec_elt_at_index (em->symbol_tables, sym.symbol_table_index);
353       s = format (s, "%s + 0x%wx",
354                   elf_symbol_name (t, &sym.symbol),
355                   address - sym.symbol.value);
356     }
357   else
358     s = format (s, "0x%wx", address);
359
360   return s;
361 }