1 /* SPDX-License-Identifier: GPL-2.0
2  * Postprocess pmd object files to export hw support
3  *
4  * Copyright 2016 Neil Horman <[email protected]>
5  * Based in part on modpost.c from the linux kernel
6  */
7 
8 #include <stdio.h>
9 #include <ctype.h>
10 #include <string.h>
11 #include <limits.h>
12 #include <stdbool.h>
13 #include <errno.h>
14 #include <libgen.h>
15 
16 #include <rte_common.h>
17 #include "pmdinfogen.h"
18 
19 #ifdef RTE_ARCH_64
20 #define ADDR_SIZE 64
21 #else
22 #define ADDR_SIZE 32
23 #endif
24 
25 static int use_stdin, use_stdout;
26 
sym_name(struct elf_info * elf,Elf_Sym * sym)27 static const char *sym_name(struct elf_info *elf, Elf_Sym *sym)
28 {
29 	if (sym)
30 		return elf->strtab + sym->st_name;
31 	else
32 		return "(unknown)";
33 }
34 
grab_file(const char * filename,unsigned long * size)35 static void *grab_file(const char *filename, unsigned long *size)
36 {
37 	struct stat st;
38 	void *map = MAP_FAILED;
39 	int fd = -1;
40 
41 	if (!use_stdin) {
42 		fd = open(filename, O_RDONLY);
43 		if (fd < 0)
44 			return NULL;
45 	} else {
46 		/* from stdin, use a temporary file to mmap */
47 		FILE *infile;
48 		char buffer[1024];
49 		int n;
50 
51 		infile = tmpfile();
52 		if (infile == NULL) {
53 			perror("tmpfile");
54 			return NULL;
55 		}
56 		fd = dup(fileno(infile));
57 		fclose(infile);
58 		if (fd < 0)
59 			return NULL;
60 
61 		n = read(STDIN_FILENO, buffer, sizeof(buffer));
62 		while (n > 0) {
63 			if (write(fd, buffer, n) != n)
64 				goto failed;
65 			n = read(STDIN_FILENO, buffer, sizeof(buffer));
66 		}
67 	}
68 
69 	if (fstat(fd, &st))
70 		goto failed;
71 
72 	*size = st.st_size;
73 	map = mmap(NULL, *size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
74 
75 failed:
76 	close(fd);
77 	if (map == MAP_FAILED)
78 		return NULL;
79 	return map;
80 }
81 
82 /**
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.
86   **/
release_file(void * file,unsigned long size)87 static void release_file(void *file, unsigned long size)
88 {
89 	munmap(file, size);
90 }
91 
92 
get_sym_value(struct elf_info * info,const Elf_Sym * sym)93 static void *get_sym_value(struct elf_info *info, const Elf_Sym *sym)
94 {
95 	return RTE_PTR_ADD(info->hdr,
96 		info->sechdrs[sym->st_shndx].sh_offset + sym->st_value);
97 }
98 
find_sym_in_symtab(struct elf_info * info,const char * name,Elf_Sym * last)99 static Elf_Sym *find_sym_in_symtab(struct elf_info *info,
100 				   const char *name, Elf_Sym *last)
101 {
102 	Elf_Sym *idx;
103 	if (last)
104 		idx = last+1;
105 	else
106 		idx = info->symtab_start;
107 
108 	for (; idx < info->symtab_stop; idx++) {
109 		const char *n = sym_name(info, idx);
110 		if (!strncmp(n, name, strlen(name)))
111 			return idx;
112 	}
113 	return NULL;
114 }
115 
parse_elf(struct elf_info * info,const char * filename)116 static int parse_elf(struct elf_info *info, const char *filename)
117 {
118 	unsigned int i;
119 	Elf_Ehdr *hdr;
120 	Elf_Shdr *sechdrs;
121 	Elf_Sym  *sym;
122 	int endian;
123 	unsigned int symtab_idx = ~0U, symtab_shndx_idx = ~0U;
124 
125 	hdr = grab_file(filename, &info->size);
126 	if (!hdr) {
127 		perror(filename);
128 		exit(1);
129 	}
130 	info->hdr = hdr;
131 	if (info->size < sizeof(*hdr)) {
132 		/* file too small, assume this is an empty .o file */
133 		return 0;
134 	}
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 */
141 		return 0;
142 	}
143 
144 	if (!hdr->e_ident[EI_DATA]) {
145 		/* Unknown endian */
146 		return 0;
147 	}
148 
149 	endian = hdr->e_ident[EI_DATA];
150 
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);
165 
166 	sechdrs = RTE_PTR_ADD(hdr, hdr->e_shoff);
167 	info->sechdrs = sechdrs;
168 
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);
175 		return 0;
176 	}
177 
178 	if (hdr->e_shnum == SHN_UNDEF) {
179 		/*
180 		 * There are more than 64k sections,
181 		 * read count from .sh_size.
182 		 */
183 		info->num_sections =
184 			TO_NATIVE(endian, ADDR_SIZE, sechdrs[0].sh_size);
185 	} else {
186 		info->num_sections = hdr->e_shnum;
187 	}
188 	if (hdr->e_shstrndx == SHN_XINDEX)
189 		info->secindex_strings =
190 			TO_NATIVE(endian, 32, sechdrs[0].sh_link);
191 	else
192 		info->secindex_strings = hdr->e_shstrndx;
193 
194 	/* Fix endianness in section headers */
195 	for (i = 0; i < info->num_sections; i++) {
196 		sechdrs[i].sh_name      =
197 			TO_NATIVE(endian, 32, sechdrs[i].sh_name);
198 		sechdrs[i].sh_type      =
199 			TO_NATIVE(endian, 32, sechdrs[i].sh_type);
200 		sechdrs[i].sh_flags     =
201 			TO_NATIVE(endian, 32, sechdrs[i].sh_flags);
202 		sechdrs[i].sh_addr      =
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);
206 		sechdrs[i].sh_size      =
207 			TO_NATIVE(endian, ADDR_SIZE, sechdrs[i].sh_size);
208 		sechdrs[i].sh_link      =
209 			TO_NATIVE(endian, 32, sechdrs[i].sh_link);
210 		sechdrs[i].sh_info      =
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);
216 	}
217 	/* Find symbol table. */
218 	for (i = 1; i < info->num_sections; i++) {
219 		int nobits = sechdrs[i].sh_type == SHT_NOBITS;
220 
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,
225 			      sizeof(*hdr));
226 			return 0;
227 		}
228 
229 		if (sechdrs[i].sh_type == SHT_SYMTAB) {
230 			unsigned int sh_link_idx;
231 			symtab_idx = i;
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);
239 		}
240 
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);
248 		}
249 	}
250 	if (!info->symtab_start)
251 		fprintf(stderr, "%s has no symtab?\n", filename);
252 	else {
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);
259 		}
260 	}
261 
262 	if (symtab_shndx_idx != ~0U) {
263 		Elf32_Word *p;
264 		if (symtab_idx != sechdrs[symtab_shndx_idx].sh_link)
265 			fprintf(stderr,
266 			      "%s: SYMTAB_SHNDX has bad sh_link: %u!=%u\n",
267 			      filename, sechdrs[symtab_shndx_idx].sh_link,
268 			      symtab_idx);
269 		/* Fix endianness */
270 		for (p = info->symtab_shndx_start; p < info->symtab_shndx_stop;
271 		     p++)
272 			*p = TO_NATIVE(endian, 32, *p);
273 	}
274 
275 	return 1;
276 }
277 
parse_elf_finish(struct elf_info * info)278 static void parse_elf_finish(struct elf_info *info)
279 {
280 	struct pmd_driver *tmp, *idx = info->drivers;
281 	release_file(info->hdr, info->size);
282 	while (idx) {
283 		tmp = idx->next;
284 		free(idx);
285 		idx = tmp;
286 	}
287 }
288 
289 struct opt_tag {
290 	const char *suffix;
291 	const char *json_id;
292 };
293 
294 static const struct opt_tag opt_tags[] = {
295 	{"_param_string_export", "params"},
296 	{"_kmod_dep_export", "kmod"},
297 };
298 
complete_pmd_entry(struct elf_info * info,struct pmd_driver * drv)299 static int complete_pmd_entry(struct elf_info *info, struct pmd_driver *drv)
300 {
301 	const char *tname;
302 	int i;
303 	char tmpsymname[128];
304 	Elf_Sym *tmpsym;
305 
306 	drv->name = get_sym_value(info, drv->name_sym);
307 
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);
312 		if (!tmpsym)
313 			continue;
314 		drv->opt_vals[i] = get_sym_value(info, tmpsym);
315 	}
316 
317 	memset(tmpsymname, 0, 128);
318 	sprintf(tmpsymname, "__%s_pci_tbl_export", drv->name);
319 
320 	tmpsym = find_sym_in_symtab(info, tmpsymname, NULL);
321 
322 
323 	/*
324 	 * If this returns NULL, then this is a PMD_VDEV, because
325 	 * it has no pci table reference
326 	 */
327 	if (!tmpsym) {
328 		drv->pci_tbl = NULL;
329 		return 0;
330 	}
331 
332 	tname = get_sym_value(info, tmpsym);
333 	tmpsym = find_sym_in_symtab(info, tname, NULL);
334 	if (!tmpsym)
335 		return -ENOENT;
336 
337 	drv->pci_tbl = (struct rte_pci_id *)get_sym_value(info, tmpsym);
338 	if (!drv->pci_tbl)
339 		return -ENOENT;
340 
341 	return 0;
342 }
343 
locate_pmd_entries(struct elf_info * info)344 static int locate_pmd_entries(struct elf_info *info)
345 {
346 	Elf_Sym *last = NULL;
347 	struct pmd_driver *new;
348 
349 	info->drivers = NULL;
350 
351 	do {
352 		new = calloc(sizeof(struct pmd_driver), 1);
353 		if (new == NULL) {
354 			fprintf(stderr, "Failed to calloc memory\n");
355 			return -1;
356 		}
357 		new->name_sym = find_sym_in_symtab(info, "this_pmd_name", last);
358 		last = new->name_sym;
359 		if (!new->name_sym)
360 			free(new);
361 		else {
362 			if (complete_pmd_entry(info, new)) {
363 				fprintf(stderr,
364 					"Failed to complete pmd entry\n");
365 				free(new);
366 			} else {
367 				new->next = info->drivers;
368 				info->drivers = new;
369 			}
370 		}
371 	} while (last);
372 
373 	return 0;
374 }
375 
output_pmd_info_string(struct elf_info * info,char * outfile)376 static void output_pmd_info_string(struct elf_info *info, char *outfile)
377 {
378 	FILE *ofd;
379 	struct pmd_driver *drv;
380 	struct rte_pci_id *pci_ids;
381 	int idx = 0;
382 
383 	if (use_stdout)
384 		ofd = stdout;
385 	else {
386 		ofd = fopen(outfile, "w+");
387 		if (!ofd) {
388 			fprintf(stderr, "Unable to open output file\n");
389 			return;
390 		}
391 	}
392 
393 	drv = info->drivers;
394 
395 	while (drv) {
396 		fprintf(ofd, "const char %s_pmd_info[] __attribute__((used)) = "
397 			"\"PMD_INFO_STRING= {",
398 			drv->name);
399 		fprintf(ofd, "\\\"name\\\" : \\\"%s\\\", ", drv->name);
400 
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,
405 					drv->opt_vals[idx]);
406 		}
407 
408 		pci_ids = drv->pci_tbl;
409 		fprintf(ofd, "\\\"pci_ids\\\" : [");
410 
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);
416 			pci_ids++;
417 			if (pci_ids->device_id)
418 				fprintf(ofd, ",");
419 			else
420 				fprintf(ofd, " ");
421 		}
422 		fprintf(ofd, "]}\";\n");
423 		drv = drv->next;
424 	}
425 
426 	fclose(ofd);
427 }
428 
main(int argc,char ** argv)429 int main(int argc, char **argv)
430 {
431 	struct elf_info info = {0};
432 	int rc = 1;
433 
434 	if (argc < 3) {
435 		fprintf(stderr,
436 			"usage: %s <object file> <c output file>\n",
437 			basename(argv[0]));
438 		exit(127);
439 	}
440 	use_stdin = !strcmp(argv[1], "-");
441 	use_stdout = !strcmp(argv[2], "-");
442 	parse_elf(&info, argv[1]);
443 
444 	if (locate_pmd_entries(&info) < 0)
445 		exit(1);
446 
447 	if (info.drivers) {
448 		output_pmd_info_string(&info, argv[2]);
449 		rc = 0;
450 	} else {
451 		fprintf(stderr, "No drivers registered\n");
452 	}
453 
454 	parse_elf_finish(&info);
455 	exit(rc);
456 }
457