| 1 |
/* SPDX-License-Identifier: BSD-2-Clause */ |
| 2 |
/* Copyright 1996-2020 The NASM Authors - All Rights Reserved */ |
| 3 |
|
| 4 |
/* |
| 5 |
* codeview.c Codeview Debug Format support for COFF |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "version.h" |
| 9 |
#include "compiler.h" |
| 10 |
|
| 11 |
|
| 12 |
#include "nasm.h" |
| 13 |
#include "nasmlib.h" |
| 14 |
#include "error.h" |
| 15 |
#include "preproc.h" |
| 16 |
#include "saa.h" |
| 17 |
#include "hashtbl.h" |
| 18 |
#include "outlib.h" |
| 19 |
#include "pecoff.h" |
| 20 |
#include "md5.h" |
| 21 |
|
| 22 |
static void cv8_init(void); |
| 23 |
static void cv8_linenum(const char *filename, int32_t linenumber, |
| 24 |
int32_t segto); |
| 25 |
static void cv8_deflabel(char *name, int32_t segment, int64_t offset, |
| 26 |
int is_global, char *special); |
| 27 |
static void cv8_typevalue(int32_t type); |
| 28 |
static void cv8_output(int type, void *param); |
| 29 |
static void cv8_cleanup(void); |
| 30 |
|
| 31 |
const struct dfmt df_cv8 = { |
| 32 |
"Codeview 8+", /* .fullname */ |
| 33 |
"cv8", /* .shortname */ |
| 34 |
cv8_init, /* .init */ |
| 35 |
cv8_linenum, /* .linenum */ |
| 36 |
cv8_deflabel, /* .debug_deflabel */ |
| 37 |
NULL, /* .debug_smacros */ |
| 38 |
NULL, /* .debug_include */ |
| 39 |
NULL, /* .debug_mmacros */ |
| 40 |
null_debug_directive, /* .debug_directive */ |
| 41 |
cv8_typevalue, /* .debug_typevalue */ |
| 42 |
cv8_output, /* .debug_output */ |
| 43 |
cv8_cleanup, /* .cleanup */ |
| 44 |
NULL /* pragma list */ |
| 45 |
}; |
| 46 |
|
| 47 |
/******************************************************************************* |
| 48 |
* dfmt callbacks |
| 49 |
******************************************************************************/ |
| 50 |
struct source_file; |
| 51 |
|
| 52 |
struct source_file { |
| 53 |
const char *filename; |
| 54 |
char *fullname; |
| 55 |
uint32_t fullnamelen; |
| 56 |
|
| 57 |
struct source_file *next; |
| 58 |
|
| 59 |
uint32_t filetbl_off; |
| 60 |
uint32_t sourcetbl_off; |
| 61 |
|
| 62 |
struct SAA *lines; |
| 63 |
uint32_t num_lines; |
| 64 |
|
| 65 |
unsigned char md5sum[MD5_HASHBYTES]; |
| 66 |
}; |
| 67 |
|
| 68 |
struct linepair { |
| 69 |
uint32_t file_offset; |
| 70 |
uint32_t linenumber; |
| 71 |
}; |
| 72 |
|
| 73 |
enum symbol_type { |
| 74 |
SYMTYPE_CODE, |
| 75 |
SYMTYPE_PROC, |
| 76 |
SYMTYPE_LDATA, |
| 77 |
SYMTYPE_GDATA, |
| 78 |
|
| 79 |
SYMTYPE_MAX |
| 80 |
}; |
| 81 |
|
| 82 |
struct cv8_symbol { |
| 83 |
enum symbol_type type; |
| 84 |
char *name; |
| 85 |
|
| 86 |
uint32_t secrel; |
| 87 |
uint16_t section; |
| 88 |
uint32_t size; |
| 89 |
uint32_t typeindex; |
| 90 |
|
| 91 |
enum symtype { |
| 92 |
TYPE_UNREGISTERED = 0x0000, /* T_NOTYPE */ |
| 93 |
TYPE_BYTE = 0x0020, |
| 94 |
TYPE_WORD = 0x0021, |
| 95 |
TYPE_DWORD= 0x0022, |
| 96 |
TYPE_QUAD = 0x0023, |
| 97 |
|
| 98 |
TYPE_REAL32 = 0x0040, |
| 99 |
TYPE_REAL64 = 0x0041, |
| 100 |
TYPE_REAL80 = 0x0042, |
| 101 |
TYPE_REAL128= 0x0043, |
| 102 |
TYPE_REAL256= 0x0044, |
| 103 |
TYPE_REAL512= 0x0045 |
| 104 |
} symtype; |
| 105 |
}; |
| 106 |
|
| 107 |
struct cv8_state { |
| 108 |
int symbol_sect; |
| 109 |
int type_sect; |
| 110 |
|
| 111 |
uint32_t text_offset; |
| 112 |
|
| 113 |
struct source_file *source_files, **source_files_tail; |
| 114 |
const char *last_filename; |
| 115 |
struct source_file *last_source_file; |
| 116 |
struct hash_table file_hash; |
| 117 |
unsigned num_files; |
| 118 |
uint32_t total_filename_len; |
| 119 |
|
| 120 |
|
| 121 |
unsigned total_lines; |
| 122 |
|
| 123 |
struct SAA *symbols; |
| 124 |
struct cv8_symbol *last_sym; |
| 125 |
unsigned num_syms[SYMTYPE_MAX]; |
| 126 |
unsigned symbol_lengths; |
| 127 |
unsigned total_syms; |
| 128 |
|
| 129 |
struct { |
| 130 |
char *name; |
| 131 |
size_t namebytes; |
| 132 |
} outfile; |
| 133 |
}; |
| 134 |
struct cv8_state cv8_state; |
| 135 |
|
| 136 |
static void cv8_init(void) |
| 137 |
{ |
| 138 |
const uint32_t sect_flags = IMAGE_SCN_MEM_READ | |
| 139 |
IMAGE_SCN_MEM_DISCARDABLE | |
| 140 |
IMAGE_SCN_CNT_INITIALIZED_DATA | |
| 141 |
IMAGE_SCN_ALIGN_1BYTES; |
| 142 |
|
| 143 |
cv8_state.symbol_sect = coff_make_section(".debug$S", sect_flags); |
| 144 |
cv8_state.type_sect = coff_make_section(".debug$T", sect_flags); |
| 145 |
|
| 146 |
cv8_state.text_offset = 0; |
| 147 |
|
| 148 |
cv8_state.source_files = NULL; |
| 149 |
cv8_state.source_files_tail = &cv8_state.source_files; |
| 150 |
|
| 151 |
cv8_state.num_files = 0; |
| 152 |
cv8_state.total_filename_len = 0; |
| 153 |
|
| 154 |
cv8_state.total_lines = 0; |
| 155 |
|
| 156 |
cv8_state.symbols = saa_init(sizeof(struct cv8_symbol)); |
| 157 |
cv8_state.last_sym = NULL; |
| 158 |
} |
| 159 |
|
| 160 |
static struct source_file *register_file(const char *filename); |
| 161 |
static struct coff_Section *find_section(int32_t segto); |
| 162 |
|
| 163 |
static void cv8_linenum(const char *filename, int32_t linenumber, |
| 164 |
int32_t segto) |
| 165 |
{ |
| 166 |
struct coff_Section *s; |
| 167 |
struct linepair *li; |
| 168 |
struct source_file *file; |
| 169 |
|
| 170 |
file = register_file(filename); |
| 171 |
|
| 172 |
s = find_section(segto); |
| 173 |
if (s == NULL) |
| 174 |
return; |
| 175 |
|
| 176 |
if ((s->flags & IMAGE_SCN_MEM_EXECUTE) == 0) |
| 177 |
return; |
| 178 |
|
| 179 |
li = saa_wstruct(file->lines); |
| 180 |
li->file_offset = cv8_state.text_offset; |
| 181 |
li->linenumber = linenumber; |
| 182 |
|
| 183 |
file->num_lines++; |
| 184 |
cv8_state.total_lines++; |
| 185 |
} |
| 186 |
|
| 187 |
static void cv8_deflabel(char *name, int32_t segment, int64_t offset, |
| 188 |
int is_global, char *special) |
| 189 |
{ |
| 190 |
struct cv8_symbol *sym; |
| 191 |
struct coff_Section *s; |
| 192 |
|
| 193 |
(void)special; |
| 194 |
|
| 195 |
s = find_section(segment); |
| 196 |
if (s == NULL) |
| 197 |
return; |
| 198 |
|
| 199 |
sym = saa_wstruct(cv8_state.symbols); |
| 200 |
|
| 201 |
if (s->flags & IMAGE_SCN_MEM_EXECUTE) |
| 202 |
sym->type = is_global ? SYMTYPE_PROC : SYMTYPE_CODE; |
| 203 |
else |
| 204 |
sym->type = is_global ? SYMTYPE_GDATA : SYMTYPE_LDATA; |
| 205 |
cv8_state.num_syms[sym->type]++; |
| 206 |
cv8_state.total_syms++; |
| 207 |
|
| 208 |
sym->section = segment; |
| 209 |
sym->secrel = offset; |
| 210 |
sym->symtype = TYPE_UNREGISTERED; |
| 211 |
sym->size = 0; |
| 212 |
sym->typeindex = 0; |
| 213 |
|
| 214 |
sym->name = nasm_strdup(name); |
| 215 |
cv8_state.symbol_lengths += strlen(sym->name) + 1; |
| 216 |
|
| 217 |
if (cv8_state.last_sym && cv8_state.last_sym->section == segment) |
| 218 |
cv8_state.last_sym->size = offset - cv8_state.last_sym->secrel; |
| 219 |
cv8_state.last_sym = sym; |
| 220 |
} |
| 221 |
|
| 222 |
static void cv8_typevalue(int32_t type) |
| 223 |
{ |
| 224 |
if (!cv8_state.last_sym) |
| 225 |
return; |
| 226 |
if (cv8_state.last_sym->symtype != TYPE_UNREGISTERED) |
| 227 |
return; |
| 228 |
|
| 229 |
switch (TYM_TYPE(type)) { |
| 230 |
case TY_BYTE: |
| 231 |
cv8_state.last_sym->symtype = TYPE_BYTE; |
| 232 |
break; |
| 233 |
case TY_WORD: |
| 234 |
cv8_state.last_sym->symtype = TYPE_WORD; |
| 235 |
break; |
| 236 |
case TY_DWORD: |
| 237 |
cv8_state.last_sym->symtype = TYPE_DWORD; |
| 238 |
break; |
| 239 |
case TY_QWORD: |
| 240 |
cv8_state.last_sym->symtype = TYPE_QUAD; |
| 241 |
break; |
| 242 |
case TY_FLOAT: |
| 243 |
cv8_state.last_sym->symtype = TYPE_REAL32; |
| 244 |
break; |
| 245 |
case TY_TBYTE: |
| 246 |
cv8_state.last_sym->symtype = TYPE_REAL80; |
| 247 |
break; |
| 248 |
case TY_OWORD: |
| 249 |
cv8_state.last_sym->symtype = TYPE_REAL128; |
| 250 |
break; |
| 251 |
case TY_YWORD: |
| 252 |
cv8_state.last_sym->symtype = TYPE_REAL256; |
| 253 |
break; |
| 254 |
case TY_ZWORD: |
| 255 |
cv8_state.last_sym->symtype = TYPE_REAL512; |
| 256 |
break; |
| 257 |
case TY_UNKNOWN: |
| 258 |
break; |
| 259 |
case TY_LABEL: |
| 260 |
break; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
static void cv8_output(int type, void *param) |
| 265 |
{ |
| 266 |
struct coff_DebugInfo *dinfo = param; |
| 267 |
|
| 268 |
(void)type; |
| 269 |
|
| 270 |
if (dinfo->section && dinfo->section->name && |
| 271 |
!strncmp(dinfo->section->name, ".text", 5)) |
| 272 |
cv8_state.text_offset += dinfo->size; |
| 273 |
} |
| 274 |
|
| 275 |
static void build_symbol_table(struct coff_Section *const sect); |
| 276 |
static void build_type_table(struct coff_Section *const sect); |
| 277 |
|
| 278 |
static void cv8_cleanup(void) |
| 279 |
{ |
| 280 |
struct cv8_symbol *sym; |
| 281 |
struct source_file *file, *ftmp; |
| 282 |
|
| 283 |
struct coff_Section *symbol_sect = coff_sects[cv8_state.symbol_sect]; |
| 284 |
struct coff_Section *type_sect = coff_sects[cv8_state.type_sect]; |
| 285 |
|
| 286 |
cv8_state.outfile.name = nasm_realpath(outname); |
| 287 |
cv8_state.outfile.namebytes = strlen(cv8_state.outfile.name) + 1; |
| 288 |
|
| 289 |
build_symbol_table(symbol_sect); |
| 290 |
build_type_table(type_sect); |
| 291 |
|
| 292 |
list_for_each_safe(file, ftmp, cv8_state.source_files) { |
| 293 |
nasm_free(file->fullname); |
| 294 |
saa_free(file->lines); |
| 295 |
nasm_free(file); |
| 296 |
} |
| 297 |
hash_free(&cv8_state.file_hash); |
| 298 |
|
| 299 |
saa_rewind(cv8_state.symbols); |
| 300 |
while ((sym = saa_rstruct(cv8_state.symbols))) |
| 301 |
nasm_free(sym->name); |
| 302 |
saa_free(cv8_state.symbols); |
| 303 |
|
| 304 |
nasm_free(cv8_state.outfile.name); |
| 305 |
} |
| 306 |
|
| 307 |
/******************************************************************************* |
| 308 |
* implementation |
| 309 |
******************************************************************************/ |
| 310 |
static void calc_md5(const char *const filename, |
| 311 |
unsigned char sum[MD5_HASHBYTES]) |
| 312 |
{ |
| 313 |
int success = 0; |
| 314 |
unsigned char *file_buf; |
| 315 |
FILE *f; |
| 316 |
MD5_CTX ctx; |
| 317 |
|
| 318 |
f = pp_input_fopen(filename, NF_BINARY); |
| 319 |
if (!f) |
| 320 |
goto done; |
| 321 |
|
| 322 |
file_buf = nasm_zalloc(BUFSIZ); |
| 323 |
|
| 324 |
MD5Init(&ctx); |
| 325 |
while (!feof(f)) { |
| 326 |
size_t i = fread(file_buf, 1, BUFSIZ, f); |
| 327 |
if (ferror(f)) |
| 328 |
goto done_0; |
| 329 |
else if (i == 0) |
| 330 |
break; |
| 331 |
MD5Update(&ctx, file_buf, i); |
| 332 |
} |
| 333 |
MD5Final(sum, &ctx); |
| 334 |
|
| 335 |
success = 1; |
| 336 |
done_0: |
| 337 |
nasm_free(file_buf); |
| 338 |
fclose(f); |
| 339 |
done: |
| 340 |
if (!success) { |
| 341 |
nasm_nonfatal("unable to hash file %s. " |
| 342 |
"Debug information may be unavailable.", |
| 343 |
filename); |
| 344 |
} |
| 345 |
return; |
| 346 |
} |
| 347 |
|
| 348 |
static struct source_file *register_file(const char *filename) |
| 349 |
{ |
| 350 |
struct source_file *file; |
| 351 |
void **filep; |
| 352 |
char *fullpath; |
| 353 |
struct hash_insert hi; |
| 354 |
|
| 355 |
/* |
| 356 |
* The common case is that we are invoked with the same filename |
| 357 |
* as we were last time. Make this a pointer comparison: this is |
| 358 |
* safe because the NASM core code allocates each filename once |
| 359 |
* and never frees it. |
| 360 |
*/ |
| 361 |
if (likely(cv8_state.last_filename == filename)) |
| 362 |
return cv8_state.last_source_file; |
| 363 |
|
| 364 |
cv8_state.last_filename = filename; |
| 365 |
|
| 366 |
filep = hash_find(&cv8_state.file_hash, filename, &hi); |
| 367 |
if (likely(filep)) { |
| 368 |
file = *filep; |
| 369 |
} else { |
| 370 |
/* New filename encounter */ |
| 371 |
|
| 372 |
fullpath = nasm_realpath(filename); |
| 373 |
|
| 374 |
nasm_new(file); |
| 375 |
file->filename = filename; |
| 376 |
file->fullname = fullpath; |
| 377 |
file->fullnamelen = strlen(fullpath); |
| 378 |
file->lines = saa_init(sizeof(struct linepair)); |
| 379 |
*cv8_state.source_files_tail = file; |
| 380 |
cv8_state.source_files_tail = &file->next; |
| 381 |
calc_md5(fullpath, file->md5sum); |
| 382 |
|
| 383 |
hash_add(&hi, filename, file); |
| 384 |
|
| 385 |
cv8_state.num_files++; |
| 386 |
cv8_state.total_filename_len += file->fullnamelen + 1; |
| 387 |
} |
| 388 |
|
| 389 |
cv8_state.last_source_file = file; |
| 390 |
return file; |
| 391 |
} |
| 392 |
|
| 393 |
static struct coff_Section *find_section(int32_t segto) |
| 394 |
{ |
| 395 |
int i; |
| 396 |
|
| 397 |
for (i = 0; i < coff_nsects; i++) { |
| 398 |
struct coff_Section *sec; |
| 399 |
|
| 400 |
sec = coff_sects[i]; |
| 401 |
if (segto == sec->index) |
| 402 |
return sec; |
| 403 |
} |
| 404 |
return NULL; |
| 405 |
} |
| 406 |
|
| 407 |
static void register_reloc(struct coff_Section *const sect, |
| 408 |
char *sym, uint32_t addr, uint16_t type) |
| 409 |
{ |
| 410 |
struct coff_Reloc *r; |
| 411 |
struct coff_Section *sec; |
| 412 |
uint32_t i; |
| 413 |
|
| 414 |
r = *sect->tail = nasm_malloc(sizeof(struct coff_Reloc)); |
| 415 |
sect->tail = &r->next; |
| 416 |
r->next = NULL; |
| 417 |
sect->nrelocs++; |
| 418 |
|
| 419 |
r->address = addr; |
| 420 |
r->symbase = SECT_SYMBOLS; |
| 421 |
r->type = type; |
| 422 |
|
| 423 |
r->symbol = 0; |
| 424 |
for (i = 0; i < (uint32_t)coff_nsects; i++) { |
| 425 |
sec = coff_sects[i]; |
| 426 |
if (!strcmp(sym, sec->name)) { |
| 427 |
return; |
| 428 |
} |
| 429 |
r->symbol += 2; |
| 430 |
} |
| 431 |
|
| 432 |
saa_rewind(coff_syms); |
| 433 |
for (i = 0; i < coff_nsyms; i++) { |
| 434 |
struct coff_Symbol *s = saa_rstruct(coff_syms); |
| 435 |
r->symbol++; |
| 436 |
if (s->strpos == -1 && !strcmp(sym, s->name)) { |
| 437 |
return; |
| 438 |
} else if (s->strpos != -1) { |
| 439 |
int res; |
| 440 |
char *symname; |
| 441 |
|
| 442 |
symname = nasm_malloc(s->namlen + 1); |
| 443 |
saa_fread(coff_strs, s->strpos-4, symname, s->namlen); |
| 444 |
symname[s->namlen] = '\0'; |
| 445 |
res = strcmp(sym, symname); |
| 446 |
nasm_free(symname); |
| 447 |
if (!res) |
| 448 |
return; |
| 449 |
} |
| 450 |
} |
| 451 |
nasm_panic("codeview: relocation for unregistered symbol: %s", sym); |
| 452 |
} |
| 453 |
|
| 454 |
static inline void section_write32(struct coff_Section *sect, uint32_t val) |
| 455 |
{ |
| 456 |
saa_write32(sect->data, val); |
| 457 |
sect->len += 4; |
| 458 |
} |
| 459 |
|
| 460 |
static inline void section_write16(struct coff_Section *sect, uint16_t val) |
| 461 |
{ |
| 462 |
saa_write16(sect->data, val); |
| 463 |
sect->len += 2; |
| 464 |
} |
| 465 |
|
| 466 |
static inline void section_write8(struct coff_Section *sect, uint8_t val) |
| 467 |
{ |
| 468 |
saa_write8(sect->data, val); |
| 469 |
sect->len++; |
| 470 |
} |
| 471 |
|
| 472 |
static inline void section_wbytes(struct coff_Section *sect, const void *buf, |
| 473 |
size_t len) |
| 474 |
{ |
| 475 |
saa_wbytes(sect->data, buf, len); |
| 476 |
sect->len += len; |
| 477 |
} |
| 478 |
|
| 479 |
static void write_filename_table(struct coff_Section *const sect) |
| 480 |
{ |
| 481 |
uint32_t field_length; |
| 482 |
uint32_t tbl_off = 1; /* offset starts at 1 to skip NULL entry */ |
| 483 |
struct source_file *file; |
| 484 |
|
| 485 |
nasm_assert(cv8_state.source_files != NULL); |
| 486 |
nasm_assert(cv8_state.num_files > 0); |
| 487 |
nasm_assert(cv8_state.total_filename_len > 0); |
| 488 |
|
| 489 |
field_length = 1 + cv8_state.total_filename_len; |
| 490 |
|
| 491 |
section_write32(sect, 0x000000F3); |
| 492 |
section_write32(sect, field_length); |
| 493 |
|
| 494 |
section_write8(sect, 0); |
| 495 |
|
| 496 |
list_for_each(file, cv8_state.source_files) { |
| 497 |
section_wbytes(sect, file->fullname, file->fullnamelen + 1); |
| 498 |
file->filetbl_off = tbl_off; |
| 499 |
tbl_off += file->fullnamelen + 1; |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
static void write_sourcefile_table(struct coff_Section *const sect) |
| 504 |
{ |
| 505 |
const uint32_t entry_size = 4 + 2 + MD5_HASHBYTES + 2; |
| 506 |
|
| 507 |
uint32_t field_length = 0; |
| 508 |
uint32_t tbl_off = 0; |
| 509 |
struct source_file *file; |
| 510 |
|
| 511 |
field_length = entry_size * cv8_state.num_files; |
| 512 |
|
| 513 |
section_write32(sect, 0x000000F4); |
| 514 |
section_write32(sect, field_length); |
| 515 |
|
| 516 |
list_for_each(file, cv8_state.source_files) { |
| 517 |
nasm_assert(file->filetbl_off > 0); |
| 518 |
section_write32(sect, file->filetbl_off); |
| 519 |
section_write16(sect, 0x0110); |
| 520 |
section_wbytes(sect, file->md5sum, MD5_HASHBYTES); |
| 521 |
section_write16(sect, 0); |
| 522 |
|
| 523 |
file->sourcetbl_off = tbl_off; |
| 524 |
tbl_off += entry_size; |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
static void write_linenumber_table(struct coff_Section *const sect) |
| 529 |
{ |
| 530 |
const uint32_t file_field_len = 12; |
| 531 |
const uint32_t line_field_len = 8; |
| 532 |
|
| 533 |
int i; |
| 534 |
uint32_t field_length = 0; |
| 535 |
size_t field_base; |
| 536 |
struct source_file *file; |
| 537 |
struct coff_Section *s; |
| 538 |
|
| 539 |
for (i = 0; i < coff_nsects; i++) { |
| 540 |
if (!strncmp(coff_sects[i]->name, ".text", 5)) |
| 541 |
break; |
| 542 |
} |
| 543 |
|
| 544 |
if (i == coff_nsects) |
| 545 |
return; |
| 546 |
s = coff_sects[i]; |
| 547 |
|
| 548 |
field_length = 12; |
| 549 |
field_length += (cv8_state.num_files * file_field_len); |
| 550 |
field_length += (cv8_state.total_lines * line_field_len); |
| 551 |
|
| 552 |
section_write32(sect, 0x000000F2); |
| 553 |
section_write32(sect, field_length); |
| 554 |
|
| 555 |
field_base = sect->len; |
| 556 |
section_write32(sect, 0); /* SECREL, updated by relocation */ |
| 557 |
section_write16(sect, 0); /* SECTION, updated by relocation*/ |
| 558 |
section_write16(sect, 0); /* pad */ |
| 559 |
section_write32(sect, s->len); |
| 560 |
|
| 561 |
register_reloc(sect, ".text", field_base, |
| 562 |
win64 ? IMAGE_REL_AMD64_SECREL : IMAGE_REL_I386_SECREL); |
| 563 |
|
| 564 |
register_reloc(sect, ".text", field_base + 4, |
| 565 |
win64 ? IMAGE_REL_AMD64_SECTION : IMAGE_REL_I386_SECTION); |
| 566 |
|
| 567 |
list_for_each(file, cv8_state.source_files) { |
| 568 |
struct linepair *li; |
| 569 |
|
| 570 |
/* source mapping */ |
| 571 |
section_write32(sect, file->sourcetbl_off); |
| 572 |
section_write32(sect, file->num_lines); |
| 573 |
section_write32(sect, file_field_len + (file->num_lines * line_field_len)); |
| 574 |
|
| 575 |
/* the pairs */ |
| 576 |
saa_rewind(file->lines); |
| 577 |
while ((li = saa_rstruct(file->lines))) { |
| 578 |
section_write32(sect, li->file_offset); |
| 579 |
section_write32(sect, li->linenumber |= 0x80000000); |
| 580 |
} |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
static uint32_t write_symbolinfo_obj(struct coff_Section *sect) |
| 585 |
{ |
| 586 |
uint32_t obj_len; |
| 587 |
|
| 588 |
obj_len = 2 + 4 + cv8_state.outfile.namebytes; |
| 589 |
|
| 590 |
section_write16(sect, obj_len); |
| 591 |
section_write16(sect, 0x1101); |
| 592 |
section_write32(sect, 0); /* ASM language */ |
| 593 |
section_wbytes(sect, cv8_state.outfile.name, cv8_state.outfile.namebytes); |
| 594 |
|
| 595 |
return obj_len; |
| 596 |
} |
| 597 |
|
| 598 |
static uint32_t write_symbolinfo_properties(struct coff_Section *sect, |
| 599 |
const char *const creator_str) |
| 600 |
{ |
| 601 |
/* https://github.com/Microsoft/microsoft-pdb/blob/1d60e041/include/cvinfo.h#L3313 */ |
| 602 |
uint32_t creator_len; |
| 603 |
|
| 604 |
creator_len = 2 + 4 + 2 + 3*2 + 3*2 + strlen(creator_str)+1 + 2; |
| 605 |
|
| 606 |
/* |
| 607 |
* We used to use a language ID of 3 for "MASM", since it's closest of the |
| 608 |
* options available; however, BinScope from WACK (the Windows Application |
| 609 |
* Certification Kit) tests for specific minimum MASM versions and trying to |
| 610 |
* match an increasing sequence of random MASM version/build numbers seems |
| 611 |
* like a fool's errand. |
| 612 |
* |
| 613 |
* Instead, use a different language ID (NASM is, after all, not MASM |
| 614 |
* syntax) and just write the actual NASM version number. BinScope appears |
| 615 |
* to be happy with that. |
| 616 |
*/ |
| 617 |
|
| 618 |
section_write16(sect, creator_len); |
| 619 |
section_write16(sect, 0x1116); |
| 620 |
section_write32(sect, 'N'); /* language: 'N' (0x4e) for "NASM"; flags are 0 */ |
| 621 |
if (win64) |
| 622 |
section_write16(sect, 0x00D0); /* machine */ |
| 623 |
else if (win32) |
| 624 |
section_write16(sect, 0x0006); /* machine */ |
| 625 |
else |
| 626 |
nasm_panic("neither win32 nor win64 are set!"); |
| 627 |
section_write16(sect, 0); /* verFEMajor */ |
| 628 |
section_write16(sect, 0); /* verFEMinor */ |
| 629 |
section_write16(sect, 0); /* verFEBuild */ |
| 630 |
|
| 631 |
/* BinScope/WACK insist on version >= 8.0.50727 */ |
| 632 |
section_write16(sect, NASM_MAJOR_VER); /* verMajor */ |
| 633 |
section_write16(sect, NASM_MINOR_VER); /* verMinor */ |
| 634 |
section_write16(sect, NASM_SUBMINOR_VER*100 + NASM_PATCHLEVEL_VER); /* verBuild */ |
| 635 |
|
| 636 |
section_wbytes(sect, creator_str, strlen(creator_str)+1); /* verSt */ |
| 637 |
/* |
| 638 |
* normally there would be key/value pairs here, but they aren't |
| 639 |
* necessary. They are terminated by 2B |
| 640 |
*/ |
| 641 |
section_write16(sect, 0); |
| 642 |
|
| 643 |
return creator_len; |
| 644 |
} |
| 645 |
|
| 646 |
static uint32_t write_symbolinfo_symbols(struct coff_Section *sect) |
| 647 |
{ |
| 648 |
uint32_t len = 0, field_len; |
| 649 |
uint32_t field_base; |
| 650 |
struct cv8_symbol *sym; |
| 651 |
|
| 652 |
saa_rewind(cv8_state.symbols); |
| 653 |
while ((sym = saa_rstruct(cv8_state.symbols))) { |
| 654 |
switch (sym->type) { |
| 655 |
case SYMTYPE_LDATA: |
| 656 |
case SYMTYPE_GDATA: |
| 657 |
field_len = 12 + strlen(sym->name) + 1; |
| 658 |
len += field_len - 2; |
| 659 |
section_write16(sect, field_len); |
| 660 |
if (sym->type == SYMTYPE_LDATA) |
| 661 |
section_write16(sect, 0x110C); |
| 662 |
else |
| 663 |
section_write16(sect, 0x110D); |
| 664 |
section_write32(sect, sym->symtype); |
| 665 |
|
| 666 |
field_base = sect->len; |
| 667 |
section_write32(sect, 0); /* SECREL */ |
| 668 |
section_write16(sect, 0); /* SECTION */ |
| 669 |
break; |
| 670 |
case SYMTYPE_PROC: |
| 671 |
case SYMTYPE_CODE: |
| 672 |
field_len = 9 + strlen(sym->name) + 1; |
| 673 |
len += field_len - 2; |
| 674 |
section_write16(sect, field_len); |
| 675 |
section_write16(sect, 0x1105); |
| 676 |
|
| 677 |
field_base = sect->len; |
| 678 |
section_write32(sect, 0); /* SECREL */ |
| 679 |
section_write16(sect, 0); /* SECTION */ |
| 680 |
section_write8(sect, 0); /* FLAG */ |
| 681 |
break; |
| 682 |
default: |
| 683 |
nasm_panic("unknown symbol type"); |
| 684 |
} |
| 685 |
|
| 686 |
section_wbytes(sect, sym->name, strlen(sym->name) + 1); |
| 687 |
|
| 688 |
register_reloc(sect, sym->name, field_base, |
| 689 |
win64 ? IMAGE_REL_AMD64_SECREL : |
| 690 |
IMAGE_REL_I386_SECREL); |
| 691 |
register_reloc(sect, sym->name, field_base + 4, |
| 692 |
win64 ? IMAGE_REL_AMD64_SECTION : |
| 693 |
IMAGE_REL_I386_SECTION); |
| 694 |
} |
| 695 |
|
| 696 |
return len; |
| 697 |
} |
| 698 |
|
| 699 |
static void write_symbolinfo_table(struct coff_Section *const sect) |
| 700 |
{ |
| 701 |
static const char creator_str[] = "The Netwide Assembler " NASM_VER; |
| 702 |
uint32_t obj_length, creator_length, sym_length; |
| 703 |
uint32_t field_length = 0, out_len; |
| 704 |
|
| 705 |
nasm_assert(cv8_state.outfile.namebytes); |
| 706 |
|
| 707 |
/* signature, language, outfile NULL */ |
| 708 |
obj_length = 2 + 4 + cv8_state.outfile.namebytes; |
| 709 |
creator_length = 2 + 4 + 2 + 3*2 + 3*2 + strlen(creator_str)+1 + 2; |
| 710 |
|
| 711 |
sym_length = ( cv8_state.num_syms[SYMTYPE_CODE] * 7) + |
| 712 |
( cv8_state.num_syms[SYMTYPE_PROC] * 7) + |
| 713 |
( cv8_state.num_syms[SYMTYPE_LDATA] * 10) + |
| 714 |
( cv8_state.num_syms[SYMTYPE_GDATA] * 10) + |
| 715 |
cv8_state.symbol_lengths; |
| 716 |
|
| 717 |
field_length = 2 + obj_length + |
| 718 |
2 + creator_length + |
| 719 |
(4 * cv8_state.total_syms) + sym_length; |
| 720 |
|
| 721 |
section_write32(sect, 0x000000F1); |
| 722 |
section_write32(sect, field_length); |
| 723 |
|
| 724 |
/* for sub fields, length proceeds type */ |
| 725 |
|
| 726 |
out_len = write_symbolinfo_obj(sect); |
| 727 |
nasm_assert(out_len == obj_length); |
| 728 |
|
| 729 |
out_len = write_symbolinfo_properties(sect, creator_str); |
| 730 |
nasm_assert(out_len == creator_length); |
| 731 |
|
| 732 |
out_len = write_symbolinfo_symbols(sect); |
| 733 |
nasm_assert(out_len == sym_length); |
| 734 |
} |
| 735 |
|
| 736 |
static inline void align4_table(struct coff_Section *const sect) |
| 737 |
{ |
| 738 |
unsigned diff; |
| 739 |
uint32_t zero = 0; |
| 740 |
struct SAA *data = sect->data; |
| 741 |
|
| 742 |
if (data->wptr % 4 == 0) |
| 743 |
return; |
| 744 |
|
| 745 |
diff = 4 - (data->wptr % 4); |
| 746 |
if (diff) |
| 747 |
section_wbytes(sect, &zero, diff); |
| 748 |
} |
| 749 |
|
| 750 |
static void build_symbol_table(struct coff_Section *const sect) |
| 751 |
{ |
| 752 |
section_write32(sect, 0x00000004); |
| 753 |
|
| 754 |
write_filename_table(sect); |
| 755 |
align4_table(sect); |
| 756 |
write_sourcefile_table(sect); |
| 757 |
align4_table(sect); |
| 758 |
write_linenumber_table(sect); |
| 759 |
align4_table(sect); |
| 760 |
write_symbolinfo_table(sect); |
| 761 |
align4_table(sect); |
| 762 |
} |
| 763 |
|
| 764 |
static void build_type_table(struct coff_Section *const sect) |
| 765 |
{ |
| 766 |
uint32_t field_len; |
| 767 |
uint32_t typeindex = 0x1000; |
| 768 |
uint32_t idx_arglist; |
| 769 |
|
| 770 |
section_write32(sect, 0x00000004); |
| 771 |
|
| 772 |
/* empty argument list type */ |
| 773 |
field_len = 2 + 4; |
| 774 |
section_write16(sect, field_len); |
| 775 |
section_write16(sect, 0x1201); /* ARGLIST */ |
| 776 |
section_write32(sect, 0); /* num params */ |
| 777 |
idx_arglist = typeindex++; |
| 778 |
|
| 779 |
/* procedure type: void proc(void) */ |
| 780 |
field_len = 2 + 4 + 1 + 1 + 2 + 4; |
| 781 |
section_write16(sect, field_len); |
| 782 |
section_write16(sect, 0x1008); /* PROC type */ |
| 783 |
|
| 784 |
section_write32(sect, 0x00000003); /* return type VOID */ |
| 785 |
section_write8(sect, 0); /* calling convention (default) */ |
| 786 |
section_write8(sect, 0); /* function attributes */ |
| 787 |
section_write16(sect, 0); /* # params */ |
| 788 |
section_write32(sect, idx_arglist); /* argument list type */ |
| 789 |
/* idx_voidfunc = typeindex++; */ |
| 790 |
} |