160867b9f36c161d74d8eb1b145d97c62fc2a106
[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 #ifdef __linux__
20 #include <endian.h>
21 #else
22 #include <sys/endian.h>
23 #endif
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <elf.h>
27 #include <rte_pci.h>
28
29 /* On BSD-alike OSes elf.h defines these according to host's word size */
30 #undef ELF_ST_BIND
31 #undef ELF_ST_TYPE
32 #undef ELF_R_SYM
33 #undef ELF_R_TYPE
34
35 /*
36  * Define ELF64_* to ELF_*, the latter being defined in both 32 and 64 bit
37  * flavors in elf.h.  This makes our code a bit more generic between arches
38  * and allows us to support 32 bit code in the future should we ever want to
39  */
40 #ifdef RTE_ARCH_64
41 #define Elf_Ehdr    Elf64_Ehdr
42 #define Elf_Shdr    Elf64_Shdr
43 #define Elf_Sym     Elf64_Sym
44 #define Elf_Addr    Elf64_Addr
45 #define Elf_Sword   Elf64_Sxword
46 #define Elf_Section Elf64_Half
47 #define ELF_ST_BIND ELF64_ST_BIND
48 #define ELF_ST_TYPE ELF64_ST_TYPE
49
50 #define Elf_Rel     Elf64_Rel
51 #define Elf_Rela    Elf64_Rela
52 #define ELF_R_SYM   ELF64_R_SYM
53 #define ELF_R_TYPE  ELF64_R_TYPE
54 #else
55 #define Elf_Ehdr    Elf32_Ehdr
56 #define Elf_Shdr    Elf32_Shdr
57 #define Elf_Sym     Elf32_Sym
58 #define Elf_Addr    Elf32_Addr
59 #define Elf_Sword   Elf32_Sxword
60 #define Elf_Section Elf32_Half
61 #define ELF_ST_BIND ELF32_ST_BIND
62 #define ELF_ST_TYPE ELF32_ST_TYPE
63
64 #define Elf_Rel     Elf32_Rel
65 #define Elf_Rela    Elf32_Rela
66 #define ELF_R_SYM   ELF32_R_SYM
67 #define ELF_R_TYPE  ELF32_R_TYPE
68 #endif
69
70
71 /*
72  * Note, it seems odd that we have both a CONVERT_NATIVE and a TO_NATIVE macro
73  * below.  We do this because the values passed to TO_NATIVE may themselves be
74  * macros and need both macros here to get expanded.  Specifically its the width
75  * variable we are concerned with, because it needs to get expanded prior to
76  * string concatenation
77  */
78 #define CONVERT_NATIVE(fend, width, x) ({ \
79 typeof(x) ___x; \
80 if ((fend) == ELFDATA2LSB) \
81         ___x = le##width##toh(x); \
82 else \
83         ___x = be##width##toh(x); \
84         ___x; \
85 })
86
87 #define TO_NATIVE(fend, width, x) CONVERT_NATIVE(fend, width, x)
88
89 enum opt_params {
90         PMD_PARAM_STRING = 0,
91         PMD_KMOD_DEP,
92         PMD_OPT_MAX
93 };
94
95 struct pmd_driver {
96         Elf_Sym *name_sym;
97         const char *name;
98         struct rte_pci_id *pci_tbl;
99         struct pmd_driver *next;
100
101         const char *opt_vals[PMD_OPT_MAX];
102 };
103
104 struct elf_info {
105         unsigned long size;
106         Elf_Ehdr     *hdr;
107         Elf_Shdr     *sechdrs;
108         Elf_Sym      *symtab_start;
109         Elf_Sym      *symtab_stop;
110         char         *strtab;
111
112         /* support for 32bit section numbers */
113
114         unsigned int num_sections; /* max_secindex + 1 */
115         unsigned int secindex_strings;
116         /* if Nth symbol table entry has .st_shndx = SHN_XINDEX,
117          * take shndx from symtab_shndx_start[N] instead
118          */
119         Elf32_Word   *symtab_shndx_start;
120         Elf32_Word   *symtab_shndx_stop;
121
122         struct pmd_driver *drivers;
123 };