| 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 "sparc64") {
|
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | $function_regex = "^([0-9a-fA-F]+)\\s+<(\\w*?)>:";
|
| 289 |
|
| 290 | |
| 291 | $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
|
| 292 |
|
| 293 | $alignment = 8;
|
| 294 | $type = ".xword";
|
| 295 | $ld .= " -m elf64_sparc";
|
| 296 | $cc .= " -m64";
|
| 297 | $objcopy .= " -O elf64-sparc";
|
| 298 | } elsif ($arch eq "mips") {
|
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | if ($is_module eq "0") {
|
| 328 | $mcount_regex = "^\\s*([0-9a-fA-F]+): R_MIPS_26\\s+_mcount\$";
|
| 329 | } else {
|
| 330 | $mcount_regex = "^\\s*([0-9a-fA-F]+): R_MIPS_HI16\\s+_mcount\$";
|
| 331 | }
|
| 332 | $objdump .= " -Melf-trad".$endian."mips ";
|
| 333 |
|
| 334 | if ($endian eq "big") {
|
| 335 | $endian = " -EB ";
|
| 336 | $ld .= " -melf".$bits."btsmip";
|
| 337 | } else {
|
| 338 | $endian = " -EL ";
|
| 339 | $ld .= " -melf".$bits."ltsmip";
|
| 340 | }
|
| 341 |
|
| 342 | $cc .= " -mno-abicalls -fno-pic -mabi=" . $bits . $endian;
|
| 343 | $ld .= $endian;
|
| 344 |
|
| 345 | if ($bits == 64) {
|
| 346 | $function_regex =
|
| 347 | "^([0-9a-fA-F]+)\\s+<(.|[^\$]L.*?|\$[^L].*?|[^\$][^L].*?)>:";
|
| 348 | $type = ".dword";
|
| 349 | }
|
| 350 | } elsif ($arch eq "microblaze") {
|
| 351 | |
| 352 | $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
|
| 353 | } elsif ($arch eq "riscv") {
|
| 354 | $function_regex = "^([0-9a-fA-F]+)\\s+<([^.0-9][0-9a-zA-Z_\\.]+)>:";
|
| 355 | $mcount_regex = "^\\s*([0-9a-fA-F]+):\\sR_RISCV_CALL(_PLT)?\\s_mcount\$";
|
| 356 | $type = ".quad";
|
| 357 | $alignment = 2;
|
| 358 | } elsif ($arch eq "csky") {
|
| 359 | $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_CKCORE_PCREL_JSR_IMM26BY2\\s+_mcount\$";
|
| 360 | $alignment = 2;
|
| 361 | } else {
|
| 362 | die "Arch $arch is not supported with CONFIG_DYNAMIC_FTRACE";
|
| 363 | }
|
| 364 |
|
| 365 | my $text_found = 0;
|
| 366 | my $read_function = 0;
|
| 367 | my $opened = 0;
|
| 368 | my $mcount_section = "__mcount_loc";
|
| 369 |
|
| 370 | my $dirname;
|
| 371 | my $filename;
|
| 372 | my $prefix;
|
| 373 | my $ext;
|
| 374 |
|
| 375 | if ($inputfile =~ m,^(.*)/([^/]*)$,) {
|
| 376 | $dirname = $1;
|
| 377 | $filename = $2;
|
| 378 | } else {
|
| 379 | $dirname = ".";
|
| 380 | $filename = $inputfile;
|
| 381 | }
|
| 382 |
|
| 383 | if ($filename =~ m,^(.*)(\.\S),) {
|
| 384 | $prefix = $1;
|
| 385 | $ext = $2;
|
| 386 | } else {
|
| 387 | $prefix = $filename;
|
| 388 | $ext = "";
|
| 389 | }
|
| 390 |
|
| 391 | my $mcount_s = $dirname . "/.tmp_mc_" . $prefix . ".s";
|
| 392 | my $mcount_o = $dirname . "/.tmp_mc_" . $prefix . ".o";
|
| 393 |
|
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | open (IN, "$nm $inputfile|") || die "error running $nm";
|
| 399 | while (<IN>) {
|
| 400 | if (/$local_regex/) {
|
| 401 | $locals{$1} = 1;
|
| 402 | } elsif (/$weak_regex/) {
|
| 403 | $weak{$2} = $1;
|
| 404 | }
|
| 405 | }
|
| 406 | close(IN);
|
| 407 |
|
| 408 | my @offsets; |
| 409 | my $ref_func; |
| 410 | my $offset = 0; |
| 411 |
|
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | sub update_funcs
|
| 419 | {
|
| 420 | return unless ($ref_func and @offsets);
|
| 421 |
|
| 422 | |
| 423 | |
| 424 | if (defined $weak{$ref_func}) {
|
| 425 | die "$inputfile: ERROR: referencing weak function" .
|
| 426 | " $ref_func for mcount\n";
|
| 427 | }
|
| 428 |
|
| 429 | |
| 430 | if (defined $locals{$ref_func}) {
|
| 431 | $convert{$ref_func} = 1;
|
| 432 | }
|
| 433 |
|
| 434 | |
| 435 | |
| 436 | if (!$opened) {
|
| 437 | open(FILE, ">$mcount_s") || die "can't create $mcount_s\n";
|
| 438 | $opened = 1;
|
| 439 | print FILE "\t.section $mcount_section,\"a\",$section_type\n";
|
| 440 | print FILE "\t.align $alignment\n" if (defined($alignment));
|
| 441 | }
|
| 442 | foreach my $cur_offset (@offsets) {
|
| 443 | printf FILE "\t%s %s + %d\n", $type, $ref_func, $cur_offset - $offset;
|
| 444 | }
|
| 445 | }
|
| 446 |
|
| 447 | |
| 448 | |
| 449 | |
| 450 | open(IN, "LC_ALL=C $objdump -hdr $inputfile|") || die "error running $objdump";
|
| 451 |
|
| 452 | my $text;
|
| 453 |
|
| 454 |
|
| 455 | |
| 456 | my $read_headers = 1;
|
| 457 |
|
| 458 | while (<IN>) {
|
| 459 |
|
| 460 | if ($read_headers && /$mcount_section/) {
|
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | print STDERR "ERROR: $mcount_section already in $inputfile\n" .
|
| 469 | "\tThis may be an indication that your build is corrupted.\n" .
|
| 470 | "\tDelete $inputfile and try again. If the same object file\n" .
|
| 471 | "\tstill causes an issue, then disable CONFIG_DYNAMIC_FTRACE.\n";
|
| 472 | exit(-1);
|
| 473 | }
|
| 474 |
|
| 475 | |
| 476 | if (/$section_regex/) {
|
| 477 | $read_headers = 0;
|
| 478 |
|
| 479 | |
| 480 | $read_function = defined($text_sections{$1});
|
| 481 | if (!$read_function) {
|
| 482 | foreach my $prefix (keys %text_section_prefixes) {
|
| 483 | if (substr($1, 0, length $prefix) eq $prefix) {
|
| 484 | $read_function = 1;
|
| 485 | last;
|
| 486 | }
|
| 487 | }
|
| 488 | }
|
| 489 | |
| 490 | update_funcs();
|
| 491 |
|
| 492 | |
| 493 | $text_found = 0;
|
| 494 | undef($ref_func);
|
| 495 | undef(@offsets);
|
| 496 |
|
| 497 | |
| 498 | } elsif ($read_function && /$function_regex/) {
|
| 499 | $text_found = 1;
|
| 500 | $text = $2;
|
| 501 |
|
| 502 | |
| 503 | |
| 504 | |
| 505 | if (!defined($locals{$text}) && !defined($weak{$text})) {
|
| 506 | $ref_func = $text;
|
| 507 | $read_function = 0;
|
| 508 | $offset = hex $1;
|
| 509 | } else {
|
| 510 | |
| 511 | if (!defined($ref_func) && !defined($weak{$text}) &&
|
| 512 | |
| 513 | |
| 514 | $text !~ /^\.L/) {
|
| 515 | $ref_func = $text;
|
| 516 | $offset = hex $1;
|
| 517 | }
|
| 518 | }
|
| 519 | }
|
| 520 | |
| 521 | if ($text_found && /$mcount_regex/) {
|
| 522 | push(@offsets, (hex $1) + $mcount_adjust);
|
| 523 | }
|
| 524 | }
|
| 525 |
|
| 526 | |
| 527 | update_funcs();
|
| 528 |
|
| 529 | |
| 530 | if (!$opened) {
|
| 531 | exit(0);
|
| 532 | }
|
| 533 |
|
| 534 | close(FILE);
|
| 535 |
|
| 536 | |
| 537 | |
| 538 | |
| 539 | `$cc -o $mcount_o -c $mcount_s`;
|
| 540 |
|
| 541 | my @converts = keys %convert;
|
| 542 |
|
| 543 | |
| 544 | |
| 545 | |
| 546 | if ($#converts >= 0) {
|
| 547 | my $globallist = "";
|
| 548 | my $locallist = "";
|
| 549 |
|
| 550 | foreach my $con (@converts) {
|
| 551 | $globallist .= " --globalize-symbol $con";
|
| 552 | $locallist .= " --localize-symbol $con";
|
| 553 | }
|
| 554 |
|
| 555 | my $globalobj = $dirname . "/.tmp_gl_" . $filename;
|
| 556 | my $globalmix = $dirname . "/.tmp_mx_" . $filename;
|
| 557 |
|
| 558 | |
| 559 | |
| 560 | |
| 561 | `$objcopy $globallist $inputfile $globalobj`;
|
| 562 |
|
| 563 | |
| 564 | |
| 565 | |
| 566 | `$ld -r $globalobj $mcount_o -o $globalmix`;
|
| 567 |
|
| 568 | |
| 569 | |
| 570 | |
| 571 | `$objcopy $locallist $globalmix $inputfile`;
|
| 572 |
|
| 573 | |
| 574 | `$rm $globalobj $globalmix`;
|
| 575 |
|
| 576 | } else {
|
| 577 |
|
| 578 | my $mix = $dirname . "/.tmp_mx_" . $filename;
|
| 579 |
|
| 580 | |
| 581 | |
| 582 | |
| 583 | `$ld -r $inputfile $mcount_o -o $mix`;
|
| 584 |
|
| 585 | |
| 586 | |
| 587 | |
| 588 | `$mv $mix $inputfile`;
|
| 589 | }
|
| 590 |
|
| 591 | |
| 592 | `$rm $mcount_o $mcount_s`;
|
| 593 |
|
| 594 | exit(0);
|
| 595 |
|
| 596 | |