Skip to content

Commit 0cf2d77

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 c830716 commit 0cf2d77

File tree

17 files changed

+110
-11
lines changed

17 files changed

+110
-11
lines changed

asm/nasm.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,8 @@ enum text_options {
942942
OPT_KEEP_ALL,
943943
OPT_NO_LINE,
944944
OPT_DEBUG,
945-
OPT_REPRODUCIBLE
945+
OPT_REPRODUCIBLE,
946+
OPT_DEBUG_PREFIX_MAP
946947
};
947948
enum need_arg {
948949
ARG_NO,
@@ -975,6 +976,7 @@ static const struct textargs textopts[] = {
975976
{"no-line", OPT_NO_LINE, ARG_NO, 0},
976977
{"debug", OPT_DEBUG, ARG_MAYBE, 0},
977978
{"reproducible", OPT_REPRODUCIBLE, ARG_NO, 0},
979+
{"debug-prefix-map", OPT_DEBUG_PREFIX_MAP, ARG_YES, 0},
978980
{NULL, OPT_BOGUS, ARG_NO, 0}
979981
};
980982

@@ -1338,6 +1340,26 @@ static bool process_arg(char *p, char *q, int pass)
13381340
case OPT_REPRODUCIBLE:
13391341
reproducible = true;
13401342
break;
1343+
case OPT_DEBUG_PREFIX_MAP: {
1344+
struct debug_prefix_list *d;
1345+
char *c;
1346+
c = strchr(param, '=');
1347+
1348+
if (!c) {
1349+
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
1350+
"option `--%s' must be of the form `BASE=DEST'", p);
1351+
break;
1352+
}
1353+
1354+
*c = '\0';
1355+
d = nasm_malloc(sizeof(*d));
1356+
d->next = debug_prefixes;
1357+
d->base = nasm_strdup(param);
1358+
d->dest = nasm_strdup(c + 1);
1359+
debug_prefixes = d;
1360+
*c = '=';
1361+
}
1362+
break;
13411363
case OPT_HELP:
13421364
help(stdout);
13431365
exit(0);
@@ -2296,6 +2318,8 @@ static void help(FILE *out)
22962318
" --lpostfix str append the given string to local symbols\n"
22972319
"\n"
22982320
" --reproducible attempt to produce run-to-run identical output\n"
2321+
" --debug-prefix-map base=dest\n"
2322+
" remap paths starting with 'base' to 'dest' in output files\n"
22992323
"\n"
23002324
" -w+x enable warning x (also -Wx)\n"
23012325
" -w-x disable warning x (also -Wno-x)\n"

include/nasmlib.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,19 @@ int64_t readstrnum(char *str, int length, bool *warn);
250250
*/
251251
int32_t seg_alloc(void);
252252

253+
struct debug_prefix_list {
254+
struct debug_prefix_list *next;
255+
char *base;
256+
char *dest;
257+
};
258+
259+
extern struct debug_prefix_list *debug_prefixes;
260+
253261
/*
254262
* Add/replace or remove an extension to the end of a filename
255263
*/
256264
const char *filename_set_extension(const char *inname, const char *extension);
265+
char *filename_debug_remap(char *dest, char const *inname, size_t len);
257266

258267
/*
259268
* 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
@@ -39,6 +39,8 @@
3939
#include "nasmlib.h"
4040
#include "error.h"
4141

42+
struct debug_prefix_list *debug_prefixes = NULL;
43+
4244
/*
4345
* Add/modify a filename extension, assumed to be a period-delimited
4446
* field at the very end of the filename. Returns a newly allocated
@@ -61,3 +63,21 @@ const char *filename_set_extension(const char *inname, const char *extension)
6163

6264
return p;
6365
}
66+
67+
char *filename_debug_remap(char *dest, char const *in, size_t len)
68+
{
69+
struct debug_prefix_list *d;
70+
size_t n;
71+
72+
for (d = debug_prefixes; d != NULL; d = d->next) {
73+
n = strlen(d->base);
74+
if (strncmp(in, d->base, n) == 0) {
75+
strlcpy(dest, d->dest, len);
76+
strlcat(dest, &in[n], len);
77+
return dest;
78+
}
79+
}
80+
81+
strlcpy(dest, in, len);
82+
return dest;
83+
}

output/outas86.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ static void as86_sect_write(struct Section *, const uint8_t *,
110110

111111
static void as86_init(void)
112112
{
113+
char filename[FILENAME_MAX];
114+
113115
stext.data = saa_init(1L);
114116
stext.datalen = 0L;
115117
stext.head = stext.last = NULL;
@@ -131,7 +133,7 @@ static void as86_init(void)
131133
strslen = 0;
132134

133135
/* as86 module name = input file minus extension */
134-
as86_add_string(filename_set_extension(inname, ""));
136+
as86_add_string(filename_debug_remap(filename, filename_set_extension(inname, ""), sizeof(filename)));
135137
}
136138

137139
static void as86_cleanup(void)

output/outcoff.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,14 +1072,14 @@ static void coff_symbol(char *name, int32_t strpos, int32_t value,
10721072

10731073
static void coff_write_symbols(void)
10741074
{
1075-
char filename[18];
1075+
char filename[19];
10761076
uint32_t i;
10771077

10781078
/*
10791079
* The `.file' record, and the file name auxiliary record.
10801080
*/
10811081
coff_symbol(".file", 0L, 0L, -2, 0, 0x67, 1);
1082-
strncpy(filename, inname, 18);
1082+
filename_debug_remap(filename, inname, 19);
10831083
nasm_write(filename, 18, ofile);
10841084

10851085
/*

output/outelf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ static void elf_init(void)
553553
};
554554
const char * const *p;
555555

556-
strlcpy(elf_module, inname, sizeof(elf_module));
556+
filename_debug_remap(elf_module, inname, sizeof(elf_module));
557557
sects = NULL;
558558
nsects = sectlen = 0;
559559
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
@@ -207,7 +207,7 @@ static void ieee_unqualified_name(char *, char *);
207207
*/
208208
static void ieee_init(void)
209209
{
210-
strlcpy(ieee_infile, inname, sizeof(ieee_infile));
210+
filename_debug_remap(ieee_infile, inname, sizeof(ieee_infile));
211211
any_segs = false;
212212
fpubhead = NULL;
213213
fpubtail = &fpubhead;

output/outobj.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ static enum directive_result obj_directive(enum directive, char *);
644644

645645
static void obj_init(void)
646646
{
647-
strlcpy(obj_infile, inname, sizeof(obj_infile));
647+
filename_debug_remap(obj_infile, inname, sizeof(obj_infile));
648648
first_seg = seg_alloc();
649649
any_segs = false;
650650
fpubhead = NULL;

stdlib/strlcat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ size_t strlcat(char *dest, const char *src, size_t size)
2929
size_t n;
3030

3131
/* find the NULL terminator in dest */
32-
for (n = 0; i < size && dest[n] != '\0'; n++)
32+
for (n = 0; n < size && dest[n] != '\0'; n++)
3333
;
3434

3535
/* destination was not NULL terminated. Return the initial size */

0 commit comments

Comments
 (0)