1#!/usr/bin/env perl
2# SPDX-License-Identifier: GPL-2.0
3
4# Stolen from Linux kernel :)
5
6#	Check the stack usage of functions
7#
8#	Copyright Joern Engel <joern@lazybastard.org>
9#	Inspired by Linus Torvalds
10#	Original idea maybe from Keith Owens
11#	s390 port and big speedup by Arnd Bergmann <arnd@bergmann-dalldorf.de>
12#	Mips port by Juan Quintela <quintela@mandrakesoft.com>
13#	IA64 port via Andreas Dilger
14#	Arm port by Holger Schurig
15#	sh64 port by Paul Mundt
16#	Random bits by Matt Mackall <mpm@selenic.com>
17#	M68k port by Geert Uytterhoeven and Andreas Schwab
18#	blackfin port by Alex Landau
19#	AArch64, PARISC ports by Kyle McMartin
20#	sparc port by Martin Habets <errandir_news@mph.eclipse.co.uk>
21#	ppc64le port by Breno Leitao <leitao@debian.org>
22#
23#	Usage:
24#	objdump -d vmlinux | scripts/checkstack.pl [arch]
25#
26#	TODO :	Port to all architectures (one regex per arch)
27
28use strict;
29
30# check for arch
31#
32# $re is used for two matches:
33# $& (whole re) matches the complete objdump line with the stack growth
34# $1 (first bracket) matches the size of the stack growth
35#
36# $dre is similar, but for dynamic stack reductions:
37# $& (whole re) matches the complete objdump line with the stack growth
38# $1 (first bracket) matches the dynamic amount of the stack growth
39#
40# use anything else and feel the pain ;)
41my (@stack, $re, $dre, $x, $xs, $funcre);
42{
43	my $arch = shift;
44	if ($arch eq "") {
45		$arch = `uname -m`;
46		chomp($arch);
47	}
48
49	$x	= "[0-9a-f]";	# hex character
50	$xs	= "[0-9a-f ]";	# hex character or space
51	$funcre = qr/^$x* <(.*)>:$/;
52	if ($arch eq 'aarch64') {
53		#ffffffc0006325cc:       a9bb7bfd        stp     x29, x30, [sp, #-80]!
54		#a110:       d11643ff        sub     sp, sp, #0x590
55		$re = qr/^.*stp.*sp, \#-([0-9]{1,8})\]\!/o;
56		$dre = qr/^.*sub.*sp, sp, #(0x$x{1,8})/o;
57	} elsif ($arch eq 'arm') {
58		#c0008ffc:	e24dd064	sub	sp, sp, #100	; 0x64
59		$re = qr/.*sub.*sp, sp, #(([0-9]{2}|[3-9])[0-9]{2})/o;
60	} elsif ($arch =~ /^x86(_64)?$/ || $arch =~ /^i[3456]86$/) {
61		#c0105234:       81 ec ac 05 00 00       sub    $0x5ac,%esp
62		# or
63		#    2f60:    48 81 ec e8 05 00 00       sub    $0x5e8,%rsp
64		$re = qr/^.*[as][du][db]    \$(0x$x{1,8}),\%(e|r)sp$/o;
65		$dre = qr/^.*[as][du][db]    (%.*),\%(e|r)sp$/o;
66	} elsif ($arch eq 'blackfin') {
67		#      52:       00 e8 03 00     LINK 0xc;
68		$re = qr/.*[[:space:]]LINK[[:space:]]*(0x$x{1,8})/o;
69	} elsif ($arch eq 'ia64') {
70		#e0000000044011fc:       01 0f fc 8c     adds r12=-384,r12
71		$re = qr/.*adds.*r12=-(([0-9]{2}|[3-9])[0-9]{2}),r12/o;
72	} elsif ($arch eq 'm68k') {
73		#    2b6c:       4e56 fb70       linkw %fp,#-1168
74		#  1df770:       defc ffe4       addaw #-28,%sp
75		$re = qr/.*(?:linkw %fp,|addaw )#-([0-9]{1,4})(?:,%sp)?$/o;
76	} elsif ($arch eq 'mips64') {
77		#8800402c:       67bdfff0        daddiu  sp,sp,-16
78		$re = qr/.*daddiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
79	} elsif ($arch eq 'mips') {
80		#88003254:       27bdffe0        addiu   sp,sp,-32
81		$re = qr/.*addiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
82	} elsif ($arch eq 'nios2') {
83		#25a8:	defffb04 	addi	sp,sp,-20
84		$re = qr/.*addi.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
85	} elsif ($arch eq 'openrisc') {
86		# c000043c:       9c 21 fe f0     l.addi r1,r1,-272
87		$re = qr/.*l\.addi.*r1,r1,-(([0-9]{2}|[3-9])[0-9]{2})/o;
88	} elsif ($arch eq 'parisc' || $arch eq 'parisc64') {
89		$re = qr/.*ldo ($x{1,8})\(sp\),sp/o;
90	} elsif ($arch eq 'powerpc' || $arch =~ /^ppc(64)?(le)?$/ ) {
91		# powerpc    : 94 21 ff 30     stwu    r1,-208(r1)
92		# ppc64(le)  : 81 ff 21 f8     stdu    r1,-128(r1)
93		$re = qr/.*st[dw]u.*r1,-($x{1,8})\(r1\)/o;
94	} elsif ($arch =~ /^s390x?$/) {
95		#   11160:       a7 fb ff 60             aghi   %r15,-160
96		# or
97		#  100092:	 e3 f0 ff c8 ff 71	 lay	 %r15,-56(%r15)
98		$re = qr/.*(?:lay|ag?hi).*\%r15,-(([0-9]{2}|[3-9])[0-9]{2})
99		      (?:\(\%r15\))?$/ox;
100	} elsif ($arch =~ /^sh64$/) {
101		#XXX: we only check for the immediate case presently,
102		#     though we will want to check for the movi/sub
103		#     pair for larger users. -- PFM.
104		#a00048e0:       d4fc40f0        addi.l  r15,-240,r15
105		$re = qr/.*addi\.l.*r15,-(([0-9]{2}|[3-9])[0-9]{2}),r15/o;
106	} elsif ($arch eq 'sparc' || $arch eq 'sparc64') {
107		# f0019d10:       9d e3 bf 90     save  %sp, -112, %sp
108		$re = qr/.*save.*%sp, -(([0-9]{2}|[3-9])[0-9]{2}), %sp/o;
109	} else {
110		print("wrong or unknown architecture \"$arch\"\n");
111		exit
112	}
113}
114
115#
116# main()
117#
118my ($func, $file, $lastslash);
119
120while (my $line = <STDIN>) {
121	if ($line =~ m/$funcre/) {
122		$func = $1;
123	}
124	elsif ($line =~ m/(.*):\s*file format/) {
125		$file = $1;
126		$file =~ s/\.ko//;
127		$lastslash = rindex($file, "/");
128		if ($lastslash != -1) {
129			$file = substr($file, $lastslash + 1);
130		}
131	}
132	elsif ($line =~ m/$re/) {
133		my $size = $1;
134		$size = hex($size) if ($size =~ /^0x/);
135
136		if ($size > 0xf0000000) {
137			$size = - $size;
138			$size += 0x80000000;
139			$size += 0x80000000;
140		}
141		next if ($size > 0x10000000);
142
143		next if $line !~ m/^($xs*)/;
144		my $addr = $1;
145		$addr =~ s/ /0/g;
146		$addr = "0x$addr";
147
148		# bbox: was: my $intro = "$addr $func [$file]:";
149		my $intro = "$func [$file]:";
150		my $padlen = 56 - length($intro);
151		while ($padlen > 0) {
152			$intro .= '	';
153			$padlen -= 8;
154		}
155		next if ($size < 100);
156		push @stack, "$intro$size\n";
157	}
158	elsif (defined $dre && $line =~ m/$dre/) {
159		my $size = "Dynamic ($1)";
160
161		next if $line !~ m/^($xs*)/;
162		my $addr = $1;
163		$addr =~ s/ /0/g;
164		$addr = "0x$addr";
165
166		#bbox: was: my $intro = "$addr $func [$file]:";
167		my $intro = "$func [$file]:";
168		my $padlen = 56 - length($intro);
169		while ($padlen > 0) {
170			$intro .= '	';
171			$padlen -= 8;
172		}
173		push @stack, "$intro$size\n";
174	}
175}
176
177sub bysize($) {
178	my ($asize, $bsize);
179	($asize = $a) =~ s/.*:  *(.*)$/$1/;
180	($bsize = $b) =~ s/.*:  *(.*)$/$1/;
181	$bsize <=> $asize
182}
183
184# Sort output by size (last field)
185print sort { ($b =~ /:\t*(\d+)$/)[0] <=> ($a =~ /:\t*(\d+)$/)[0] } @stack;
186