From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from s01.bc.larksuite.com (s01.bc.larksuite.com [209.127.230.13]) by sourceware.org (Postfix) with UTF8SMTPS id 3684C3858002 for ; Tue, 11 Apr 2023 08:12:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3684C3858002 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=oss.cipunited.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=oss.cipunited.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; s=s1; d=oss-cipunited-com.20200927.dkim.feishu.cn; t=1681200766; h=from:subject:mime-version:from:date:message-id:subject:to:cc: reply-to:content-type:mime-version:in-reply-to:message-id; bh=d6onvIp9GAcNWJ4oUuN/PsDp9wDW/TRysVQjN+8umHg=; b=jjKSu9Bom/khQgOjPCyqtajcD9XH2lK/yS/+q7FCD9prYHtqbRaoJc78xpcSOdwE+b0NPu v5991kefLZqtipVmZltmvWRlF6VjP1TwATN1XCof8fTmIWOqTzfZtYPEhZmt4BA8r/PZsa v+Hc4EGgHq2aWf6lmuInD7T00StWHU7N+b10PDqwt1V6wPs/T4klI9XEqRe80S+DJrz/7p MszUBxcAfJjTUwwjXIYOwNlb13YDhpFtzjSjKA2qGwDP8X6M5Q2869MaTTlGUWpIMomPEo tQWskGMV6+LZm+yKZMEcc76g591Tb0jJA8JJ3j8T4EtvnrwYWNmHVLPQ8z/gIQ== Content-Type: multipart/alternative; boundary=9cb8c710e05c0c77d8c331564e3aef4d87570ab0f2cfe151ce1f9abe8705 From: "Ying Huang" Cc: "Ying Huang" Message-Id: <20230411081141.1762395-4-ying.huang@oss.cipunited.com> X-Lms-Return-Path: In-Reply-To: <20230411081141.1762395-1-ying.huang@oss.cipunited.com> Content-Transfer-Encoding: 8bit Reply-To: ying.huang@oss.cipunited.com Subject: [PATCH 3/5] elflint: Fix invalid type of relocation info and other issues on mips References: <20230411081141.1762395-1-ying.huang@oss.cipunited.com> To: Mime-Version: 1.0 X-Original-From: "Ying Huang" X-Mailer: git-send-email 2.30.2 Date: Tue, 11 Apr 2023 16:12:46 +0800 X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,GIT_PATCH_0,HTML_MESSAGE,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --9cb8c710e05c0c77d8c331564e3aef4d87570ab0f2cfe151ce1f9abe8705 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 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; } 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; } =20 +bool +mips_check_reloc_target_type (Ebl *ebl __attribute__ ((unused)), Elf64_Wor= d sh_type) +{ + return (sh_type =3D=3D 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) !=3D 0) + return false; + const char *sname =3D elf_strptr (elf, shstrndx, destshdr->sh_name); + if (sname =3D=3D NULL) + return false; + return (strcmp (sname, ".got") =3D=3D 0 || strcmp (sname, ".bss") =3D=3D= 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)) =3D=3D 0); +} + /* 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 =20 #include - +#include =20 bool ebl_reloc_valid_use (Ebl *ebl, int reloc) { - return ebl !=3D NULL ? ebl->reloc_valid_use (ebl->elf, reloc) : false; + int relocNew =3D reloc; + GElf_Ehdr ehdr; + if(ebl->elf->class =3D=3D ELFCLASS64 && gelf_getehdr(ebl->elf, &ehdr) != =3D NULL && ehdr.e_machine =3D=3D EM_MIPS) + relocNew =3D ELF64_MIPS_R_TYPE(reloc); + return ebl !=3D NULL ? ebl->reloc_valid_use (ebl->elf, relocNew) : false; } 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 } =20 if (GELF_ST_TYPE (sym->st_info) =3D=3D STT_SECTION - && GELF_ST_BIND (sym->st_info) !=3D STB_LOCAL) + && GELF_ST_BIND (sym->st_info) !=3D STB_LOCAL + && ehdr->e_machine !=3D EM_MIPS + && strcmp (name, "_DYNAMIC_LINK") !=3D 0 + && strcmp (name, "_DYNAMIC_LINKING") !=3D 0) 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 =3D SHT_NOBITS; =20 + if (ehdr->e_machine =3D=3D EM_MIPS + && (strstr(special_sections[s].name, ".debug") !=3D NULL)) + { + good_type =3D 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 !=3D good_type @@ -3953,8 +3962,16 @@ section [%2zu] '%s': size not multiple of entry size= \n"), sh_flags &=3D ~(GElf_Xword) SHF_MASKPROC; } if (sh_flags & SHF_MASKOS) - if (gnuld) - sh_flags &=3D ~(GElf_Xword) SHF_GNU_RETAIN; + { + if (gnuld) + sh_flags &=3D ~(GElf_Xword) SHF_GNU_RETAIN; + if (ehdr->e_machine =3D=3D EM_MIPS) + { + if(sh_flags =3D=3D SHF_MIPS_NOSTRIP || sh_flags =3D=3D SHF_MIPS= _LOCAL + || sh_flags =3D=3D SHF_MIPS_NAMES || sh_flags =3D=3D SHF_MIPS_NODUPE) + sh_flags &=3D ~shdr->sh_flags; + } + } if (sh_flags !=3D 0) ERROR (_("section [%2zu] '%s' contains unknown flag(s)" " %#" PRIx64 "\n"), --=20 2.30.2= --9cb8c710e05c0c77d8c331564e3aef4d87570ab0f2cfe151ce1f9abe8705--