1 /* SPDX-License-Identifier: GPL-2.0
2 * Postprocess pmd object files to export hw support
4 * Copyright 2016 Neil Horman <nhorman@tuxdriver.com>
5 * Based in part on modpost.c from the linux kernel
16 #include <rte_common.h>
17 #include "pmdinfogen.h"
25 static int use_stdin, use_stdout;
27 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
30 return elf->strtab + sym->st_name;
35 static void *grab_file(const char *filename, unsigned long *size)
38 void *map = MAP_FAILED;
42 fd = open(filename, O_RDONLY);
46 /* from stdin, use a temporary file to mmap */
56 fd = dup(fileno(infile));
61 n = read(STDIN_FILENO, buffer, sizeof(buffer));
63 if (write(fd, buffer, n) != n)
65 n = read(STDIN_FILENO, buffer, sizeof(buffer));
73 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
77 if (map == MAP_FAILED)
83 * Return a copy of the next line in a mmap'ed file.
84 * spaces in the beginning of the line is trimmed away.
85 * Return a pointer to a static buffer.
87 static void release_file(void *file, unsigned long size)
93 static void *get_sym_value(struct elf_info *info, const Elf_Sym *sym)
95 return RTE_PTR_ADD(info->hdr,
96 info->sechdrs[sym->st_shndx].sh_offset + sym->st_value);
99 static Elf_Sym *find_sym_in_symtab(struct elf_info *info,
100 const char *name, Elf_Sym *last)
106 idx = info->symtab_start;
108 for (; idx < info->symtab_stop; idx++) {
109 const char *n = sym_name(info, idx);
110 if (!strncmp(n, name, strlen(name)))
116 static int parse_elf(struct elf_info *info, const char *filename)
123 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
125 hdr = grab_file(filename, &info->size);
131 if (info->size < sizeof(*hdr)) {
132 /* file too small, assume this is an empty .o file */
135 /* Is this a valid ELF file? */
136 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
137 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
138 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
139 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
140 /* Not an ELF file - silently ignore it */
144 if (!hdr->e_ident[EI_DATA]) {
149 endian = hdr->e_ident[EI_DATA];
151 /* Fix endianness in ELF header */
152 hdr->e_type = TO_NATIVE(endian, 16, hdr->e_type);
153 hdr->e_machine = TO_NATIVE(endian, 16, hdr->e_machine);
154 hdr->e_version = TO_NATIVE(endian, 32, hdr->e_version);
155 hdr->e_entry = TO_NATIVE(endian, ADDR_SIZE, hdr->e_entry);
156 hdr->e_phoff = TO_NATIVE(endian, ADDR_SIZE, hdr->e_phoff);
157 hdr->e_shoff = TO_NATIVE(endian, ADDR_SIZE, hdr->e_shoff);
158 hdr->e_flags = TO_NATIVE(endian, 32, hdr->e_flags);
159 hdr->e_ehsize = TO_NATIVE(endian, 16, hdr->e_ehsize);
160 hdr->e_phentsize = TO_NATIVE(endian, 16, hdr->e_phentsize);
161 hdr->e_phnum = TO_NATIVE(endian, 16, hdr->e_phnum);
162 hdr->e_shentsize = TO_NATIVE(endian, 16, hdr->e_shentsize);
163 hdr->e_shnum = TO_NATIVE(endian, 16, hdr->e_shnum);
164 hdr->e_shstrndx = TO_NATIVE(endian, 16, hdr->e_shstrndx);
166 sechdrs = RTE_PTR_ADD(hdr, hdr->e_shoff);
167 info->sechdrs = sechdrs;
169 /* Check if file offset is correct */
170 if (hdr->e_shoff > info->size) {
171 fprintf(stderr, "section header offset=%lu in file '%s' "
172 "is bigger than filesize=%lu\n",
173 (unsigned long)hdr->e_shoff,
174 filename, info->size);
178 if (hdr->e_shnum == SHN_UNDEF) {
180 * There are more than 64k sections,
181 * read count from .sh_size.
184 TO_NATIVE(endian, ADDR_SIZE, sechdrs[0].sh_size);
186 info->num_sections = hdr->e_shnum;
188 if (hdr->e_shstrndx == SHN_XINDEX)
189 info->secindex_strings =
190 TO_NATIVE(endian, 32, sechdrs[0].sh_link);
192 info->secindex_strings = hdr->e_shstrndx;
194 /* Fix endianness in section headers */
195 for (i = 0; i < info->num_sections; i++) {
197 TO_NATIVE(endian, 32, sechdrs[i].sh_name);
199 TO_NATIVE(endian, 32, sechdrs[i].sh_type);
200 sechdrs[i].sh_flags =
201 TO_NATIVE(endian, 32, sechdrs[i].sh_flags);
203 TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_addr);
204 sechdrs[i].sh_offset =
205 TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_offset);
207 TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_size);
209 TO_NATIVE(endian, 32, sechdrs[i].sh_link);
211 TO_NATIVE(endian, 32, sechdrs[i].sh_info);
212 sechdrs[i].sh_addralign =
213 TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_addralign);
214 sechdrs[i].sh_entsize =
215 TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_entsize);
217 /* Find symbol table. */
218 for (i = 1; i < info->num_sections; i++) {
219 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
221 if (!nobits && sechdrs[i].sh_offset > info->size) {
222 fprintf(stderr, "%s is truncated. "
223 "sechdrs[i].sh_offset=%lu > sizeof(*hrd)=%zu\n",
224 filename, (unsigned long)sechdrs[i].sh_offset,
229 if (sechdrs[i].sh_type == SHT_SYMTAB) {
230 unsigned int sh_link_idx;
232 info->symtab_start = RTE_PTR_ADD(hdr,
233 sechdrs[i].sh_offset);
234 info->symtab_stop = RTE_PTR_ADD(hdr,
235 sechdrs[i].sh_offset + sechdrs[i].sh_size);
236 sh_link_idx = sechdrs[i].sh_link;
237 info->strtab = RTE_PTR_ADD(hdr,
238 sechdrs[sh_link_idx].sh_offset);
241 /* 32bit section no. table? ("more than 64k sections") */
242 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
243 symtab_shndx_idx = i;
244 info->symtab_shndx_start = RTE_PTR_ADD(hdr,
245 sechdrs[i].sh_offset);
246 info->symtab_shndx_stop = RTE_PTR_ADD(hdr,
247 sechdrs[i].sh_offset + sechdrs[i].sh_size);
250 if (!info->symtab_start)
251 fprintf(stderr, "%s has no symtab?\n", filename);
253 /* Fix endianness in symbols */
254 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
255 sym->st_shndx = TO_NATIVE(endian, 16, sym->st_shndx);
256 sym->st_name = TO_NATIVE(endian, 32, sym->st_name);
257 sym->st_value = TO_NATIVE(endian, ADDR_SIZE, sym->st_value);
258 sym->st_size = TO_NATIVE(endian, ADDR_SIZE, sym->st_size);
262 if (symtab_shndx_idx != ~0U) {
264 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
266 "%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
267 filename, sechdrs[symtab_shndx_idx].sh_link,
270 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
272 *p = TO_NATIVE(endian, 32, *p);
278 static void parse_elf_finish(struct elf_info *info)
280 struct pmd_driver *tmp, *idx = info->drivers;
281 release_file(info->hdr, info->size);
294 static const struct opt_tag opt_tags[] = {
295 {"_param_string_export", "params"},
296 {"_kmod_dep_export", "kmod"},
299 static int complete_pmd_entry(struct elf_info *info, struct pmd_driver *drv)
303 char tmpsymname[128];
306 drv->name = get_sym_value(info, drv->name_sym);
308 for (i = 0; i < PMD_OPT_MAX; i++) {
309 memset(tmpsymname, 0, 128);
310 sprintf(tmpsymname, "__%s%s", drv->name, opt_tags[i].suffix);
311 tmpsym = find_sym_in_symtab(info, tmpsymname, NULL);
314 drv->opt_vals[i] = get_sym_value(info, tmpsym);
317 memset(tmpsymname, 0, 128);
318 sprintf(tmpsymname, "__%s_pci_tbl_export", drv->name);
320 tmpsym = find_sym_in_symtab(info, tmpsymname, NULL);
324 * If this returns NULL, then this is a PMD_VDEV, because
325 * it has no pci table reference
332 tname = get_sym_value(info, tmpsym);
333 tmpsym = find_sym_in_symtab(info, tname, NULL);
337 drv->pci_tbl = (struct rte_pci_id *)get_sym_value(info, tmpsym);
344 static int locate_pmd_entries(struct elf_info *info)
346 Elf_Sym *last = NULL;
347 struct pmd_driver *new;
349 info->drivers = NULL;
352 new = calloc(sizeof(struct pmd_driver), 1);
354 fprintf(stderr, "Failed to calloc memory\n");
357 new->name_sym = find_sym_in_symtab(info, "this_pmd_name", last);
358 last = new->name_sym;
362 if (complete_pmd_entry(info, new)) {
364 "Failed to complete pmd entry\n");
367 new->next = info->drivers;
376 static void output_pmd_info_string(struct elf_info *info, char *outfile)
379 struct pmd_driver *drv;
380 struct rte_pci_id *pci_ids;
386 ofd = fopen(outfile, "w+");
388 fprintf(stderr, "Unable to open output file\n");
396 fprintf(ofd, "const char %s_pmd_info[] __attribute__((used)) = "
397 "\"PMD_INFO_STRING= {",
399 fprintf(ofd, "\\\"name\\\" : \\\"%s\\\", ", drv->name);
401 for (idx = 0; idx < PMD_OPT_MAX; idx++) {
402 if (drv->opt_vals[idx])
403 fprintf(ofd, "\\\"%s\\\" : \\\"%s\\\", ",
404 opt_tags[idx].json_id,
408 pci_ids = drv->pci_tbl;
409 fprintf(ofd, "\\\"pci_ids\\\" : [");
411 while (pci_ids && pci_ids->device_id) {
412 fprintf(ofd, "[%d, %d, %d, %d]",
413 pci_ids->vendor_id, pci_ids->device_id,
414 pci_ids->subsystem_vendor_id,
415 pci_ids->subsystem_device_id);
417 if (pci_ids->device_id)
422 fprintf(ofd, "]}\";\n");
429 int main(int argc, char **argv)
431 struct elf_info info = {0};
436 "usage: %s <object file> <c output file>\n",
440 use_stdin = !strcmp(argv[1], "-");
441 use_stdout = !strcmp(argv[2], "-");
442 parse_elf(&info, argv[1]);
444 if (locate_pmd_entries(&info) < 0)
448 output_pmd_info_string(&info, argv[2]);
451 fprintf(stderr, "No drivers registered\n");
454 parse_elf_finish(&info);