Michal Suchanek ee8ebe
From 4797470a6625aa16783b0cb9ce3eb071cb6608a8 Mon Sep 17 00:00:00 2001
Michal Suchanek ee8ebe
From: Michal Suchanek <msuchanek@suse.de>
Michal Suchanek ee8ebe
Date: Wed, 7 Feb 2018 15:16:04 +0100
Michal Suchanek ee8ebe
Subject: [PATCH] Add ksym-provides tool.
Michal Suchanek ee8ebe
Michal Suchanek ee8ebe
References: bsc#1077692
Michal Suchanek ee8ebe
Patch-mainline: no, not needed
Michal Suchanek ee8ebe
Michal Suchanek ee8ebe
Signed-off-by: Michal Suchanek <msuchanek@suse.de>
Michal Suchanek ee8ebe
---
Michal Suchanek ee8ebe
 scripts/mod/Makefile        |   6 ++-
Michal Suchanek ee8ebe
 scripts/mod/ksym-provides.c | 124 ++++++++++++++++++++++++++++++++++++++++++++
Michal Suchanek ee8ebe
 2 files changed, 129 insertions(+), 1 deletion(-)
Michal Suchanek ee8ebe
 create mode 100644 scripts/mod/ksym-provides.c
Michal Suchanek ee8ebe
Michal Suchanek ee8ebe
diff --git a/scripts/mod/Makefile b/scripts/mod/Makefile
Michal Suchanek ee8ebe
index b497d9764dcf..024e8151259a 100644
Michal Suchanek ee8ebe
--- a/scripts/mod/Makefile
Michal Suchanek ee8ebe
+++ b/scripts/mod/Makefile
Michal Suchanek ee8ebe
@@ -1,10 +1,14 @@
Michal Suchanek ee8ebe
 OBJECT_FILES_NON_STANDARD := y
Michal Suchanek ee8ebe
 
Michal Suchanek ee8ebe
-hostprogs-y	:= modpost mk_elfconfig
Michal Suchanek ee8ebe
+hostprogs-y	:= modpost mk_elfconfig ksym-provides
Michal Suchanek ee8ebe
 always		:= $(hostprogs-y) empty.o
Michal Suchanek ee8ebe
 
Michal Suchanek ee8ebe
 modpost-objs	:= modpost.o file2alias.o sumversion.o
Michal Suchanek ee8ebe
 
Michal Suchanek ee8ebe
+ksym-provides-objs	:= ksym-provides.o
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+HOSTLOADLIBES_ksym-provides := -lelf
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
 devicetable-offsets-file := devicetable-offsets.h
Michal Suchanek ee8ebe
 
Michal Suchanek ee8ebe
 $(obj)/$(devicetable-offsets-file): $(obj)/devicetable-offsets.s FORCE
Michal Suchanek ee8ebe
diff --git a/scripts/mod/ksym-provides.c b/scripts/mod/ksym-provides.c
Michal Suchanek ee8ebe
new file mode 100644
Michal Suchanek ee8ebe
index 000000000000..c71fb79d4640
Michal Suchanek ee8ebe
--- /dev/null
Michal Suchanek ee8ebe
+++ b/scripts/mod/ksym-provides.c
Michal Suchanek ee8ebe
@@ -0,0 +1,124 @@
Michal Suchanek ee8ebe
+#include <stdio.h>
Michal Suchanek ee8ebe
+#include <sys/types.h>
Michal Suchanek ee8ebe
+#include <sys/stat.h>
Michal Suchanek ee8ebe
+#include <fcntl.h>
Michal Suchanek ee8ebe
+#include <unistd.h>
Michal Suchanek ee8ebe
+#include <gelf.h>
Michal Suchanek ee8ebe
+#include <string.h>
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+int main(int argc, char *argv[])
Michal Suchanek ee8ebe
+{
Michal Suchanek ee8ebe
+	int fd;
Michal Suchanek ee8ebe
+	Elf *elf;
Michal Suchanek ee8ebe
+	Elf_Scn *scn;
Michal Suchanek ee8ebe
+	GElf_Shdr shdr;
Michal Suchanek ee8ebe
+	size_t strndx;
Michal Suchanek ee8ebe
+	int ndx;
Michal Suchanek ee8ebe
+	Elf_Data *symdata, *data;
Michal Suchanek ee8ebe
+	GElf_Sym sym;
Michal Suchanek ee8ebe
+	char *name;
Michal Suchanek ee8ebe
+	const char * flavor = argv[1];
Michal Suchanek ee8ebe
+	const char * prefix = "__crc_";
Michal Suchanek ee8ebe
+	size_t prefixlen = strlen(prefix);
Michal Suchanek ee8ebe
+	const char * symformat = "ksym(%s:%s) = %lx\n";
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+	if (argc != 3) {
Michal Suchanek ee8ebe
+		fprintf(stderr, "Usage: %s <flavor> <filename>\n", argv[0]);
Michal Suchanek ee8ebe
+		return 1;
Michal Suchanek ee8ebe
+	}
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+	if (elf_version(EV_CURRENT) == EV_NONE) {
Michal Suchanek ee8ebe
+		fprintf(stderr, "libelf initialization failed: %s\n",
Michal Suchanek ee8ebe
+			elf_errmsg(-1));
Michal Suchanek ee8ebe
+		return 1;
Michal Suchanek ee8ebe
+	}
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+	fd = open(argv[2], O_RDONLY);
Michal Suchanek ee8ebe
+	if (fd < 0) {
Michal Suchanek ee8ebe
+		perror("open failed");
Michal Suchanek ee8ebe
+		return 1;
Michal Suchanek ee8ebe
+	}
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+	elf = elf_begin(fd, ELF_C_READ, NULL);
Michal Suchanek ee8ebe
+	if (!elf) {
Michal Suchanek ee8ebe
+		fprintf(stderr, "elf_begin failed: %s\n", elf_errmsg(-1));
Michal Suchanek ee8ebe
+		goto err_close;
Michal Suchanek ee8ebe
+	}
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+	scn = NULL;
Michal Suchanek ee8ebe
+	while ((scn = elf_nextscn(elf, scn)) != NULL) {
Michal Suchanek ee8ebe
+		if (!gelf_getshdr(scn, &shdr)) {
Michal Suchanek ee8ebe
+			fprintf(stderr, "gelf_getshdr failed: %s\n",
Michal Suchanek ee8ebe
+				elf_errmsg(-1));
Michal Suchanek ee8ebe
+			goto err_end;
Michal Suchanek ee8ebe
+		}
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+		if (shdr.sh_type == SHT_SYMTAB)
Michal Suchanek ee8ebe
+			break;
Michal Suchanek ee8ebe
+	}
Michal Suchanek ee8ebe
+	if (!scn) {
Michal Suchanek ee8ebe
+		fputs("symbol table not found\n", stderr);
Michal Suchanek ee8ebe
+		goto err_end;
Michal Suchanek ee8ebe
+	}
Michal Suchanek ee8ebe
+	strndx = shdr.sh_link;
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+	symdata = elf_getdata(scn, NULL);
Michal Suchanek ee8ebe
+	if (!symdata) {
Michal Suchanek ee8ebe
+		fprintf(stderr, "elf_getdata failed: %s\n", elf_errmsg(-1));
Michal Suchanek ee8ebe
+		goto err_end;
Michal Suchanek ee8ebe
+	}
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+	for (ndx = 0; gelf_getsym(symdata, ndx, &sym) != NULL; ++ndx) {
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+		name = elf_strptr(elf, strndx, sym.st_name);
Michal Suchanek ee8ebe
+		if (!name) {
Michal Suchanek ee8ebe
+			fprintf(stderr, "elf_strptr failed: %s\n",
Michal Suchanek ee8ebe
+				elf_errmsg(-1));
Michal Suchanek ee8ebe
+			goto err_end;
Michal Suchanek ee8ebe
+		}
Michal Suchanek ee8ebe
+		if (strncmp(prefix, name, prefixlen))
Michal Suchanek ee8ebe
+			continue;
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+		if (sym.st_shndx >= SHN_LORESERVE) {
Michal Suchanek ee8ebe
+			printf(symformat, flavor, name + prefixlen,
Michal Suchanek ee8ebe
+				sym.st_value);
Michal Suchanek ee8ebe
+			continue;
Michal Suchanek ee8ebe
+		}
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+		scn = elf_getscn(elf, sym.st_shndx);
Michal Suchanek ee8ebe
+		if (!scn) {
Michal Suchanek ee8ebe
+			fprintf(stderr, "elf_getscn failed: %s\n",
Michal Suchanek ee8ebe
+				elf_errmsg(-1));
Michal Suchanek ee8ebe
+			goto err_end;
Michal Suchanek ee8ebe
+		}
Michal Suchanek ee8ebe
+		if (!gelf_getshdr(scn, &shdr)) {
Michal Suchanek ee8ebe
+			fprintf(stderr, "gelf_getshdr failed: %s\n",
Michal Suchanek ee8ebe
+				elf_errmsg(-1));
Michal Suchanek ee8ebe
+			goto err_end;
Michal Suchanek ee8ebe
+		}
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+		if (shdr.sh_type != SHT_PROGBITS)
Michal Suchanek ee8ebe
+			continue;
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+		data = elf_getdata_rawchunk(
Michal Suchanek ee8ebe
+			elf, shdr.sh_offset + sym.st_value - shdr.sh_addr,
Michal Suchanek ee8ebe
+			sizeof(GElf_Word), ELF_T_WORD);
Michal Suchanek ee8ebe
+		if (!data) {
Michal Suchanek ee8ebe
+			fprintf(stderr, "elf_getdata_rawchunk failed: %s\n",
Michal Suchanek ee8ebe
+				elf_errmsg(-1));
Michal Suchanek ee8ebe
+			goto err_end;
Michal Suchanek ee8ebe
+		}
Michal Suchanek ee8ebe
+		printf(symformat, flavor, name + prefixlen,
Michal Suchanek ee8ebe
+		       (unsigned long) *(GElf_Word*)data->d_buf);
Michal Suchanek ee8ebe
+	}
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+	elf_end(elf);
Michal Suchanek ee8ebe
+	close(fd);
Michal Suchanek ee8ebe
+	return 0;
Michal Suchanek ee8ebe
+
Michal Suchanek ee8ebe
+ err_end:
Michal Suchanek ee8ebe
+	elf_end(elf);
Michal Suchanek ee8ebe
+ err_close:
Michal Suchanek ee8ebe
+	close(fd);
Michal Suchanek ee8ebe
+	return 1;
Michal Suchanek ee8ebe
+}
Michal Suchanek ee8ebe
-- 
Michal Suchanek ee8ebe
2.13.6
Michal Suchanek ee8ebe