https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120970

            Bug ID: 120970
           Summary: -static-pie together with -fsanitize=address should be
                    disallowed
           Product: gcc
           Version: 15.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: sanitizer
          Assignee: unassigned at gcc dot gnu.org
          Reporter: christian.morales.vega at gmail dot com
                CC: dodji at gcc dot gnu.org, dvyukov at gcc dot gnu.org,
                    jakub at gcc dot gnu.org, kcc at gcc dot gnu.org
  Target Milestone: ---

If I take this code

```
static int x[1];

int main() {
  x[1] = 5;
}
```
and compile it with "gcc -o test test.c -fsanitize=address", ASan correctly
complains when running "test".

If I add "-static" to it, it correctly fails to even try to build anything
```
$ gcc -o test test.c -fsanitize=address -static
gcc: error: cannot specify -static with -fsanitize=address
```

But if I use -static-pie instead of -static, I only get
```
# gcc -o test test.c -fsanitize=address -static-pie -fpie
/usr/bin/ld:
/usr/lib/gcc/x86_64-redhat-linux/15/libasan.a(asan_interceptors.o): in function
`InitializeCommonInterceptors()':
(.text+0xea7a): warning: Using 'dlopen' in statically linked applications
requires at runtime the shared libraries from the glibc version used for
linking
/usr/bin/ld:
/usr/lib/gcc/x86_64-redhat-linux/15/libasan.a(asan_interceptors.o): in function
`___interceptor_lgamma.part.0':
(.text+0x173d7): undefined reference to `signgam'
/usr/bin/ld: (.text+0x1748f): undefined reference to `signgam'
/usr/bin/ld: (.text+0x174af): undefined reference to `signgam'
/usr/bin/ld:
/usr/lib/gcc/x86_64-redhat-linux/15/libasan.a(asan_interceptors.o): in function
`___interceptor_lgammaf.part.0':
(.text+0x17737): undefined reference to `signgam'
/usr/bin/ld: (.text+0x177ef): undefined reference to `signgam'
/usr/bin/ld:
/usr/lib/gcc/x86_64-redhat-linux/15/libasan.a(asan_interceptors.o):(.text+0x1780f):
more undefined references to `signgam' follow
collect2: error: ld returned 1 exit status
```

And by adding -lm, I can actually make it create a binary
```
# gcc -o test test.c -fsanitize=address -static-pie -fpie -lm
/usr/bin/ld:
/usr/lib/gcc/x86_64-redhat-linux/15/libasan.a(asan_interceptors.o): in function
`InitializeCommonInterceptors()':
(.text+0xea7a): warning: Using 'dlopen' in statically linked applications
requires at runtime the shared libraries from the glibc version used for
linking
```

which fails in a more (not really) "surprising" way
```
$ ./test 
AddressSanitizer: CHECK failed:
sanitizer_common_interceptors_memintrinsics.inc:239
"((__interception::real_memcpy)) != (0)" (0x0, 0x0) (tid=160)
    <empty stack>

$
```

So, it seems to me, using -static-pie should make gcc complaing with "gcc:
error: cannot specify -static-pie with -fsanitize=address".

Reply via email to