| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 |
|
| 109 | use warnings;
|
| 110 | use strict;
|
| 111 |
|
| 112 | my $P = $0;
|
| 113 | $P =~ s@.*/@@g;
|
| 114 |
|
| 115 | my $V = '0.1';
|
| 116 |
|
| 117 | if ($#ARGV != 11) {
|
| 118 | print "usage: $P arch endian bits objdump objcopy cc ld nm rm mv is_module inputfile\n";
|
| 119 | print "version: $V\n";
|
| 120 | exit(1);
|
| 121 | }
|
| 122 |
|
| 123 | my ($arch, $endian, $bits, $objdump, $objcopy, $cc,
|
| 124 | $ld, $nm, $rm, $mv, $is_module, $inputfile) = @ARGV;
|
| 125 |
|
| 126 | |
| 127 | if ($inputfile =~ m,kernel/trace/ftrace\.o$,) {
|
| 128 | exit(0);
|
| 129 | }
|
| 130 |
|
| 131 | |
| 132 | my %text_sections = (
|
| 133 | ".text" => 1,
|
| 134 | ".init.text" => 1,
|
| 135 | ".ref.text" => 1,
|
| 136 | ".sched.text" => 1,
|
| 137 | ".spinlock.text" => 1,
|
| 138 | ".irqentry.text" => 1,
|
| 139 | ".softirqentry.text" => 1,
|
| 140 | ".kprobes.text" => 1,
|
| 141 | ".cpuidle.text" => 1,
|
| 142 | ".text.unlikely" => 1,
|
| 143 | );
|
| 144 |
|
| 145 | |
| 146 | my %text_section_prefixes = (
|
| 147 | ".text." => 1,
|
| 148 | );
|
| 149 |
|
| 150 | |
| 151 | $objdump = 'objdump' if (!$objdump);
|
| 152 | $objcopy = 'objcopy' if (!$objcopy);
|
| 153 | $cc = 'gcc' if (!$cc);
|
| 154 | $ld = 'ld' if (!$ld);
|
| 155 | $nm = 'nm' if (!$nm);
|
| 156 | $rm = 'rm' if (!$rm);
|
| 157 | $mv = 'mv' if (!$mv);
|
| 158 |
|
| 159 | |
| 160 | |
| 161 |
|
| 162 | my %locals; |
| 163 | my %weak; |
| 164 | my %convert; |
| 165 |
|
| 166 | my $type;
|
| 167 | my $local_regex; |
| 168 | my $weak_regex; |
| 169 | my $section_regex; |
| 170 | my $function_regex; |
| 171 | |
| 172 | my $mcount_regex; |
| 173 | my $mcount_adjust; |
| 174 | my $alignment; |
| 175 | my $section_type; |
| 176 |
|
| 177 | if ($arch =~ /(x86(_64)?)|(i386)/) {
|
| 178 | if ($bits == 64) {
|
| 179 | $arch = "x86_64";
|
| 180 | } else {
|
| 181 | $arch = "i386";
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | $local_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\S+)";
|
| 190 | $weak_regex = "^[0-9a-fA-F]+\\s+([wW])\\s+(\\S+)";
|
| 191 | $section_regex = "Disassembly of section\\s+(\\S+):";
|
| 192 | $function_regex = "^([0-9a-fA-F]+)\\s+<([^^]*?)>:";
|
| 193 | $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s(mcount|__fentry__)\$";
|
| 194 | $section_type = '@progbits';
|
| 195 | $mcount_adjust = 0;
|
| 196 | $type = ".long";
|
| 197 |
|
| 198 | if ($arch eq "x86_64") {
|
| 199 | $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s(mcount|__fentry__)([+-]0x[0-9a-zA-Z]+)?\$";
|
| 200 | $type = ".quad";
|
| 201 | $alignment = 8;
|
| 202 | $mcount_adjust = -1;
|
| 203 |
|
| 204 | |
| 205 | $ld .= " -m elf_x86_64";
|
| 206 | $objdump .= " -M x86-64";
|
| 207 | $objcopy .= " -O elf64-x86-64";
|
| 208 | $cc .= " -m64";
|
| 209 |
|
| 210 | } elsif ($arch eq "i386") {
|
| 211 | $alignment = 4;
|
| 212 | $mcount_adjust = -1;
|
| 213 |
|
| 214 | |
| 215 | $ld .= " -m elf_i386";
|
| 216 | $objdump .= " -M i386";
|
| 217 | $objcopy .= " -O elf32-i386";
|
| 218 | $cc .= " -m32";
|
| 219 |
|
| 220 | } elsif ($arch eq "s390" && $bits == 64) {
|
| 221 | if ($cc =~ /-DCC_USING_HOTPATCH/) {
|
| 222 | $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*c0 04 00 00 00 00\\s*(brcl\\s*0,|jgnop\\s*)[0-9a-f]+ <([^\+]*)>\$";
|
| 223 | $mcount_adjust = 0;
|
| 224 | }
|
| 225 | $alignment = 8;
|
| 226 | $type = ".quad";
|
| 227 | $ld .= " -m elf64_s390";
|
| 228 | $cc .= " -m64";
|
| 229 |
|
| 230 | } elsif ($arch eq "sh") {
|
| 231 | $alignment = 2;
|
| 232 |
|
| 233 | |
| 234 | $ld .= " -m shlelf_linux";
|
| 235 | if ($endian eq "big") {
|
| 236 | $objcopy .= " -O elf32-shbig-linux";
|
| 237 | } else {
|
| 238 | $objcopy .= " -O elf32-sh-linux";
|
| 239 | }
|
| 240 |
|
| 241 | } elsif ($arch eq "powerpc") {
|
| 242 | my $ldemulation;
|
| 243 |
|
| 244 | $local_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\.?\\S+)";
|
| 245 | |
| 246 | $function_regex = "^([0-9a-fA-F]+)\\s+<(\\.?\\w*?)>:";
|
| 247 | $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s\\.?_mcount\$";
|
| 248 |
|
| 249 | if ($endian eq "big") {
|
| 250 | $cc .= " -mbig-endian ";
|
| 251 | $ld .= " -EB ";
|
| 252 | $ldemulation = "ppc"
|
| 253 | } else {
|
| 254 | $cc .= " -mlittle-endian ";
|
| 255 | $ld .= " -EL ";
|
| 256 | $ldemulation = "lppc"
|
| 257 | }
|
| 258 | if ($bits == 64) {
|
| 259 | $type = ".quad";
|
| 260 | $cc .= " -m64 ";
|
| 261 | $ld .= " -m elf64".$ldemulation." ";
|
| 262 | } else {
|
| 263 | $cc .= " -m32 ";
|
| 264 | $ld .= " -m elf32".$ldemulation." ";
|
| 265 | }
|
| 266 |
|
| 267 | } elsif ($arch eq "arm") {
|
| 268 | $alignment = 2;
|
| 269 | $section_type = '%progbits';
|
| 270 | $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_ARM_(CALL|PC24|THM_CALL)" .
|
| 271 | "\\s+(__gnu_mcount_nc|mcount)\$";
|
| 272 |
|
| 273 | } elsif ($arch eq "arm64") {
|
| 274 | $alignment = 3;
|
| 275 | $section_type = '%progbits';
|
| 276 | $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_AARCH64_CALL26\\s+_mcount\$";
|
| 277 | $type = ".quad";
|
| 278 | } elsif ($arch eq "ia64") {
|
| 279 | $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
|
| 280 | $type = "data8";
|
| 281 |
|
| 282 | if ($is_module eq "0") {
|
| 283 | $cc .= " -mconstant-gp";
|
| 284 | }
|
| 285 | } elsif ($arch eq "sparc64") {
|
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | $function_regex = "^([0-9a-fA-F]+)\\s+<(\\w*?)>:";
|
| 296 |
|
| 297 | |
| 298 | $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
|
| 299 |
|
| 300 | $alignment = 8;
|
| 301 | $type = ".xword";
|
| 302 | $ld .= " -m elf64_sparc";
|
| 303 | $cc .= " -m64";
|
| 304 | $objcopy .= " -O elf64-sparc";
|
| 305 | } elsif ($arch eq "mips") {
|
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | if ($is_module eq "0") {
|
| 335 | $mcount_regex = "^\\s*([0-9a-fA-F]+): R_MIPS_26\\s+_mcount\$";
|
| 336 | } else {
|
| 337 | $mcount_regex = "^\\s*([0-9a-fA-F]+): R_MIPS_HI16\\s+_mcount\$";
|
| 338 | }
|
| 339 | $objdump .= " -Melf-trad".$endian."mips ";
|
| 340 |
|
| 341 | if ($endian eq "big") {
|
| 342 | $endian = " -EB ";
|
| 343 | $ld .= " -melf".$bits."btsmip";
|
| 344 | } else {
|
| 345 | $endian = " -EL ";
|
| 346 | $ld .= " -melf".$bits."ltsmip";
|
| 347 | }
|
| 348 |
|
| 349 | $cc .= " -mno-abicalls -fno-pic -mabi=" . $bits . $endian;
|
| 350 | $ld .= $endian;
|
| 351 |
|
| 352 | if ($bits == 64) {
|
| 353 | $function_regex =
|
| 354 | "^([0-9a-fA-F]+)\\s+<(.|[^\$]L.*?|\$[^L].*?|[^\$][^L].*?)>:";
|
| 355 | $type = ".dword";
|
| 356 | }
|
| 357 | } elsif ($arch eq "microblaze") {
|
| 358 | |
| 359 | $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
|
| 360 | } elsif ($arch eq "riscv") {
|
| 361 | $function_regex = "^([0-9a-fA-F]+)\\s+<([^.0-9][0-9a-zA-Z_\\.]+)>:";
|
| 362 | $mcount_regex = "^\\s*([0-9a-fA-F]+):\\sR_RISCV_CALL(_PLT)?\\s_?mcount\$";
|
| 363 | $type = ".quad";
|
| 364 | $alignment = 2;
|
| 365 | } elsif ($arch eq "csky") {
|
| 366 | $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_CKCORE_PCREL_JSR_IMM26BY2\\s+_mcount\$";
|
| 367 | $alignment = 2;
|
| 368 | } else {
|
| 369 | die "Arch $arch is not supported with CONFIG_FTRACE_MCOUNT_RECORD";
|
| 370 | }
|
| 371 |
|
| 372 | my $text_found = 0;
|
| 373 | my $read_function = 0;
|
| 374 | my $opened = 0;
|
| 375 | my $mcount_section = "__mcount_loc";
|
| 376 |
|
| 377 | my $dirname;
|
| 378 | my $filename;
|
| 379 | my $prefix;
|
| 380 | my $ext;
|
| 381 |
|
| 382 | if ($inputfile =~ m,^(.*)/([^/]*)$,) {
|
| 383 | $dirname = $1;
|
| 384 | $filename = $2;
|
| 385 | } else {
|
| 386 | $dirname = ".";
|
| 387 | $filename = $inputfile;
|
| 388 | }
|
| 389 |
|
| 390 | if ($filename =~ m,^(.*)(\.\S),) {
|
| 391 | $prefix = $1;
|
| 392 | $ext = $2;
|
| 393 | } else {
|
| 394 | $prefix = $filename;
|
| 395 | $ext = "";
|
| 396 | }
|
| 397 |
|
| 398 | my $mcount_s = $dirname . "/.tmp_mc_" . $prefix . ".s";
|
| 399 | my $mcount_o = $dirname . "/.tmp_mc_" . $prefix . ".o";
|
| 400 |
|
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | open (IN, "$nm $inputfile|") || die "error running $nm";
|
| 406 | while (<IN>) {
|
| 407 | if (/$local_regex/) {
|
| 408 | $locals{$1} = 1;
|
| 409 | } elsif (/$weak_regex/) {
|
| 410 | $weak{$2} = $1;
|
| 411 | }
|
| 412 | }
|
| 413 | close(IN);
|
| 414 |
|
| 415 | my @offsets; |
| 416 | my $ref_func; |
| 417 | my $offset = 0; |
| 418 |
|
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | sub update_funcs
|
| 426 | {
|
| 427 | return unless ($ref_func and @offsets);
|
| 428 |
|
| 429 | |
| 430 | |
| 431 | if (defined $weak{$ref_func}) {
|
| 432 | die "$inputfile: ERROR: referencing weak function" .
|
| 433 | " $ref_func for mcount\n";
|
| 434 | }
|
| 435 |
|
| 436 | |
| 437 | if (defined $locals{$ref_func}) {
|
| 438 | $convert{$ref_func} = 1;
|
| 439 | }
|
| 440 |
|
| 441 | |
| 442 | |
| 443 | if (!$opened) {
|
| 444 | open(FILE, ">$mcount_s") || die "can't create $mcount_s\n";
|
| 445 | $opened = 1;
|
| 446 | print FILE "\t.section $mcount_section,\"a\",$section_type\n";
|
| 447 | print FILE "\t.align $alignment\n" if (defined($alignment));
|
| 448 | }
|
| 449 | foreach my $cur_offset (@offsets) {
|
| 450 | printf FILE "\t%s %s + %d\n", $type, $ref_func, $cur_offset - $offset;
|
| 451 | }
|
| 452 | }
|
| 453 |
|
| 454 | |
| 455 | |
| 456 | |
| 457 | open(IN, "LC_ALL=C $objdump -hdr $inputfile|") || die "error running $objdump";
|
| 458 |
|
| 459 | my $text;
|
| 460 |
|
| 461 |
|
| 462 | |
| 463 | my $read_headers = 1;
|
| 464 |
|
| 465 | while (<IN>) {
|
| 466 |
|
| 467 | if ($read_headers && /$mcount_section/) {
|
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | print STDERR "ERROR: $mcount_section already in $inputfile\n" .
|
| 476 | "\tThis may be an indication that your build is corrupted.\n" .
|
| 477 | "\tDelete $inputfile and try again. If the same object file\n" .
|
| 478 | "\tstill causes an issue, then disable CONFIG_DYNAMIC_FTRACE.\n";
|
| 479 | exit(-1);
|
| 480 | }
|
| 481 |
|
| 482 | |
| 483 | if (/$section_regex/) {
|
| 484 | $read_headers = 0;
|
| 485 |
|
| 486 | |
| 487 | $read_function = defined($text_sections{$1});
|
| 488 | if (!$read_function) {
|
| 489 | foreach my $prefix (keys %text_section_prefixes) {
|
| 490 | if (substr($1, 0, length $prefix) eq $prefix) {
|
| 491 | $read_function = 1;
|
| 492 | last;
|
| 493 | }
|
| 494 | }
|
| 495 | }
|
| 496 | |
| 497 | update_funcs();
|
| 498 |
|
| 499 | |
| 500 | $text_found = 0;
|
| 501 | undef($ref_func);
|
| 502 | undef(@offsets);
|
| 503 |
|
| 504 | |
| 505 | } elsif ($read_function && /$function_regex/) {
|
| 506 | $text_found = 1;
|
| 507 | $text = $2;
|
| 508 |
|
| 509 | |
| 510 | |
| 511 | |
| 512 | if (!defined($locals{$text}) && !defined($weak{$text})) {
|
| 513 | $ref_func = $text;
|
| 514 | $read_function = 0;
|
| 515 | $offset = hex $1;
|
| 516 | } else {
|
| 517 | |
| 518 | if (!defined($ref_func) && !defined($weak{$text}) &&
|
| 519 | |
| 520 | |
| 521 | $text !~ /^\.L/) {
|
| 522 | $ref_func = $text;
|
| 523 | $offset = hex $1;
|
| 524 | }
|
| 525 | }
|
| 526 | }
|
| 527 | |
| 528 | if ($text_found && /$mcount_regex/) {
|
| 529 | push(@offsets, (hex $1) + $mcount_adjust);
|
| 530 | }
|
| 531 | }
|
| 532 |
|
| 533 | |
| 534 | update_funcs();
|
| 535 |
|
| 536 | |
| 537 | if (!$opened) {
|
| 538 | exit(0);
|
| 539 | }
|
| 540 |
|
| 541 | close(FILE);
|
| 542 |
|
| 543 | |
| 544 | |
| 545 | |
| 546 | `$cc -o $mcount_o -c $mcount_s`;
|
| 547 |
|
| 548 | my @converts = keys %convert;
|
| 549 |
|
| 550 | |
| 551 | |
| 552 | |
| 553 | if ($#converts >= 0) {
|
| 554 | my $globallist = "";
|
| 555 | my $locallist = "";
|
| 556 |
|
| 557 | foreach my $con (@converts) {
|
| 558 | $globallist .= " --globalize-symbol $con";
|
| 559 | $locallist .= " --localize-symbol $con";
|
| 560 | }
|
| 561 |
|
| 562 | my $globalobj = $dirname . "/.tmp_gl_" . $filename;
|
| 563 | my $globalmix = $dirname . "/.tmp_mx_" . $filename;
|
| 564 |
|
| 565 | |
| 566 | |
| 567 | |
| 568 | `$objcopy $globallist $inputfile $globalobj`;
|
| 569 |
|
| 570 | |
| 571 | |
| 572 | |
| 573 | `$ld -r $globalobj $mcount_o -o $globalmix`;
|
| 574 |
|
| 575 | |
| 576 | |
| 577 | |
| 578 | `$objcopy $locallist $globalmix $inputfile`;
|
| 579 |
|
| 580 | |
| 581 | `$rm $globalobj $globalmix`;
|
| 582 |
|
| 583 | } else {
|
| 584 |
|
| 585 | my $mix = $dirname . "/.tmp_mx_" . $filename;
|
| 586 |
|
| 587 | |
| 588 | |
| 589 | |
| 590 | `$ld -r $inputfile $mcount_o -o $mix`;
|
| 591 |
|
| 592 | |
| 593 | |
| 594 | |
| 595 | `$mv $mix $inputfile`;
|
| 596 | }
|
| 597 |
|
| 598 | |
| 599 | `$rm $mcount_o $mcount_s`;
|
| 600 |
|
| 601 | exit(0);
|
| 602 |
|
| 603 | |