1*a9fa9459Szrj /* Remap file names for debug info for GNU assembler.
2*a9fa9459Szrj Copyright (C) 2007-2016 Free Software Foundation, Inc.
3*a9fa9459Szrj
4*a9fa9459Szrj This file is part of GAS, the GNU Assembler.
5*a9fa9459Szrj
6*a9fa9459Szrj GAS is free software; you can redistribute it and/or modify
7*a9fa9459Szrj it under the terms of the GNU General Public License as published by
8*a9fa9459Szrj the Free Software Foundation; either version 3, or (at your option)
9*a9fa9459Szrj any later version.
10*a9fa9459Szrj
11*a9fa9459Szrj GAS is distributed in the hope that it will be useful,
12*a9fa9459Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
13*a9fa9459Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*a9fa9459Szrj GNU General Public License for more details.
15*a9fa9459Szrj
16*a9fa9459Szrj You should have received a copy of the GNU General Public License
17*a9fa9459Szrj along with GAS; see the file COPYING. If not, write to the Free
18*a9fa9459Szrj Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19*a9fa9459Szrj 02110-1301, USA. */
20*a9fa9459Szrj
21*a9fa9459Szrj #include "as.h"
22*a9fa9459Szrj #include "filenames.h"
23*a9fa9459Szrj
24*a9fa9459Szrj /* Structure recording the mapping from source file and directory
25*a9fa9459Szrj names at compile time to those to be embedded in debug
26*a9fa9459Szrj information. */
27*a9fa9459Szrj typedef struct debug_prefix_map
28*a9fa9459Szrj {
29*a9fa9459Szrj const char *old_prefix;
30*a9fa9459Szrj const char *new_prefix;
31*a9fa9459Szrj size_t old_len;
32*a9fa9459Szrj size_t new_len;
33*a9fa9459Szrj struct debug_prefix_map *next;
34*a9fa9459Szrj } debug_prefix_map;
35*a9fa9459Szrj
36*a9fa9459Szrj /* Linked list of such structures. */
37*a9fa9459Szrj debug_prefix_map *debug_prefix_maps;
38*a9fa9459Szrj
39*a9fa9459Szrj
40*a9fa9459Szrj /* Record a debug file prefix mapping. ARG is the argument to
41*a9fa9459Szrj -fdebug-prefix-map and must be of the form OLD=NEW. */
42*a9fa9459Szrj
43*a9fa9459Szrj void
add_debug_prefix_map(const char * arg)44*a9fa9459Szrj add_debug_prefix_map (const char *arg)
45*a9fa9459Szrj {
46*a9fa9459Szrj debug_prefix_map *map;
47*a9fa9459Szrj const char *p;
48*a9fa9459Szrj char *o;
49*a9fa9459Szrj
50*a9fa9459Szrj p = strchr (arg, '=');
51*a9fa9459Szrj if (!p)
52*a9fa9459Szrj {
53*a9fa9459Szrj as_fatal (_("invalid argument '%s' to -fdebug-prefix-map"), arg);
54*a9fa9459Szrj return;
55*a9fa9459Szrj }
56*a9fa9459Szrj map = XNEW (debug_prefix_map);
57*a9fa9459Szrj o = xstrdup (arg);
58*a9fa9459Szrj map->old_prefix = o;
59*a9fa9459Szrj map->old_len = p - arg;
60*a9fa9459Szrj o[map->old_len] = 0;
61*a9fa9459Szrj p++;
62*a9fa9459Szrj map->new_prefix = xstrdup (p);
63*a9fa9459Szrj map->new_len = strlen (p);
64*a9fa9459Szrj map->next = debug_prefix_maps;
65*a9fa9459Szrj debug_prefix_maps = map;
66*a9fa9459Szrj }
67*a9fa9459Szrj
68*a9fa9459Szrj /* Perform user-specified mapping of debug filename prefixes. Returns
69*a9fa9459Szrj a newly allocated buffer containing the name corresponding to FILENAME.
70*a9fa9459Szrj It is the caller's responsibility to free the buffer. */
71*a9fa9459Szrj
72*a9fa9459Szrj const char *
remap_debug_filename(const char * filename)73*a9fa9459Szrj remap_debug_filename (const char *filename)
74*a9fa9459Szrj {
75*a9fa9459Szrj debug_prefix_map *map;
76*a9fa9459Szrj
77*a9fa9459Szrj for (map = debug_prefix_maps; map; map = map->next)
78*a9fa9459Szrj if (filename_ncmp (filename, map->old_prefix, map->old_len) == 0)
79*a9fa9459Szrj {
80*a9fa9459Szrj const char *name = filename + map->old_len;
81*a9fa9459Szrj return concat (map->new_prefix, name, NULL);
82*a9fa9459Szrj }
83*a9fa9459Szrj
84*a9fa9459Szrj return xstrdup (filename);
85*a9fa9459Szrj }
86