// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x48(%rsp), %rsi
movl $0x18, %edx
movl %ebp, %edi
callq 0x2250
cmpq $0x18, %rax
jne 0x63e1
cmpl $0x5a44525a, 0x48(%rsp) # imm = 0x5A44525A
jne 0x63f7
cmpw $0x1, 0x4c(%rsp)
jne 0x6416
cmpb $0x2, 0x4e(%rsp)
jne 0x6435
leaq 0x911a(%rip), %rdi      # 0xf336
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2040
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movq $0x0, 0x20(%rsp)
movl 0x50(%rsp), %r14d
cmpq $0x18, %r14
jb 0x6454
addq $-0x18, %r14
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x681c
movq 0x10(%rsp), %rsi
movl %ebp, %edi
movq %r14, %rdx
callq 0x2250
cmpq %r14, %rax
jne 0x645d
leaq 0x9168(%rip), %rdi      # 0xf3de
callq 0x2270
movq 0x10(%rsp), %rax
movq %rax, 0x8(%rsp)
movl 0x5c(%rsp), %r12d
testl %r12d, %r12d
je 0x6302
leaq 0x28(%rsp), %rbx
leaq 0x8(%rsp), %r14
leaq 0x90b2(%rip), %r15      # 0xf352
movq 0x8(%rsp), %rax
movzbl (%rax), %r13d
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rsp)
movzbl 0x1(%rax), %ebp
addq $0x2, %rax
movq %rax, 0x8(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6a0f
shll $0x8, %ebp
orl %r13d, %ebp
movq 0x28(%rsp), %rdx
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x62fd
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
decl %r12d
jne 0x62a0
movl $0xa, %edi
callq 0x2240
leaq 0x90d4(%rip), %rdi      # 0xf3e7
callq 0x2270
movl 0x54(%rsp), %r12d
testl %r12d, %r12d
je 0x63b1
leal (,%r12,8), %r13d
addq 0x8(%rsp), %r13
leaq 0x28(%rsp), %rbx
leaq 0x60(%rsp), %r14
leaq 0x901a(%rip), %r15      # 0xf35e
xorl %ebp, %ebp
movq 0x8(%rsp), %rax
movzbl (%rax), %ecx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%rsp)
movzbl 0x1(%rax), %edx
shll $0x8, %edx
orq %rcx, %rdx
addq %r13, %rdx
addq $0x8, %rax
movq %rax, 0x8(%rsp)
movq %rdx, 0x60(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6a0f
movq 0x28(%rsp), %rdx
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0x63aa
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x21c0
incl %ebp
cmpl %ebp, %r12d
jne 0x6346
movl $0xa, %edi
callq 0x2240
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x63d2
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8cd4(%rip), %rsi      # 0xf0bc
movl $0x41, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2310
leaq 0x8edf(%rip), %rdi      # 0xf2dd
leaq 0x8ce7(%rip), %rsi      # 0xf0ec
leaq 0x8ee8(%rip), %rcx      # 0xf2f4
movl $0x246, %edx            # imm = 0x246
callq 0x2120
leaq 0x8ef8(%rip), %rdi      # 0xf315
leaq 0x8cc8(%rip), %rsi      # 0xf0ec
leaq 0x8ec9(%rip), %rcx      # 0xf2f4
movl $0x247, %edx            # imm = 0x247
callq 0x2120
leaq 0x8eea(%rip), %rdi      # 0xf326
leaq 0x8ca9(%rip), %rsi      # 0xf0ec
leaq 0x8eaa(%rip), %rcx      # 0xf2f4
movl $0x248, %edx            # imm = 0x248
callq 0x2120
leaq 0x987f(%rip), %rsi      # 0xfcda
jmp 0x6464
leaq 0x8ee1(%rip), %rsi      # 0xf345
movl $0x41, %edi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2310
jmp 0x6477
jmp 0x6477
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x6491
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x21c0
movq %rbx, %rdi
callq 0x22c0