@@ -42,11 +42,17 @@ struct forwrefinfo { /* info held on forward refs. */
4242 int operand ;
4343};
4444
45+ struct debug_prefix_list {
46+ struct debug_prefix_list * next ;
47+ char * base ;
48+ char * dest ;
49+ };
50+
4551const char * _progname ;
4652
4753static void open_and_process_respfile (char * , int );
4854static void parse_cmdline (int , char * * , int );
49- static void assemble_file (const char * , struct strlist * );
55+ static void assemble_file (const char * , char const * , struct strlist * );
5056static void help (FILE * out , const char * what );
5157
5258static bool using_debug_info ;
@@ -81,6 +87,8 @@ FILE *ofile = NULL;
8187enum optimization optimizing = OPTIM_DEFAULT ;
8288static int cmd_sb = 16 ; /* by default */
8389
90+ static struct debug_prefix_list * debug_prefixes = NULL ;
91+
8492iflag_t cpu , cmd_cpu ;
8593
8694struct location location ;
@@ -721,7 +729,7 @@ int main(int argc, char **argv)
721729 if (depend_missing_ok )
722730 pp_include_path (NULL ); /* "assume generated" */
723731
724- pp_reset (get_filename (FN_INFILE ), PP_DEPS , depend_list );
732+ pp_reset (get_filename (FN_INFILE ), get_filename ( FN_MAPPED_INFILE ), PP_DEPS , depend_list );
725733 ofile = NULL ;
726734 while ((line = pp_getline ()))
727735 nasm_free (line );
@@ -747,7 +755,7 @@ int main(int argc, char **argv)
747755 location .known = false;
748756
749757 _pass_type = PASS_PREPROC ;
750- pp_reset (get_filename (FN_INFILE ), PP_PREPROC , depend_list );
758+ pp_reset (get_filename (FN_INFILE ), get_filename ( FN_MAPPED_INFILE ), PP_PREPROC , depend_list );
751759 error_pass_start (true);
752760
753761 while ((line = pp_getline ())) {
@@ -808,7 +816,7 @@ int main(int argc, char **argv)
808816 ofmt -> init ();
809817 dfmt -> init ();
810818
811- assemble_file (get_filename (FN_INFILE ), depend_list );
819+ assemble_file (get_filename (FN_INFILE ), get_filename ( FN_MAPPED_INFILE ), depend_list );
812820
813821 if (!terminate_after_phase ()) {
814822 ofmt -> cleanup ();
@@ -864,6 +872,30 @@ static char *get_param(char *p, char *q, bool *advance)
864872 return r ;
865873}
866874
875+ /*
876+ * Apply debug prefix mappings to a path buffer
877+ */
878+ static char * debug_prefix_remap (char const * path )
879+ {
880+ struct debug_prefix_list * d ;
881+ size_t n ;
882+
883+ for (d = debug_prefixes ; d != NULL ; d = d -> next ) {
884+ n = strlen (d -> base );
885+ if (strncmp (path , d -> base , n ) == 0 ) {
886+ size_t prefix_len = strlen (d -> dest );
887+ size_t suffix_len = strlen (path ) - n ;
888+ size_t len = prefix_len + suffix_len + 1 ;
889+ char * out = nasm_malloc (len );
890+ strlcpy (out , d -> dest , len );
891+ strlcpy (& out [prefix_len ], & path [n ], len - prefix_len );
892+ return out ;
893+ }
894+ }
895+
896+ return nasm_strdup (path );
897+ }
898+
867899/*
868900 * Convert a string to a POSIX make-safe form; returns a newly allocated
869901 * string.
@@ -1030,7 +1062,8 @@ enum text_options {
10301062 OPT_DEBUG ,
10311063 OPT_INFO ,
10321064 OPT_REPRODUCIBLE ,
1033- OPT_BITS
1065+ OPT_BITS ,
1066+ OPT_DEBUG_PREFIX_MAP
10341067};
10351068enum need_arg {
10361069 ARG_NO ,
@@ -1069,6 +1102,7 @@ static const struct textargs textopts[] = {
10691102 {"debug" , OPT_DEBUG , ARG_MAYBE , 0 },
10701103 {"reproducible" , OPT_REPRODUCIBLE , ARG_NO , 0 },
10711104 {"bits" , OPT_BITS , ARG_YES , 0 },
1105+ {"debug-prefix-map" , OPT_DEBUG_PREFIX_MAP , ARG_YES , 0 },
10721106 {NULL , OPT_BOGUS , ARG_NO , 0 }
10731107};
10741108
@@ -1455,6 +1489,26 @@ static bool process_arg(char *p, char *q, int pass)
14551489 case OPT_REPRODUCIBLE :
14561490 reproducible = true;
14571491 break ;
1492+ case OPT_DEBUG_PREFIX_MAP : {
1493+ struct debug_prefix_list * d ;
1494+ char * c ;
1495+ c = strchr (param , '=' );
1496+
1497+ if (!c ) {
1498+ nasm_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE ,
1499+ "option `--%s' must be of the form `BASE=DEST'" , p );
1500+ break ;
1501+ }
1502+
1503+ * c = '\0' ;
1504+ d = nasm_malloc (sizeof (* d ));
1505+ d -> next = debug_prefixes ;
1506+ d -> base = nasm_strdup (param );
1507+ d -> dest = nasm_strdup (c + 1 );
1508+ debug_prefixes = d ;
1509+ * c = '=' ;
1510+ }
1511+ break ;
14581512 case OPT_HELP :
14591513 /* Allow --help topic without *requiring* topic */
14601514 if (!param )
@@ -1672,6 +1726,8 @@ static void parse_cmdline(int argc, char **argv, int pass)
16721726 if (!get_filename (FN_INFILE ))
16731727 nasm_fatalf (ERR_USAGE , "no input file specified" );
16741728
1729+ set_filename (FN_MAPPED_INFILE , debug_prefix_remap (get_filename (FN_INFILE )));
1730+
16751731 check_overwrite_files ();
16761732
16771733 open_errfile (get_filename (FN_ERRFILE ));
@@ -1724,7 +1780,7 @@ void print_final_report(bool failure)
17241780 }
17251781}
17261782
1727- static void assemble_file (const char * fname , struct strlist * depend_list )
1783+ static void assemble_file (const char * fname , char const * mapped_fname , struct strlist * depend_list )
17281784{
17291785 static int64_t stall_count = 0 ; /* Make sure we make forward progress... */
17301786 char * line ;
@@ -1814,7 +1870,7 @@ static void assemble_file(const char *fname, struct strlist *depend_list)
18141870 location .known = true;
18151871 ofmt -> reset ();
18161872 switch_segment (ofmt -> section (NULL , & globl .bits ));
1817- pp_reset (fname , PP_NORMAL , depend_list );
1873+ pp_reset (fname , mapped_fname , PP_NORMAL , depend_list );
18181874
18191875 globallineno = 0 ;
18201876
@@ -2205,6 +2261,8 @@ static void help(FILE *out, const char *what)
22052261 " --lprefix str prepend the given string to local symbols\n"
22062262 " --lpostfix str append the given string to local symbols\n"
22072263 " --reproducible attempt to produce run-to-run identical output\n"
2264+ " --debug-prefix-map base=dest\n"
2265+ " remap paths starting with 'base' to 'dest' in output files\n"
22082266 , out );
22092267 }
22102268 if (help_is (with , HW_LIMIT )) {
0 commit comments