1 /* Implementation of file prefix remapping support (-f*-prefix-map options).
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 3, or (at your option) any
7 later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
17
18 #include "config.h"
19 #include "system.h"
20 #include "coretypes.h"
21 #include "diagnostic.h"
22 #include "file-prefix-map.h"
23
24 /* Structure recording the mapping from source file and directory names at
25 compile time to those to be embedded in the compilation result (debug
26 information, the __FILE__ macro expansion, etc). */
27 struct file_prefix_map
28 {
29 const char *old_prefix;
30 const char *new_prefix;
31 size_t old_len;
32 size_t new_len;
33 struct file_prefix_map *next;
34 };
35
36 /* Record a file prefix mapping in the specified map. ARG is the argument to
37 -f*-prefix-map and must be of the form OLD=NEW. OPT is the option name
38 for diagnostics. */
39 static void
add_prefix_map(file_prefix_map * & maps,const char * arg,const char * opt)40 add_prefix_map (file_prefix_map *&maps, const char *arg, const char *opt)
41 {
42 file_prefix_map *map;
43 const char *p, *old;
44 size_t oldlen;
45
46 /* Note: looking for the last '='. The thinking is we can control the paths
47 inside our projects but not where the users build them. */
48 p = strrchr (arg, '=');
49 if (!p)
50 {
51 error ("invalid argument %qs to %qs", arg, opt);
52 return;
53 }
54 if (*arg == '$')
55 {
56 char *env = xstrndup (arg + 1, p - (arg + 1));
57 old = getenv(env);
58 if (!old)
59 {
60 warning (0, "environment variable %qs not set in argument to "
61 "%s", env, opt);
62 free(env);
63 return;
64 }
65 oldlen = strlen(old);
66 free(env);
67 }
68 else
69 {
70 old = xstrndup (arg, p - arg);
71 oldlen = p - arg;
72 }
73 map = XNEW (file_prefix_map);
74 map->old_prefix = old;
75 map->old_len = oldlen;
76 p++;
77 map->new_prefix = xstrdup (p);
78 map->new_len = strlen (p);
79 map->next = maps;
80 maps = map;
81 }
82
83 /* Perform user-specified mapping of filename prefixes. Return the
84 GC-allocated new name corresponding to FILENAME or FILENAME if no
85 remapping was performed. */
86
87 static const char *
remap_filename(file_prefix_map * maps,const char * filename)88 remap_filename (file_prefix_map *maps, const char *filename)
89 {
90 file_prefix_map *map;
91 char *s;
92 const char *name;
93 size_t name_len;
94
95 for (map = maps; map; map = map->next)
96 if (filename_ncmp (filename, map->old_prefix, map->old_len) == 0)
97 break;
98 if (!map)
99 return filename;
100 name = filename + map->old_len;
101 name_len = strlen (name) + 1;
102
103 s = (char *) ggc_alloc_atomic (name_len + map->new_len);
104 memcpy (s, map->new_prefix, map->new_len);
105 memcpy (s + map->new_len, name, name_len);
106 return s;
107 }
108
109 /* NOTE: if adding another -f*-prefix-map option then don't forget to
110 ignore it in DW_AT_producer (dwarf2out.cc). */
111
112 /* Linked lists of file_prefix_map structures. */
113 static file_prefix_map *macro_prefix_maps; /* -fmacro-prefix-map */
114 static file_prefix_map *debug_prefix_maps; /* -fdebug-prefix-map */
115 static file_prefix_map *profile_prefix_maps; /* -fprofile-prefix-map */
116
117 /* Record a file prefix mapping for -fmacro-prefix-map. */
118 void
add_macro_prefix_map(const char * arg)119 add_macro_prefix_map (const char *arg)
120 {
121 add_prefix_map (macro_prefix_maps, arg, "-fmacro-prefix-map");
122 }
123
124 /* Record a file prefix mapping for -fdebug-prefix-map. */
125 void
add_debug_prefix_map(const char * arg)126 add_debug_prefix_map (const char *arg)
127 {
128 add_prefix_map (debug_prefix_maps, arg, "-fdebug-prefix-map");
129 }
130
131 /* Record a file prefix mapping for all -f*-prefix-map. */
132 void
add_file_prefix_map(const char * arg)133 add_file_prefix_map (const char *arg)
134 {
135 add_prefix_map (macro_prefix_maps, arg, "-ffile-prefix-map");
136 add_prefix_map (debug_prefix_maps, arg, "-ffile-prefix-map");
137 add_prefix_map (profile_prefix_maps, arg, "-ffile-prefix-map");
138 }
139
140 /* Record a file prefix mapping for -fprofile-prefix-map. */
141 void
add_profile_prefix_map(const char * arg)142 add_profile_prefix_map (const char *arg)
143 {
144 add_prefix_map (profile_prefix_maps, arg, "-fprofile-prefix-map");
145 }
146
147 /* Remap using -fmacro-prefix-map. Return the GC-allocated new name
148 corresponding to FILENAME or FILENAME if no remapping was performed. */
149 const char *
remap_macro_filename(const char * filename)150 remap_macro_filename (const char *filename)
151 {
152 return remap_filename (macro_prefix_maps, filename);
153 }
154
155 /* Original GCC version disabled. The NetBSD version handles regex */
156 #if 0
157 /* Remap using -fdebug-prefix-map. Return the GC-allocated new name
158 corresponding to FILENAME or FILENAME if no remapping was performed. */
159 const char *
160 remap_debug_filename (const char *filename)
161 {
162 return remap_filename (debug_prefix_maps, filename);
163 }
164 #endif
165
166 /*****
167 ***** The following code is a NetBSD extension that allows regex and
168 ***** \[0-9] substitutition arguments.
169 *****/
170
171 /* Perform user-specified mapping of debug filename prefixes. Return
172 the new name corresponding to FILENAME. */
173
174 static const char *
remap_debug_prefix_filename(const char * filename)175 remap_debug_prefix_filename (const char *filename)
176 {
177 file_prefix_map *map;
178 char *s;
179 const char *name;
180 size_t name_len;
181
182 for (map = debug_prefix_maps; map; map = map->next)
183 if (filename_ncmp (filename, map->old_prefix, map->old_len) == 0)
184 break;
185 if (!map)
186 return filename;
187 name = filename + map->old_len;
188 name_len = strlen (name) + 1;
189 s = (char *) alloca (name_len + map->new_len);
190 memcpy (s, map->new_prefix, map->new_len);
191 memcpy (s + map->new_len, name, name_len);
192 return ggc_strdup (s);
193 }
194
195 #include <regex.h>
196
197 typedef struct debug_regex_map
198 {
199 regex_t re;
200 const char *sub;
201 struct debug_regex_map *next;
202 } debug_regex_map;
203
204 /* Linked list of such structures. */
205 debug_regex_map *debug_regex_maps;
206
207
208 /* Record a debug file regex mapping. ARG is the argument to
209 -fdebug-regex-map and must be of the form OLD=NEW. */
210
211 void
add_debug_regex_map(const char * arg)212 add_debug_regex_map (const char *arg)
213 {
214 debug_regex_map *map;
215 const char *p;
216 char *old;
217 char buf[1024];
218 regex_t re;
219 int e;
220
221 p = strchr (arg, '=');
222 if (!p)
223 {
224 error ("invalid argument %qs to -fdebug-regex-map", arg);
225 return;
226 }
227
228 old = xstrndup (arg, p - arg);
229 if ((e = regcomp(&re, old, REG_EXTENDED)) != 0)
230 {
231 regerror(e, &re, buf, sizeof(buf));
232 warning (0, "regular expression compilation for %qs in argument to "
233 "-fdebug-regex-map failed: %qs", old, buf);
234 free(old);
235 return;
236 }
237 free(old);
238
239 map = XNEW (debug_regex_map);
240 map->re = re;
241 p++;
242 map->sub = xstrdup (p);
243 map->next = debug_regex_maps;
244 debug_regex_maps = map;
245 }
246
247 extern "C" ssize_t regasub(char **, const char *,
248 const regmatch_t *rm, const char *);
249
250 /* Perform user-specified mapping of debug filename regular expressions. Return
251 the new name corresponding to FILENAME. */
252
253 static const char *
remap_debug_regex_filename(const char * filename)254 remap_debug_regex_filename (const char *filename)
255 {
256 debug_regex_map *map;
257 char *s;
258 regmatch_t rm[10];
259
260 for (map = debug_regex_maps; map; map = map->next)
261 if (regexec (&map->re, filename, 10, rm, 0) == 0
262 && regasub (&s, map->sub, rm, filename) >= 0)
263 {
264 const char *name = ggc_strdup(s);
265 free(s);
266 return name;
267 }
268 return filename;
269 }
270
271 const char *
remap_debug_filename(const char * filename)272 remap_debug_filename (const char *filename)
273 {
274 return remap_debug_regex_filename (remap_debug_prefix_filename (filename));
275 }
276
277 /* Remap using -fprofile-prefix-map. Return the GC-allocated new name
278 corresponding to FILENAME or FILENAME if no remapping was performed. */
279 const char *
remap_profile_filename(const char * filename)280 remap_profile_filename (const char *filename)
281 {
282 return remap_filename (profile_prefix_maps, filename);
283 }
284