Skip to content

Commit f88bcb7

Browse files
committed
Add --debug-prefix-map option
Adds an option to remap file prefixes in output object files. This is analogous to the "-fdebug-prefix-map" option in GCC, and allows files to be built in a reproducible manner regardless of the build directory. Signed-off-by: Joshua Watt <[email protected]>
1 parent 676ba55 commit f88bcb7

File tree

15 files changed

+109
-11
lines changed

15 files changed

+109
-11
lines changed

asm/nasm.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,8 @@ enum text_options {
886886
OPT_DEBUG,
887887
OPT_INFO,
888888
OPT_REPRODUCIBLE,
889-
OPT_BITS
889+
OPT_BITS,
890+
OPT_DEBUG_PREFIX_MAP
890891
};
891892
enum need_arg {
892893
ARG_NO,
@@ -925,6 +926,7 @@ static const struct textargs textopts[] = {
925926
{"debug", OPT_DEBUG, ARG_MAYBE, 0},
926927
{"reproducible", OPT_REPRODUCIBLE, ARG_NO, 0},
927928
{"bits", OPT_BITS, ARG_YES, 0},
929+
{"debug-prefix-map", OPT_DEBUG_PREFIX_MAP, ARG_YES, 0},
928930
{NULL, OPT_BOGUS, ARG_NO, 0}
929931
};
930932

@@ -1305,6 +1307,26 @@ static bool process_arg(char *p, char *q, int pass)
13051307
case OPT_REPRODUCIBLE:
13061308
reproducible = true;
13071309
break;
1310+
case OPT_DEBUG_PREFIX_MAP: {
1311+
struct debug_prefix_list *d;
1312+
char *c;
1313+
c = strchr(param, '=');
1314+
1315+
if (!c) {
1316+
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1317+
"option `--%s' must be of the form `BASE=DEST'", p);
1318+
break;
1319+
}
1320+
1321+
*c = '\0';
1322+
d = nasm_malloc(sizeof(*d));
1323+
d->next = debug_prefixes;
1324+
d->base = nasm_strdup(param);
1325+
d->dest = nasm_strdup(c + 1);
1326+
debug_prefixes = d;
1327+
*c = '=';
1328+
}
1329+
break;
13081330
case OPT_HELP:
13091331
/* Allow --help topic without *requiring* topic */
13101332
if (!param)
@@ -2031,6 +2053,8 @@ static void help(FILE *out, const char *what)
20312053
" --lprefix str prepend the given string to local symbols\n"
20322054
" --lpostfix str append the given string to local symbols\n"
20332055
" --reproducible attempt to produce run-to-run identical output\n"
2056+
" --debug-prefix-map base=dest\n"
2057+
" remap paths starting with 'base' to 'dest' in output files\n"
20342058
, out);
20352059
}
20362060
if (help_optor(with, HW_LIMIT)) {

include/nasmlib.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,19 @@ static inline const char *nasm_digit_chars(bool ucase)
334334
*/
335335
int32_t seg_alloc(void);
336336

337+
struct debug_prefix_list {
338+
struct debug_prefix_list *next;
339+
char *base;
340+
char *dest;
341+
};
342+
343+
extern struct debug_prefix_list *debug_prefixes;
344+
337345
/*
338346
* Add/replace or remove an extension to the end of a filename
339347
*/
340348
const char *filename_set_extension(const char *inname, const char *extension);
349+
char *filename_debug_remap(char *dest, char const *inname, size_t len);
341350

342351
/*
343352
* Utility macros...

nasm.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ OPTIONS
147147
Prepend or append (respectively) the given argument to all global or
148148
extern variables.
149149

150+
--debug-prefix-map 'BASE=DEST'::
151+
Map file names beginning with 'BASE' to 'DEST' when encoding them in
152+
output object files.
153+
150154
SYNTAX
151155
------
152156
This man page does not fully describe the syntax of *nasm*'s assembly language,

nasmlib/filename.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "nasmlib.h"
1010
#include "error.h"
1111

12+
struct debug_prefix_list *debug_prefixes = NULL;
13+
1214
/*
1315
* Add/modify a filename extension, assumed to be a period-delimited
1416
* field at the very end of the filename. Returns a newly allocated
@@ -31,3 +33,21 @@ const char *filename_set_extension(const char *inname, const char *extension)
3133

3234
return p;
3335
}
36+
37+
char *filename_debug_remap(char *dest, char const *in, size_t len)
38+
{
39+
struct debug_prefix_list *d;
40+
size_t n;
41+
42+
for (d = debug_prefixes; d != NULL; d = d->next) {
43+
n = strlen(d->base);
44+
if (strncmp(in, d->base, n) == 0) {
45+
strlcpy(dest, d->dest, len);
46+
strlcat(dest, &in[n], len);
47+
return dest;
48+
}
49+
}
50+
51+
strlcpy(dest, in, len);
52+
return dest;
53+
}

output/outas86.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ static struct SAA *strs;
7171
static size_t strslen;
7272

7373
static int as86_reloc_size;
74+
static char filename[FILENAME_MAX];
7475

7576
static void as86_write(void);
7677
static void as86_write_section(struct Section *, int);
@@ -101,7 +102,7 @@ static void as86_init(void)
101102
strslen = 0;
102103

103104
/* as86 module name = input file minus extension */
104-
as86_add_string(filename_set_extension(inname, ""));
105+
as86_add_string(filename_debug_remap(filename, filename_set_extension(inname, ""), sizeof(filename)));
105106
}
106107

107108
static void as86_cleanup(void)

output/outcoff.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,10 +1239,10 @@ static void coff_write_symbols(void)
12391239
*/
12401240
coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
12411241
if (reproducible)
1242-
memset(filename, 0, 18);
1242+
memset(filename, 0, sizeof(filename));
12431243
else
1244-
strncpy(filename, inname, 18);
1245-
nasm_write(filename, 18, ofile);
1244+
filename_debug_remap(filename, inname, sizeof(filename));
1245+
nasm_write(filename, sizeof(filename), ofile);
12461246

12471247
/*
12481248
* The section records, with their auxiliaries.

output/outelf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,8 @@ static void elf_init(void)
516516
const char * const *p;
517517
const char * cur_path = nasm_realpath(inname);
518518

519-
strlcpy(elf_module, inname, sizeof(elf_module));
520-
strlcpy(elf_dir, nasm_dirname(cur_path), sizeof(elf_dir));
519+
filename_debug_remap(elf_module, inname, sizeof(elf_module));
520+
filename_debug_remap(elf_dir, nasm_dirname(cur_path), sizeof(elf_dir));
521521
sects = NULL;
522522
nsects = sectlen = 0;
523523
syms = saa_init((int32_t)sizeof(struct elf_symbol));

output/outieee.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ static void ieee_unqualified_name(char *, char *);
178178
*/
179179
static void ieee_init(void)
180180
{
181-
strlcpy(ieee_infile, inname, sizeof(ieee_infile));
181+
filename_debug_remap(ieee_infile, inname, sizeof(ieee_infile));
182182
any_segs = false;
183183
fpubhead = NULL;
184184
fpubtail = &fpubhead;

output/outobj.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ static const char *get_default_class(const char *segment)
656656

657657
static void obj_init(void)
658658
{
659-
strlcpy(obj_infile, inname, sizeof(obj_infile));
659+
filename_debug_remap(obj_infile, inname, sizeof(obj_infile));
660660
first_seg = seg_alloc();
661661
any_segs = false;
662662
fpubhead = NULL;

test/elfdebugprefix.asm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
;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'
2+
3+
SECTION .text
4+
test: ; [1]
5+
ret
6+

0 commit comments

Comments
 (0)