[PATCH v4 01/10] objtool: Handle various symbol types of rodata

Tiezhu Yang posted 10 patches 1 year, 2 months ago
There is a newer version of this series
[PATCH v4 01/10] objtool: Handle various symbol types of rodata
Posted by Tiezhu Yang 1 year, 2 months ago
In the relocation section ".rela.rodata" of each .o file compiled with
LoongArch toolchain, there are various symbol types such as STT_NOTYPE,
STT_OBJECT, STT_FUNC in addition to the usual STT_SECTION, it needs to
use reloc symbol offset instead of reloc addend to find the destination
instruction in find_jump_table() and add_jump_table().

This is preparation for later patch on LoongArch, there is no effect for
the other archs with this patch.

Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 tools/objtool/check.c | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 6604f5d038aa..9601235e908d 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2079,6 +2079,7 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
 	unsigned int prev_offset = 0;
 	struct reloc *reloc = table;
 	struct alternative *alt;
+	unsigned long offset;
 
 	/*
 	 * Each @reloc is a switch table relocation which points to the target
@@ -2094,12 +2095,19 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
 		if (prev_offset && reloc_offset(reloc) != prev_offset + 8)
 			break;
 
+		if (reloc->sym->type == STT_SECTION) {
+			/* Addend field in the relocation entry associated with the symbol */
+			offset = reloc_addend(reloc);
+		} else {
+			/* The address of the symbol in the relocation entry */
+			offset = reloc->sym->offset;
+		}
+
 		/* Detect function pointers from contiguous objects: */
-		if (reloc->sym->sec == pfunc->sec &&
-		    reloc_addend(reloc) == pfunc->offset)
+		if (reloc->sym->sec == pfunc->sec && offset == pfunc->offset)
 			break;
 
-		dest_insn = find_insn(file, reloc->sym->sec, reloc_addend(reloc));
+		dest_insn = find_insn(file, reloc->sym->sec, offset);
 		if (!dest_insn)
 			break;
 
@@ -2137,6 +2145,7 @@ static struct reloc *find_jump_table(struct objtool_file *file,
 {
 	struct reloc *table_reloc;
 	struct instruction *dest_insn, *orig_insn = insn;
+	unsigned long offset;
 
 	/*
 	 * Backward search using the @first_jump_src links, these help avoid
@@ -2160,7 +2169,16 @@ static struct reloc *find_jump_table(struct objtool_file *file,
 		table_reloc = arch_find_switch_table(file, insn);
 		if (!table_reloc)
 			continue;
-		dest_insn = find_insn(file, table_reloc->sym->sec, reloc_addend(table_reloc));
+
+		if (table_reloc->sym->type == STT_SECTION) {
+			/* Addend field in the relocation entry associated with the symbol */
+			offset = reloc_addend(table_reloc);
+		} else {
+			/* The address of the symbol in the relocation entry */
+			offset = table_reloc->sym->offset;
+		}
+
+		dest_insn = find_insn(file, table_reloc->sym->sec, offset);
 		if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != func)
 			continue;
 
-- 
2.42.0
Re: [PATCH v4 01/10] objtool: Handle various symbol types of rodata
Posted by Josh Poimboeuf 1 year, 2 months ago
On Fri, Nov 22, 2024 at 12:49:56PM +0800, Tiezhu Yang wrote:
> @@ -2094,12 +2095,19 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
>  		if (prev_offset && reloc_offset(reloc) != prev_offset + 8)
>  			break;
>  
> +		if (reloc->sym->type == STT_SECTION) {
> +			/* Addend field in the relocation entry associated with the symbol */
> +			offset = reloc_addend(reloc);
> +		} else {
> +			/* The address of the symbol in the relocation entry */
> +			offset = reloc->sym->offset;

The comments don't seem helpful.

In the case of STT_SECTION, sym->offset is always zero.  Therefore the
if-else can be converted to a simple unconditional statement:

	offset = reloc->sym->offset + reloc_addend(reloc);

'prev_offset' needs to be updated as well.

> @@ -2137,6 +2145,7 @@ static struct reloc *find_jump_table(struct objtool_file *file,
>  {
>  	struct reloc *table_reloc;
>  	struct instruction *dest_insn, *orig_insn = insn;
> +	unsigned long offset;
>  
>  	/*
>  	 * Backward search using the @first_jump_src links, these help avoid
> @@ -2160,7 +2169,16 @@ static struct reloc *find_jump_table(struct objtool_file *file,
>  		table_reloc = arch_find_switch_table(file, insn);
>  		if (!table_reloc)
>  			continue;
> -		dest_insn = find_insn(file, table_reloc->sym->sec, reloc_addend(table_reloc));
> +
> +		if (table_reloc->sym->type == STT_SECTION) {
> +			/* Addend field in the relocation entry associated with the symbol */
> +			offset = reloc_addend(table_reloc);
> +		} else {
> +			/* The address of the symbol in the relocation entry */
> +			offset = table_reloc->sym->offset;
> +		}

Same comment here.

-- 
Josh
Re: [PATCH v4 01/10] objtool: Handle various symbol types of rodata
Posted by Tiezhu Yang 1 year, 2 months ago
On 11/26/2024 02:44 PM, Josh Poimboeuf wrote:
> On Fri, Nov 22, 2024 at 12:49:56PM +0800, Tiezhu Yang wrote:
>> @@ -2094,12 +2095,19 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
>>  		if (prev_offset && reloc_offset(reloc) != prev_offset + 8)
>>  			break;
>>
>> +		if (reloc->sym->type == STT_SECTION) {
>> +			/* Addend field in the relocation entry associated with the symbol */
>> +			offset = reloc_addend(reloc);
>> +		} else {
>> +			/* The address of the symbol in the relocation entry */
>> +			offset = reloc->sym->offset;
>
> The comments don't seem helpful.

Will remove it.

>
> In the case of STT_SECTION, sym->offset is always zero.  Therefore the
> if-else can be converted to a simple unconditional statement:
>
> 	offset = reloc->sym->offset + reloc_addend(reloc);

OK, let me test it.

>
> 'prev_offset' needs to be updated as well.

I am not sure I understand your comment correctly, I can not see
what should to do about 'prev_offset'.

>
>> @@ -2137,6 +2145,7 @@ static struct reloc *find_jump_table(struct objtool_file *file,
>>  {
>>  	struct reloc *table_reloc;
>>  	struct instruction *dest_insn, *orig_insn = insn;
>> +	unsigned long offset;
>>
>>  	/*
>>  	 * Backward search using the @first_jump_src links, these help avoid
>> @@ -2160,7 +2169,16 @@ static struct reloc *find_jump_table(struct objtool_file *file,
>>  		table_reloc = arch_find_switch_table(file, insn);
>>  		if (!table_reloc)
>>  			continue;
>> -		dest_insn = find_insn(file, table_reloc->sym->sec, reloc_addend(table_reloc));
>> +
>> +		if (table_reloc->sym->type == STT_SECTION) {
>> +			/* Addend field in the relocation entry associated with the symbol */
>> +			offset = reloc_addend(table_reloc);
>> +		} else {
>> +			/* The address of the symbol in the relocation entry */
>> +			offset = table_reloc->sym->offset;
>> +		}
>
> Same comment here.

OK, will do it.

Thanks,
Tiezhu
Re: [PATCH v4 01/10] objtool: Handle various symbol types of rodata
Posted by Josh Poimboeuf 1 year, 2 months ago
On Tue, Nov 26, 2024 at 06:41:29PM +0800, Tiezhu Yang wrote:
> On 11/26/2024 02:44 PM, Josh Poimboeuf wrote:
> > On Fri, Nov 22, 2024 at 12:49:56PM +0800, Tiezhu Yang wrote:
> > > @@ -2094,12 +2095,19 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
> >
> > 'prev_offset' needs to be updated as well.
> 
> I am not sure I understand your comment correctly, I can not see
> what should to do about 'prev_offset'.

Further down the function there is

  prev_offset = reloc_offset(reloc);

which needs to be changed to

  prev_offset = offset;

as part of the patch.

-- 
Josh
Re: [PATCH v4 01/10] objtool: Handle various symbol types of rodata
Posted by Tiezhu Yang 1 year, 2 months ago
On 11/27/2024 08:52 AM, Josh Poimboeuf wrote:
> On Tue, Nov 26, 2024 at 06:41:29PM +0800, Tiezhu Yang wrote:
>> On 11/26/2024 02:44 PM, Josh Poimboeuf wrote:
>>> On Fri, Nov 22, 2024 at 12:49:56PM +0800, Tiezhu Yang wrote:
>>>> @@ -2094,12 +2095,19 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
>>>
>>> 'prev_offset' needs to be updated as well.
>>
>> I am not sure I understand your comment correctly, I can not see
>> what should to do about 'prev_offset'.
>
> Further down the function there is
>
>   prev_offset = reloc_offset(reloc);
>
> which needs to be changed to
>
>   prev_offset = offset;
>
> as part of the patch.

If I understand correctly, reloc_offset(reloc) is different with
reloc->sym->offset + reloc_addend(reloc), tested on x86 and readelf
shows that their values are different, reloc_offset(reloc) is the
first column of .rela.rodata, reloc->sym->offset is the second to
last column of .rela.rodata, reloc_addend(reloc) is the last column
of .rela.rodata.

If do the above change as you suggested, there will be some objtool
warnings on x86. I think it should be:

   prev_offset = reloc_offset(reloc);

rather than:

   prev_offset = offset;

That is to say, no need to change "prev_offset".
Could you please check it again, please let me know if I am wrong.

Thanks,
Tiezhu
Re: [PATCH v4 01/10] objtool: Handle various symbol types of rodata
Posted by Josh Poimboeuf 1 year, 2 months ago
On Wed, Nov 27, 2024 at 02:39:13PM +0800, Tiezhu Yang wrote:
> On 11/27/2024 08:52 AM, Josh Poimboeuf wrote:
> > On Tue, Nov 26, 2024 at 06:41:29PM +0800, Tiezhu Yang wrote:
> > > On 11/26/2024 02:44 PM, Josh Poimboeuf wrote:
> > > > On Fri, Nov 22, 2024 at 12:49:56PM +0800, Tiezhu Yang wrote:
> > > > > @@ -2094,12 +2095,19 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
> > > > 
> > > > 'prev_offset' needs to be updated as well.
> > > 
> > > I am not sure I understand your comment correctly, I can not see
> > > what should to do about 'prev_offset'.
> > 
> > Further down the function there is
> > 
> >   prev_offset = reloc_offset(reloc);
> > 
> > which needs to be changed to
> > 
> >   prev_offset = offset;
> > 
> > as part of the patch.
> 
> If I understand correctly, reloc_offset(reloc) is different with
> reloc->sym->offset + reloc_addend(reloc), tested on x86 and readelf
> shows that their values are different, reloc_offset(reloc) is the
> first column of .rela.rodata, reloc->sym->offset is the second to
> last column of .rela.rodata, reloc_addend(reloc) is the last column
> of .rela.rodata.
> 
> If do the above change as you suggested, there will be some objtool
> warnings on x86. I think it should be:
> 
>   prev_offset = reloc_offset(reloc);
> 
> rather than:
> 
>   prev_offset = offset;
> 
> That is to say, no need to change "prev_offset".
> Could you please check it again, please let me know if I am wrong.

Sorry, I was confused by the fact there are two different meanings for
"offset": one for where the relocation is written, and one for the
symbol it refers to.

How about instead of 'offset', call it 'sym_offset'?

-- 
Josh
Re: [PATCH v4 01/10] objtool: Handle various symbol types of rodata
Posted by Tiezhu Yang 1 year, 2 months ago
On 11/28/2024 02:53 AM, Josh Poimboeuf wrote:
> On Wed, Nov 27, 2024 at 02:39:13PM +0800, Tiezhu Yang wrote:
>> On 11/27/2024 08:52 AM, Josh Poimboeuf wrote:
>>> On Tue, Nov 26, 2024 at 06:41:29PM +0800, Tiezhu Yang wrote:
>>>> On 11/26/2024 02:44 PM, Josh Poimboeuf wrote:
>>>>> On Fri, Nov 22, 2024 at 12:49:56PM +0800, Tiezhu Yang wrote:
>>>>>> @@ -2094,12 +2095,19 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
>>>>>
>>>>> 'prev_offset' needs to be updated as well.
>>>>
>>>> I am not sure I understand your comment correctly, I can not see
>>>> what should to do about 'prev_offset'.
>>>
>>> Further down the function there is
>>>
>>>   prev_offset = reloc_offset(reloc);
>>>
>>> which needs to be changed to
>>>
>>>   prev_offset = offset;
>>>
>>> as part of the patch.
>>
>> If I understand correctly, reloc_offset(reloc) is different with
>> reloc->sym->offset + reloc_addend(reloc), tested on x86 and readelf
>> shows that their values are different, reloc_offset(reloc) is the
>> first column of .rela.rodata, reloc->sym->offset is the second to
>> last column of .rela.rodata, reloc_addend(reloc) is the last column
>> of .rela.rodata.
>>
>> If do the above change as you suggested, there will be some objtool
>> warnings on x86. I think it should be:
>>
>>   prev_offset = reloc_offset(reloc);
>>
>> rather than:
>>
>>   prev_offset = offset;
>>
>> That is to say, no need to change "prev_offset".
>> Could you please check it again, please let me know if I am wrong.
>
> Sorry, I was confused by the fact there are two different meanings for
> "offset": one for where the relocation is written, and one for the
> symbol it refers to.
>
> How about instead of 'offset', call it 'sym_offset'?

OK, looks better, will modify it in the next version.