Skip to content

Commit f2a6eb4

Browse files
mvanotticyrillos
authored andcommitted
Add DW_AT_comp_dir to elf files.
This commit adds the compilation directory debug information for ELF files. This feature helps debuggers locate the source file when debugging. The feature is already present for Mach-O files. Signed-off-by: Marco Vanotti <[email protected]> Signed-off-by: Cyrill Gorcunov <[email protected]>
1 parent a9faae3 commit f2a6eb4

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

output/outelf.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ static struct hash_table section_by_name;
8484
static struct elf_symbol *fwds;
8585

8686
static char elf_module[FILENAME_MAX];
87+
static char elf_dir[FILENAME_MAX];
8788

8889
extern const struct ofmt of_elf32;
8990
extern const struct ofmt of_elf64;
@@ -552,8 +553,10 @@ static void elf_init(void)
552553
".shstrtab", ".strtab", ".symtab", ".symtab_shndx", NULL
553554
};
554555
const char * const *p;
556+
const char * cur_path = nasm_realpath(inname);
555557

556558
strlcpy(elf_module, inname, sizeof(elf_module));
559+
strlcpy(elf_dir, nasm_dirname(cur_path), sizeof(elf_dir));
557560
sects = NULL;
558561
nsects = sectlen = 0;
559562
syms = saa_init((int32_t)sizeof(struct elf_symbol));
@@ -3307,7 +3310,8 @@ static void dwarf_generate(void)
33073310
saa_write32(pinforel, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
33083311
saa_write32(pinforel, 0);
33093312
saa_write32(pinfo,0); /* DW_AT_stmt_list */
3310-
saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3313+
saa_wbytes(pinfo, elf_module, strlen(elf_module)+1); /* DW_AT_name */
3314+
saa_wbytes(pinfo, elf_dir, strlen(elf_dir)+1); /* DW_AT_comp_dir */
33113315
saa_wbytes(pinfo, nasm_signature(), nasm_signature_len()+1);
33123316
saa_write16(pinfo,DW_LANG_Mips_Assembler);
33133317
saa_write8(pinfo,2); /* abbrviation number LEB128u */
@@ -3346,7 +3350,8 @@ static void dwarf_generate(void)
33463350
saa_write32(pinforel, (dwarf_linesym << 8) + R_X86_64_32); /* reloc to line */
33473351
saa_write32(pinforel, 0);
33483352
saa_write32(pinfo,0); /* DW_AT_stmt_list */
3349-
saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3353+
saa_wbytes(pinfo, elf_module, strlen(elf_module)+1); /* DW_AT_name */
3354+
saa_wbytes(pinfo, elf_dir, strlen(elf_dir)+1); /* DW_AT_comp_dir */
33503355
saa_wbytes(pinfo, nasm_signature(), nasm_signature_len()+1);
33513356
saa_write16(pinfo,DW_LANG_Mips_Assembler);
33523357
saa_write8(pinfo,2); /* abbrviation number LEB128u */
@@ -3386,7 +3391,8 @@ static void dwarf_generate(void)
33863391
saa_write64(pinforel, (dwarf_linesym << 32) + R_X86_64_32); /* reloc to line */
33873392
saa_write64(pinforel, 0);
33883393
saa_write32(pinfo,0); /* DW_AT_stmt_list */
3389-
saa_wbytes(pinfo, elf_module, strlen(elf_module)+1);
3394+
saa_wbytes(pinfo, elf_module, strlen(elf_module)+1); /* DW_AT_name */
3395+
saa_wbytes(pinfo, elf_dir, strlen(elf_dir)+1); /* DW_AT_comp_dir */
33903396
saa_wbytes(pinfo, nasm_signature(), nasm_signature_len()+1);
33913397
saa_write16(pinfo,DW_LANG_Mips_Assembler);
33923398
saa_write8(pinfo,2); /* abbrviation number LEB128u */
@@ -3424,6 +3430,8 @@ static void dwarf_generate(void)
34243430
saa_write8(pabbrev,DW_FORM_data4);
34253431
saa_write8(pabbrev,DW_AT_name);
34263432
saa_write8(pabbrev,DW_FORM_string);
3433+
saa_write8(pabbrev,DW_AT_comp_dir);
3434+
saa_write8(pabbrev,DW_FORM_string);
34273435
saa_write8(pabbrev,DW_AT_producer);
34283436
saa_write8(pabbrev,DW_FORM_string);
34293437
saa_write8(pabbrev,DW_AT_language);

0 commit comments

Comments
 (0)