xref: /openbmc/linux/scripts/recordmcount.pl (revision 42e007d0)
1#!/usr/bin/perl -w
2# (c) 2008, Steven Rostedt <srostedt@redhat.com>
3# Licensed under the terms of the GNU GPL License version 2
4#
5# recordmcount.pl - makes a section called __mcount_loc that holds
6#                   all the offsets to the calls to mcount.
7#
8#
9# What we want to end up with is a section in vmlinux called
10# __mcount_loc that contains a list of pointers to all the
11# call sites in the kernel that call mcount. Later on boot up, the kernel
12# will read this list, save the locations and turn them into nops.
13# When tracing or profiling is later enabled, these locations will then
14# be converted back to pointers to some function.
15#
16# This is no easy feat. This script is called just after the original
17# object is compiled and before it is linked.
18#
19# The references to the call sites are offsets from the section of text
20# that the call site is in. Hence, all functions in a section that
21# has a call site to mcount, will have the offset from the beginning of
22# the section and not the beginning of the function.
23#
24# The trick is to find a way to record the beginning of the section.
25# The way we do this is to look at the first function in the section
26# which will also be the location of that section after final link.
27# e.g.
28#
29#  .section ".text.sched"
30#  .globl my_func
31#  my_func:
32#        [...]
33#        call mcount  (offset: 0x5)
34#        [...]
35#        ret
36#  other_func:
37#        [...]
38#        call mcount (offset: 0x1b)
39#        [...]
40#
41# Both relocation offsets for the mcounts in the above example will be
42# offset from .text.sched. If we make another file called tmp.s with:
43#
44#  .section __mcount_loc
45#  .quad  my_func + 0x5
46#  .quad  my_func + 0x1b
47#
48# We can then compile this tmp.s into tmp.o, and link it to the original
49# object.
50#
51# But this gets hard if my_func is not globl (a static function).
52# In such a case we have:
53#
54#  .section ".text.sched"
55#  my_func:
56#        [...]
57#        call mcount  (offset: 0x5)
58#        [...]
59#        ret
60#  .globl my_func
61#  other_func:
62#        [...]
63#        call mcount (offset: 0x1b)
64#        [...]
65#
66# If we make the tmp.s the same as above, when we link together with
67# the original object, we will end up with two symbols for my_func:
68# one local, one global.  After final compile, we will end up with
69# an undefined reference to my_func.
70#
71# Since local objects can reference local variables, we need to find
72# a way to make tmp.o reference the local objects of the original object
73# file after it is linked together. To do this, we convert the my_func
74# into a global symbol before linking tmp.o. Then after we link tmp.o
75# we will only have a single symbol for my_func that is global.
76# We can convert my_func back into a local symbol and we are done.
77#
78# Here are the steps we take:
79#
80# 1) Record all the local symbols by using 'nm'
81# 2) Use objdump to find all the call site offsets and sections for
82#    mcount.
83# 3) Compile the list into its own object.
84# 4) Do we have to deal with local functions? If not, go to step 8.
85# 5) Make an object that converts these local functions to global symbols
86#    with objcopy.
87# 6) Link together this new object with the list object.
88# 7) Convert the local functions back to local symbols and rename
89#    the result as the original object.
90#    End.
91# 8) Link the object with the list object.
92# 9) Move the result back to the original object.
93#    End.
94#
95
96use strict;
97
98my $P = $0;
99$P =~ s@.*/@@g;
100
101my $V = '0.1';
102
103if ($#ARGV < 6) {
104	print "usage: $P arch objdump objcopy cc ld nm rm mv inputfile\n";
105	print "version: $V\n";
106	exit(1);
107}
108
109my ($arch, $bits, $objdump, $objcopy, $cc,
110    $ld, $nm, $rm, $mv, $inputfile) = @ARGV;
111
112# Acceptable sections to record.
113my %text_sections = (
114     ".text" => 1,
115);
116
117$objdump = "objdump" if ((length $objdump) == 0);
118$objcopy = "objcopy" if ((length $objcopy) == 0);
119$cc = "gcc" if ((length $cc) == 0);
120$ld = "ld" if ((length $ld) == 0);
121$nm = "nm" if ((length $nm) == 0);
122$rm = "rm" if ((length $rm) == 0);
123$mv = "mv" if ((length $mv) == 0);
124
125#print STDERR "running: $P '$arch' '$objdump' '$objcopy' '$cc' '$ld' " .
126#    "'$nm' '$rm' '$mv' '$inputfile'\n";
127
128my %locals;		# List of local (static) functions
129my %weak;		# List of weak functions
130my %convert;		# List of local functions used that needs conversion
131
132my $type;
133my $nm_regex;		# Find the local functions (return function)
134my $section_regex;	# Find the start of a section
135my $function_regex;	# Find the name of a function
136			#    (return offset and func name)
137my $mcount_regex;	# Find the call site to mcount (return offset)
138my $alignment;         # The .align value to use for $mcount_section
139
140if ($arch eq "x86") {
141    if ($bits == 64) {
142	$arch = "x86_64";
143    } else {
144	$arch = "i386";
145    }
146}
147
148if ($arch eq "x86_64") {
149    $nm_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\S+)";
150    $section_regex = "Disassembly of section\\s+(\\S+):";
151    $function_regex = "^([0-9a-fA-F]+)\\s+<(.*?)>:";
152    $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\smcount([+-]0x[0-9a-zA-Z]+)?\$";
153    $type = ".quad";
154    $alignment = 8;
155
156    # force flags for this arch
157    $ld .= " -m elf_x86_64";
158    $objdump .= " -M x86-64";
159    $objcopy .= " -O elf64-x86-64";
160    $cc .= " -m64";
161
162} elsif ($arch eq "i386") {
163    $nm_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\S+)";
164    $section_regex = "Disassembly of section\\s+(\\S+):";
165    $function_regex = "^([0-9a-fA-F]+)\\s+<(.*?)>:";
166    $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\smcount\$";
167    $type = ".long";
168    $alignment = 4;
169
170    # force flags for this arch
171    $ld .= " -m elf_i386";
172    $objdump .= " -M i386";
173    $objcopy .= " -O elf32-i386";
174    $cc .= " -m32";
175
176} elsif ($arch eq "sh") {
177    $nm_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\S+)";
178    $section_regex = "Disassembly of section\\s+(\\S+):";
179    $function_regex = "^([0-9a-fA-F]+)\\s+<(.*?)>:";
180    $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\smcount\$";
181    $type = ".long";
182
183    # force flags for this arch
184    $ld .= " -m shlelf_linux";
185    $objcopy .= " -O elf32-sh-linux";
186    $cc .= " -m32";
187
188} elsif ($arch eq "powerpc") {
189    $nm_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\.?\\S+)";
190    $section_regex = "Disassembly of section\\s+(\\S+):";
191    $function_regex = "^([0-9a-fA-F]+)\\s+<(\\.?.*?)>:";
192    $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s\\.?_mcount\$";
193    if ($bits == 64) {
194	$type = ".quad";
195    } else {
196	$type = ".long";
197    }
198
199} else {
200    die "Arch $arch is not supported with CONFIG_FTRACE_MCOUNT_RECORD";
201}
202
203my $text_found = 0;
204my $read_function = 0;
205my $opened = 0;
206my $mcount_section = "__mcount_loc";
207
208my $dirname;
209my $filename;
210my $prefix;
211my $ext;
212
213if ($inputfile =~ m,^(.*)/([^/]*)$,) {
214    $dirname = $1;
215    $filename = $2;
216} else {
217    $dirname = ".";
218    $filename = $inputfile;
219}
220
221if ($filename =~ m,^(.*)(\.\S),) {
222    $prefix = $1;
223    $ext = $2;
224} else {
225    $prefix = $filename;
226    $ext = "";
227}
228
229my $mcount_s = $dirname . "/.tmp_mc_" . $prefix . ".s";
230my $mcount_o = $dirname . "/.tmp_mc_" . $prefix . ".o";
231
232#
233# --globalize-symbols came out in 2.17, we must test the version
234# of objcopy, and if it is less than 2.17, then we can not
235# record local functions.
236my $use_locals = 01;
237my $local_warn_once = 0;
238my $found_version = 0;
239
240open (IN, "$objcopy --version |") || die "error running $objcopy";
241while (<IN>) {
242    if (/objcopy.*\s(\d+)\.(\d+)/) {
243	my $major = $1;
244	my $minor = $2;
245
246	$found_version = 1;
247	if ($major < 2 ||
248	    ($major == 2 && $minor < 17)) {
249	    $use_locals = 0;
250	}
251	last;
252    }
253}
254close (IN);
255
256if (!$found_version) {
257    print STDERR "WARNING: could not find objcopy version.\n" .
258	"\tDisabling local function references.\n";
259}
260
261
262#
263# Step 1: find all the local (static functions) and weak symbols.
264#        't' is local, 'w/W' is weak (we never use a weak function)
265#
266open (IN, "$nm $inputfile|") || die "error running $nm";
267while (<IN>) {
268    if (/$nm_regex/) {
269	$locals{$1} = 1;
270    } elsif (/^[0-9a-fA-F]+\s+([wW])\s+(\S+)/) {
271	$weak{$2} = $1;
272    }
273}
274close(IN);
275
276my @offsets;		# Array of offsets of mcount callers
277my $ref_func;		# reference function to use for offsets
278my $offset = 0;		# offset of ref_func to section beginning
279
280##
281# update_funcs - print out the current mcount callers
282#
283#  Go through the list of offsets to callers and write them to
284#  the output file in a format that can be read by an assembler.
285#
286sub update_funcs
287{
288    return if ($#offsets < 0);
289
290    defined($ref_func) || die "No function to reference";
291
292    # A section only had a weak function, to represent it.
293    # Unfortunately, a weak function may be overwritten by another
294    # function of the same name, making all these offsets incorrect.
295    # To be safe, we simply print a warning and bail.
296    if (defined $weak{$ref_func}) {
297	print STDERR
298	    "$inputfile: WARNING: referencing weak function" .
299	    " $ref_func for mcount\n";
300	return;
301    }
302
303    # is this function static? If so, note this fact.
304    if (defined $locals{$ref_func}) {
305
306	# only use locals if objcopy supports globalize-symbols
307	if (!$use_locals) {
308	    return;
309	}
310	$convert{$ref_func} = 1;
311    }
312
313    # Loop through all the mcount caller offsets and print a reference
314    # to the caller based from the ref_func.
315    for (my $i=0; $i <= $#offsets; $i++) {
316	if (!$opened) {
317	    open(FILE, ">$mcount_s") || die "can't create $mcount_s\n";
318	    $opened = 1;
319	    print FILE "\t.section $mcount_section,\"a\",\@progbits\n";
320	    print FILE "\t.align $alignment\n" if (defined($alignment));
321	}
322	printf FILE "\t%s %s + %d\n", $type, $ref_func, $offsets[$i] - $offset;
323    }
324}
325
326#
327# Step 2: find the sections and mcount call sites
328#
329open(IN, "$objdump -dr $inputfile|") || die "error running $objdump";
330
331my $text;
332
333while (<IN>) {
334    # is it a section?
335    if (/$section_regex/) {
336
337	# Only record text sections that we know are safe
338	if (defined($text_sections{$1})) {
339	    $read_function = 1;
340	} else {
341	    $read_function = 0;
342	}
343	# print out any recorded offsets
344	update_funcs() if ($text_found);
345
346	# reset all markers and arrays
347	$text_found = 0;
348	undef($ref_func);
349	undef(@offsets);
350
351    # section found, now is this a start of a function?
352    } elsif ($read_function && /$function_regex/) {
353	$text_found = 1;
354	$offset = hex $1;
355	$text = $2;
356
357	# if this is either a local function or a weak function
358	# keep looking for functions that are global that
359	# we can use safely.
360	if (!defined($locals{$text}) && !defined($weak{$text})) {
361	    $ref_func = $text;
362	    $read_function = 0;
363	} else {
364	    # if we already have a function, and this is weak, skip it
365	    if (!defined($ref_func) || !defined($weak{$text})) {
366		$ref_func = $text;
367	    }
368	}
369    }
370
371    # is this a call site to mcount? If so, record it to print later
372    if ($text_found && /$mcount_regex/) {
373	$offsets[$#offsets + 1] = hex $1;
374    }
375}
376
377# dump out anymore offsets that may have been found
378update_funcs() if ($text_found);
379
380# If we did not find any mcount callers, we are done (do nothing).
381if (!$opened) {
382    exit(0);
383}
384
385close(FILE);
386
387#
388# Step 3: Compile the file that holds the list of call sites to mcount.
389#
390`$cc -o $mcount_o -c $mcount_s`;
391
392my @converts = keys %convert;
393
394#
395# Step 4: Do we have sections that started with local functions?
396#
397if ($#converts >= 0) {
398    my $globallist = "";
399    my $locallist = "";
400
401    foreach my $con (@converts) {
402	$globallist .= " --globalize-symbol $con";
403	$locallist .= " --localize-symbol $con";
404    }
405
406    my $globalobj = $dirname . "/.tmp_gl_" . $filename;
407    my $globalmix = $dirname . "/.tmp_mx_" . $filename;
408
409    #
410    # Step 5: set up each local function as a global
411    #
412    `$objcopy $globallist $inputfile $globalobj`;
413
414    #
415    # Step 6: Link the global version to our list.
416    #
417    `$ld -r $globalobj $mcount_o -o $globalmix`;
418
419    #
420    # Step 7: Convert the local functions back into local symbols
421    #
422    `$objcopy $locallist $globalmix $inputfile`;
423
424    # Remove the temp files
425    `$rm $globalobj $globalmix`;
426
427} else {
428
429    my $mix = $dirname . "/.tmp_mx_" . $filename;
430
431    #
432    # Step 8: Link the object with our list of call sites object.
433    #
434    `$ld -r $inputfile $mcount_o -o $mix`;
435
436    #
437    # Step 9: Move the result back to the original object.
438    #
439    `$mv $mix $inputfile`;
440}
441
442# Clean up the temp files
443`$rm $mcount_o $mcount_s`;
444
445exit(0);
446