1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * codeview.c Codeview Debug Format support for COFF
54 static void cv8_init(void);
55 static void cv8_linenum(const char *filename
, int32_t linenumber
,
57 static void cv8_deflabel(char *name
, int32_t segment
, int64_t offset
,
58 int is_global
, char *special
);
59 static void cv8_typevalue(int32_t type
);
60 static void cv8_output(int type
, void *param
);
61 static void cv8_cleanup(void);
63 const struct dfmt df_cv8
= {
64 "Codeview 8", /* .fullname */
65 "cv8", /* .shortname */
67 cv8_linenum
, /* .linenum */
68 cv8_deflabel
, /* .debug_deflabel */
69 null_debug_directive
, /* .debug_directive */
70 cv8_typevalue
, /* .debug_typevalue */
71 cv8_output
, /* .debug_output */
72 cv8_cleanup
, /* .cleanup */
75 /*******************************************************************************
77 ******************************************************************************/
85 struct source_file
*next
;
88 uint32_t sourcetbl_off
;
93 unsigned char md5sum
[MD5_HASHBYTES
];
111 enum symbol_type type
;
120 TYPE_UNREGISTERED
= 0x0000, /* T_NOTYPE */
126 TYPE_REAL32
= 0x0040,
127 TYPE_REAL64
= 0x0041,
128 TYPE_REAL80
= 0x0042,
129 TYPE_REAL128
= 0x0043,
130 TYPE_REAL256
= 0x0044,
139 uint32_t text_offset
;
141 struct source_file
*source_files
, **source_files_tail
;
142 const char *last_filename
;
143 struct source_file
*last_source_file
;
144 struct hash_table file_hash
;
146 uint32_t total_filename_len
;
149 unsigned total_lines
;
152 struct cv8_symbol
*last_sym
;
153 unsigned num_syms
[SYMTYPE_MAX
];
154 unsigned symbol_lengths
;
162 struct cv8_state cv8_state
;
164 static void cv8_init(void)
166 const uint32_t sect_flags
= IMAGE_SCN_MEM_READ
|
167 IMAGE_SCN_MEM_DISCARDABLE
|
168 IMAGE_SCN_CNT_INITIALIZED_DATA
|
169 IMAGE_SCN_ALIGN_1BYTES
;
171 cv8_state
.symbol_sect
= coff_make_section(".debug$S", sect_flags
);
172 cv8_state
.type_sect
= coff_make_section(".debug$T", sect_flags
);
174 cv8_state
.text_offset
= 0;
176 cv8_state
.source_files
= NULL
;
177 cv8_state
.source_files_tail
= &cv8_state
.source_files
;
178 hash_init(&cv8_state
.file_hash
, HASH_MEDIUM
);
180 cv8_state
.num_files
= 0;
181 cv8_state
.total_filename_len
= 0;
183 cv8_state
.total_lines
= 0;
185 cv8_state
.symbols
= saa_init(sizeof(struct cv8_symbol
));
186 cv8_state
.last_sym
= NULL
;
189 static struct source_file
*register_file(const char *filename
);
190 static struct coff_Section
*find_section(int32_t segto
);
192 static void cv8_linenum(const char *filename
, int32_t linenumber
,
195 struct coff_Section
*s
;
197 struct source_file
*file
;
199 file
= register_file(filename
);
201 s
= find_section(segto
);
205 if ((s
->flags
& IMAGE_SCN_MEM_EXECUTE
) == 0)
208 li
= saa_wstruct(file
->lines
);
209 li
->file_offset
= cv8_state
.text_offset
;
210 li
->linenumber
= linenumber
;
213 cv8_state
.total_lines
++;
216 static void cv8_deflabel(char *name
, int32_t segment
, int64_t offset
,
217 int is_global
, char *special
)
219 struct cv8_symbol
*sym
;
220 struct coff_Section
*s
;
224 s
= find_section(segment
);
228 sym
= saa_wstruct(cv8_state
.symbols
);
230 if (s
->flags
& IMAGE_SCN_MEM_EXECUTE
)
231 sym
->type
= is_global
? SYMTYPE_PROC
: SYMTYPE_CODE
;
233 sym
->type
= is_global
? SYMTYPE_GDATA
: SYMTYPE_LDATA
;
234 cv8_state
.num_syms
[sym
->type
]++;
235 cv8_state
.total_syms
++;
237 sym
->section
= segment
;
238 sym
->secrel
= offset
;
239 sym
->symtype
= TYPE_UNREGISTERED
;
243 sym
->name
= nasm_strdup(name
);
244 cv8_state
.symbol_lengths
+= strlen(sym
->name
) + 1;
246 if (cv8_state
.last_sym
&& cv8_state
.last_sym
->section
== segment
)
247 cv8_state
.last_sym
->size
= offset
- cv8_state
.last_sym
->secrel
;
248 cv8_state
.last_sym
= sym
;
251 static void cv8_typevalue(int32_t type
)
253 if (!cv8_state
.last_sym
)
255 if (cv8_state
.last_sym
->symtype
!= TYPE_UNREGISTERED
)
258 switch (TYM_TYPE(type
)) {
260 cv8_state
.last_sym
->symtype
= TYPE_BYTE
;
263 cv8_state
.last_sym
->symtype
= TYPE_WORD
;
266 cv8_state
.last_sym
->symtype
= TYPE_DWORD
;
269 cv8_state
.last_sym
->symtype
= TYPE_QUAD
;
272 cv8_state
.last_sym
->symtype
= TYPE_REAL32
;
275 cv8_state
.last_sym
->symtype
= TYPE_REAL80
;
278 cv8_state
.last_sym
->symtype
= TYPE_REAL128
;
281 cv8_state
.last_sym
->symtype
= TYPE_REAL256
;
290 static void cv8_output(int type
, void *param
)
292 struct coff_DebugInfo
*dinfo
= param
;
296 if (dinfo
->section
&& dinfo
->section
->name
&&
297 !strncmp(dinfo
->section
->name
, ".text", 5))
298 cv8_state
.text_offset
+= dinfo
->size
;
301 static void build_symbol_table(struct coff_Section
*const sect
);
302 static void build_type_table(struct coff_Section
*const sect
);
304 static void cv8_cleanup(void)
306 struct cv8_symbol
*sym
;
307 struct source_file
*file
;
309 struct coff_Section
*symbol_sect
= coff_sects
[cv8_state
.symbol_sect
];
310 struct coff_Section
*type_sect
= coff_sects
[cv8_state
.type_sect
];
312 cv8_state
.outfile
.name
= nasm_realpath(coff_outfile
);
313 cv8_state
.outfile
.namebytes
= strlen(cv8_state
.outfile
.name
) + 1;
315 build_symbol_table(symbol_sect
);
316 build_type_table(type_sect
);
318 list_for_each(file
, cv8_state
.source_files
) {
319 nasm_free(file
->fullname
);
320 saa_free(file
->lines
);
323 hash_free(&cv8_state
.file_hash
);
325 saa_rewind(cv8_state
.symbols
);
326 while ((sym
= saa_rstruct(cv8_state
.symbols
)))
327 nasm_free(sym
->name
);
328 saa_free(cv8_state
.symbols
);
330 nasm_free(cv8_state
.outfile
.name
);
333 /*******************************************************************************
335 ******************************************************************************/
336 static void calc_md5(const char *const filename
,
337 unsigned char sum
[MD5_HASHBYTES
])
340 unsigned char *file_buf
;
344 f
= pp_input_fopen(filename
, NF_BINARY
);
348 file_buf
= nasm_zalloc(BUFSIZ
);
352 size_t i
= fread(file_buf
, 1, BUFSIZ
, f
);
357 MD5Update(&ctx
, file_buf
, i
);
367 nasm_error(ERR_NONFATAL
, "unable to hash file %s. "
368 "Debug information may be unavailable.\n",
374 static struct source_file
*register_file(const char *filename
)
376 struct source_file
*file
;
379 struct hash_insert hi
;
382 * The common case is that we are invoked with the same filename
383 * as we were last time. Make this a pointer comparison: this is
384 * safe because the NASM core code allocates each filename once
385 * and never frees it.
387 if (likely(cv8_state
.last_filename
== filename
))
388 return cv8_state
.last_source_file
;
390 cv8_state
.last_filename
= filename
;
392 filep
= hash_find(&cv8_state
.file_hash
, filename
, &hi
);
396 /* New filename encounter */
398 fullpath
= nasm_realpath(filename
);
400 file
= nasm_zalloc(sizeof(*file
));
402 file
->filename
= filename
;
403 file
->fullname
= fullpath
;
404 file
->fullnamelen
= strlen(fullpath
);
405 file
->lines
= saa_init(sizeof(struct linepair
));
406 *cv8_state
.source_files_tail
= file
;
407 cv8_state
.source_files_tail
= &file
->next
;
408 calc_md5(fullpath
, file
->md5sum
);
410 hash_add(&hi
, filename
, file
);
412 cv8_state
.num_files
++;
413 cv8_state
.total_filename_len
+= file
->fullnamelen
+ 1;
416 cv8_state
.last_source_file
= file
;
420 static struct coff_Section
*find_section(int32_t segto
)
424 for (i
= 0; i
< coff_nsects
; i
++) {
425 struct coff_Section
*sec
;
428 if (segto
== sec
->index
)
434 static void register_reloc(struct coff_Section
*const sect
,
435 char *sym
, uint32_t addr
, uint16_t type
)
437 struct coff_Reloc
*r
;
438 struct coff_Section
*sec
;
441 r
= *sect
->tail
= nasm_malloc(sizeof(struct coff_Reloc
));
442 sect
->tail
= &r
->next
;
447 r
->symbase
= SECT_SYMBOLS
;
451 for (i
= 0; i
< (uint32_t)coff_nsects
; i
++) {
453 if (!strcmp(sym
, sec
->name
)) {
459 saa_rewind(coff_syms
);
460 for (i
= 0; i
< coff_nsyms
; i
++) {
461 struct coff_Symbol
*s
= saa_rstruct(coff_syms
);
463 if (s
->strpos
== -1 && !strcmp(sym
, s
->name
)) {
465 } else if (s
->strpos
!= -1) {
469 symname
= nasm_malloc(s
->namlen
+ 1);
470 saa_fread(coff_strs
, s
->strpos
-4, symname
, s
->namlen
);
471 symname
[s
->namlen
] = '\0';
472 res
= strcmp(sym
, symname
);
478 nasm_panic(0, "codeview: relocation for unregistered symbol: %s", sym
);
481 static inline void section_write32(struct coff_Section
*sect
, uint32_t val
)
483 saa_write32(sect
->data
, val
);
487 static inline void section_write16(struct coff_Section
*sect
, uint16_t val
)
489 saa_write16(sect
->data
, val
);
493 static inline void section_write8(struct coff_Section
*sect
, uint8_t val
)
495 saa_write8(sect
->data
, val
);
499 static inline void section_wbytes(struct coff_Section
*sect
, const void *buf
,
502 saa_wbytes(sect
->data
, buf
, len
);
506 static void write_filename_table(struct coff_Section
*const sect
)
508 uint32_t field_length
;
509 uint32_t tbl_off
= 1; /* offset starts at 1 to skip NULL entry */
510 struct source_file
*file
;
512 nasm_assert(cv8_state
.source_files
!= NULL
);
513 nasm_assert(cv8_state
.num_files
> 0);
514 nasm_assert(cv8_state
.total_filename_len
> 0);
516 field_length
= 1 + cv8_state
.total_filename_len
;
518 section_write32(sect
, 0x000000F3);
519 section_write32(sect
, field_length
);
521 section_write8(sect
, 0);
523 list_for_each(file
, cv8_state
.source_files
) {
524 section_wbytes(sect
, file
->fullname
, file
->fullnamelen
+ 1);
525 file
->filetbl_off
= tbl_off
;
526 tbl_off
+= file
->fullnamelen
+ 1;
530 static void write_sourcefile_table(struct coff_Section
*const sect
)
532 const uint32_t entry_size
= 4 + 2 + MD5_HASHBYTES
+ 2;
534 uint32_t field_length
= 0;
535 uint32_t tbl_off
= 0;
536 struct source_file
*file
;
538 field_length
= entry_size
* cv8_state
.num_files
;
540 section_write32(sect
, 0x000000F4);
541 section_write32(sect
, field_length
);
543 list_for_each(file
, cv8_state
.source_files
) {
544 nasm_assert(file
->filetbl_off
> 0);
545 section_write32(sect
, file
->filetbl_off
);
546 section_write16(sect
, 0x0110);
547 section_wbytes(sect
, file
->md5sum
, MD5_HASHBYTES
);
548 section_write16(sect
, 0);
550 file
->sourcetbl_off
= tbl_off
;
551 tbl_off
+= entry_size
;
555 static void write_linenumber_table(struct coff_Section
*const sect
)
557 const uint32_t file_field_len
= 12;
558 const uint32_t line_field_len
= 8;
561 uint32_t field_length
= 0;
563 struct source_file
*file
;
564 struct coff_Section
*s
;
566 for (i
= 0; i
< coff_nsects
; i
++) {
567 if (!strncmp(coff_sects
[i
]->name
, ".text", 5))
571 if (i
== coff_nsects
)
576 field_length
+= (cv8_state
.num_files
* file_field_len
);
577 field_length
+= (cv8_state
.total_lines
* line_field_len
);
579 section_write32(sect
, 0x000000F2);
580 section_write32(sect
, field_length
);
582 field_base
= sect
->len
;
583 section_write32(sect
, 0); /* SECREL, updated by relocation */
584 section_write16(sect
, 0); /* SECTION, updated by relocation*/
585 section_write16(sect
, 0); /* pad */
586 section_write32(sect
, s
->len
);
588 register_reloc(sect
, ".text", field_base
,
589 win64
? IMAGE_REL_AMD64_SECREL
: IMAGE_REL_I386_SECREL
);
591 register_reloc(sect
, ".text", field_base
+ 4,
592 win64
? IMAGE_REL_AMD64_SECTION
: IMAGE_REL_I386_SECTION
);
594 list_for_each(file
, cv8_state
.source_files
) {
598 section_write32(sect
, file
->sourcetbl_off
);
599 section_write32(sect
, file
->num_lines
);
600 section_write32(sect
, file_field_len
+ (file
->num_lines
* line_field_len
));
603 saa_rewind(file
->lines
);
604 while ((li
= saa_rstruct(file
->lines
))) {
605 section_write32(sect
, li
->file_offset
);
606 section_write32(sect
, li
->linenumber
|= 0x80000000);
611 static uint16_t write_symbolinfo_obj(struct coff_Section
*sect
)
615 obj_len
= 2 + 4 + cv8_state
.outfile
.namebytes
;
617 section_write16(sect
, obj_len
);
618 section_write16(sect
, 0x1101);
619 section_write32(sect
, 0); /* ASM language */
620 section_wbytes(sect
, cv8_state
.outfile
.name
, cv8_state
.outfile
.namebytes
);
625 static uint16_t write_symbolinfo_properties(struct coff_Section
*sect
,
626 const char *const creator_str
)
628 /* https://github.com/Microsoft/microsoft-pdb/blob/1d60e041/include/cvinfo.h#L3313 */
629 uint16_t creator_len
;
631 creator_len
= 2 + 4 + 2 + 3*2 + 3*2 + strlen(creator_str
)+1 + 2;
633 section_write16(sect
, creator_len
);
634 section_write16(sect
, 0x1116);
635 section_write32(sect
, 3); /* language/flags */
637 section_write16(sect
, 0x00D0); /* machine */
639 section_write16(sect
, 0x0006); /* machine */
641 nasm_assert(!"neither win32 nor win64 are set!");
642 section_write16(sect
, 0); /* verFEMajor */
643 section_write16(sect
, 0); /* verFEMinor */
644 section_write16(sect
, 0); /* verFEBuild */
646 /* BinScope/WACK insist on version >= 8.0.50727 */
647 section_write16(sect
, 8); /* verMajor */
648 section_write16(sect
, 0); /* verMinor */
649 section_write16(sect
, 50727); /* verBuild */
651 section_wbytes(sect
, creator_str
, strlen(creator_str
)+1); /* verSt */
653 * normally there would be key/value pairs here, but they aren't
654 * necessary. They are terminated by 2B
656 section_write16(sect
, 0);
661 static uint16_t write_symbolinfo_symbols(struct coff_Section
*sect
)
663 uint16_t len
= 0, field_len
;
665 struct cv8_symbol
*sym
;
667 saa_rewind(cv8_state
.symbols
);
668 while ((sym
= saa_rstruct(cv8_state
.symbols
))) {
672 field_len
= 12 + strlen(sym
->name
) + 1;
673 len
+= field_len
- 2;
674 section_write16(sect
, field_len
);
675 if (sym
->type
== SYMTYPE_LDATA
)
676 section_write16(sect
, 0x110C);
678 section_write16(sect
, 0x110D);
679 section_write32(sect
, sym
->symtype
);
681 field_base
= sect
->len
;
682 section_write32(sect
, 0); /* SECREL */
683 section_write16(sect
, 0); /* SECTION */
687 field_len
= 9 + strlen(sym
->name
) + 1;
688 len
+= field_len
- 2;
689 section_write16(sect
, field_len
);
690 section_write16(sect
, 0x1105);
692 field_base
= sect
->len
;
693 section_write32(sect
, 0); /* SECREL */
694 section_write16(sect
, 0); /* SECTION */
695 section_write8(sect
, 0); /* FLAG */
698 nasm_assert(!"unknown symbol type");
701 section_wbytes(sect
, sym
->name
, strlen(sym
->name
) + 1);
703 register_reloc(sect
, sym
->name
, field_base
,
704 win64
? IMAGE_REL_AMD64_SECREL
:
705 IMAGE_REL_I386_SECREL
);
706 register_reloc(sect
, sym
->name
, field_base
+ 4,
707 win64
? IMAGE_REL_AMD64_SECTION
:
708 IMAGE_REL_I386_SECTION
);
714 static void write_symbolinfo_table(struct coff_Section
*const sect
)
716 static const char creator_str
[] = "The Netwide Assembler " NASM_VER
;
717 uint16_t obj_length
, creator_length
, sym_length
;
718 uint32_t field_length
= 0, out_len
;
720 nasm_assert(cv8_state
.outfile
.namebytes
);
722 /* signature, language, outfile NULL */
723 obj_length
= 2 + 4 + cv8_state
.outfile
.namebytes
;
724 creator_length
= 2 + 4 + 2 + 3*2 + 3*2 + strlen(creator_str
)+1 + 2;
726 sym_length
= ( cv8_state
.num_syms
[SYMTYPE_CODE
] * 7) +
727 ( cv8_state
.num_syms
[SYMTYPE_PROC
] * 7) +
728 ( cv8_state
.num_syms
[SYMTYPE_LDATA
] * 10) +
729 ( cv8_state
.num_syms
[SYMTYPE_GDATA
] * 10) +
730 cv8_state
.symbol_lengths
;
732 field_length
= 2 + obj_length
+
734 (4 * cv8_state
.total_syms
) + sym_length
;
736 section_write32(sect
, 0x000000F1);
737 section_write32(sect
, field_length
);
739 /* for sub fields, length preceeds type */
741 out_len
= write_symbolinfo_obj(sect
);
742 nasm_assert(out_len
== obj_length
);
744 out_len
= write_symbolinfo_properties(sect
, creator_str
);
745 nasm_assert(out_len
== creator_length
);
747 out_len
= write_symbolinfo_symbols(sect
);
748 nasm_assert(out_len
== sym_length
);
751 static inline void align4_table(struct coff_Section
*const sect
)
755 struct SAA
*data
= sect
->data
;
757 if (data
->wptr
% 4 == 0)
760 diff
= 4 - (data
->wptr
% 4);
762 section_wbytes(sect
, &zero
, diff
);
765 static void build_symbol_table(struct coff_Section
*const sect
)
767 section_write32(sect
, 0x00000004);
769 write_filename_table(sect
);
771 write_sourcefile_table(sect
);
773 write_linenumber_table(sect
);
775 write_symbolinfo_table(sect
);
779 static void build_type_table(struct coff_Section
*const sect
)
782 struct cv8_symbol
*sym
;
784 section_write32(sect
, 0x00000004);
786 saa_rewind(cv8_state
.symbols
);
787 while ((sym
= saa_rstruct(cv8_state
.symbols
))) {
788 if (sym
->type
!= SYMTYPE_PROC
)
793 field_len
= 2 + 4 + 4 + 4 + 2;
794 section_write16(sect
, field_len
);
795 section_write16(sect
, 0x1008); /* PROC type */
797 section_write32(sect
, 0x00000003); /* return type */
798 section_write32(sect
, 0); /* calling convention (default) */
799 section_write32(sect
, sym
->typeindex
);
800 section_write16(sect
, 0); /* # params */
805 section_write16(sect
, field_len
);
806 section_write16(sect
, 0x1201); /* ARGLIST */
807 section_write32(sect
, 0); /*num params */