blob: 84fcca0fe1c9a99ee6f1fcf4e8dab2e60b7e9723 [file] [log] [blame]
Andrew Geissler517393d2023-01-13 08:55:19 -06001From e28c8883050d34d18ee2d66dfeece51e13adb6d5 Mon Sep 17 00:00:00 2001
Andrew Geissler82c905d2020-04-13 13:39:40 -05002From: Joshua Watt <JPEWhacker@gmail.com>
3Date: Tue, 19 Nov 2019 13:12:17 -0600
Andrew Geissler635e0e42020-08-21 15:58:33 -05004Subject: [PATCH] Add --debug-prefix-map option
Andrew Geissler82c905d2020-04-13 13:39:40 -05005
6Adds an option to remap file prefixes in output object files. This is
7analogous to the "-fdebug-prefix-map" option in GCC, and allows files to
8be built in a reproducible manner regardless of the build directory.
9
10Upstream-Status: Submitted [https://bugzilla.nasm.us/show_bug.cgi?id=3392635]
11Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
Andrew Geissler635e0e42020-08-21 15:58:33 -050012
Andrew Geissler82c905d2020-04-13 13:39:40 -050013---
Andrew Geisslerc9f78652020-09-18 14:11:35 -050014 asm/nasm.c | 24 ++++++++++++++++++++++++
Andrew Geissler82c905d2020-04-13 13:39:40 -050015 include/nasmlib.h | 9 +++++++++
16 nasm.txt | 4 ++++
17 nasmlib/filename.c | 20 ++++++++++++++++++++
18 output/outas86.c | 4 +++-
19 output/outcoff.c | 4 ++--
Andrew Geissler517393d2023-01-13 08:55:19 -060020 output/outelf.c | 13 ++++++++-----
Andrew Geissler82c905d2020-04-13 13:39:40 -050021 output/outieee.c | 2 +-
22 output/outobj.c | 2 +-
23 stdlib/strlcat.c | 2 +-
24 test/elfdebugprefix.asm | 6 ++++++
25 test/performtest.pl | 12 ++++++++++--
Andrew Geissler517393d2023-01-13 08:55:19 -060026 12 files changed, 89 insertions(+), 13 deletions(-)
Andrew Geissler82c905d2020-04-13 13:39:40 -050027 create mode 100644 test/elfdebugprefix.asm
28
29diff --git a/asm/nasm.c b/asm/nasm.c
Andrew Geissler517393d2023-01-13 08:55:19 -060030index 76c70f6..08ff119 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -050031--- a/asm/nasm.c
32+++ b/asm/nasm.c
Andrew Geisslerc9f78652020-09-18 14:11:35 -050033@@ -939,6 +939,7 @@ enum text_options {
Andrew Geissler82c905d2020-04-13 13:39:40 -050034 OPT_KEEP_ALL,
Andrew Geissler635e0e42020-08-21 15:58:33 -050035 OPT_NO_LINE,
Andrew Geisslerc9f78652020-09-18 14:11:35 -050036 OPT_DEBUG,
37+ OPT_DEBUG_PREFIX_MAP,
38 OPT_REPRODUCIBLE
Andrew Geissler82c905d2020-04-13 13:39:40 -050039 };
Andrew Geissler635e0e42020-08-21 15:58:33 -050040 enum need_arg {
Andrew Geisslerc9f78652020-09-18 14:11:35 -050041@@ -971,6 +972,7 @@ static const struct textargs textopts[] = {
Andrew Geissler635e0e42020-08-21 15:58:33 -050042 {"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
43 {"no-line", OPT_NO_LINE, ARG_NO, 0},
44 {"debug", OPT_DEBUG, ARG_MAYBE, 0},
Andrew Geissler82c905d2020-04-13 13:39:40 -050045+ {"debug-prefix-map", OPT_DEBUG_PREFIX_MAP, true, 0},
Andrew Geisslerc9f78652020-09-18 14:11:35 -050046 {"reproducible", OPT_REPRODUCIBLE, ARG_NO, 0},
Andrew Geissler635e0e42020-08-21 15:58:33 -050047 {NULL, OPT_BOGUS, ARG_NO, 0}
Andrew Geissler82c905d2020-04-13 13:39:40 -050048 };
Andrew Geissler517393d2023-01-13 08:55:19 -060049@@ -1335,6 +1337,26 @@ static bool process_arg(char *p, char *q, int pass)
Andrew Geisslerc9f78652020-09-18 14:11:35 -050050 case OPT_REPRODUCIBLE:
51 reproducible = true;
Andrew Geissler82c905d2020-04-13 13:39:40 -050052 break;
53+ case OPT_DEBUG_PREFIX_MAP: {
54+ struct debug_prefix_list *d;
55+ char *c;
56+ c = strchr(param, '=');
57+
58+ if (!c) {
59+ nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
60+ "option `--%s' must be of the form `BASE=DEST'", p);
61+ break;
62+ }
63+
64+ *c = '\0';
65+ d = nasm_malloc(sizeof(*d));
66+ d->next = debug_prefixes;
67+ d->base = nasm_strdup(param);
68+ d->dest = nasm_strdup(c + 1);
69+ debug_prefixes = d;
70+ *c = '=';
71+ }
72+ break;
73 case OPT_HELP:
Andrew Geissler635e0e42020-08-21 15:58:33 -050074 help(stdout);
Andrew Geissler82c905d2020-04-13 13:39:40 -050075 exit(0);
Andrew Geissler517393d2023-01-13 08:55:19 -060076@@ -2298,6 +2320,8 @@ static void help(FILE *out)
Andrew Geissler635e0e42020-08-21 15:58:33 -050077 " -w-x disable warning x (also -Wno-x)\n"
78 " -w[+-]error promote all warnings to errors (also -Werror)\n"
79 " -w[+-]error=x promote warning x to errors (also -Werror=x)\n"
80+ " --debug-prefix-map base=dest\n"
81+ " remap paths starting with 'base' to 'dest' in output files\n"
82 , out);
Andrew Geissler82c905d2020-04-13 13:39:40 -050083
Andrew Geissler635e0e42020-08-21 15:58:33 -050084 fprintf(out, " %-20s %s\n",
Andrew Geissler82c905d2020-04-13 13:39:40 -050085diff --git a/include/nasmlib.h b/include/nasmlib.h
Andrew Geissler517393d2023-01-13 08:55:19 -060086index 87a7fc6..a3e5144 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -050087--- a/include/nasmlib.h
88+++ b/include/nasmlib.h
Andrew Geissler635e0e42020-08-21 15:58:33 -050089@@ -250,10 +250,19 @@ int64_t readstrnum(char *str, int length, bool *warn);
Andrew Geissler82c905d2020-04-13 13:39:40 -050090 */
91 int32_t seg_alloc(void);
92
93+struct debug_prefix_list {
94+ struct debug_prefix_list *next;
95+ char *base;
96+ char *dest;
97+};
98+
99+extern struct debug_prefix_list *debug_prefixes;
100+
101 /*
102 * Add/replace or remove an extension to the end of a filename
103 */
104 const char *filename_set_extension(const char *inname, const char *extension);
105+char *filename_debug_remap(char *dest, char const *inname, size_t len);
106
107 /*
108 * Utility macros...
109diff --git a/nasm.txt b/nasm.txt
Andrew Geissler517393d2023-01-13 08:55:19 -0600110index 950c361..784618c 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500111--- a/nasm.txt
112+++ b/nasm.txt
113@@ -147,6 +147,10 @@ OPTIONS
114 Prepend or append (respectively) the given argument to all global or
115 extern variables.
116
117+--debug-prefix-map 'BASE=DEST'::
118+ Map file names beginning with 'BASE' to 'DEST' when encoding them in
119+ output object files.
120+
121 SYNTAX
122 ------
123 This man page does not fully describe the syntax of *nasm*'s assembly language,
124diff --git a/nasmlib/filename.c b/nasmlib/filename.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500125index 172ae0b..fda2be4 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500126--- a/nasmlib/filename.c
127+++ b/nasmlib/filename.c
128@@ -39,6 +39,8 @@
129 #include "nasmlib.h"
130 #include "error.h"
131
132+struct debug_prefix_list *debug_prefixes = NULL;
133+
134 /*
135 * Add/modify a filename extension, assumed to be a period-delimited
136 * field at the very end of the filename. Returns a newly allocated
137@@ -61,3 +63,21 @@ const char *filename_set_extension(const char *inname, const char *extension)
138
139 return p;
140 }
141+
142+char *filename_debug_remap(char *dest, char const *in, size_t len)
143+{
144+ struct debug_prefix_list *d;
145+ size_t n;
146+
147+ for (d = debug_prefixes; d != NULL; d = d->next) {
148+ n = strlen(d->base);
149+ if (strncmp(in, d->base, n) == 0) {
150+ strlcpy(dest, d->dest, len);
151+ strlcat(dest, &in[n], len);
152+ return dest;
153+ }
154+ }
155+
156+ strlcpy(dest, in, len);
157+ return dest;
158+}
159diff --git a/output/outas86.c b/output/outas86.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500160index 54b22f8..c4a412c 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500161--- a/output/outas86.c
162+++ b/output/outas86.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500163@@ -110,6 +110,8 @@ static void as86_sect_write(struct Section *, const uint8_t *,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500164
165 static void as86_init(void)
166 {
167+ char filename[FILENAME_MAX];
168+
169 stext.data = saa_init(1L);
170 stext.datalen = 0L;
171 stext.head = stext.last = NULL;
Andrew Geissler635e0e42020-08-21 15:58:33 -0500172@@ -131,7 +133,7 @@ static void as86_init(void)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500173 strslen = 0;
174
175 /* as86 module name = input file minus extension */
176- as86_add_string(filename_set_extension(inname, ""));
177+ as86_add_string(filename_debug_remap(filename, filename_set_extension(inname, ""), sizeof(filename)));
178 }
179
180 static void as86_cleanup(void)
181diff --git a/output/outcoff.c b/output/outcoff.c
Andrew Geissler517393d2023-01-13 08:55:19 -0600182index c2b4eb6..e242db2 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500183--- a/output/outcoff.c
184+++ b/output/outcoff.c
Andrew Geissler517393d2023-01-13 08:55:19 -0600185@@ -1259,7 +1259,7 @@ static void coff_symbol(char *name, int32_t strpos, int32_t value,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500186
187 static void coff_write_symbols(void)
188 {
189- char filename[18];
190+ char filename[19];
191 uint32_t i;
192
193 /*
Andrew Geissler517393d2023-01-13 08:55:19 -0600194@@ -1269,7 +1269,7 @@ static void coff_write_symbols(void)
195 if (reproducible)
196 memset(filename, 0, 18);
197 else
198- strncpy(filename, inname, 18);
199+ filename_debug_remap(filename, inname, 19);
Andrew Geissler82c905d2020-04-13 13:39:40 -0500200 nasm_write(filename, 18, ofile);
201
202 /*
203diff --git a/output/outelf.c b/output/outelf.c
Andrew Geissler517393d2023-01-13 08:55:19 -0600204index ad8d210..29f1dc1 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500205--- a/output/outelf.c
206+++ b/output/outelf.c
Andrew Geissler517393d2023-01-13 08:55:19 -0600207@@ -546,8 +546,8 @@ static void elf_init(void)
Andrew Geissler635e0e42020-08-21 15:58:33 -0500208 const char * const *p;
Andrew Geissler517393d2023-01-13 08:55:19 -0600209 const char * cur_path = nasm_realpath(inname);
Andrew Geissler82c905d2020-04-13 13:39:40 -0500210
Andrew Geissler82c905d2020-04-13 13:39:40 -0500211- strlcpy(elf_module, inname, sizeof(elf_module));
Andrew Geissler517393d2023-01-13 08:55:19 -0600212- strlcpy(elf_dir, nasm_dirname(cur_path), sizeof(elf_dir));
Andrew Geissler82c905d2020-04-13 13:39:40 -0500213+ filename_debug_remap(elf_module, inname, sizeof(elf_module));
Andrew Geissler517393d2023-01-13 08:55:19 -0600214+ filename_debug_remap(elf_dir, nasm_dirname(cur_path), sizeof(elf_dir));
Andrew Geissler82c905d2020-04-13 13:39:40 -0500215 sects = NULL;
216 nsects = sectlen = 0;
217 syms = saa_init((int32_t)sizeof(struct elf_symbol));
Andrew Geissler517393d2023-01-13 08:55:19 -0600218@@ -3590,13 +3590,17 @@ static void dwarf_findfile(const char * fname)
219 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename)))
220 return;
221
222+ char * fname_remapped = nasm_malloc(FILENAME_MAX);
223+ filename_debug_remap(fname_remapped,fname,FILENAME_MAX);
224+
225 /* search for match */
226 match = 0;
227 if (dwarf_flist) {
228 match = dwarf_flist;
229 for (finx = 0; finx < dwarf_numfiles; finx++) {
230- if (!(strcmp(fname, match->filename))) {
231+ if (!(strcmp(fname_remapped, match->filename))) {
232 dwarf_clist = match;
233+ nasm_free(fname_remapped);
234 return;
235 }
236 match = match->next;
237@@ -3607,8 +3611,7 @@ static void dwarf_findfile(const char * fname)
238 dwarf_clist = nasm_malloc(sizeof(struct linelist));
239 dwarf_numfiles++;
240 dwarf_clist->line = dwarf_numfiles;
241- dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
242- strcpy(dwarf_clist->filename,fname);
243+ dwarf_clist->filename = fname_remapped;
244 dwarf_clist->next = 0;
245 if (!dwarf_flist) { /* if first entry */
246 dwarf_flist = dwarf_elist = dwarf_clist;
Andrew Geissler82c905d2020-04-13 13:39:40 -0500247diff --git a/output/outieee.c b/output/outieee.c
Andrew Geissler517393d2023-01-13 08:55:19 -0600248index 7ba9036..796e5af 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500249--- a/output/outieee.c
250+++ b/output/outieee.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500251@@ -207,7 +207,7 @@ static void ieee_unqualified_name(char *, char *);
Andrew Geissler82c905d2020-04-13 13:39:40 -0500252 */
253 static void ieee_init(void)
254 {
255- strlcpy(ieee_infile, inname, sizeof(ieee_infile));
256+ filename_debug_remap(ieee_infile, inname, sizeof(ieee_infile));
257 any_segs = false;
258 fpubhead = NULL;
259 fpubtail = &fpubhead;
260diff --git a/output/outobj.c b/output/outobj.c
Andrew Geissler517393d2023-01-13 08:55:19 -0600261index 281839d..fc336c1 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500262--- a/output/outobj.c
263+++ b/output/outobj.c
Andrew Geisslerc9f78652020-09-18 14:11:35 -0500264@@ -644,7 +644,7 @@ static enum directive_result obj_directive(enum directive, char *);
Andrew Geissler82c905d2020-04-13 13:39:40 -0500265
266 static void obj_init(void)
267 {
268- strlcpy(obj_infile, inname, sizeof(obj_infile));
269+ filename_debug_remap(obj_infile, inname, sizeof(obj_infile));
270 first_seg = seg_alloc();
271 any_segs = false;
272 fpubhead = NULL;
273diff --git a/stdlib/strlcat.c b/stdlib/strlcat.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500274index 7084d46..ee93dea 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500275--- a/stdlib/strlcat.c
276+++ b/stdlib/strlcat.c
277@@ -29,7 +29,7 @@ size_t strlcat(char *dest, const char *src, size_t size)
278 size_t n;
279
280 /* find the NULL terminator in dest */
281- for (n = 0; i < size && dest[n] != '\0'; n++)
282+ for (n = 0; n < size && dest[n] != '\0'; n++)
283 ;
284
285 /* destination was not NULL terminated. Return the initial size */
286diff --git a/test/elfdebugprefix.asm b/test/elfdebugprefix.asm
287new file mode 100644
Andrew Geissler635e0e42020-08-21 15:58:33 -0500288index 0000000..a67ba29
Andrew Geissler82c905d2020-04-13 13:39:40 -0500289--- /dev/null
290+++ b/test/elfdebugprefix.asm
291@@ -0,0 +1,6 @@
292+;Testname=unoptimized; Arguments=-O0 --debug-prefix-map elf=ELF -felf -oelfdebugprefix.o; Files=stdout stderr elfdebugprefix.o; Validate=readelf --wide --symbols elfdebugprefix.o | grep 'FILE.*ELFdebugprefix.asm'
293+
294+ SECTION .text
295+test: ; [1]
296+ ret
297+
298diff --git a/test/performtest.pl b/test/performtest.pl
Andrew Geissler517393d2023-01-13 08:55:19 -0600299index 46b1bdf..2426848 100755
Andrew Geissler82c905d2020-04-13 13:39:40 -0500300--- a/test/performtest.pl
301+++ b/test/performtest.pl
302@@ -42,14 +42,22 @@ sub perform {
303 TEST:
304 while(<TESTFILE>) {
305 #See if there is a test case
306- last unless /Testname=(.*);\s*Arguments=(.*);\s*Files=(.*)/;
307- my ($subname, $arguments, $files) = ($1, $2, $3);
308+ last unless /Testname=(.*);\s*Arguments=(.*);\s*Files=([^;]*)(?:;\s*Validate=(.*))?/;
309+ my ($subname, $arguments, $files, $validate) = ($1, $2, $3, $4);
310+ chomp $files;
311 debugprint("$subname | $arguments | $files");
312
313 #Call nasm with this test case
314 system("$nasm $arguments $testpath > $stdoutfile 2> $stderrfile");
315 debugprint("$nasm $arguments $testpath > $stdoutfile 2> $stderrfile ----> $?");
316
317+ if($validate) {
318+ if(system("$validate >> $stdoutfile 2>> $stderrfile") != 0) {
319+ print "Test $testname/$subname validation failed\n";
320+ $globalresult = 1;
321+ }
322+ }
323+
324 #Move the output to the test dir
325 mkpath("$outputdir/$testname/$subname");
326 foreach(split / /,$files) {