2009-12-04 13:55:55

by Jiri Olsa

[permalink] [raw]
Subject: [RFC PATCH] tracing - moving some of recordmcount functionality to binary

hi,

I noticed on some discussion, that it might speedup the FTRACE build process,
if some of the 'scripts/recordmcount.pl' functionality was done in binary,
instead of in the script using nm/.objdump output.


Before I dive into details, to sum it up:
- i wrote tool which took over some of the 'recordmcount.pl' stuff
- with 'allyesconfig' the build was faster about 2 minutes

I'm not sure wether it's worth to continue in this effort,
but I at least provide some details to have some enclosure.. :)


recordmcount does very roughly this:
(for details plz see the doc in the script itself)

- is executed on each object after it is compiled by gcc

1) get object's symbols using 'nm' tool
2) run 'objdump -hdr' to get object's disassembly mixed with relocations
3) parse (2) output looking for 'mcount' relocations and reference symbols
4) based on (3) produce assembly code with __mcount_loc section
5) compile the (4) assembly
6) link the (5) object with the original object + optional local->global->local game :)


My first idea was to cover all these steps in binary, but as I studied objcopy/objdump,
this turned up not to be the best idea.

The reason I saw was that any time objcopy wanted to change anything in object file,
it manages this by copying the whole object to the new one (section by section...)
changing the requested stuff (eg make global symbol to local) as it goes.

As there were tons of stuff to check/copy/manage I dont thing this was suitable.


So I ended up with only some steps. I took steps: 1), 2), 3) and 4).
The mctool I created locates all the mcount relocations and provides
the assemble output.


I investigated 'libbfd' and 'libelf' and endup with 'libbfd'. For the tool I wrote
I think it does not matter, since both have reasonable interface for getting info
out of the objects. It might be different for actually changing the object.


My opinion is that having this part in binary is better than having
it parsed out of objdump. I haven't check the tool on other archs,
bu I assume it should work properly.


As I said above I did not get much build speedup, so unless I get some
possitive feedback I consider this as a deadend for me :)


The attached patch is not by all means final in any way,
it's just working... tested on x86_64 :)


thanks for any feedback,
jirka


---
diff --git a/scripts/Makefile b/scripts/Makefile
index 842dbc2..ddda6cd 100644
--- a/scripts/Makefile
+++ b/scripts/Makefile
@@ -6,11 +6,13 @@
# pnmttologo: Convert pnm files to logo files
# conmakehash: Create chartable
# conmakehash: Create arrays for initializing the kernel console tables
+# mctool: Create mcount section for tracing

hostprogs-$(CONFIG_KALLSYMS) += kallsyms
hostprogs-$(CONFIG_LOGO) += pnmtologo
hostprogs-$(CONFIG_VT) += conmakehash
hostprogs-$(CONFIG_IKCONFIG) += bin2c
+hostprogs-$(CONFIG_FTRACE) += mctool

always := $(hostprogs-y) $(hostprogs-m)

@@ -22,5 +24,7 @@ subdir-y += mod
subdir-$(CONFIG_SECURITY_SELINUX) += selinux
subdir-$(CONFIG_DTC) += dtc

+HOSTLOADLIBES_mctool:= -lbfd
+
# Let clean descend into subdirs
subdir- += basic kconfig package selinux
diff --git a/scripts/Makefile.build b/scripts/Makefile.build
index 341b589..110b3a6 100644
--- a/scripts/Makefile.build
+++ b/scripts/Makefile.build
@@ -208,7 +208,7 @@ endif
ifdef CONFIG_FTRACE_MCOUNT_RECORD
cmd_record_mcount = set -e ; perl $(srctree)/scripts/recordmcount.pl "$(ARCH)" \
"$(if $(CONFIG_64BIT),64,32)" \
- "$(OBJDUMP)" "$(OBJCOPY)" "$(CC)" "$(LD)" "$(NM)" "$(RM)" "$(MV)" \
+ "$(srctree)/scripts/mctool" "$(OBJCOPY)" "$(CC)" "$(LD)" "$(RM)" "$(MV)" \
"$(if $(part-of-module),1,0)" "$(@)";
endif

diff --git a/scripts/mctool.c b/scripts/mctool.c
new file mode 100644
index 0000000..3c0710d
--- /dev/null
+++ b/scripts/mctool.c
@@ -0,0 +1,470 @@
+
+#include <stdio.h>
+#include <bfd.h>
+#include <getopt.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <search.h>
+#include <string.h>
+
+static char *filename_in = NULL;
+static char *filename_out = NULL;
+static char *filename_convert = NULL;
+static char *data_type = NULL;
+static char *section_type = NULL;
+static char *section_align = NULL;
+static char *section_name = NULL;
+static int verbose = 0;
+
+static bfd *bfd_in = NULL;
+static FILE *file_out = NULL;
+static asymbol **symbol_table = NULL;
+
+#define MCTOOL_VERSION "0.1"
+
+#define PRINT(fmt, args...) \
+do { \
+ char lpbuf[1024]; \
+ sprintf(lpbuf, "[%s:%05d] %s", \
+ __FUNCTION__, \
+ __LINE__, \
+ fmt); \
+ printf(lpbuf, ## args); \
+ fflush(NULL); \
+} while(0)
+
+#define PRINT_VERBOSE(cond, fmt, args...) \
+do { \
+ if (cond > verbose) \
+ break; \
+ PRINT(fmt, ## args); \
+} while(0)
+
+
+struct section_st {
+ char *name;
+ asymbol *ref_sym;
+ int used;
+};
+
+static struct section_st safe_sections[] = {
+ { .name = ".text" },
+ { .name = ".sched.text" },
+ { .name = ".spinlock.text" },
+ { .name = ".irqentry.text" },
+ { .name = ".text.unlikely" },
+};
+
+#define SAFE_SECTIONS_COUNT (sizeof(safe_sections) / sizeof(struct section_st))
+
+static struct section_st* section_find(const char *name)
+{
+ int i;
+
+ for(i = 0; i < SAFE_SECTIONS_COUNT; i++) {
+ if (!strcmp(name, safe_sections[i].name))
+ return &safe_sections[i];
+ }
+
+ return NULL;
+}
+
+static void usage(void)
+{
+ printf("usage: mctool [-vVhtTacon] <object>\n");
+ _exit(1);
+}
+
+static int process_args(int argc, char **argv)
+{
+ while (1) {
+ int c;
+ int option_index = 0;
+ static struct option long_options[] = {
+ {"output", required_argument, 0, 'o'},
+ {"data-type", required_argument, 0, 't'},
+ {"section-type", required_argument, 0, 'T'},
+ {"section-align", required_argument, 0, 'a'},
+ {"section-name", required_argument, 0, 'n'},
+ {"convert", required_argument, 0, 'c'},
+ {"verbose", no_argument, 0, 'v'},
+ {"version", no_argument, 0, 'V'},
+ {"help", no_argument, 0, 'h'},
+ {0, 0, 0, 0}
+ };
+
+ c = getopt_long(argc, argv, "+vVht:T:a:c:o:n:",
+ long_options, &option_index);
+
+ if (c == -1)
+ break;
+
+ switch (c) {
+ case 'v':
+ verbose++;
+ break;
+
+ case 'V':
+ printf("mctool " MCTOOL_VERSION "\n");
+ _exit(1);
+
+ case 'o':
+ filename_out = optarg;
+ break;
+
+ case 'T':
+ section_type = optarg;
+ break;
+
+ case 't':
+ data_type = optarg;
+ break;
+
+ case 'a':
+ section_align = optarg;
+ break;
+
+ case 'n':
+ section_name = optarg;
+ break;
+
+ case 'c':
+ filename_convert = optarg;
+ break;
+
+ case 'h':
+ usage();
+ break;
+ };
+ };
+
+ if (optind < argc) {
+ filename_in = argv[optind++];
+ } else {
+ printf("no input file specified\n");
+ return 1;
+ }
+
+ if (optind < argc) {
+ printf("only one input file supported\n");
+ return 1;
+ }
+
+ if (!section_type ||
+ !data_type ||
+ !section_name ||
+ !filename_convert)
+ return 1;
+
+ return 0;
+}
+
+static void bfd_nonfatal(const char *string)
+{
+ const char *errmsg = bfd_errmsg(bfd_get_error());
+
+ if (string)
+ fprintf(stderr, "%s: %s\n", string, errmsg);
+ else
+ fprintf(stderr, "%s\n", errmsg);
+}
+
+static void bfd_fatal(const char *string)
+{
+ bfd_nonfatal(string);
+ _exit(1);
+}
+
+static int file_in_open(char *filename)
+{
+ bfd_in = bfd_openr(filename, "default");
+ if (bfd_in == NULL) {
+ bfd_nonfatal(filename);
+ return 1;
+ }
+
+ if (!bfd_check_format(bfd_in, bfd_object)) {
+ fprintf(stderr, "error: expecting object file\n");
+ return 1;
+ }
+
+ return 0;
+}
+
+static int file_in_close(void)
+{
+ if (!bfd_close(bfd_in))
+ bfd_fatal(filename_in);
+
+ return 0;
+}
+
+static int file_out_open(void)
+{
+ if (!filename_out) {
+ file_out = stdout;
+ return 0;
+ }
+
+ file_out = fopen(filename_out, "w+");
+ if (!file_out) {
+ perror("fopen failed");
+ return 1;
+ }
+
+ return 0;
+}
+
+static int file_out_close(void)
+{
+ if (file_out)
+ fclose(file_out);
+
+ return 0;
+}
+
+static int get_symbols(void)
+{
+ long storage_needed;
+ long number_of_symbols;
+ long i;
+
+ storage_needed = bfd_get_symtab_upper_bound(bfd_in);
+ if (storage_needed < 0) {
+ bfd_nonfatal("bfd_get_symtab_upper_bound failed");
+ return 1;
+ }
+
+ if (storage_needed == 0) {
+ fprintf(stderr, "no symbols...\n");
+ return 1;
+ }
+
+ symbol_table = malloc(storage_needed);
+ if (!symbol_table) {
+ perror("malloc failed");
+ return 1;
+ }
+
+ number_of_symbols = bfd_canonicalize_symtab(bfd_in, symbol_table);
+ if (number_of_symbols < 0) {
+ bfd_nonfatal("bfd_canonicalize_symtab failed");
+ return 1;
+ }
+
+ PRINT_VERBOSE(1, "got %d symbols\n", number_of_symbols);
+
+ for (i = 0; i < number_of_symbols; i++) {
+
+ const char *section = NULL;
+ struct section_st *ref_sec;
+
+ asymbol *sym = symbol_table[i];
+ flagword sym_flags = sym->flags;
+
+
+ /* no country for weak symbols... */
+ if (sym_flags & BSF_WEAK)
+ continue;
+
+ /* and function club only... */
+ if (!(sym_flags & BSF_FUNCTION))
+ continue;
+
+ if (sym->section)
+ section = symbol_table[i]->section->name;
+
+ ref_sec = section_find(section);
+ if (!ref_sec)
+ continue;
+
+
+ PRINT_VERBOSE(1, "trying func %s %x\n", sym->name, sym->value);
+
+ if (ref_sec->ref_sym) {
+
+ flagword ref_flags = ref_sec->ref_sym->flags;
+ int current_is_lower = (ref_sec->ref_sym->value < sym->value);
+
+ /* we already have lower global symbol */
+ if ((ref_flags & BSF_GLOBAL) && current_is_lower)
+ continue;
+
+ /* we already have lower local symbol */
+ if ((sym_flags & BSF_LOCAL) && current_is_lower)
+ continue;
+ }
+
+ PRINT_VERBOSE(1, "setting func %s %x\n", sym->name, sym->value);
+ ref_sec->ref_sym = sym;
+ }
+
+ for(i = 0; i < SAFE_SECTIONS_COUNT; i++) {
+ struct section_st *sec = &safe_sections[i];
+
+ PRINT_VERBOSE(1, "%s -> %s\n", sec->name,
+ sec->ref_sym ? sec->ref_sym->name : "undef");
+ }
+
+ return 0;
+}
+
+static void dump_relocs_in_section(bfd *abfd, asection *section, void *dummy)
+{
+ arelent **relpp;
+ long relcount;
+ long relsize;
+ arelent **p;
+ int display_head = 1;
+ struct section_st *sec;
+
+ sec = section_find(section->name);
+ if (!sec)
+ return;
+
+ PRINT_VERBOSE(1, "got %s\n", section->name);
+
+ if (!sec->ref_sym)
+ return;
+
+ relsize = bfd_get_reloc_upper_bound(abfd, section);
+ if (relsize < 0)
+ bfd_fatal("bfd_get_reloc_upper_bound failed");
+
+ if (relsize == 0) {
+ PRINT_VERBOSE(1, "no relocations for section %s\n",
+ section->name);
+ return;
+ }
+
+ relpp = (arelent **) malloc(relsize);
+ if (!relpp) {
+ perror("malloc failed");
+ return;
+ }
+
+ relcount = bfd_canonicalize_reloc(abfd, section, relpp, symbol_table);
+ if (relcount < 0)
+ bfd_fatal("bfd_canonicalize_reloc failed");
+ else if (relcount == 0)
+ return;
+
+
+ for (p = relpp; relcount && *p != NULL; p++, relcount--) {
+
+ const char *sym_name;
+ arelent *q = *p;
+
+ if (q->sym_ptr_ptr && *q->sym_ptr_ptr) {
+ sym_name = (*(q->sym_ptr_ptr))->name;
+ } else {
+ sym_name = NULL;
+ }
+
+ if (strcmp("mcount", sym_name))
+ continue;
+
+ PRINT_VERBOSE(1, "got mcount reloc %x\n", q->address);
+
+ if (display_head) {
+ if (!file_out && file_out_open())
+ return;
+
+ fprintf(file_out, "\t.section %s,\"a\",%s\n",
+ section_name, section_type);
+
+ if (section_align)
+ fprintf(file_out, "\t.align %s\n", section_align);
+
+ sec->used = 1;
+ display_head = 0;
+ }
+
+ fprintf(file_out, "\t%s %s + %ld\n",
+ data_type, sec->ref_sym->name,
+ q->address - sec->ref_sym->value);
+ }
+
+ free(relpp);
+}
+
+static int process_relocs(void)
+{
+ bfd_map_over_sections(bfd_in, dump_relocs_in_section, NULL);
+ return 0;
+}
+
+static int process_convert(void)
+{
+ FILE *file_convert = NULL;
+ int i;
+
+ for(i = 0; i < SAFE_SECTIONS_COUNT; i++) {
+ struct section_st *sec = &safe_sections[i];
+ flagword ref_flags;
+
+ if (!sec->used)
+ continue;
+
+ if (!sec->ref_sym) {
+ fprintf(stderr, "section is used and no ref symbol!\n");
+ return 1;
+ }
+
+ ref_flags = sec->ref_sym->flags;
+
+ /* convert only local symbols */
+ if (ref_flags & BSF_GLOBAL)
+ continue;
+
+ if (!file_convert) {
+ file_convert = fopen(filename_convert, "w");
+ if (!file_convert) {
+ perror("fopen failed");
+ return 1;
+ }
+ }
+
+ fprintf(file_convert, "%s ", sec->ref_sym->name);
+ }
+
+ if (file_convert)
+ fclose(file_convert);
+
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ int ret = -1;
+
+ if (process_args(argc, argv))
+ usage();
+
+ bfd_init();
+
+ if (filename_out)
+ unlink(filename_out);
+
+ if (file_in_open(filename_in))
+ return 1;
+
+ do {
+ if (get_symbols())
+ break;
+
+ if (process_relocs())
+ break;
+
+ if (process_convert())
+ break;
+
+ ret = 0;
+
+ } while(0);
+
+ file_in_close();
+ file_out_close();
+
+ return ret;
+}
diff --git a/scripts/recordmcount.pl b/scripts/recordmcount.pl
index f0d1445..05368ac 100755
--- a/scripts/recordmcount.pl
+++ b/scripts/recordmcount.pl
@@ -111,53 +111,35 @@ use strict;
my $P = $0;
$P =~ s@.*/@@g;

-my $V = '0.1';
+my $V = '0.2';

-if ($#ARGV != 10) {
- print "usage: $P arch bits objdump objcopy cc ld nm rm mv is_module inputfile\n";
+if ($#ARGV != 9) {
+ print "usage: $P arch bits mctool objcopy cc ld rm mv is_module inputfile\n";
print "version: $V\n";
exit(1);
}

-my ($arch, $bits, $objdump, $objcopy, $cc,
- $ld, $nm, $rm, $mv, $is_module, $inputfile) = @ARGV;
+my ($arch, $bits, $mctool, $objcopy, $cc,
+ $ld, $rm, $mv, $is_module, $inputfile) = @ARGV;

# This file refers to mcount and shouldn't be ftraced, so lets' ignore it
if ($inputfile =~ m,kernel/trace/ftrace\.o$,) {
exit(0);
}

-# Acceptable sections to record.
-my %text_sections = (
- ".text" => 1,
- ".sched.text" => 1,
- ".spinlock.text" => 1,
- ".irqentry.text" => 1,
- ".text.unlikely" => 1,
-);
-
-$objdump = "objdump" if ((length $objdump) == 0);
+# TODO check for valid mctool settings
$objcopy = "objcopy" if ((length $objcopy) == 0);
$cc = "gcc" if ((length $cc) == 0);
$ld = "ld" if ((length $ld) == 0);
-$nm = "nm" if ((length $nm) == 0);
$rm = "rm" if ((length $rm) == 0);
$mv = "mv" if ((length $mv) == 0);

#print STDERR "running: $P '$arch' '$objdump' '$objcopy' '$cc' '$ld' " .
# "'$nm' '$rm' '$mv' '$inputfile'\n";

-my %locals; # List of local (static) functions
-my %weak; # List of weak functions
my %convert; # List of local functions used that needs conversion

my $type;
-my $local_regex; # Match a local function (return function)
-my $weak_regex; # Match a weak function (return function)
-my $section_regex; # Find the start of a section
-my $function_regex; # Find the name of a function
- # (return offset and func name)
-my $mcount_regex; # Find the call site to mcount (return offset)
my $alignment; # The .align value to use for $mcount_section
my $section_type; # Section header plus possible alignment command
my $can_use_local = 0; # If we can use local function references
@@ -206,22 +188,15 @@ if ($arch eq "x86") {
# We base the defaults off of i386, the other archs may
# feel free to change them in the below if statements.
#
-$local_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\S+)";
-$weak_regex = "^[0-9a-fA-F]+\\s+([wW])\\s+(\\S+)";
-$section_regex = "Disassembly of section\\s+(\\S+):";
-$function_regex = "^([0-9a-fA-F]+)\\s+<(.*?)>:";
-$mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\smcount\$";
$section_type = '@progbits';
$type = ".long";

if ($arch eq "x86_64") {
- $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\smcount([+-]0x[0-9a-zA-Z]+)?\$";
$type = ".quad";
$alignment = 8;

# force flags for this arch
$ld .= " -m elf_x86_64";
- $objdump .= " -M x86-64";
$objcopy .= " -O elf64-x86-64";
$cc .= " -m64";

@@ -230,18 +205,15 @@ if ($arch eq "x86_64") {

# force flags for this arch
$ld .= " -m elf_i386";
- $objdump .= " -M i386";
$objcopy .= " -O elf32-i386";
$cc .= " -m32";

} elsif ($arch eq "s390" && $bits == 32) {
- $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_390_32\\s+_mcount\$";
$alignment = 4;
$ld .= " -m elf_s390";
$cc .= " -m31";

} elsif ($arch eq "s390" && $bits == 64) {
- $mcount_regex = "^\\s*([0-9a-fA-F]+):\\s*R_390_(PC|PLT)32DBL\\s+_mcount\\+0x2\$";
$alignment = 8;
$type = ".quad";
$ld .= " -m elf64_s390";
@@ -256,10 +228,6 @@ if ($arch eq "x86_64") {
$cc .= " -m32";

} elsif ($arch eq "powerpc") {
- $local_regex = "^[0-9a-fA-F]+\\s+t\\s+(\\.?\\S+)";
- $function_regex = "^([0-9a-fA-F]+)\\s+<(\\.?.*?)>:";
- $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s\\.?_mcount\$";
-
if ($bits == 64) {
$type = ".quad";
}
@@ -269,27 +237,12 @@ if ($arch eq "x86_64") {
$section_type = '%progbits';

} elsif ($arch eq "ia64") {
- $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
$type = "data8";

if ($is_module eq "0") {
$cc .= " -mconstant-gp";
}
} elsif ($arch eq "sparc64") {
- # In the objdump output there are giblets like:
- # 0000000000000000 <igmp_net_exit-0x18>:
- # As there's some data blobs that get emitted into the
- # text section before the first instructions and the first
- # real symbols. We don't want to match that, so to combat
- # this we use '\w' so we'll match just plain symbol names,
- # and not those that also include hex offsets inside of the
- # '<>' brackets. Actually the generic function_regex setting
- # could safely use this too.
- $function_regex = "^([0-9a-fA-F]+)\\s+<(\\w*?)>:";
-
- # Sparc64 calls '_mcount' instead of plain 'mcount'.
- $mcount_regex = "^\\s*([0-9a-fA-F]+):.*\\s_mcount\$";
-
$alignment = 8;
$type = ".xword";
$ld .= " -m elf64_sparc";
@@ -299,9 +252,6 @@ if ($arch eq "x86_64") {
die "Arch $arch is not supported with CONFIG_FTRACE_MCOUNT_RECORD";
}

-my $text_found = 0;
-my $read_function = 0;
-my $opened = 0;
my $mcount_section = "__mcount_loc";

my $dirname;
@@ -325,160 +275,26 @@ if ($filename =~ m,^(.*)(\.\S),) {
$ext = "";
}

-my $mcount_s = $dirname . "/.tmp_mc_" . $prefix . ".s";
-my $mcount_o = $dirname . "/.tmp_mc_" . $prefix . ".o";
+my $mcount_s = $dirname . "/.tmp_mc_" . $prefix . ".s";
+my $mcount_o = $dirname . "/.tmp_mc_" . $prefix . ".o";
+my $mcount_conv = $dirname . "/.tmp_mc_" . $prefix . ".conv";

check_objcopy();

-#
-# Step 1: find all the local (static functions) and weak symbols.
-# 't' is local, 'w/W' is weak
-#
-open (IN, "$nm $inputfile|") || die "error running $nm";
-while (<IN>) {
- if (/$local_regex/) {
- $locals{$1} = 1;
- } elsif (/$weak_regex/) {
- $weak{$2} = $1;
- }
-}
-close(IN);
-
-my @offsets; # Array of offsets of mcount callers
-my $ref_func; # reference function to use for offsets
-my $offset = 0; # offset of ref_func to section beginning
-
-##
-# update_funcs - print out the current mcount callers
-#
-# Go through the list of offsets to callers and write them to
-# the output file in a format that can be read by an assembler.
-#
-sub update_funcs
-{
- return unless ($ref_func and @offsets);
-
- # Sanity check on weak function. A weak function may be overwritten by
- # another function of the same name, making all these offsets incorrect.
- if (defined $weak{$ref_func}) {
- die "$inputfile: ERROR: referencing weak function" .
- " $ref_func for mcount\n";
- }
-
- # is this function static? If so, note this fact.
- if (defined $locals{$ref_func}) {
-
- # only use locals if objcopy supports globalize-symbols
- if (!$can_use_local) {
- return;
- }
- $convert{$ref_func} = 1;
- }
-
- # Loop through all the mcount caller offsets and print a reference
- # to the caller based from the ref_func.
- for (my $i=0; $i <= $#offsets; $i++) {
- if (!$opened) {
- open(FILE, ">$mcount_s") || die "can't create $mcount_s\n";
- $opened = 1;
- print FILE "\t.section $mcount_section,\"a\",$section_type\n";
- print FILE "\t.align $alignment\n" if (defined($alignment));
- }
- printf FILE "\t%s %s + %d\n", $type, $ref_func, $offsets[$i] - $offset;
- }
-}
-
-#
-# Step 2: find the sections and mcount call sites
-#
-open(IN, "$objdump -hdr $inputfile|") || die "error running $objdump";
-
-my $text;
-
-
-# read headers first
-my $read_headers = 1;
-
-while (<IN>) {
-
- if ($read_headers && /$mcount_section/) {
- #
- # Somehow the make process can execute this script on an
- # object twice. If it does, we would duplicate the mcount
- # section and it will cause the function tracer self test
- # to fail. Check if the mcount section exists, and if it does,
- # warn and exit.
- #
- print STDERR "ERROR: $mcount_section already in $inputfile\n" .
- "\tThis may be an indication that your build is corrupted.\n" .
- "\tDelete $inputfile and try again. If the same object file\n" .
- "\tstill causes an issue, then disable CONFIG_DYNAMIC_FTRACE.\n";
- exit(-1);
- }
-
- # is it a section?
- if (/$section_regex/) {
- $read_headers = 0;
-
- # Only record text sections that we know are safe
- if (defined($text_sections{$1})) {
- $read_function = 1;
- } else {
- $read_function = 0;
- }
- # print out any recorded offsets
- update_funcs();
-
- # reset all markers and arrays
- $text_found = 0;
- undef($ref_func);
- undef(@offsets);
-
- # section found, now is this a start of a function?
- } elsif ($read_function && /$function_regex/) {
- $text_found = 1;
- $text = $2;
-
- # if this is either a local function or a weak function
- # keep looking for functions that are global that
- # we can use safely.
- if (!defined($locals{$text}) && !defined($weak{$text})) {
- $ref_func = $text;
- $read_function = 0;
- $offset = hex $1;
- } else {
- # if we already have a function, and this is weak, skip it
- if (!defined($ref_func) && !defined($weak{$text}) &&
- # PPC64 can have symbols that start with .L and
- # gcc considers these special. Don't use them!
- $text !~ /^\.L/) {
- $ref_func = $text;
- $offset = hex $1;
- }
- }
- }
- # is this a call site to mcount? If so, record it to print later
- if ($text_found && /$mcount_regex/) {
- $offsets[$#offsets + 1] = hex $1;
- }
-}
-
-# dump out anymore offsets that may have been found
-update_funcs();
+`$mctool -T "$section_type" -a "$alignment" -t "$type" -c "$mcount_conv" -o "$mcount_s" -n "$mcount_section" $inputfile`;

# If we did not find any mcount callers, we are done (do nothing).
-if (!$opened) {
- exit(0);
+if (!stat("$mcount_s")) {
+ exit(0);
}

-close(FILE);
-
#
# Step 3: Compile the file that holds the list of call sites to mcount.
#
`$cc -o $mcount_o -c $mcount_s`;

-my @converts = keys %convert;
+my $convert = `touch $mcount_conv; cat "$mcount_conv"`;
+my @converts = split(/ /, $convert);

#
# Step 4: Do we have sections that started with local functions?


2009-12-04 14:16:11

by Steven Rostedt

[permalink] [raw]
Subject: Re: [RFC PATCH] tracing - moving some of recordmcount functionality to binary

On Fri, 2009-12-04 at 14:55 +0100, Jiri Olsa wrote:
> hi,
>
> I noticed on some discussion, that it might speedup the FTRACE build process,
> if some of the 'scripts/recordmcount.pl' functionality was done in binary,
> instead of in the script using nm/.objdump output.
>
>
> Before I dive into details, to sum it up:
> - i wrote tool which took over some of the 'recordmcount.pl' stuff
> - with 'allyesconfig' the build was faster about 2 minutes
>
> I'm not sure wether it's worth to continue in this effort,
> but I at least provide some details to have some enclosure.. :)

Actually, John Reiser sent me code a while back to convert the entire
shebang. I like that approach better. I actually just started looking at
it again recently.

The code will need libelf library, and may be arch specific (maybe not).
But if the current system can't compile it or use the binary, I was
going to have the make system default back to the perl script.

I rather keep the perl script intact, and just have an alternative
binary in case that doesn't work.

John,

I haven't touched the code (just dabbled with it). You can post it again
if you want, or I can if you can't find it.

-- Steve

2009-12-04 17:28:25

by John Reiser

[permalink] [raw]
Subject: Re: [RFC PATCH] tracing - moving some of recordmcount functionality to binary

On 12/04/2009 06:16 AM, Steven Rostedt wrote:
> On Fri, 2009-12-04 at 14:55 +0100, Jiri Olsa wrote:
>> I noticed on some discussion, that it might speedup the FTRACE build process,
>> if some of the 'scripts/recordmcount.pl' functionality was done in binary,
>> instead of in the script using nm/.objdump output.

> Actually, John Reiser sent me code a while back to convert the entire
> shebang. I like that approach better. I actually just started looking at
> it again recently.

Here is scripts/recordmcount.c. It contains a minor fix for PowerPC-64 ".mcount",
but otherwise it is what I mentioned on LKML, and sent to Steve Rostedt
on 2009-08-17.

/* recordmcount.c: construct a table of the locations of calls to 'mcount'
* so that ftrace can find them quickly.
* Copyright 2009 John F. Reiser <[email protected]>. All rights reserved.
* Licensed under the GNU General Public License, version 2 (GPLv2).
*/

/* Strategy: alter the .o file in-place.
*
* Append a new STRTAB that has the new section names, followed by a new array
* ElfXX_Shdr[] that has the new section headers, followed by the section
* contents for __mcount_loc and its relocations. The old shstrtab strings,
* and the old ElfXX_Shdr[] array, remain as "garbage" (commonly, a couple
* kilobytes.) Subsequent processing by /bin/ld (or the kernel module loader)
* will ignore the garbage regions, because they are not designated by the
* new .e_shoff nor the new ElfXX_Shdr[]. [In order to remove the garbage,
* then use "ld -r" to create a new file that omits the garbage.]
*/

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <elf.h>
#include <fcntl.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static int fd_map; /* File descriptor for file being modified. */
static int mmap_failed; /* Boolean flag. */
static void *ehdr_curr; /* current ElfXX_Ehdr * for resource cleanup */
static char gpfx; /* prefix for global symbol name (sometimes '_') */
static struct stat sb; /* Remember .st_size, etc. */
static jmp_buf jmpenv; /* setjmp/longjmp per-file error escape */

/* setjmp() return values */
enum {
SJ_SETJMP = 0, /* hardwired first return */
SJ_FAIL,
SJ_SUCCEED
};

/* Per-file resource cleanup when multiple files. */
static void
cleanup(void)
{
if (!mmap_failed)
munmap(ehdr_curr, sb.st_size);
else
free(ehdr_curr);
close(fd_map);
}

static void __attribute__((noreturn))
fail_file(void)
{
cleanup();
longjmp(jmpenv, SJ_FAIL);
}

static void __attribute__((noreturn))
succeed_file(void)
{
cleanup();
longjmp(jmpenv, SJ_SUCCEED);
}

/* ulseek, uread, ...: Check return value for errors. */

static off_t
ulseek(int const fd, off_t const offset, int const whence)
{
off_t const w = lseek(fd, offset, whence);
if ((off_t)-1 == w) {
perror("lseek");
fail_file();
}
return w;
}

static size_t
uread(int const fd, void *const buf, size_t const count)
{
size_t const n = read(fd, buf, count);
if (n != count) {
perror("read");
fail_file();
}
return n;
}

static size_t
uwrite(int const fd, void const *const buf, size_t const count)
{
size_t const n = write(fd, buf, count);
if (n != count) {
perror("write");
fail_file();
}
return n;
}

static void *
umalloc(size_t size)
{
void *const addr = malloc(size);
if (0 == addr) {
fprintf(stderr, "malloc failed: %zu bytes\n", size);
fail_file();
}
return addr;
}

/* Get the whole file as a programming convenience in order to avoid
* malloc+lseek+read+free of many pieces. If successful, then mmap
* avoids copying unused pieces; else just read the whole file.
* Open for both read and write; new info will be appended to the file.
* Use MAP_PRIVATE so that a few changes to the in-memory ElfXX_Ehdr
* do not propagate to the file until an explicit overwrite at the last.
* This preserves most aspects of consistency (all except .st_size)
* for simultaneous readers of the file while we are appending to it.
* However, multiple writers still are bad. We choose not to use
* locking because it is expensive and the use case of kernel build
* makes multiple writers unlikely.
*/
static void *
mmap_file(char const *fname)
{
void *addr;

fd_map = open(fname, O_RDWR);
if (0 > fd_map
|| 0 > fstat(fd_map, &sb)) {
perror(fname);
fail_file();
}
if (!S_ISREG(sb.st_mode)) {
fprintf(stderr, "not a regular file: %s\n", fname);
fail_file();
}
addr = mmap(0, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE,
fd_map, 0);
mmap_failed = 0;
if (MAP_FAILED == addr) {
mmap_failed = 1;
addr = umalloc(sb.st_size);
uread(fd_map, addr, sb.st_size);
}
return addr;
}

/* w8rev, w8nat, ...: Handle endianness. */

static uint64_t
w8rev(uint64_t const x)
{
return ( ((0xff & (x >> (0 * 8))) << (7 * 8))
| ((0xff & (x >> (1 * 8))) << (6 * 8))
| ((0xff & (x >> (2 * 8))) << (5 * 8))
| ((0xff & (x >> (3 * 8))) << (4 * 8))
| ((0xff & (x >> (4 * 8))) << (3 * 8))
| ((0xff & (x >> (5 * 8))) << (2 * 8))
| ((0xff & (x >> (6 * 8))) << (1 * 8))
| ((0xff & (x >> (7 * 8))) << (0 * 8)) );
}

static uint32_t
w4rev(uint32_t const x)
{
return ( ((0xff & (x >> (0 * 8))) << (3 * 8))
| ((0xff & (x >> (1 * 8))) << (2 * 8))
| ((0xff & (x >> (2 * 8))) << (1 * 8))
| ((0xff & (x >> (3 * 8))) << (0 * 8)) );
}

static uint32_t
w2rev(uint16_t const x)
{
return ( ((0xff & (x >> (0 * 8))) << (1 * 8))
| ((0xff & (x >> (1 * 8))) << (0 * 8)) );
}

static uint64_t
w8nat(uint64_t const x)
{
return x;
}

static uint32_t
w4nat(uint32_t const x)
{
return x;
}

static uint32_t
w2nat(uint16_t const x)
{
return x;
}

static uint64_t (*w8)(uint64_t);
static uint32_t (*w)(uint32_t);
static uint32_t (*w2)(uint16_t);

/* Names of the sections that could contain calls to mcount. */
static int
is_mcounted_section_name(char const *const txtname)
{
return 0 == strcmp(".text", txtname)
|| 0 == strcmp(".sched.text", txtname)
|| 0 == strcmp(".spinlock.text", txtname)
|| 0 == strcmp(".irqentry.text", txtname);
}

/* Append the new shstrtab, Elf32_Shdr[], __mcount_loc and its relocations. */
static void
append32(
Elf32_Ehdr *const ehdr,
Elf32_Shdr *const shstr,
uint32_t const *const mloc0,
uint32_t const *const mlocp,
Elf32_Rel const *const mrel0,
Elf32_Rel const *const mrelp,
unsigned int const rel_entsize,
unsigned int const symsec_sh_link
)
{
/* Begin constructing output file */
Elf32_Shdr mcsec;
char const *mc_name = (sizeof(Elf32_Rela) == rel_entsize)
? ".rela__mcount_loc"
: ".rel__mcount_loc";
unsigned const old_shnum = w2(ehdr->e_shnum);
uint32_t const old_shoff = w(ehdr->e_shoff);
uint32_t const old_shstr_sh_size = w(shstr->sh_size);
uint32_t const old_shstr_sh_offset = w(shstr->sh_offset);
uint32_t t = 1+ strlen(mc_name) + w(shstr->sh_size);
uint32_t new_e_shoff;

shstr->sh_size = w(t);
shstr->sh_offset = w(sb.st_size);
t += sb.st_size;
t += (3u & -t); /* 4-byte align */
new_e_shoff = t;

/* body for new shstrtab */
ulseek(fd_map, sb.st_size, SEEK_SET);
uwrite(fd_map, old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size);
uwrite(fd_map, mc_name, 1+ strlen(mc_name));

/* old(modified) Elf32_Shdr table, 4-byte aligned */
ulseek(fd_map, t, SEEK_SET);
t += sizeof(Elf32_Shdr) * old_shnum;
uwrite(fd_map, old_shoff + (void *)ehdr,
sizeof(Elf32_Shdr) * old_shnum);

/* new sections __mcount_loc and .rel__mcount_loc */
t += 2*sizeof(mcsec);
mcsec.sh_name = w((sizeof(Elf32_Rela) == rel_entsize) + strlen(".rel")
+ old_shstr_sh_size);
mcsec.sh_type = w(SHT_PROGBITS);
mcsec.sh_flags = w(SHF_ALLOC);
mcsec.sh_addr = 0;
mcsec.sh_offset = w(t);
mcsec.sh_size = w((void *)mlocp - (void *)mloc0);
mcsec.sh_link = 0;
mcsec.sh_info = 0;
mcsec.sh_addralign = w(4);
mcsec.sh_entsize = w(4);
uwrite(fd_map, &mcsec, sizeof(mcsec));

mcsec.sh_name = w(old_shstr_sh_size);
mcsec.sh_type = (sizeof(Elf32_Rela) == rel_entsize)
? w(SHT_RELA)
: w(SHT_REL);
mcsec.sh_flags = 0;
mcsec.sh_addr = 0;
mcsec.sh_offset = w((void *)mlocp - (void *)mloc0 + t);
mcsec.sh_size = w((void *)mrelp - (void *)mrel0);
mcsec.sh_link = w(symsec_sh_link);
mcsec.sh_info = w(old_shnum);
mcsec.sh_addralign = w(4);
mcsec.sh_entsize = w(rel_entsize);
uwrite(fd_map, &mcsec, sizeof(mcsec));

uwrite(fd_map, mloc0, (void *)mlocp - (void *)mloc0);
uwrite(fd_map, mrel0, (void *)mrelp - (void *)mrel0);

ehdr->e_shoff = w(new_e_shoff);
ehdr->e_shnum = w2(2+ w2(ehdr->e_shnum)); /* {.rel,}__mcount_loc */
ulseek(fd_map, 0, SEEK_SET);
uwrite(fd_map, ehdr, sizeof(*ehdr));
}

/* append64 and append32 (and other analogous pairs) could be templated
* using C++, but the complexity is high. (For an example, look at p_elf.h
* in the source for UPX, http://upx.sourceforge.net) So: remember to make
* the corresponding change in the routine for the other size.
*/
static void
append64(
Elf64_Ehdr *const ehdr,
Elf64_Shdr *const shstr,
uint64_t const *const mloc0,
uint64_t const *const mlocp,
Elf64_Rel const *const mrel0,
Elf64_Rel const *const mrelp,
unsigned int const rel_entsize,
unsigned int const symsec_sh_link
)
{
/* Begin constructing output file */
Elf64_Shdr mcsec;
char const *mc_name = (sizeof(Elf64_Rela) == rel_entsize)
? ".rela__mcount_loc"
: ".rel__mcount_loc";
unsigned const old_shnum = w2(ehdr->e_shnum);
uint64_t const old_shoff = w8(ehdr->e_shoff);
uint64_t const old_shstr_sh_size = w8(shstr->sh_size);
uint64_t const old_shstr_sh_offset = w8(shstr->sh_offset);
uint64_t t = 1+ strlen(mc_name) + w8(shstr->sh_size);
uint64_t new_e_shoff;

shstr->sh_size = w8(t);
shstr->sh_offset = w8(sb.st_size);
t += sb.st_size;
t += (7u & -t); /* 8-byte align */
new_e_shoff = t;

/* body for new shstrtab */
ulseek(fd_map, sb.st_size, SEEK_SET);
uwrite(fd_map, old_shstr_sh_offset + (void *)ehdr, old_shstr_sh_size);
uwrite(fd_map, mc_name, 1+ strlen(mc_name));

/* old(modified) Elf64_Shdr table, 8-byte aligned */
ulseek(fd_map, t, SEEK_SET);
t += sizeof(Elf64_Shdr) * old_shnum;
uwrite(fd_map, old_shoff + (void *)ehdr,
sizeof(Elf64_Shdr) * old_shnum);

/* new sections __mcount_loc and .rel__mcount_loc */
t += 2*sizeof(mcsec);
mcsec.sh_name = w((sizeof(Elf64_Rela) == rel_entsize) + strlen(".rel")
+ old_shstr_sh_size);
mcsec.sh_type = w(SHT_PROGBITS);
mcsec.sh_flags = w8(SHF_ALLOC);
mcsec.sh_addr = 0;
mcsec.sh_offset = w8(t);
mcsec.sh_size = w8((void *)mlocp - (void *)mloc0);
mcsec.sh_link = 0;
mcsec.sh_info = 0;
mcsec.sh_addralign = w8(8);
mcsec.sh_entsize = w8(8);
uwrite(fd_map, &mcsec, sizeof(mcsec));

mcsec.sh_name = w(old_shstr_sh_size);
mcsec.sh_type = (sizeof(Elf64_Rela) == rel_entsize)
? w(SHT_RELA)
: w(SHT_REL);
mcsec.sh_flags = 0;
mcsec.sh_addr = 0;
mcsec.sh_offset = w8((void *)mlocp - (void *)mloc0 + t);
mcsec.sh_size = w8((void *)mrelp - (void *)mrel0);
mcsec.sh_link = w(symsec_sh_link);
mcsec.sh_info = w(old_shnum);
mcsec.sh_addralign = w8(8);
mcsec.sh_entsize = w8(rel_entsize);
uwrite(fd_map, &mcsec, sizeof(mcsec));

uwrite(fd_map, mloc0, (void *)mlocp - (void *)mloc0);
uwrite(fd_map, mrel0, (void *)mrelp - (void *)mrel0);

ehdr->e_shoff = w8(new_e_shoff);
ehdr->e_shnum = w2(2+ w2(ehdr->e_shnum)); /* {.rel,}__mcount_loc */
ulseek(fd_map, 0, SEEK_SET);
uwrite(fd_map, ehdr, sizeof(*ehdr));
}

/* Look at the relocations in order to find the calls to mcount.
* Accumulate the section offsets that are found, and their relocation info,
* onto the end of the existing arrays.
*/
static uint32_t *
sift32_rel_mcount(
uint32_t *mlocp,
unsigned const offbase,
Elf32_Rel **const mrelpp,
Elf32_Shdr const *const relhdr,
Elf32_Ehdr const *const ehdr,
unsigned const recsym,
uint32_t const recval,
unsigned const reltype
)
{
uint32_t *const mloc0 = mlocp;
Elf32_Rel *mrelp = *mrelpp;
Elf32_Shdr *const shdr0 = (Elf32_Shdr *)(w(ehdr->e_shoff)
+ (void *)ehdr);
unsigned const symsec_sh_link = w(relhdr->sh_link);
Elf32_Shdr const *const symsec = &shdr0[symsec_sh_link];
Elf32_Sym const *const sym0 = (Elf32_Sym const *)(w(symsec->sh_offset)
+ (void *)ehdr);

Elf32_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
char const *const str0 = (char const *)(w(strsec->sh_offset)
+ (void *)ehdr);

Elf32_Rel const *const rel0 = (Elf32_Rel const *)(w(relhdr->sh_offset)
+ (void *)ehdr);
unsigned rel_entsize = w(relhdr->sh_entsize);
unsigned const nrel = w(relhdr->sh_size) / rel_entsize;
Elf32_Rel const *relp = rel0;

unsigned mcountsym = 0;
unsigned t;
for (t = nrel; 0 != t; --t) {
if (0 == mcountsym) {
Elf32_Sym const *const symp = &sym0[
ELF32_R_SYM(w(relp->r_info))];
if (0 == strcmp((('_' == gpfx) ? "_mcount" : "mcount"),
&str0[w(symp->st_name)])) {
mcountsym = ELF32_R_SYM(w(relp->r_info));
}
}
if (mcountsym == ELF32_R_SYM(w(relp->r_info))) {
uint32_t const addend = w(w(relp->r_offset) - recval);
mrelp->r_offset = w(offbase
+ ((void *)mlocp - (void *)mloc0));
mrelp->r_info = w(ELF32_R_INFO(recsym, reltype));
if (sizeof(Elf32_Rela) == rel_entsize) {
((Elf32_Rela *)mrelp)->r_addend = addend;
*mlocp++ = 0;
}
else {
*mlocp++ = addend;
}
mrelp = (Elf32_Rel *)(rel_entsize + (void *)mrelp);
}
relp = (Elf32_Rel const *)(rel_entsize + (void *)relp);
}
*mrelpp = mrelp;
return mlocp;
}

static uint64_t *
sift64_rel_mcount(
uint64_t *mlocp,
unsigned const offbase,
Elf64_Rel **const mrelpp,
Elf64_Shdr const *const relhdr,
Elf64_Ehdr const *const ehdr,
unsigned const recsym,
uint64_t const recval,
unsigned const reltype
)
{
uint64_t *const mloc0 = mlocp;
Elf64_Rel *mrelp = *mrelpp;
Elf64_Shdr *const shdr0 = (Elf64_Shdr *)(w8(ehdr->e_shoff)
+ (void *)ehdr);
unsigned const symsec_sh_link = w(relhdr->sh_link);
Elf64_Shdr const *const symsec = &shdr0[symsec_sh_link];
Elf64_Sym const *const sym0 = (Elf64_Sym const *)(w8(symsec->sh_offset)
+ (void *)ehdr);

Elf64_Shdr const *const strsec = &shdr0[w(symsec->sh_link)];
char const *const str0 = (char const *)(w8(strsec->sh_offset)
+ (void *)ehdr);

Elf64_Rel const *const rel0 = (Elf64_Rel const *)(w8(relhdr->sh_offset)
+ (void *)ehdr);
unsigned rel_entsize = w8(relhdr->sh_entsize);
unsigned const nrel = w8(relhdr->sh_size) / rel_entsize;
Elf64_Rel const *relp = rel0;

unsigned mcountsym = 0;
unsigned t;
for (t = nrel; 0 != t; --t) {
if (0 == mcountsym) {
Elf64_Sym const *const symp = &sym0[
ELF64_R_SYM(w8(relp->r_info))];
char const *symname = &str0[w(symp->st_name)];
if ('.' == symname[0])
++symname; /* ppc64 hack */
if (0 == strcmp((('_' == gpfx) ? "_mcount" : "mcount"),
symname)) {
mcountsym = ELF64_R_SYM(w8(relp->r_info));
}
}
if (mcountsym == ELF64_R_SYM(w8(relp->r_info))) {
uint64_t const addend = w8(w8(relp->r_offset) - recval);
mrelp->r_offset = w8(offbase
+ ((void *)mlocp - (void *)mloc0));
mrelp->r_info = w8(ELF64_R_INFO(recsym, reltype));
if (sizeof(Elf64_Rela) == rel_entsize) {
((Elf64_Rela *)mrelp)->r_addend = addend;
*mlocp++ = 0;
}
else {
*mlocp++ = addend;
}
mrelp = (Elf64_Rel *)(rel_entsize + (void *)mrelp);
}
relp = (Elf64_Rel const *)(rel_entsize + (void *)relp);
}
*mrelpp = mrelp;
return mlocp;
}

/* Find a symbol in the given section, to be used as the base for relocating
* the table of offsets of calls to mcount. A local or global symbol suffices,
* but avoid a Weak symbol because it may be overridden; the change in value
* would invalidate the relocations of the offsets of the calls to mcount.
* Often the found symbol will be the unnamed local symbol generated by
* GNU 'as' for the start of each section. For example:
* Num: Value Size Type Bind Vis Ndx Name
* 2: 00000000 0 SECTION LOCAL DEFAULT 1
*/
static unsigned
find32_secsym_ndx(
unsigned const txtndx,
char const *const txtname,
uint32_t *const recvalp,
Elf32_Shdr const *const symhdr,
Elf32_Ehdr const *const ehdr
)
{
Elf32_Sym const *const sym0 = (Elf32_Sym const *)(w(symhdr->sh_offset)
+ (void *)ehdr);
unsigned const nsym = w(symhdr->sh_size) / w(symhdr->sh_entsize);
Elf32_Sym const *symp;

unsigned t;
for ((symp = sym0), (t = nsym); 0 != t; --t, ++symp) {
unsigned int const st_bind = ELF32_ST_BIND(symp->st_info);
if (txtndx == w2(symp->st_shndx)
/* avoid STB_WEAK */
&& (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
*recvalp = w(symp->st_value);
return symp - sym0;
}
}
fprintf(stderr, "Cannot find symbol for section %d: %s.\n",
txtndx, txtname);
fail_file();
}

static unsigned
find64_secsym_ndx(
unsigned const txtndx,
char const *const txtname,
uint64_t *const recvalp,
Elf64_Shdr const *const symhdr,
Elf64_Ehdr const *const ehdr
)
{
Elf64_Sym const *const sym0 = (Elf64_Sym const *)(w8(symhdr->sh_offset)
+ (void *)ehdr);
unsigned const nsym = w8(symhdr->sh_size) / w8(symhdr->sh_entsize);
Elf64_Sym const *symp;

unsigned t;
for ((symp = sym0), (t = nsym); 0 != t; --t, ++symp) {
unsigned int const st_bind = ELF64_ST_BIND(symp->st_info);
if (txtndx == w2(symp->st_shndx)
/* avoid STB_WEAK */
&& (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
*recvalp = w8(symp->st_value);
return symp - sym0;
}
}
fprintf(stderr, "Cannot find symbol for section %d: %s.\n",
txtndx, txtname);
fail_file();
}

/* Evade ISO C restriction: no declaration after statement in has32_rel_mcount. */
static char const *
__has32_rel_mcount(
Elf32_Shdr const *const relhdr, /* is SHT_REL or SHT_RELA */
Elf32_Shdr const *const shdr0,
char const *const shstrtab,
char const *const fname
)
{
/* .sh_info depends on .sh_type == SHT_REL[,A] */
Elf32_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
char const *const txtname = &shstrtab[w(txthdr->sh_name)];

if (0 == strcmp("__mcount_loc", txtname)) {
fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
fname);
succeed_file();
}
if (SHT_PROGBITS != w(txthdr->sh_type)
|| !is_mcounted_section_name(txtname))
return NULL;
return txtname;
}

static char const *
has32_rel_mcount(
Elf32_Shdr const *const relhdr,
Elf32_Shdr const *const shdr0,
char const *const shstrtab,
char const *const fname
)
{
if (SHT_REL != w(relhdr->sh_type)
&& SHT_RELA != w(relhdr->sh_type))
return NULL;
return __has32_rel_mcount(relhdr, shdr0, shstrtab, fname);
}

static char const *
__has64_rel_mcount(
Elf64_Shdr const *const relhdr,
Elf64_Shdr const *const shdr0,
char const *const shstrtab,
char const *const fname
)
{
/* .sh_info depends on .sh_type == SHT_REL[,A] */
Elf64_Shdr const *const txthdr = &shdr0[w(relhdr->sh_info)];
char const *const txtname = &shstrtab[w(txthdr->sh_name)];

if (0 == strcmp("__mcount_loc", txtname)) {
fprintf(stderr, "warning: __mcount_loc already exists: %s\n",
fname);
succeed_file();
}
if (SHT_PROGBITS != w(txthdr->sh_type)
|| !is_mcounted_section_name(txtname))
return NULL;
return txtname;
}

static char const *
has64_rel_mcount(
Elf64_Shdr const *const relhdr,
Elf64_Shdr const *const shdr0,
char const *const shstrtab,
char const *const fname
)
{
if (SHT_REL != w(relhdr->sh_type)
&& SHT_RELA != w(relhdr->sh_type))
return NULL;
return __has64_rel_mcount(relhdr, shdr0, shstrtab, fname);
}

static unsigned
tot32_relsize(
Elf32_Shdr const *const shdr0,
unsigned nhdr,
const char *const shstrtab,
const char *const fname
)
{
unsigned totrelsz = 0;
Elf32_Shdr const *shdrp = shdr0;
for (; 0 != nhdr; --nhdr, ++shdrp) {
if (has32_rel_mcount(shdrp, shdr0, shstrtab, fname))
totrelsz += w(shdrp->sh_size);
}
return totrelsz;
}

static unsigned
tot64_relsize(
Elf64_Shdr const *const shdr0,
unsigned nhdr,
const char *const shstrtab,
const char *const fname
)
{
unsigned totrelsz = 0;
Elf64_Shdr const *shdrp = shdr0;
for (; 0 != nhdr; --nhdr, ++shdrp) {
if (has64_rel_mcount(shdrp, shdr0, shstrtab, fname))
totrelsz += w8(shdrp->sh_size);
}
return totrelsz;
}

/* Overall supervision for Elf32 ET_REL file. */
static void
do32(Elf32_Ehdr *const ehdr, char const *const fname, unsigned const reltype)
{
Elf32_Shdr *const shdr0 = (Elf32_Shdr *)(w(ehdr->e_shoff)
+ (void *)ehdr);
unsigned const nhdr = w2(ehdr->e_shnum);
Elf32_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)];
char const *const shstrtab = (char const *)(w(shstr->sh_offset)
+ (void *)ehdr);

Elf32_Shdr const *relhdr;
unsigned k;

/* Upper bound on space: assume all relevant relocs are for mcount. */
unsigned const totrelsz = tot32_relsize(shdr0, nhdr, shstrtab, fname);
Elf32_Rel *const mrel0 = umalloc(totrelsz);
Elf32_Rel * mrelp = mrel0;

/* 2*sizeof(address) <= sizeof(Elf32_Rel) */
uint32_t *const mloc0 = umalloc(totrelsz>>1);
uint32_t * mlocp = mloc0;

unsigned rel_entsize = 0;
unsigned symsec_sh_link = 0;
for ((relhdr = shdr0), k = nhdr; 0 != k; --k, ++relhdr) {
char const *const txtname = has32_rel_mcount(relhdr, shdr0,
shstrtab, fname);
if (txtname) {
uint32_t recval = 0;
unsigned const recsym = find32_secsym_ndx(
w(relhdr->sh_info), txtname, &recval,
&shdr0[symsec_sh_link = w(relhdr->sh_link)],
ehdr);

rel_entsize = w(relhdr->sh_entsize);
mlocp = sift32_rel_mcount(mlocp,
(void *)mlocp - (void *)mloc0, &mrelp,
relhdr, ehdr, recsym, recval, reltype);
}
}
if (mloc0 != mlocp) {
append32(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
rel_entsize, symsec_sh_link);
}
free(mrel0);
free(mloc0);
}

static void
do64(Elf64_Ehdr *const ehdr, char const *const fname, unsigned const reltype)
{
Elf64_Shdr *const shdr0 = (Elf64_Shdr *)(w8(ehdr->e_shoff)
+ (void *)ehdr);
unsigned const nhdr = w2(ehdr->e_shnum);
Elf64_Shdr *const shstr = &shdr0[w2(ehdr->e_shstrndx)];
char const *const shstrtab = (char const *)(w8(shstr->sh_offset)
+ (void *)ehdr);

Elf64_Shdr const *relhdr;
unsigned k;

/* Upper bound on space: assume all relevant relocs are for mcount. */
unsigned const totrelsz = tot64_relsize(shdr0, nhdr, shstrtab, fname);
Elf64_Rel *const mrel0 = umalloc(totrelsz);
Elf64_Rel * mrelp = mrel0;

/* 2*sizeof(address) <= sizeof(Elf64_Rel) */
uint64_t *const mloc0 = umalloc(totrelsz>>1);
uint64_t * mlocp = mloc0;

unsigned rel_entsize = 0;
unsigned symsec_sh_link = 0;
for ((relhdr = shdr0), k = nhdr; 0 != k; --k, ++relhdr) {
char const *const txtname = has64_rel_mcount(relhdr, shdr0,
shstrtab, fname);
if (txtname) {
uint64_t recval = 0;
unsigned const recsym = find64_secsym_ndx(
w(relhdr->sh_info), txtname, &recval,
&shdr0[symsec_sh_link = w(relhdr->sh_link)],
ehdr);

rel_entsize = w8(relhdr->sh_entsize);
mlocp = sift64_rel_mcount(mlocp,
(void *)mlocp - (void *)mloc0, &mrelp,
relhdr, ehdr, recsym, recval, reltype);
}
}
if (mloc0 != mlocp) {
append64(ehdr, shstr, mloc0, mlocp, mrel0, mrelp,
rel_entsize, symsec_sh_link);
}
free(mrel0);
free(mloc0);
}

static void
do_file(char const *const fname)
{
Elf32_Ehdr *const ehdr = mmap_file(fname);
unsigned int reltype = 0;

ehdr_curr = ehdr;
w = w4nat;
w2 = w2nat;
w8 = w8nat;
switch (ehdr->e_ident[EI_DATA]) {
static unsigned int const endian = 1;
default: {
fprintf(stderr, "unrecognized ELF data encoding %d: %s\n",
ehdr->e_ident[EI_DATA], fname);
fail_file();
} break;
case ELFDATA2LSB: {
if (1 != *(unsigned char const *)&endian) {
/* main() is big endian, file.o is little endian. */
w = w4rev;
w2 = w2rev;
w8 = w8rev;
}
} break;
case ELFDATA2MSB: {
if (0 != *(unsigned char const *)&endian) {
/* main() is little endian, file.o is big endian. */
w = w4rev;
w2 = w2rev;
w8 = w8rev;
}
} break;
} /* end switch */
if (0 != memcmp(ELFMAG, ehdr->e_ident, SELFMAG)
|| ET_REL != w2(ehdr->e_type)
|| EV_CURRENT != ehdr->e_ident[EI_VERSION]) {
fprintf(stderr, "unrecognized ET_REL file %s\n", fname);
fail_file();
}

gpfx = 0;
switch (w2(ehdr->e_machine)) {
default: {
fprintf(stderr, "unrecognized e_machine %d %s\n",
w2(ehdr->e_machine), fname);
fail_file();
} break;
case EM_386: reltype = R_386_32; break;
case EM_ARM: reltype = R_ARM_ABS32; break;
case EM_IA_64: reltype = R_IA64_IMM64; gpfx = '_'; break;
case EM_PPC: reltype = R_PPC_ADDR32; gpfx = '_'; break;
case EM_PPC64: reltype = R_PPC64_ADDR64; gpfx = '_'; break;
case EM_S390: /* reltype: e_class */ gpfx = '_'; break;
case EM_SH: reltype = R_SH_DIR32; break;
case EM_SPARCV9: reltype = R_SPARC_64; gpfx = '_'; break;
case EM_X86_64: reltype = R_X86_64_64; break;
} /* end switch */

switch (ehdr->e_ident[EI_CLASS]) {
default: {
fprintf(stderr, "unrecognized ELF class %d %s\n",
ehdr->e_ident[EI_CLASS], fname);
fail_file();
} break;
case ELFCLASS32: {
if (sizeof(Elf32_Ehdr) != w2(ehdr->e_ehsize)
|| sizeof(Elf32_Shdr) != w2(ehdr->e_shentsize)) {
fprintf(stderr, "unrecognized ET_REL file: %s\n", fname);
fail_file();
}
if (EM_S390 == w2(ehdr->e_machine))
reltype = R_390_32;
do32(ehdr, fname, reltype);
} break;
case ELFCLASS64: {
Elf64_Ehdr *const ghdr = (Elf64_Ehdr *)ehdr;
if (sizeof(Elf64_Ehdr) != w2(ghdr->e_ehsize)
|| sizeof(Elf64_Shdr) != w2(ghdr->e_shentsize)) {
fprintf(stderr, "unrecognized ET_REL file: %s\n", fname);
fail_file();
}
if (EM_S390 == w2(ghdr->e_machine))
reltype = R_390_64;
do64(ghdr, fname, reltype);
} break;
} /* end switch */

cleanup();
}

int
main(int argc, char const *argv[])
{
int volatile n_error = 0; /* gcc-4.3.0 false positive complaint */
if (argc <= 1)
fprintf(stderr, "usage: recordmcount file.o...\n");
else /* Process each file in turn, allowing deep failure. */
for (--argc, ++argv; 0 < argc; --argc, ++argv) {
int const sjval = setjmp(jmpenv);
switch (sjval) {
default: {
fprintf(stderr, "internal error: %s\n", argv[0]);
exit(1);
} break;
case SJ_SETJMP: { /* normal sequence */
/* Avoid problems if early cleanup() */
fd_map = -1;
ehdr_curr = NULL;
mmap_failed = 1;
do_file(argv[0]);
} break;
case SJ_FAIL: { /* error in do_file or below */
++n_error;
} break;
case SJ_SUCCEED: { /* premature success */
/* do nothing */
} break;
} /* end switch */
}
return (0 != n_error);
}



--