// File: ksherlock[P]wdc-utils/zrdz_disassembler.cpp
// Function: zrdz_disassembler::location_name[abi:cxx11](unsigned int, unsigned int) const
std::string zrdz_disassembler::location_name(unsigned section, uint32_t offset) const {

	if (section >= _sections.size()) {
		warnx("Invalid section %d", section);
		return "$";
	}
	auto &e = _sections[section];
	if (!e.valid) {
		warnx("Invalid section %d", section);
		return "$";
	}

	// todo -- need to verify relative/absolute value

	auto iter = std::find_if(_symbols.begin(), _symbols.end(), [=](const symbol &s){
		if (s.type == equ_type) return false;
		if (s.type == S_UND) return false;
		return s.section == section && s.offset == offset;
	});

	if (iter != _symbols.end()) return iter->name;

	//fallback to section name + offset
	std::string tmp = e.name;
	if (tmp.empty()) tmp = "section" + std::to_string(section);
	if (offset) tmp += "+$" + to_x(offset, 4);
	return tmp;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movq %rdi, -0x110(%rbp)
movq %rdi, %rax
movq %rax, -0x108(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl %ecx, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movq %rdi, -0x100(%rbp)
movl -0x14(%rbp), %eax
movq %rax, -0xf8(%rbp)
addq $0x50, %rdi
callq 0x15af0
movq %rax, %rcx
movq -0xf8(%rbp), %rax
cmpq %rcx, %rax
jb 0x13ecc
movl -0x14(%rbp), %esi
leaq 0x7f33(%rip), %rdi      # 0x1bda4
xorl %eax, %eax
callq 0x32a0
leaq -0x19(%rbp), %rdi
movq %rdi, -0x118(%rbp)
callq 0x3460
movq -0x110(%rbp), %rdi
movq -0x118(%rbp), %rdx
leaq 0x7ebb(%rip), %rsi      # 0x1bd58
callq 0x8060
jmp 0x13ea4
leaq -0x19(%rbp), %rdi
callq 0x32b0
jmp 0x14159
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x19(%rbp), %rdi
callq 0x32b0
jmp 0x14169
movq -0x100(%rbp), %rdi
addq $0x50, %rdi
movl -0x14(%rbp), %eax
movl %eax, %esi
callq 0x16190
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
testb $0x1, 0x2d(%rax)
jne 0x13f54
movl -0x14(%rbp), %esi
leaq 0x7eab(%rip), %rdi      # 0x1bda4
xorl %eax, %eax
callq 0x32a0
leaq -0x39(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x3460
movq -0x110(%rbp), %rdi
movq -0x120(%rbp), %rdx
leaq 0x7e33(%rip), %rsi      # 0x1bd58
callq 0x8060
jmp 0x13f2c
leaq -0x39(%rbp), %rdi
callq 0x32b0
jmp 0x14159
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0x39(%rbp), %rdi
callq 0x32b0
jmp 0x14169
movq -0x100(%rbp), %rdi
addq $0x38, %rdi
callq 0x161b0
movq -0x100(%rbp), %rdi
movq %rax, -0x50(%rbp)
addq $0x38, %rdi
callq 0x161e0
movq %rax, -0x58(%rbp)
movl -0x14(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x18(%rbp), %eax
movl %eax, -0x5c(%rbp)
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x14180
movq -0x100(%rbp), %rdi
movq %rax, -0x48(%rbp)
addq $0x38, %rdi
callq 0x161e0
movq %rax, -0x68(%rbp)
leaq -0x48(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x16210
testb $0x1, %al
jne 0x13fc4
jmp 0x13fe1
leaq -0x48(%rbp), %rdi
callq 0x16250
movq -0x110(%rbp), %rdi
movq %rax, %rsi
callq 0x3100
jmp 0x14159
movq -0x110(%rbp), %rdi
movb $0x0, -0x69(%rbp)
movq -0x38(%rbp), %rsi
callq 0x3100
movq -0x110(%rbp), %rdi
callq 0x3430
testb $0x1, %al
jne 0x1400a
jmp 0x14092
movl -0x14(%rbp), %esi
leaq -0xb0(%rbp), %rdi
callq 0x8980
jmp 0x1401b
leaq 0x7d7a(%rip), %rsi      # 0x1bd9c
leaq -0x90(%rbp), %rdi
leaq -0xb0(%rbp), %rdx
callq 0x8b80
jmp 0x14037
movq -0x110(%rbp), %rdi
leaq -0x90(%rbp), %rsi
callq 0x3310
leaq -0x90(%rbp), %rdi
callq 0x3160
leaq -0xb0(%rbp), %rdi
callq 0x3160
jmp 0x14092
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x1414b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x3160
jmp 0x1414b
cmpl $0x0, -0x18(%rbp)
je 0x14133
movl -0x18(%rbp), %esi
leaq -0xf0(%rbp), %rdi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0xcb90
jmp 0x140b4
leaq 0x7d34(%rip), %rsi      # 0x1bdef
leaq -0xd0(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
callq 0x8b80
jmp 0x140d0
movq -0x110(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x3340
jmp 0x140e5
leaq -0xd0(%rbp), %rdi
callq 0x3160
leaq -0xf0(%rbp), %rdi
callq 0x3160
jmp 0x14133
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
jmp 0x14125
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x28(%rbp)
movl %eax, -0x2c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3160
leaq -0xf0(%rbp), %rdi
callq 0x3160
jmp 0x1414b
movb $0x1, -0x69(%rbp)
testb $0x1, -0x69(%rbp)
jne 0x14149
movq -0x110(%rbp), %rdi
callq 0x3160
jmp 0x14159
movq -0x110(%rbp), %rdi
callq 0x3160
jmp 0x14169
movq -0x108(%rbp), %rax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
movq -0x28(%rbp), %rdi
callq 0x3440
nopw %cs:(%rax,%rax)