Back to home page

LXR

 
 

    


0001 #!/usr/bin/perl -w
0002 # (c) 2008, Steven Rostedt <srostedt@redhat.com>
0003 # Licensed under the terms of the GNU GPL License version 2
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 strict;
0110 
0111 my $P = $0;
0112 $P =~ s@.*/@@g;
0113 
0114 my $V = '0.1';
0115 
0116 if ($#ARGV != 11) {
0117     print "usage: $P arch endian bits objdump objcopy cc ld nm rm mv is_module inputfile\n";
0118     print "version: $V\n";
0119     exit(1);
0120 }
0121 
0122 my ($arch, $endian, $bits, $objdump, $objcopy, $cc,
0123     $ld, $nm, $rm, $mv, $is_module, $inputfile) = @ARGV;
0124 
0125 # This file refers to mcount and shouldn't be ftraced, so lets' ignore it
0126 if ($inputfile =~ m,kernel/trace/ftrace\.o$,) {
0127     exit(0);
0128 }
0129 
0130 # Acceptable sections to record.
0131 my %text_sections = (
0132      ".text" => 1,
0133      ".ref.text" => 1,
0134      ".sched.text" => 1,
0135      ".spinlock.text" => 1,
0136      ".irqentry.text" => 1,
0137      ".softirqentry.text" => 1,
0138      ".kprobes.text" => 1,
0139      ".cpuidle.text" => 1,
0140      ".text.unlikely" => 1,
0141 );
0142 
0143 # Note: we are nice to C-programmers here, thus we skip the '||='-idiom.
0144 $objdump = 'objdump' if (!$objdump);
0145 $objcopy = 'objcopy' if (!$objcopy);
0146 $cc = 'gcc' if (!$cc);
0147 $ld = 'ld' if (!$ld);
0148 $nm = 'nm' if (!$nm);
0149 $rm = 'rm' if (!$rm);
0150 $mv = 'mv' if (!$mv);
0151 
0152 #print STDERR "running: $P '$arch' '$objdump' '$objcopy' '$cc' '$ld' " .
0153 #    "'$nm' '$rm' '$mv' '$inputfile'\n";
0154 
0155 my %locals;     # List of local (static) functions
0156 my %weak;       # List of weak functions
0157 my %convert;        # List of local functions used that needs conversion
0158 
0159 my $type;
0160 my $local_regex;    # Match a local function (return function)
0161 my $weak_regex;     # Match a weak function (return function)
0162 my $section_regex;  # Find the start of a section
0163 my $function_regex; # Find the name of a function
0164             #    (return offset and func name)
0165 my $mcount_regex;   # Find the call site to mcount (return offset)
0166 my $mcount_adjust;  # Address adjustment to mcount offset
0167 my $alignment;      # The .align value to use for $mcount_section
0168 my $section_type;   # Section header plus possible alignment command
0169 my $can_use_local = 0;  # If we can use local function references
0170 
0171 # Shut up recordmcount if user has older objcopy
0172 my $quiet_recordmcount = ".tmp_quiet_recordmcount";
0173 my $print_warning = 1;
0174 $print_warning = 0 if ( -f $quiet_recordmcount);
0175 
0176 ##
0177 # check_objcopy - whether objcopy supports --globalize-symbols
0178 #
0179 #  --globalize-symbols came out in 2.17, we must test the version
0180 #  of objcopy, and if it is less than 2.17, then we can not
0181 #  record local functions.
0182 sub check_objcopy
0183 {
0184     open (IN, "$objcopy --version |") or die "error running $objcopy";
0185     while (<IN>) {
0186     if (/objcopy.*\s(\d+)\.(\d+)/) {
0187         $can_use_local = 1 if ($1 > 2 || ($1 == 2 && $2 >= 17));
0188         last;
0189     }
0190     }
0191     close (IN);
0192 
0193     if (!$can_use_local && $print_warning) {
0194     print STDERR "WARNING: could not find objcopy version or version " .
0195         "is less than 2.17.\n" .
0196         "\tLocal function references are disabled.\n";
0197     open (QUIET, ">$quiet_recordmcount");
0198     printf QUIET "Disables the warning from recordmcount.pl\n";
0199     close QUIET;
0200     }
0201 }
0202 
0203 if ($arch =~ /(x86(_64)?)|(i386)/) {
0204     if ($bits == 64) {
0205     $arch = "x86_64";
0206     } else {
0207     $arch = "i386";
0208     }
0209 }
0210 
0211 #
0212 # We base the defaults off of i386, the other archs may
0213 # feel free to change them in the below if statements.
0214 #
0215 $local_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\S+)";
0216 $weak_regex = "^[0-9a-fA-F]+\\s+([wW])\\s+(\\S+)";
0217 $section_regex = "Disassembly of section\\s+(\\S+):";
0218 $function_regex = "^([0-9a-fA-F]+)\\s+<(.*?)>:";
0219 $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s(mcount|__fentry__)\$";
0220 $section_type = '@progbits';
0221 $mcount_adjust = 0;
0222 $type = ".long";
0223 
0224 if ($arch eq "x86_64") {
0225     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s(mcount|__fentry__)([+-]0x[0-9a-zA-Z]+)?\$";
0226     $type = ".quad";
0227     $alignment = 8;
0228     $mcount_adjust = -1;
0229 
0230     # force flags for this arch
0231     $ld .= " -m elf_x86_64";
0232     $objdump .= " -M x86-64";
0233     $objcopy .= " -O elf64-x86-64";
0234     $cc .= " -m64";
0235 
0236 } elsif ($arch eq "i386") {
0237     $alignment = 4;
0238     $mcount_adjust = -1;
0239 
0240     # force flags for this arch
0241     $ld .= " -m elf_i386";
0242     $objdump .= " -M i386";
0243     $objcopy .= " -O elf32-i386";
0244     $cc .= " -m32";
0245 
0246 } elsif ($arch eq "s390" && $bits == 64) {
0247     if ($cc =~ /-DCC_USING_HOTPATCH/) {
0248     $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*c0 04 00 00 00 00\\s*brcl\\s*0,[0-9a-f]+ <([^\+]*)>\$";
0249     $mcount_adjust = 0;
0250     } else {
0251     $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_390_(PC|PLT)32DBL\\s+_mcount\\+0x2\$";
0252     $mcount_adjust = -14;
0253     }
0254     $alignment = 8;
0255     $type = ".quad";
0256     $ld .= " -m elf64_s390";
0257     $cc .= " -m64";
0258 
0259 } elsif ($arch eq "sh") {
0260     $alignment = 2;
0261 
0262     # force flags for this arch
0263     $ld .= " -m shlelf_linux";
0264     $objcopy .= " -O elf32-sh-linux";
0265 
0266 } elsif ($arch eq "powerpc") {
0267     $local_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\.?\\S+)";
0268     # See comment in the sparc64 section for why we use '\w'.
0269     $function_regex = "^([0-9a-fA-F]+)\\s+<(\\.?\\w*?)>:";
0270     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s\\.?_mcount\$";
0271 
0272     if ($bits == 64) {
0273     $type = ".quad";
0274     }
0275 
0276 } elsif ($arch eq "arm") {
0277     $alignment = 2;
0278     $section_type = '%progbits';
0279     $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_ARM_(CALL|PC24|THM_CALL)" .
0280             "\\s+(__gnu_mcount_nc|mcount)\$";
0281 
0282 } elsif ($arch eq "arm64") {
0283     $alignment = 3;
0284     $section_type = '%progbits';
0285     $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_AARCH64_CALL26\\s+_mcount\$";
0286     $type = ".quad";
0287 } elsif ($arch eq "ia64") {
0288     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
0289     $type = "data8";
0290 
0291     if ($is_module eq "0") {
0292         $cc .= " -mconstant-gp";
0293     }
0294 } elsif ($arch eq "sparc64") {
0295     # In the objdump output there are giblets like:
0296     # 0000000000000000 <igmp_net_exit-0x18>:
0297     # As there's some data blobs that get emitted into the
0298     # text section before the first instructions and the first
0299     # real symbols.  We don't want to match that, so to combat
0300     # this we use '\w' so we'll match just plain symbol names,
0301     # and not those that also include hex offsets inside of the
0302     # '<>' brackets.  Actually the generic function_regex setting
0303     # could safely use this too.
0304     $function_regex = "^([0-9a-fA-F]+)\\s+<(\\w*?)>:";
0305 
0306     # Sparc64 calls '_mcount' instead of plain 'mcount'.
0307     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
0308 
0309     $alignment = 8;
0310     $type = ".xword";
0311     $ld .= " -m elf64_sparc";
0312     $cc .= " -m64";
0313     $objcopy .= " -O elf64-sparc";
0314 } elsif ($arch eq "mips") {
0315     # To enable module support, we need to enable the -mlong-calls option
0316     # of gcc for module, after using this option, we can not get the real
0317     # offset of the calling to _mcount, but the offset of the lui
0318     # instruction or the addiu one. herein, we record the address of the
0319     # first one, and then we can replace this instruction by a branch
0320     # instruction to jump over the profiling function to filter the
0321     # indicated functions, or swith back to the lui instruction to trace
0322     # them, which means dynamic tracing.
0323     #
0324     #       c:  3c030000    lui v1,0x0
0325     #           c: R_MIPS_HI16  _mcount
0326     #           c: R_MIPS_NONE  *ABS*
0327     #           c: R_MIPS_NONE  *ABS*
0328     #      10:  64630000    daddiu  v1,v1,0
0329     #           10: R_MIPS_LO16 _mcount
0330     #           10: R_MIPS_NONE *ABS*
0331     #           10: R_MIPS_NONE *ABS*
0332     #      14:  03e0082d    move    at,ra
0333     #      18:  0060f809    jalr    v1
0334     #
0335     # for the kernel:
0336     #
0337     #     10:   03e0082d        move    at,ra
0338     #     14:   0c000000        jal     0 <loongson_halt>
0339     #                    14: R_MIPS_26   _mcount
0340     #                    14: R_MIPS_NONE *ABS*
0341     #                    14: R_MIPS_NONE *ABS*
0342     #    18:   00020021        nop
0343     if ($is_module eq "0") {
0344         $mcount_regex = "^\\s*([0-9a-fA-F]+): R_MIPS_26\\s+_mcount\$";
0345     } else {
0346         $mcount_regex = "^\\s*([0-9a-fA-F]+): R_MIPS_HI16\\s+_mcount\$";
0347     }
0348     $objdump .= " -Melf-trad".$endian."mips ";
0349 
0350     if ($endian eq "big") {
0351         $endian = " -EB ";
0352         $ld .= " -melf".$bits."btsmip";
0353     } else {
0354         $endian = " -EL ";
0355         $ld .= " -melf".$bits."ltsmip";
0356     }
0357 
0358     $cc .= " -mno-abicalls -fno-pic -mabi=" . $bits . $endian;
0359     $ld .= $endian;
0360 
0361     if ($bits == 64) {
0362         $function_regex =
0363         "^([0-9a-fA-F]+)\\s+<(.|[^\$]L.*?|\$[^L].*?|[^\$][^L].*?)>:";
0364         $type = ".dword";
0365     }
0366 } elsif ($arch eq "microblaze") {
0367     # Microblaze calls '_mcount' instead of plain 'mcount'.
0368     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
0369 } elsif ($arch eq "blackfin") {
0370     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s__mcount\$";
0371     $mcount_adjust = -4;
0372 } elsif ($arch eq "tilegx" || $arch eq "tile") {
0373     # Default to the newer TILE-Gx architecture if only "tile" is given.
0374     $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s__mcount\$";
0375     $type = ".quad";
0376     $alignment = 8;
0377 } else {
0378     die "Arch $arch is not supported with CONFIG_FTRACE_MCOUNT_RECORD";
0379 }
0380 
0381 my $text_found = 0;
0382 my $read_function = 0;
0383 my $opened = 0;
0384 my $mcount_section = "__mcount_loc";
0385 
0386 my $dirname;
0387 my $filename;
0388 my $prefix;
0389 my $ext;
0390 
0391 if ($inputfile =~ m,^(.*)/([^/]*)$,) {
0392     $dirname = $1;
0393     $filename = $2;
0394 } else {
0395     $dirname = ".";
0396     $filename = $inputfile;
0397 }
0398 
0399 if ($filename =~ m,^(.*)(\.\S),) {
0400     $prefix = $1;
0401     $ext = $2;
0402 } else {
0403     $prefix = $filename;
0404     $ext = "";
0405 }
0406 
0407 my $mcount_s = $dirname . "/.tmp_mc_" . $prefix . ".s";
0408 my $mcount_o = $dirname . "/.tmp_mc_" . $prefix . ".o";
0409 
0410 check_objcopy();
0411 
0412 #
0413 # Step 1: find all the local (static functions) and weak symbols.
0414 #         't' is local, 'w/W' is weak
0415 #
0416 open (IN, "$nm $inputfile|") || die "error running $nm";
0417 while (<IN>) {
0418     if (/$local_regex/) {
0419     $locals{$1} = 1;
0420     } elsif (/$weak_regex/) {
0421     $weak{$2} = $1;
0422     }
0423 }
0424 close(IN);
0425 
0426 my @offsets;        # Array of offsets of mcount callers
0427 my $ref_func;       # reference function to use for offsets
0428 my $offset = 0;     # offset of ref_func to section beginning
0429 
0430 ##
0431 # update_funcs - print out the current mcount callers
0432 #
0433 #  Go through the list of offsets to callers and write them to
0434 #  the output file in a format that can be read by an assembler.
0435 #
0436 sub update_funcs
0437 {
0438     return unless ($ref_func and @offsets);
0439 
0440     # Sanity check on weak function. A weak function may be overwritten by
0441     # another function of the same name, making all these offsets incorrect.
0442     if (defined $weak{$ref_func}) {
0443     die "$inputfile: ERROR: referencing weak function" .
0444         " $ref_func for mcount\n";
0445     }
0446 
0447     # is this function static? If so, note this fact.
0448     if (defined $locals{$ref_func}) {
0449 
0450     # only use locals if objcopy supports globalize-symbols
0451     if (!$can_use_local) {
0452         return;
0453     }
0454     $convert{$ref_func} = 1;
0455     }
0456 
0457     # Loop through all the mcount caller offsets and print a reference
0458     # to the caller based from the ref_func.
0459     if (!$opened) {
0460     open(FILE, ">$mcount_s") || die "can't create $mcount_s\n";
0461     $opened = 1;
0462     print FILE "\t.section $mcount_section,\"a\",$section_type\n";
0463     print FILE "\t.align $alignment\n" if (defined($alignment));
0464     }
0465     foreach my $cur_offset (@offsets) {
0466     printf FILE "\t%s %s + %d\n", $type, $ref_func, $cur_offset - $offset;
0467     }
0468 }
0469 
0470 #
0471 # Step 2: find the sections and mcount call sites
0472 #
0473 open(IN, "$objdump -hdr $inputfile|") || die "error running $objdump";
0474 
0475 my $text;
0476 
0477 
0478 # read headers first
0479 my $read_headers = 1;
0480 
0481 while (<IN>) {
0482 
0483     if ($read_headers && /$mcount_section/) {
0484     #
0485     # Somehow the make process can execute this script on an
0486     # object twice. If it does, we would duplicate the mcount
0487     # section and it will cause the function tracer self test
0488     # to fail. Check if the mcount section exists, and if it does,
0489     # warn and exit.
0490     #
0491     print STDERR "ERROR: $mcount_section already in $inputfile\n" .
0492         "\tThis may be an indication that your build is corrupted.\n" .
0493         "\tDelete $inputfile and try again. If the same object file\n" .
0494         "\tstill causes an issue, then disable CONFIG_DYNAMIC_FTRACE.\n";
0495     exit(-1);
0496     }
0497 
0498     # is it a section?
0499     if (/$section_regex/) {
0500     $read_headers = 0;
0501 
0502     # Only record text sections that we know are safe
0503     $read_function = defined($text_sections{$1});
0504     # print out any recorded offsets
0505     update_funcs();
0506 
0507     # reset all markers and arrays
0508     $text_found = 0;
0509     undef($ref_func);
0510     undef(@offsets);
0511 
0512     # section found, now is this a start of a function?
0513     } elsif ($read_function && /$function_regex/) {
0514     $text_found = 1;
0515     $text = $2;
0516 
0517     # if this is either a local function or a weak function
0518     # keep looking for functions that are global that
0519     # we can use safely.
0520     if (!defined($locals{$text}) && !defined($weak{$text})) {
0521         $ref_func = $text;
0522         $read_function = 0;
0523         $offset = hex $1;
0524     } else {
0525         # if we already have a function, and this is weak, skip it
0526         if (!defined($ref_func) && !defined($weak{$text}) &&
0527          # PPC64 can have symbols that start with .L and
0528          # gcc considers these special. Don't use them!
0529          $text !~ /^\.L/) {
0530         $ref_func = $text;
0531         $offset = hex $1;
0532         }
0533     }
0534     }
0535     # is this a call site to mcount? If so, record it to print later
0536     if ($text_found && /$mcount_regex/) {
0537     push(@offsets, (hex $1) + $mcount_adjust);
0538     }
0539 }
0540 
0541 # dump out anymore offsets that may have been found
0542 update_funcs();
0543 
0544 # If we did not find any mcount callers, we are done (do nothing).
0545 if (!$opened) {
0546     exit(0);
0547 }
0548 
0549 close(FILE);
0550 
0551 #
0552 # Step 3: Compile the file that holds the list of call sites to mcount.
0553 #
0554 `$cc -o $mcount_o -c $mcount_s`;
0555 
0556 my @converts = keys %convert;
0557 
0558 #
0559 # Step 4: Do we have sections that started with local functions?
0560 #
0561 if ($#converts >= 0) {
0562     my $globallist = "";
0563     my $locallist = "";
0564 
0565     foreach my $con (@converts) {
0566     $globallist .= " --globalize-symbol $con";
0567     $locallist .= " --localize-symbol $con";
0568     }
0569 
0570     my $globalobj = $dirname . "/.tmp_gl_" . $filename;
0571     my $globalmix = $dirname . "/.tmp_mx_" . $filename;
0572 
0573     #
0574     # Step 5: set up each local function as a global
0575     #
0576     `$objcopy $globallist $inputfile $globalobj`;
0577 
0578     #
0579     # Step 6: Link the global version to our list.
0580     #
0581     `$ld -r $globalobj $mcount_o -o $globalmix`;
0582 
0583     #
0584     # Step 7: Convert the local functions back into local symbols
0585     #
0586     `$objcopy $locallist $globalmix $inputfile`;
0587 
0588     # Remove the temp files
0589     `$rm $globalobj $globalmix`;
0590 
0591 } else {
0592 
0593     my $mix = $dirname . "/.tmp_mx_" . $filename;
0594 
0595     #
0596     # Step 8: Link the object with our list of call sites object.
0597     #
0598     `$ld -r $inputfile $mcount_o -o $mix`;
0599 
0600     #
0601     # Step 9: Move the result back to the original object.
0602     #
0603     `$mv $mix $inputfile`;
0604 }
0605 
0606 # Clean up the temp files
0607 `$rm $mcount_o $mcount_s`;
0608 
0609 exit(0);