xref: /OK3568_Linux_fs/kernel/scripts/gen_autoksyms.sh (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun#!/bin/sh
2*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only
3*4882a593Smuzhiyun
4*4882a593Smuzhiyun# Create an autoksyms.h header file from the list of all module's needed symbols
5*4882a593Smuzhiyun# as recorded on the second line of *.mod files and the user-provided symbol
6*4882a593Smuzhiyun# whitelist.
7*4882a593Smuzhiyun
8*4882a593Smuzhiyunset -e
9*4882a593Smuzhiyun
10*4882a593Smuzhiyunoutput_file="$1"
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun# Use "make V=1" to debug this script.
13*4882a593Smuzhiyuncase "$KBUILD_VERBOSE" in
14*4882a593Smuzhiyun*1*)
15*4882a593Smuzhiyun	set -x
16*4882a593Smuzhiyun	;;
17*4882a593Smuzhiyunesac
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun# We need access to CONFIG_ symbols
20*4882a593Smuzhiyun. include/config/auto.conf
21*4882a593Smuzhiyun
22*4882a593Smuzhiyunneeded_symbols=
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun# Special case for modversions (see modpost.c)
25*4882a593Smuzhiyunif [ -n "$CONFIG_MODVERSIONS" ]; then
26*4882a593Smuzhiyun	needed_symbols="$needed_symbols module_layout"
27*4882a593Smuzhiyunfi
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun# With CONFIG_LTO_CLANG, LLVM bitcode has not yet been compiled into a binary
30*4882a593Smuzhiyun# when the .mod files are generated, which means they don't yet contain
31*4882a593Smuzhiyun# references to certain symbols that will be present in the final binaries.
32*4882a593Smuzhiyunif [ -n "$CONFIG_LTO_CLANG" ]; then
33*4882a593Smuzhiyun	# intrinsic functions
34*4882a593Smuzhiyun	needed_symbols="$needed_symbols memcpy memmove memset"
35*4882a593Smuzhiyun	# ftrace
36*4882a593Smuzhiyun	needed_symbols="$needed_symbols _mcount"
37*4882a593Smuzhiyun	# stack protector symbols
38*4882a593Smuzhiyun	needed_symbols="$needed_symbols __stack_chk_fail __stack_chk_guard"
39*4882a593Smuzhiyunfi
40*4882a593Smuzhiyun
41*4882a593Smuzhiyunksym_wl=
42*4882a593Smuzhiyunif [ -n "$CONFIG_UNUSED_KSYMS_WHITELIST" ]; then
43*4882a593Smuzhiyun	# Use 'eval' to expand the whitelist path and check if it is relative
44*4882a593Smuzhiyun	eval ksym_wl="$CONFIG_UNUSED_KSYMS_WHITELIST"
45*4882a593Smuzhiyun	[ "${ksym_wl}" != "${ksym_wl#/}" ] || ksym_wl="$abs_srctree/$ksym_wl"
46*4882a593Smuzhiyun	if [ ! -f "$ksym_wl" ] || [ ! -r "$ksym_wl" ]; then
47*4882a593Smuzhiyun		echo "ERROR: '$ksym_wl' whitelist file not found" >&2
48*4882a593Smuzhiyun		exit 1
49*4882a593Smuzhiyun	fi
50*4882a593Smuzhiyunfi
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun# Generate a new ksym list file with symbols needed by the current
53*4882a593Smuzhiyun# set of modules.
54*4882a593Smuzhiyuncat > "$output_file" << EOT
55*4882a593Smuzhiyun/*
56*4882a593Smuzhiyun * Automatically generated file; DO NOT EDIT.
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun
59*4882a593SmuzhiyunEOT
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun[ -f modules.order ] && modlist=modules.order || modlist=/dev/null
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun{
64*4882a593Smuzhiyun	sed 's/ko$/mod/' $modlist | xargs -n1 sed -n -e '2p'
65*4882a593Smuzhiyun	echo "$needed_symbols"
66*4882a593Smuzhiyun	[ -n "$ksym_wl" ] && cat "$ksym_wl"
67*4882a593Smuzhiyun} | sed -e 's/ /\n/g' | sed -n -e '/^$/!p' |
68*4882a593Smuzhiyun# Remove the dot prefix for ppc64; symbol names with a dot (.) hold entry
69*4882a593Smuzhiyun# point addresses.
70*4882a593Smuzhiyunsed -e 's/^\.//' |
71*4882a593Smuzhiyunsort -u |
72*4882a593Smuzhiyunsed -e 's/\(.*\)/#define __KSYM_\1 1/' >> "$output_file"
73