From 6b41dbf977a3e001a77100f092532b879115d8e0 Mon Sep 17 00:00:00 2001
From: Rong Xu <xur@google.com>
Date: Wed, 26 Jan 2011 14:33:10 -0800
Subject: [PATCH] core-gcov: enable FDO profile generation in GCC

This change enables profile generation for Feedback Directed
Optimization (FDO) in GCC. It only works with gcc version
4.4.3 or later. Compiling with gcc 3.4 gives the same behavior
as in the original gcov implementation.

This change focuses on enabling FDO and it does not maintain
the same feature set as the original gcov implementation:
1) .gcno and .c files will no longer be linked to profiles
   directory.
2) all .gcda files will have a .tmp_ prefix, the same prefix
    when feeding c source files to the compiler.

To enable profile generation, we need to
1) set the following config:
   CONFIG_GCOV_KERNEL=y
   CONFIG_GCOV_PROFILE_ALL=y
2) set COMPILER_LIB_PATH to the absolute path to the
   parent directory of "gcov-src". "gcov-src" is a directory
   containing the following 4 files:
     gcov-iov.h gcov-io.h gcov-io.c and libgcov.c.
   It usually sits at
${GCC_ROOT}/lib/gcc/x86_64-unknown-linux-gnu/${GCC_VERSION}/.

The profiles will be stored in /sys/debug/debug/gcov/.

Google-Bug-Id: 2405721
Effort: core-gcov
Change-Id: I3e43e06771abf168775d6082e999fd0334845b60
---
 kernel/gcov/Makefile            |    7 +-
 kernel/gcov/base.c              |   24 ++-
 kernel/gcov/fs.c                |  278 ++++++++++++++++++++----
 kernel/gcov/gcc.c               |  461 +++++++++++++++++++++++++++++++++++++++
 kernel/gcov/gcc_3_4.c           |  447 -------------------------------------
 kernel/gcov/gcc_version_check.h |   16 ++
 kernel/gcov/gcov.h              |   13 +
 7 files changed, 745 insertions(+), 501 deletions(-)
 create mode 100644 kernel/gcov/gcc.c
 delete mode 100644 kernel/gcov/gcc_3_4.c
 create mode 100644 kernel/gcov/gcc_version_check.h

diff --git a/kernel/gcov/Makefile b/kernel/gcov/Makefile
index 3f76100..c2c3e04 100644
--- a/kernel/gcov/Makefile
+++ b/kernel/gcov/Makefile
@@ -1,3 +1,6 @@
-EXTRA_CFLAGS := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"'
+EXTRA_CFLAGS := -DSRCTREE='"$(srctree)"' -DOBJTREE='"$(objtree)"' -iquote "$(COMPILER_LIB_PATH)"
 
-obj-$(CONFIG_GCOV_KERNEL) := base.o fs.o gcc_3_4.o
+# don't profile itself (maybe needed for code coverage test?)
+GCOV_PROFILE := n
+
+obj-$(CONFIG_GCOV_KERNEL) := base.o fs.o gcc.o
diff --git a/kernel/gcov/base.c b/kernel/gcov/base.c
index 9b22d03..0d4d0cc 100644
--- a/kernel/gcov/base.c
+++ b/kernel/gcov/base.c
@@ -13,6 +13,9 @@
  *		 Paul Larson
  */
 
+#include "gcc_version_check.h"
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3) || GCC_VERSION_EQUAL(3, 4)
+
 #define pr_fmt(fmt)	"gcov: " fmt
 
 #include <linux/init.h>
@@ -20,7 +23,7 @@
 #include <linux/mutex.h>
 #include "gcov.h"
 
-static struct gcov_info *gcov_info_head;
+static struct gcov_info *__gcov_list;
 static int gcov_events_enabled;
 static DEFINE_MUTEX(gcov_lock);
 
@@ -45,14 +48,20 @@ void __gcov_init(struct gcov_info *info)
 	 * Add new profiling data structure to list and inform event
 	 * listener.
 	 */
-	info->next = gcov_info_head;
-	gcov_info_head = info;
+	info->next = __gcov_list;
+	__gcov_list = info;
+
 	if (gcov_events_enabled)
 		gcov_event(GCOV_ADD, info);
 	mutex_unlock(&gcov_lock);
 }
 EXPORT_SYMBOL(__gcov_init);
 
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3)
+
+#include "gcov-src/libgcov.c"
+
+#else /* GCC_VERSION_EQUAL(3, 4) */
 /*
  * These functions may be referenced by gcc-generated profiling code but serve
  * no function for kernel profiling.
@@ -80,6 +89,7 @@ void __gcov_merge_delta(gcov_type *counters, unsigned int n_counters)
 	/* Unused. */
 }
 EXPORT_SYMBOL(__gcov_merge_delta);
+#endif /* GCC_VERSION */
 
 /**
  * gcov_enable_events - enable event reporting through gcov_event()
@@ -96,7 +106,7 @@ void gcov_enable_events(void)
 	mutex_lock(&gcov_lock);
 	gcov_events_enabled = 1;
 	/* Perform event callback for previously registered entries. */
-	for (info = gcov_info_head; info; info = info->next)
+	for (info = __gcov_list; info; info = info->next)
 		gcov_event(GCOV_ADD, info);
 	mutex_unlock(&gcov_lock);
 }
@@ -120,12 +130,12 @@ static int gcov_module_notifier(struct notifier_block *nb, unsigned long event,
 	mutex_lock(&gcov_lock);
 	prev = NULL;
 	/* Remove entries located in module from linked list. */
-	for (info = gcov_info_head; info; info = info->next) {
+	for (info = __gcov_list; info; info = info->next) {
 		if (within(info, mod->module_core, mod->core_size)) {
 			if (prev)
 				prev->next = info->next;
 			else
-				gcov_info_head = info->next;
+				__gcov_list = info->next;
 			if (gcov_events_enabled)
 				gcov_event(GCOV_REMOVE, info);
 		} else
@@ -146,3 +156,5 @@ static int __init gcov_init(void)
 }
 device_initcall(gcov_init);
 #endif /* CONFIG_MODULES */
+
+#endif /* GCC_VERSION */
diff --git a/kernel/gcov/fs.c b/kernel/gcov/fs.c
index f83972b..6507532 100644
--- a/kernel/gcov/fs.c
+++ b/kernel/gcov/fs.c
@@ -14,6 +14,9 @@
  *		 Yi CDL Yang
  */
 
+#include "gcc_version_check.h"
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3) || GCC_VERSION_EQUAL(3, 4)
+
 #define pr_fmt(fmt)	"gcov: " fmt
 
 #include <linux/init.h>
@@ -24,7 +27,12 @@
 #include <linux/string.h>
 #include <linux/slab.h>
 #include <linux/mutex.h>
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3)
+#include <linux/vmalloc.h>
+#include <asm/uaccess.h>
+#else /* GCC_VERSION_EQUAL(3, 4) */
 #include <linux/seq_file.h>
+#endif
 #include "gcov.h"
 
 /**
@@ -86,6 +94,185 @@ static int __init gcov_persist_setup(char *str)
 __setup("gcov_persist=", gcov_persist_setup);
 
 /*
+ * Return a profiling data set associated with the given node. This is
+ * either a data set for a loaded object file or a data set copy in case
+ * all associated object files have been unloaded.
+ */
+static struct gcov_info *get_node_info(struct gcov_node *node)
+{
+	if (node->num_loaded > 0)
+		return node->loaded_info[0];
+
+	return node->unloaded_info;
+}
+
+static struct gcov_node *get_node_by_name(const char *name);
+static void reset_node(struct gcov_node *node);
+static void remove_node(struct gcov_node *node);
+
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3)
+extern void gcov_set_vfile (gcov_kernel_vfile *);
+extern unsigned gcov_gcda_file_size (struct gcov_info *);
+extern void gcov_kernel_dump_one_gcov (struct gcov_info *);
+
+/*
+ * open() implementation for gcov data files. Create a copy of the profiling
+ * data set and initialize the object and vfile interface.
+ */
+static int gcda_file_open(struct inode *inode, struct file *file)
+{
+	struct gcov_node *node = inode->i_private;
+	gcov_kernel_vfile *kvf;
+	struct gcov_info *info;
+	int rc = -ENOMEM;
+	unsigned vfile_size, alloc_size;
+
+	mutex_lock(&node_lock);
+	/*
+	 * Read from a profiling data copy to minimize reference tracking
+	 * complexity and concurrent access.
+	 */
+	info = gcov_info_dup(get_node_info(node));
+	if (!info)
+		goto out_unlock;
+
+	vfile_size = gcov_gcda_file_size (info);
+	alloc_size = vfile_size + sizeof(gcov_kernel_vfile);
+	kvf = (gcov_kernel_vfile *) vmalloc (alloc_size);
+
+	if (kvf == NULL) {
+		pr_err("Cannot allocate memory (%d) for gcov.\n", alloc_size);
+		goto err_free_info;
+	}
+
+	kvf->size = vfile_size;
+	kvf->info = info;
+	kvf->count = 0;
+
+	file->private_data = kvf;
+	gcov_set_vfile (kvf);
+
+	gcov_kernel_dump_one_gcov (info);
+	rc = 0;
+
+	goto out_unlock;
+
+err_free_info:
+	gcov_info_free(info);
+
+out_unlock:
+	mutex_unlock(&node_lock);
+	return rc;
+
+}
+
+/*
+ * read() implementation for gcov data files. Copy the profile data
+ * stored in virtual file to user space buffer.
+ */
+static ssize_t gcda_file_read(struct file *filp, char __user *ubuf,
+			      size_t count, loff_t *ppos)
+{
+	gcov_kernel_vfile *kvf;
+	char *buf;
+	int ret;
+	int copied;
+	int n;
+	unsigned buf_size, buf_cnt;
+
+	if (!count)
+		return 0;
+
+	kvf = filp->private_data;
+	buf = kvf->buf;
+	if (!buf)
+		return 0;
+
+	buf_size = kvf->size;
+	buf_cnt = kvf->count;
+
+	if (buf_cnt == *ppos)
+		return 0;
+
+	if (buf_cnt < *ppos) {
+		pr_err("Buffer overflows: buf_cnt=%u, pos=%u\n",
+			buf_cnt, (unsigned)*ppos);
+		return -EFAULT;
+	}
+
+	n = count;
+	if (buf_cnt - *ppos < n)
+		n = buf_cnt - *ppos;
+
+	ret = copy_to_user(ubuf, buf + *ppos, n);
+
+	/* Not all data copied. Invalid user space address? */
+	if (ret != 0)
+		return -EFAULT;
+
+	copied = n-ret;
+	*ppos += copied;
+
+	return copied;
+}
+
+/*
+ * release() implementation for gcov data files. Release resources allocated
+ * by open().
+ */
+static int gcda_file_release(struct inode *inode, struct file *file)
+{
+	struct gcov_info *info;
+	gcov_kernel_vfile *kvf;
+
+	kvf = file->private_data;
+	info = kvf->info;
+	gcov_info_free(info);
+	vfree (kvf);
+
+	return 0;
+}
+
+/*
+ * write() implementation for gcov data files. Reset profiling data for the
+ * associated file. If the object file has been unloaded (i.e. this is
+ * a "ghost" node), remove the debug fs node as well.
+ */
+static ssize_t gcda_file_write(struct file *file, const char __user *addr,
+			        size_t len, loff_t *pos)
+{
+	gcov_kernel_vfile *kvf;
+	struct gcov_info *info;
+	struct gcov_node *node;
+
+	kvf = file->private_data;
+	info = kvf->info;
+	mutex_lock(&node_lock);
+	node = get_node_by_name(info->filename);
+	if (node) {
+		/* Reset counts or remove node for unloaded modules.  */
+		if (node->num_loaded)
+			remove_node(node);
+		else
+			reset_node(node);
+	}
+	/* Reset counts for open file.  */
+	gcov_info_reset(info);
+	mutex_unlock(&node_lock);
+
+	return len;
+}
+
+static const struct file_operations gcda_file_fops = {
+	.open		= gcda_file_open,
+	.read		= gcda_file_read,
+	.write		= gcda_file_write,
+	.release	= gcda_file_release,
+};
+
+#else /* GCC_VERSION_EQUAL(3, 4) */
+
+/*
  * seq_file.start() implementation for gcov data files. Note that the
  * gcov_iterator interface is designed to be more restrictive than seq_file
  * (no start from arbitrary position, etc.), to simplify the iterator
@@ -138,19 +325,6 @@ static const struct seq_operations gcov_seq_ops = {
 };
 
 /*
- * Return a profiling data set associated with the given node. This is
- * either a data set for a loaded object file or a data set copy in case
- * all associated object files have been unloaded.
- */
-static struct gcov_info *get_node_info(struct gcov_node *node)
-{
-	if (node->num_loaded > 0)
-		return node->loaded_info[0];
-
-	return node->unloaded_info;
-}
-
-/*
  * Return a newly allocated profiling data set which contains the sum of
  * all profiling data associated with the given node.
  */
@@ -232,39 +406,6 @@ static int gcov_seq_release(struct inode *inode, struct file *file)
 }
 
 /*
- * Find a node by the associated data file name. Needs to be called with
- * node_lock held.
- */
-static struct gcov_node *get_node_by_name(const char *name)
-{
-	struct gcov_node *node;
-	struct gcov_info *info;
-
-	list_for_each_entry(node, &all_head, all) {
-		info = get_node_info(node);
-		if (info && (strcmp(info->filename, name) == 0))
-			return node;
-	}
-
-	return NULL;
-}
-
-/*
- * Reset all profiling data associated with the specified node.
- */
-static void reset_node(struct gcov_node *node)
-{
-	int i;
-
-	if (node->unloaded_info)
-		gcov_info_reset(node->unloaded_info);
-	for (i = 0; i < node->num_loaded; i++)
-		gcov_info_reset(node->loaded_info[i]);
-}
-
-static void remove_node(struct gcov_node *node);
-
-/*
  * write() implementation for gcov data files. Reset profiling data for the
  * corresponding file. If all associated object files have been unloaded,
  * remove the debug fs node as well.
@@ -408,6 +549,26 @@ static const struct file_operations gcov_data_fops = {
 	.write		= gcov_seq_write,
 };
 
+#endif /* GCC_VERSION */
+
+/*
+ * Find a node by the associated data file name. Needs to be called with
+ * node_lock held.
+ */
+static struct gcov_node *get_node_by_name(const char *name)
+{
+	struct gcov_node *node;
+	struct gcov_info *info;
+
+	list_for_each_entry(node, &all_head, all) {
+		info = get_node_info(node);
+		if (info && (strcmp(info->filename, name) == 0))
+			return node;
+	}
+
+	return NULL;
+}
+
 /* Basic initialization of a new node. */
 static void init_node(struct gcov_node *node, struct gcov_info *info,
 		      const char *name, struct gcov_node *parent)
@@ -445,8 +606,13 @@ static struct gcov_node *new_node(struct gcov_node *parent,
 	init_node(node, info, name, parent);
 	/* Differentiate between gcov data file nodes and directory nodes. */
 	if (info) {
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3)
+		node->dentry = debugfs_create_file(node->name, 0600,
+					parent->dentry, node, &gcda_file_fops);
+#else /* GCC_VERSION_EQUAL(3, 4) */
 		node->dentry = debugfs_create_file(deskew(node->name), 0600,
 					parent->dentry, node, &gcov_data_fops);
+#endif
 	} else
 		node->dentry = debugfs_create_dir(node->name, parent->dentry);
 	if (!node->dentry) {
@@ -454,8 +620,13 @@ static struct gcov_node *new_node(struct gcov_node *parent,
 		kfree(node);
 		return NULL;
 	}
+
+#if GCC_VERSION_EQUAL(3, 4)
+	/* keep the original file name and don't link gcno and .c */
 	if (info)
 		add_links(node, parent->dentry);
+#endif
+
 	list_add(&node->list, &parent->children);
 	list_add(&node->all, &all_head);
 
@@ -496,6 +667,19 @@ static void release_node(struct gcov_node *node)
 	kfree(node);
 }
 
+/*
+ * Reset all profiling data associated with the specified node.
+ */
+static void reset_node(struct gcov_node *node)
+{
+	int i;
+
+	if (node->unloaded_info)
+		gcov_info_reset(node->unloaded_info);
+	for (i = 0; i < node->num_loaded; i++)
+		gcov_info_reset(node->loaded_info[i]);
+}
+
 /* Release node and empty parents. Needs to be called with node_lock held. */
 static void remove_node(struct gcov_node *node)
 {
@@ -787,3 +971,5 @@ err_remove:
 	return rc;
 }
 device_initcall(gcov_fs_init);
+
+#endif /* Not a supported gcc version. */
diff --git a/kernel/gcov/gcc.c b/kernel/gcov/gcc.c
new file mode 100644
index 0000000..784ac44
--- /dev/null
+++ b/kernel/gcov/gcc.c
@@ -0,0 +1,461 @@
+/*
+ *  This code provides functions to handle gcc's profiling data format
+ *  introduced with gcc 3.4. Future versions of gcc may change the gcov
+ *  format (as happened before), so all format-specific information needs
+ *  to be kept modular and easily exchangeable.
+ *
+ *  This file is based on gcc-internal definitions. Functions and data
+ *  structures are defined to be compatible with gcc counterparts.
+ *  For a better understanding, refer to gcc source: gcc/gcov-io.h.
+ *
+ *    Copyright IBM Corp. 2009
+ *    Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
+ *
+ *    Uses gcc-internal data definitions.
+ */
+
+#include "gcc_version_check.h"
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3) || GCC_VERSION_EQUAL(3, 4)
+
+#include <linux/errno.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/seq_file.h>
+#include <linux/vmalloc.h>
+#include "gcov.h"
+
+/* Symbolic links to be created for each profiling data file. */
+const struct gcov_link gcov_link[] = {
+	{ OBJ_TREE, "gcno" },	/* Link to .gcno file in $(objtree). */
+	{ 0, NULL},
+};
+
+/*
+ * Determine whether a counter is active. Based on gcc magic. Doesn't change
+ * at run-time.
+ */
+static int counter_active(struct gcov_info *info, unsigned int type)
+{
+	return (1 << type) & info->ctr_mask;
+}
+
+/* Determine number of active counters. Based on gcc magic. */
+static unsigned int num_counter_active(struct gcov_info *info)
+{
+	unsigned int i;
+	unsigned int result = 0;
+
+	for (i = 0; i < GCOV_COUNTERS; i++) {
+		if (counter_active(info, i))
+			result++;
+	}
+	return result;
+}
+
+/**
+ * gcov_info_reset - reset profiling data to zero
+ * @info: profiling data set
+ */
+void gcov_info_reset(struct gcov_info *info)
+{
+	unsigned int active = num_counter_active(info);
+	unsigned int i;
+
+	for (i = 0; i < active; i++) {
+		memset(info->counts[i].values, 0,
+		       info->counts[i].num * sizeof(gcov_type));
+	}
+}
+
+/**
+ * gcov_info_is_compatible - check if profiling data can be added
+ * @info1: first profiling data set
+ * @info2: second profiling data set
+ *
+ * Returns non-zero if profiling data can be added, zero otherwise.
+ */
+int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
+{
+	return (info1->stamp == info2->stamp);
+}
+
+/**
+ * gcov_info_add - add up profiling data
+ * @dest: profiling data set to which data is added
+ * @source: profiling data set which is added
+ *
+ * Adds profiling counts of @source to @dest.
+ */
+void gcov_info_add(struct gcov_info *dest, struct gcov_info *source)
+{
+	unsigned int i;
+	unsigned int j;
+
+	for (i = 0; i < num_counter_active(dest); i++) {
+		for (j = 0; j < dest->counts[i].num; j++) {
+			dest->counts[i].values[j] +=
+				source->counts[i].values[j];
+		}
+	}
+}
+
+/* Get size of function info entry. Based on gcc magic. */
+static size_t get_fn_size(struct gcov_info *info)
+{
+	size_t size;
+
+	size = sizeof(struct gcov_fn_info) + num_counter_active(info) *
+	       sizeof(unsigned int);
+	if (__alignof__(struct gcov_fn_info) > sizeof(unsigned int))
+		size = ALIGN(size, __alignof__(struct gcov_fn_info));
+	return size;
+}
+
+/**
+ * gcov_info_dup - duplicate profiling data set
+ * @info: profiling data set to duplicate
+ *
+ * Return newly allocated duplicate on success, %NULL on error.
+ */
+struct gcov_info *gcov_info_dup(struct gcov_info *info)
+{
+	struct gcov_info *dup;
+	unsigned int i;
+	unsigned int active;
+
+	/* Duplicate gcov_info. */
+	active = num_counter_active(info);
+	dup = kzalloc(sizeof(struct gcov_info) +
+		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);
+	if (!dup)
+		return NULL;
+
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3)
+	memcpy (dup, info, sizeof (struct gcov_info));
+	dup->next               = 0;
+	dup->mod_info           = 0;
+#else /* GCC_VERSION_EQUAL(3, 4) */
+	dup->version		= info->version;
+	dup->stamp		= info->stamp;
+	dup->n_functions	= info->n_functions;
+	dup->ctr_mask		= info->ctr_mask;
+#endif
+
+	/* Duplicate filename. */
+	dup->filename		= kstrdup(info->filename, GFP_KERNEL);
+	if (!dup->filename)
+		goto err_free;
+	/* Duplicate table of functions. */
+	dup->functions = kmemdup(info->functions, info->n_functions *
+				 get_fn_size(info), GFP_KERNEL);
+	if (!dup->functions)
+		goto err_free;
+	/* Duplicate counter arrays. */
+	for (i = 0; i < active ; i++) {
+		struct gcov_ctr_info *ctr = &info->counts[i];
+		size_t size = ctr->num * sizeof(gcov_type);
+
+		dup->counts[i].num = ctr->num;
+		dup->counts[i].merge = ctr->merge;
+		dup->counts[i].values = vmalloc(size);
+		if (!dup->counts[i].values)
+			goto err_free;
+		memcpy(dup->counts[i].values, ctr->values, size);
+	}
+	return dup;
+
+err_free:
+	gcov_info_free(dup);
+	return NULL;
+}
+
+/**
+ * gcov_info_free - release memory for profiling data set duplicate
+ * @info: profiling data set duplicate to free
+ */
+void gcov_info_free(struct gcov_info *info)
+{
+	unsigned int active = num_counter_active(info);
+	unsigned int i;
+
+	for (i = 0; i < active ; i++)
+		vfree(info->counts[i].values);
+	kfree(info->functions);
+	kfree(info->filename);
+	kfree(info);
+}
+
+#if GCC_VERSION_EQUAL(3, 4)
+/* Get address of function info entry. Based on gcc magic. */
+static struct gcov_fn_info *get_fn_info(struct gcov_info *info, unsigned int fn)
+{
+	return (struct gcov_fn_info *)
+		((char *) info->functions + fn * get_fn_size(info));
+}
+
+/**
+ * struct type_info - iterator helper array
+ * @ctr_type: counter type
+ * @offset: index of the first value of the current function for this type
+ *
+ * This array is needed to convert the in-memory data format into the in-file
+ * data format:
+ *
+ * In-memory:
+ *   for each counter type
+ *     for each function
+ *       values
+ *
+ * In-file:
+ *   for each function
+ *     for each counter type
+ *       values
+ *
+ * See gcc source gcc/gcov-io.h for more information on data organization.
+ */
+struct type_info {
+	int ctr_type;
+	unsigned int offset;
+};
+
+/**
+ * struct gcov_iterator - specifies current file position in logical records
+ * @info: associated profiling data
+ * @record: record type
+ * @function: function number
+ * @type: counter type
+ * @count: index into values array
+ * @num_types: number of counter types
+ * @type_info: helper array to get values-array offset for current function
+ */
+struct gcov_iterator {
+	struct gcov_info *info;
+
+	int record;
+	unsigned int function;
+	unsigned int type;
+	unsigned int count;
+
+	int num_types;
+	struct type_info type_info[0];
+};
+
+static struct gcov_fn_info *get_func(struct gcov_iterator *iter)
+{
+	return get_fn_info(iter->info, iter->function);
+}
+
+static struct type_info *get_type(struct gcov_iterator *iter)
+{
+	return &iter->type_info[iter->type];
+}
+
+/**
+ * gcov_iter_new - allocate and initialize profiling data iterator
+ * @info: profiling data set to be iterated
+ *
+ * Return file iterator on success, %NULL otherwise.
+ */
+struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
+{
+	struct gcov_iterator *iter;
+
+	iter = kzalloc(sizeof(struct gcov_iterator) +
+		       num_counter_active(info) * sizeof(struct type_info),
+		       GFP_KERNEL);
+	if (iter)
+		iter->info = info;
+
+	return iter;
+}
+
+/**
+ * gcov_iter_free - release memory for iterator
+ * @iter: file iterator to free
+ */
+void gcov_iter_free(struct gcov_iterator *iter)
+{
+	kfree(iter);
+}
+
+/**
+ * gcov_iter_get_info - return profiling data set for given file iterator
+ * @iter: file iterator
+ */
+struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
+{
+	return iter->info;
+}
+
+/**
+ * gcov_iter_start - reset file iterator to starting position
+ * @iter: file iterator
+ */
+void gcov_iter_start(struct gcov_iterator *iter)
+{
+	int i;
+
+	iter->record = 0;
+	iter->function = 0;
+	iter->type = 0;
+	iter->count = 0;
+	iter->num_types = 0;
+	for (i = 0; i < GCOV_COUNTERS; i++) {
+		if (counter_active(iter->info, i)) {
+			iter->type_info[iter->num_types].ctr_type = i;
+			iter->type_info[iter->num_types++].offset = 0;
+		}
+	}
+}
+
+/* Mapping of logical record number to actual file content. */
+#define RECORD_FILE_MAGIC	0
+#define RECORD_GCOV_VERSION	1
+#define RECORD_TIME_STAMP	2
+#define RECORD_FUNCTION_TAG	3
+#define RECORD_FUNCTON_TAG_LEN	4
+#define RECORD_FUNCTION_IDENT	5
+#define RECORD_FUNCTION_CHECK	6
+#define RECORD_COUNT_TAG	7
+#define RECORD_COUNT_LEN	8
+#define RECORD_COUNT		9
+
+/**
+ * gcov_iter_next - advance file iterator to next logical record
+ * @iter: file iterator
+ *
+ * Return zero if new position is valid, non-zero if iterator has reached end.
+ */
+int gcov_iter_next(struct gcov_iterator *iter)
+{
+	switch (iter->record) {
+	case RECORD_FILE_MAGIC:
+	case RECORD_GCOV_VERSION:
+	case RECORD_FUNCTION_TAG:
+	case RECORD_FUNCTON_TAG_LEN:
+	case RECORD_FUNCTION_IDENT:
+	case RECORD_COUNT_TAG:
+		/* Advance to next record */
+		iter->record++;
+		break;
+	case RECORD_COUNT:
+		/* Advance to next count */
+		iter->count++;
+		/* fall through */
+	case RECORD_COUNT_LEN:
+		if (iter->count < get_func(iter)->n_ctrs[iter->type]) {
+			iter->record = 9;
+			break;
+		}
+		/* Advance to next counter type */
+		get_type(iter)->offset += iter->count;
+		iter->count = 0;
+		iter->type++;
+		/* fall through */
+	case RECORD_FUNCTION_CHECK:
+		if (iter->type < iter->num_types) {
+			iter->record = 7;
+			break;
+		}
+		/* Advance to next function */
+		iter->type = 0;
+		iter->function++;
+		/* fall through */
+	case RECORD_TIME_STAMP:
+		if (iter->function < iter->info->n_functions)
+			iter->record = 3;
+		else
+			iter->record = -1;
+		break;
+	}
+	/* Check for EOF. */
+	if (iter->record == -1)
+		return -EINVAL;
+	else
+		return 0;
+}
+
+/**
+ * seq_write_gcov_u32 - write 32 bit number in gcov format to seq_file
+ * @seq: seq_file handle
+ * @v: value to be stored
+ *
+ * Number format defined by gcc: numbers are recorded in the 32 bit
+ * unsigned binary form of the endianness of the machine generating the
+ * file.
+ */
+static int seq_write_gcov_u32(struct seq_file *seq, u32 v)
+{
+	return seq_write(seq, &v, sizeof(v));
+}
+
+/**
+ * seq_write_gcov_u64 - write 64 bit number in gcov format to seq_file
+ * @seq: seq_file handle
+ * @v: value to be stored
+ *
+ * Number format defined by gcc: numbers are recorded in the 32 bit
+ * unsigned binary form of the endianness of the machine generating the
+ * file. 64 bit numbers are stored as two 32 bit numbers, the low part
+ * first.
+ */
+static int seq_write_gcov_u64(struct seq_file *seq, u64 v)
+{
+	u32 data[2];
+
+	data[0] = (v & 0xffffffffUL);
+	data[1] = (v >> 32);
+	return seq_write(seq, data, sizeof(data));
+}
+
+/**
+ * gcov_iter_write - write data for current pos to seq_file
+ * @iter: file iterator
+ * @seq: seq_file handle
+ *
+ * Return zero on success, non-zero otherwise.
+ */
+int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
+{
+	int rc = -EINVAL;
+
+	switch (iter->record) {
+	case RECORD_FILE_MAGIC:
+		rc = seq_write_gcov_u32(seq, GCOV_DATA_MAGIC);
+		break;
+	case RECORD_GCOV_VERSION:
+		rc = seq_write_gcov_u32(seq, iter->info->version);
+		break;
+	case RECORD_TIME_STAMP:
+		rc = seq_write_gcov_u32(seq, iter->info->stamp);
+		break;
+	case RECORD_FUNCTION_TAG:
+		rc = seq_write_gcov_u32(seq, GCOV_TAG_FUNCTION);
+		break;
+	case RECORD_FUNCTON_TAG_LEN:
+		rc = seq_write_gcov_u32(seq, 2);
+		break;
+	case RECORD_FUNCTION_IDENT:
+		rc = seq_write_gcov_u32(seq, get_func(iter)->ident);
+		break;
+	case RECORD_FUNCTION_CHECK:
+		rc = seq_write_gcov_u32(seq, get_func(iter)->checksum);
+		break;
+	case RECORD_COUNT_TAG:
+		rc = seq_write_gcov_u32(seq,
+			GCOV_TAG_FOR_COUNTER(get_type(iter)->ctr_type));
+		break;
+	case RECORD_COUNT_LEN:
+		rc = seq_write_gcov_u32(seq,
+				get_func(iter)->n_ctrs[iter->type] * 2);
+		break;
+	case RECORD_COUNT:
+		rc = seq_write_gcov_u64(seq,
+			iter->info->counts[iter->type].
+				values[iter->count + get_type(iter)->offset]);
+		break;
+	}
+	return rc;
+}
+#endif /* GCC_VERSION_EQUAL(3, 4) */
+#endif /* Not a supported gcc version. */
diff --git a/kernel/gcov/gcc_3_4.c b/kernel/gcov/gcc_3_4.c
deleted file mode 100644
index ae5bb42..0000000
--- a/kernel/gcov/gcc_3_4.c
+++ /dev/null
@@ -1,447 +0,0 @@
-/*
- *  This code provides functions to handle gcc's profiling data format
- *  introduced with gcc 3.4. Future versions of gcc may change the gcov
- *  format (as happened before), so all format-specific information needs
- *  to be kept modular and easily exchangeable.
- *
- *  This file is based on gcc-internal definitions. Functions and data
- *  structures are defined to be compatible with gcc counterparts.
- *  For a better understanding, refer to gcc source: gcc/gcov-io.h.
- *
- *    Copyright IBM Corp. 2009
- *    Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
- *
- *    Uses gcc-internal data definitions.
- */
-
-#include <linux/errno.h>
-#include <linux/slab.h>
-#include <linux/string.h>
-#include <linux/seq_file.h>
-#include <linux/vmalloc.h>
-#include "gcov.h"
-
-/* Symbolic links to be created for each profiling data file. */
-const struct gcov_link gcov_link[] = {
-	{ OBJ_TREE, "gcno" },	/* Link to .gcno file in $(objtree). */
-	{ 0, NULL},
-};
-
-/*
- * Determine whether a counter is active. Based on gcc magic. Doesn't change
- * at run-time.
- */
-static int counter_active(struct gcov_info *info, unsigned int type)
-{
-	return (1 << type) & info->ctr_mask;
-}
-
-/* Determine number of active counters. Based on gcc magic. */
-static unsigned int num_counter_active(struct gcov_info *info)
-{
-	unsigned int i;
-	unsigned int result = 0;
-
-	for (i = 0; i < GCOV_COUNTERS; i++) {
-		if (counter_active(info, i))
-			result++;
-	}
-	return result;
-}
-
-/**
- * gcov_info_reset - reset profiling data to zero
- * @info: profiling data set
- */
-void gcov_info_reset(struct gcov_info *info)
-{
-	unsigned int active = num_counter_active(info);
-	unsigned int i;
-
-	for (i = 0; i < active; i++) {
-		memset(info->counts[i].values, 0,
-		       info->counts[i].num * sizeof(gcov_type));
-	}
-}
-
-/**
- * gcov_info_is_compatible - check if profiling data can be added
- * @info1: first profiling data set
- * @info2: second profiling data set
- *
- * Returns non-zero if profiling data can be added, zero otherwise.
- */
-int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
-{
-	return (info1->stamp == info2->stamp);
-}
-
-/**
- * gcov_info_add - add up profiling data
- * @dest: profiling data set to which data is added
- * @source: profiling data set which is added
- *
- * Adds profiling counts of @source to @dest.
- */
-void gcov_info_add(struct gcov_info *dest, struct gcov_info *source)
-{
-	unsigned int i;
-	unsigned int j;
-
-	for (i = 0; i < num_counter_active(dest); i++) {
-		for (j = 0; j < dest->counts[i].num; j++) {
-			dest->counts[i].values[j] +=
-				source->counts[i].values[j];
-		}
-	}
-}
-
-/* Get size of function info entry. Based on gcc magic. */
-static size_t get_fn_size(struct gcov_info *info)
-{
-	size_t size;
-
-	size = sizeof(struct gcov_fn_info) + num_counter_active(info) *
-	       sizeof(unsigned int);
-	if (__alignof__(struct gcov_fn_info) > sizeof(unsigned int))
-		size = ALIGN(size, __alignof__(struct gcov_fn_info));
-	return size;
-}
-
-/* Get address of function info entry. Based on gcc magic. */
-static struct gcov_fn_info *get_fn_info(struct gcov_info *info, unsigned int fn)
-{
-	return (struct gcov_fn_info *)
-		((char *) info->functions + fn * get_fn_size(info));
-}
-
-/**
- * gcov_info_dup - duplicate profiling data set
- * @info: profiling data set to duplicate
- *
- * Return newly allocated duplicate on success, %NULL on error.
- */
-struct gcov_info *gcov_info_dup(struct gcov_info *info)
-{
-	struct gcov_info *dup;
-	unsigned int i;
-	unsigned int active;
-
-	/* Duplicate gcov_info. */
-	active = num_counter_active(info);
-	dup = kzalloc(sizeof(struct gcov_info) +
-		      sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);
-	if (!dup)
-		return NULL;
-	dup->version		= info->version;
-	dup->stamp		= info->stamp;
-	dup->n_functions	= info->n_functions;
-	dup->ctr_mask		= info->ctr_mask;
-	/* Duplicate filename. */
-	dup->filename		= kstrdup(info->filename, GFP_KERNEL);
-	if (!dup->filename)
-		goto err_free;
-	/* Duplicate table of functions. */
-	dup->functions = kmemdup(info->functions, info->n_functions *
-				 get_fn_size(info), GFP_KERNEL);
-	if (!dup->functions)
-		goto err_free;
-	/* Duplicate counter arrays. */
-	for (i = 0; i < active ; i++) {
-		struct gcov_ctr_info *ctr = &info->counts[i];
-		size_t size = ctr->num * sizeof(gcov_type);
-
-		dup->counts[i].num = ctr->num;
-		dup->counts[i].merge = ctr->merge;
-		dup->counts[i].values = vmalloc(size);
-		if (!dup->counts[i].values)
-			goto err_free;
-		memcpy(dup->counts[i].values, ctr->values, size);
-	}
-	return dup;
-
-err_free:
-	gcov_info_free(dup);
-	return NULL;
-}
-
-/**
- * gcov_info_free - release memory for profiling data set duplicate
- * @info: profiling data set duplicate to free
- */
-void gcov_info_free(struct gcov_info *info)
-{
-	unsigned int active = num_counter_active(info);
-	unsigned int i;
-
-	for (i = 0; i < active ; i++)
-		vfree(info->counts[i].values);
-	kfree(info->functions);
-	kfree(info->filename);
-	kfree(info);
-}
-
-/**
- * struct type_info - iterator helper array
- * @ctr_type: counter type
- * @offset: index of the first value of the current function for this type
- *
- * This array is needed to convert the in-memory data format into the in-file
- * data format:
- *
- * In-memory:
- *   for each counter type
- *     for each function
- *       values
- *
- * In-file:
- *   for each function
- *     for each counter type
- *       values
- *
- * See gcc source gcc/gcov-io.h for more information on data organization.
- */
-struct type_info {
-	int ctr_type;
-	unsigned int offset;
-};
-
-/**
- * struct gcov_iterator - specifies current file position in logical records
- * @info: associated profiling data
- * @record: record type
- * @function: function number
- * @type: counter type
- * @count: index into values array
- * @num_types: number of counter types
- * @type_info: helper array to get values-array offset for current function
- */
-struct gcov_iterator {
-	struct gcov_info *info;
-
-	int record;
-	unsigned int function;
-	unsigned int type;
-	unsigned int count;
-
-	int num_types;
-	struct type_info type_info[0];
-};
-
-static struct gcov_fn_info *get_func(struct gcov_iterator *iter)
-{
-	return get_fn_info(iter->info, iter->function);
-}
-
-static struct type_info *get_type(struct gcov_iterator *iter)
-{
-	return &iter->type_info[iter->type];
-}
-
-/**
- * gcov_iter_new - allocate and initialize profiling data iterator
- * @info: profiling data set to be iterated
- *
- * Return file iterator on success, %NULL otherwise.
- */
-struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
-{
-	struct gcov_iterator *iter;
-
-	iter = kzalloc(sizeof(struct gcov_iterator) +
-		       num_counter_active(info) * sizeof(struct type_info),
-		       GFP_KERNEL);
-	if (iter)
-		iter->info = info;
-
-	return iter;
-}
-
-/**
- * gcov_iter_free - release memory for iterator
- * @iter: file iterator to free
- */
-void gcov_iter_free(struct gcov_iterator *iter)
-{
-	kfree(iter);
-}
-
-/**
- * gcov_iter_get_info - return profiling data set for given file iterator
- * @iter: file iterator
- */
-struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
-{
-	return iter->info;
-}
-
-/**
- * gcov_iter_start - reset file iterator to starting position
- * @iter: file iterator
- */
-void gcov_iter_start(struct gcov_iterator *iter)
-{
-	int i;
-
-	iter->record = 0;
-	iter->function = 0;
-	iter->type = 0;
-	iter->count = 0;
-	iter->num_types = 0;
-	for (i = 0; i < GCOV_COUNTERS; i++) {
-		if (counter_active(iter->info, i)) {
-			iter->type_info[iter->num_types].ctr_type = i;
-			iter->type_info[iter->num_types++].offset = 0;
-		}
-	}
-}
-
-/* Mapping of logical record number to actual file content. */
-#define RECORD_FILE_MAGIC	0
-#define RECORD_GCOV_VERSION	1
-#define RECORD_TIME_STAMP	2
-#define RECORD_FUNCTION_TAG	3
-#define RECORD_FUNCTON_TAG_LEN	4
-#define RECORD_FUNCTION_IDENT	5
-#define RECORD_FUNCTION_CHECK	6
-#define RECORD_COUNT_TAG	7
-#define RECORD_COUNT_LEN	8
-#define RECORD_COUNT		9
-
-/**
- * gcov_iter_next - advance file iterator to next logical record
- * @iter: file iterator
- *
- * Return zero if new position is valid, non-zero if iterator has reached end.
- */
-int gcov_iter_next(struct gcov_iterator *iter)
-{
-	switch (iter->record) {
-	case RECORD_FILE_MAGIC:
-	case RECORD_GCOV_VERSION:
-	case RECORD_FUNCTION_TAG:
-	case RECORD_FUNCTON_TAG_LEN:
-	case RECORD_FUNCTION_IDENT:
-	case RECORD_COUNT_TAG:
-		/* Advance to next record */
-		iter->record++;
-		break;
-	case RECORD_COUNT:
-		/* Advance to next count */
-		iter->count++;
-		/* fall through */
-	case RECORD_COUNT_LEN:
-		if (iter->count < get_func(iter)->n_ctrs[iter->type]) {
-			iter->record = 9;
-			break;
-		}
-		/* Advance to next counter type */
-		get_type(iter)->offset += iter->count;
-		iter->count = 0;
-		iter->type++;
-		/* fall through */
-	case RECORD_FUNCTION_CHECK:
-		if (iter->type < iter->num_types) {
-			iter->record = 7;
-			break;
-		}
-		/* Advance to next function */
-		iter->type = 0;
-		iter->function++;
-		/* fall through */
-	case RECORD_TIME_STAMP:
-		if (iter->function < iter->info->n_functions)
-			iter->record = 3;
-		else
-			iter->record = -1;
-		break;
-	}
-	/* Check for EOF. */
-	if (iter->record == -1)
-		return -EINVAL;
-	else
-		return 0;
-}
-
-/**
- * seq_write_gcov_u32 - write 32 bit number in gcov format to seq_file
- * @seq: seq_file handle
- * @v: value to be stored
- *
- * Number format defined by gcc: numbers are recorded in the 32 bit
- * unsigned binary form of the endianness of the machine generating the
- * file.
- */
-static int seq_write_gcov_u32(struct seq_file *seq, u32 v)
-{
-	return seq_write(seq, &v, sizeof(v));
-}
-
-/**
- * seq_write_gcov_u64 - write 64 bit number in gcov format to seq_file
- * @seq: seq_file handle
- * @v: value to be stored
- *
- * Number format defined by gcc: numbers are recorded in the 32 bit
- * unsigned binary form of the endianness of the machine generating the
- * file. 64 bit numbers are stored as two 32 bit numbers, the low part
- * first.
- */
-static int seq_write_gcov_u64(struct seq_file *seq, u64 v)
-{
-	u32 data[2];
-
-	data[0] = (v & 0xffffffffUL);
-	data[1] = (v >> 32);
-	return seq_write(seq, data, sizeof(data));
-}
-
-/**
- * gcov_iter_write - write data for current pos to seq_file
- * @iter: file iterator
- * @seq: seq_file handle
- *
- * Return zero on success, non-zero otherwise.
- */
-int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
-{
-	int rc = -EINVAL;
-
-	switch (iter->record) {
-	case RECORD_FILE_MAGIC:
-		rc = seq_write_gcov_u32(seq, GCOV_DATA_MAGIC);
-		break;
-	case RECORD_GCOV_VERSION:
-		rc = seq_write_gcov_u32(seq, iter->info->version);
-		break;
-	case RECORD_TIME_STAMP:
-		rc = seq_write_gcov_u32(seq, iter->info->stamp);
-		break;
-	case RECORD_FUNCTION_TAG:
-		rc = seq_write_gcov_u32(seq, GCOV_TAG_FUNCTION);
-		break;
-	case RECORD_FUNCTON_TAG_LEN:
-		rc = seq_write_gcov_u32(seq, 2);
-		break;
-	case RECORD_FUNCTION_IDENT:
-		rc = seq_write_gcov_u32(seq, get_func(iter)->ident);
-		break;
-	case RECORD_FUNCTION_CHECK:
-		rc = seq_write_gcov_u32(seq, get_func(iter)->checksum);
-		break;
-	case RECORD_COUNT_TAG:
-		rc = seq_write_gcov_u32(seq,
-			GCOV_TAG_FOR_COUNTER(get_type(iter)->ctr_type));
-		break;
-	case RECORD_COUNT_LEN:
-		rc = seq_write_gcov_u32(seq,
-				get_func(iter)->n_ctrs[iter->type] * 2);
-		break;
-	case RECORD_COUNT:
-		rc = seq_write_gcov_u64(seq,
-			iter->info->counts[iter->type].
-				values[iter->count + get_type(iter)->offset]);
-		break;
-	}
-	return rc;
-}
diff --git a/kernel/gcov/gcc_version_check.h b/kernel/gcov/gcc_version_check.h
new file mode 100644
index 0000000..ebfd040
--- /dev/null
+++ b/kernel/gcov/gcc_version_check.h
@@ -0,0 +1,16 @@
+/* This file defines macros to check gcc version number. */
+
+#ifndef GCC_VERSION_CHECH_H
+#define GCC_VERSION_CHECH_H
+
+#define GCC_VERSION_GREATER_OR_EQUAL(major, minor, patch_level) \
+                                ((__GNUC__ > major) || \
+                                 ((__GNUC__ == major) && \
+                                  (__GNUC_MINOR__ > minor)) || \
+                                 ((__GNUC__ == major) && \
+                                  (__GNUC_MINOR__ == minor) && \
+                                  (__GNUC_PATCHLEVEL__ >= patch_level)))
+#define GCC_VERSION_EQUAL(major, minor) ((__GNUC__ == major) && \
+                                         (__GNUC_MINOR__ == minor))
+
+#endif /* ! GCC_VERSION_CHECH_H */
diff --git a/kernel/gcov/gcov.h b/kernel/gcov/gcov.h
index 060073e..37d76ea 100644
--- a/kernel/gcov/gcov.h
+++ b/kernel/gcov/gcov.h
@@ -14,8 +14,19 @@
 #ifndef GCOV_H
 #define GCOV_H GCOV_H
 
+#include "gcc_version_check.h"
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3) || GCC_VERSION_EQUAL(3, 4)
+
 #include <linux/types.h>
 
+#if GCC_VERSION_GREATER_OR_EQUAL(4, 4, 3)
+
+#define IN_LIBGCOV 1
+#define IN_GCOV 0
+/* this gcov-io.h file is copied from gcc source tree */
+#include "gcov-src/gcov-io.h"
+
+#else /* GCC_VERSION_EQUAL(3, 4) */
 /*
  * Profiling data types used for gcc 3.4 and above - these are defined by
  * gcc and need to be kept as close to the original definition as possible to
@@ -88,6 +99,7 @@ struct gcov_info {
 	unsigned int			ctr_mask;
 	struct gcov_ctr_info		counts[0];
 };
+#endif /* GCC_VERSION */
 
 /* Base interface. */
 enum gcov_action {
@@ -125,4 +137,5 @@ struct gcov_link {
 };
 extern const struct gcov_link gcov_link[];
 
+#endif /* GCC_VERSION */
 #endif /* GCOV_H */
-- 
1.7.3.1

