Back to home page

OSCL-LXR

 
 

    


0001 #!/usr/bin/env perl
0002 # SPDX-License-Identifier: GPL-2.0-only
0003 # (c) 2008, Steven Rostedt <srostedt@redhat.com>
0004 #
0005 # recordmcount.pl - makes a section called __mcount_loc that holds
0006 #                   all the offsets to the calls to mcount.
0007 #
0008 #
0009 # What we want to end up with this is that each object file will have a
0010 # section called __mcount_loc that will hold the list of pointers to mcount
0011 # callers. After final linking, the vmlinux will have within .init.data the
0012 # list of all callers to mcount between __start_mcount_loc and __stop_mcount_loc.
0013 # Later on boot up, the kernel will read this list, save the locations and turn
0014 # them into nops. When tracing or profiling is later enabled, these locations
0015 # will then be converted back to pointers to some function.
0016 #
0017 # This is no easy feat. This script is called just after the original
0018 # object is compiled and before it is linked.
0019 #
0020 # When parse this object file using 'objdump', the references to the call
0021 # sites are offsets from the section that the call site is in. Hence, all
0022 # functions in a section that has a call site to mcount, will have the
0023 # offset from the beginning of the section and not the beginning of the
0024 # function.
0025 #
0026 # But where this section will reside finally in vmlinx is undetermined at
0027 # this point. So we can't use this kind of offsets to record the final
0028 # address of this call site.
0029 #
0030 # The trick is to change the call offset referring the start of a section to
0031 # referring a function symbol in this section. During the link step, 'ld' will
0032 # compute the final address according to the information we record.
0033 #
0034 # e.g.
0035 #
0036 #  .section ".sched.text", "ax"
0037 #        [...]
0038 #  func1:
0039 #        [...]
0040 #        call mcount  (offset: 0x10)
0041 #        [...]
0042 #        ret
0043 #  .globl fun2
0044 #  func2:             (offset: 0x20)
0045 #        [...]
0046 #        [...]
0047 #        ret
0048 #  func3:
0049 #        [...]
0050 #        call mcount (offset: 0x30)
0051 #        [...]
0052 #
0053 # Both relocation offsets for the mcounts in the above example will be
0054 # offset from .sched.text. If we choose global symbol func2 as a reference and
0055 # make another file called tmp.s with the new offsets:
0056 #
0057 #  .section __mcount_loc
0058 #  .quad  func2 - 0x10
0059 #  .quad  func2 + 0x10
0060 #
0061 # We can then compile this tmp.s into tmp.o, and link it back to the original
0062 # object.
0063 #
0064 # In our algorithm, we will choose the first global function we meet in this
0065 # section as the reference. But this gets hard if there is no global functions
0066 # in this section. In such a case we have to select a local one. E.g. func1:
0067 #
0068 #  .section ".sched.text", "ax"
0069 #  func1:
0070 #        [...]
0071 #        call mcount  (offset: 0x10)
0072 #        [...]
0073 #        ret
0074 #  func2:
0075 #        [...]
0076 #        call mcount (offset: 0x20)
0077 #        [...]
0078 #  .section "other.section"
0079 #
0080 # If we make the tmp.s the same as above, when we link together with
0081 # the original object, we will end up with two symbols for func1:
0082 # one local, one global.  After final compile, we will end up with
0083 # an undefined reference to func1 or a wrong reference to another global
0084 # func1 in other files.
0085 #
0086 # Since local objects can reference local variables, we need to find
0087 # a way to make tmp.o reference the local objects of the original object
0088 # file after it is linked together. To do this, we convert func1
0089 # into a global symbol before linking tmp.o. Then after we link tmp.o
0090 # we will only have a single symbol for func1 that is global.
0091 # We can convert func1 back into a local symbol and we are done.
0092 #
0093 # Here are the steps we take:
0094 #
0095 # 1) Record all the local and weak symbols by using 'nm'
0096 # 2) Use objdump to find all the call site offsets and sections for
0097 #    mcount.
0098 # 3) Compile the list into its own object.
0099 # 4) Do we have to deal with local functions? If not, go to step 8.
0100 # 5) Make an object that converts these local functions to global symbols
0101 #    with objcopy.
0102 # 6) Link together this new object with the list object.
0103 # 7) Convert the local functions back to local symbols and rename
0104 #    the result as the original object.
0105 # 8) Link the object with the list object.
0106 # 9) Move the result back to the original object.
0107 #
0108 
0109 use warnings;
0110 use strict;
0111 
0112 my $P = $0;
0113 $P =~ s@.*/@@g;
0114 
0115 my $V = '0.1';
0116 
0117 if ($#ARGV != 11) {
0118     print "usage: $P arch endian bits objdump objcopy cc ld nm rm mv is_module inputfile\n";
0119     print "version: $V\n";
0120     exit(1);
0121 }
0122 
0123 my ($arch, $endian, $bits, $objdump, $objcopy, $cc,
0124     $ld, $nm, $rm, $mv, $is_module, $inputfile) = @ARGV;
0125 
0126 # This file refers to mcount and shouldn't be ftraced, so lets' ignore it
0127 if ($inputfile =~ m,kernel/trace/ftrace\.o$,) {
0128     exit(0);
0129 }
0130 
0131 # Acceptable sections to record.
0132 my %text_sections = (
0133      ".text" => 1,
0134      ".init.text" => 1,
0135      ".ref.text" => 1,
0136      ".sched.text" => 1,
0137      ".spinlock.text" => 1,
0138      ".irqentry.text" => 1,
0139      ".softirqentry.text" => 1,
0140      ".kprobes.text" => 1,
0141      ".cpuidle.text" => 1,
0142      ".text.unlikely" => 1,
0143 );
0144 
0145 # Acceptable section-prefixes to record.
0146 my %text_section_prefixes = (
0147      ".text." => 1,
0148 );
0149 
0150 # Note: we are nice to C-programmers here, thus we skip the '||='-idiom.
0151 $objdump = 'objdump' if (!$objdump);
0152 $objcopy = 'objcopy' if (!$objcopy);
0153 $cc = 'gcc' if (!$cc);
0154 $ld = 'ld' if (!$ld);
0155 $nm = 'nm' if (!$nm);
0156 $rm = 'rm' if (!$rm);
0157 $mv = 'mv' if (!$mv);
0158 
0159 #print STDERR "running: $P '$arch' '$objdump' '$objcopy' '$cc' '$ld' " .
0160 #    "'$nm' '$rm' '$mv' '$inputfile'\n";
0161 
0162 my %locals;     # List of local (static) functions
0163 my %weak;       # List of weak functions
0164 my %convert;        # List of local functions used that needs conversion
0165 
0166 my $type;
0167 my $local_regex;    # Match a local function (return function)
0168 my $weak_regex;     # Match a weak function (return function)
0169 my $section_regex;  # Find the start of a section
0170 my $function_regex; # Find the name of a function
0171             #    (return offset and func name)
0172 my $mcount_regex;   # Find the call site to mcount (return offset)
0173 my $mcount_adjust;  # Address adjustment to mcount offset
0174 my $alignment;      # The .align value to use for $mcount_section
0175 my $section_type;   # Section header plus possible alignment command
0176 
0177 if ($arch =~ /(x86(_64)?)|(i386)/) {
0178     if ($bits == 64) {
0179     $arch = "x86_64";
0180     } else {
0181     $arch = "i386";
0182     }
0183 }
0184 
0185 #
0186 # We base the defaults off of i386, the other archs may
0187 # feel free to change them in the below if statements.
0188 #
0189 $local_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\S+)";
0190 $weak_regex = "^[0-9a-fA-F]+\\s+([wW])\\s+(\\S+)";
0191 $section_regex = "Disassembly of section\\s+(\\S+):";
0192 $function_regex = "^([0-9a-fA-F]+)\\s+<([^^]*?)>:";
0193 $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s(mcount|__fentry__)\$";
0194 $section_type = '@progbits';
0195 $mcount_adjust = 0;
0196 $type = ".long";
0197 
0198 if ($arch eq "x86_64") {
0199     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s(mcount|__fentry__)([+-]0x[0-9a-zA-Z]+)?\$";
0200     $type = ".quad";
0201     $alignment = 8;
0202     $mcount_adjust = -1;
0203 
0204     # force flags for this arch
0205     $ld .= " -m elf_x86_64";
0206     $objdump .= " -M x86-64";
0207     $objcopy .= " -O elf64-x86-64";
0208     $cc .= " -m64";
0209 
0210 } elsif ($arch eq "i386") {
0211     $alignment = 4;
0212     $mcount_adjust = -1;
0213 
0214     # force flags for this arch
0215     $ld .= " -m elf_i386";
0216     $objdump .= " -M i386";
0217     $objcopy .= " -O elf32-i386";
0218     $cc .= " -m32";
0219 
0220 } elsif ($arch eq "s390" && $bits == 64) {
0221     if ($cc =~ /-DCC_USING_HOTPATCH/) {
0222     $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*c0 04 00 00 00 00\\s*(brcl\\s*0,|jgnop\\s*)[0-9a-f]+ <([^\+]*)>\$";
0223     $mcount_adjust = 0;
0224     }
0225     $alignment = 8;
0226     $type = ".quad";
0227     $ld .= " -m elf64_s390";
0228     $cc .= " -m64";
0229 
0230 } elsif ($arch eq "sh") {
0231     $alignment = 2;
0232 
0233     # force flags for this arch
0234     $ld .= " -m shlelf_linux";
0235     if ($endian eq "big") {
0236     $objcopy .= " -O elf32-shbig-linux";
0237     } else {
0238     $objcopy .= " -O elf32-sh-linux";
0239     }
0240 
0241 } elsif ($arch eq "powerpc") {
0242     my $ldemulation;
0243 
0244     $local_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\.?\\S+)";
0245     # See comment in the sparc64 section for why we use '\w'.
0246     $function_regex = "^([0-9a-fA-F]+)\\s+<(\\.?\\w*?)>:";
0247     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s\\.?_mcount\$";
0248 
0249     if ($endian eq "big") {
0250         $cc .= " -mbig-endian ";
0251         $ld .= " -EB ";
0252         $ldemulation = "ppc"
0253     } else {
0254         $cc .= " -mlittle-endian ";
0255         $ld .= " -EL ";
0256         $ldemulation = "lppc"
0257     }
0258     if ($bits == 64) {
0259     $type = ".quad";
0260     $cc .= " -m64 ";
0261     $ld .= " -m elf64".$ldemulation." ";
0262     } else {
0263     $cc .= " -m32 ";
0264     $ld .= " -m elf32".$ldemulation." ";
0265     }
0266 
0267 } elsif ($arch eq "arm") {
0268     $alignment = 2;
0269     $section_type = '%progbits';
0270     $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_ARM_(CALL|PC24|THM_CALL)" .
0271             "\\s+(__gnu_mcount_nc|mcount)\$";
0272 
0273 } elsif ($arch eq "arm64") {
0274     $alignment = 3;
0275     $section_type = '%progbits';
0276     $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_AARCH64_CALL26\\s+_mcount\$";
0277     $type = ".quad";
0278 } elsif ($arch eq "ia64") {
0279     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
0280     $type = "data8";
0281 
0282     if ($is_module eq "0") {
0283     $cc .= " -mconstant-gp";
0284     }
0285 } elsif ($arch eq "sparc64") {
0286     # In the objdump output there are giblets like:
0287     # 0000000000000000 <igmp_net_exit-0x18>:
0288     # As there's some data blobs that get emitted into the
0289     # text section before the first instructions and the first
0290     # real symbols.  We don't want to match that, so to combat
0291     # this we use '\w' so we'll match just plain symbol names,
0292     # and not those that also include hex offsets inside of the
0293     # '<>' brackets.  Actually the generic function_regex setting
0294     # could safely use this too.
0295     $function_regex = "^([0-9a-fA-F]+)\\s+<(\\w*?)>:";
0296 
0297     # Sparc64 calls '_mcount' instead of plain 'mcount'.
0298     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
0299 
0300     $alignment = 8;
0301     $type = ".xword";
0302     $ld .= " -m elf64_sparc";
0303     $cc .= " -m64";
0304     $objcopy .= " -O elf64-sparc";
0305 } elsif ($arch eq "mips") {
0306     # To enable module support, we need to enable the -mlong-calls option
0307     # of gcc for module, after using this option, we can not get the real
0308     # offset of the calling to _mcount, but the offset of the lui
0309     # instruction or the addiu one. herein, we record the address of the
0310     # first one, and then we can replace this instruction by a branch
0311     # instruction to jump over the profiling function to filter the
0312     # indicated functions, or switch back to the lui instruction to trace
0313     # them, which means dynamic tracing.
0314     #
0315     #       c:  3c030000    lui v1,0x0
0316     #           c: R_MIPS_HI16  _mcount
0317     #           c: R_MIPS_NONE  *ABS*
0318     #           c: R_MIPS_NONE  *ABS*
0319     #      10:  64630000    daddiu  v1,v1,0
0320     #           10: R_MIPS_LO16 _mcount
0321     #           10: R_MIPS_NONE *ABS*
0322     #           10: R_MIPS_NONE *ABS*
0323     #      14:  03e0082d    move    at,ra
0324     #      18:  0060f809    jalr    v1
0325     #
0326     # for the kernel:
0327     #
0328     #     10:   03e0082d        move    at,ra
0329     #     14:   0c000000        jal     0 <loongson_halt>
0330     #                    14: R_MIPS_26   _mcount
0331     #                    14: R_MIPS_NONE *ABS*
0332     #                    14: R_MIPS_NONE *ABS*
0333     #    18:   00020021        nop
0334     if ($is_module eq "0") {
0335         $mcount_regex = "^\\s*([0-9a-fA-F]+): R_MIPS_26\\s+_mcount\$";
0336     } else {
0337         $mcount_regex = "^\\s*([0-9a-fA-F]+): R_MIPS_HI16\\s+_mcount\$";
0338     }
0339     $objdump .= " -Melf-trad".$endian."mips ";
0340 
0341     if ($endian eq "big") {
0342         $endian = " -EB ";
0343         $ld .= " -melf".$bits."btsmip";
0344     } else {
0345         $endian = " -EL ";
0346         $ld .= " -melf".$bits."ltsmip";
0347     }
0348 
0349     $cc .= " -mno-abicalls -fno-pic -mabi=" . $bits . $endian;
0350     $ld .= $endian;
0351 
0352     if ($bits == 64) {
0353         $function_regex =
0354         "^([0-9a-fA-F]+)\\s+<(.|[^\$]L.*?|\$[^L].*?|[^\$][^L].*?)>:";
0355         $type = ".dword";
0356     }
0357 } elsif ($arch eq "microblaze") {
0358     # Microblaze calls '_mcount' instead of plain 'mcount'.
0359     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
0360 } elsif ($arch eq "riscv") {
0361     $function_regex = "^([0-9a-fA-F]+)\\s+<([^.0-9][0-9a-zA-Z_\\.]+)>:";
0362     $mcount_regex = "^\\s*([0-9a-fA-F]+):\\sR_RISCV_CALL(_PLT)?\\s_?mcount\$";
0363     $type = ".quad";
0364     $alignment = 2;
0365 } elsif ($arch eq "csky") {
0366     $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_CKCORE_PCREL_JSR_IMM26BY2\\s+_mcount\$";
0367     $alignment = 2;
0368 } else {
0369     die "Arch $arch is not supported with CONFIG_FTRACE_MCOUNT_RECORD";
0370 }
0371 
0372 my $text_found = 0;
0373 my $read_function = 0;
0374 my $opened = 0;
0375 my $mcount_section = "__mcount_loc";
0376 
0377 my $dirname;
0378 my $filename;
0379 my $prefix;
0380 my $ext;
0381 
0382 if ($inputfile =~ m,^(.*)/([^/]*)$,) {
0383     $dirname = $1;
0384     $filename = $2;
0385 } else {
0386     $dirname = ".";
0387     $filename = $inputfile;
0388 }
0389 
0390 if ($filename =~ m,^(.*)(\.\S),) {
0391     $prefix = $1;
0392     $ext = $2;
0393 } else {
0394     $prefix = $filename;
0395     $ext = "";
0396 }
0397 
0398 my $mcount_s = $dirname . "/.tmp_mc_" . $prefix . ".s";
0399 my $mcount_o = $dirname . "/.tmp_mc_" . $prefix . ".o";
0400 
0401 #
0402 # Step 1: find all the local (static functions) and weak symbols.
0403 #         't' is local, 'w/W' is weak
0404 #
0405 open (IN, "$nm $inputfile|") || die "error running $nm";
0406 while (<IN>) {
0407     if (/$local_regex/) {
0408     $locals{$1} = 1;
0409     } elsif (/$weak_regex/) {
0410     $weak{$2} = $1;
0411     }
0412 }
0413 close(IN);
0414 
0415 my @offsets;        # Array of offsets of mcount callers
0416 my $ref_func;       # reference function to use for offsets
0417 my $offset = 0;     # offset of ref_func to section beginning
0418 
0419 ##
0420 # update_funcs - print out the current mcount callers
0421 #
0422 #  Go through the list of offsets to callers and write them to
0423 #  the output file in a format that can be read by an assembler.
0424 #
0425 sub update_funcs
0426 {
0427     return unless ($ref_func and @offsets);
0428 
0429     # Sanity check on weak function. A weak function may be overwritten by
0430     # another function of the same name, making all these offsets incorrect.
0431     if (defined $weak{$ref_func}) {
0432     die "$inputfile: ERROR: referencing weak function" .
0433         " $ref_func for mcount\n";
0434     }
0435 
0436     # is this function static? If so, note this fact.
0437     if (defined $locals{$ref_func}) {
0438     $convert{$ref_func} = 1;
0439     }
0440 
0441     # Loop through all the mcount caller offsets and print a reference
0442     # to the caller based from the ref_func.
0443     if (!$opened) {
0444     open(FILE, ">$mcount_s") || die "can't create $mcount_s\n";
0445     $opened = 1;
0446     print FILE "\t.section $mcount_section,\"a\",$section_type\n";
0447     print FILE "\t.align $alignment\n" if (defined($alignment));
0448     }
0449     foreach my $cur_offset (@offsets) {
0450     printf FILE "\t%s %s + %d\n", $type, $ref_func, $cur_offset - $offset;
0451     }
0452 }
0453 
0454 #
0455 # Step 2: find the sections and mcount call sites
0456 #
0457 open(IN, "LC_ALL=C $objdump -hdr $inputfile|") || die "error running $objdump";
0458 
0459 my $text;
0460 
0461 
0462 # read headers first
0463 my $read_headers = 1;
0464 
0465 while (<IN>) {
0466 
0467     if ($read_headers && /$mcount_section/) {
0468     #
0469     # Somehow the make process can execute this script on an
0470     # object twice. If it does, we would duplicate the mcount
0471     # section and it will cause the function tracer self test
0472     # to fail. Check if the mcount section exists, and if it does,
0473     # warn and exit.
0474     #
0475     print STDERR "ERROR: $mcount_section already in $inputfile\n" .
0476         "\tThis may be an indication that your build is corrupted.\n" .
0477         "\tDelete $inputfile and try again. If the same object file\n" .
0478         "\tstill causes an issue, then disable CONFIG_DYNAMIC_FTRACE.\n";
0479     exit(-1);
0480     }
0481 
0482     # is it a section?
0483     if (/$section_regex/) {
0484     $read_headers = 0;
0485 
0486     # Only record text sections that we know are safe
0487     $read_function = defined($text_sections{$1});
0488     if (!$read_function) {
0489         foreach my $prefix (keys %text_section_prefixes) {
0490         if (substr($1, 0, length $prefix) eq $prefix) {
0491             $read_function = 1;
0492             last;
0493         }
0494         }
0495     }
0496     # print out any recorded offsets
0497     update_funcs();
0498 
0499     # reset all markers and arrays
0500     $text_found = 0;
0501     undef($ref_func);
0502     undef(@offsets);
0503 
0504     # section found, now is this a start of a function?
0505     } elsif ($read_function && /$function_regex/) {
0506     $text_found = 1;
0507     $text = $2;
0508 
0509     # if this is either a local function or a weak function
0510     # keep looking for functions that are global that
0511     # we can use safely.
0512     if (!defined($locals{$text}) && !defined($weak{$text})) {
0513         $ref_func = $text;
0514         $read_function = 0;
0515         $offset = hex $1;
0516     } else {
0517         # if we already have a function, and this is weak, skip it
0518         if (!defined($ref_func) && !defined($weak{$text}) &&
0519          # PPC64 can have symbols that start with .L and
0520          # gcc considers these special. Don't use them!
0521          $text !~ /^\.L/) {
0522         $ref_func = $text;
0523         $offset = hex $1;
0524         }
0525     }
0526     }
0527     # is this a call site to mcount? If so, record it to print later
0528     if ($text_found && /$mcount_regex/) {
0529     push(@offsets, (hex $1) + $mcount_adjust);
0530     }
0531 }
0532 
0533 # dump out anymore offsets that may have been found
0534 update_funcs();
0535 
0536 # If we did not find any mcount callers, we are done (do nothing).
0537 if (!$opened) {
0538     exit(0);
0539 }
0540 
0541 close(FILE);
0542 
0543 #
0544 # Step 3: Compile the file that holds the list of call sites to mcount.
0545 #
0546 `$cc -o $mcount_o -c $mcount_s`;
0547 
0548 my @converts = keys %convert;
0549 
0550 #
0551 # Step 4: Do we have sections that started with local functions?
0552 #
0553 if ($#converts >= 0) {
0554     my $globallist = "";
0555     my $locallist = "";
0556 
0557     foreach my $con (@converts) {
0558     $globallist .= " --globalize-symbol $con";
0559     $locallist .= " --localize-symbol $con";
0560     }
0561 
0562     my $globalobj = $dirname . "/.tmp_gl_" . $filename;
0563     my $globalmix = $dirname . "/.tmp_mx_" . $filename;
0564 
0565     #
0566     # Step 5: set up each local function as a global
0567     #
0568     `$objcopy $globallist $inputfile $globalobj`;
0569 
0570     #
0571     # Step 6: Link the global version to our list.
0572     #
0573     `$ld -r $globalobj $mcount_o -o $globalmix`;
0574 
0575     #
0576     # Step 7: Convert the local functions back into local symbols
0577     #
0578     `$objcopy $locallist $globalmix $inputfile`;
0579 
0580     # Remove the temp files
0581     `$rm $globalobj $globalmix`;
0582 
0583 } else {
0584 
0585     my $mix = $dirname . "/.tmp_mx_" . $filename;
0586 
0587     #
0588     # Step 8: Link the object with our list of call sites object.
0589     #
0590     `$ld -r $inputfile $mcount_o -o $mix`;
0591 
0592     #
0593     # Step 9: Move the result back to the original object.
0594     #
0595     `$mv $mix $inputfile`;
0596 }
0597 
0598 # Clean up the temp files
0599 `$rm $mcount_o $mcount_s`;
0600 
0601 exit(0);
0602 
0603 # vim: softtabstop=4