backends: add checks for _GLOBAL_OFFSET_TABLE_ on loongarch

Message ID 1680319133-22776-1-git-send-email-tangyouling@loongson.cn
State Committed
Headers
Series backends: add checks for _GLOBAL_OFFSET_TABLE_ on loongarch |

Commit Message

Youling Tang April 1, 2023, 3:18 a.m. UTC
  Add handling of _GLOBAL_OFFSET_TABLE_.

Before applying the patch:
$ ./src/elflint --gnu-ld ./src/elflint
section [35] '.symtab': _GLOBAL_OFFSET_TABLE_ symbol value 0x68548
does not match .got.plt section address 0x68238

After applying the patch:
$ ./src/elflint --gnu-ld ./src/elflint
No errors

Signed-off-by: Liwei Ge <geliwei@openanolis.org>
Signed-off-by: Youling Tang <tangyouling@loongson.cn>
---
 backends/ChangeLog          |  4 ++++
 backends/loongarch_init.c   |  1 +
 backends/loongarch_symbol.c | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+)
  

Comments

Hengqi Chen April 4, 2023, 3:45 p.m. UTC | #1
Hi, Youling

On Sat, Apr 1, 2023 at 11:19 AM Youling Tang <tangyouling@loongson.cn> wrote:
>
> Add handling of _GLOBAL_OFFSET_TABLE_.
>
> Before applying the patch:
> $ ./src/elflint --gnu-ld ./src/elflint
> section [35] '.symtab': _GLOBAL_OFFSET_TABLE_ symbol value 0x68548
> does not match .got.plt section address 0x68238
>
> After applying the patch:
> $ ./src/elflint --gnu-ld ./src/elflint
> No errors
>
> Signed-off-by: Liwei Ge <geliwei@openanolis.org>
> Signed-off-by: Youling Tang <tangyouling@loongson.cn>
> ---
>  backends/ChangeLog          |  4 ++++
>  backends/loongarch_init.c   |  1 +
>  backends/loongarch_symbol.c | 35 +++++++++++++++++++++++++++++++++++
>  3 files changed, 40 insertions(+)
>
> diff --git a/backends/ChangeLog b/backends/ChangeLog
> index 81f08314..41071953 100644
> --- a/backends/ChangeLog
> +++ b/backends/ChangeLog
> @@ -1,3 +1,7 @@
> +2023-04-01  Youling Tang <tangyouling@loongson.cn>
> +       * loongarch_init.c (loongarch_init): Hook check_special_symbol.
> +       * loongarch_symbol.c (loongarch_check_special_symbol): New function.
> +
>  2023-02-07  Mark Wielaard  <mark@klomp.org>
>
>         * libebl_CPU.h (dwarf_peeled_die_type): Explicitly handle
> diff --git a/backends/loongarch_init.c b/backends/loongarch_init.c
> index 59d8cc3d..b641b07f 100644
> --- a/backends/loongarch_init.c
> +++ b/backends/loongarch_init.c
> @@ -46,6 +46,7 @@ loongarch_init (Elf *elf __attribute__ ((unused)),
>    loongarch_init_reloc (eh);
>    HOOK (eh, reloc_simple_type);
>    HOOK (eh, machine_flag_check);
> +  HOOK (eh, check_special_symbol);
>
>    return eh;
>  }
> diff --git a/backends/loongarch_symbol.c b/backends/loongarch_symbol.c
> index 43306ab8..5ce55bad 100644
> --- a/backends/loongarch_symbol.c
> +++ b/backends/loongarch_symbol.c
> @@ -79,3 +79,38 @@ loongarch_machine_flag_check (GElf_Word flags)
>    return ((flags &~ (EF_LARCH_ABI_MODIFIER_MASK
>                      | EF_LARCH_OBJABI_V1)) == 0);
>  }
> +
> +/* Check whether given symbol's st_value and st_size are OK despite failing
> +   normal checks.  */
> +bool
> +loongarch_check_special_symbol (Elf *elf, const GElf_Sym *sym,
> +                           const char *name, const GElf_Shdr *destshdr)
> +{
> +  if (name != NULL
> +      && strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
> +    {
> +      size_t shstrndx;
> +      if (elf_getshdrstrndx (elf, &shstrndx) != 0)
> +       return false;
> +      const char *sname = elf_strptr (elf, shstrndx, destshdr->sh_name);
> +      if (sname != NULL
> +         && (strcmp (sname, ".got") == 0 || strcmp (sname, ".got.plt") == 0))
> +       {
> +         Elf_Scn *scn = NULL;
> +         while ((scn = elf_nextscn (elf, scn)) != NULL)
> +           {
> +             GElf_Shdr shdr_mem;
> +             GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
> +             if (shdr != NULL)
> +               {
> +                 sname = elf_strptr (elf, shstrndx, shdr->sh_name);
> +                 if (sname != NULL && strcmp (sname, ".got") == 0)
> +                   return (sym->st_value >= shdr->sh_addr
> +                           && sym->st_value < shdr->sh_addr + shdr->sh_size);
> +               }
> +           }
> +       }
> +    }
> +
> +  return false;
> +}
> --
> 2.37.1
>

I've tested this locally, but still remains one error:
section [34] '.symtab': _DYNAMIC symbol size 0 does not match dynamic
segment size 480

Cheers,
---
Hengqi
  
Youling Tang April 6, 2023, 1:59 a.m. UTC | #2
Hi, Hengqi

On 04/04/2023 11:45 PM, Hengqi Chen wrote:
> Hi, Youling
>
> On Sat, Apr 1, 2023 at 11:19 AM Youling Tang <tangyouling@loongson.cn> wrote:
>>
>> Add handling of _GLOBAL_OFFSET_TABLE_.
>>
>> Before applying the patch:
>> $ ./src/elflint --gnu-ld ./src/elflint
>> section [35] '.symtab': _GLOBAL_OFFSET_TABLE_ symbol value 0x68548
>> does not match .got.plt section address 0x68238
>>
>> After applying the patch:
>> $ ./src/elflint --gnu-ld ./src/elflint
>> No errors
>>
>> Signed-off-by: Liwei Ge <geliwei@openanolis.org>
>> Signed-off-by: Youling Tang <tangyouling@loongson.cn>
>> ---
>>  backends/ChangeLog          |  4 ++++
>>  backends/loongarch_init.c   |  1 +
>>  backends/loongarch_symbol.c | 35 +++++++++++++++++++++++++++++++++++
>>  3 files changed, 40 insertions(+)
>>
>> diff --git a/backends/ChangeLog b/backends/ChangeLog
>> index 81f08314..41071953 100644
>> --- a/backends/ChangeLog
>> +++ b/backends/ChangeLog
>> @@ -1,3 +1,7 @@
>> +2023-04-01  Youling Tang <tangyouling@loongson.cn>
>> +       * loongarch_init.c (loongarch_init): Hook check_special_symbol.
>> +       * loongarch_symbol.c (loongarch_check_special_symbol): New function.
>> +
>>  2023-02-07  Mark Wielaard  <mark@klomp.org>
>>
>>         * libebl_CPU.h (dwarf_peeled_die_type): Explicitly handle
>> diff --git a/backends/loongarch_init.c b/backends/loongarch_init.c
>> index 59d8cc3d..b641b07f 100644
>> --- a/backends/loongarch_init.c
>> +++ b/backends/loongarch_init.c
>> @@ -46,6 +46,7 @@ loongarch_init (Elf *elf __attribute__ ((unused)),
>>    loongarch_init_reloc (eh);
>>    HOOK (eh, reloc_simple_type);
>>    HOOK (eh, machine_flag_check);
>> +  HOOK (eh, check_special_symbol);
>>
>>    return eh;
>>  }
>> diff --git a/backends/loongarch_symbol.c b/backends/loongarch_symbol.c
>> index 43306ab8..5ce55bad 100644
>> --- a/backends/loongarch_symbol.c
>> +++ b/backends/loongarch_symbol.c
>> @@ -79,3 +79,38 @@ loongarch_machine_flag_check (GElf_Word flags)
>>    return ((flags &~ (EF_LARCH_ABI_MODIFIER_MASK
>>                      | EF_LARCH_OBJABI_V1)) == 0);
>>  }
>> +
>> +/* Check whether given symbol's st_value and st_size are OK despite failing
>> +   normal checks.  */
>> +bool
>> +loongarch_check_special_symbol (Elf *elf, const GElf_Sym *sym,
>> +                           const char *name, const GElf_Shdr *destshdr)
>> +{
>> +  if (name != NULL
>> +      && strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
>> +    {
>> +      size_t shstrndx;
>> +      if (elf_getshdrstrndx (elf, &shstrndx) != 0)
>> +       return false;
>> +      const char *sname = elf_strptr (elf, shstrndx, destshdr->sh_name);
>> +      if (sname != NULL
>> +         && (strcmp (sname, ".got") == 0 || strcmp (sname, ".got.plt") == 0))
>> +       {
>> +         Elf_Scn *scn = NULL;
>> +         while ((scn = elf_nextscn (elf, scn)) != NULL)
>> +           {
>> +             GElf_Shdr shdr_mem;
>> +             GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
>> +             if (shdr != NULL)
>> +               {
>> +                 sname = elf_strptr (elf, shstrndx, shdr->sh_name);
>> +                 if (sname != NULL && strcmp (sname, ".got") == 0)
>> +                   return (sym->st_value >= shdr->sh_addr
>> +                           && sym->st_value < shdr->sh_addr + shdr->sh_size);
>> +               }
>> +           }
>> +       }
>> +    }
>> +
>> +  return false;
>> +}
>> --
>> 2.37.1
>>
>
> I've tested this locally, but still remains one error:
> section [34] '.symtab': _DYNAMIC symbol size 0 does not match dynamic
> segment size 480

The problem should be independent of this patch, but I can't reproduce
it.

- Test Environment 1: Old Toolchain:
$ cat /etc/os-release
NAME="My GNU/Linux System for LoongArch64"
VERSION="5.0"
ID=CLFS4LA64
PRETTY_NAME="My GNU/Linux System for LoongArch64 5.0"

$ gcc -v
gcc version 12.1.0 (GCC)

$ ./src/elflint --gnu-ld ./src/elflint
No errors


- Test Environment 2: New Toolchain:
$ cat /etc/os-release
NAME="My GNU/Linux System for LoongArch64"
VERSION="6.0"
ID=CLFS4LA64
PRETTY_NAME="My GNU/Linux System for LoongArch64 6.0"

$ gcc -v
gcc version 13.0.0 20220919 (experimental) (GCC)

$ ./src/elflint --gnu-ld ./src/elflint
No errors

Can you provide your test environment, or help debug the possible
causes of the problem?

Thanks,
Youling.
  
Mark Wielaard April 6, 2023, 10:52 p.m. UTC | #3
Hi,

On Sat, Apr 01, 2023 at 11:18:53AM +0800, Youling Tang wrote:
> Add handling of _GLOBAL_OFFSET_TABLE_.
> 
> Before applying the patch:
> $ ./src/elflint --gnu-ld ./src/elflint
> section [35] '.symtab': _GLOBAL_OFFSET_TABLE_ symbol value 0x68548
> does not match .got.plt section address 0x68238
> 
> After applying the patch:
> $ ./src/elflint --gnu-ld ./src/elflint
> No errors

Thanks, code looks correct.  I agree that the other issue mentioned
(_DYNAMIC symbol size 0) is a separate issue.

Pushed,

Mark
  

Patch

diff --git a/backends/ChangeLog b/backends/ChangeLog
index 81f08314..41071953 100644
--- a/backends/ChangeLog
+++ b/backends/ChangeLog
@@ -1,3 +1,7 @@ 
+2023-04-01  Youling Tang <tangyouling@loongson.cn>
+	* loongarch_init.c (loongarch_init): Hook check_special_symbol.
+	* loongarch_symbol.c (loongarch_check_special_symbol): New function.
+
 2023-02-07  Mark Wielaard  <mark@klomp.org>
 
 	* libebl_CPU.h (dwarf_peeled_die_type): Explicitly handle
diff --git a/backends/loongarch_init.c b/backends/loongarch_init.c
index 59d8cc3d..b641b07f 100644
--- a/backends/loongarch_init.c
+++ b/backends/loongarch_init.c
@@ -46,6 +46,7 @@  loongarch_init (Elf *elf __attribute__ ((unused)),
   loongarch_init_reloc (eh);
   HOOK (eh, reloc_simple_type);
   HOOK (eh, machine_flag_check);
+  HOOK (eh, check_special_symbol);
 
   return eh;
 }
diff --git a/backends/loongarch_symbol.c b/backends/loongarch_symbol.c
index 43306ab8..5ce55bad 100644
--- a/backends/loongarch_symbol.c
+++ b/backends/loongarch_symbol.c
@@ -79,3 +79,38 @@  loongarch_machine_flag_check (GElf_Word flags)
   return ((flags &~ (EF_LARCH_ABI_MODIFIER_MASK
 		     | EF_LARCH_OBJABI_V1)) == 0);
 }
+
+/* Check whether given symbol's st_value and st_size are OK despite failing
+   normal checks.  */
+bool
+loongarch_check_special_symbol (Elf *elf, const GElf_Sym *sym,
+			    const char *name, const GElf_Shdr *destshdr)
+{
+  if (name != NULL
+      && strcmp (name, "_GLOBAL_OFFSET_TABLE_") == 0)
+    {
+      size_t shstrndx;
+      if (elf_getshdrstrndx (elf, &shstrndx) != 0)
+	return false;
+      const char *sname = elf_strptr (elf, shstrndx, destshdr->sh_name);
+      if (sname != NULL
+	  && (strcmp (sname, ".got") == 0 || strcmp (sname, ".got.plt") == 0))
+	{
+	  Elf_Scn *scn = NULL;
+	  while ((scn = elf_nextscn (elf, scn)) != NULL)
+	    {
+	      GElf_Shdr shdr_mem;
+	      GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
+	      if (shdr != NULL)
+		{
+		  sname = elf_strptr (elf, shstrndx, shdr->sh_name);
+		  if (sname != NULL && strcmp (sname, ".got") == 0)
+		    return (sym->st_value >= shdr->sh_addr
+			    && sym->st_value < shdr->sh_addr + shdr->sh_size);
+		}
+	    }
+	}
+    }
+
+  return false;
+}