blob: f788e0fd43469b648757fd59288f422db4a77c8c [file] [log] [blame]
Andrew Geissler635e0e42020-08-21 15:58:33 -05001From bb4e42ad3a0cdd23a1d1797e6299c76b474867c0 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 Geissler635e0e42020-08-21 15:58:33 -050014 asm/nasm.c | 26 +++++++++++++++++++++++++-
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 Geissler635e0e42020-08-21 15:58:33 -050020 output/outelf.c | 2 +-
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 Geissler635e0e42020-08-21 15:58:33 -050026 12 files changed, 83 insertions(+), 10 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 Geissler635e0e42020-08-21 15:58:33 -050030index a0e1719..fc6c62e 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -050031--- a/asm/nasm.c
32+++ b/asm/nasm.c
Andrew Geissler635e0e42020-08-21 15:58:33 -050033@@ -938,7 +938,8 @@ enum text_options {
Andrew Geissler82c905d2020-04-13 13:39:40 -050034 OPT_LIMIT,
35 OPT_KEEP_ALL,
Andrew Geissler635e0e42020-08-21 15:58:33 -050036 OPT_NO_LINE,
37- OPT_DEBUG
38+ OPT_DEBUG,
Andrew Geissler82c905d2020-04-13 13:39:40 -050039+ OPT_DEBUG_PREFIX_MAP
40 };
Andrew Geissler635e0e42020-08-21 15:58:33 -050041 enum need_arg {
42 ARG_NO,
43@@ -970,6 +971,7 @@ static const struct textargs textopts[] = {
44 {"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
45 {"no-line", OPT_NO_LINE, ARG_NO, 0},
46 {"debug", OPT_DEBUG, ARG_MAYBE, 0},
Andrew Geissler82c905d2020-04-13 13:39:40 -050047+ {"debug-prefix-map", OPT_DEBUG_PREFIX_MAP, true, 0},
Andrew Geissler635e0e42020-08-21 15:58:33 -050048 {NULL, OPT_BOGUS, ARG_NO, 0}
Andrew Geissler82c905d2020-04-13 13:39:40 -050049 };
50
Andrew Geissler635e0e42020-08-21 15:58:33 -050051@@ -1332,6 +1334,26 @@ static bool process_arg(char *p, char *q, int pass)
52 case OPT_DEBUG:
53 debug_nasm = param ? strtoul(param, NULL, 10) : debug_nasm+1;
Andrew Geissler82c905d2020-04-13 13:39:40 -050054 break;
55+ case OPT_DEBUG_PREFIX_MAP: {
56+ struct debug_prefix_list *d;
57+ char *c;
58+ c = strchr(param, '=');
59+
60+ if (!c) {
61+ nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
62+ "option `--%s' must be of the form `BASE=DEST'", p);
63+ break;
64+ }
65+
66+ *c = '\0';
67+ d = nasm_malloc(sizeof(*d));
68+ d->next = debug_prefixes;
69+ d->base = nasm_strdup(param);
70+ d->dest = nasm_strdup(c + 1);
71+ debug_prefixes = d;
72+ *c = '=';
73+ }
74+ break;
75 case OPT_HELP:
Andrew Geissler635e0e42020-08-21 15:58:33 -050076 help(stdout);
Andrew Geissler82c905d2020-04-13 13:39:40 -050077 exit(0);
Andrew Geissler635e0e42020-08-21 15:58:33 -050078@@ -2297,6 +2319,8 @@ static void help(FILE *out)
79 " -w-x disable warning x (also -Wno-x)\n"
80 " -w[+-]error promote all warnings to errors (also -Werror)\n"
81 " -w[+-]error=x promote warning x to errors (also -Werror=x)\n"
82+ " --debug-prefix-map base=dest\n"
83+ " remap paths starting with 'base' to 'dest' in output files\n"
84 , out);
Andrew Geissler82c905d2020-04-13 13:39:40 -050085
Andrew Geissler635e0e42020-08-21 15:58:33 -050086 fprintf(out, " %-20s %s\n",
Andrew Geissler82c905d2020-04-13 13:39:40 -050087diff --git a/include/nasmlib.h b/include/nasmlib.h
Andrew Geissler635e0e42020-08-21 15:58:33 -050088index e9bfbcc..98fc653 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -050089--- a/include/nasmlib.h
90+++ b/include/nasmlib.h
Andrew Geissler635e0e42020-08-21 15:58:33 -050091@@ -250,10 +250,19 @@ int64_t readstrnum(char *str, int length, bool *warn);
Andrew Geissler82c905d2020-04-13 13:39:40 -050092 */
93 int32_t seg_alloc(void);
94
95+struct debug_prefix_list {
96+ struct debug_prefix_list *next;
97+ char *base;
98+ char *dest;
99+};
100+
101+extern struct debug_prefix_list *debug_prefixes;
102+
103 /*
104 * Add/replace or remove an extension to the end of a filename
105 */
106 const char *filename_set_extension(const char *inname, const char *extension);
107+char *filename_debug_remap(char *dest, char const *inname, size_t len);
108
109 /*
110 * Utility macros...
111diff --git a/nasm.txt b/nasm.txt
Andrew Geissler635e0e42020-08-21 15:58:33 -0500112index cc7fa27..d3485c9 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500113--- a/nasm.txt
114+++ b/nasm.txt
115@@ -147,6 +147,10 @@ OPTIONS
116 Prepend or append (respectively) the given argument to all global or
117 extern variables.
118
119+--debug-prefix-map 'BASE=DEST'::
120+ Map file names beginning with 'BASE' to 'DEST' when encoding them in
121+ output object files.
122+
123 SYNTAX
124 ------
125 This man page does not fully describe the syntax of *nasm*'s assembly language,
126diff --git a/nasmlib/filename.c b/nasmlib/filename.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500127index 172ae0b..fda2be4 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500128--- a/nasmlib/filename.c
129+++ b/nasmlib/filename.c
130@@ -39,6 +39,8 @@
131 #include "nasmlib.h"
132 #include "error.h"
133
134+struct debug_prefix_list *debug_prefixes = NULL;
135+
136 /*
137 * Add/modify a filename extension, assumed to be a period-delimited
138 * field at the very end of the filename. Returns a newly allocated
139@@ -61,3 +63,21 @@ const char *filename_set_extension(const char *inname, const char *extension)
140
141 return p;
142 }
143+
144+char *filename_debug_remap(char *dest, char const *in, size_t len)
145+{
146+ struct debug_prefix_list *d;
147+ size_t n;
148+
149+ for (d = debug_prefixes; d != NULL; d = d->next) {
150+ n = strlen(d->base);
151+ if (strncmp(in, d->base, n) == 0) {
152+ strlcpy(dest, d->dest, len);
153+ strlcat(dest, &in[n], len);
154+ return dest;
155+ }
156+ }
157+
158+ strlcpy(dest, in, len);
159+ return dest;
160+}
161diff --git a/output/outas86.c b/output/outas86.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500162index 54b22f8..c4a412c 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500163--- a/output/outas86.c
164+++ b/output/outas86.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500165@@ -110,6 +110,8 @@ static void as86_sect_write(struct Section *, const uint8_t *,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500166
167 static void as86_init(void)
168 {
169+ char filename[FILENAME_MAX];
170+
171 stext.data = saa_init(1L);
172 stext.datalen = 0L;
173 stext.head = stext.last = NULL;
Andrew Geissler635e0e42020-08-21 15:58:33 -0500174@@ -131,7 +133,7 @@ static void as86_init(void)
Andrew Geissler82c905d2020-04-13 13:39:40 -0500175 strslen = 0;
176
177 /* as86 module name = input file minus extension */
178- as86_add_string(filename_set_extension(inname, ""));
179+ as86_add_string(filename_debug_remap(filename, filename_set_extension(inname, ""), sizeof(filename)));
180 }
181
182 static void as86_cleanup(void)
183diff --git a/output/outcoff.c b/output/outcoff.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500184index bcd9ff3..15bfcf3 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500185--- a/output/outcoff.c
186+++ b/output/outcoff.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500187@@ -1095,14 +1095,14 @@ static void coff_symbol(char *name, int32_t strpos, int32_t value,
Andrew Geissler82c905d2020-04-13 13:39:40 -0500188
189 static void coff_write_symbols(void)
190 {
191- char filename[18];
192+ char filename[19];
193 uint32_t i;
194
195 /*
196 * The `.file' record, and the file name auxiliary record.
197 */
198 coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
199- strncpy(filename, inname, 18);
200+ filename_debug_remap(filename, inname, 19);
201 nasm_write(filename, 18, ofile);
202
203 /*
204diff --git a/output/outelf.c b/output/outelf.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500205index 61af020..1292958 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500206--- a/output/outelf.c
207+++ b/output/outelf.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500208@@ -553,7 +553,7 @@ static void elf_init(void)
209 };
210 const char * const *p;
Andrew Geissler82c905d2020-04-13 13:39:40 -0500211
Andrew Geissler82c905d2020-04-13 13:39:40 -0500212- strlcpy(elf_module, inname, sizeof(elf_module));
213+ filename_debug_remap(elf_module, inname, sizeof(elf_module));
214 sects = NULL;
215 nsects = sectlen = 0;
216 syms = saa_init((int32_t)sizeof(struct elf_symbol));
Andrew Geissler82c905d2020-04-13 13:39:40 -0500217diff --git a/output/outieee.c b/output/outieee.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500218index 4cc0f0f..2468724 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500219--- a/output/outieee.c
220+++ b/output/outieee.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500221@@ -207,7 +207,7 @@ static void ieee_unqualified_name(char *, char *);
Andrew Geissler82c905d2020-04-13 13:39:40 -0500222 */
223 static void ieee_init(void)
224 {
225- strlcpy(ieee_infile, inname, sizeof(ieee_infile));
226+ filename_debug_remap(ieee_infile, inname, sizeof(ieee_infile));
227 any_segs = false;
228 fpubhead = NULL;
229 fpubtail = &fpubhead;
230diff --git a/output/outobj.c b/output/outobj.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500231index 0d4d311..d8dd6a0 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500232--- a/output/outobj.c
233+++ b/output/outobj.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500234@@ -638,7 +638,7 @@ static enum directive_result obj_directive(enum directive, char *);
Andrew Geissler82c905d2020-04-13 13:39:40 -0500235
236 static void obj_init(void)
237 {
238- strlcpy(obj_infile, inname, sizeof(obj_infile));
239+ filename_debug_remap(obj_infile, inname, sizeof(obj_infile));
240 first_seg = seg_alloc();
241 any_segs = false;
242 fpubhead = NULL;
243diff --git a/stdlib/strlcat.c b/stdlib/strlcat.c
Andrew Geissler635e0e42020-08-21 15:58:33 -0500244index 7084d46..ee93dea 100644
Andrew Geissler82c905d2020-04-13 13:39:40 -0500245--- a/stdlib/strlcat.c
246+++ b/stdlib/strlcat.c
247@@ -29,7 +29,7 @@ size_t strlcat(char *dest, const char *src, size_t size)
248 size_t n;
249
250 /* find the NULL terminator in dest */
251- for (n = 0; i < size && dest[n] != '\0'; n++)
252+ for (n = 0; n < size && dest[n] != '\0'; n++)
253 ;
254
255 /* destination was not NULL terminated. Return the initial size */
256diff --git a/test/elfdebugprefix.asm b/test/elfdebugprefix.asm
257new file mode 100644
Andrew Geissler635e0e42020-08-21 15:58:33 -0500258index 0000000..a67ba29
Andrew Geissler82c905d2020-04-13 13:39:40 -0500259--- /dev/null
260+++ b/test/elfdebugprefix.asm
261@@ -0,0 +1,6 @@
262+;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'
263+
264+ SECTION .text
265+test: ; [1]
266+ ret
267+
268diff --git a/test/performtest.pl b/test/performtest.pl
Andrew Geissler635e0e42020-08-21 15:58:33 -0500269index f7865b3..096f960 100755
Andrew Geissler82c905d2020-04-13 13:39:40 -0500270--- a/test/performtest.pl
271+++ b/test/performtest.pl
272@@ -42,14 +42,22 @@ sub perform {
273 TEST:
274 while(<TESTFILE>) {
275 #See if there is a test case
276- last unless /Testname=(.*);\s*Arguments=(.*);\s*Files=(.*)/;
277- my ($subname, $arguments, $files) = ($1, $2, $3);
278+ last unless /Testname=(.*);\s*Arguments=(.*);\s*Files=([^;]*)(?:;\s*Validate=(.*))?/;
279+ my ($subname, $arguments, $files, $validate) = ($1, $2, $3, $4);
280+ chomp $files;
281 debugprint("$subname | $arguments | $files");
282
283 #Call nasm with this test case
284 system("$nasm $arguments $testpath > $stdoutfile 2> $stderrfile");
285 debugprint("$nasm $arguments $testpath > $stdoutfile 2> $stderrfile ----> $?");
286
287+ if($validate) {
288+ if(system("$validate >> $stdoutfile 2>> $stderrfile") != 0) {
289+ print "Test $testname/$subname validation failed\n";
290+ $globalresult = 1;
291+ }
292+ }
293+
294 #Move the output to the test dir
295 mkpath("$outputdir/$testname/$subname");
296 foreach(split / /,$files) {