162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci#include <inttypes.h>
362306a36Sopenharmony_ci#include <unistd.h>
462306a36Sopenharmony_ci#include <stdio.h>
562306a36Sopenharmony_ci#include <string.h>
662306a36Sopenharmony_ci#include <internal/lib.h> // page_size
762306a36Sopenharmony_ci#include "machine.h"
862306a36Sopenharmony_ci#include "api/fs/fs.h"
962306a36Sopenharmony_ci#include "debug.h"
1062306a36Sopenharmony_ci#include "symbol.h"
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ciint arch__fix_module_text_start(u64 *start, u64 *size, const char *name)
1362306a36Sopenharmony_ci{
1462306a36Sopenharmony_ci	u64 m_start = *start;
1562306a36Sopenharmony_ci	char path[PATH_MAX];
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci	snprintf(path, PATH_MAX, "module/%.*s/sections/.text",
1862306a36Sopenharmony_ci				(int)strlen(name) - 2, name + 1);
1962306a36Sopenharmony_ci	if (sysfs__read_ull(path, (unsigned long long *)start) < 0) {
2062306a36Sopenharmony_ci		pr_debug2("Using module %s start:%#lx\n", path, m_start);
2162306a36Sopenharmony_ci		*start = m_start;
2262306a36Sopenharmony_ci	} else {
2362306a36Sopenharmony_ci		/* Successful read of the modules segment text start address.
2462306a36Sopenharmony_ci		 * Calculate difference between module start address
2562306a36Sopenharmony_ci		 * in memory and module text segment start address.
2662306a36Sopenharmony_ci		 * For example module load address is 0x3ff8011b000
2762306a36Sopenharmony_ci		 * (from /proc/modules) and module text segment start
2862306a36Sopenharmony_ci		 * address is 0x3ff8011b870 (from file above).
2962306a36Sopenharmony_ci		 *
3062306a36Sopenharmony_ci		 * Adjust the module size and subtract the GOT table
3162306a36Sopenharmony_ci		 * size located at the beginning of the module.
3262306a36Sopenharmony_ci		 */
3362306a36Sopenharmony_ci		*size -= (*start - m_start);
3462306a36Sopenharmony_ci	}
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci	return 0;
3762306a36Sopenharmony_ci}
38