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