[PATCH] [RFC] Come up with startswith function.
Martin Liška
mliska@suse.cz
Thu Mar 18 14:20:04 GMT 2021
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
More information about the Binutils
mailing list