benchmark: detect libteec offset

Add detection of an offset of load-time relocatable libteec for correct
translation of addresses into file name/line numbers.

Acked-by: Joakim Bech <joakim.bech@linaro.org>
Signed-off-by: Igor Opaniuk <igor.opaniuk@linaro.org>
diff --git a/benchmark_aux.c b/benchmark_aux.c
index 222b179..c997403 100644
--- a/benchmark_aux.c
+++ b/benchmark_aux.c
@@ -149,3 +149,47 @@
 	close(devmem);
 	return (hw_addr + offset);
 }
+
+size_t get_library_load_offset(pid_t pid, const char *libname)
+{
+	char path[256];
+	char buf[256];
+	FILE *file;
+	size_t addr = 0;
+	size_t start, end, offset;
+	char flags[4];
+	int len;
+	int len_libname = strlen(libname);
+
+	snprintf(path, sizeof(path), "/proc/%d/smaps", pid);
+
+	file = fopen(path, "rt");
+	if (file == NULL)
+		return 0;
+
+	while (fgets(buf, sizeof(buf), file) != NULL) {
+		len = strlen(buf);
+		if (len > 0 && buf[len-1] == '\n')
+			buf[--len] = '\0';
+
+		if (len <= len_libname || !strstr(buf, libname))
+			continue;
+
+		printf("%s\n", buf);
+		if (sscanf(buf, "%zx-%zx %c%c%c%c %zx", &start, &end,
+			   &flags[0], &flags[1],
+			   &flags[2], &flags[3], &offset) != 7)
+			continue;
+
+		if (flags[0] != 'r' || flags[2] != 'x')
+			continue;
+
+		addr = start - offset;
+		break;
+	}
+
+	fclose(file);
+
+	return addr;
+}
+