public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: ying.huang@oss.cipunited.com, elfutils-devel@sourceware.org
Subject: Re: [PATCH 3/5] elflint: Fix invalid type of relocation info and other issues on mips
Date: Thu, 11 May 2023 17:59:57 +0200	[thread overview]
Message-ID: <66059a3000132413a3d7ebede540309256c85cfd.camel@klomp.org> (raw)
In-Reply-To: <20230411081141.1762395-4-ying.huang@oss.cipunited.com>

Hi,

On Tue, 2023-04-11 at 16:12 +0800, Ying Huang wrote:
> From: Ying Huang <ying.huang@oss.cipunited.com>
> 
> 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

> diff --git a/backends/mips_symbol.c b/backends/mips_symbol.c
> index e760d58d..8787fcee 100644
> --- a/backends/mips_symbol.c
> +++ b/backends/mips_symbol.c
> @@ -158,6 +158,39 @@ mips_section_type_name (int type,
>    return NULL;
>  }
>  
> +bool
> +mips_check_reloc_target_type (Ebl *ebl __attribute__ ((unused)), Elf64_Word sh_type)
> +{
> +  return (sh_type == SHT_MIPS_DWARF);
> +}

OK

> +/* 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?

> +/* 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.

>  /* Check whether machine flags are valid.  */
>  bool
>  mips_machine_flag_check (GElf_Word flags)
> diff --git a/libebl/eblrelocvaliduse.c b/libebl/eblrelocvaliduse.c
> index f0bed345..44b8d300 100644
> --- a/libebl/eblrelocvaliduse.c
> +++ b/libebl/eblrelocvaliduse.c
> @@ -32,10 +32,14 @@
>  #endif
>  
>  #include <libeblP.h>
> -
> +#include <libelfP.h>
>  
>  bool
>  ebl_reloc_valid_use (Ebl *ebl, int reloc)
>  {
> -  return ebl != NULL ? ebl->reloc_valid_use (ebl->elf, reloc) : false;
> +  int relocNew = reloc;
> +  GElf_Ehdr ehdr;
> +  if(ebl->elf->class == ELFCLASS64 && gelf_getehdr(ebl->elf, &ehdr) != NULL && ehdr.e_machine == EM_MIPS)
> +    relocNew = ELF64_MIPS_R_TYPE(reloc);
> +  return ebl != NULL ? ebl->reloc_valid_use (ebl->elf, relocNew) : false;
>  }

This should be a mips reloc_valid_use hook.

> 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?

>  	ERROR (_("\
>  section [%2d] '%s': symbol %zu (%s): non-local section symbol\n"),
>  	       idx, section_name (ebl, idx), cnt, name);
> @@ -3789,6 +3792,12 @@ cannot get section header for section [%2zu] '%s': %s\n"),
>  		    && ebl_bss_plt_p (ebl))
>  		  good_type = SHT_NOBITS;
>  
> +	        if (ehdr->e_machine == EM_MIPS
> +	            && (strstr(special_sections[s].name, ".debug") != NULL))
> +		  {
> +	            good_type = SHT_MIPS_DWARF;
> +		  }

OK. You don't need explicit brackets here

>  		/* In a debuginfo file, any normal section can be SHT_NOBITS.
>  		   This is only invalid for DWARF sections and .shstrtab.  */
>  		if (shdr->sh_type != good_type
> @@ -3953,8 +3962,16 @@ section [%2zu] '%s': size not multiple of entry size\n"),
>  	      sh_flags &= ~(GElf_Xword) SHF_MASKPROC;
>  	    }
>  	  if (sh_flags & SHF_MASKOS)
> -	    if (gnuld)
> -	      sh_flags &= ~(GElf_Xword) SHF_GNU_RETAIN;
> +	    {
> +	      if (gnuld)
> +	        sh_flags &= ~(GElf_Xword) SHF_GNU_RETAIN;
> +	      if (ehdr->e_machine == EM_MIPS)
> +	        {
> +	          if(sh_flags == SHF_MIPS_NOSTRIP || sh_flags == SHF_MIPS_LOCAL
> +		   || sh_flags == SHF_MIPS_NAMES || sh_flags == SHF_MIPS_NODUPE)
> +	            sh_flags &= ~shdr->sh_flags;
> +	        }
> +	    }
>  	  if (sh_flags != 0)
>  	    ERROR (_("section [%2zu] '%s' contains unknown flag(s)"
>  			    " %#" PRIx64 "\n"),

Can this be checked with the machine_section_flag_check hook?

Thanks,

Mark

  reply	other threads:[~2023-05-11 15:59 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-11  8:12 [PATCH 0/5] Add support for MIPS Ying Huang
2023-04-11  8:12 ` [PATCH 1/5] strip: Adapt src/strip -o -f on mips Ying Huang
2023-05-09 15:15   ` Mark Wielaard
2023-05-16  6:38     ` Ying Huang
2023-05-21 21:13       ` Mark Wielaard
2023-05-24  6:21         ` Ying Huang
2023-05-16  6:46     ` Ying Huang
2023-05-16  7:34       ` Ying Huang
2023-05-21 21:14         ` Mark Wielaard
2023-05-26  2:48           ` Ying Huang
2023-05-16  8:05     ` [EXTERNAL] " Luke Diamand
2023-04-11  8:12 ` [PATCH 2/5] readelf: Adapt src/readelf -h/-S/-r/-w/-l/-d/-a " Ying Huang
2023-05-11 14:31   ` Mark Wielaard
2023-05-16  8:01     ` Ying Huang
2023-07-24  8:35     ` Ying Huang
2023-07-25  8:15       ` Ying Huang
2023-07-27  6:08         ` Ying Huang
2023-08-01 21:43           ` Mark Wielaard
2023-08-01 13:14         ` Mark Wielaard
2023-08-01  9:25       ` Mark Wielaard
2023-04-11  8:12 ` [PATCH 3/5] elflint: Fix invalid type of relocation info and other issues " Ying Huang
2023-05-11 15:59   ` Mark Wielaard [this message]
2023-05-17  9:14     ` Ying Huang
2023-04-11  8:12 ` [PATCH 4/5] stack: Fix stack unwind failure " Ying Huang
2023-05-11 16:07   ` Mark Wielaard
2023-05-18  6:14     ` Ying Huang
2023-04-11  8:12 ` [PATCH 5/5] backends: Fix run-native-test.sh and run-funcretval++11.sh run fail " Ying Huang
2023-05-11 16:38   ` Mark Wielaard
2023-05-18  9:06     ` Ying Huang
2023-05-04  2:24 ` [PATCH 0/5] Add support for MIPS 黄莺

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=66059a3000132413a3d7ebede540309256c85cfd.camel@klomp.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    --cc=ying.huang@oss.cipunited.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).