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.
20 #include <rte_common.h>
21 #include "pmdinfogen.h"
29 static int use_stdin, use_stdout;
31 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
34 return elf->strtab + sym->st_name;
39 static void *grab_file(const char *filename, unsigned long *size)
42 void *map = MAP_FAILED;
46 fd = open(filename, O_RDONLY);
50 /* from stdin, use a temporary file to mmap */
60 fd = dup(fileno(infile));
65 n = read(STDIN_FILENO, buffer, sizeof(buffer));
67 if (write(fd, buffer, n) != n)
69 n = read(STDIN_FILENO, buffer, sizeof(buffer));
77 map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
81 if (map == MAP_FAILED)
87 * Return a copy of the next line in a mmap'ed file.
88 * spaces in the beginning of the line is trimmed away.
89 * Return a pointer to a static buffer.
91 static void release_file(void *file, unsigned long size)
97 static void *get_sym_value(struct elf_info *info, const Elf_Sym *sym)
99 return RTE_PTR_ADD(info->hdr,
100 info->sechdrs[sym->st_shndx].sh_offset + sym->st_value);
103 static Elf_Sym *find_sym_in_symtab(struct elf_info *info,
104 const char *name, Elf_Sym *last)
110 idx = info->symtab_start;
112 for (; idx < info->symtab_stop; idx++) {
113 const char *n = sym_name(info, idx);
114 if (!strncmp(n, name, strlen(name)))
120 static int parse_elf(struct elf_info *info, const char *filename)
127 unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
129 hdr = grab_file(filename, &info->size);
135 if (info->size < sizeof(*hdr)) {
136 /* file too small, assume this is an empty .o file */
139 /* Is this a valid ELF file? */
140 if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
141 (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
142 (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
143 (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
144 /* Not an ELF file - silently ignore it */
148 if (!hdr->e_ident[EI_DATA]) {
153 endian = hdr->e_ident[EI_DATA];
155 /* Fix endianness in ELF header */
156 hdr->e_type = TO_NATIVE(endian, 16, hdr->e_type);
157 hdr->e_machine = TO_NATIVE(endian, 16, hdr->e_machine);
158 hdr->e_version = TO_NATIVE(endian, 32, hdr->e_version);
159 hdr->e_entry = TO_NATIVE(endian, ADDR_SIZE, hdr->e_entry);
160 hdr->e_phoff = TO_NATIVE(endian, ADDR_SIZE, hdr->e_phoff);
161 hdr->e_shoff = TO_NATIVE(endian, ADDR_SIZE, hdr->e_shoff);
162 hdr->e_flags = TO_NATIVE(endian, 32, hdr->e_flags);
163 hdr->e_ehsize = TO_NATIVE(endian, 16, hdr->e_ehsize);
164 hdr->e_phentsize = TO_NATIVE(endian, 16, hdr->e_phentsize);
165 hdr->e_phnum = TO_NATIVE(endian, 16, hdr->e_phnum);
166 hdr->e_shentsize = TO_NATIVE(endian, 16, hdr->e_shentsize);
167 hdr->e_shnum = TO_NATIVE(endian, 16, hdr->e_shnum);
168 hdr->e_shstrndx = TO_NATIVE(endian, 16, hdr->e_shstrndx);
170 sechdrs = RTE_PTR_ADD(hdr, hdr->e_shoff);
171 info->sechdrs = sechdrs;
173 /* Check if file offset is correct */
174 if (hdr->e_shoff > info->size) {
175 fprintf(stderr, "section header offset=%lu in file '%s' "
176 "is bigger than filesize=%lu\n",
177 (unsigned long)hdr->e_shoff,
178 filename, info->size);
182 if (hdr->e_shnum == SHN_UNDEF) {
184 * There are more than 64k sections,
185 * read count from .sh_size.
188 TO_NATIVE(endian, ADDR_SIZE, sechdrs[0].sh_size);
190 info->num_sections = hdr->e_shnum;
192 if (hdr->e_shstrndx == SHN_XINDEX)
193 info->secindex_strings =
194 TO_NATIVE(endian, 32, sechdrs[0].sh_link);
196 info->secindex_strings = hdr->e_shstrndx;
198 /* Fix endianness in section headers */
199 for (i = 0; i < info->num_sections; i++) {
201 TO_NATIVE(endian, 32, sechdrs[i].sh_name);
203 TO_NATIVE(endian, 32, sechdrs[i].sh_type);
204 sechdrs[i].sh_flags =
205 TO_NATIVE(endian, 32, sechdrs[i].sh_flags);
207 TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_addr);
208 sechdrs[i].sh_offset =
209 TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_offset);
211 TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_size);
213 TO_NATIVE(endian, 32, sechdrs[i].sh_link);
215 TO_NATIVE(endian, 32, sechdrs[i].sh_info);
216 sechdrs[i].sh_addralign =
217 TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_addralign);
218 sechdrs[i].sh_entsize =
219 TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_entsize);
221 /* Find symbol table. */
222 for (i = 1; i < info->num_sections; i++) {
223 int nobits = sechdrs[i].sh_type == SHT_NOBITS;
225 if (!nobits && sechdrs[i].sh_offset > info->size) {
226 fprintf(stderr, "%s is truncated. "
227 "sechdrs[i].sh_offset=%lu > sizeof(*hrd)=%zu\n",
228 filename, (unsigned long)sechdrs[i].sh_offset,
233 if (sechdrs[i].sh_type == SHT_SYMTAB) {
234 unsigned int sh_link_idx;
236 info->symtab_start = RTE_PTR_ADD(hdr,
237 sechdrs[i].sh_offset);
238 info->symtab_stop = RTE_PTR_ADD(hdr,
239 sechdrs[i].sh_offset + sechdrs[i].sh_size);
240 sh_link_idx = sechdrs[i].sh_link;
241 info->strtab = RTE_PTR_ADD(hdr,
242 sechdrs[sh_link_idx].sh_offset);
245 /* 32bit section no. table? ("more than 64k sections") */
246 if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
247 symtab_shndx_idx = i;
248 info->symtab_shndx_start = RTE_PTR_ADD(hdr,
249 sechdrs[i].sh_offset);
250 info->symtab_shndx_stop = RTE_PTR_ADD(hdr,
251 sechdrs[i].sh_offset + sechdrs[i].sh_size);
254 if (!info->symtab_start)
255 fprintf(stderr, "%s has no symtab?\n", filename);
257 /* Fix endianness in symbols */
258 for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
259 sym->st_shndx = TO_NATIVE(endian, 16, sym->st_shndx);
260 sym->st_name = TO_NATIVE(endian, 32, sym->st_name);
261 sym->st_value = TO_NATIVE(endian, ADDR_SIZE, sym->st_value);
262 sym->st_size = TO_NATIVE(endian, ADDR_SIZE, sym->st_size);
266 if (symtab_shndx_idx != ~0U) {
268 if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
270 "%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
271 filename, sechdrs[symtab_shndx_idx].sh_link,
274 for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
276 *p = TO_NATIVE(endian, 32, *p);
282 static void parse_elf_finish(struct elf_info *info)
284 struct pmd_driver *tmp, *idx = info->drivers;
285 release_file(info->hdr, info->size);
298 static const struct opt_tag opt_tags[] = {
299 {"_param_string_export", "params"},
300 {"_kmod_dep_export", "kmod"},
303 static int complete_pmd_entry(struct elf_info *info, struct pmd_driver *drv)
307 char tmpsymname[128];
310 drv->name = get_sym_value(info, drv->name_sym);
312 for (i = 0; i < PMD_OPT_MAX; i++) {
313 memset(tmpsymname, 0, 128);
314 sprintf(tmpsymname, "__%s%s", drv->name, opt_tags[i].suffix);
315 tmpsym = find_sym_in_symtab(info, tmpsymname, NULL);
318 drv->opt_vals[i] = get_sym_value(info, tmpsym);
321 memset(tmpsymname, 0, 128);
322 sprintf(tmpsymname, "__%s_pci_tbl_export", drv->name);
324 tmpsym = find_sym_in_symtab(info, tmpsymname, NULL);
328 * If this returns NULL, then this is a PMD_VDEV, because
329 * it has no pci table reference
336 tname = get_sym_value(info, tmpsym);
337 tmpsym = find_sym_in_symtab(info, tname, NULL);
341 drv->pci_tbl = (struct rte_pci_id *)get_sym_value(info, tmpsym);
348 static int locate_pmd_entries(struct elf_info *info)
350 Elf_Sym *last = NULL;
351 struct pmd_driver *new;
353 info->drivers = NULL;
356 new = calloc(sizeof(struct pmd_driver), 1);
358 fprintf(stderr, "Failed to calloc memory\n");
361 new->name_sym = find_sym_in_symtab(info, "this_pmd_name", last);
362 last = new->name_sym;
366 if (complete_pmd_entry(info, new)) {
368 "Failed to complete pmd entry\n");
371 new->next = info->drivers;
380 static void output_pmd_info_string(struct elf_info *info, char *outfile)
383 struct pmd_driver *drv;
384 struct rte_pci_id *pci_ids;
390 ofd = fopen(outfile, "w+");
392 fprintf(stderr, "Unable to open output file\n");
400 fprintf(ofd, "const char %s_pmd_info[] __attribute__((used)) = "
401 "\"PMD_INFO_STRING= {",
403 fprintf(ofd, "\\\"name\\\" : \\\"%s\\\", ", drv->name);
405 for (idx = 0; idx < PMD_OPT_MAX; idx++) {
406 if (drv->opt_vals[idx])
407 fprintf(ofd, "\\\"%s\\\" : \\\"%s\\\", ",
408 opt_tags[idx].json_id,
412 pci_ids = drv->pci_tbl;
413 fprintf(ofd, "\\\"pci_ids\\\" : [");
415 while (pci_ids && pci_ids->device_id) {
416 fprintf(ofd, "[%d, %d, %d, %d]",
417 pci_ids->vendor_id, pci_ids->device_id,
418 pci_ids->subsystem_vendor_id,
419 pci_ids->subsystem_device_id);
421 if (pci_ids->device_id)
426 fprintf(ofd, "]}\";\n");
433 int main(int argc, char **argv)
435 struct elf_info info = {0};
440 "usage: %s <object file> <c output file>\n",
444 use_stdin = !strcmp(argv[1], "-");
445 use_stdout = !strcmp(argv[2], "-");
446 parse_elf(&info, argv[1]);
448 if (locate_pmd_entries(&info) < 0)
452 output_pmd_info_string(&info, argv[2]);
455 fprintf(stderr, "No drivers registered\n");
458 parse_elf_finish(&info);