xref: /OK3568_Linux_fs/kernel/tools/perf/arch/s390/util/machine.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #include <unistd.h>
3*4882a593Smuzhiyun #include <stdio.h>
4*4882a593Smuzhiyun #include <string.h>
5*4882a593Smuzhiyun #include <internal/lib.h> // page_size
6*4882a593Smuzhiyun #include "machine.h"
7*4882a593Smuzhiyun #include "api/fs/fs.h"
8*4882a593Smuzhiyun #include "debug.h"
9*4882a593Smuzhiyun #include "symbol.h"
10*4882a593Smuzhiyun 
arch__fix_module_text_start(u64 * start,u64 * size,const char * name)11*4882a593Smuzhiyun int arch__fix_module_text_start(u64 *start, u64 *size, const char *name)
12*4882a593Smuzhiyun {
13*4882a593Smuzhiyun 	u64 m_start = *start;
14*4882a593Smuzhiyun 	char path[PATH_MAX];
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun 	snprintf(path, PATH_MAX, "module/%.*s/sections/.text",
17*4882a593Smuzhiyun 				(int)strlen(name) - 2, name + 1);
18*4882a593Smuzhiyun 	if (sysfs__read_ull(path, (unsigned long long *)start) < 0) {
19*4882a593Smuzhiyun 		pr_debug2("Using module %s start:%#lx\n", path, m_start);
20*4882a593Smuzhiyun 		*start = m_start;
21*4882a593Smuzhiyun 	} else {
22*4882a593Smuzhiyun 		/* Successful read of the modules segment text start address.
23*4882a593Smuzhiyun 		 * Calculate difference between module start address
24*4882a593Smuzhiyun 		 * in memory and module text segment start address.
25*4882a593Smuzhiyun 		 * For example module load address is 0x3ff8011b000
26*4882a593Smuzhiyun 		 * (from /proc/modules) and module text segment start
27*4882a593Smuzhiyun 		 * address is 0x3ff8011b870 (from file above).
28*4882a593Smuzhiyun 		 *
29*4882a593Smuzhiyun 		 * Adjust the module size and subtract the GOT table
30*4882a593Smuzhiyun 		 * size located at the beginning of the module.
31*4882a593Smuzhiyun 		 */
32*4882a593Smuzhiyun 		*size -= (*start - m_start);
33*4882a593Smuzhiyun 	}
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	return 0;
36*4882a593Smuzhiyun }
37