// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
leaq 0x11ac2(%rip), %rdi     # 0x15310
pushq $0x5
popq %rsi
callq 0x5f90
movq 0x11ab3(%rip), %rdi     # 0x15310
andl $0x0, 0x2c(%rdi)
movb $-0x18, 0x20(%rdi)
leaq 0x9ae4(%rip), %rsi      # 0xd350
callq 0x2450
movq 0x11a98(%rip), %rax     # 0x15310
leaq 0x68(%rax), %rdi
movl $0x1, 0x94(%rax)
movb $0x40, 0x88(%rax)
leaq 0x9ac2(%rip), %rsi      # 0xd356
callq 0x2450
movq 0x11a70(%rip), %rax     # 0x15310
leaq 0xd0(%rax), %rdi
movl $0x2, 0xfc(%rax)
movb $-0x40, %bl
movb %bl, 0xf0(%rax)
leaq 0x9a9b(%rip), %rsi      # 0xd35b
callq 0x2450
movq 0x11a44(%rip), %rax     # 0x15310
leaq 0x138(%rax), %rdi
movl $0x3, 0x164(%rax)
movb %bl, 0x158(%rax)
leaq 0x9a72(%rip), %rsi      # 0xd35c
callq 0x2450
movq 0x11a1a(%rip), %rax     # 0x15310
leaq 0x1a0(%rax), %rdi
movl $0x4, 0x1cc(%rax)
movb $-0x38, 0x1c0(%rax)
leaq 0x9a4c(%rip), %rsi      # 0xd361
callq 0x2450
movb 0x11bd0(%rip), %al      # 0x154f0
testb %al, %al
je 0x3a30
pushq $0x30
popq %rbp
pushq $0x1
popq %rbx
xorl %r12d, %r12d
leaq 0x18(%rsp), %r14
leaq 0x11a23(%rip), %r13     # 0x15360
xorl %r15d, %r15d
cmpq $0x140, %r15            # imm = 0x140
je 0x3a21
leaq 0x28(%rsp), %rax
movq %rax, 0x18(%rsp)
andq $0x0, 0x20(%rsp)
movb $0x0, 0x28(%rsp)
andw $0x0, 0x38(%rsp)
movabsq $-0x100000000, %rax     # imm = 0xFFFFFFFF00000000
movq %rax, 0x3c(%rsp)
leaq 0x11a32(%rip), %rax     # 0x153b0
leaq (%rax,%r15), %rsi
movq %r14, %rdi
callq 0x2160
movl %r12d, 0x40(%rsp)
movw $0x302, 0x38(%rsp)      # imm = 0x302
leal -0x1(%rbx), %eax
movl %eax, 0x14(%rsp)
leaq 0x11984(%rip), %rdi     # 0x15328
movq %r14, %rsi
leaq 0x14(%rsp), %rdx
callq 0x89ba
movq %r13, %rdi
movq %r14, %rsi
callq 0x5484
leaq 0x119ed(%rip), %rax     # 0x153b0
leaq (%r15,%rax), %rsi
addq $0x20, %rsi
movq %r14, %rdi
callq 0x2160
movl %ebx, 0x40(%rsp)
andw $0x0, 0x38(%rsp)
movq 0x1192c(%rip), %rax     # 0x15310
movl %ebx, (%rax,%rbp,2)
leaq 0x1193a(%rip), %rdi     # 0x15328
movq %r14, %rsi
leaq 0x40(%rsp), %rdx
callq 0x745e
movq %r13, %rdi
movq %r14, %rsi
callq 0x5484
movq %r14, %rdi
callq 0x2130
addq $0x40, %r15
addq $0x34, %rbp
addl $0x2, %ebx
incl %r12d
jmp 0x3940
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x11ab9(%rip), %rdi     # 0x154f0
callq 0x23f0
testl %eax, %eax
je 0x3928
leaq 0x11965(%rip), %rbx     # 0x153b0
leaq 0x9915(%rip), %rsi      # 0xd367
leaq 0x18(%rsp), %rdx
movq %rbx, %rdi
callq 0x5fc6
leaq 0x1196a(%rip), %r14     # 0x153d0
leaq 0x9905(%rip), %rsi      # 0xd372
leaq 0x14(%rsp), %rdx
movq %r14, %rdi
callq 0x5fc6
leaq 0x1196f(%rip), %r14     # 0x153f0
leaq 0x98f5(%rip), %rsi      # 0xd37d
leaq 0x13(%rsp), %rdx
movq %r14, %rdi
callq 0x5fc6
leaq 0x11974(%rip), %r14     # 0x15410
leaq 0x98e4(%rip), %rsi      # 0xd387
leaq 0x12(%rsp), %rdx
movq %r14, %rdi
callq 0x5fc6
leaq 0x11979(%rip), %r14     # 0x15430
leaq 0x98d3(%rip), %rsi      # 0xd391
leaq 0x11(%rsp), %rdx
movq %r14, %rdi
callq 0x5fc6
leaq 0x1197e(%rip), %r14     # 0x15450
leaq 0x98c3(%rip), %rsi      # 0xd39c
leaq 0x10(%rsp), %rdx
movq %r14, %rdi
callq 0x5fc6
leaq 0x11983(%rip), %r14     # 0x15470
leaq 0x98b3(%rip), %rsi      # 0xd3a7
leaq 0xf(%rsp), %rdx
movq %r14, %rdi
callq 0x5fc6
leaq 0x11988(%rip), %r14     # 0x15490
leaq 0x98a2(%rip), %rsi      # 0xd3b1
leaq 0xe(%rsp), %rdx
movq %r14, %rdi
callq 0x5fc6
leaq 0x1198d(%rip), %r14     # 0x154b0
leaq 0x9891(%rip), %rsi      # 0xd3bb
leaq 0xd(%rsp), %rdx
movq %r14, %rdi
callq 0x5fc6
leaq 0x11992(%rip), %r14     # 0x154d0
leaq 0x9881(%rip), %rsi      # 0xd3c6
leaq 0xc(%rsp), %rdx
movq %r14, %rdi
callq 0x5fc6
leaq -0x16b9(%rip), %rdi     # 0x24a0
leaq 0x116f0(%rip), %rdx     # 0x15250
xorl %esi, %esi
callq 0x2230
leaq 0x11982(%rip), %rdi     # 0x154f0
callq 0x21b0
jmp 0x3928
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
jmp 0x3b88
movq %rax, %r15
addq $-0x20, %r14
movq %r14, %rdi
callq 0x2130
cmpq %rbx, %r14
jne 0x3b8b
leaq 0x1194d(%rip), %rdi     # 0x154f0
callq 0x21a0
jmp 0x3bbe
movq %rax, %r15
jmp 0x3b9c
jmp 0x3bb1
movq %rax, %r15
leaq 0x18(%rsp), %rdi
callq 0x2130
movq %r15, %rdi
callq 0x23c0