public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: "Ying Huang" <ying.huang@oss.cipunited.com>
To: <elfutils-devel@sourceware.org>
Cc: <yunqiang.su@oss.cipunited.com>,
	 "Ying Huang" <ying.huang@oss.cipunited.com>
Subject: [PATCH v2 3/5] elflint: adapt src/elflint --gnu src/nm on mips
Date: Thu,  2 Nov 2023 14:56:00 +0800	[thread overview]
Message-ID: <20231102065602.3128526-4-ying.huang@oss.cipunited.com> (raw)
In-Reply-To: <20231102065602.3128526-1-ying.huang@oss.cipunited.com>

From: Ying Huang <ying.huang@oss.cipunited.com>

The errors were:
$ src/elflint --gnu src/nm
section [ 2] '.MIPS.options' contains unknown flag(s) 0x8000000
section [ 7] '.dynsym': symbol 165 (_DYNAMIC_LINKING): non-local section symbol
section [24] '.got' contains invalid processor-specific flag(s) 0x10000000
section [25] '.sdata' contains invalid processor-specific flag(s) 0x10000000
section [29] '.debug_aranges' has wrong type: expected PROGBITS, is MIPS_DWARF
section [30] '.debug_info' has wrong type: expected PROGBITS, is MIPS_DWARF
section [31] '.debug_abbrev' has wrong type: expected PROGBITS, is MIPS_DWARF
section [32] '.debug_line' has wrong type: expected PROGBITS, is MIPS_DWARF
section [33] '.debug_frame' has wrong type: expected PROGBITS, is MIPS_DWARF
section [34] '.debug_str' has wrong type: expected PROGBITS, is MIPS_DWARF
section [35] '.debug_loc' has wrong type: expected PROGBITS, is MIPS_DWARF
section [36] '.debug_ranges' has wrong type: expected PROGBITS, is MIPS_DWARF
section [38] '.symtab': symbol 785 (_gp): st_value out of bounds
section [38] '.symtab': symbol 910 (_fbss): st_value out of bounds
section [38] '.symtab': symbol 1051 (_DYNAMIC_LINKING): non-local section symbol

After fixing:
$ src/elflint --gnu src/nm
No errors
---
 backends/mips_init.c   |  3 +++
 backends/mips_symbol.c | 37 +++++++++++++++++++++++++++++++++++++
 src/elflint.c          | 26 +++++++++++++++++++++-----
 3 files changed, 61 insertions(+), 5 deletions(-)

diff --git a/backends/mips_init.c b/backends/mips_init.c
index 905d97eb..e883b7e4 100644
--- a/backends/mips_init.c
+++ b/backends/mips_init.c
@@ -51,9 +51,12 @@ mips_init (Elf *elf __attribute__ ((unused)),
   HOOK (eh, section_type_name);
   HOOK (eh, machine_flag_check);
   HOOK (eh, machine_flag_name);
+  HOOK (eh, machine_section_flag_check);
   HOOK (eh, segment_type_name);
   HOOK (eh, dynamic_tag_check);
   HOOK (eh, dynamic_tag_name);
   HOOK (eh, check_object_attribute);
+  HOOK (eh, check_special_symbol);
+  HOOK (eh, check_reloc_target_type);
   return eh;
 }
diff --git a/backends/mips_symbol.c b/backends/mips_symbol.c
index 6418a01d..1e3d55c4 100644
--- a/backends/mips_symbol.c
+++ b/backends/mips_symbol.c
@@ -158,6 +158,43 @@ 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);
+}
+
+/* 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);
+}
+
+/* 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 |
+			SHF_MIPS_NOSTRIP |
+			SHF_MIPS_LOCAL |
+			SHF_MIPS_NAMES |
+			SHF_MIPS_NODUPE)) == 0);
+}
+
 /* Check whether machine flags are valid.  */
 bool
 mips_machine_flag_check (GElf_Word flags)
diff --git a/src/elflint.c b/src/elflint.c
index 864de710..092409a2 100644
--- a/src/elflint.c
+++ b/src/elflint.c
@@ -936,7 +936,9 @@ 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_LINKING") != 0)
 	ERROR (_("\
 section [%2d] '%s': symbol %zu (%s): non-local section symbol\n"),
 	       idx, section_name (ebl, idx), cnt, name);
@@ -3828,6 +3830,10 @@ 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;
+
 		/* 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
@@ -3988,12 +3994,21 @@ section [%2zu] '%s': size not multiple of entry size\n"),
 		ERROR (_("section [%2zu] '%s'"
 				" contains invalid processor-specific flag(s)"
 				" %#" PRIx64 "\n"),
-		       cnt, section_name (ebl, cnt), sh_flags & SHF_MASKPROC);
+			cnt, section_name (ebl, cnt), sh_flags & SHF_MASKPROC);
 	      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 (!ebl_machine_section_flag_check (ebl,
+						   sh_flags & SHF_MASKOS))
+		ERROR (_("section [%2zu] '%s'"
+				" contains invalid os-specific flag(s)"
+				" %#" PRIx64 "\n"),
+			cnt, section_name (ebl, cnt), sh_flags & SHF_MASKOS);
+	      sh_flags &= ~(GElf_Xword) SHF_MASKOS;
+	    }
 	  if (sh_flags != 0)
 	    ERROR (_("section [%2zu] '%s' contains unknown flag(s)"
 			    " %#" PRIx64 "\n"),
@@ -4059,6 +4074,7 @@ section [%2zu] '%s': merge flag set but entry size is zero\n"),
 	  switch (shdr->sh_type)
 	    {
 	    case SHT_PROGBITS:
+	    case SHT_MIPS_DWARF:
 	      break;
 
 	    case SHT_NOBITS:
@@ -4716,7 +4732,7 @@ program header offset in ELF header and PHDR entry do not match"));
 	      if (shdr != NULL
 		  && ((is_debuginfo && shdr->sh_type == SHT_NOBITS)
 		      || (! is_debuginfo
-			  && (shdr->sh_type == SHT_PROGBITS
+			  && (is_debug_section_type(shdr->sh_type)
 			      || shdr->sh_type == SHT_X86_64_UNWIND)))
 		  && elf_strptr (ebl->elf, shstrndx, shdr->sh_name) != NULL
 		  && ! strcmp (".eh_frame_hdr",
-- 
2.30.2

  parent reply	other threads:[~2023-11-02  6:56 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-02  6:55 [PATCH v2 0/5] Add support for MIPS Ying Huang
2023-11-02  6:55 ` [PATCH v2 1/5] strip: Adapt src/strip -o -f on mips Ying Huang
2023-11-03 12:18   ` Mark Wielaard
2024-02-10  0:20     ` Mark Wielaard
2024-02-18  7:22       ` Ying Huang
2023-11-02  6:55 ` [PATCH v2 2/5] readelf: Adapt src/readelf -h/-S/-r/-w/-l/-d/-a " Ying Huang
2023-11-02  6:56 ` Ying Huang [this message]
2023-11-02  6:56 ` [PATCH v2 4/5] stack: Fix stack unwind failure " Ying Huang
2023-11-02  6:56 ` [PATCH v2 5/5] backends: Add register_info, return_value_location, core_note function " Ying Huang
2023-11-03 11:56 ` [PATCH v2 0/5] Add support for MIPS Mark Wielaard
2023-11-03 12:20   ` Mark Wielaard
2023-11-09  3:00   ` Ying Huang
2023-11-29  7:40     ` Ying Huang
2023-12-19  2:53       ` Ying Huang
2023-12-29  9:15         ` Ying Huang
2024-01-23  8:21     ` Ying Huang
2024-02-04  1:44       ` Ying Huang

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=20231102065602.3128526-4-ying.huang@oss.cipunited.com \
    --to=ying.huang@oss.cipunited.com \
    --cc=elfutils-devel@sourceware.org \
    --cc=yunqiang.su@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).