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