pmdinfogen: add buildtools and pmdinfogen utility
[dpdk.git] / buildtools / pmdinfogen / pmdinfogen.h
1
2 /* Postprocess pmd object files to export hw support
3  *
4  * Copyright 2016 Neil Horman <nhorman@tuxdriver.com>
5  * Based in part on modpost.c from the linux kernel
6  *
7  * This software may be used and distributed according to the terms
8  * of the GNU General Public License V2, incorporated herein by reference.
9  *
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <sys/mman.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <elf.h>
22 #include <rte_config.h>
23 #include <rte_pci.h>
24 #include <rte_byteorder.h>
25
26 /* On BSD-alike OSes elf.h defines these according to host's word size */
27 #undef ELF_ST_BIND
28 #undef ELF_ST_TYPE
29 #undef ELF_R_SYM
30 #undef ELF_R_TYPE
31
32 /*
33  * Define ELF64_* to ELF_*, the latter being defined in both 32 and 64 bit
34  * flavors in elf.h.  This makes our code a bit more generic between arches
35  * and allows us to support 32 bit code in the future should we ever want to
36  */
37 #ifdef RTE_ARCH_64
38 #define Elf_Ehdr    Elf64_Ehdr
39 #define Elf_Shdr    Elf64_Shdr
40 #define Elf_Sym     Elf64_Sym
41 #define Elf_Addr    Elf64_Addr
42 #define Elf_Sword   Elf64_Sxword
43 #define Elf_Section Elf64_Half
44 #define ELF_ST_BIND ELF64_ST_BIND
45 #define ELF_ST_TYPE ELF64_ST_TYPE
46
47 #define Elf_Rel     Elf64_Rel
48 #define Elf_Rela    Elf64_Rela
49 #define ELF_R_SYM   ELF64_R_SYM
50 #define ELF_R_TYPE  ELF64_R_TYPE
51 #else
52 #define Elf_Ehdr    Elf32_Ehdr
53 #define Elf_Shdr    Elf32_Shdr
54 #define Elf_Sym     Elf32_Sym
55 #define Elf_Addr    Elf32_Addr
56 #define Elf_Sword   Elf32_Sxword
57 #define Elf_Section Elf32_Half
58 #define ELF_ST_BIND ELF32_ST_BIND
59 #define ELF_ST_TYPE ELF32_ST_TYPE
60
61 #define Elf_Rel     Elf32_Rel
62 #define Elf_Rela    Elf32_Rela
63 #define ELF_R_SYM   ELF32_R_SYM
64 #define ELF_R_TYPE  ELF32_R_TYPE
65 #endif
66
67
68 /*
69  * Note, it seems odd that we have both a CONVERT_NATIVE and a TO_NATIVE macro
70  * below.  We do this because the values passed to TO_NATIVE may themselves be
71  * macros and need both macros here to get expanded.  Specifically its the width
72  * variable we are concerned with, because it needs to get expanded prior to
73  * string concatenation
74  */
75 #define CONVERT_NATIVE(fend, width, x) ({ \
76 typeof(x) ___x; \
77 if ((fend) == ELFDATA2LSB) \
78         ___x = rte_le_to_cpu_##width(x); \
79 else \
80         ___x = rte_be_to_cpu_##width(x); \
81         ___x; \
82 })
83
84 #define TO_NATIVE(fend, width, x) CONVERT_NATIVE(fend, width, x)
85
86 enum opt_params {
87         PMD_PARAM_STRING = 0,
88         PMD_OPT_MAX
89 };
90
91 struct pmd_driver {
92         Elf_Sym *name_sym;
93         const char *name;
94         struct rte_pci_id *pci_tbl;
95         struct pmd_driver *next;
96
97         const char *opt_vals[PMD_OPT_MAX];
98 };
99
100 struct elf_info {
101         unsigned long size;
102         Elf_Ehdr     *hdr;
103         Elf_Shdr     *sechdrs;
104         Elf_Sym      *symtab_start;
105         Elf_Sym      *symtab_stop;
106         char         *strtab;
107
108         /* support for 32bit section numbers */
109
110         unsigned int num_sections; /* max_secindex + 1 */
111         unsigned int secindex_strings;
112         /* if Nth symbol table entry has .st_shndx = SHN_XINDEX,
113          * take shndx from symtab_shndx_start[N] instead
114          */
115         Elf32_Word   *symtab_shndx_start;
116         Elf32_Word   *symtab_shndx_stop;
117
118         struct pmd_driver *drivers;
119 };
120