Re: [PATCH 3/5] elflint: Fix invalid type of relocation info and other issues on mips

2023-05-17 Thread Ying Huang
Hi,

在 2023/5/11 23:59, Mark Wielaard 写道:
> Hi,
>
> On Tue, 2023-04-11 at 16:12 +0800, Ying Huang wrote:
>> From: Ying Huang 
>>
>> add some check related functions
>> ---
>>  backends/mips_init.c  |  3 +++
>>  backends/mips_symbol.c| 33 +
>>  libebl/eblrelocvaliduse.c |  8 ++--
>>  src/elflint.c | 23 ---
>>  4 files changed, 62 insertions(+), 5 deletions(-)
>>
>> diff --git a/backends/mips_init.c b/backends/mips_init.c
>> index 5bba822b..4c2f21b9 100644
>> --- a/backends/mips_init.c
>> +++ b/backends/mips_init.c
>> @@ -51,6 +51,9 @@ mips_init (Elf *elf __attribute__ ((unused)),
>>HOOK (eh, segment_type_name);
>>HOOK (eh, dynamic_tag_check);
>>HOOK (eh, dynamic_tag_name);
>> +  HOOK (eh, machine_section_flag_check);
>>HOOK (eh, check_object_attribute);
>> +  HOOK (eh, check_special_symbol);
>> +  HOOK (eh, check_reloc_target_type);
>>return eh;
>>  }
> OK. But see below for hooking reloc_valid_use

 OK, I would add hook reloc_valid_use.


>> +/* Check whether given symbol's st_value and st_size are OK despite failing
>> +   normal checks.  */
>> +bool
>> +mips_check_special_symbol (Elf *elf,
>> +const GElf_Sym *sym __attribute__ ((unused)),
>> +const char *name __attribute__ ((unused)),
>> +const GElf_Shdr *destshdr)
>> +{
>> +  size_t shstrndx;
>> +  if (elf_getshdrstrndx (elf, &shstrndx) != 0)
>> +return false;
>> +  const char *sname = elf_strptr (elf, shstrndx, destshdr->sh_name);
>> +  if (sname == NULL)
>> +return false;
>> +  return (strcmp (sname, ".got") == 0 || strcmp (sname, ".bss") == 0);
>> +}
> Could you add a comment why .got and .bss are special in this case?

    raw code:

    huangying@Sleepygon:~/elf/elfutils_4$ src/elflint  src/nm
    section [38] '.symtab': symbol 781 (_gp): st_value out of bounds
    section [38] '.symtab': _DYNAMIC symbol size 0 does not match dynamic 
segment size 624
    section [38] '.symtab': _GLOBAL_OFFSET_TABLE_ symbol size 0 does not match 
.got section size 3736

    huangying@Sleepygon:~/elf/elfutils_4$ src/elflint --gnu src/nm
    section [38] '.symtab': symbol 781 (_gp): st_value out of bounds

    After add '.got':

    huangying@Sleepygon:~/elf/elfutils_4$ src/elflint  src/nm
    section [38] '.symtab': _DYNAMIC symbol size 0 does not match dynamic 
segment size 624
    section [38] '.symtab': symbol 912 (_fbss): st_value out of bounds
    section [38] '.symtab': symbol 1160 (__bss_start): st_value out of bounds

    huangying@Sleepygon:~/elf/elfutils_4$ src/elflint  --gnu src/nm
    section [38] '.symtab': symbol 912 (_fbss): st_value out of bounds

    After add '.bss':

    huangying@Sleepygon:~/elf/elfutils_4$ src/elflint  src/nm
    section [38] '.symtab': _DYNAMIC symbol size 0 does not match dynamic 
segment size 624

    huangying@Sleepygon:~/elf/elfutils_4$ src/elflint  --gnu src/nm
    No errors

    And also has error , but make check is OK.

   

>
>> +/* Check whether SHF_MASKPROC flags are valid.  */
>> +bool
>> +mips_machine_section_flag_check (GElf_Xword sh_flags)
>> +{
>> +  return ((sh_flags &~ (SHF_MIPS_GPREL |
>> +SHF_MIPS_MERGE |
>> +SHF_MIPS_ADDR |
>> +SHF_MIPS_STRINGS)) == 0);
>> +}
> OK. But see below for checking other SHF_MIPS flags.

    OK, add it:

diff --git a/backends/mips_symbol.c b/backends/mips_symbol.c
index 8787fcee..f4dee731 100644
--- a/backends/mips_symbol.c
+++ b/backends/mips_symbol.c
@@ -188,7 +188,11 @@ mips_machine_section_flag_check (GElf_Xword sh_flags)
   return ((sh_flags &~ (SHF_MIPS_GPREL |
    SHF_MIPS_MERGE |
    SHF_MIPS_ADDR |
-   SHF_MIPS_STRINGS)) == 0);
+   SHF_MIPS_STRINGS |
+   SHF_MIPS_NOSTRIP |
+   SHF_MIPS_LOCAL |
+   SHF_MIPS_NAMES |
+   SHF_MIPS_NODUPE)) == 0);
 }

>
>> diff --git a/src/elflint.c b/src/elflint.c
>> index dd42dcb4..04f1ee92 100644
>> --- a/src/elflint.c
>> +++ b/src/elflint.c
>> @@ -935,7 +935,10 @@ section [%2d] '%s': symbol %zu (%s): non-local symbol 
>> outside range described in
>>  }
>>  
>>if (GELF_ST_TYPE (sym->st_info) == STT_SECTION
>> -  && GELF_ST_BIND (sym->st_info) != STB_LOCAL)
>> +  && GELF_ST_BIND (sym->st_info) != STB_LOCAL
>> +  && ehdr->e_machine != EM_MIPS
>> +  && strcmp (name, "_DYNAMIC_LINK") != 0
>> +  && strcmp (name, "_DYNAMIC_LINKING") != 0)
> Could you add a comment here about both symbols not being local on
> MIPS?

    raw code:

    huangying@Sleepygon:~/elf/elfutils_4$ src/elflint --gnu src/nm
    section [ 7] '.dynsym': symbol 166 (_DYNAMIC_LINKING): non-local section 
symbol
    section [38] '.symtab': symbol 1052 (_DYNAMIC_LINKING): non-local section 
symbol
   

>
>>  ERROR (_("\
>>  section [%2d] '%s': symbol %

Re: [PATCH 4/5] stack: Fix stack unwind failure on mips

2023-05-17 Thread Ying Huang
Hi,

在 2023/5/12 00:07, Mark Wielaard 写道:
>>  libebl_backends_a_SOURCES = $(i386_SRCS) $(sh_SRCS) $(x86_64_SRCS) \
>>  $(ia64_SRCS) $(alpha_SRCS) $(arm_SRCS) \
>> diff --git a/backends/mips_cfi.c b/backends/mips_cfi.c
>> new file mode 100644
>> index ..77132cc1
>> --- /dev/null
>> +++ b/backends/mips_cfi.c
>> @@ -0,0 +1,68 @@
>> +/* MIPS ABI-specified defaults for DWARF CFI.
>> +   Copyright (C) 2009 Red Hat, Inc.
>> +   Copyright (C) 2023 CIP United Inc.
>> +   This file is part of elfutils.
>> +
>> +   This file is free software; you can redistribute it and/or modify
>> +   it under the terms of either
>> +
>> + * the GNU Lesser General Public License as published by the Free
>> +   Software Foundation; either version 3 of the License, or (at
>> +   your option) any later version
>> +
>> +   or
>> +
>> + * the GNU General Public License as published by the Free
>> +   Software Foundation; either version 2 of the License, or (at
>> +   your option) any later version
>> +
>> +   or both in parallel, as here.
>> +
>> +   elfutils is distributed in the hope that it will be useful, but
>> +   WITHOUT ANY WARRANTY; without even the implied warranty of
>> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> +   General Public License for more details.
>> +
>> +   You should have received copies of the GNU General Public License and
>> +   the GNU Lesser General Public License along with this program.  If
>> +   not, see .  */
>> +
>> +#ifdef HAVE_CONFIG_H
>> +# include 
>> +#endif
>> +
>> +#include 
>> +
>> +#define BACKEND mips_
>> +#include "libebl_CPU.h"
>> +
>> +int
>> +mips_abi_cfi (Ebl *ebl __attribute__ ((unused)), Dwarf_CIE *abi_info)
>> +{
>> +  static const uint8_t abi_cfi[] =
>> +{
>> +  DW_CFA_def_cfa, ULEB128_7 (31), ULEB128_7 (0),
>> +  /* Callee-saved regs.  */
>> +  DW_CFA_same_value, ULEB128_7 (16), /* s0 */
>> +  DW_CFA_same_value, ULEB128_7 (17), /* s1 */
>> +  DW_CFA_same_value, ULEB128_7 (18), /* s2 */
>> +  DW_CFA_same_value, ULEB128_7 (19), /* s3 */
>> +  DW_CFA_same_value, ULEB128_7 (20), /* s4 */
>> +  DW_CFA_same_value, ULEB128_7 (21), /* s5 */
>> +  DW_CFA_same_value, ULEB128_7 (22), /* s6 */
>> +  DW_CFA_same_value, ULEB128_7 (23), /* s7 */
>> +  DW_CFA_same_value, ULEB128_7 (28), /* gp */
>> +  DW_CFA_same_value, ULEB128_7 (29), /* sp */
>> +  DW_CFA_same_value, ULEB128_7 (30), /* fp */
>> +
>> +  DW_CFA_val_offset, ULEB128_7 (29), ULEB128_7 (0),
>> +};
>> +
>> +  abi_info->initial_instructions = abi_cfi;
>> +  abi_info->initial_instructions_end = &abi_cfi[sizeof abi_cfi];
>> +  abi_info->data_alignment_factor = -4;
>> +
>> +  abi_info->return_address_register = 31; /* %ra */
>> +
>> +  return 0;
>> +}
> Looks good, but do you have a reference to the ABI docs would be nice
> to add an URL as comment for people to double check.

    document:

    https://irix7.com/techpubs/007-2816-004.pdf

    Page17 Page18 Page14

>> diff --git a/backends/mips_initreg.c b/backends/mips_initreg.c
>> new file mode 100644
>> index ..31b8de13
>> --- /dev/null
>> +++ b/backends/mips_initreg.c
>> @@ -0,0 +1,70 @@
>> +/* Fetch live process registers from TID.
>> +   Copyright (C) 2023 CIP United Inc.
>> +   This file is part of elfutils.
>> +
>> +   This file is free software; you can redistribute it and/or modify
>> +   it under the terms of either
>> +
>> + * the GNU Lesser General Public License as published by the Free
>> +   Software Foundation; either version 3 of the License, or (at
>> +   your option) any later version
>> +
>> +   or
>> +
>> + * the GNU General Public License as published by the Free
>> +   Software Foundation; either version 2 of the License, or (at
>> +   your option) any later version
>> +
>> +   or both in parallel, as here.
>> +
>> +   elfutils is distributed in the hope that it will be useful, but
>> +   WITHOUT ANY WARRANTY; without even the implied warranty of
>> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> +   General Public License for more details.
>> +
>> +   You should have received copies of the GNU General Public License and
>> +   the GNU Lesser General Public License along with this program.  If
>> +   not, see .  */
>> +
>> +#ifdef HAVE_CONFIG_H
>> +# include 
>> +#endif
>> +
>> +#include 
>> +#if (defined(mips) || defined(__mips) || defined(__mips__) || defined(MIPS) 
>> || defined(__MIPS__)) && defined(__linux__)
>> +# include 
>> +# include 
>> +#endif
>> +
>> +#define BACKEND mips_
>> +#include "libebl_CPU.h"
>> +#include 
>> +
>> +
>> +bool
>> +mips_set_initial_registers_tid (pid_t tid __attribute__ ((unused)),
>> +  ebl_tid_registers_t *setfunc __attribute__ ((unused)),
>> +  void *arg __attribute__ ((unused)))
>> +{
>> +#if (!defined(mips) && !defined(_