0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
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
0127 if ($inputfile =~ ) {
0128 exit(0);
0129 }
0130
0131
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
0146 my %text_section_prefixes = (
0147 ".text." => 1,
0148 );
0149
0150
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
0160
0161
0162 my %locals;
0163 my %weak;
0164 my %convert;
0165
0166 my $type;
0167 my $local_regex;
0168 my $weak_regex;
0169 my $section_regex;
0170 my $function_regex;
0171
0172 my $mcount_regex;
0173 my $mcount_adjust;
0174 my $alignment;
0175 my $section_type;
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
0187
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
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
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
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
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
0287
0288
0289
0290
0291
0292
0293
0294
0295 $function_regex = "^([0-9a-fA-F]+)\\s+<(\\w*?)>:";
0296
0297
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
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
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
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 =~ ) {
0383 $dirname = $1;
0384 $filename = $2;
0385 } else {
0386 $dirname = ".";
0387 $filename = $inputfile;
0388 }
0389
0390 if ($filename =~ ) {
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
0403
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;
0416 my $ref_func;
0417 my $offset = 0;
0418
0419
0420
0421
0422
0423
0424
0425 sub update_funcs
0426 {
0427 return unless ($ref_func and @offsets);
0428
0429
0430
0431 if (defined $weak{$ref_func}) {
0432 die "$inputfile: ERROR: referencing weak function" .
0433 " $ref_func for mcount\n";
0434 }
0435
0436
0437 if (defined $locals{$ref_func}) {
0438 $convert{$ref_func} = 1;
0439 }
0440
0441
0442
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
0456
0457 open(IN, "LC_ALL=C $objdump -hdr $inputfile|") || die "error running $objdump";
0458
0459 my $text;
0460
0461
0462
0463 my $read_headers = 1;
0464
0465 while (<IN>) {
0466
0467 if ($read_headers && /$mcount_section/) {
0468
0469
0470
0471
0472
0473
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
0483 if (/$section_regex/) {
0484 $read_headers = 0;
0485
0486
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
0497 update_funcs();
0498
0499
0500 $text_found = 0;
0501 undef($ref_func);
0502 undef(@offsets);
0503
0504
0505 } elsif ($read_function && /$function_regex/) {
0506 $text_found = 1;
0507 $text = $2;
0508
0509
0510
0511
0512 if (!defined($locals{$text}) && !defined($weak{$text})) {
0513 $ref_func = $text;
0514 $read_function = 0;
0515 $offset = hex $1;
0516 } else {
0517
0518 if (!defined($ref_func) && !defined($weak{$text}) &&
0519
0520
0521 $text !~ /^\.L/) {
0522 $ref_func = $text;
0523 $offset = hex $1;
0524 }
0525 }
0526 }
0527
0528 if ($text_found && /$mcount_regex/) {
0529 push(@offsets, (hex $1) + $mcount_adjust);
0530 }
0531 }
0532
0533
0534 update_funcs();
0535
0536
0537 if (!$opened) {
0538 exit(0);
0539 }
0540
0541 close(FILE);
0542
0543
0544
0545
0546 `$cc -o $mcount_o -c $mcount_s`;
0547
0548 my @converts = keys %convert;
0549
0550
0551
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
0567
0568 `$objcopy $globallist $inputfile $globalobj`;
0569
0570
0571
0572
0573 `$ld -r $globalobj $mcount_o -o $globalmix`;
0574
0575
0576
0577
0578 `$objcopy $locallist $globalmix $inputfile`;
0579
0580
0581 `$rm $globalobj $globalmix`;
0582
0583 } else {
0584
0585 my $mix = $dirname . "/.tmp_mx_" . $filename;
0586
0587
0588
0589
0590 `$ld -r $inputfile $mcount_o -o $mix`;
0591
0592
0593
0594
0595 `$mv $mix $inputfile`;
0596 }
0597
0598
0599 `$rm $mcount_o $mcount_s`;
0600
0601 exit(0);
0602
0603