// File: /workspace/github_c_makefile_star2_O2/mikeakohn[P]print_pe/build/../src/pdb.c
// Function: print_pdb_symbols
void print_pdb_symbols(
  struct pdb_dir_t *pdb_dir,
  struct pdb_header_t *pdb_header,
  struct pdb_dbi_t *pdb_dbi,
  FILE *in,
  uint8_t demangle_symbols)
{
  if (pdb_dbi->version_signature == 0)
  {
    read_pdb_dbi_stream(pdb_dir, pdb_header, pdb_dbi, in);
  }

  uint8_t *data = pdb_dir->heap;
  uint32_t virtual_address = 0;

  // Need to find .text section to find the offset to the code.
  if (pdb_dbi->optional_debug_header_size != 0)
  {
    uint8_t *debug_section = data + 64 +
      pdb_dbi->mod_info_size +
      pdb_dbi->section_contribution_size +
      pdb_dbi->section_map_size +
      pdb_dbi->source_info_size +
      pdb_dbi->type_server_size +
      pdb_dbi->ec_substream_size;

    int index = get_int16(debug_section + 10);
    int length = pdb_dir->stream_sizes[index];
    int n = 0;
    uint8_t *next = pdb_dir->heap;
    read_data(pdb_dir, index, pdb_header->block_size, in);

    printf("  -- sections --\n");

    while (n < length)
    {
      const char *name = (char *)next;
      printf("                      name: %-8s\n", name);
      printf("              virtual_size: 0x%08x\n", get_uint32(next + 8));
      printf("           virtual_address: 0x%08x\n", get_uint32(next + 12));
      printf("          size_of_raw_data: 0x%04x\n", get_uint32(next + 16));
      printf("       pointer_to_raw_data: 0x%04x\n", get_uint32(next + 20));
      printf("    pointer_to_relocations: 0x%04x\n", get_uint32(next + 24));
      printf("   pointer_to_line_numbers: 0x%04x\n", get_uint32(next + 28));
      printf("          relocation_count: %d\n", get_uint16(next + 32));
      printf("         line_number_count: %d\n", get_uint16(next + 34));
      printf("           characteristics: 0x%08x\n", get_uint32(next + 36));
      printf("\n");

      if (strncmp(name, ".text", 8) == 0)
      {
        virtual_address = get_uint32(next + 12);
      }

      n += 40;
      next += 40;
    }
  }

  read_data(pdb_dir, pdb_dbi->symbol_record_stream, pdb_header->block_size, in);

  struct symbol_record_t symbol_record;
  int length = pdb_dir->stream_sizes[pdb_dbi->symbol_record_stream];
  uint8_t *next = data;
  int n = 0;

  printf("  -- Symbols --\n");

  while (n < length)
  {
    int offset = 14;

    symbol_record.length = get_uint16(next + 0);
    symbol_record.type = get_uint16(next + 2);

    printf("       length: %d\n", symbol_record.length);

    switch (symbol_record.type)
    {
      case 0x110e:
        printf("  symbol_type: 0x%04x (PUB32)\n", symbol_record.type);
        printf("      section: %d\n", get_int32(next+ 4));
        if (virtual_address != 0)
        {
          printf("       offset: 0x%04x (address=0x%04x)\n",
            get_uint32(next + 8),
            get_uint32(next + 8) + virtual_address);
        }
          else
        {
          printf("       offset: 0x%04x\n", get_uint32(next + 8));
        }
        printf("        index: %d\n", get_uint16(next + 12));
        break;
      case 0x1125:
        printf("  symbol_type: 0x%04x (PROCREF)\n", symbol_record.type);
        printf("      section: %d\n", get_int32(next+ 4));
        printf("       offset: 0x%04x\n", get_uint32(next + 8));
        printf("        index: %d\n", get_uint16(next + 12));
        break;
      case 0x1107:
        printf("  symbol_type: 0x%04x (CONSTANT)\n", symbol_record.type);
        printf("       offset: 0x%04x\n", get_uint32(next + 4));
        printf("        index: %d\n", get_uint16(next + 8));
        offset = 10;
        break;
      case 0x1108:
        printf("  symbol_type: 0x%04x (UDT)\n", symbol_record.type);
        printf("       offset: 0x%04x\n", get_uint32(next + 4));
        offset = 8;
        break;
      case 0x110c:
        printf("  symbol_type: 0x%04x (LDATA32)\n", symbol_record.type);
        printf("      section: %d\n", get_int32(next+ 4));
        printf("       offset: 0x%04x\n", get_uint32(next + 8));
        printf("        index: %d\n", get_uint16(next + 12));
        break;
      case 0x110d:
        printf("  symbol_type: 0x%04x (GDATA32)\n", symbol_record.type);
        printf("      section: %d\n", get_int32(next+ 4));
        printf("       offset: 0x%04x\n", get_uint32(next + 8));
        printf("        index: %d\n", get_uint16(next + 12));
        break;
      case 0x1127:
        printf("  symbol_type: 0x%04x (LPROCREF)\n", symbol_record.type);
        printf("      section: %d\n", get_int32(next+ 4));
        printf("       offset: 0x%04x\n", get_uint32(next + 8));
        printf("        index: %d\n", get_uint16(next + 12));
        break;
      default:
        printf("  symbol_type: 0x%04x (UNKNOWN)\n", symbol_record.type);
        printf("      section: %d\n", get_int32(next+ 4));
        printf("       offset: 0x%04x\n", get_uint32(next + 8));
        printf("        index: %d\n", get_uint16(next + 12));
        break;
    }

    printf("%s\n", next + offset);

    if (demangle_symbols == 1)
    {
      char demangled[1024];

      int r = demangle(
        (const char *)next + offset,
        demangled,
        sizeof(demangled));

      if (r > 0) { printf("%s\n", demangled); }
    }

    printf(" --------\n");

    int align = (symbol_record.length + 3) & ~3;

    next += align;
    n += align;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x438, %rsp            # imm = 0x438
movl %r8d, 0x14(%rsp)
movq %rcx, %rbp
movq %rdx, %r14
movq %rsi, %r8
movq %rdi, %r12
cmpl $0x0, (%rdx)
jne 0x3a92
movq %r12, %rdi
movq %r8, %rsi
movq %r14, %rdx
movq %rbp, %rcx
movq %r8, %rbx
callq 0x31b8
movq %rbx, %r8
movq 0x18(%r12), %r13
cmpl $0x0, 0x30(%r14)
movl $0x0, %ebx
je 0x3c1f
movslq 0x18(%r14), %rax
addq %r13, %rax
movslq 0x1c(%r14), %rcx
movslq 0x20(%r14), %rdx
addq %rcx, %rdx
addq %rax, %rdx
movslq 0x24(%r14), %rax
movslq 0x28(%r14), %rcx
addq %rax, %rcx
addq %rdx, %rcx
movq %r14, 0x8(%rsp)
movslq 0x34(%r14), %rax
movswq 0x4a(%rax,%rcx), %rsi
movq 0x8(%r12), %rax
movl (%rax,%rsi,4), %r15d
movq %r8, 0x18(%rsp)
movl 0x58(%r8), %edx
movq %r12, 0x28(%rsp)
movq %r12, %rdi
movq %rbp, 0x20(%rsp)
movq %rbp, %rcx
callq 0x298d
leaq 0x1ed6(%rip), %rdi      # 0x59dd
callq 0x1060
pushq $0x8
popq %rbp
xorl %r14d, %r14d
xorl %ebx, %ebx
cmpl %r15d, %r14d
jge 0x3c0b
leaq (%r14,%r13), %r12
leaq 0x1a8c(%rip), %rdi      # 0x55b4
movq %r12, %rsi
xorl %eax, %eax
callq 0x1090
movl 0x8(%r13,%r14), %esi
leaq 0x1a98(%rip), %rdi      # 0x55d6
xorl %eax, %eax
callq 0x1090
movl 0xc(%r13,%r14), %esi
leaq 0x1aa9(%rip), %rdi      # 0x55fa
xorl %eax, %eax
callq 0x1090
movl 0x10(%r13,%r14), %esi
leaq 0x1aba(%rip), %rdi      # 0x561e
xorl %eax, %eax
callq 0x1090
movl 0x14(%r13,%r14), %esi
leaq 0x1acb(%rip), %rdi      # 0x5642
xorl %eax, %eax
callq 0x1090
movl 0x18(%r13,%r14), %esi
leaq 0x1adc(%rip), %rdi      # 0x5666
xorl %eax, %eax
callq 0x1090
movl 0x1c(%r13,%r14), %esi
leaq 0x1aed(%rip), %rdi      # 0x568a
xorl %eax, %eax
callq 0x1090
movzwl 0x20(%r13,%r14), %esi
leaq 0x1afd(%rip), %rdi      # 0x56ae
xorl %eax, %eax
callq 0x1090
movzwl 0x22(%r13,%r14), %esi
leaq 0x7de(%rip), %rdi       # 0x43a3
xorl %eax, %eax
callq 0x1090
movl 0x24(%r13,%r14), %esi
leaq 0x1af6(%rip), %rdi      # 0x56ce
xorl %eax, %eax
callq 0x1090
pushq $0xa
popq %rdi
callq 0x1040
movq %r12, %rdi
leaq 0x1b01(%rip), %rsi      # 0x56f2
movq %rbp, %rdx
callq 0x1050
testl %eax, %eax
jne 0x3c02
movl 0xc(%r13,%r14), %ebx
addq $0x28, %r14
jmp 0x3b14
movq 0x8(%rsp), %r14
movq 0x28(%rsp), %r12
movq 0x20(%rsp), %rbp
movq 0x18(%rsp), %r8
movq %rbx, 0x8(%rsp)
movzwl 0x14(%r14), %esi
movl 0x58(%r8), %edx
movq %r12, %rdi
movq %rbp, %rcx
callq 0x298d
movq 0x8(%r12), %rax
movzwl 0x14(%r14), %ecx
movl (%rax,%rcx,4), %ebp
leaq 0x1da2(%rip), %rdi      # 0x59ee
callq 0x1060
leaq 0x30(%rsp), %r14
xorl %r12d, %r12d
cmpl %ebp, %r12d
jge 0x3e44
movzwl (%r13), %r15d
movzwl 0x2(%r13), %ebx
leaq 0x1a85(%rip), %rdi      # 0x56f8
movl %r15d, %esi
xorl %eax, %eax
callq 0x1090
leal -0x1107(%rbx), %eax
cmpl $0x7, %eax
ja 0x3cd7
leaq 0xb79(%rip), %rcx       # 0x4808
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x1afe(%rip), %rdi      # 0x579d
movl $0x1107, %esi           # imm = 0x1107
xorl %eax, %eax
callq 0x1090
movl 0x4(%r13), %esi
leaq 0x1a9c(%rip), %rdi      # 0x5752
xorl %eax, %eax
callq 0x1090
movzwl 0x8(%r13), %esi
leaq 0x1aa0(%rip), %rdi      # 0x5769
xorl %eax, %eax
callq 0x1090
pushq $0xa
jmp 0x3de7
cmpl $0x1125, %ebx           # imm = 0x1125
je 0x3d9b
cmpl $0x1127, %ebx           # imm = 0x1127
jne 0x3cfc
leaq 0x1b2c(%rip), %rdi      # 0x581e
movl $0x1127, %esi           # imm = 0x1127
jmp 0x3da7
leaq 0x1b3d(%rip), %rdi      # 0x5840
movl %ebx, %esi
jmp 0x3da7
leaq 0x1aae(%rip), %rdi      # 0x57bf
movl $0x1108, %esi           # imm = 0x1108
xorl %eax, %eax
callq 0x1090
movl 0x4(%r13), %esi
leaq 0x1a2a(%rip), %rdi      # 0x5752
xorl %eax, %eax
callq 0x1090
pushq $0x8
jmp 0x3de7
leaq 0x1a9f(%rip), %rdi      # 0x57dc
movl $0x110c, %esi           # imm = 0x110C
jmp 0x3da7
leaq 0x1ab2(%rip), %rdi      # 0x57fd
movl $0x110d, %esi           # imm = 0x110D
jmp 0x3da7
leaq 0x19b2(%rip), %rdi      # 0x570b
movl $0x110e, %esi           # imm = 0x110E
xorl %eax, %eax
callq 0x1090
movl 0x4(%r13), %esi
leaq 0x129d(%rip), %rdi      # 0x500d
xorl %eax, %eax
callq 0x1090
movl 0x8(%r13), %esi
movq 0x8(%rsp), %rax
testl %eax, %eax
je 0x3e3b
leal (%rsi,%rax), %edx
leaq 0x1998(%rip), %rdi      # 0x572a
xorl %eax, %eax
callq 0x1090
jmp 0x3dd2
leaq 0x19da(%rip), %rdi      # 0x577c
movl $0x1125, %esi           # imm = 0x1125
xorl %eax, %eax
callq 0x1090
movl 0x4(%r13), %esi
leaq 0x1254(%rip), %rdi      # 0x500d
xorl %eax, %eax
callq 0x1090
movl 0x8(%r13), %esi
leaq 0x1987(%rip), %rdi      # 0x5752
xorl %eax, %eax
callq 0x1090
movzwl 0xc(%r13), %esi
leaq 0x198b(%rip), %rdi      # 0x5769
xorl %eax, %eax
callq 0x1090
pushq $0xe
popq %rbx
addq %r13, %rbx
movq %rbx, %rdi
callq 0x1060
cmpb $0x1, 0x14(%rsp)
jne 0x3e19
movq %rbx, %rdi
movq %r14, %rbx
movq %r14, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x1f6c
testl %eax, %eax
jle 0x3e19
movq %rbx, %rdi
callq 0x1060
leaq 0x1bde(%rip), %rdi      # 0x59fe
callq 0x1060
addl $0x3, %r15d
andl $-0x4, %r15d
addq %r15, %r13
addl %r12d, %r15d
movl %r15d, %r12d
jmp 0x3c59
leaq 0x1910(%rip), %rdi      # 0x5752
jmp 0x3dcb
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq