// File: ksherlock[P]wdc-utils/link.cpp
// Function: init()
void init() {

	sections.resize(5);

	sections[SECT_PAGE0].number = SECT_PAGE0;
	sections[SECT_PAGE0].flags = SEC_DATA | SEC_NONAME | SEC_DIRECT | SEC_REF_ONLY;
	sections[SECT_PAGE0].name = "page0";

	sections[SECT_CODE].number = SECT_CODE;
	sections[SECT_CODE].flags = SEC_NONAME;
	sections[SECT_CODE].name = "code"; 

	sections[SECT_KDATA].number = SECT_KDATA;
	sections[SECT_KDATA].flags = SEC_DATA | SEC_NONAME;
	sections[SECT_KDATA].name = "kdata"; 

	sections[SECT_DATA].number = SECT_DATA;
	sections[SECT_DATA].flags = SEC_DATA | SEC_NONAME;
	sections[SECT_DATA].name = "data"; 

	sections[SECT_UDATA].number = SECT_UDATA;
	sections[SECT_UDATA].flags = SEC_DATA | SEC_NONAME | SEC_REF_ONLY;
	sections[SECT_UDATA].name = "udata"; 

	/*
	 * For each section, [the linker] creates three symbols, 
	 * _ROM_BEG_secname, _BEG_secname and _END_secname, which
	 * correspond to the rom location and the execution beginning
	 * and end of the section. These will be used more in the next
	 * two sections of code.
	 */

	// n.b - only for pre-defined sections [?], skip the _ROM_BEG_* symbols...

	static std::string names[] = {
			"_BEG_PAGE0", "_END_PAGE0",
			"_BEG_CODE", "_END_CODE",
			"_BEG_KDATA", "_END_KDATA",
			"_BEG_DATA", "_END_DATA",
			"_BEG_UDATA", "_END_UDATA",
	};

	for (int i = 0; i < 5; ++i) {

		// begin is 0.
		symbol s;
		s.name = names[i * 2];
		s.section = i;
		s.type = S_REL;
		s.flags = SF_DEF | SF_GBL;
		symbol_map.emplace(s.name, i * 2);
		symbols.emplace_back(s);

		// end is undefined...
		s.name = names[i * 2 + 1];
		s.section = i * 2 + 1; // symbol number.
		s.type = S_UND;
		s.flags = 0;

		sections[i].end_symbol = s.section;

		symbol_map.emplace(s.name, s.section);
		symbols.emplace_back(s);

		// even though it's undefined, don't add it to the undefined list ...
		// don't need to search libraries for it!
	}

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x170, %rsp            # imm = 0x170
leaq 0x39d6e(%rip), %rdi     # 0x3f3e0
movl $0x5, %esi
callq 0xbe10
leaq 0x39d5d(%rip), %rdi     # 0x3f3e0
xorl %eax, %eax
movl %eax, %esi
callq 0xa1c0
movl $0x0, 0x2c(%rax)
leaq 0x39d46(%rip), %rdi     # 0x3f3e0
xorl %eax, %eax
movl %eax, %esi
callq 0xa1c0
movb $-0x18, 0x20(%rax)
leaq 0x39d32(%rip), %rdi     # 0x3f3e0
xorl %eax, %eax
movl %eax, %esi
callq 0xa1c0
movq %rax, %rdi
leaq 0x25d8f(%rip), %rsi     # 0x2b450
callq 0x3440
leaq 0x39d13(%rip), %rdi     # 0x3f3e0
movl $0x1, %esi
callq 0xa1c0
movl $0x1, 0x2c(%rax)
leaq 0x39cfb(%rip), %rdi     # 0x3f3e0
movl $0x1, %esi
callq 0xa1c0
movb $0x40, 0x20(%rax)
leaq 0x39ce6(%rip), %rdi     # 0x3f3e0
movl $0x1, %esi
callq 0xa1c0
movq %rax, %rdi
leaq 0x25d48(%rip), %rsi     # 0x2b456
callq 0x3440
leaq 0x39cc6(%rip), %rdi     # 0x3f3e0
movl $0x2, %esi
callq 0xa1c0
movl $0x2, 0x2c(%rax)
leaq 0x39cae(%rip), %rdi     # 0x3f3e0
movl $0x2, %esi
callq 0xa1c0
movb $-0x40, 0x20(%rax)
leaq 0x39c99(%rip), %rdi     # 0x3f3e0
movl $0x2, %esi
callq 0xa1c0
movq %rax, %rdi
leaq 0x25d00(%rip), %rsi     # 0x2b45b
callq 0x3440
leaq 0x39c79(%rip), %rdi     # 0x3f3e0
movl $0x3, %esi
callq 0xa1c0
movl $0x3, 0x2c(%rax)
leaq 0x39c61(%rip), %rdi     # 0x3f3e0
movl $0x3, %esi
callq 0xa1c0
movb $-0x40, 0x20(%rax)
leaq 0x39c4c(%rip), %rdi     # 0x3f3e0
movl $0x3, %esi
callq 0xa1c0
movq %rax, %rdi
leaq 0x25cb4(%rip), %rsi     # 0x2b45c
callq 0x3440
leaq 0x39c2c(%rip), %rdi     # 0x3f3e0
movl $0x4, %esi
callq 0xa1c0
movl $0x4, 0x2c(%rax)
leaq 0x39c14(%rip), %rdi     # 0x3f3e0
movl $0x4, %esi
callq 0xa1c0
movb $-0x38, 0x20(%rax)
leaq 0x39bff(%rip), %rdi     # 0x3f3e0
movl $0x4, %esi
callq 0xa1c0
movq %rax, %rdi
leaq 0x25c6c(%rip), %rsi     # 0x2b461
callq 0x3440
cmpb $0x0, 0x39dbf(%rip)     # 0x3f5c0
jne 0x5b0d
leaq 0x39db2(%rip), %rdi     # 0x3f5c0
callq 0x3580
cmpl $0x0, %eax
je 0x5b0d
movb $0x1, -0x26(%rbp)
leaq 0x39c59(%rip), %rax     # 0x3f480
movq %rax, -0x98(%rbp)
movq %rax, -0x8(%rbp)
leaq -0x9(%rbp), %rdi
movq %rdi, -0x90(%rbp)
callq 0x3550
movq -0x98(%rbp), %rdi
movq -0x90(%rbp), %rdx
leaq 0x25c10(%rip), %rsi     # 0x2b467
callq 0xbea0
jmp 0x585e
leaq 0x39c3b(%rip), %rax     # 0x3f4a0
movq %rax, -0xa8(%rbp)
movq %rax, -0x8(%rbp)
leaq -0x1d(%rbp), %rdi
movq %rdi, -0xa0(%rbp)
callq 0x3550
movq -0xa8(%rbp), %rdi
movq -0xa0(%rbp), %rdx
leaq 0x25bdd(%rip), %rsi     # 0x2b472
callq 0xbea0
jmp 0x589c
leaq 0x39c1d(%rip), %rax     # 0x3f4c0
movq %rax, -0xb8(%rbp)
movq %rax, -0x8(%rbp)
leaq -0x1e(%rbp), %rdi
movq %rdi, -0xb0(%rbp)
callq 0x3550
movq -0xb8(%rbp), %rdi
movq -0xb0(%rbp), %rdx
leaq 0x25baa(%rip), %rsi     # 0x2b47d
callq 0xbea0
jmp 0x58da
leaq 0x39bff(%rip), %rax     # 0x3f4e0
movq %rax, -0xc8(%rbp)
movq %rax, -0x8(%rbp)
leaq -0x1f(%rbp), %rdi
movq %rdi, -0xc0(%rbp)
callq 0x3550
movq -0xc8(%rbp), %rdi
movq -0xc0(%rbp), %rdx
leaq 0x25b76(%rip), %rsi     # 0x2b487
callq 0xbea0
jmp 0x5918
leaq 0x39be1(%rip), %rax     # 0x3f500
movq %rax, -0xd8(%rbp)
movq %rax, -0x8(%rbp)
leaq -0x20(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x3550
movq -0xd8(%rbp), %rdi
movq -0xd0(%rbp), %rdx
leaq 0x25b42(%rip), %rsi     # 0x2b491
callq 0xbea0
jmp 0x5956
leaq 0x39bc3(%rip), %rax     # 0x3f520
movq %rax, -0xe8(%rbp)
movq %rax, -0x8(%rbp)
leaq -0x21(%rbp), %rdi
movq %rdi, -0xe0(%rbp)
callq 0x3550
movq -0xe8(%rbp), %rdi
movq -0xe0(%rbp), %rdx
leaq 0x25b0f(%rip), %rsi     # 0x2b49c
callq 0xbea0
jmp 0x5994
leaq 0x39ba5(%rip), %rax     # 0x3f540
movq %rax, -0xf8(%rbp)
movq %rax, -0x8(%rbp)
leaq -0x22(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x3550
movq -0xf8(%rbp), %rdi
movq -0xf0(%rbp), %rdx
leaq 0x25adc(%rip), %rsi     # 0x2b4a7
callq 0xbea0
jmp 0x59d2
leaq 0x39b87(%rip), %rax     # 0x3f560
movq %rax, -0x108(%rbp)
movq %rax, -0x8(%rbp)
leaq -0x23(%rbp), %rdi
movq %rdi, -0x100(%rbp)
callq 0x3550
movq -0x108(%rbp), %rdi
movq -0x100(%rbp), %rdx
leaq 0x25aa8(%rip), %rsi     # 0x2b4b1
callq 0xbea0
jmp 0x5a10
leaq 0x39b69(%rip), %rax     # 0x3f580
movq %rax, -0x118(%rbp)
movq %rax, -0x8(%rbp)
leaq -0x24(%rbp), %rdi
movq %rdi, -0x110(%rbp)
callq 0x3550
movq -0x118(%rbp), %rdi
movq -0x110(%rbp), %rdx
leaq 0x25a74(%rip), %rsi     # 0x2b4bb
callq 0xbea0
jmp 0x5a4e
leaq 0x39b4b(%rip), %rax     # 0x3f5a0
movq %rax, -0x128(%rbp)
movq %rax, -0x8(%rbp)
leaq -0x25(%rbp), %rdi
movq %rdi, -0x120(%rbp)
callq 0x3550
movq -0x128(%rbp), %rdi
movq -0x120(%rbp), %rdx
leaq 0x25a41(%rip), %rsi     # 0x2b4c6
callq 0xbea0
jmp 0x5a8c
movb $0x0, -0x26(%rbp)
leaq -0x25(%rbp), %rdi
callq 0x3370
leaq -0x24(%rbp), %rdi
callq 0x3370
leaq -0x23(%rbp), %rdi
callq 0x3370
leaq -0x22(%rbp), %rdi
callq 0x3370
leaq -0x21(%rbp), %rdi
callq 0x3370
leaq -0x20(%rbp), %rdi
callq 0x3370
leaq -0x1f(%rbp), %rdi
callq 0x3370
leaq -0x1e(%rbp), %rdi
callq 0x3370
leaq -0x1d(%rbp), %rdi
callq 0x3370
leaq -0x9(%rbp), %rdi
callq 0x3370
leaq -0x23a1(%rip), %rdi     # 0x3750
xorl %eax, %eax
movl %eax, %esi
leaq 0x3981c(%rip), %rdx     # 0x3f318
callq 0x32f0
leaq 0x39ab8(%rip), %rdi     # 0x3f5c0
callq 0x3210
movl $0x0, -0x2c(%rbp)
cmpl $0x5, -0x2c(%rbp)
jge 0x5de0
leaq -0x60(%rbp), %rdi
movq %rdi, -0x130(%rbp)
callq 0x9bb0
movq -0x130(%rbp), %rdi
movl -0x2c(%rbp), %eax
addl %eax, %eax
cltq
shlq $0x5, %rax
leaq 0x39939(%rip), %rsi     # 0x3f480
addq %rax, %rsi
callq 0x3040
jmp 0x5b51
movl -0x2c(%rbp), %eax
movl %eax, -0x38(%rbp)
movb $0x2, -0x40(%rbp)
movb $0x3, -0x3f(%rbp)
movl -0x2c(%rbp), %eax
addl %eax, %eax
movl %eax, -0x64(%rbp)
leaq 0x3988a(%rip), %rdi     # 0x3f3f8
leaq -0x60(%rbp), %rsi
leaq -0x64(%rbp), %rdx
callq 0xbf50
movb %dl, -0x139(%rbp)
movq %rax, -0x138(%rbp)
jmp 0x5b8a
movb -0x139(%rbp), %al
movq -0x138(%rbp), %rcx
movq %rcx, -0x78(%rbp)
movb %al, -0x70(%rbp)
leaq 0x3988b(%rip), %rdi     # 0x3f430
leaq -0x60(%rbp), %rsi
callq 0xa5f0
jmp 0x5bb0
movl -0x2c(%rbp), %eax
addl %eax, %eax
cltq
shlq $0x5, %rax
leaq 0x398be(%rip), %rcx     # 0x3f480
leaq 0x20(%rax,%rcx), %rsi
leaq -0x60(%rbp), %rdi
callq 0x3040
jmp 0x5bd2
movl -0x2c(%rbp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %eax
leaq -0x38(%rbp), %rcx
movq %rcx, -0x158(%rbp)
movl %eax, -0x38(%rbp)
movb $0x0, -0x40(%rbp)
movb $0x0, -0x3f(%rbp)
movl -0x38(%rbp), %eax
movl %eax, -0x15c(%rbp)
movslq -0x2c(%rbp), %rsi
leaq 0x397db(%rip), %rdi     # 0x3f3e0
callq 0xa1c0
movl -0x15c(%rbp), %ecx
movq -0x158(%rbp), %rdx
movl %ecx, 0x60(%rax)
leaq 0x397d7(%rip), %rdi     # 0x3f3f8
leaq -0x60(%rbp), %rsi
callq 0xa430
movb %dl, -0x149(%rbp)
movq %rax, -0x148(%rbp)
jmp 0x5c39
movb -0x149(%rbp), %al
movq -0x148(%rbp), %rcx
movq %rcx, -0x88(%rbp)
movb %al, -0x80(%rbp)
leaq 0x397d9(%rip), %rdi     # 0x3f430
leaq -0x60(%rbp), %rsi
callq 0xa5f0
jmp 0x5c62
leaq -0x60(%rbp), %rdi
callq 0x9c70
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x5b14
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x5d60
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x5d57
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x5d4e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x5d45
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x5d3c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x5d33
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x5d2a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x5d21
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
jmp 0x5d18
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x25(%rbp), %rdi
callq 0x3370
leaq -0x24(%rbp), %rdi
callq 0x3370
leaq -0x23(%rbp), %rdi
callq 0x3370
leaq -0x22(%rbp), %rdi
callq 0x3370
leaq -0x21(%rbp), %rdi
callq 0x3370
leaq -0x20(%rbp), %rdi
callq 0x3370
leaq -0x1f(%rbp), %rdi
callq 0x3370
leaq -0x1e(%rbp), %rdi
callq 0x3370
leaq -0x1d(%rbp), %rdi
callq 0x3370
leaq -0x9(%rbp), %rdi
callq 0x3370
testb $0x1, -0x26(%rbp)
jne 0x5d71
jmp 0x5dbb
movq -0x8(%rbp), %rax
leaq 0x39704(%rip), %rcx     # 0x3f480
cmpq %rax, %rcx
movq %rax, -0x168(%rbp)
je 0x5db9
movq -0x168(%rbp), %rdi
addq $-0x20, %rdi
movq %rdi, -0x170(%rbp)
callq 0x31f0
movq -0x170(%rbp), %rax
leaq 0x396d3(%rip), %rcx     # 0x3f480
cmpq %rcx, %rax
movq %rax, -0x168(%rbp)
jne 0x5d88
jmp 0x5dbb
leaq 0x397fe(%rip), %rdi     # 0x3f5c0
callq 0x3200
jmp 0x5de9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x18(%rbp)
movl %eax, -0x1c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x9c70
jmp 0x5de9
addq $0x170, %rsp            # imm = 0x170
popq %rbp
retq
movq -0x18(%rbp), %rdi
callq 0x3540
nopw %cs:(%rax,%rax)