1 /* Postprocess pmd object files to export hw support
2  *
3  * Copyright 2016 Neil Horman <[email protected]>
4  * Based in part on modpost.c from the linux kernel
5  *
6  * This software may be used and distributed according to the terms
7  * of the GNU General Public License V2, incorporated herein by reference.
8  *
9  */
10 
11 #define _GNU_SOURCE
12 #include <stdio.h>
13 #include <ctype.h>
14 #include <string.h>
15 #include <limits.h>
16 #include <stdbool.h>
17 #include <errno.h>
18 #include <libgen.h>
19 
20 #include <rte_common.h>
21 #include "pmdinfogen.h"
22 
23 #ifdef RTE_ARCH_64
24 #define ADDR_SIZE 64
25 #else
26 #define ADDR_SIZE 32
27 #endif
28 
29 
30 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
31 {
32 	if (sym)
33 		return elf->strtab + sym->st_name;
34 	else
35 		return "(unknown)";
36 }
37 
38 static void *grab_file(const char *filename, unsigned long *size)
39 {
40 	struct stat st;
41 	void *map = MAP_FAILED;
42 	int fd;
43 
44 	fd = open(filename, O_RDONLY);
45 	if (fd < 0)
46 		return NULL;
47 	if (fstat(fd, &st))
48 		goto failed;
49 
50 	*size = st.st_size;
51 	map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
52 
53 failed:
54 	close(fd);
55 	if (map == MAP_FAILED)
56 		return NULL;
57 	return map;
58 }
59 
60 /**
61   * Return a copy of the next line in a mmap'ed file.
62   * spaces in the beginning of the line is trimmed away.
63   * Return a pointer to a static buffer.
64   **/
65 static void release_file(void *file, unsigned long size)
66 {
67 	munmap(file, size);
68 }
69 
70 
71 static void *get_sym_value(struct elf_info *info, const Elf_Sym *sym)
72 {
73 	return RTE_PTR_ADD(info->hdr,
74 		info->sechdrs[sym->st_shndx].sh_offset + sym->st_value);
75 }
76 
77 static Elf_Sym *find_sym_in_symtab(struct elf_info *info,
78 				   const char *name, Elf_Sym *last)
79 {
80 	Elf_Sym *idx;
81 	if (last)
82 		idx = last+1;
83 	else
84 		idx = info->symtab_start;
85 
86 	for (; idx < info->symtab_stop; idx++) {
87 		const char *n = sym_name(info, idx);
88 		if (!strncmp(n, name, strlen(name)))
89 			return idx;
90 	}
91 	return NULL;
92 }
93 
94 static int parse_elf(struct elf_info *info, const char *filename)
95 {
96 	unsigned int i;
97 	Elf_Ehdr *hdr;
98 	Elf_Shdr *sechdrs;
99 	Elf_Sym  *sym;
100 	int endian;
101 	unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
102 
103 	hdr = grab_file(filename, &info->size);
104 	if (!hdr) {
105 		perror(filename);
106 		exit(1);
107 	}
108 	info->hdr = hdr;
109 	if (info->size < sizeof(*hdr)) {
110 		/* file too small, assume this is an empty .o file */
111 		return 0;
112 	}
113 	/* Is this a valid ELF file? */
114 	if ((hdr->e_ident[EI_MAG0] != ELFMAG0) ||
115 	    (hdr->e_ident[EI_MAG1] != ELFMAG1) ||
116 	    (hdr->e_ident[EI_MAG2] != ELFMAG2) ||
117 	    (hdr->e_ident[EI_MAG3] != ELFMAG3)) {
118 		/* Not an ELF file - silently ignore it */
119 		return 0;
120 	}
121 
122 	if (!hdr->e_ident[EI_DATA]) {
123 		/* Unknown endian */
124 		return 0;
125 	}
126 
127 	endian = hdr->e_ident[EI_DATA];
128 
129 	/* Fix endianness in ELF header */
130 	hdr->e_type      = TO_NATIVE(endian, 16, hdr->e_type);
131 	hdr->e_machine   = TO_NATIVE(endian, 16, hdr->e_machine);
132 	hdr->e_version   = TO_NATIVE(endian, 32, hdr->e_version);
133 	hdr->e_entry     = TO_NATIVE(endian, ADDR_SIZE, hdr->e_entry);
134 	hdr->e_phoff     = TO_NATIVE(endian, ADDR_SIZE, hdr->e_phoff);
135 	hdr->e_shoff     = TO_NATIVE(endian, ADDR_SIZE, hdr->e_shoff);
136 	hdr->e_flags     = TO_NATIVE(endian, 32, hdr->e_flags);
137 	hdr->e_ehsize    = TO_NATIVE(endian, 16, hdr->e_ehsize);
138 	hdr->e_phentsize = TO_NATIVE(endian, 16, hdr->e_phentsize);
139 	hdr->e_phnum     = TO_NATIVE(endian, 16, hdr->e_phnum);
140 	hdr->e_shentsize = TO_NATIVE(endian, 16, hdr->e_shentsize);
141 	hdr->e_shnum     = TO_NATIVE(endian, 16, hdr->e_shnum);
142 	hdr->e_shstrndx  = TO_NATIVE(endian, 16, hdr->e_shstrndx);
143 
144 	sechdrs = RTE_PTR_ADD(hdr, hdr->e_shoff);
145 	info->sechdrs = sechdrs;
146 
147 	/* Check if file offset is correct */
148 	if (hdr->e_shoff > info->size) {
149 		fprintf(stderr, "section header offset=%lu in file '%s' "
150 		      "is bigger than filesize=%lu\n",
151 		      (unsigned long)hdr->e_shoff,
152 		      filename, info->size);
153 		return 0;
154 	}
155 
156 	if (hdr->e_shnum == SHN_UNDEF) {
157 		/*
158 		 * There are more than 64k sections,
159 		 * read count from .sh_size.
160 		 */
161 		info->num_sections =
162 			TO_NATIVE(endian, ADDR_SIZE, sechdrs[0].sh_size);
163 	} else {
164 		info->num_sections = hdr->e_shnum;
165 	}
166 	if (hdr->e_shstrndx == SHN_XINDEX)
167 		info->secindex_strings =
168 			TO_NATIVE(endian, 32, sechdrs[0].sh_link);
169 	else
170 		info->secindex_strings = hdr->e_shstrndx;
171 
172 	/* Fix endianness in section headers */
173 	for (i = 0; i < info->num_sections; i++) {
174 		sechdrs[i].sh_name      =
175 			TO_NATIVE(endian, 32, sechdrs[i].sh_name);
176 		sechdrs[i].sh_type      =
177 			TO_NATIVE(endian, 32, sechdrs[i].sh_type);
178 		sechdrs[i].sh_flags     =
179 			TO_NATIVE(endian, 32, sechdrs[i].sh_flags);
180 		sechdrs[i].sh_addr      =
181 			TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_addr);
182 		sechdrs[i].sh_offset    =
183 			TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_offset);
184 		sechdrs[i].sh_size      =
185 			TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_size);
186 		sechdrs[i].sh_link      =
187 			TO_NATIVE(endian, 32, sechdrs[i].sh_link);
188 		sechdrs[i].sh_info      =
189 			TO_NATIVE(endian, 32, sechdrs[i].sh_info);
190 		sechdrs[i].sh_addralign =
191 			TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_addralign);
192 		sechdrs[i].sh_entsize   =
193 			TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_entsize);
194 	}
195 	/* Find symbol table. */
196 	for (i = 1; i < info->num_sections; i++) {
197 		int nobits = sechdrs[i].sh_type == SHT_NOBITS;
198 
199 		if (!nobits && sechdrs[i].sh_offset > info->size) {
200 			fprintf(stderr, "%s is truncated. "
201 			      "sechdrs[i].sh_offset=%lu > sizeof(*hrd)=%zu\n",
202 			      filename, (unsigned long)sechdrs[i].sh_offset,
203 			      sizeof(*hdr));
204 			return 0;
205 		}
206 
207 		if (sechdrs[i].sh_type == SHT_SYMTAB) {
208 			unsigned int sh_link_idx;
209 			symtab_idx = i;
210 			info->symtab_start = RTE_PTR_ADD(hdr,
211 				sechdrs[i].sh_offset);
212 			info->symtab_stop  = RTE_PTR_ADD(hdr,
213 				sechdrs[i].sh_offset + sechdrs[i].sh_size);
214 			sh_link_idx = sechdrs[i].sh_link;
215 			info->strtab       = RTE_PTR_ADD(hdr,
216 				sechdrs[sh_link_idx].sh_offset);
217 		}
218 
219 		/* 32bit section no. table? ("more than 64k sections") */
220 		if (sechdrs[i].sh_type == SHT_SYMTAB_SHNDX) {
221 			symtab_shndx_idx = i;
222 			info->symtab_shndx_start = RTE_PTR_ADD(hdr,
223 				sechdrs[i].sh_offset);
224 			info->symtab_shndx_stop  = RTE_PTR_ADD(hdr,
225 				sechdrs[i].sh_offset + sechdrs[i].sh_size);
226 		}
227 	}
228 	if (!info->symtab_start)
229 		fprintf(stderr, "%s has no symtab?\n", filename);
230 	else {
231 		/* Fix endianness in symbols */
232 		for (sym = info->symtab_start; sym < info->symtab_stop; sym++) {
233 			sym->st_shndx = TO_NATIVE(endian, 16, sym->st_shndx);
234 			sym->st_name  = TO_NATIVE(endian, 32, sym->st_name);
235 			sym->st_value = TO_NATIVE(endian, ADDR_SIZE, sym->st_value);
236 			sym->st_size  = TO_NATIVE(endian, ADDR_SIZE, sym->st_size);
237 		}
238 	}
239 
240 	if (symtab_shndx_idx != ~0U) {
241 		Elf32_Word *p;
242 		if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
243 			fprintf(stderr,
244 			      "%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
245 			      filename, sechdrs[symtab_shndx_idx].sh_link,
246 			      symtab_idx);
247 		/* Fix endianness */
248 		for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
249 		     p++)
250 			*p = TO_NATIVE(endian, 32, *p);
251 	}
252 
253 	return 1;
254 }
255 
256 static void parse_elf_finish(struct elf_info *info)
257 {
258 	struct pmd_driver *tmp, *idx = info->drivers;
259 	release_file(info->hdr, info->size);
260 	while (idx) {
261 		tmp = idx->next;
262 		free(idx);
263 		idx = tmp;
264 	}
265 }
266 
267 struct opt_tag {
268 	const char *suffix;
269 	const char *json_id;
270 };
271 
272 static const struct opt_tag opt_tags[] = {
273 	{"_param_string_export", "params"},
274 	{"_kmod_dep_export", "kmod"},
275 };
276 
277 static int complete_pmd_entry(struct elf_info *info, struct pmd_driver *drv)
278 {
279 	const char *tname;
280 	int i;
281 	char tmpsymname[128];
282 	Elf_Sym *tmpsym;
283 
284 	drv->name = get_sym_value(info, drv->name_sym);
285 
286 	for (i = 0; i < PMD_OPT_MAX; i++) {
287 		memset(tmpsymname, 0, 128);
288 		sprintf(tmpsymname, "__%s%s", drv->name, opt_tags[i].suffix);
289 		tmpsym = find_sym_in_symtab(info, tmpsymname, NULL);
290 		if (!tmpsym)
291 			continue;
292 		drv->opt_vals[i] = get_sym_value(info, tmpsym);
293 	}
294 
295 	memset(tmpsymname, 0, 128);
296 	sprintf(tmpsymname, "__%s_pci_tbl_export", drv->name);
297 
298 	tmpsym = find_sym_in_symtab(info, tmpsymname, NULL);
299 
300 
301 	/*
302 	 * If this returns NULL, then this is a PMD_VDEV, because
303 	 * it has no pci table reference
304 	 */
305 	if (!tmpsym) {
306 		drv->pci_tbl = NULL;
307 		return 0;
308 	}
309 
310 	tname = get_sym_value(info, tmpsym);
311 	tmpsym = find_sym_in_symtab(info, tname, NULL);
312 	if (!tmpsym)
313 		return -ENOENT;
314 
315 	drv->pci_tbl = (struct rte_pci_id *)get_sym_value(info, tmpsym);
316 	if (!drv->pci_tbl)
317 		return -ENOENT;
318 
319 	return 0;
320 }
321 
322 static int locate_pmd_entries(struct elf_info *info)
323 {
324 	Elf_Sym *last = NULL;
325 	struct pmd_driver *new;
326 
327 	info->drivers = NULL;
328 
329 	do {
330 		new = calloc(sizeof(struct pmd_driver), 1);
331 		if (new == NULL) {
332 			fprintf(stderr, "Failed to calloc memory\n");
333 			return -1;
334 		}
335 		new->name_sym = find_sym_in_symtab(info, "this_pmd_name", last);
336 		last = new->name_sym;
337 		if (!new->name_sym)
338 			free(new);
339 		else {
340 			if (complete_pmd_entry(info, new)) {
341 				fprintf(stderr,
342 					"Failed to complete pmd entry\n");
343 				free(new);
344 			} else {
345 				new->next = info->drivers;
346 				info->drivers = new;
347 			}
348 		}
349 	} while (last);
350 
351 	return 0;
352 }
353 
354 static void output_pmd_info_string(struct elf_info *info, char *outfile)
355 {
356 	FILE *ofd;
357 	struct pmd_driver *drv;
358 	struct rte_pci_id *pci_ids;
359 	int idx = 0;
360 
361 	ofd = fopen(outfile, "w+");
362 	if (!ofd) {
363 		fprintf(stderr, "Unable to open output file\n");
364 		return;
365 	}
366 
367 	drv = info->drivers;
368 
369 	while (drv) {
370 		fprintf(ofd, "const char %s_pmd_info[] __attribute__((used)) = "
371 			"\"PMD_INFO_STRING= {",
372 			drv->name);
373 		fprintf(ofd, "\\\"name\\\" : \\\"%s\\\", ", drv->name);
374 
375 		for (idx = 0; idx < PMD_OPT_MAX; idx++) {
376 			if (drv->opt_vals[idx])
377 				fprintf(ofd, "\\\"%s\\\" : \\\"%s\\\", ",
378 					opt_tags[idx].json_id,
379 					drv->opt_vals[idx]);
380 		}
381 
382 		pci_ids = drv->pci_tbl;
383 		fprintf(ofd, "\\\"pci_ids\\\" : [");
384 
385 		while (pci_ids && pci_ids->device_id) {
386 			fprintf(ofd, "[%d, %d, %d, %d]",
387 				pci_ids->vendor_id, pci_ids->device_id,
388 				pci_ids->subsystem_vendor_id,
389 				pci_ids->subsystem_device_id);
390 			pci_ids++;
391 			if (pci_ids->device_id)
392 				fprintf(ofd, ",");
393 			else
394 				fprintf(ofd, " ");
395 		}
396 		fprintf(ofd, "]}\";\n");
397 		drv = drv->next;
398 	}
399 
400 	fclose(ofd);
401 }
402 
403 int main(int argc, char **argv)
404 {
405 	struct elf_info info = {0};
406 	int rc = 1;
407 
408 	if (argc < 3) {
409 		fprintf(stderr,
410 			"usage: %s <object file> <c output file>\n",
411 			basename(argv[0]));
412 		exit(127);
413 	}
414 	parse_elf(&info, argv[1]);
415 
416 	if (locate_pmd_entries(&info) < 0)
417 		exit(1);
418 
419 	if (info.drivers) {
420 		output_pmd_info_string(&info, argv[2]);
421 		rc = 0;
422 	} else {
423 		fprintf(stderr, "No drivers registered\n");
424 	}
425 
426 	parse_elf_finish(&info);
427 	exit(rc);
428 }
429