Andrew Geissler | 82c905d | 2020-04-13 13:39:40 -0500 | [diff] [blame] | 1 | From fa677c1caf6b8192971920cf5c1aa8cb33c74605 Mon Sep 17 00:00:00 2001 |
| 2 | From: Joshua Watt <JPEWhacker@gmail.com> |
| 3 | Date: Tue, 19 Nov 2019 13:12:17 -0600 |
| 4 | Subject: [PATCH 2/2] Add --debug-prefix-map option |
| 5 | |
| 6 | Adds an option to remap file prefixes in output object files. This is |
| 7 | analogous to the "-fdebug-prefix-map" option in GCC, and allows files to |
| 8 | be built in a reproducible manner regardless of the build directory. |
| 9 | |
| 10 | Upstream-Status: Submitted [https://bugzilla.nasm.us/show_bug.cgi?id=3392635] |
| 11 | Signed-off-by: Joshua Watt <JPEWhacker@gmail.com> |
| 12 | --- |
| 13 | asm/nasm.c | 28 ++++++++++++++++++++++++++-- |
| 14 | include/nasmlib.h | 9 +++++++++ |
| 15 | nasm.txt | 4 ++++ |
| 16 | nasmlib/filename.c | 20 ++++++++++++++++++++ |
| 17 | output/outas86.c | 4 +++- |
| 18 | output/outcoff.c | 4 ++-- |
| 19 | output/outelf.c | 8 ++++---- |
| 20 | output/outieee.c | 2 +- |
| 21 | output/outobj.c | 2 +- |
| 22 | stdlib/strlcat.c | 2 +- |
| 23 | test/elfdebugprefix.asm | 6 ++++++ |
| 24 | test/performtest.pl | 12 ++++++++++-- |
| 25 | 12 files changed, 87 insertions(+), 14 deletions(-) |
| 26 | create mode 100644 test/elfdebugprefix.asm |
| 27 | |
| 28 | diff --git a/asm/nasm.c b/asm/nasm.c |
| 29 | index 1c5a5fc5..5d45103c 100644 |
| 30 | --- a/asm/nasm.c |
| 31 | +++ b/asm/nasm.c |
| 32 | @@ -841,7 +841,8 @@ enum text_options { |
| 33 | OPT_BEFORE, |
| 34 | OPT_LIMIT, |
| 35 | OPT_KEEP_ALL, |
| 36 | - OPT_NO_LINE |
| 37 | + OPT_NO_LINE, |
| 38 | + OPT_DEBUG_PREFIX_MAP |
| 39 | }; |
| 40 | struct textargs { |
| 41 | const char *label; |
| 42 | @@ -866,6 +867,7 @@ static const struct textargs textopts[] = { |
| 43 | {"limit-", OPT_LIMIT, true, 0}, |
| 44 | {"keep-all", OPT_KEEP_ALL, false, 0}, |
| 45 | {"no-line", OPT_NO_LINE, false, 0}, |
| 46 | + {"debug-prefix-map", OPT_DEBUG_PREFIX_MAP, true, 0}, |
| 47 | {NULL, OPT_BOGUS, false, 0} |
| 48 | }; |
| 49 | |
| 50 | @@ -1217,6 +1219,26 @@ static bool process_arg(char *p, char *q, int pass) |
| 51 | case OPT_NO_LINE: |
| 52 | pp_noline = true; |
| 53 | break; |
| 54 | + case OPT_DEBUG_PREFIX_MAP: { |
| 55 | + struct debug_prefix_list *d; |
| 56 | + char *c; |
| 57 | + c = strchr(param, '='); |
| 58 | + |
| 59 | + if (!c) { |
| 60 | + nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE, |
| 61 | + "option `--%s' must be of the form `BASE=DEST'", p); |
| 62 | + break; |
| 63 | + } |
| 64 | + |
| 65 | + *c = '\0'; |
| 66 | + d = nasm_malloc(sizeof(*d)); |
| 67 | + d->next = debug_prefixes; |
| 68 | + d->base = nasm_strdup(param); |
| 69 | + d->dest = nasm_strdup(c + 1); |
| 70 | + debug_prefixes = d; |
| 71 | + *c = '='; |
| 72 | + } |
| 73 | + break; |
| 74 | case OPT_HELP: |
| 75 | help(0); |
| 76 | exit(0); |
| 77 | @@ -2010,7 +2032,9 @@ static void help(const char xopt) |
| 78 | " --lpostfix str append the given string to all other symbols\n" |
| 79 | " --keep-all output files will not be removed even if an error happens\n" |
| 80 | " --no-line ignore %%line directives in input\n" |
| 81 | - " --limit-X val set execution limit X\n"); |
| 82 | + " --limit-X val set execution limit X\n" |
| 83 | + " --debug-prefix-map base=dest\n" |
| 84 | + " remap paths starting with 'base' to 'dest' in output files\n"); |
| 85 | |
| 86 | for (i = 0; i <= LIMIT_MAX; i++) { |
| 87 | printf(" %-15s %s (default ", |
| 88 | diff --git a/include/nasmlib.h b/include/nasmlib.h |
| 89 | index e57d0e6d..cf921547 100644 |
| 90 | --- a/include/nasmlib.h |
| 91 | +++ b/include/nasmlib.h |
| 92 | @@ -195,10 +195,19 @@ int64_t readstrnum(char *str, int length, bool *warn); |
| 93 | */ |
| 94 | int32_t seg_alloc(void); |
| 95 | |
| 96 | +struct debug_prefix_list { |
| 97 | + struct debug_prefix_list *next; |
| 98 | + char *base; |
| 99 | + char *dest; |
| 100 | +}; |
| 101 | + |
| 102 | +extern struct debug_prefix_list *debug_prefixes; |
| 103 | + |
| 104 | /* |
| 105 | * Add/replace or remove an extension to the end of a filename |
| 106 | */ |
| 107 | const char *filename_set_extension(const char *inname, const char *extension); |
| 108 | +char *filename_debug_remap(char *dest, char const *inname, size_t len); |
| 109 | |
| 110 | /* |
| 111 | * Utility macros... |
| 112 | diff --git a/nasm.txt b/nasm.txt |
| 113 | index a28202f9..443c06b2 100644 |
| 114 | --- a/nasm.txt |
| 115 | +++ b/nasm.txt |
| 116 | @@ -147,6 +147,10 @@ OPTIONS |
| 117 | Prepend or append (respectively) the given argument to all global or |
| 118 | extern variables. |
| 119 | |
| 120 | +--debug-prefix-map 'BASE=DEST':: |
| 121 | + Map file names beginning with 'BASE' to 'DEST' when encoding them in |
| 122 | + output object files. |
| 123 | + |
| 124 | SYNTAX |
| 125 | ------ |
| 126 | This man page does not fully describe the syntax of *nasm*'s assembly language, |
| 127 | diff --git a/nasmlib/filename.c b/nasmlib/filename.c |
| 128 | index 172ae0bc..fda2be41 100644 |
| 129 | --- a/nasmlib/filename.c |
| 130 | +++ b/nasmlib/filename.c |
| 131 | @@ -39,6 +39,8 @@ |
| 132 | #include "nasmlib.h" |
| 133 | #include "error.h" |
| 134 | |
| 135 | +struct debug_prefix_list *debug_prefixes = NULL; |
| 136 | + |
| 137 | /* |
| 138 | * Add/modify a filename extension, assumed to be a period-delimited |
| 139 | * field at the very end of the filename. Returns a newly allocated |
| 140 | @@ -61,3 +63,21 @@ const char *filename_set_extension(const char *inname, const char *extension) |
| 141 | |
| 142 | return p; |
| 143 | } |
| 144 | + |
| 145 | +char *filename_debug_remap(char *dest, char const *in, size_t len) |
| 146 | +{ |
| 147 | + struct debug_prefix_list *d; |
| 148 | + size_t n; |
| 149 | + |
| 150 | + for (d = debug_prefixes; d != NULL; d = d->next) { |
| 151 | + n = strlen(d->base); |
| 152 | + if (strncmp(in, d->base, n) == 0) { |
| 153 | + strlcpy(dest, d->dest, len); |
| 154 | + strlcat(dest, &in[n], len); |
| 155 | + return dest; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + strlcpy(dest, in, len); |
| 160 | + return dest; |
| 161 | +} |
| 162 | diff --git a/output/outas86.c b/output/outas86.c |
| 163 | index 3f9867b9..d5f4f966 100644 |
| 164 | --- a/output/outas86.c |
| 165 | +++ b/output/outas86.c |
| 166 | @@ -113,6 +113,8 @@ static void as86_sect_write(struct Section *, const uint8_t *, |
| 167 | |
| 168 | static void as86_init(void) |
| 169 | { |
| 170 | + char filename[FILENAME_MAX]; |
| 171 | + |
| 172 | stext.data = saa_init(1L); |
| 173 | stext.datalen = 0L; |
| 174 | stext.head = stext.last = NULL; |
| 175 | @@ -134,7 +136,7 @@ static void as86_init(void) |
| 176 | strslen = 0; |
| 177 | |
| 178 | /* as86 module name = input file minus extension */ |
| 179 | - as86_add_string(filename_set_extension(inname, "")); |
| 180 | + as86_add_string(filename_debug_remap(filename, filename_set_extension(inname, ""), sizeof(filename))); |
| 181 | } |
| 182 | |
| 183 | static void as86_cleanup(void) |
| 184 | diff --git a/output/outcoff.c b/output/outcoff.c |
| 185 | index a2fd302c..bcf576fb 100644 |
| 186 | --- a/output/outcoff.c |
| 187 | +++ b/output/outcoff.c |
| 188 | @@ -1070,14 +1070,14 @@ static void coff_symbol(char *name, int32_t strpos, int32_t value, |
| 189 | |
| 190 | static void coff_write_symbols(void) |
| 191 | { |
| 192 | - char filename[18]; |
| 193 | + char filename[19]; |
| 194 | uint32_t i; |
| 195 | |
| 196 | /* |
| 197 | * The `.file' record, and the file name auxiliary record. |
| 198 | */ |
| 199 | coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1); |
| 200 | - strncpy(filename, inname, 18); |
| 201 | + filename_debug_remap(filename, inname, 19); |
| 202 | nasm_write(filename, 18, ofile); |
| 203 | |
| 204 | /* |
| 205 | diff --git a/output/outelf.c b/output/outelf.c |
| 206 | index de99d076..203b5dc0 100644 |
| 207 | --- a/output/outelf.c |
| 208 | +++ b/output/outelf.c |
| 209 | @@ -1,5 +1,5 @@ |
| 210 | /* ----------------------------------------------------------------------- * |
| 211 | - * |
| 212 | + * |
| 213 | * Copyright 1996-2017 The NASM Authors - All Rights Reserved |
| 214 | * See the file AUTHORS included with the NASM distribution for |
| 215 | * the specific copyright holders. |
| 216 | @@ -14,7 +14,7 @@ |
| 217 | * copyright notice, this list of conditions and the following |
| 218 | * disclaimer in the documentation and/or other materials provided |
| 219 | * with the distribution. |
| 220 | - * |
| 221 | + * |
| 222 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 223 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 224 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 225 | @@ -315,7 +315,7 @@ elf_directive(enum directive directive, char *value, int pass) |
| 226 | |
| 227 | static void elf_init(void) |
| 228 | { |
| 229 | - strlcpy(elf_module, inname, sizeof(elf_module)); |
| 230 | + filename_debug_remap(elf_module, inname, sizeof(elf_module)); |
| 231 | sects = NULL; |
| 232 | nsects = sectlen = 0; |
| 233 | syms = saa_init((int32_t)sizeof(struct elf_symbol)); |
| 234 | @@ -868,7 +868,7 @@ static void elf32_out(int32_t segto, const void *data, |
| 235 | " segment base references"); |
| 236 | } else { |
| 237 | if (wrt == NO_SEG) { |
| 238 | - /* |
| 239 | + /* |
| 240 | * The if() is a hack to deal with compilers which |
| 241 | * don't handle switch() statements with 64-bit |
| 242 | * expressions. |
| 243 | diff --git a/output/outieee.c b/output/outieee.c |
| 244 | index 3a28942d..f61824e4 100644 |
| 245 | --- a/output/outieee.c |
| 246 | +++ b/output/outieee.c |
| 247 | @@ -209,7 +209,7 @@ static void ieee_unqualified_name(char *, char *); |
| 248 | */ |
| 249 | static void ieee_init(void) |
| 250 | { |
| 251 | - strlcpy(ieee_infile, inname, sizeof(ieee_infile)); |
| 252 | + filename_debug_remap(ieee_infile, inname, sizeof(ieee_infile)); |
| 253 | any_segs = false; |
| 254 | fpubhead = NULL; |
| 255 | fpubtail = &fpubhead; |
| 256 | diff --git a/output/outobj.c b/output/outobj.c |
| 257 | index b4f2c499..55bba4a1 100644 |
| 258 | --- a/output/outobj.c |
| 259 | +++ b/output/outobj.c |
| 260 | @@ -640,7 +640,7 @@ static enum directive_result obj_directive(enum directive, char *, int); |
| 261 | |
| 262 | static void obj_init(void) |
| 263 | { |
| 264 | - strlcpy(obj_infile, inname, sizeof(obj_infile)); |
| 265 | + filename_debug_remap(obj_infile, inname, sizeof(obj_infile)); |
| 266 | first_seg = seg_alloc(); |
| 267 | any_segs = false; |
| 268 | fpubhead = NULL; |
| 269 | diff --git a/stdlib/strlcat.c b/stdlib/strlcat.c |
| 270 | index 7084d460..ee93dea3 100644 |
| 271 | --- a/stdlib/strlcat.c |
| 272 | +++ b/stdlib/strlcat.c |
| 273 | @@ -29,7 +29,7 @@ size_t strlcat(char *dest, const char *src, size_t size) |
| 274 | size_t n; |
| 275 | |
| 276 | /* find the NULL terminator in dest */ |
| 277 | - for (n = 0; i < size && dest[n] != '\0'; n++) |
| 278 | + for (n = 0; n < size && dest[n] != '\0'; n++) |
| 279 | ; |
| 280 | |
| 281 | /* destination was not NULL terminated. Return the initial size */ |
| 282 | diff --git a/test/elfdebugprefix.asm b/test/elfdebugprefix.asm |
| 283 | new file mode 100644 |
| 284 | index 00000000..a67ba29c |
| 285 | --- /dev/null |
| 286 | +++ b/test/elfdebugprefix.asm |
| 287 | @@ -0,0 +1,6 @@ |
| 288 | +;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' |
| 289 | + |
| 290 | + SECTION .text |
| 291 | +test: ; [1] |
| 292 | + ret |
| 293 | + |
| 294 | diff --git a/test/performtest.pl b/test/performtest.pl |
| 295 | index f7865b39..096f9604 100755 |
| 296 | --- a/test/performtest.pl |
| 297 | +++ b/test/performtest.pl |
| 298 | @@ -42,14 +42,22 @@ sub perform { |
| 299 | TEST: |
| 300 | while(<TESTFILE>) { |
| 301 | #See if there is a test case |
| 302 | - last unless /Testname=(.*);\s*Arguments=(.*);\s*Files=(.*)/; |
| 303 | - my ($subname, $arguments, $files) = ($1, $2, $3); |
| 304 | + last unless /Testname=(.*);\s*Arguments=(.*);\s*Files=([^;]*)(?:;\s*Validate=(.*))?/; |
| 305 | + my ($subname, $arguments, $files, $validate) = ($1, $2, $3, $4); |
| 306 | + chomp $files; |
| 307 | debugprint("$subname | $arguments | $files"); |
| 308 | |
| 309 | #Call nasm with this test case |
| 310 | system("$nasm $arguments $testpath > $stdoutfile 2> $stderrfile"); |
| 311 | debugprint("$nasm $arguments $testpath > $stdoutfile 2> $stderrfile ----> $?"); |
| 312 | |
| 313 | + if($validate) { |
| 314 | + if(system("$validate >> $stdoutfile 2>> $stderrfile") != 0) { |
| 315 | + print "Test $testname/$subname validation failed\n"; |
| 316 | + $globalresult = 1; |
| 317 | + } |
| 318 | + } |
| 319 | + |
| 320 | #Move the output to the test dir |
| 321 | mkpath("$outputdir/$testname/$subname"); |
| 322 | foreach(split / /,$files) { |
| 323 | -- |
| 324 | 2.23.0 |
| 325 | |