// File: ksherlock[P]wdc-utils/dumpobj.cpp
// Function: dump_lib(char const*, int)
void dump_lib(const char *name, int fd)
{
	Lib_head h;
	ssize_t ok;

	ok = read(fd, &h, sizeof(h));
	if (ok != sizeof(h))
		errx(EX_DATAERR, "%s is not an object file", name);


	le_to_host(h.l_magic);
	le_to_host(h.l_version);
	le_to_host(h.l_filtyp);
	le_to_host(h.l_modstart);
	le_to_host(h.l_numsyms);
	le_to_host(h.l_symsize);
	le_to_host(h.l_numfiles);

	assert(h.l_magic == MOD_MAGIC);
	assert(h.l_version == 1);
	assert(h.l_filtyp == 2);

	printf("; library %s\n\n", name);
	/*
	printf("; modstart      : $%04x\n", h.l_modstart);
	printf("; number symbols: $%04x\n", h.l_numsyms);
	printf("; number files  : $%04x\n", h.l_numfiles);
	printf("\n");
	*/

	std::vector<uint8_t> data;
	long count = h.l_modstart - sizeof(h);
	if (count < 0) errx(EX_DATAERR, "%s", name);
	data.resize(count);
	ok = read(fd, data.data(), count);
	if (ok != count) errx(EX_DATAERR, "%s truncated", name);


	// files
	printf("; files:\n");
	auto iter = data.begin();
	for (int i = 0; i < h.l_numfiles; ++i) {
		uint16_t file_number = read_16(iter);
		std::string s = read_pstring(iter);
		printf("; $%02x %s\n", file_number, s.c_str());
	}
	printf("\n");

	// symbols
	printf("; symbols:\n");
	auto name_iter = iter + h.l_numsyms * 8;
	for (int i = 0; i < h.l_numsyms; ++i) {
		uint16_t name_offset = read_16(iter);
		uint16_t file_number = read_16(iter);
		uint32_t offset = read_32(iter);
		auto tmp = name_iter + name_offset;
		std::string name = read_pstring(tmp);

		printf("; $%04x %s\n", i, name.c_str());
		//printf("name offset: %02x\n", name_offset);
		//printf("file_number  : $%02x\n", file_number);
		//printf("module offset: $%04x\n", offset); 
	}
	printf("\n");
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xf0, %rsp
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl -0xc(%rbp), %edi
leaq -0x24(%rbp), %rsi
movl $0x18, %edx
callq 0x33a0
movq %rax, -0x30(%rbp)
cmpq $0x18, -0x30(%rbp)
je 0x73b5
movq -0x8(%rbp), %rdx
movl $0x41, %edi
leaq 0x13d3e(%rip), %rsi     # 0x1b0ec
movb $0x0, %al
callq 0x34a0
leaq -0x24(%rbp), %rdi
callq 0x7e60
leaq -0x24(%rbp), %rdi
addq $0x4, %rdi
callq 0x7e80
leaq -0x24(%rbp), %rdi
addq $0x6, %rdi
callq 0x7ea0
leaq -0x24(%rbp), %rdi
addq $0x8, %rdi
callq 0x7e60
leaq -0x24(%rbp), %rdi
addq $0xc, %rdi
callq 0x7e60
leaq -0x24(%rbp), %rdi
addq $0x10, %rdi
callq 0x7e60
leaq -0x24(%rbp), %rdi
addq $0x14, %rdi
callq 0x7e60
cmpl $0x5a44525a, -0x24(%rbp) # imm = 0x5A44525A
jne 0x7417
jmp 0x7436
leaq 0x13eef(%rip), %rdi     # 0x1b30d
leaq 0x13cf7(%rip), %rsi     # 0x1b11c
movl $0x246, %edx            # imm = 0x246
leaq 0x13ef3(%rip), %rcx     # 0x1b324
callq 0x31a0
movzwl -0x20(%rbp), %eax
cmpl $0x1, %eax
jne 0x7441
jmp 0x7460
leaq 0x13efd(%rip), %rdi     # 0x1b345
leaq 0x13ccd(%rip), %rsi     # 0x1b11c
movl $0x247, %edx            # imm = 0x247
leaq 0x13ec9(%rip), %rcx     # 0x1b324
callq 0x31a0
movzbl -0x1e(%rbp), %eax
cmpl $0x2, %eax
jne 0x746b
jmp 0x748a
leaq 0x13ee4(%rip), %rdi     # 0x1b356
leaq 0x13ca3(%rip), %rsi     # 0x1b11c
movl $0x248, %edx            # imm = 0x248
leaq 0x13e9f(%rip), %rcx     # 0x1b324
callq 0x31a0
movq -0x8(%rbp), %rsi
leaq 0x13ed1(%rip), %rdi     # 0x1b366
movb $0x0, %al
callq 0x3060
leaq -0x48(%rbp), %rdi
callq 0x7f90
movl -0x1c(%rbp), %eax
subq $0x18, %rax
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jge 0x74e1
movq -0x8(%rbp), %rdx
leaq 0x147e4(%rip), %rsi     # 0x1bca6
xorl %eax, %eax
movl $0x41, %edi
callq 0x34a0
jmp 0x74d0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
jmp 0x777e
movq -0x50(%rbp), %rsi
leaq -0x48(%rbp), %rdi
callq 0x7fb0
jmp 0x74f0
movl -0xc(%rbp), %eax
movl %eax, -0xdc(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x8040
movl -0xdc(%rbp), %edi
movq %rax, %rsi
movq -0x50(%rbp), %rdx
callq 0x33a0
movq %rax, -0xd8(%rbp)
jmp 0x751d
movq -0xd8(%rbp), %rax
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rax
cmpq -0x50(%rbp), %rax
je 0x754b
movq -0x8(%rbp), %rdx
leaq 0x13e38(%rip), %rsi     # 0x1b375
xorl %eax, %eax
movl $0x41, %edi
callq 0x34a0
jmp 0x754b
leaq 0x13e30(%rip), %rdi     # 0x1b382
xorl %eax, %eax
callq 0x3060
jmp 0x755b
leaq -0x48(%rbp), %rdi
callq 0x8110
movq %rax, -0x68(%rbp)
movl $0x0, -0x6c(%rbp)
movl -0x6c(%rbp), %eax
cmpl -0x10(%rbp), %eax
jae 0x7610
leaq -0x68(%rbp), %rdi
callq 0x8510
movw %ax, -0xde(%rbp)
jmp 0x758d
movw -0xde(%rbp), %ax
movw %ax, -0x6e(%rbp)
leaq -0x90(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x8cd0
jmp 0x75aa
movzwl -0x6e(%rbp), %eax
movl %eax, -0xe4(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x30f0
movl -0xe4(%rbp), %esi
movq %rax, %rdx
leaq 0x13dbc(%rip), %rdi     # 0x1b38c
xorl %eax, %eax
callq 0x3060
jmp 0x75d9
leaq -0x90(%rbp), %rdi
callq 0x3160
movl -0x6c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x6c(%rbp)
jmp 0x756f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0x90(%rbp), %rdi
callq 0x3160
jmp 0x777e
leaq 0x13d5c(%rip), %rdi     # 0x1b373
xorl %eax, %eax
callq 0x3060
jmp 0x7620
leaq 0x13d71(%rip), %rdi     # 0x1b398
xorl %eax, %eax
callq 0x3060
jmp 0x7630
movl -0x18(%rbp), %eax
shll $0x3, %eax
movl %eax, %eax
movl %eax, %esi
leaq -0x68(%rbp), %rdi
callq 0x81e0
movq %rax, -0x98(%rbp)
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0x18(%rbp), %eax
jae 0x775c
leaq -0x68(%rbp), %rdi
callq 0x8510
movw %ax, -0xe6(%rbp)
jmp 0x7675
movw -0xe6(%rbp), %ax
movw %ax, -0x9e(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x8510
movw %ax, -0xe8(%rbp)
jmp 0x7695
movw -0xe8(%rbp), %ax
movw %ax, -0xa0(%rbp)
leaq -0x68(%rbp), %rdi
callq 0x82a0
movl %eax, -0xec(%rbp)
jmp 0x76b4
movl -0xec(%rbp), %eax
movl %eax, -0xa4(%rbp)
movzwl -0x9e(%rbp), %eax
movl %eax, %esi
leaq -0x98(%rbp), %rdi
callq 0x81e0
movq %rax, -0xb0(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x8cd0
jmp 0x76f1
movl -0x9c(%rbp), %eax
movl %eax, -0xf0(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x30f0
movl -0xf0(%rbp), %esi
movq %rax, %rdx
leaq 0x13c8b(%rip), %rdi     # 0x1b3a4
xorl %eax, %eax
callq 0x3060
jmp 0x7722
leaq -0xd0(%rbp), %rdi
callq 0x3160
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x7654
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x58(%rbp)
movl %eax, -0x5c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3160
jmp 0x777e
leaq 0x13c10(%rip), %rdi     # 0x1b373
xorl %eax, %eax
callq 0x3060
jmp 0x776c
leaq -0x48(%rbp), %rdi
callq 0x8c10
addq $0xf0, %rsp
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x8c10
movq -0x58(%rbp), %rdi
callq 0x3440