1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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
50 #include "output/outlib.h"
51 #include "output/pecoff.h"
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 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
;
159 struct cv8_state cv8_state
;
161 static void cv8_init(void)
163 const uint32_t sect_flags
= IMAGE_SCN_MEM_READ
|
164 IMAGE_SCN_MEM_DISCARDABLE
|
165 IMAGE_SCN_CNT_INITIALIZED_DATA
|
166 IMAGE_SCN_ALIGN_1BYTES
;
168 cv8_state
.symbol_sect
= coff_make_section(".debug$S", sect_flags
);
169 cv8_state
.type_sect
= coff_make_section(".debug$T", sect_flags
);
171 cv8_state
.text_offset
= 0;
173 cv8_state
.source_files
= NULL
;
174 cv8_state
.source_files_tail
= &cv8_state
.source_files
;
175 hash_init(&cv8_state
.file_hash
, HASH_MEDIUM
);
177 cv8_state
.num_files
= 0;
178 cv8_state
.total_filename_len
= 0;
180 cv8_state
.total_lines
= 0;
182 cv8_state
.symbols
= saa_init(sizeof(struct cv8_symbol
));
183 cv8_state
.last_sym
= NULL
;
185 cv8_state
.cwd
= nasm_realpath(".");
188 static struct source_file
*register_file(const char *filename
);
189 static struct coff_Section
*find_section(int32_t segto
);
191 static void cv8_linenum(const char *filename
, int32_t linenumber
,
194 struct coff_Section
*s
;
196 struct source_file
*file
;
198 file
= register_file(filename
);
200 s
= find_section(segto
);
204 if ((s
->flags
& IMAGE_SCN_MEM_EXECUTE
) == 0)
207 li
= saa_wstruct(file
->lines
);
208 li
->file_offset
= cv8_state
.text_offset
;
209 li
->linenumber
= linenumber
;
212 cv8_state
.total_lines
++;
215 static void cv8_deflabel(char *name
, int32_t segment
, int64_t offset
,
216 int is_global
, char *special
)
218 struct cv8_symbol
*sym
;
219 struct coff_Section
*s
;
223 s
= find_section(segment
);
227 sym
= saa_wstruct(cv8_state
.symbols
);
229 if (s
->flags
& IMAGE_SCN_MEM_EXECUTE
)
230 sym
->type
= is_global
? SYMTYPE_PROC
: SYMTYPE_CODE
;
232 sym
->type
= is_global
? SYMTYPE_GDATA
: SYMTYPE_LDATA
;
233 cv8_state
.num_syms
[sym
->type
]++;
234 cv8_state
.total_syms
++;
236 sym
->section
= segment
;
237 sym
->secrel
= offset
;
238 sym
->symtype
= TYPE_UNREGISTERED
;
242 sym
->name
= nasm_strdup(name
);
243 cv8_state
.symbol_lengths
+= strlen(sym
->name
) + 1;
245 if (cv8_state
.last_sym
&& cv8_state
.last_sym
->section
== segment
)
246 cv8_state
.last_sym
->size
= offset
- cv8_state
.last_sym
->secrel
;
247 cv8_state
.last_sym
= sym
;
250 static void cv8_typevalue(int32_t type
)
252 if (!cv8_state
.last_sym
)
254 if (cv8_state
.last_sym
->symtype
!= TYPE_UNREGISTERED
)
257 switch (TYM_TYPE(type
)) {
259 cv8_state
.last_sym
->symtype
= TYPE_BYTE
;
262 cv8_state
.last_sym
->symtype
= TYPE_WORD
;
265 cv8_state
.last_sym
->symtype
= TYPE_DWORD
;
268 cv8_state
.last_sym
->symtype
= TYPE_QUAD
;
271 cv8_state
.last_sym
->symtype
= TYPE_REAL32
;
274 cv8_state
.last_sym
->symtype
= TYPE_REAL80
;
277 cv8_state
.last_sym
->symtype
= TYPE_REAL128
;
280 cv8_state
.last_sym
->symtype
= TYPE_REAL256
;
289 static void cv8_output(int type
, void *param
)
291 struct coff_DebugInfo
*dinfo
= param
;
295 if (dinfo
->section
&& dinfo
->section
->name
&&
296 !strncmp(dinfo
->section
->name
, ".text", 5))
297 cv8_state
.text_offset
+= dinfo
->size
;
300 static void build_symbol_table(struct coff_Section
*const sect
);
301 static void build_type_table(struct coff_Section
*const sect
);
303 static void cv8_cleanup(void)
305 struct cv8_symbol
*sym
;
306 struct source_file
*file
;
308 struct coff_Section
*symbol_sect
= coff_sects
[cv8_state
.symbol_sect
];
309 struct coff_Section
*type_sect
= coff_sects
[cv8_state
.type_sect
];
311 build_symbol_table(symbol_sect
);
312 build_type_table(type_sect
);
314 list_for_each(file
, cv8_state
.source_files
) {
315 nasm_free(file
->fullname
);
316 saa_free(file
->lines
);
319 hash_free(&cv8_state
.file_hash
);
321 if (cv8_state
.cwd
!= NULL
)
322 nasm_free(cv8_state
.cwd
);
324 saa_rewind(cv8_state
.symbols
);
325 while ((sym
= saa_rstruct(cv8_state
.symbols
)))
326 nasm_free(sym
->name
);
327 saa_free(cv8_state
.symbols
);
330 /*******************************************************************************
332 ******************************************************************************/
333 static void calc_md5(const char *const filename
,
334 unsigned char sum
[MD5_HASHBYTES
])
337 unsigned char *file_buf
;
341 f
= pp_input_fopen(filename
, "rb");
345 file_buf
= nasm_zalloc(BUFSIZ
);
349 size_t i
= fread(file_buf
, 1, BUFSIZ
, f
);
354 MD5Update(&ctx
, file_buf
, i
);
364 nasm_error(ERR_NONFATAL
, "unable to hash file %s. "
365 "Debug information may be unavailable.\n",
371 static struct source_file
*register_file(const char *filename
)
373 struct source_file
*file
;
376 struct hash_insert hi
;
379 * The common case is that we are invoked with the same filename
380 * as we were last time. Make this a pointer comparison: this is
381 * safe because the NASM core code allocates each filename once
382 * and never frees it.
384 if (likely(cv8_state
.last_filename
== filename
))
385 return cv8_state
.last_source_file
;
387 cv8_state
.last_filename
= filename
;
389 filep
= hash_find(&cv8_state
.file_hash
, filename
, &hi
);
393 /* New filename encounter */
395 fullpath
= nasm_realpath(filename
);
397 file
= nasm_zalloc(sizeof(*file
));
399 file
->filename
= filename
;
400 file
->fullname
= fullpath
;
401 file
->fullnamelen
= strlen(fullpath
);
402 file
->lines
= saa_init(sizeof(struct linepair
));
403 *cv8_state
.source_files_tail
= file
;
404 cv8_state
.source_files_tail
= &file
->next
;
405 calc_md5(fullpath
, file
->md5sum
);
407 hash_add(&hi
, filename
, file
);
409 cv8_state
.num_files
++;
410 cv8_state
.total_filename_len
+= file
->fullnamelen
+ 1;
413 cv8_state
.last_source_file
= file
;
417 static struct coff_Section
*find_section(int32_t segto
)
421 for (i
= 0; i
< coff_nsects
; i
++) {
422 struct coff_Section
*sec
;
425 if (segto
== sec
->index
)
431 static void register_reloc(struct coff_Section
*const sect
,
432 char *sym
, uint32_t addr
, uint16_t type
)
434 struct coff_Reloc
*r
;
435 struct coff_Section
*sec
;
437 r
= *sect
->tail
= nasm_malloc(sizeof(struct coff_Reloc
));
438 sect
->tail
= &r
->next
;
443 r
->symbase
= SECT_SYMBOLS
;
447 for (int i
= 0; i
< coff_nsects
; i
++) {
449 if (!strcmp(sym
, sec
->name
)) {
455 saa_rewind(coff_syms
);
456 for (uint32_t i
= 0; i
< coff_nsyms
; i
++) {
457 struct coff_Symbol
*s
= saa_rstruct(coff_syms
);
459 if (s
->strpos
== -1 && !strcmp(sym
, s
->name
)) {
461 } else if (s
->strpos
!= -1) {
465 symname
= nasm_malloc(s
->namlen
+ 1);
466 saa_fread(coff_strs
, s
->strpos
-4, symname
, s
->namlen
);
467 symname
[s
->namlen
] = '\0';
468 res
= strcmp(sym
, symname
);
474 nasm_panic(0, "codeview: relocation for unregistered symbol: %s", sym
);
477 static inline void section_write32(struct coff_Section
*sect
, uint32_t val
)
479 saa_write32(sect
->data
, val
);
483 static inline void section_write16(struct coff_Section
*sect
, uint16_t val
)
485 saa_write16(sect
->data
, val
);
489 static inline void section_write8(struct coff_Section
*sect
, uint8_t val
)
491 saa_write8(sect
->data
, val
);
495 static inline void section_wbytes(struct coff_Section
*sect
, const void *buf
,
498 saa_wbytes(sect
->data
, buf
, len
);
502 static void write_filename_table(struct coff_Section
*const sect
)
504 uint32_t field_length
;
505 uint32_t tbl_off
= 1; /* offset starts at 1 to skip NULL entry */
506 struct source_file
*file
;
508 nasm_assert(cv8_state
.source_files
!= NULL
);
509 nasm_assert(cv8_state
.num_files
> 0);
510 nasm_assert(cv8_state
.total_filename_len
> 0);
512 field_length
= 1 + cv8_state
.total_filename_len
;
514 section_write32(sect
, 0x000000F3);
515 section_write32(sect
, field_length
);
517 section_write8(sect
, 0);
519 list_for_each(file
, cv8_state
.source_files
) {
520 section_wbytes(sect
, file
->fullname
, file
->fullnamelen
+ 1);
521 file
->filetbl_off
= tbl_off
;
522 tbl_off
+= file
->fullnamelen
+ 1;
526 static void write_sourcefile_table(struct coff_Section
*const sect
)
528 const uint32_t entry_size
= 4 + 2 + MD5_HASHBYTES
+ 2;
530 uint32_t field_length
= 0;
531 uint32_t tbl_off
= 0;
532 struct source_file
*file
;
534 field_length
= entry_size
* cv8_state
.num_files
;
536 section_write32(sect
, 0x000000F4);
537 section_write32(sect
, field_length
);
539 list_for_each(file
, cv8_state
.source_files
) {
540 nasm_assert(file
->filetbl_off
> 0);
541 section_write32(sect
, file
->filetbl_off
);
542 section_write16(sect
, 0x0110);
543 section_wbytes(sect
, file
->md5sum
, MD5_HASHBYTES
);
544 section_write16(sect
, 0);
546 file
->sourcetbl_off
= tbl_off
;
547 tbl_off
+= entry_size
;
551 static void write_linenumber_table(struct coff_Section
*const sect
)
553 const uint32_t file_field_len
= 12;
554 const uint32_t line_field_len
= 8;
557 uint32_t field_length
= 0;
559 struct source_file
*file
;
560 struct coff_Section
*s
;
562 for (i
= 0; i
< coff_nsects
; i
++) {
563 if (!strncmp(coff_sects
[i
]->name
, ".text", 5))
567 if (i
== coff_nsects
)
572 field_length
+= (cv8_state
.num_files
* file_field_len
);
573 field_length
+= (cv8_state
.total_lines
* line_field_len
);
575 section_write32(sect
, 0x000000F2);
576 section_write32(sect
, field_length
);
578 field_base
= sect
->len
;
579 section_write32(sect
, 0); /* SECREL, updated by relocation */
580 section_write16(sect
, 0); /* SECTION, updated by relocation*/
581 section_write16(sect
, 0); /* pad */
582 section_write32(sect
, s
->len
);
584 register_reloc(sect
, ".text", field_base
,
585 win64
? IMAGE_REL_AMD64_SECREL
: IMAGE_REL_I386_SECREL
);
587 register_reloc(sect
, ".text", field_base
+ 4,
588 win64
? IMAGE_REL_AMD64_SECTION
: IMAGE_REL_I386_SECTION
);
590 list_for_each(file
, cv8_state
.source_files
) {
594 section_write32(sect
, file
->sourcetbl_off
);
595 section_write32(sect
, file
->num_lines
);
596 section_write32(sect
, file_field_len
+ (file
->num_lines
* line_field_len
));
599 saa_rewind(file
->lines
);
600 while ((li
= saa_rstruct(file
->lines
))) {
601 section_write32(sect
, li
->file_offset
);
602 section_write32(sect
, li
->linenumber
|= 0x80000000);
607 static uint16_t write_symbolinfo_obj(struct coff_Section
*sect
,
612 obj_len
= 2 + 4 + strlen(cv8_state
.cwd
)+ 1 + strlen(coff_outfile
) +1;
614 section_write16(sect
, obj_len
);
615 section_write16(sect
, 0x1101);
616 section_write32(sect
, 0); /* ASM language */
617 section_wbytes(sect
, cv8_state
.cwd
, strlen(cv8_state
.cwd
));
618 section_write8(sect
, sep
);
619 section_wbytes(sect
, coff_outfile
, strlen(coff_outfile
)+1);
624 static uint16_t write_symbolinfo_properties(struct coff_Section
*sect
,
625 const char *const creator_str
)
627 uint16_t creator_len
;
629 creator_len
= 2 + 4 + 4 + 4 + 4 + strlen(creator_str
)+1 + 2;
631 section_write16(sect
, creator_len
);
632 section_write16(sect
, 0x1116);
633 section_write32(sect
, 3); /* language */
635 section_write32(sect
, 0x000000D0);
637 section_write32(sect
, 0x00000006);
639 nasm_assert(!"neither win32 nor win64 are set!");
640 section_write32(sect
, 0); /* flags*/
641 section_write32(sect
, 8); /* version */
642 section_wbytes(sect
, creator_str
, strlen(creator_str
)+1);
644 * normally there would be key/value pairs here, but they aren't
645 * necessary. They are terminated by 2B
647 section_write16(sect
, 0);
652 static uint16_t write_symbolinfo_symbols(struct coff_Section
*sect
)
654 uint16_t len
= 0, field_len
;
656 struct cv8_symbol
*sym
;
658 saa_rewind(cv8_state
.symbols
);
659 while ((sym
= saa_rstruct(cv8_state
.symbols
))) {
663 field_len
= 12 + strlen(sym
->name
) + 1;
664 len
+= field_len
- 2;
665 section_write16(sect
, field_len
);
666 if (sym
->type
== SYMTYPE_LDATA
)
667 section_write16(sect
, 0x110C);
669 section_write16(sect
, 0x110D);
670 section_write32(sect
, sym
->symtype
);
672 field_base
= sect
->len
;
673 section_write32(sect
, 0); /* SECREL */
674 section_write16(sect
, 0); /* SECTION */
678 field_len
= 9 + strlen(sym
->name
) + 1;
679 len
+= field_len
- 2;
680 section_write16(sect
, field_len
);
681 section_write16(sect
, 0x1105);
683 field_base
= sect
->len
;
684 section_write32(sect
, 0); /* SECREL */
685 section_write16(sect
, 0); /* SECTION */
686 section_write8(sect
, 0); /* FLAG */
689 nasm_assert(!"unknown symbol type");
692 section_wbytes(sect
, sym
->name
, strlen(sym
->name
) + 1);
694 register_reloc(sect
, sym
->name
, field_base
,
695 win64
? IMAGE_REL_AMD64_SECREL
:
696 IMAGE_REL_I386_SECREL
);
697 register_reloc(sect
, sym
->name
, field_base
+ 4,
698 win64
? IMAGE_REL_AMD64_SECTION
:
699 IMAGE_REL_I386_SECTION
);
705 static void write_symbolinfo_table(struct coff_Section
*const sect
)
707 const char sep
= '\\';
708 const char *creator_str
= "The Netwide Assembler " NASM_VER
;
711 uint16_t obj_length
, creator_length
, sym_length
;
712 uint32_t field_length
= 0, out_len
;
714 /* signature, language, workingdir / coff_outfile NULL */
715 obj_length
= 2 + 4 + strlen(cv8_state
.cwd
)+ 1 + strlen(coff_outfile
) +1;
716 creator_length
= 2 + 4 + 4 + 4 + 4 + strlen(creator_str
)+1 + 2;
718 sym_length
= ( cv8_state
.num_syms
[SYMTYPE_CODE
] * 7) +
719 ( cv8_state
.num_syms
[SYMTYPE_PROC
] * 7) +
720 ( cv8_state
.num_syms
[SYMTYPE_LDATA
] * 10) +
721 ( cv8_state
.num_syms
[SYMTYPE_GDATA
] * 10) +
722 cv8_state
.symbol_lengths
;
724 field_length
= 2 + obj_length
+
726 (4 * cv8_state
.total_syms
) + sym_length
;
728 section_write32(sect
, 0x000000F1);
729 section_write32(sect
, field_length
);
731 /* for sub fields, length preceeds type */
733 out_len
= write_symbolinfo_obj(sect
, sep
);
734 nasm_assert(out_len
== obj_length
);
736 out_len
= write_symbolinfo_properties(sect
, creator_str
);
737 nasm_assert(out_len
== creator_length
);
739 out_len
= write_symbolinfo_symbols(sect
);
740 nasm_assert(out_len
== sym_length
);
743 static inline void align4_table(struct coff_Section
*const sect
)
747 struct SAA
*data
= sect
->data
;
749 if (data
->wptr
% 4 == 0)
752 diff
= 4 - (data
->wptr
% 4);
754 section_wbytes(sect
, &zero
, diff
);
757 static void build_symbol_table(struct coff_Section
*const sect
)
759 section_write32(sect
, 0x00000004);
761 write_filename_table(sect
);
763 write_sourcefile_table(sect
);
765 write_linenumber_table(sect
);
767 write_symbolinfo_table(sect
);
771 static void build_type_table(struct coff_Section
*const sect
)
774 struct cv8_symbol
*sym
;
776 section_write32(sect
, 0x00000004);
778 saa_rewind(cv8_state
.symbols
);
779 while ((sym
= saa_rstruct(cv8_state
.symbols
))) {
780 if (sym
->type
!= SYMTYPE_PROC
)
785 field_len
= 2 + 4 + 4 + 4 + 2;
786 section_write16(sect
, field_len
);
787 section_write16(sect
, 0x1008); /* PROC type */
789 section_write32(sect
, 0x00000003); /* return type */
790 section_write32(sect
, 0); /* calling convention (default) */
791 section_write32(sect
, sym
->typeindex
);
792 section_write16(sect
, 0); /* # params */
797 section_write16(sect
, field_len
);
798 section_write16(sect
, 0x1201); /* ARGLIST */
799 section_write32(sect
, 0); /*num params */