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