1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2*6db59d35SArnaldo Carvalho de Melo #include <inttypes.h>
3203d8a4aSSong Shan Gong #include <unistd.h>
4203d8a4aSSong Shan Gong #include <stdio.h>
5203d8a4aSSong Shan Gong #include <string.h>
620f2be1dSJiri Olsa #include <internal/lib.h> // page_size
7203d8a4aSSong Shan Gong #include "machine.h"
8203d8a4aSSong Shan Gong #include "api/fs/fs.h"
96738028dSThomas Richter #include "debug.h"
10b9c0a649SThomas Richter #include "symbol.h"
11203d8a4aSSong Shan Gong
arch__fix_module_text_start(u64 * start,u64 * size,const char * name)1212a6d294SThomas Richter int arch__fix_module_text_start(u64 *start, u64 *size, const char *name)
13203d8a4aSSong Shan Gong {
146738028dSThomas Richter u64 m_start = *start;
15203d8a4aSSong Shan Gong char path[PATH_MAX];
16203d8a4aSSong Shan Gong
17203d8a4aSSong Shan Gong snprintf(path, PATH_MAX, "module/%.*s/sections/.text",
18203d8a4aSSong Shan Gong (int)strlen(name) - 2, name + 1);
196738028dSThomas Richter if (sysfs__read_ull(path, (unsigned long long *)start) < 0) {
206738028dSThomas Richter pr_debug2("Using module %s start:%#lx\n", path, m_start);
216738028dSThomas Richter *start = m_start;
2212a6d294SThomas Richter } else {
2312a6d294SThomas Richter /* Successful read of the modules segment text start address.
2412a6d294SThomas Richter * Calculate difference between module start address
2512a6d294SThomas Richter * in memory and module text segment start address.
2612a6d294SThomas Richter * For example module load address is 0x3ff8011b000
2712a6d294SThomas Richter * (from /proc/modules) and module text segment start
2812a6d294SThomas Richter * address is 0x3ff8011b870 (from file above).
2912a6d294SThomas Richter *
3012a6d294SThomas Richter * Adjust the module size and subtract the GOT table
3112a6d294SThomas Richter * size located at the beginning of the module.
3212a6d294SThomas Richter */
3312a6d294SThomas Richter *size -= (*start - m_start);
346738028dSThomas Richter }
35203d8a4aSSong Shan Gong
36203d8a4aSSong Shan Gong return 0;
37203d8a4aSSong Shan Gong }
38