// File: /workspace/github_c_makefile_star2_O3/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, 0xc(%rsp)
movq %rcx, %r15
movq %rdx, %rbx
movq %rsi, %r13
movq %rdi, %r14
cmpl $0x0, (%rdx)
jne 0x3c54
movq %r14, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq %r15, %rcx
callq 0x330a
movq 0x18(%r14), %rbp
movq $0x0, (%rsp)
cmpl $0x0, 0x30(%rbx)
movq %rbx, 0x28(%rsp)
movq %r14, 0x20(%rsp)
movq %r15, 0x18(%rsp)
movq %r13, 0x10(%rsp)
je 0x3df2
movslq 0x18(%rbx), %rax
addq %rbp, %rax
movslq 0x1c(%rbx), %rcx
movslq 0x20(%rbx), %rdx
addq %rcx, %rdx
addq %rax, %rdx
movslq 0x24(%rbx), %rax
movslq 0x28(%rbx), %rcx
addq %rax, %rcx
addq %rdx, %rcx
movslq 0x34(%rbx), %rax
movzbl 0x4a(%rax,%rcx), %edx
movzbl 0x4b(%rax,%rcx), %eax
shll $0x8, %eax
movswq %ax, %rsi
orq %rdx, %rsi
movq 0x8(%r14), %rax
movl (%rax,%rsi,4), %r12d
movl 0x58(%r13), %edx
movq %r14, %rdi
movq %r15, %rcx
callq 0x2aaa
leaq 0x2d06(%rip), %rdi      # 0x69dd
callq 0x1060
testl %r12d, %r12d
jle 0x3df2
leaq 0x29e2(%rip), %r15      # 0x66ce
leaq 0x29ff(%rip), %rbx      # 0x66f2
xorl %r13d, %r13d
movq $0x0, (%rsp)
movq %rbp, %r14
addq %r13, %r14
leaq 0x28a9(%rip), %rdi      # 0x65b4
movq %r14, %rsi
xorl %eax, %eax
callq 0x1090
movl 0x8(%rbp,%r13), %esi
leaq 0x28b5(%rip), %rdi      # 0x65d6
xorl %eax, %eax
callq 0x1090
movl 0xc(%rbp,%r13), %esi
leaq 0x28c6(%rip), %rdi      # 0x65fa
xorl %eax, %eax
callq 0x1090
movl 0x10(%rbp,%r13), %esi
leaq 0x28d7(%rip), %rdi      # 0x661e
xorl %eax, %eax
callq 0x1090
movl 0x14(%rbp,%r13), %esi
leaq 0x28e8(%rip), %rdi      # 0x6642
xorl %eax, %eax
callq 0x1090
movl 0x18(%rbp,%r13), %esi
leaq 0x28f9(%rip), %rdi      # 0x6666
xorl %eax, %eax
callq 0x1090
movl 0x1c(%rbp,%r13), %esi
leaq 0x290a(%rip), %rdi      # 0x668a
xorl %eax, %eax
callq 0x1090
movzwl 0x20(%rbp,%r13), %esi
leaq 0x291a(%rip), %rdi      # 0x66ae
xorl %eax, %eax
callq 0x1090
movzwl 0x22(%rbp,%r13), %esi
leaq 0x15fb(%rip), %rdi      # 0x53a3
xorl %eax, %eax
callq 0x1090
movl 0x24(%rbp,%r13), %esi
movq %r15, %rdi
xorl %eax, %eax
callq 0x1090
movl $0xa, %edi
callq 0x1040
movl $0x8, %edx
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1050
testl %eax, %eax
jne 0x3de5
movl 0xc(%rbp,%r13), %eax
movq %rax, (%rsp)
addq $0x28, %r13
cmpl %r12d, %r13d
jl 0x3cfe
movq 0x28(%rsp), %rbx
movzwl 0x14(%rbx), %esi
movq 0x10(%rsp), %rax
movl 0x58(%rax), %edx
movq 0x20(%rsp), %r14
movq %r14, %rdi
movq 0x18(%rsp), %rcx
callq 0x2aaa
movq 0x8(%r14), %rax
movzwl 0x14(%rbx), %ecx
movl (%rax,%rcx,4), %r13d
leaq 0x2bc6(%rip), %rdi      # 0x69ee
callq 0x1060
testl %r13d, %r13d
jle 0x401e
leaq 0x19cb(%rip), %r14      # 0x5808
xorl %r12d, %r12d
movzwl (%rbp), %r15d
movzwl 0x2(%rbp), %ebx
leaq 0x28a8(%rip), %rdi      # 0x66f8
movl %r15d, %esi
xorl %eax, %eax
callq 0x1090
leal -0x1107(%rbx), %eax
cmpl $0x7, %eax
ja 0x3eae
movslq (%r14,%rax,4), %rax
addq %r14, %rax
jmpq *%rax
leaq 0x2928(%rip), %rdi      # 0x679d
movl $0x1107, %esi           # imm = 0x1107
xorl %eax, %eax
callq 0x1090
movl 0x4(%rbp), %esi
leaq 0x28c7(%rip), %rdi      # 0x6752
xorl %eax, %eax
callq 0x1090
movzwl 0x8(%rbp), %esi
leaq 0x28cc(%rip), %rdi      # 0x6769
xorl %eax, %eax
callq 0x1090
movl $0xa, %ebx
jmp 0x3fbd
cmpl $0x1125, %ebx           # imm = 0x1125
je 0x3f71
cmpl $0x1127, %ebx           # imm = 0x1127
jne 0x3ed3
leaq 0x2955(%rip), %rdi      # 0x681e
movl $0x1127, %esi           # imm = 0x1127
jmp 0x3f7d
leaq 0x2966(%rip), %rdi      # 0x6840
movl %ebx, %esi
jmp 0x3f7d
leaq 0x28d7(%rip), %rdi      # 0x67bf
movl $0x1108, %esi           # imm = 0x1108
xorl %eax, %eax
callq 0x1090
movl 0x4(%rbp), %esi
leaq 0x2854(%rip), %rdi      # 0x6752
xorl %eax, %eax
callq 0x1090
movl $0x8, %ebx
jmp 0x3fbd
leaq 0x28c6(%rip), %rdi      # 0x67dc
movl $0x110c, %esi           # imm = 0x110C
jmp 0x3f7d
leaq 0x28d9(%rip), %rdi      # 0x67fd
movl $0x110d, %esi           # imm = 0x110D
jmp 0x3f7d
leaq 0x27d9(%rip), %rdi      # 0x670b
movl $0x110e, %esi           # imm = 0x110E
xorl %eax, %eax
callq 0x1090
movl 0x4(%rbp), %esi
leaq 0x20c5(%rip), %rdi      # 0x600d
xorl %eax, %eax
callq 0x1090
movl 0x8(%rbp), %esi
movq (%rsp), %rax
testl %eax, %eax
je 0x4015
leal (%rsi,%rax), %edx
leaq 0x27c2(%rip), %rdi      # 0x672a
xorl %eax, %eax
callq 0x1090
jmp 0x3fa6
leaq 0x2804(%rip), %rdi      # 0x677c
movl $0x1125, %esi           # imm = 0x1125
xorl %eax, %eax
callq 0x1090
movl 0x4(%rbp), %esi
leaq 0x207f(%rip), %rdi      # 0x600d
xorl %eax, %eax
callq 0x1090
movl 0x8(%rbp), %esi
leaq 0x27b3(%rip), %rdi      # 0x6752
xorl %eax, %eax
callq 0x1090
movzwl 0xc(%rbp), %esi
leaq 0x27b8(%rip), %rdi      # 0x6769
xorl %eax, %eax
callq 0x1090
movl $0xe, %ebx
addq %rbp, %rbx
movq %rbx, %rdi
callq 0x1060
cmpb $0x1, 0xc(%rsp)
jne 0x3ff0
movq %rbx, %rdi
leaq 0x30(%rsp), %rbx
movq %rbx, %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x1fcc
testl %eax, %eax
jle 0x3ff0
movq %rbx, %rdi
callq 0x1060
leaq 0x2a07(%rip), %rdi      # 0x69fe
callq 0x1060
addl $0x3, %r15d
andl $-0x4, %r15d
addq %r15, %rbp
addl %r15d, %r12d
cmpl %r13d, %r12d
jl 0x3e40
jmp 0x401e
leaq 0x2736(%rip), %rdi      # 0x6752
jmp 0x3f9f
addq $0x438, %rsp            # imm = 0x438
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq