2011-05-11 17:23:36

by Rabin Vincent

[permalink] [raw]
Subject: [PATCH] ftrace: recordmcount: avoid STT_FUNC symbols as base on ARM

While find_secsym_ndx often finds the unamed local STT_SECTION, if a
section has only one function in it, the ARM toolchain generates the
STT_FUNC symbol before the STT_SECTION, and recordmcount finds this
instead.

This is problematic on ARM because in ARM ELFs, "if a [STT_FUNC] symbol
addresses a Thumb instruction, its value is the address of the
instruction with bit zero set (in a relocatable object, the section
offset with bit zero set)". This leads to incorrect mcount addresses
being recorded.

Fix this by not using STT_FUNC symbols as the base on ARM.

Signed-off-by: Rabin Vincent <[email protected]>
---
scripts/recordmcount.h | 8 ++++++++
1 files changed, 8 insertions(+), 0 deletions(-)

diff --git a/scripts/recordmcount.h b/scripts/recordmcount.h
index baf187b..0e2944a 100644
--- a/scripts/recordmcount.h
+++ b/scripts/recordmcount.h
@@ -39,6 +39,7 @@
#undef ELF_R_INFO
#undef Elf_r_info
#undef ELF_ST_BIND
+#undef ELF_ST_TYPE
#undef fn_ELF_R_SYM
#undef fn_ELF_R_INFO
#undef uint_t
@@ -68,6 +69,7 @@
# define ELF_R_INFO ELF64_R_INFO
# define Elf_r_info Elf64_r_info
# define ELF_ST_BIND ELF64_ST_BIND
+# define ELF_ST_TYPE ELF64_ST_TYPE
# define fn_ELF_R_SYM fn_ELF64_R_SYM
# define fn_ELF_R_INFO fn_ELF64_R_INFO
# define uint_t uint64_t
@@ -96,6 +98,7 @@
# define ELF_R_INFO ELF32_R_INFO
# define Elf_r_info Elf32_r_info
# define ELF_ST_BIND ELF32_ST_BIND
+# define ELF_ST_TYPE ELF32_ST_TYPE
# define fn_ELF_R_SYM fn_ELF32_R_SYM
# define fn_ELF_R_INFO fn_ELF32_R_INFO
# define uint_t uint32_t
@@ -333,6 +336,11 @@ static unsigned find_secsym_ndx(unsigned const txtndx,
if (txtndx == w2(symp->st_shndx)
/* avoid STB_WEAK */
&& (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
+ /* function symbols on ARM have quirks, avoid them */
+ if (w2(ehdr->e_machine) == EM_ARM
+ && ELF_ST_TYPE(symp->st_info) == STT_FUNC)
+ continue;
+
*recvalp = _w(symp->st_value);
return symp - sym0;
}
--
1.7.4.1


2011-05-17 16:10:32

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH] ftrace: recordmcount: avoid STT_FUNC symbols as base on ARM

On Wed, 2011-05-11 at 22:53 +0530, Rabin Vincent wrote:
> While find_secsym_ndx often finds the unamed local STT_SECTION, if a
> section has only one function in it, the ARM toolchain generates the
> STT_FUNC symbol before the STT_SECTION, and recordmcount finds this
> instead.
>
> This is problematic on ARM because in ARM ELFs, "if a [STT_FUNC] symbol
> addresses a Thumb instruction, its value is the address of the
> instruction with bit zero set (in a relocatable object, the section
> offset with bit zero set)". This leads to incorrect mcount addresses
> being recorded.
>
> Fix this by not using STT_FUNC symbols as the base on ARM.

Hi Russell,

Can you give me an Acked-by on this patch?

Thanks,

-- Steve

>
> Signed-off-by: Rabin Vincent <[email protected]>
> ---
> scripts/recordmcount.h | 8 ++++++++
> 1 files changed, 8 insertions(+), 0 deletions(-)
>
> diff --git a/scripts/recordmcount.h b/scripts/recordmcount.h
> index baf187b..0e2944a 100644
> --- a/scripts/recordmcount.h
> +++ b/scripts/recordmcount.h
> @@ -39,6 +39,7 @@
> #undef ELF_R_INFO
> #undef Elf_r_info
> #undef ELF_ST_BIND
> +#undef ELF_ST_TYPE
> #undef fn_ELF_R_SYM
> #undef fn_ELF_R_INFO
> #undef uint_t
> @@ -68,6 +69,7 @@
> # define ELF_R_INFO ELF64_R_INFO
> # define Elf_r_info Elf64_r_info
> # define ELF_ST_BIND ELF64_ST_BIND
> +# define ELF_ST_TYPE ELF64_ST_TYPE
> # define fn_ELF_R_SYM fn_ELF64_R_SYM
> # define fn_ELF_R_INFO fn_ELF64_R_INFO
> # define uint_t uint64_t
> @@ -96,6 +98,7 @@
> # define ELF_R_INFO ELF32_R_INFO
> # define Elf_r_info Elf32_r_info
> # define ELF_ST_BIND ELF32_ST_BIND
> +# define ELF_ST_TYPE ELF32_ST_TYPE
> # define fn_ELF_R_SYM fn_ELF32_R_SYM
> # define fn_ELF_R_INFO fn_ELF32_R_INFO
> # define uint_t uint32_t
> @@ -333,6 +336,11 @@ static unsigned find_secsym_ndx(unsigned const txtndx,
> if (txtndx == w2(symp->st_shndx)
> /* avoid STB_WEAK */
> && (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
> + /* function symbols on ARM have quirks, avoid them */
> + if (w2(ehdr->e_machine) == EM_ARM
> + && ELF_ST_TYPE(symp->st_info) == STT_FUNC)
> + continue;
> +
> *recvalp = _w(symp->st_value);
> return symp - sym0;
> }

2011-05-17 22:50:38

by Russell King - ARM Linux

[permalink] [raw]
Subject: Re: [PATCH] ftrace: recordmcount: avoid STT_FUNC symbols as base on ARM

On Tue, May 17, 2011 at 12:10:29PM -0400, Steven Rostedt wrote:
> On Wed, 2011-05-11 at 22:53 +0530, Rabin Vincent wrote:
> > While find_secsym_ndx often finds the unamed local STT_SECTION, if a
> > section has only one function in it, the ARM toolchain generates the
> > STT_FUNC symbol before the STT_SECTION, and recordmcount finds this
> > instead.
> >
> > This is problematic on ARM because in ARM ELFs, "if a [STT_FUNC] symbol
> > addresses a Thumb instruction, its value is the address of the
> > instruction with bit zero set (in a relocatable object, the section
> > offset with bit zero set)". This leads to incorrect mcount addresses
> > being recorded.
> >
> > Fix this by not using STT_FUNC symbols as the base on ARM.
>
> Hi Russell,
>
> Can you give me an Acked-by on this patch?

It would be better for someone who knows the ELF format for ARM and
its weirdnesses to ack this patch. I'm afraid my ARM ELF format
knowledge was been superseded when EABI came along and introduced
all sorts of new stuff.

2011-05-18 00:27:19

by Steven Rostedt

[permalink] [raw]
Subject: Re: [PATCH] ftrace: recordmcount: avoid STT_FUNC symbols as base on ARM

On Tue, 2011-05-17 at 23:50 +0100, Russell King - ARM Linux wrote:
> On Tue, May 17, 2011 at 12:10:29PM -0400, Steven Rostedt wrote:
> > On Wed, 2011-05-11 at 22:53 +0530, Rabin Vincent wrote:
> > > While find_secsym_ndx often finds the unamed local STT_SECTION, if a
> > > section has only one function in it, the ARM toolchain generates the
> > > STT_FUNC symbol before the STT_SECTION, and recordmcount finds this
> > > instead.
> > >
> > > This is problematic on ARM because in ARM ELFs, "if a [STT_FUNC] symbol
> > > addresses a Thumb instruction, its value is the address of the
> > > instruction with bit zero set (in a relocatable object, the section
> > > offset with bit zero set)". This leads to incorrect mcount addresses
> > > being recorded.
> > >
> > > Fix this by not using STT_FUNC symbols as the base on ARM.
> >
> > Hi Russell,
> >
> > Can you give me an Acked-by on this patch?
>
> It would be better for someone who knows the ELF format for ARM and
> its weirdnesses to ack this patch. I'm afraid my ARM ELF format
> knowledge was been superseded when EABI came along and introduced
> all sorts of new stuff.

OK, in that case, I'll just apply the patch.

Thanks!

-- Steve

2011-05-27 12:47:07

by Rabin Vincent

[permalink] [raw]
Subject: [tip:perf/urgent] ftrace/recordmcount: Avoid STT_FUNC symbols as base on ARM

Commit-ID: 9905ce8ad7b79dddd23c7b4753d0b2cdb65bde3c
Gitweb: http://git.kernel.org/tip/9905ce8ad7b79dddd23c7b4753d0b2cdb65bde3c
Author: Rabin Vincent <[email protected]>
AuthorDate: Wed, 11 May 2011 22:53:51 +0530
Committer: Steven Rostedt <[email protected]>
CommitDate: Wed, 25 May 2011 19:56:33 -0400

ftrace/recordmcount: Avoid STT_FUNC symbols as base on ARM

While find_secsym_ndx often finds the unamed local STT_SECTION, if a
section has only one function in it, the ARM toolchain generates the
STT_FUNC symbol before the STT_SECTION, and recordmcount finds this
instead.

This is problematic on ARM because in ARM ELFs, "if a [STT_FUNC] symbol
addresses a Thumb instruction, its value is the address of the
instruction with bit zero set (in a relocatable object, the section
offset with bit zero set)". This leads to incorrect mcount addresses
being recorded.

Fix this by not using STT_FUNC symbols as the base on ARM.

Signed-off-by: Rabin Vincent <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Steven Rostedt <[email protected]>
---
scripts/recordmcount.h | 8 ++++++++
1 files changed, 8 insertions(+), 0 deletions(-)

diff --git a/scripts/recordmcount.h b/scripts/recordmcount.h
index 4be6036..f40a6af6 100644
--- a/scripts/recordmcount.h
+++ b/scripts/recordmcount.h
@@ -43,6 +43,7 @@
#undef ELF_R_INFO
#undef Elf_r_info
#undef ELF_ST_BIND
+#undef ELF_ST_TYPE
#undef fn_ELF_R_SYM
#undef fn_ELF_R_INFO
#undef uint_t
@@ -76,6 +77,7 @@
# define ELF_R_INFO ELF64_R_INFO
# define Elf_r_info Elf64_r_info
# define ELF_ST_BIND ELF64_ST_BIND
+# define ELF_ST_TYPE ELF64_ST_TYPE
# define fn_ELF_R_SYM fn_ELF64_R_SYM
# define fn_ELF_R_INFO fn_ELF64_R_INFO
# define uint_t uint64_t
@@ -108,6 +110,7 @@
# define ELF_R_INFO ELF32_R_INFO
# define Elf_r_info Elf32_r_info
# define ELF_ST_BIND ELF32_ST_BIND
+# define ELF_ST_TYPE ELF32_ST_TYPE
# define fn_ELF_R_SYM fn_ELF32_R_SYM
# define fn_ELF_R_INFO fn_ELF32_R_INFO
# define uint_t uint32_t
@@ -427,6 +430,11 @@ static unsigned find_secsym_ndx(unsigned const txtndx,
if (txtndx == w2(symp->st_shndx)
/* avoid STB_WEAK */
&& (STB_LOCAL == st_bind || STB_GLOBAL == st_bind)) {
+ /* function symbols on ARM have quirks, avoid them */
+ if (w2(ehdr->e_machine) == EM_ARM
+ && ELF_ST_TYPE(symp->st_info) == STT_FUNC)
+ continue;
+
*recvalp = _w(symp->st_value);
return symp - sym0;
}