public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] [RFC] Come up with startswith function.
@ 2021-03-18 14:20 Martin Liška
  2021-03-18 14:54 ` Tom Tromey
  2021-03-18 18:29 ` Hans-Peter Nilsson
  0 siblings, 2 replies; 41+ messages in thread
From: Martin Liška @ 2021-03-18 14:20 UTC (permalink / raw)
  To: binutils

Hello.

This patch is the same what I suggested for the GCC project:
https://gcc.gnu.org/pipermail/gcc-patches/2021-March/566897.html

In case on binutils, it's hard to find a single header file that
is always used after string.h is included. That's why I for now
use __builtin_* functions.

Is the binutils community interested in the function?
Thanks,
Martin

---
  bfd/elf.c          | 17 ++++++++---------
  binutils/objcopy.c | 18 +++++++++---------
  include/ansidecl.h |  8 ++++++++
  3 files changed, 25 insertions(+), 18 deletions(-)

diff --git a/bfd/elf.c b/bfd/elf.c
index 35c31cf40bf..91f8694ad4f 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -1084,18 +1084,18 @@ _bfd_elf_make_section_from_shdr (bfd *abfd,
  	 not any sort of flag.  Their SEC_ALLOC bits are cleared.  */
        if (name [0] == '.')
  	{
-	  if (strncmp (name, ".debug", 6) == 0
-	      || strncmp (name, ".gnu.linkonce.wi.", 17) == 0
-	      || strncmp (name, ".zdebug", 7) == 0)
+	  if (startswith (name, ".debug")
+	      || startswith (name, ".gnu.linkonce.wi.")
+	      || startswith (name, ".zdebug"))
  	    flags |= SEC_DEBUGGING | SEC_ELF_OCTETS;
-	  else if (strncmp (name, GNU_BUILD_ATTRS_SECTION_NAME, 21) == 0
-		   || strncmp (name, ".note.gnu", 9) == 0)
+	  else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
+		   || startswith (name, ".note.gnu"))
  	    {
  	      flags |= SEC_ELF_OCTETS;
  	      opb = 1;
  	    }
-	  else if (strncmp (name, ".line", 5) == 0
-		   || strncmp (name, ".stab", 5) == 0
+	  else if (startswith (name, ".line")
+		   || startswith (name, ".stab")
  		   || strcmp (name, ".gdb_index") == 0)
  	    flags |= SEC_DEBUGGING;
  	}
@@ -1276,8 +1276,7 @@ _bfd_elf_make_section_from_shdr (bfd *abfd,
  
    /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information
       section.  */
-  const char *lto_section_name = ".gnu.lto_.lto.";
-  if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0)
+  if (startswith (name, ".gnu.lto_.lto."))
      {
        struct lto_section lsection;
        if (bfd_get_section_contents (abfd, newsect, &lsection, 0,
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index d58f910f2fa..7c3d28d35cb 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -1284,7 +1284,7 @@ is_dwo_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
    if (len < 5)
      return FALSE;
  
-  return strncmp (name + len - 4, ".dwo", 4) == 0;
+  return startswith (name + len - 4, ".dwo");
  }
  
  /* Return TRUE if section SEC is in the update list.  */
@@ -4260,7 +4260,7 @@ static void
  handle_remove_section_option (const char *section_pattern)
  {
    find_section_list (section_pattern, TRUE, SECTION_CONTEXT_REMOVE);
-  if (strncmp (section_pattern, ".rel", 4) == 0)
+  if (startswith (section_pattern, ".rel"))
      {
        section_pattern += 4;
        if (*section_pattern == 'a')
@@ -5874,13 +5874,13 @@ copy_main (int argc, char *argv[])
  
    /* Convert input EFI target to PEI target.  */
    if (input_target != NULL
-      && strncmp (input_target, "efi-", 4) == 0)
+      && startswith (input_target, "efi-"))
      {
        char *efi;
  
        efi = xstrdup (output_target + 4);
-      if (strncmp (efi, "bsdrv-", 6) == 0
-	  || strncmp (efi, "rtdrv-", 6) == 0)
+      if (startswith (efi, "bsdrv-")
+	  || startswith (efi, "rtdrv-"))
  	efi += 2;
        else if (strncmp (efi, "app-", 4) != 0)
  	fatal (_("unknown input EFI target: %s"), input_target);
@@ -5891,23 +5891,23 @@ copy_main (int argc, char *argv[])
  
    /* Convert output EFI target to PEI target.  */
    if (output_target != NULL
-      && strncmp (output_target, "efi-", 4) == 0)
+      && startswith (output_target, "efi-"))
      {
        char *efi;
  
        efi = xstrdup (output_target + 4);
-      if (strncmp (efi, "app-", 4) == 0)
+      if (startswith (efi, "app-"))
  	{
  	  if (pe_subsystem == -1)
  	    pe_subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION;
  	}
-      else if (strncmp (efi, "bsdrv-", 6) == 0)
+      else if (startswith (efi, "bsdrv-"))
  	{
  	  if (pe_subsystem == -1)
  	    pe_subsystem = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER;
  	  efi += 2;
  	}
-      else if (strncmp (efi, "rtdrv-", 6) == 0)
+      else if (startswith (efi, "rtdrv-"))
  	{
  	  if (pe_subsystem == -1)
  	    pe_subsystem = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER;
diff --git a/include/ansidecl.h b/include/ansidecl.h
index 0515228f325..5d11fd130b0 100644
--- a/include/ansidecl.h
+++ b/include/ansidecl.h
@@ -429,6 +429,14 @@ So instead we use the macro below and test it against specific values.  */
    void operator= (const TYPE &)
  #endif /* __cplusplus >= 201103 */
  
+/* Return 1 if STR string starts with PREFIX.  */
+
+static inline int
+startswith (const char *str, const char *prefix)
+{
+  return __builtin_strncmp (str, prefix, __builtin_strlen (prefix)) == 0;
+}
+
  #ifdef __cplusplus
  }
  #endif
-- 
2.30.2


^ permalink raw reply	[flat|nested] 41+ messages in thread

end of thread, other threads:[~2021-04-01 13:03 UTC | newest]

Thread overview: 41+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-18 14:20 [PATCH] [RFC] Come up with startswith function Martin Liška
2021-03-18 14:54 ` Tom Tromey
2021-03-18 15:26   ` Martin Liška
2021-03-18 18:00     ` Tom Tromey
2021-03-18 18:29 ` Hans-Peter Nilsson
2021-03-19  6:37   ` Alan Modra
2021-03-19 12:44     ` [PATCH] Add startswith function and use it instead of CONST_STRNEQ Martin Liška
2021-03-19 17:55       ` Hans-Peter Nilsson
2021-03-20  7:00       ` Alan Modra
2021-03-20 18:58         ` Tom Tromey
2021-03-21 13:12           ` Alan Modra
2021-03-22  2:13             ` Tom Tromey
2021-03-22 12:06               ` Alan Modra
2021-03-22 16:13                 ` Luis Machado
2021-03-22 22:56                   ` Alan Modra
2021-03-25 10:53                     ` Luis Machado
2021-03-25 11:54                       ` Alan Modra
2021-03-25 12:05                         ` Luis Machado
2021-03-25 19:47                         ` Luis Machado
2021-03-25 22:31                           ` Mike Frysinger
2021-03-26 11:44                             ` Luis Machado
2021-03-30 11:58                               ` Luis Machado
2021-03-31 13:12                                 ` Martin Liška
2021-03-31 13:44                                   ` Luis Machado
2021-03-22 16:42                 ` Martin Liška
2021-03-23  0:02                   ` Alan Modra
2021-03-23  4:49                     ` Mike Frysinger
2021-03-24  8:19                     ` [PATCH 0/5] Start using startswith instead of strncmp Martin Liska
2021-03-18 14:16                       ` [PATCH 2/5] Use startswith more for strncmp function calls Martin Liska
2021-03-22 11:12                       ` [PATCH 1/5] Replace const_strneq with startswith Martin Liska
2021-03-22 12:33                       ` [PATCH 3/5] Use startswith in gas subfolder Martin Liska
2021-03-22 13:56                       ` [PATCH 4/5] Remove strneq macro and use startswith Martin Liska
2021-03-23  9:02                       ` [PATCH 5/5] Use startswith in gdb subfolder Martin Liska
2021-03-31 20:26                         ` Tom Tromey
2021-04-01  5:21                       ` [PATCH 0/5] Start using startswith instead of strncmp Martin Liška
2021-04-01 12:47                         ` Alan Modra
2021-04-01 13:03                           ` Martin Liška
2021-03-31 20:18                     ` [PATCH] Add startswith function and use it instead of CONST_STRNEQ Tom Tromey
2021-03-22  6:57             ` Mike Frysinger
2021-03-21 13:19       ` Alan Modra
2021-03-22 10:04         ` Martin Liška

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).