1 /* Postprocess pmd object files to export hw support
3 * Copyright 2016 Neil Horman <nhorman@tuxdriver.com>
4 * Based in part on modpost.c from the linux kernel
6 * This software may be used and distributed according to the terms
7 * of the GNU General Public License V2, incorporated herein by reference.
18 #include <rte_common.h>
19 #include "pmdinfogen.h"
28 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
31 return elf->strtab + sym->st_name;
36 static void *grab_file(const char *filename, unsigned long *size)
39 void *map = MAP_FAILED;
42 fd = open(filename, O_RDONLY);
49 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
53 if (map == MAP_FAILED)
59 * Return a copy of the next line in a mmap'ed file.
60 * spaces in the beginning of the line is trimmed away.
61 * Return a pointer to a static buffer.
63 static void release_file(void *file, unsigned long size)
69 static void *get_sym_value(struct elf_info *info, const Elf_Sym *sym)
71 return RTE_PTR_ADD(info->hdr,
72 info->sechdrs[sym->st_shndx].sh_offset + sym->st_value);
75 static Elf_Sym *find_sym_in_symtab(struct elf_info *info,
76 const char *name, Elf_Sym *last)
82 idx = info->symtab_start;
84 for (; idx < info->symtab_stop; idx++) {
85 const char *n = sym_name(info, idx);
86 if (!strncmp(n, name, strlen(name)))
92 static int parse_elf(struct elf_info *info, const char *filename)
99 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
101 hdr = grab_file(filename, &info->size);
107 if (info->size < sizeof(*hdr)) {
108 /* file too small, assume this is an empty .o file */
111 /* Is this a valid ELF file? */
112 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
113 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
114 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
115 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
116 /* Not an ELF file - silently ignore it */
120 if (!hdr->e_ident[EI_DATA]) {
125 endian = hdr->e_ident[EI_DATA];
127 /* Fix endianness in ELF header */
128 hdr->e_type = TO_NATIVE(endian, 16, hdr->e_type);
129 hdr->e_machine = TO_NATIVE(endian, 16, hdr->e_machine);
130 hdr->e_version = TO_NATIVE(endian, 32, hdr->e_version);
131 hdr->e_entry = TO_NATIVE(endian, ADDR_SIZE, hdr->e_entry);
132 hdr->e_phoff = TO_NATIVE(endian, ADDR_SIZE, hdr->e_phoff);
133 hdr->e_shoff = TO_NATIVE(endian, ADDR_SIZE, hdr->e_shoff);
134 hdr->e_flags = TO_NATIVE(endian, 32, hdr->e_flags);
135 hdr->e_ehsize = TO_NATIVE(endian, 16, hdr->e_ehsize);
136 hdr->e_phentsize = TO_NATIVE(endian, 16, hdr->e_phentsize);
137 hdr->e_phnum = TO_NATIVE(endian, 16, hdr->e_phnum);
138 hdr->e_shentsize = TO_NATIVE(endian, 16, hdr->e_shentsize);
139 hdr->e_shnum = TO_NATIVE(endian, 16, hdr->e_shnum);
140 hdr->e_shstrndx = TO_NATIVE(endian, 16, hdr->e_shstrndx);
142 sechdrs = RTE_PTR_ADD(hdr, hdr->e_shoff);
143 info->sechdrs = sechdrs;
145 /* Check if file offset is correct */
146 if (hdr->e_shoff > info->size) {
147 fprintf(stderr, "section header offset=%lu in file '%s' "
148 "is bigger than filesize=%lu\n",
149 (unsigned long)hdr->e_shoff,
150 filename, info->size);
154 if (hdr->e_shnum == SHN_UNDEF) {
156 * There are more than 64k sections,
157 * read count from .sh_size.
159 info->num_sections = TO_NATIVE(endian, 32, sechdrs[0].sh_size);
161 info->num_sections = hdr->e_shnum;
163 if (hdr->e_shstrndx == SHN_XINDEX)
164 info->secindex_strings =
165 TO_NATIVE(endian, 32, sechdrs[0].sh_link);
167 info->secindex_strings = hdr->e_shstrndx;
169 /* Fix endianness in section headers */
170 for (i = 0; i < info->num_sections; i++) {
172 TO_NATIVE(endian, 32, sechdrs[i].sh_name);
174 TO_NATIVE(endian, 32, sechdrs[i].sh_type);
175 sechdrs[i].sh_flags =
176 TO_NATIVE(endian, 32, sechdrs[i].sh_flags);
178 TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_addr);
179 sechdrs[i].sh_offset =
180 TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_offset);
182 TO_NATIVE(endian, 32, sechdrs[i].sh_size);
184 TO_NATIVE(endian, 32, sechdrs[i].sh_link);
186 TO_NATIVE(endian, 32, sechdrs[i].sh_info);
187 sechdrs[i].sh_addralign =
188 TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_addralign);
189 sechdrs[i].sh_entsize =
190 TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_entsize);
192 /* Find symbol table. */
193 for (i = 1; i < info->num_sections; i++) {
194 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
196 if (!nobits && sechdrs[i].sh_offset > info->size) {
197 fprintf(stderr, "%s is truncated. "
198 "sechdrs[i].sh_offset=%lu > sizeof(*hrd)=%zu\n",
199 filename, (unsigned long)sechdrs[i].sh_offset,
204 if (sechdrs[i].sh_type == SHT_SYMTAB) {
205 unsigned int sh_link_idx;
207 info->symtab_start = RTE_PTR_ADD(hdr,
208 sechdrs[i].sh_offset);
209 info->symtab_stop = RTE_PTR_ADD(hdr,
210 sechdrs[i].sh_offset + sechdrs[i].sh_size);
211 sh_link_idx = sechdrs[i].sh_link;
212 info->strtab = RTE_PTR_ADD(hdr,
213 sechdrs[sh_link_idx].sh_offset);
216 /* 32bit section no. table? ("more than 64k sections") */
217 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
218 symtab_shndx_idx = i;
219 info->symtab_shndx_start = RTE_PTR_ADD(hdr,
220 sechdrs[i].sh_offset);
221 info->symtab_shndx_stop = RTE_PTR_ADD(hdr,
222 sechdrs[i].sh_offset + sechdrs[i].sh_size);
225 if (!info->symtab_start)
226 fprintf(stderr, "%s has no symtab?\n", filename);
228 /* Fix endianness in symbols */
229 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
230 sym->st_shndx = TO_NATIVE(endian, 16, sym->st_shndx);
231 sym->st_name = TO_NATIVE(endian, 32, sym->st_name);
232 sym->st_value = TO_NATIVE(endian, ADDR_SIZE, sym->st_value);
233 sym->st_size = TO_NATIVE(endian, ADDR_SIZE, sym->st_size);
236 if (symtab_shndx_idx != ~0U) {
238 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
240 "%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
241 filename, sechdrs[symtab_shndx_idx].sh_link,
244 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
246 *p = TO_NATIVE(endian, 32, *p);
252 static void parse_elf_finish(struct elf_info *info)
254 struct pmd_driver *tmp, *idx = info->drivers;
255 release_file(info->hdr, info->size);
268 static const struct opt_tag opt_tags[] = {
269 {"_param_string_export", "params"},
272 static int complete_pmd_entry(struct elf_info *info, struct pmd_driver *drv)
276 char tmpsymname[128];
279 drv->name = get_sym_value(info, drv->name_sym);
281 for (i = 0; i < PMD_OPT_MAX; i++) {
282 memset(tmpsymname, 0, 128);
283 sprintf(tmpsymname, "__%s%s", drv->name, opt_tags[i].suffix);
284 tmpsym = find_sym_in_symtab(info, tmpsymname, NULL);
287 drv->opt_vals[i] = get_sym_value(info, tmpsym);
290 memset(tmpsymname, 0, 128);
291 sprintf(tmpsymname, "__%s_pci_tbl_export", drv->name);
293 tmpsym = find_sym_in_symtab(info, tmpsymname, NULL);
297 * If this returns NULL, then this is a PMD_VDEV, because
298 * it has no pci table reference
305 tname = get_sym_value(info, tmpsym);
306 tmpsym = find_sym_in_symtab(info, tname, NULL);
310 drv->pci_tbl = (struct rte_pci_id *)get_sym_value(info, tmpsym);
317 static int locate_pmd_entries(struct elf_info *info)
319 Elf_Sym *last = NULL;
320 struct pmd_driver *new;
322 info->drivers = NULL;
325 new = calloc(sizeof(struct pmd_driver), 1);
326 new->name_sym = find_sym_in_symtab(info, "this_pmd_name", last);
327 last = new->name_sym;
331 if (complete_pmd_entry(info, new)) {
333 "Failed to complete pmd entry\n");
336 new->next = info->drivers;
345 static void output_pmd_info_string(struct elf_info *info, char *outfile)
348 struct pmd_driver *drv;
349 struct rte_pci_id *pci_ids;
352 ofd = fopen(outfile, "w+");
354 fprintf(stderr, "Unable to open output file\n");
361 fprintf(ofd, "const char %s_pmd_info[] __attribute__((used)) = "
362 "\"PMD_INFO_STRING= {",
364 fprintf(ofd, "\\\"name\\\" : \\\"%s\\\", ", drv->name);
366 for (idx = 0; idx < PMD_OPT_MAX; idx++) {
367 if (drv->opt_vals[idx])
368 fprintf(ofd, "\\\"%s\\\" : \\\"%s\\\", ",
369 opt_tags[idx].json_id,
373 pci_ids = drv->pci_tbl;
374 fprintf(ofd, "\\\"pci_ids\\\" : [");
376 while (pci_ids && pci_ids->device_id) {
377 fprintf(ofd, "[%d, %d, %d, %d]",
378 pci_ids->vendor_id, pci_ids->device_id,
379 pci_ids->subsystem_vendor_id,
380 pci_ids->subsystem_device_id);
382 if (pci_ids->device_id)
387 fprintf(ofd, "]}\";");
394 int main(int argc, char **argv)
396 struct elf_info info;
401 "usage: pmdinfo <object file> <c output file>\n");
404 parse_elf(&info, argv[1]);
406 locate_pmd_entries(&info);
409 output_pmd_info_string(&info, argv[2]);
412 fprintf(stderr, "No drivers registered\n");
415 parse_elf_finish(&info);