// File: /workspace/github_cpp_makefile_star2_O2/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 0x28(%rsp), %rsi
pushq $0x18
popq %rdx
movl %ebp, %edi
callq 0x2290
cmpq $0x18, %rax
jne 0x4540
cmpl $0x5a44525a, 0x28(%rsp) # imm = 0x5A44525A
jne 0x4554
cmpw $0x1, 0x2c(%rsp)
jne 0x4573
cmpb $0x2, 0x2e(%rsp)
jne 0x4592
leaq 0x6fe1(%rip), %rdi      # 0xb392
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2040
andq $0x0, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movl 0x30(%rsp), %r14d
cmpq $0x18, %r14
jb 0x45b1
addq $-0x18, %r14
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x48a8
movq 0x10(%rsp), %rsi
movl %ebp, %edi
movq %r14, %rdx
callq 0x2290
cmpq %r14, %rax
jne 0x45ba
leaq 0x7032(%rip), %rdi      # 0xb43a
callq 0x22b0
movq 0x10(%rsp), %rax
leaq 0x8(%rsp), %rcx
movq %rax, (%rcx)
movl 0x3c(%rsp), %r12d
leaq 0x48(%rsp), %r14
leaq 0x6f83(%rip), %r15      # 0xb3ae
xorl %r13d, %r13d
cmpl %r12d, %r13d
jae 0x4481
movq 0x8(%rsp), %rax
movzbl (%rax), %ebx
leaq 0x1(%rax), %rcx
movq %rcx, 0x8(%rsp)
movzbl 0x1(%rax), %ebp
addq $0x2, %rax
movq %rax, 0x8(%rsp)
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
callq 0x4bf1
shll $0x8, %ebp
orl %ebx, %ebp
movq 0x48(%rsp), %rdx
movq %r15, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movq %r14, %rdi
callq 0x20d0
incl %r13d
jmp 0x442e
pushq $0xa
popq %rdi
callq 0x2280
leaq 0x6fb3(%rip), %rdi      # 0xb443
callq 0x22b0
movl 0x34(%rsp), %r13d
leal (,%r13,8), %ebx
leaq 0x8(%rsp), %rax
addq (%rax), %rbx
leaq 0x48(%rsp), %r14
leaq 0x6f04(%rip), %r12      # 0xb3ba
xorl %ebp, %ebp
cmpl %ebp, %r13d
je 0x451f
movq 0x8(%rsp), %rax
movzbl (%rax), %ecx
leaq 0x1(%rax), %rdx
movq %rdx, 0x8(%rsp)
movzbl 0x1(%rax), %r15d
shll $0x8, %r15d
orq %rcx, %r15
addq %rbx, %r15
addq $0x4, %rax
movq %rax, 0x8(%rsp)
leaq 0x8(%rsp), %rdi
callq 0x490f
movq %r15, 0x40(%rsp)
movq %r14, %rdi
leaq 0x40(%rsp), %rsi
callq 0x4bf1
movq 0x48(%rsp), %rdx
movq %r12, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x2040
movq %r14, %rdi
callq 0x20d0
incl %ebp
jmp 0x44b8
pushq $0xa
popq %rdi
callq 0x2280
leaq 0x10(%rsp), %rdi
callq 0x5362
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x6bd1(%rip), %rsi      # 0xb118
pushq $0x41
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2350
leaq 0x6dde(%rip), %rdi      # 0xb339
leaq 0x6be6(%rip), %rsi      # 0xb148
leaq 0x6de7(%rip), %rcx      # 0xb350
movl $0x246, %edx            # imm = 0x246
callq 0x2130
leaq 0x6df7(%rip), %rdi      # 0xb371
leaq 0x6bc7(%rip), %rsi      # 0xb148
leaq 0x6dc8(%rip), %rcx      # 0xb350
movl $0x247, %edx            # imm = 0x247
callq 0x2130
leaq 0x6de9(%rip), %rdi      # 0xb382
leaq 0x6ba8(%rip), %rsi      # 0xb148
leaq 0x6da9(%rip), %rcx      # 0xb350
movl $0x248, %edx            # imm = 0x248
callq 0x2130
leaq 0x7772(%rip), %rsi      # 0xbd2a
jmp 0x45c1
leaq 0x6de0(%rip), %rsi      # 0xb3a1
pushq $0x41
popq %rdi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x2350
jmp 0x45d2
jmp 0x45d2
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x5362
movq %rbx, %rdi
callq 0x2300