gdbserver build broken with -fsanitize=address

Tom de Vries tdevries@suse.de
Wed Jan 6 07:39:22 GMT 2021


[ was: Re: [PATCH] gdb: improve command completion for 'print', 'x', and
'display' ]

On 12/11/20 11:07 PM, Andrew Burgess wrote:
> * Tom Tromey <tom@tromey.com> [2020-12-03 10:03:06 -0700]:
> 
>>>>>>> "Andrew" == Andrew Burgess <andrew.burgess@embecosm.com> writes:
>>
>>>> skip_* are intended for use by gdb commands -- they originally lived in
>>>> gdb/cli/ -- so if they do the wrong thing in some situation, they should
>>>> definitely be changed.
>>
>> Andrew> Like this?
>>
>> Works for me, thanks.
> 
> Turns out that libinproctrace.so did not link against libiberty, so I
> fixed that and pushed the patch below.
> 

Hi,

I tried to build gdb with asan (using CFLAGS/CXXFLAGS="-O0 -g
-fsanitize=address" and LDFLAGS=-lasan), and ran into:
...
/usr/lib64/gcc/x86_64-suse-linux/11/../../../../x86_64-suse-linux/bin/ld: ../libiberty/libiberty.a(safe-ctype.o):
relocation R_X86_64_32 against `.data' can not be used when making a
shared object; recompile with -fPIC
collect2: error: ld returned 1 exit status
make[1]: *** [libinproctrace.so] Error 1
...

Using this patch, I got things building again:
...
diff --git a/gdbserver/Makefile.in b/gdbserver/Makefile.in
index a2fe302d247..671cc67177e 100644
--- a/gdbserver/Makefile.in
+++ b/gdbserver/Makefile.in
@@ -102,7 +102,7 @@ CC_LD = $(CXX) $(CXX_DIALECT)
 INCLUDE_DIR = ${srcdir}/../include
 INCLUDE_DEP = $$(INCLUDE_DIR)

-LIBIBERTY_BUILDDIR = ../libiberty
+LIBIBERTY_BUILDDIR = ../libiberty/noasan
 LIBIBERTY = $(LIBIBERTY_BUILDDIR)/libiberty.a

 GDBSUPPORT_BUILDDIR = ../gdbsupport
...
but this means that libiberty/noasan is also used for the gdbserver
binary, which is probably too restrictive.

I've looked at gcc-repo/src/libcc1/Makefile.am, where we have:
...
override CXXFLAGS := $(filter-out -fsanitize=address,$(CXXFLAGS))
override LDFLAGS := $(filter-out -fsanitize=address,$(LDFLAGS))
# Can be simplified when libiberty becomes a normal convenience library.

libiberty_normal = ../libiberty/libiberty.a
libiberty_noasan = ../libiberty/noasan/libiberty.a
libiberty_pic = ../libiberty/pic/libiberty.a
Wc=-Wc,
libiberty = $(if $(wildcard $(libiberty_noasan)),$(Wc)$(libiberty_noasan), \
            $(if $(wildcard $(libiberty_pic)),$(Wc)$(libiberty_pic), \
            $(Wc)$(libiberty_normal)))
libiberty_dep = $(patsubst $(Wc)%,%,$(libiberty))
...
but I haven't been able to decode what this does yet.

Thanks,
- Tom

> Thanks,
> Andrew
> 
> 
> --
> 
> commit 966484941738b7a474fb7e4fe29eb5693fc9096c
> Author: Andrew Burgess <andrew.burgess@embecosm.com>
> Date:   Fri Nov 20 17:23:03 2020 +0000
> 
>     gdbsupport: make use of safe-ctype functions from libiberty
>     
>     Make use of the safe-ctype replacements for the standard ctype
>     character checking functions in gdbsupport/common-utils.cc.  The
>     gdbsupport library is included into both gdb and gdbserver, and on the
>     gdbserver side there are two targets, gdbserver itself, and also
>     libinproctrace.so.
>     
>     libiberty was already being included in the gdbserver link command,
>     but was missing from the libinproctrace.so link.  As a result, after
>     changing gdbsupport/common-utils.cc to depend on libiberty,
>     libinproctrace.so would no longer link until I modified its link line.
>     
>     gdbserver/ChangeLog:
>     
>             * Makefile.in (IPA_LIB): Include libiberty library.
>     
>     gdbsupport/ChangeLog:
>     
>             * gdbsupport/common-utils.cc: Change 'ctype.h' include to
>             'safe-ctype.h'.
>             (extract_string_maybe_quoted): Use safe-ctype function versions.
>             (is_digit_in_base): Likewise.
>             (digit_to_int): Likewise.
>             (strtoulst): Likewise.
>             (skip_spaces): Likewise.
>             (skip_to_space): Likewise.
> 
> diff --git a/gdbserver/Makefile.in b/gdbserver/Makefile.in
> index 1969ed0ec37..e12848c1ce2 100644
> --- a/gdbserver/Makefile.in
> +++ b/gdbserver/Makefile.in
> @@ -395,7 +395,7 @@ $(IPA_LIB): $(sort $(IPA_OBJS)) ${CDEPS}
>  	$(ECHO_CXXLD) $(CC_LD) -shared -fPIC -Wl,--soname=$(IPA_LIB) \
>  		-Wl,--no-undefined $(INTERNAL_CFLAGS) $(INTERNAL_LDFLAGS) \
>  		 $(CXXFLAGS) \
> -		-o $(IPA_LIB) ${IPA_OBJS} -ldl -pthread
> +		-o $(IPA_LIB) ${IPA_OBJS} $(LIBIBERTY) -ldl -pthread
>  
>  # Put the proper machine-specific files first, so M-. on a machine
>  # specific routine gets the one for the correct machine.
> diff --git a/gdbsupport/common-utils.cc b/gdbsupport/common-utils.cc
> index b5e4d2928ec..4f5c26d075c 100644
> --- a/gdbsupport/common-utils.cc
> +++ b/gdbsupport/common-utils.cc
> @@ -20,7 +20,7 @@
>  #include "common-defs.h"
>  #include "common-utils.h"
>  #include "host-defs.h"
> -#include <ctype.h>
> +#include "safe-ctype.h"
>  
>  void *
>  xzalloc (size_t size)
> @@ -177,7 +177,7 @@ extract_string_maybe_quoted (const char **arg)
>    /* Parse p similarly to gdb_argv buildargv function.  */
>    while (*p != '\0')
>      {
> -      if (isspace (*p) && !squote && !dquote && !bsquote)
> +      if (ISSPACE (*p) && !squote && !dquote && !bsquote)
>  	break;
>        else
>  	{
> @@ -230,21 +230,21 @@ extract_string_maybe_quoted (const char **arg)
>  static int
>  is_digit_in_base (unsigned char digit, int base)
>  {
> -  if (!isalnum (digit))
> +  if (!ISALNUM (digit))
>      return 0;
>    if (base <= 10)
> -    return (isdigit (digit) && digit < base + '0');
> +    return (ISDIGIT (digit) && digit < base + '0');
>    else
> -    return (isdigit (digit) || tolower (digit) < base - 10 + 'a');
> +    return (ISDIGIT (digit) || TOLOWER (digit) < base - 10 + 'a');
>  }
>  
>  static int
>  digit_to_int (unsigned char c)
>  {
> -  if (isdigit (c))
> +  if (ISDIGIT (c))
>      return c - '0';
>    else
> -    return tolower (c) - 'a' + 10;
> +    return TOLOWER (c) - 'a' + 10;
>  }
>  
>  /* As for strtoul, but for ULONGEST results.  */
> @@ -258,7 +258,7 @@ strtoulst (const char *num, const char **trailer, int base)
>    int i = 0;
>  
>    /* Skip leading whitespace.  */
> -  while (isspace (num[i]))
> +  while (ISSPACE (num[i]))
>      i++;
>  
>    /* Handle prefixes.  */
> @@ -325,7 +325,7 @@ skip_spaces (char *chp)
>  {
>    if (chp == NULL)
>      return NULL;
> -  while (*chp && isspace (*chp))
> +  while (*chp && ISSPACE (*chp))
>      chp++;
>    return chp;
>  }
> @@ -337,7 +337,7 @@ skip_spaces (const char *chp)
>  {
>    if (chp == NULL)
>      return NULL;
> -  while (*chp && isspace (*chp))
> +  while (*chp && ISSPACE (*chp))
>      chp++;
>    return chp;
>  }
> @@ -349,7 +349,7 @@ skip_to_space (const char *chp)
>  {
>    if (chp == NULL)
>      return NULL;
> -  while (*chp && !isspace (*chp))
> +  while (*chp && !ISSPACE (*chp))
>      chp++;
>    return chp;
>  }
> 


More information about the Gdb-patches mailing list