0947dc6aacfdb4f858c0df6c005d66783ee28565
[dpdk.git] / buildtools / pmdinfogen / pmdinfogen.c
1 /* Postprocess pmd object files to export hw support
2  *
3  * Copyright 2016 Neil Horman <nhorman@tuxdriver.com>
4  * Based in part on modpost.c from the linux kernel
5  *
6  * This software may be used and distributed according to the terms
7  * of the GNU General Public License V2, incorporated herein by reference.
8  *
9  */
10
11 #define _GNU_SOURCE
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <limits.h>
16 #include <stdbool.h>
17 #include <errno.h>
18 #include "pmdinfogen.h"
19
20 #ifdef RTE_ARCH_64
21 #define ADDR_SIZE 64
22 #else
23 #define ADDR_SIZE 32
24 #endif
25
26
27 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
28 {
29         if (sym)
30                 return elf->strtab + sym->st_name;
31         else
32                 return "(unknown)";
33 }
34
35 void *grab_file(const char *filename, unsigned long *size)
36 {
37         struct stat st;
38         void *map = MAP_FAILED;
39         int fd;
40
41         fd = open(filename, O_RDONLY);
42         if (fd < 0)
43                 return NULL;
44         if (fstat(fd, &st))
45                 goto failed;
46
47         *size = st.st_size;
48         map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
49
50 failed:
51         close(fd);
52         if (map == MAP_FAILED)
53                 return NULL;
54         return map;
55 }
56
57 /**
58   * Return a copy of the next line in a mmap'ed file.
59   * spaces in the beginning of the line is trimmed away.
60   * Return a pointer to a static buffer.
61   **/
62 void release_file(void *file, unsigned long size)
63 {
64         munmap(file, size);
65 }
66
67
68 static void *get_sym_value(struct elf_info *info, const Elf_Sym *sym)
69 {
70         void *ptr = (void *)info->hdr + info->sechdrs[sym->st_shndx].sh_offset;
71
72         return (void *)(ptr + sym->st_value);
73 }
74
75 static Elf_Sym *find_sym_in_symtab(struct elf_info *info,
76                                    const char *name, Elf_Sym *last)
77 {
78         Elf_Sym *idx;
79         if (last)
80                 idx = last+1;
81         else
82                 idx = info->symtab_start;
83
84         for (; idx < info->symtab_stop; idx++) {
85                 const char *n = sym_name(info, idx);
86                 if (!strncmp(n, name, strlen(name)))
87                         return idx;
88         }
89         return NULL;
90 }
91
92 static int parse_elf(struct elf_info *info, const char *filename)
93 {
94         unsigned int i;
95         Elf_Ehdr *hdr;
96         Elf_Shdr *sechdrs;
97         Elf_Sym  *sym;
98         const char *secstrings;
99         int endian;
100         unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
101
102         hdr = grab_file(filename, &info->size);
103         if (!hdr) {
104                 perror(filename);
105                 exit(1);
106         }
107         info->hdr = hdr;
108         if (info->size < sizeof(*hdr)) {
109                 /* file too small, assume this is an empty .o file */
110                 return 0;
111         }
112         /* Is this a valid ELF file? */
113         if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
114             (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
115             (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
116             (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
117                 /* Not an ELF file - silently ignore it */
118                 return 0;
119         }
120
121         if (!hdr->e_ident[EI_DATA]) {
122                 /* Unknown endian */
123                 return 0;
124         }
125
126         endian = hdr->e_ident[EI_DATA];
127
128         /* Fix endianness in ELF header */
129         hdr->e_type      = TO_NATIVE(endian, 16, hdr->e_type);
130         hdr->e_machine   = TO_NATIVE(endian, 16, hdr->e_machine);
131         hdr->e_version   = TO_NATIVE(endian, 32, hdr->e_version);
132         hdr->e_entry     = TO_NATIVE(endian, ADDR_SIZE, hdr->e_entry);
133         hdr->e_phoff     = TO_NATIVE(endian, ADDR_SIZE, hdr->e_phoff);
134         hdr->e_shoff     = TO_NATIVE(endian, ADDR_SIZE, hdr->e_shoff);
135         hdr->e_flags     = TO_NATIVE(endian, 32, hdr->e_flags);
136         hdr->e_ehsize    = TO_NATIVE(endian, 16, hdr->e_ehsize);
137         hdr->e_phentsize = TO_NATIVE(endian, 16, hdr->e_phentsize);
138         hdr->e_phnum     = TO_NATIVE(endian, 16, hdr->e_phnum);
139         hdr->e_shentsize = TO_NATIVE(endian, 16, hdr->e_shentsize);
140         hdr->e_shnum     = TO_NATIVE(endian, 16, hdr->e_shnum);
141         hdr->e_shstrndx  = TO_NATIVE(endian, 16, hdr->e_shstrndx);
142
143         sechdrs = (void *)hdr + hdr->e_shoff;
144         info->sechdrs = sechdrs;
145
146         /* Check if file offset is correct */
147         if (hdr->e_shoff > info->size) {
148                 fprintf(stderr, "section header offset=%lu in file '%s' "
149                       "is bigger than filesize=%lu\n",
150                       (unsigned long)hdr->e_shoff,
151                       filename, info->size);
152                 return 0;
153         }
154
155         if (hdr->e_shnum == SHN_UNDEF) {
156                 /*
157                  * There are more than 64k sections,
158                  * read count from .sh_size.
159                  */
160                 info->num_sections = TO_NATIVE(endian, 32, sechdrs[0].sh_size);
161         } else {
162                 info->num_sections = hdr->e_shnum;
163         }
164         if (hdr->e_shstrndx == SHN_XINDEX)
165                 info->secindex_strings =
166                         TO_NATIVE(endian, 32, sechdrs[0].sh_link);
167         else
168                 info->secindex_strings = hdr->e_shstrndx;
169
170         /* Fix endianness in section headers */
171         for (i = 0; i < info->num_sections; i++) {
172                 sechdrs[i].sh_name      =
173                         TO_NATIVE(endian, 32, sechdrs[i].sh_name);
174                 sechdrs[i].sh_type      =
175                         TO_NATIVE(endian, 32, sechdrs[i].sh_type);
176                 sechdrs[i].sh_flags     =
177                         TO_NATIVE(endian, 32, sechdrs[i].sh_flags);
178                 sechdrs[i].sh_addr      =
179                         TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_addr);
180                 sechdrs[i].sh_offset    =
181                         TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_offset);
182                 sechdrs[i].sh_size      =
183                         TO_NATIVE(endian, 32, sechdrs[i].sh_size);
184                 sechdrs[i].sh_link      =
185                         TO_NATIVE(endian, 32, sechdrs[i].sh_link);
186                 sechdrs[i].sh_info      =
187                         TO_NATIVE(endian, 32, sechdrs[i].sh_info);
188                 sechdrs[i].sh_addralign =
189                         TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_addralign);
190                 sechdrs[i].sh_entsize   =
191                         TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_entsize);
192         }
193         /* Find symbol table. */
194         secstrings = (void *)hdr + sechdrs[info->secindex_strings].sh_offset;
195         for (i = 1; i < info->num_sections; i++) {
196                 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
197
198                 if (!nobits && sechdrs[i].sh_offset > info->size) {
199                         fprintf(stderr, "%s is truncated. "
200                               "sechdrs[i].sh_offset=%lu > sizeof(*hrd)=%zu\n",
201                               filename, (unsigned long)sechdrs[i].sh_offset,
202                               sizeof(*hdr));
203                         return 0;
204                 }
205
206                 if (sechdrs[i].sh_type == SHT_SYMTAB) {
207                         unsigned int sh_link_idx;
208                         symtab_idx = i;
209                         info->symtab_start = (void *)hdr +
210                             sechdrs[i].sh_offset;
211                         info->symtab_stop  = (void *)hdr +
212                             sechdrs[i].sh_offset + sechdrs[i].sh_size;
213                         sh_link_idx = sechdrs[i].sh_link;
214                         info->strtab       = (void *)hdr +
215                             sechdrs[sh_link_idx].sh_offset;
216                 }
217
218                 /* 32bit section no. table? ("more than 64k sections") */
219                 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
220                         symtab_shndx_idx = i;
221                         info->symtab_shndx_start = (void *)hdr +
222                             sechdrs[i].sh_offset;
223                         info->symtab_shndx_stop  = (void *)hdr +
224                             sechdrs[i].sh_offset + sechdrs[i].sh_size;
225                 }
226         }
227         if (!info->symtab_start)
228                 fprintf(stderr, "%s has no symtab?\n", filename);
229
230         /* Fix endianness in symbols */
231         for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
232                 sym->st_shndx = TO_NATIVE(endian, 16, sym->st_shndx);
233                 sym->st_name  = TO_NATIVE(endian, 32, sym->st_name);
234                 sym->st_value = TO_NATIVE(endian, ADDR_SIZE, sym->st_value);
235                 sym->st_size  = TO_NATIVE(endian, ADDR_SIZE, sym->st_size);
236         }
237
238         if (symtab_shndx_idx != ~0U) {
239                 Elf32_Word *p;
240                 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
241                         fprintf(stderr,
242                               "%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
243                               filename, sechdrs[symtab_shndx_idx].sh_link,
244                               symtab_idx);
245                 /* Fix endianness */
246                 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
247                      p++)
248                         *p = TO_NATIVE(endian, 32, *p);
249         }
250
251         return 1;
252 }
253
254 static void parse_elf_finish(struct elf_info *info)
255 {
256         struct pmd_driver *tmp, *idx = info->drivers;
257         release_file(info->hdr, info->size);
258         while (idx) {
259                 tmp = idx->next;
260                 free(idx);
261                 idx = tmp;
262         }
263 }
264
265 static const char *sec_name(struct elf_info *elf, int secindex)
266 {
267         Elf_Shdr *sechdrs = elf->sechdrs;
268         return (void *)elf->hdr +
269                 elf->sechdrs[elf->secindex_strings].sh_offset +
270                 sechdrs[secindex].sh_name;
271 }
272
273 static int get_symbol_index(struct elf_info *info, Elf_Sym *sym)
274 {
275         const char *name =  sym_name(info, sym);
276         const char *idx;
277
278         idx = name;
279         while (idx) {
280                 if (isdigit(*idx))
281                         return atoi(idx);
282                 idx++;
283         }
284         return -1;
285 }
286
287 struct opt_tag {
288         const char *suffix;
289         const char *json_id;
290 };
291
292 static const struct opt_tag opt_tags[] = {
293         {"_param_string_export", "params"},
294 };
295
296 static int complete_pmd_entry(struct elf_info *info, struct pmd_driver *drv)
297 {
298         const char *tname;
299         int i;
300         char tmpsymname[128];
301         Elf_Sym *tmpsym;
302
303         drv->name = get_sym_value(info, drv->name_sym);
304
305         for (i = 0; i < PMD_OPT_MAX; i++) {
306                 memset(tmpsymname, 0, 128);
307                 sprintf(tmpsymname, "__%s%s", drv->name, opt_tags[i].suffix);
308                 tmpsym = find_sym_in_symtab(info, tmpsymname, NULL);
309                 if (!tmpsym)
310                         continue;
311                 drv->opt_vals[i] = get_sym_value(info, tmpsym);
312         }
313
314         memset(tmpsymname, 0, 128);
315         sprintf(tmpsymname, "__%s_pci_tbl_export", drv->name);
316
317         tmpsym = find_sym_in_symtab(info, tmpsymname, NULL);
318
319
320         /*
321          * If this returns NULL, then this is a PMD_VDEV, because
322          * it has no pci table reference
323          */
324         if (!tmpsym) {
325                 drv->pci_tbl = NULL;
326                 return 0;
327         }
328
329         tname = get_sym_value(info, tmpsym);
330         tmpsym = find_sym_in_symtab(info, tname, NULL);
331         if (!tmpsym)
332                 return -ENOENT;
333
334         drv->pci_tbl = (struct rte_pci_id *)get_sym_value(info, tmpsym);
335         if (!drv->pci_tbl)
336                 return -ENOENT;
337
338         return 0;
339 }
340
341 static int locate_pmd_entries(struct elf_info *info)
342 {
343         Elf_Sym *last = NULL;
344         struct pmd_driver *new;
345
346         info->drivers = NULL;
347
348         do {
349                 new = calloc(sizeof(struct pmd_driver), 1);
350                 new->name_sym = find_sym_in_symtab(info, "this_pmd_name", last);
351                 last = new->name_sym;
352                 if (!new->name_sym)
353                         free(new);
354                 else {
355                         if (complete_pmd_entry(info, new)) {
356                                 fprintf(stderr,
357                                         "Failed to complete pmd entry\n");
358                                 free(new);
359                         } else {
360                                 new->next = info->drivers;
361                                 info->drivers = new;
362                         }
363                 }
364         } while (last);
365 }
366
367 static void output_pmd_info_string(struct elf_info *info, char *outfile)
368 {
369         FILE *ofd;
370         struct pmd_driver *drv;
371         struct rte_pci_id *pci_ids;
372         int idx = 0;
373
374         ofd = fopen(outfile, "w+");
375         if (!ofd) {
376                 fprintf(stderr, "Unable to open output file\n");
377                 return;
378         }
379
380         drv = info->drivers;
381
382         while (drv) {
383                 fprintf(ofd, "const char %s_pmd_info[] __attribute__((used)) = "
384                         "\"PMD_INFO_STRING= {",
385                         drv->name);
386                 fprintf(ofd, "\\\"name\\\" : \\\"%s\\\", ", drv->name);
387
388                 for (idx = 0; idx < PMD_OPT_MAX; idx++) {
389                         if (drv->opt_vals[idx])
390                                 fprintf(ofd, "\\\"%s\\\" : \\\"%s\\\", ",
391                                         opt_tags[idx].json_id,
392                                         drv->opt_vals[idx]);
393                 }
394
395                 pci_ids = drv->pci_tbl;
396                 fprintf(ofd, "\\\"pci_ids\\\" : [");
397
398                 while (pci_ids && pci_ids->device_id) {
399                         fprintf(ofd, "[%d, %d, %d, %d]",
400                                 pci_ids->vendor_id, pci_ids->device_id,
401                                 pci_ids->subsystem_vendor_id,
402                                 pci_ids->subsystem_device_id);
403                         pci_ids++;
404                         if (pci_ids->device_id)
405                                 fprintf(ofd, ",");
406                         else
407                                 fprintf(ofd, " ");
408                 }
409                 fprintf(ofd, "]}\";");
410                 drv = drv->next;
411         }
412
413         fclose(ofd);
414 }
415
416 int main(int argc, char **argv)
417 {
418         struct elf_info info;
419         int rc = 1;
420
421         if (argc < 3) {
422                 fprintf(stderr,
423                         "usage: pmdinfo <object file> <c output file>\n");
424                 exit(127);
425         }
426         parse_elf(&info, argv[1]);
427
428         locate_pmd_entries(&info);
429
430         if (info.drivers) {
431                 output_pmd_info_string(&info, argv[2]);
432                 rc = 0;
433         } else {
434                 fprintf(stderr, "No drivers registered\n");
435         }
436
437         parse_elf_finish(&info);
438         exit(rc);
439 }