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