[PATCH v4 04/10] objtool: Handle PC relative relocation type

Tiezhu Yang posted 10 patches 1 year, 2 months ago
There is a newer version of this series
[PATCH v4 04/10] objtool: Handle PC relative relocation type
Posted by Tiezhu Yang 1 year, 2 months ago
When compling with Clang on LoongArch, there exists 32 bit PC relative
relocation type, it needs to get the offset with "S + A - PC" according
to the spec of "ELF for the LoongArch Architecture".

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

Link: https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
 tools/objtool/check.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 19d1263e64e4..8733ca620cca 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -2126,6 +2126,11 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
 		if (reloc->sym->type == STT_SECTION) {
 			/* Addend field in the relocation entry associated with the symbol */
 			offset = reloc_addend(reloc);
+			/* Handle the special cases compiled with Clang on LoongArch */
+			if (file->elf->ehdr.e_machine == EM_LOONGARCH &&
+			    reloc_type(reloc) == R_LARCH_32_PCREL)
+				offset = reloc->sym->offset + reloc_addend(reloc) -
+					 (reloc_offset(reloc) - reloc_offset(table));
 		} else {
 			/* The address of the symbol in the relocation entry */
 			offset = reloc->sym->offset;
-- 
2.42.0
Re: [PATCH v4 04/10] objtool: Handle PC relative relocation type
Posted by Josh Poimboeuf 1 year, 2 months ago
On Fri, Nov 22, 2024 at 12:49:59PM +0800, Tiezhu Yang wrote:
> When compling with Clang on LoongArch, there exists 32 bit PC relative
> relocation type, it needs to get the offset with "S + A - PC" according
> to the spec of "ELF for the LoongArch Architecture".
> 
> This is preparation for later patch on LoongArch, there is no effect for
> the other archs with this patch.
> 
> Link: https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
>  tools/objtool/check.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
> index 19d1263e64e4..8733ca620cca 100644
> --- a/tools/objtool/check.c
> +++ b/tools/objtool/check.c
> @@ -2126,6 +2126,11 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
>  		if (reloc->sym->type == STT_SECTION) {
>  			/* Addend field in the relocation entry associated with the symbol */
>  			offset = reloc_addend(reloc);
> +			/* Handle the special cases compiled with Clang on LoongArch */
> +			if (file->elf->ehdr.e_machine == EM_LOONGARCH &&
> +			    reloc_type(reloc) == R_LARCH_32_PCREL)
> +				offset = reloc->sym->offset + reloc_addend(reloc) -
> +					 (reloc_offset(reloc) - reloc_offset(table));

Please, no more "special cases".  It makes the common code unreadable.
We should avoid checking 'elf->ehdr.e_machine' in check.c.

Maybe there should be an arch-specific function arch_adjusted_addend().

-- 
Josh
Re: [PATCH v4 04/10] objtool: Handle PC relative relocation type
Posted by Tiezhu Yang 1 year, 2 months ago
On 11/26/2024 03:19 PM, Josh Poimboeuf wrote:
> On Fri, Nov 22, 2024 at 12:49:59PM +0800, Tiezhu Yang wrote:
>> When compling with Clang on LoongArch, there exists 32 bit PC relative
>> relocation type, it needs to get the offset with "S + A - PC" according
>> to the spec of "ELF for the LoongArch Architecture".
>>
>> This is preparation for later patch on LoongArch, there is no effect for
>> the other archs with this patch.
>>
>> Link: https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc
>> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
>> ---
>>  tools/objtool/check.c | 5 +++++
>>  1 file changed, 5 insertions(+)
>>
>> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
>> index 19d1263e64e4..8733ca620cca 100644
>> --- a/tools/objtool/check.c
>> +++ b/tools/objtool/check.c
>> @@ -2126,6 +2126,11 @@ static int add_jump_table(struct objtool_file *file, struct instruction *insn,
>>  		if (reloc->sym->type == STT_SECTION) {
>>  			/* Addend field in the relocation entry associated with the symbol */
>>  			offset = reloc_addend(reloc);
>> +			/* Handle the special cases compiled with Clang on LoongArch */
>> +			if (file->elf->ehdr.e_machine == EM_LOONGARCH &&
>> +			    reloc_type(reloc) == R_LARCH_32_PCREL)
>> +				offset = reloc->sym->offset + reloc_addend(reloc) -
>> +					 (reloc_offset(reloc) - reloc_offset(table));
>
> Please, no more "special cases".  It makes the common code unreadable.
> We should avoid checking 'elf->ehdr.e_machine' in check.c.

OK.

>
> Maybe there should be an arch-specific function arch_adjusted_addend().
>

Will do it.
Re: [PATCH v4 04/10] objtool: Handle PC relative relocation type
Posted by Tiezhu Yang 1 year, 2 months ago
On 11/26/2024 07:00 PM, Tiezhu Yang wrote:
> On 11/26/2024 03:19 PM, Josh Poimboeuf wrote:
>> On Fri, Nov 22, 2024 at 12:49:59PM +0800, Tiezhu Yang wrote:
>>> When compling with Clang on LoongArch, there exists 32 bit PC relative
>>> relocation type, it needs to get the offset with "S + A - PC" according
>>> to the spec of "ELF for the LoongArch Architecture".
>>>
>>> This is preparation for later patch on LoongArch, there is no effect for
>>> the other archs with this patch.
>>>
>>> Link: https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc
>>> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
>>> ---
>>>  tools/objtool/check.c | 5 +++++
>>>  1 file changed, 5 insertions(+)
>>>
>>> diff --git a/tools/objtool/check.c b/tools/objtool/check.c
>>> index 19d1263e64e4..8733ca620cca 100644
>>> --- a/tools/objtool/check.c
>>> +++ b/tools/objtool/check.c
>>> @@ -2126,6 +2126,11 @@ static int add_jump_table(struct objtool_file
>>> *file, struct instruction *insn,
>>>          if (reloc->sym->type == STT_SECTION) {
>>>              /* Addend field in the relocation entry associated with
>>> the symbol */
>>>              offset = reloc_addend(reloc);
>>> +            /* Handle the special cases compiled with Clang on
>>> LoongArch */
>>> +            if (file->elf->ehdr.e_machine == EM_LOONGARCH &&
>>> +                reloc_type(reloc) == R_LARCH_32_PCREL)
>>> +                offset = reloc->sym->offset + reloc_addend(reloc) -
>>> +                     (reloc_offset(reloc) - reloc_offset(table));
>>
>> Please, no more "special cases".  It makes the common code unreadable.
>> We should avoid checking 'elf->ehdr.e_machine' in check.c.
>
> OK.
>
>>
>> Maybe there should be an arch-specific function arch_adjusted_addend().

Add adjust_offset() in
tools/objtool/include/objtool/elf.h,
like this:

static inline unsigned long adjust_offset(struct elf *elf, struct reloc 
*reloc,
                                           unsigned long offset)
{
         if (elf->ehdr.e_machine == EM_LOONGARCH && reloc_type(reloc) == 
R_LARCH_32_PCREL)
                 offset = reloc->sym->offset + reloc_addend(reloc) -
                          (reloc_offset(reloc) - reloc_offset(table));

         return offset;
}

then call it in check.c, like this:

offset = reloc->sym->offset + reloc_addend(reloc);
offset = adjust_offset(file->elf, reloc, offset);

What do you think?

Thanks,
Tiezhu
Re: [PATCH v4 04/10] objtool: Handle PC relative relocation type
Posted by Josh Poimboeuf 1 year, 2 months ago
On Tue, Nov 26, 2024 at 09:24:59PM +0800, Tiezhu Yang wrote:
> On 11/26/2024 07:00 PM, Tiezhu Yang wrote:
> > > Maybe there should be an arch-specific function arch_adjusted_addend().
> 
> Add adjust_offset() in
> tools/objtool/include/objtool/elf.h,
> like this:
> 
> static inline unsigned long adjust_offset(struct elf *elf, struct reloc
> *reloc,
>                                           unsigned long offset)
> {
>         if (elf->ehdr.e_machine == EM_LOONGARCH && reloc_type(reloc) ==
> R_LARCH_32_PCREL)
>                 offset = reloc->sym->offset + reloc_addend(reloc) -
>                          (reloc_offset(reloc) - reloc_offset(table));
> 
>         return offset;
> }
> 
> then call it in check.c, like this:
> 
> offset = reloc->sym->offset + reloc_addend(reloc);
> offset = adjust_offset(file->elf, reloc, offset);
> 
> What do you think?

Similar to reloc_size() I'm thinking this should also be in
arch-specific decode.c.

-- 
Josh