[PATCH v2 29/62] objtool: Mark prefix functions

Josh Poimboeuf posted 62 patches 7 months, 1 week ago
There is a newer version of this series
[PATCH v2 29/62] objtool: Mark prefix functions
Posted by Josh Poimboeuf 7 months, 1 week ago
In preparation for the objtool klp diff subcommand, introduce a flag to
identify __pfx_*() and __cfi_*() functions in advance so they don't need
to be manually identified every time a check is needed.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
 tools/objtool/check.c               | 3 +--
 tools/objtool/elf.c                 | 5 +++++
 tools/objtool/include/objtool/elf.h | 6 ++++++
 3 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index a2a025ec57e8..6b2e57d9aaf8 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -3601,8 +3601,7 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
 
 		if (func && insn_func(insn) && func != insn_func(insn)->pfunc) {
 			/* Ignore KCFI type preambles, which always fall through */
-			if (!strncmp(func->name, "__cfi_", 6) ||
-			    !strncmp(func->name, "__pfx_", 6))
+			if (is_prefix_func(func))
 				return 0;
 
 			if (file->ignore_unreachables)
diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 59568381486c..9a1fc0392b7f 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -442,6 +442,11 @@ static void elf_add_symbol(struct elf *elf, struct symbol *sym)
 	elf_hash_add(symbol, &sym->hash, sym->idx);
 	elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name));
 
+	if (is_func_sym(sym) && sym->len == 16 &&
+	    (strstarts(sym->name, "__pfx") || strstarts(sym->name, "__cfi_")))
+		sym->prefix = 1;
+
+
 	if (is_func_sym(sym) && strstr(sym->name, ".cold"))
 		sym->cold = 1;
 	sym->pfunc = sym->cfunc = sym;
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index f41496b0ad8f..842faec1b9a9 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -72,6 +72,7 @@ struct symbol {
 	u8 frame_pointer     : 1;
 	u8 ignore	     : 1;
 	u8 cold		     : 1;
+	u8 prefix	     : 1;
 	struct list_head pv_target;
 	struct reloc *relocs;
 	struct section *group_sec;
@@ -229,6 +230,11 @@ static inline bool is_local_sym(struct symbol *sym)
 	return sym->bind == STB_LOCAL;
 }
 
+static inline bool is_prefix_func(struct symbol *sym)
+{
+	return is_func_sym(sym) && sym->prefix;
+}
+
 static inline bool is_reloc_sec(struct section *sec)
 {
 	return sec->sh.sh_type == SHT_RELA || sec->sh.sh_type == SHT_REL;
-- 
2.49.0
Re: [PATCH v2 29/62] objtool: Mark prefix functions
Posted by Peter Zijlstra 6 months, 3 weeks ago
On Fri, May 09, 2025 at 01:16:53PM -0700, Josh Poimboeuf wrote:
> In preparation for the objtool klp diff subcommand, introduce a flag to
> identify __pfx_*() and __cfi_*() functions in advance so they don't need
> to be manually identified every time a check is needed.
> 
> Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
> ---
>  tools/objtool/check.c               | 3 +--
>  tools/objtool/elf.c                 | 5 +++++
>  tools/objtool/include/objtool/elf.h | 6 ++++++
>  3 files changed, 12 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> index a2a025ec57e8..6b2e57d9aaf8 100644
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -3601,8 +3601,7 @@ static int validate_branch(struct objtool_file *file, struct symbol *func,
>  
>  		if (func && insn_func(insn) && func != insn_func(insn)->pfunc) {
>  			/* Ignore KCFI type preambles, which always fall through */
> -			if (!strncmp(func->name, "__cfi_", 6) ||
> -			    !strncmp(func->name, "__pfx_", 6))
> +			if (is_prefix_func(func))
>  				return 0;
>  
>  			if (file->ignore_unreachables)
> diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
> index 59568381486c..9a1fc0392b7f 100644
> --- a/tools/objtool/elf.c
> +++ b/tools/objtool/elf.c
> @@ -442,6 +442,11 @@ static void elf_add_symbol(struct elf *elf, struct symbol *sym)
>  	elf_hash_add(symbol, &sym->hash, sym->idx);
>  	elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name));
>  
> +	if (is_func_sym(sym) && sym->len == 16 &&

Where did that 'sym->len == 16' thing come from? I mean, they are, but
the old code didn't assert that.

I would rather objtool issue a warn if not 16, but still consider these
as prefix.

> +	    (strstarts(sym->name, "__pfx") || strstarts(sym->name, "__cfi_")))
> +		sym->prefix = 1;
> +
> +
>  	if (is_func_sym(sym) && strstr(sym->name, ".cold"))
>  		sym->cold = 1;
>  	sym->pfunc = sym->cfunc = sym;
Re: [PATCH v2 29/62] objtool: Mark prefix functions
Posted by Josh Poimboeuf 6 months, 2 weeks ago
On Mon, May 26, 2025 at 12:43:55PM +0200, Peter Zijlstra wrote:
> > +++ b/tools/objtool/elf.c
> > @@ -442,6 +442,11 @@ static void elf_add_symbol(struct elf *elf, struct symbol *sym)
> >  	elf_hash_add(symbol, &sym->hash, sym->idx);
> >  	elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name));
> >  
> > +	if (is_func_sym(sym) && sym->len == 16 &&
> 
> Where did that 'sym->len == 16' thing come from? I mean, they are, but
> the old code didn't assert that.
> 
> I would rather objtool issue a warn if not 16, but still consider these
> as prefix.

Ok:

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 9a1fc0392b7f..2953aa8d2b81 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -442,10 +442,12 @@ static void elf_add_symbol(struct elf *elf, struct symbol *sym)
 	elf_hash_add(symbol, &sym->hash, sym->idx);
 	elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name));
 
-	if (is_func_sym(sym) && sym->len == 16 &&
-	    (strstarts(sym->name, "__pfx") || strstarts(sym->name, "__cfi_")))
+	if (is_func_sym(sym) &&
+	    (strstarts(sym->name, "__pfx_") || strstarts(sym->name, "__cfi_"))) {
+		if (sym->len != 16)
+			WARN("%s size %d != 16", sym->name, sym->len);
 		sym->prefix = 1;
-
+	}
 
 	if (is_func_sym(sym) && strstr(sym->name, ".cold"))
 		sym->cold = 1;
Re: [PATCH v2 29/62] objtool: Mark prefix functions
Posted by Josh Poimboeuf 5 months, 3 weeks ago
On Wed, Jun 04, 2025 at 05:04:33PM -0700, Josh Poimboeuf wrote:
> On Mon, May 26, 2025 at 12:43:55PM +0200, Peter Zijlstra wrote:
> > > +++ b/tools/objtool/elf.c
> > > @@ -442,6 +442,11 @@ static void elf_add_symbol(struct elf *elf, struct symbol *sym)
> > >  	elf_hash_add(symbol, &sym->hash, sym->idx);
> > >  	elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name));
> > >  
> > > +	if (is_func_sym(sym) && sym->len == 16 &&
> > 
> > Where did that 'sym->len == 16' thing come from? I mean, they are, but
> > the old code didn't assert that.
> > 
> > I would rather objtool issue a warn if not 16, but still consider these
> > as prefix.
> 
> Ok:

Turns out there are kCFI cases with prefixes != 16, so I omitted the
size check altogether.

-- 
Josh