1*4882a593Smuzhiyun#!/usr/bin/env perl 2*4882a593Smuzhiyun# SPDX-License-Identifier: GPL-2.0-only 3*4882a593Smuzhiyun# (c) 2008, Steven Rostedt <srostedt@redhat.com> 4*4882a593Smuzhiyun# 5*4882a593Smuzhiyun# recordmcount.pl - makes a section called __mcount_loc that holds 6*4882a593Smuzhiyun# all the offsets to the calls to mcount. 7*4882a593Smuzhiyun# 8*4882a593Smuzhiyun# 9*4882a593Smuzhiyun# What we want to end up with this is that each object file will have a 10*4882a593Smuzhiyun# section called __mcount_loc that will hold the list of pointers to mcount 11*4882a593Smuzhiyun# callers. After final linking, the vmlinux will have within .init.data the 12*4882a593Smuzhiyun# list of all callers to mcount between __start_mcount_loc and __stop_mcount_loc. 13*4882a593Smuzhiyun# Later on boot up, the kernel will read this list, save the locations and turn 14*4882a593Smuzhiyun# them into nops. When tracing or profiling is later enabled, these locations 15*4882a593Smuzhiyun# will then be converted back to pointers to some function. 16*4882a593Smuzhiyun# 17*4882a593Smuzhiyun# This is no easy feat. This script is called just after the original 18*4882a593Smuzhiyun# object is compiled and before it is linked. 19*4882a593Smuzhiyun# 20*4882a593Smuzhiyun# When parse this object file using 'objdump', the references to the call 21*4882a593Smuzhiyun# sites are offsets from the section that the call site is in. Hence, all 22*4882a593Smuzhiyun# functions in a section that has a call site to mcount, will have the 23*4882a593Smuzhiyun# offset from the beginning of the section and not the beginning of the 24*4882a593Smuzhiyun# function. 25*4882a593Smuzhiyun# 26*4882a593Smuzhiyun# But where this section will reside finally in vmlinx is undetermined at 27*4882a593Smuzhiyun# this point. So we can't use this kind of offsets to record the final 28*4882a593Smuzhiyun# address of this call site. 29*4882a593Smuzhiyun# 30*4882a593Smuzhiyun# The trick is to change the call offset referring the start of a section to 31*4882a593Smuzhiyun# referring a function symbol in this section. During the link step, 'ld' will 32*4882a593Smuzhiyun# compute the final address according to the information we record. 33*4882a593Smuzhiyun# 34*4882a593Smuzhiyun# e.g. 35*4882a593Smuzhiyun# 36*4882a593Smuzhiyun# .section ".sched.text", "ax" 37*4882a593Smuzhiyun# [...] 38*4882a593Smuzhiyun# func1: 39*4882a593Smuzhiyun# [...] 40*4882a593Smuzhiyun# call mcount (offset: 0x10) 41*4882a593Smuzhiyun# [...] 42*4882a593Smuzhiyun# ret 43*4882a593Smuzhiyun# .globl fun2 44*4882a593Smuzhiyun# func2: (offset: 0x20) 45*4882a593Smuzhiyun# [...] 46*4882a593Smuzhiyun# [...] 47*4882a593Smuzhiyun# ret 48*4882a593Smuzhiyun# func3: 49*4882a593Smuzhiyun# [...] 50*4882a593Smuzhiyun# call mcount (offset: 0x30) 51*4882a593Smuzhiyun# [...] 52*4882a593Smuzhiyun# 53*4882a593Smuzhiyun# Both relocation offsets for the mcounts in the above example will be 54*4882a593Smuzhiyun# offset from .sched.text. If we choose global symbol func2 as a reference and 55*4882a593Smuzhiyun# make another file called tmp.s with the new offsets: 56*4882a593Smuzhiyun# 57*4882a593Smuzhiyun# .section __mcount_loc 58*4882a593Smuzhiyun# .quad func2 - 0x10 59*4882a593Smuzhiyun# .quad func2 + 0x10 60*4882a593Smuzhiyun# 61*4882a593Smuzhiyun# We can then compile this tmp.s into tmp.o, and link it back to the original 62*4882a593Smuzhiyun# object. 63*4882a593Smuzhiyun# 64*4882a593Smuzhiyun# In our algorithm, we will choose the first global function we meet in this 65*4882a593Smuzhiyun# section as the reference. But this gets hard if there is no global functions 66*4882a593Smuzhiyun# in this section. In such a case we have to select a local one. E.g. func1: 67*4882a593Smuzhiyun# 68*4882a593Smuzhiyun# .section ".sched.text", "ax" 69*4882a593Smuzhiyun# func1: 70*4882a593Smuzhiyun# [...] 71*4882a593Smuzhiyun# call mcount (offset: 0x10) 72*4882a593Smuzhiyun# [...] 73*4882a593Smuzhiyun# ret 74*4882a593Smuzhiyun# func2: 75*4882a593Smuzhiyun# [...] 76*4882a593Smuzhiyun# call mcount (offset: 0x20) 77*4882a593Smuzhiyun# [...] 78*4882a593Smuzhiyun# .section "other.section" 79*4882a593Smuzhiyun# 80*4882a593Smuzhiyun# If we make the tmp.s the same as above, when we link together with 81*4882a593Smuzhiyun# the original object, we will end up with two symbols for func1: 82*4882a593Smuzhiyun# one local, one global. After final compile, we will end up with 83*4882a593Smuzhiyun# an undefined reference to func1 or a wrong reference to another global 84*4882a593Smuzhiyun# func1 in other files. 85*4882a593Smuzhiyun# 86*4882a593Smuzhiyun# Since local objects can reference local variables, we need to find 87*4882a593Smuzhiyun# a way to make tmp.o reference the local objects of the original object 88*4882a593Smuzhiyun# file after it is linked together. To do this, we convert func1 89*4882a593Smuzhiyun# into a global symbol before linking tmp.o. Then after we link tmp.o 90*4882a593Smuzhiyun# we will only have a single symbol for func1 that is global. 91*4882a593Smuzhiyun# We can convert func1 back into a local symbol and we are done. 92*4882a593Smuzhiyun# 93*4882a593Smuzhiyun# Here are the steps we take: 94*4882a593Smuzhiyun# 95*4882a593Smuzhiyun# 1) Record all the local and weak symbols by using 'nm' 96*4882a593Smuzhiyun# 2) Use objdump to find all the call site offsets and sections for 97*4882a593Smuzhiyun# mcount. 98*4882a593Smuzhiyun# 3) Compile the list into its own object. 99*4882a593Smuzhiyun# 4) Do we have to deal with local functions? If not, go to step 8. 100*4882a593Smuzhiyun# 5) Make an object that converts these local functions to global symbols 101*4882a593Smuzhiyun# with objcopy. 102*4882a593Smuzhiyun# 6) Link together this new object with the list object. 103*4882a593Smuzhiyun# 7) Convert the local functions back to local symbols and rename 104*4882a593Smuzhiyun# the result as the original object. 105*4882a593Smuzhiyun# 8) Link the object with the list object. 106*4882a593Smuzhiyun# 9) Move the result back to the original object. 107*4882a593Smuzhiyun# 108*4882a593Smuzhiyun 109*4882a593Smuzhiyunuse warnings; 110*4882a593Smuzhiyunuse strict; 111*4882a593Smuzhiyun 112*4882a593Smuzhiyunmy $P = $0; 113*4882a593Smuzhiyun$P =~ s@.*/@@g; 114*4882a593Smuzhiyun 115*4882a593Smuzhiyunmy $V = '0.1'; 116*4882a593Smuzhiyun 117*4882a593Smuzhiyunif ($#ARGV != 11) { 118*4882a593Smuzhiyun print "usage: $P arch endian bits objdump objcopy cc ld nm rm mv is_module inputfile\n"; 119*4882a593Smuzhiyun print "version: $V\n"; 120*4882a593Smuzhiyun exit(1); 121*4882a593Smuzhiyun} 122*4882a593Smuzhiyun 123*4882a593Smuzhiyunmy ($arch, $endian, $bits, $objdump, $objcopy, $cc, 124*4882a593Smuzhiyun $ld, $nm, $rm, $mv, $is_module, $inputfile) = @ARGV; 125*4882a593Smuzhiyun 126*4882a593Smuzhiyun# This file refers to mcount and shouldn't be ftraced, so lets' ignore it 127*4882a593Smuzhiyunif ($inputfile =~ m,kernel/trace/ftrace\.o$,) { 128*4882a593Smuzhiyun exit(0); 129*4882a593Smuzhiyun} 130*4882a593Smuzhiyun 131*4882a593Smuzhiyun# Acceptable sections to record. 132*4882a593Smuzhiyunmy %text_sections = ( 133*4882a593Smuzhiyun ".text" => 1, 134*4882a593Smuzhiyun ".init.text" => 1, 135*4882a593Smuzhiyun ".ref.text" => 1, 136*4882a593Smuzhiyun ".sched.text" => 1, 137*4882a593Smuzhiyun ".spinlock.text" => 1, 138*4882a593Smuzhiyun ".irqentry.text" => 1, 139*4882a593Smuzhiyun ".softirqentry.text" => 1, 140*4882a593Smuzhiyun ".kprobes.text" => 1, 141*4882a593Smuzhiyun ".cpuidle.text" => 1, 142*4882a593Smuzhiyun ".text.unlikely" => 1, 143*4882a593Smuzhiyun); 144*4882a593Smuzhiyun 145*4882a593Smuzhiyun# Acceptable section-prefixes to record. 146*4882a593Smuzhiyunmy %text_section_prefixes = ( 147*4882a593Smuzhiyun ".text." => 1, 148*4882a593Smuzhiyun); 149*4882a593Smuzhiyun 150*4882a593Smuzhiyun# Note: we are nice to C-programmers here, thus we skip the '||='-idiom. 151*4882a593Smuzhiyun$objdump = 'objdump' if (!$objdump); 152*4882a593Smuzhiyun$objcopy = 'objcopy' if (!$objcopy); 153*4882a593Smuzhiyun$cc = 'gcc' if (!$cc); 154*4882a593Smuzhiyun$ld = 'ld' if (!$ld); 155*4882a593Smuzhiyun$nm = 'nm' if (!$nm); 156*4882a593Smuzhiyun$rm = 'rm' if (!$rm); 157*4882a593Smuzhiyun$mv = 'mv' if (!$mv); 158*4882a593Smuzhiyun 159*4882a593Smuzhiyun#print STDERR "running: $P '$arch' '$objdump' '$objcopy' '$cc' '$ld' " . 160*4882a593Smuzhiyun# "'$nm' '$rm' '$mv' '$inputfile'\n"; 161*4882a593Smuzhiyun 162*4882a593Smuzhiyunmy %locals; # List of local (static) functions 163*4882a593Smuzhiyunmy %weak; # List of weak functions 164*4882a593Smuzhiyunmy %convert; # List of local functions used that needs conversion 165*4882a593Smuzhiyun 166*4882a593Smuzhiyunmy $type; 167*4882a593Smuzhiyunmy $local_regex; # Match a local function (return function) 168*4882a593Smuzhiyunmy $weak_regex; # Match a weak function (return function) 169*4882a593Smuzhiyunmy $section_regex; # Find the start of a section 170*4882a593Smuzhiyunmy $function_regex; # Find the name of a function 171*4882a593Smuzhiyun # (return offset and func name) 172*4882a593Smuzhiyunmy $mcount_regex; # Find the call site to mcount (return offset) 173*4882a593Smuzhiyunmy $mcount_adjust; # Address adjustment to mcount offset 174*4882a593Smuzhiyunmy $alignment; # The .align value to use for $mcount_section 175*4882a593Smuzhiyunmy $section_type; # Section header plus possible alignment command 176*4882a593Smuzhiyunmy $can_use_local = 0; # If we can use local function references 177*4882a593Smuzhiyun 178*4882a593Smuzhiyun# Shut up recordmcount if user has older objcopy 179*4882a593Smuzhiyunmy $quiet_recordmcount = ".tmp_quiet_recordmcount"; 180*4882a593Smuzhiyunmy $print_warning = 1; 181*4882a593Smuzhiyun$print_warning = 0 if ( -f $quiet_recordmcount); 182*4882a593Smuzhiyun 183*4882a593Smuzhiyun## 184*4882a593Smuzhiyun# check_objcopy - whether objcopy supports --globalize-symbols 185*4882a593Smuzhiyun# 186*4882a593Smuzhiyun# --globalize-symbols came out in 2.17, we must test the version 187*4882a593Smuzhiyun# of objcopy, and if it is less than 2.17, then we can not 188*4882a593Smuzhiyun# record local functions. 189*4882a593Smuzhiyunsub check_objcopy 190*4882a593Smuzhiyun{ 191*4882a593Smuzhiyun open (IN, "$objcopy --version |") or die "error running $objcopy"; 192*4882a593Smuzhiyun while (<IN>) { 193*4882a593Smuzhiyun if (/objcopy.*\s(\d+)\.(\d+)/) { 194*4882a593Smuzhiyun $can_use_local = 1 if ($1 > 2 || ($1 == 2 && $2 >= 17)); 195*4882a593Smuzhiyun last; 196*4882a593Smuzhiyun } 197*4882a593Smuzhiyun } 198*4882a593Smuzhiyun close (IN); 199*4882a593Smuzhiyun 200*4882a593Smuzhiyun if (!$can_use_local && $print_warning) { 201*4882a593Smuzhiyun print STDERR "WARNING: could not find objcopy version or version " . 202*4882a593Smuzhiyun "is less than 2.17.\n" . 203*4882a593Smuzhiyun "\tLocal function references are disabled.\n"; 204*4882a593Smuzhiyun open (QUIET, ">$quiet_recordmcount"); 205*4882a593Smuzhiyun printf QUIET "Disables the warning from recordmcount.pl\n"; 206*4882a593Smuzhiyun close QUIET; 207*4882a593Smuzhiyun } 208*4882a593Smuzhiyun} 209*4882a593Smuzhiyun 210*4882a593Smuzhiyunif ($arch =~ /(x86(_64)?)|(i386)/) { 211*4882a593Smuzhiyun if ($bits == 64) { 212*4882a593Smuzhiyun $arch = "x86_64"; 213*4882a593Smuzhiyun } else { 214*4882a593Smuzhiyun $arch = "i386"; 215*4882a593Smuzhiyun } 216*4882a593Smuzhiyun} 217*4882a593Smuzhiyun 218*4882a593Smuzhiyun# 219*4882a593Smuzhiyun# We base the defaults off of i386, the other archs may 220*4882a593Smuzhiyun# feel free to change them in the below if statements. 221*4882a593Smuzhiyun# 222*4882a593Smuzhiyun$local_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\S+)"; 223*4882a593Smuzhiyun$weak_regex = "^[0-9a-fA-F]+\\s+([wW])\\s+(\\S+)"; 224*4882a593Smuzhiyun$section_regex = "Disassembly of section\\s+(\\S+):"; 225*4882a593Smuzhiyun$function_regex = "^([0-9a-fA-F]+)\\s+<([^^]*?)>:"; 226*4882a593Smuzhiyun$mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s(mcount|__fentry__)\$"; 227*4882a593Smuzhiyun$section_type = '@progbits'; 228*4882a593Smuzhiyun$mcount_adjust = 0; 229*4882a593Smuzhiyun$type = ".long"; 230*4882a593Smuzhiyun 231*4882a593Smuzhiyunif ($arch eq "x86_64") { 232*4882a593Smuzhiyun $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s(mcount|__fentry__)([+-]0x[0-9a-zA-Z]+)?\$"; 233*4882a593Smuzhiyun $type = ".quad"; 234*4882a593Smuzhiyun $alignment = 8; 235*4882a593Smuzhiyun $mcount_adjust = -1; 236*4882a593Smuzhiyun 237*4882a593Smuzhiyun # force flags for this arch 238*4882a593Smuzhiyun $ld .= " -m elf_x86_64"; 239*4882a593Smuzhiyun $objdump .= " -M x86-64"; 240*4882a593Smuzhiyun $objcopy .= " -O elf64-x86-64"; 241*4882a593Smuzhiyun $cc .= " -m64"; 242*4882a593Smuzhiyun 243*4882a593Smuzhiyun} elsif ($arch eq "i386") { 244*4882a593Smuzhiyun $alignment = 4; 245*4882a593Smuzhiyun $mcount_adjust = -1; 246*4882a593Smuzhiyun 247*4882a593Smuzhiyun # force flags for this arch 248*4882a593Smuzhiyun $ld .= " -m elf_i386"; 249*4882a593Smuzhiyun $objdump .= " -M i386"; 250*4882a593Smuzhiyun $objcopy .= " -O elf32-i386"; 251*4882a593Smuzhiyun $cc .= " -m32"; 252*4882a593Smuzhiyun 253*4882a593Smuzhiyun} elsif ($arch eq "s390" && $bits == 64) { 254*4882a593Smuzhiyun if ($cc =~ /-DCC_USING_HOTPATCH/) { 255*4882a593Smuzhiyun $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*c0 04 00 00 00 00\\s*(brcl\\s*0,|jgnop\\s*)[0-9a-f]+ <([^\+]*)>\$"; 256*4882a593Smuzhiyun $mcount_adjust = 0; 257*4882a593Smuzhiyun } else { 258*4882a593Smuzhiyun $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_390_(PC|PLT)32DBL\\s+_mcount\\+0x2\$"; 259*4882a593Smuzhiyun $mcount_adjust = -14; 260*4882a593Smuzhiyun } 261*4882a593Smuzhiyun $alignment = 8; 262*4882a593Smuzhiyun $type = ".quad"; 263*4882a593Smuzhiyun $ld .= " -m elf64_s390"; 264*4882a593Smuzhiyun $cc .= " -m64"; 265*4882a593Smuzhiyun 266*4882a593Smuzhiyun} elsif ($arch eq "sh") { 267*4882a593Smuzhiyun $alignment = 2; 268*4882a593Smuzhiyun 269*4882a593Smuzhiyun # force flags for this arch 270*4882a593Smuzhiyun $ld .= " -m shlelf_linux"; 271*4882a593Smuzhiyun if ($endian eq "big") { 272*4882a593Smuzhiyun $objcopy .= " -O elf32-shbig-linux"; 273*4882a593Smuzhiyun } else { 274*4882a593Smuzhiyun $objcopy .= " -O elf32-sh-linux"; 275*4882a593Smuzhiyun } 276*4882a593Smuzhiyun 277*4882a593Smuzhiyun} elsif ($arch eq "powerpc") { 278*4882a593Smuzhiyun my $ldemulation; 279*4882a593Smuzhiyun 280*4882a593Smuzhiyun $local_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\.?\\S+)"; 281*4882a593Smuzhiyun # See comment in the sparc64 section for why we use '\w'. 282*4882a593Smuzhiyun $function_regex = "^([0-9a-fA-F]+)\\s+<(\\.?\\w*?)>:"; 283*4882a593Smuzhiyun $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s\\.?_mcount\$"; 284*4882a593Smuzhiyun 285*4882a593Smuzhiyun if ($endian eq "big") { 286*4882a593Smuzhiyun $cc .= " -mbig-endian "; 287*4882a593Smuzhiyun $ld .= " -EB "; 288*4882a593Smuzhiyun $ldemulation = "ppc" 289*4882a593Smuzhiyun } else { 290*4882a593Smuzhiyun $cc .= " -mlittle-endian "; 291*4882a593Smuzhiyun $ld .= " -EL "; 292*4882a593Smuzhiyun $ldemulation = "lppc" 293*4882a593Smuzhiyun } 294*4882a593Smuzhiyun if ($bits == 64) { 295*4882a593Smuzhiyun $type = ".quad"; 296*4882a593Smuzhiyun $cc .= " -m64 "; 297*4882a593Smuzhiyun $ld .= " -m elf64".$ldemulation." "; 298*4882a593Smuzhiyun } else { 299*4882a593Smuzhiyun $cc .= " -m32 "; 300*4882a593Smuzhiyun $ld .= " -m elf32".$ldemulation." "; 301*4882a593Smuzhiyun } 302*4882a593Smuzhiyun 303*4882a593Smuzhiyun} elsif ($arch eq "arm") { 304*4882a593Smuzhiyun $alignment = 2; 305*4882a593Smuzhiyun $section_type = '%progbits'; 306*4882a593Smuzhiyun $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_ARM_(CALL|PC24|THM_CALL)" . 307*4882a593Smuzhiyun "\\s+(__gnu_mcount_nc|mcount)\$"; 308*4882a593Smuzhiyun 309*4882a593Smuzhiyun} elsif ($arch eq "arm64") { 310*4882a593Smuzhiyun $alignment = 3; 311*4882a593Smuzhiyun $section_type = '%progbits'; 312*4882a593Smuzhiyun $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_AARCH64_CALL26\\s+_mcount\$"; 313*4882a593Smuzhiyun $type = ".quad"; 314*4882a593Smuzhiyun} elsif ($arch eq "ia64") { 315*4882a593Smuzhiyun $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$"; 316*4882a593Smuzhiyun $type = "data8"; 317*4882a593Smuzhiyun 318*4882a593Smuzhiyun if ($is_module eq "0") { 319*4882a593Smuzhiyun $cc .= " -mconstant-gp"; 320*4882a593Smuzhiyun } 321*4882a593Smuzhiyun} elsif ($arch eq "sparc64") { 322*4882a593Smuzhiyun # In the objdump output there are giblets like: 323*4882a593Smuzhiyun # 0000000000000000 <igmp_net_exit-0x18>: 324*4882a593Smuzhiyun # As there's some data blobs that get emitted into the 325*4882a593Smuzhiyun # text section before the first instructions and the first 326*4882a593Smuzhiyun # real symbols. We don't want to match that, so to combat 327*4882a593Smuzhiyun # this we use '\w' so we'll match just plain symbol names, 328*4882a593Smuzhiyun # and not those that also include hex offsets inside of the 329*4882a593Smuzhiyun # '<>' brackets. Actually the generic function_regex setting 330*4882a593Smuzhiyun # could safely use this too. 331*4882a593Smuzhiyun $function_regex = "^([0-9a-fA-F]+)\\s+<(\\w*?)>:"; 332*4882a593Smuzhiyun 333*4882a593Smuzhiyun # Sparc64 calls '_mcount' instead of plain 'mcount'. 334*4882a593Smuzhiyun $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$"; 335*4882a593Smuzhiyun 336*4882a593Smuzhiyun $alignment = 8; 337*4882a593Smuzhiyun $type = ".xword"; 338*4882a593Smuzhiyun $ld .= " -m elf64_sparc"; 339*4882a593Smuzhiyun $cc .= " -m64"; 340*4882a593Smuzhiyun $objcopy .= " -O elf64-sparc"; 341*4882a593Smuzhiyun} elsif ($arch eq "mips") { 342*4882a593Smuzhiyun # To enable module support, we need to enable the -mlong-calls option 343*4882a593Smuzhiyun # of gcc for module, after using this option, we can not get the real 344*4882a593Smuzhiyun # offset of the calling to _mcount, but the offset of the lui 345*4882a593Smuzhiyun # instruction or the addiu one. herein, we record the address of the 346*4882a593Smuzhiyun # first one, and then we can replace this instruction by a branch 347*4882a593Smuzhiyun # instruction to jump over the profiling function to filter the 348*4882a593Smuzhiyun # indicated functions, or switch back to the lui instruction to trace 349*4882a593Smuzhiyun # them, which means dynamic tracing. 350*4882a593Smuzhiyun # 351*4882a593Smuzhiyun # c: 3c030000 lui v1,0x0 352*4882a593Smuzhiyun # c: R_MIPS_HI16 _mcount 353*4882a593Smuzhiyun # c: R_MIPS_NONE *ABS* 354*4882a593Smuzhiyun # c: R_MIPS_NONE *ABS* 355*4882a593Smuzhiyun # 10: 64630000 daddiu v1,v1,0 356*4882a593Smuzhiyun # 10: R_MIPS_LO16 _mcount 357*4882a593Smuzhiyun # 10: R_MIPS_NONE *ABS* 358*4882a593Smuzhiyun # 10: R_MIPS_NONE *ABS* 359*4882a593Smuzhiyun # 14: 03e0082d move at,ra 360*4882a593Smuzhiyun # 18: 0060f809 jalr v1 361*4882a593Smuzhiyun # 362*4882a593Smuzhiyun # for the kernel: 363*4882a593Smuzhiyun # 364*4882a593Smuzhiyun # 10: 03e0082d move at,ra 365*4882a593Smuzhiyun # 14: 0c000000 jal 0 <loongson_halt> 366*4882a593Smuzhiyun # 14: R_MIPS_26 _mcount 367*4882a593Smuzhiyun # 14: R_MIPS_NONE *ABS* 368*4882a593Smuzhiyun # 14: R_MIPS_NONE *ABS* 369*4882a593Smuzhiyun # 18: 00020021 nop 370*4882a593Smuzhiyun if ($is_module eq "0") { 371*4882a593Smuzhiyun $mcount_regex = "^\\s*([0-9a-fA-F]+): R_MIPS_26\\s+_mcount\$"; 372*4882a593Smuzhiyun } else { 373*4882a593Smuzhiyun $mcount_regex = "^\\s*([0-9a-fA-F]+): R_MIPS_HI16\\s+_mcount\$"; 374*4882a593Smuzhiyun } 375*4882a593Smuzhiyun $objdump .= " -Melf-trad".$endian."mips "; 376*4882a593Smuzhiyun 377*4882a593Smuzhiyun if ($endian eq "big") { 378*4882a593Smuzhiyun $endian = " -EB "; 379*4882a593Smuzhiyun $ld .= " -melf".$bits."btsmip"; 380*4882a593Smuzhiyun } else { 381*4882a593Smuzhiyun $endian = " -EL "; 382*4882a593Smuzhiyun $ld .= " -melf".$bits."ltsmip"; 383*4882a593Smuzhiyun } 384*4882a593Smuzhiyun 385*4882a593Smuzhiyun $cc .= " -mno-abicalls -fno-pic -mabi=" . $bits . $endian; 386*4882a593Smuzhiyun $ld .= $endian; 387*4882a593Smuzhiyun 388*4882a593Smuzhiyun if ($bits == 64) { 389*4882a593Smuzhiyun $function_regex = 390*4882a593Smuzhiyun "^([0-9a-fA-F]+)\\s+<(.|[^\$]L.*?|\$[^L].*?|[^\$][^L].*?)>:"; 391*4882a593Smuzhiyun $type = ".dword"; 392*4882a593Smuzhiyun } 393*4882a593Smuzhiyun} elsif ($arch eq "microblaze") { 394*4882a593Smuzhiyun # Microblaze calls '_mcount' instead of plain 'mcount'. 395*4882a593Smuzhiyun $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$"; 396*4882a593Smuzhiyun} elsif ($arch eq "riscv") { 397*4882a593Smuzhiyun $function_regex = "^([0-9a-fA-F]+)\\s+<([^.0-9][0-9a-zA-Z_\\.]+)>:"; 398*4882a593Smuzhiyun $mcount_regex = "^\\s*([0-9a-fA-F]+):\\sR_RISCV_CALL(_PLT)?\\s_?mcount\$"; 399*4882a593Smuzhiyun $type = ".quad"; 400*4882a593Smuzhiyun $alignment = 2; 401*4882a593Smuzhiyun} elsif ($arch eq "nds32") { 402*4882a593Smuzhiyun $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_NDS32_HI20_RELA\\s+_mcount\$"; 403*4882a593Smuzhiyun $alignment = 2; 404*4882a593Smuzhiyun} elsif ($arch eq "csky") { 405*4882a593Smuzhiyun $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_CKCORE_PCREL_JSR_IMM26BY2\\s+_mcount\$"; 406*4882a593Smuzhiyun $alignment = 2; 407*4882a593Smuzhiyun} else { 408*4882a593Smuzhiyun die "Arch $arch is not supported with CONFIG_FTRACE_MCOUNT_RECORD"; 409*4882a593Smuzhiyun} 410*4882a593Smuzhiyun 411*4882a593Smuzhiyunmy $text_found = 0; 412*4882a593Smuzhiyunmy $read_function = 0; 413*4882a593Smuzhiyunmy $opened = 0; 414*4882a593Smuzhiyunmy $mcount_section = "__mcount_loc"; 415*4882a593Smuzhiyun 416*4882a593Smuzhiyunmy $dirname; 417*4882a593Smuzhiyunmy $filename; 418*4882a593Smuzhiyunmy $prefix; 419*4882a593Smuzhiyunmy $ext; 420*4882a593Smuzhiyun 421*4882a593Smuzhiyunif ($inputfile =~ m,^(.*)/([^/]*)$,) { 422*4882a593Smuzhiyun $dirname = $1; 423*4882a593Smuzhiyun $filename = $2; 424*4882a593Smuzhiyun} else { 425*4882a593Smuzhiyun $dirname = "."; 426*4882a593Smuzhiyun $filename = $inputfile; 427*4882a593Smuzhiyun} 428*4882a593Smuzhiyun 429*4882a593Smuzhiyunif ($filename =~ m,^(.*)(\.\S),) { 430*4882a593Smuzhiyun $prefix = $1; 431*4882a593Smuzhiyun $ext = $2; 432*4882a593Smuzhiyun} else { 433*4882a593Smuzhiyun $prefix = $filename; 434*4882a593Smuzhiyun $ext = ""; 435*4882a593Smuzhiyun} 436*4882a593Smuzhiyun 437*4882a593Smuzhiyunmy $mcount_s = $dirname . "/.tmp_mc_" . $prefix . ".s"; 438*4882a593Smuzhiyunmy $mcount_o = $dirname . "/.tmp_mc_" . $prefix . ".o"; 439*4882a593Smuzhiyun 440*4882a593Smuzhiyuncheck_objcopy(); 441*4882a593Smuzhiyun 442*4882a593Smuzhiyun# 443*4882a593Smuzhiyun# Step 1: find all the local (static functions) and weak symbols. 444*4882a593Smuzhiyun# 't' is local, 'w/W' is weak 445*4882a593Smuzhiyun# 446*4882a593Smuzhiyunopen (IN, "$nm $inputfile|") || die "error running $nm"; 447*4882a593Smuzhiyunwhile (<IN>) { 448*4882a593Smuzhiyun if (/$local_regex/) { 449*4882a593Smuzhiyun $locals{$1} = 1; 450*4882a593Smuzhiyun } elsif (/$weak_regex/) { 451*4882a593Smuzhiyun $weak{$2} = $1; 452*4882a593Smuzhiyun } 453*4882a593Smuzhiyun} 454*4882a593Smuzhiyunclose(IN); 455*4882a593Smuzhiyun 456*4882a593Smuzhiyunmy @offsets; # Array of offsets of mcount callers 457*4882a593Smuzhiyunmy $ref_func; # reference function to use for offsets 458*4882a593Smuzhiyunmy $offset = 0; # offset of ref_func to section beginning 459*4882a593Smuzhiyun 460*4882a593Smuzhiyun## 461*4882a593Smuzhiyun# update_funcs - print out the current mcount callers 462*4882a593Smuzhiyun# 463*4882a593Smuzhiyun# Go through the list of offsets to callers and write them to 464*4882a593Smuzhiyun# the output file in a format that can be read by an assembler. 465*4882a593Smuzhiyun# 466*4882a593Smuzhiyunsub update_funcs 467*4882a593Smuzhiyun{ 468*4882a593Smuzhiyun return unless ($ref_func and @offsets); 469*4882a593Smuzhiyun 470*4882a593Smuzhiyun # Sanity check on weak function. A weak function may be overwritten by 471*4882a593Smuzhiyun # another function of the same name, making all these offsets incorrect. 472*4882a593Smuzhiyun if (defined $weak{$ref_func}) { 473*4882a593Smuzhiyun die "$inputfile: ERROR: referencing weak function" . 474*4882a593Smuzhiyun " $ref_func for mcount\n"; 475*4882a593Smuzhiyun } 476*4882a593Smuzhiyun 477*4882a593Smuzhiyun # is this function static? If so, note this fact. 478*4882a593Smuzhiyun if (defined $locals{$ref_func}) { 479*4882a593Smuzhiyun 480*4882a593Smuzhiyun # only use locals if objcopy supports globalize-symbols 481*4882a593Smuzhiyun if (!$can_use_local) { 482*4882a593Smuzhiyun return; 483*4882a593Smuzhiyun } 484*4882a593Smuzhiyun $convert{$ref_func} = 1; 485*4882a593Smuzhiyun } 486*4882a593Smuzhiyun 487*4882a593Smuzhiyun # Loop through all the mcount caller offsets and print a reference 488*4882a593Smuzhiyun # to the caller based from the ref_func. 489*4882a593Smuzhiyun if (!$opened) { 490*4882a593Smuzhiyun open(FILE, ">$mcount_s") || die "can't create $mcount_s\n"; 491*4882a593Smuzhiyun $opened = 1; 492*4882a593Smuzhiyun print FILE "\t.section $mcount_section,\"a\",$section_type\n"; 493*4882a593Smuzhiyun print FILE "\t.align $alignment\n" if (defined($alignment)); 494*4882a593Smuzhiyun } 495*4882a593Smuzhiyun foreach my $cur_offset (@offsets) { 496*4882a593Smuzhiyun printf FILE "\t%s %s + %d\n", $type, $ref_func, $cur_offset - $offset; 497*4882a593Smuzhiyun } 498*4882a593Smuzhiyun} 499*4882a593Smuzhiyun 500*4882a593Smuzhiyun# 501*4882a593Smuzhiyun# Step 2: find the sections and mcount call sites 502*4882a593Smuzhiyun# 503*4882a593Smuzhiyunopen(IN, "LANG=C $objdump -hdr $inputfile|") || die "error running $objdump"; 504*4882a593Smuzhiyun 505*4882a593Smuzhiyunmy $text; 506*4882a593Smuzhiyun 507*4882a593Smuzhiyun 508*4882a593Smuzhiyun# read headers first 509*4882a593Smuzhiyunmy $read_headers = 1; 510*4882a593Smuzhiyun 511*4882a593Smuzhiyunwhile (<IN>) { 512*4882a593Smuzhiyun 513*4882a593Smuzhiyun if ($read_headers && /$mcount_section/) { 514*4882a593Smuzhiyun # 515*4882a593Smuzhiyun # Somehow the make process can execute this script on an 516*4882a593Smuzhiyun # object twice. If it does, we would duplicate the mcount 517*4882a593Smuzhiyun # section and it will cause the function tracer self test 518*4882a593Smuzhiyun # to fail. Check if the mcount section exists, and if it does, 519*4882a593Smuzhiyun # warn and exit. 520*4882a593Smuzhiyun # 521*4882a593Smuzhiyun print STDERR "ERROR: $mcount_section already in $inputfile\n" . 522*4882a593Smuzhiyun "\tThis may be an indication that your build is corrupted.\n" . 523*4882a593Smuzhiyun "\tDelete $inputfile and try again. If the same object file\n" . 524*4882a593Smuzhiyun "\tstill causes an issue, then disable CONFIG_DYNAMIC_FTRACE.\n"; 525*4882a593Smuzhiyun exit(-1); 526*4882a593Smuzhiyun } 527*4882a593Smuzhiyun 528*4882a593Smuzhiyun # is it a section? 529*4882a593Smuzhiyun if (/$section_regex/) { 530*4882a593Smuzhiyun $read_headers = 0; 531*4882a593Smuzhiyun 532*4882a593Smuzhiyun # Only record text sections that we know are safe 533*4882a593Smuzhiyun $read_function = defined($text_sections{$1}); 534*4882a593Smuzhiyun if (!$read_function) { 535*4882a593Smuzhiyun foreach my $prefix (keys %text_section_prefixes) { 536*4882a593Smuzhiyun if (substr($1, 0, length $prefix) eq $prefix) { 537*4882a593Smuzhiyun $read_function = 1; 538*4882a593Smuzhiyun last; 539*4882a593Smuzhiyun } 540*4882a593Smuzhiyun } 541*4882a593Smuzhiyun } 542*4882a593Smuzhiyun # print out any recorded offsets 543*4882a593Smuzhiyun update_funcs(); 544*4882a593Smuzhiyun 545*4882a593Smuzhiyun # reset all markers and arrays 546*4882a593Smuzhiyun $text_found = 0; 547*4882a593Smuzhiyun undef($ref_func); 548*4882a593Smuzhiyun undef(@offsets); 549*4882a593Smuzhiyun 550*4882a593Smuzhiyun # section found, now is this a start of a function? 551*4882a593Smuzhiyun } elsif ($read_function && /$function_regex/) { 552*4882a593Smuzhiyun $text_found = 1; 553*4882a593Smuzhiyun $text = $2; 554*4882a593Smuzhiyun 555*4882a593Smuzhiyun # if this is either a local function or a weak function 556*4882a593Smuzhiyun # keep looking for functions that are global that 557*4882a593Smuzhiyun # we can use safely. 558*4882a593Smuzhiyun if (!defined($locals{$text}) && !defined($weak{$text})) { 559*4882a593Smuzhiyun $ref_func = $text; 560*4882a593Smuzhiyun $read_function = 0; 561*4882a593Smuzhiyun $offset = hex $1; 562*4882a593Smuzhiyun } else { 563*4882a593Smuzhiyun # if we already have a function, and this is weak, skip it 564*4882a593Smuzhiyun if (!defined($ref_func) && !defined($weak{$text}) && 565*4882a593Smuzhiyun # PPC64 can have symbols that start with .L and 566*4882a593Smuzhiyun # gcc considers these special. Don't use them! 567*4882a593Smuzhiyun $text !~ /^\.L/) { 568*4882a593Smuzhiyun $ref_func = $text; 569*4882a593Smuzhiyun $offset = hex $1; 570*4882a593Smuzhiyun } 571*4882a593Smuzhiyun } 572*4882a593Smuzhiyun } 573*4882a593Smuzhiyun # is this a call site to mcount? If so, record it to print later 574*4882a593Smuzhiyun if ($text_found && /$mcount_regex/) { 575*4882a593Smuzhiyun push(@offsets, (hex $1) + $mcount_adjust); 576*4882a593Smuzhiyun } 577*4882a593Smuzhiyun} 578*4882a593Smuzhiyun 579*4882a593Smuzhiyun# dump out anymore offsets that may have been found 580*4882a593Smuzhiyunupdate_funcs(); 581*4882a593Smuzhiyun 582*4882a593Smuzhiyun# If we did not find any mcount callers, we are done (do nothing). 583*4882a593Smuzhiyunif (!$opened) { 584*4882a593Smuzhiyun exit(0); 585*4882a593Smuzhiyun} 586*4882a593Smuzhiyun 587*4882a593Smuzhiyunclose(FILE); 588*4882a593Smuzhiyun 589*4882a593Smuzhiyun# 590*4882a593Smuzhiyun# Step 3: Compile the file that holds the list of call sites to mcount. 591*4882a593Smuzhiyun# 592*4882a593Smuzhiyun`$cc -o $mcount_o -c $mcount_s`; 593*4882a593Smuzhiyun 594*4882a593Smuzhiyunmy @converts = keys %convert; 595*4882a593Smuzhiyun 596*4882a593Smuzhiyun# 597*4882a593Smuzhiyun# Step 4: Do we have sections that started with local functions? 598*4882a593Smuzhiyun# 599*4882a593Smuzhiyunif ($#converts >= 0) { 600*4882a593Smuzhiyun my $globallist = ""; 601*4882a593Smuzhiyun my $locallist = ""; 602*4882a593Smuzhiyun 603*4882a593Smuzhiyun foreach my $con (@converts) { 604*4882a593Smuzhiyun $globallist .= " --globalize-symbol $con"; 605*4882a593Smuzhiyun $locallist .= " --localize-symbol $con"; 606*4882a593Smuzhiyun } 607*4882a593Smuzhiyun 608*4882a593Smuzhiyun my $globalobj = $dirname . "/.tmp_gl_" . $filename; 609*4882a593Smuzhiyun my $globalmix = $dirname . "/.tmp_mx_" . $filename; 610*4882a593Smuzhiyun 611*4882a593Smuzhiyun # 612*4882a593Smuzhiyun # Step 5: set up each local function as a global 613*4882a593Smuzhiyun # 614*4882a593Smuzhiyun `$objcopy $globallist $inputfile $globalobj`; 615*4882a593Smuzhiyun 616*4882a593Smuzhiyun # 617*4882a593Smuzhiyun # Step 6: Link the global version to our list. 618*4882a593Smuzhiyun # 619*4882a593Smuzhiyun `$ld -r $globalobj $mcount_o -o $globalmix`; 620*4882a593Smuzhiyun 621*4882a593Smuzhiyun # 622*4882a593Smuzhiyun # Step 7: Convert the local functions back into local symbols 623*4882a593Smuzhiyun # 624*4882a593Smuzhiyun `$objcopy $locallist $globalmix $inputfile`; 625*4882a593Smuzhiyun 626*4882a593Smuzhiyun # Remove the temp files 627*4882a593Smuzhiyun `$rm $globalobj $globalmix`; 628*4882a593Smuzhiyun 629*4882a593Smuzhiyun} else { 630*4882a593Smuzhiyun 631*4882a593Smuzhiyun my $mix = $dirname . "/.tmp_mx_" . $filename; 632*4882a593Smuzhiyun 633*4882a593Smuzhiyun # 634*4882a593Smuzhiyun # Step 8: Link the object with our list of call sites object. 635*4882a593Smuzhiyun # 636*4882a593Smuzhiyun `$ld -r $inputfile $mcount_o -o $mix`; 637*4882a593Smuzhiyun 638*4882a593Smuzhiyun # 639*4882a593Smuzhiyun # Step 9: Move the result back to the original object. 640*4882a593Smuzhiyun # 641*4882a593Smuzhiyun `$mv $mix $inputfile`; 642*4882a593Smuzhiyun} 643*4882a593Smuzhiyun 644*4882a593Smuzhiyun# Clean up the temp files 645*4882a593Smuzhiyun`$rm $mcount_o $mcount_s`; 646*4882a593Smuzhiyun 647*4882a593Smuzhiyunexit(0); 648