https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124581
Bug ID: 124581
Summary: -flto -ffile-prefix-map does not work for files
without slash
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: [email protected]
Target Milestone: ---
I found some cases where `gcc -flto -fcanon-prefix-map -ffile-prefix-map` does
not replace the path. It all seems to be related to paths without a directory
segment; like `test.c` vs. `./test.c`.
Here is the first test case *test-1.c*:
```
static const char * const text[] = { "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 0 };
struct error_table { char const * const * msgs; };
const struct error_table et_ext2_error_table = { text };
```
If you compile this with `test-1.c` the path mapping isn't applied. But running
gcc with `./test-1.c` applies the mapping.
Here is a helper script *test.sh* to test this and the later cases
```
#!/bin/sh
set -e
: "${CC:=gcc}"
$CC --version
case "$CC" in
*gcc*) cc_opt=-fcanon-prefix-map;;
esac
echo "pwd = $PWD"
set -x
for i in '' ./
do
$CC -flto $cc_opt -ffile-prefix-map=${PWD%/*}=/src -c "$i"test-1.c -o
test-1.o
strings test-1.o |grep --color ${PWD%/*} || true
done
for i in 2a 2b 3
do
$CC -flto $cc_opt -ffile-prefix-map=${PWD%/*}=/src -c ./test-"$i".c -o
test-"$i".o
strings test-"$i".o |grep --color ${PWD%/*} || true
done
```
```
% sh test.sh
gcc (Debian 15.2.0-15) 15.2.0
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
pwd = /tmp/gcc/dir
+ gcc -flto -fcanon-prefix-map -ffile-prefix-map=/tmp/gcc=/src -c test-1.c -o
test-1.o
+ strings test-1.o
+ grep --color /tmp/gcc
/tmp/gcc/dir
/tmp/gcc/dir
+ gcc -flto -fcanon-prefix-map -ffile-prefix-map=/tmp/gcc=/src -c ./test-1.c -o
test-1.o
+ strings test-1.o
+ grep --color /tmp/gcc
+ true
```
The same is with `#line`: test-2a.c is without a slash and test-2b.c is with
slash:
```
#line 373 "test-1.c"
static const char * const text[] = { "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 0 };
struct error_table { char const * const * msgs; };
const struct error_table et_ext2_error_table = { text };
```
```
#line 373 "./test-1.c"
static const char * const text[] = { "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 0 };
struct error_table { char const * const * msgs; };
const struct error_table et_ext2_error_table = { text };
```
```
+ gcc -flto -fcanon-prefix-map -ffile-prefix-map=/tmp/gcc=/src -c ./test-2a.c
-o test-2a.o
+ strings test-2a.o
+ grep --color /tmp/gcc
/tmp/gcc/dir
/tmp/gcc/dir
+ gcc -flto -fcanon-prefix-map -ffile-prefix-map=/tmp/gcc=/src -c ./test-2b.c
-o test-2b.o
+ strings test-2b.o
+ grep --color /tmp/gcc
+ true
```
And if the path in `#line` refers to a non-existing file the mapping does not
happen
```
#line 373 "non-existing.c"
static const char * const text[] = { "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 0 };
struct error_table { char const * const * msgs; };
const struct error_table et_ext2_error_table = { text };
```
```
+ gcc -flto -fcanon-prefix-map -ffile-prefix-map=/tmp/gcc=/src -c ./test-3.c -o
test-3.o
+ strings test-3.o
+ grep --color /tmp/gcc
/tmp/gcc/dir
/tmp/gcc/dir
```