// File: /workspace/github_c_makefile_star2_O1/Astor-Bizard[P]ProjetS5_Groupe2/src/reimpl_R_ARM.c
// Function: reimplantation_R_ARM
void reimplantation_R_ARM(FILE *oldF, FILE *newF, Elf32_Ehdr oldElfHeader, SectionsHeadersList oldSH, SectionsHeadersList newSH, Str_Reloc tableReloc, ListeSymboles symbList)
{
	unsigned char *section = NULL;
	Elf32_Shdr sectionHeader;
	int sectionID = -1;
	int i, newID;
	uint16_t relType;

	for(i=0; i<tableReloc.nb_Rel; i++)
	{
		if(oldSH.headers[tableReloc.Sec_Rel[i]].sh_info != sectionID) 
		{
			sectionID = oldSH.headers[tableReloc.Sec_Rel[i]].sh_info;

			if(section!=NULL)
				free(section);
			sectionHeader = oldSH.headers[sectionID];
			section = recuperer_section_num(oldF, oldSH, sectionID);

			newID = index_Shdr(oldSH.names+sectionHeader.sh_name, newSH);
			fseek(newF, newSH.headers[newID].sh_addr, SEEK_SET);
			fwrite(section, sectionHeader.sh_size, 1, newF);
		}

		fseek(oldF, sectionHeader.sh_offset + tableReloc.Rel[i].r_offset, SEEK_SET);
		fseek(newF, newSH.headers[newID].sh_addr + tableReloc.Rel[i].r_offset, SEEK_SET);

		relType = 0xFF & tableReloc.Rel[i].r_info;
		switch(relType)
		{
			case R_ARM_ABS32:
				r_arm_abs32(oldF, newF, oldElfHeader, symbList, tableReloc, i, newSH.headers[newID].sh_addr, section);
				break;
			case R_ARM_ABS16:
				r_arm_abs16(oldF, newF, oldElfHeader, symbList, tableReloc, i, newSH.headers[newID].sh_addr, section);
				break;
			case R_ARM_ABS8:
				r_arm_abs8(oldF, newF, oldElfHeader, symbList, tableReloc, i, newSH.headers[newID].sh_addr, section);
				break;
			case R_ARM_CALL:
				r_arm_call(oldF, newF, oldElfHeader, symbList, tableReloc, i, newSH.headers[newID].sh_addr, section);
				break;
			case R_ARM_JUMP24:
				r_arm_jump24(oldF, newF, oldElfHeader, symbList, tableReloc, i, newSH.headers[newID].sh_addr, section);
				break;
			default:
				printf("ERREUR: Type de relocation inconnu !\n");
				break;
		}
	}
	if(section!=NULL)
		free(section);
	printf("%d reimplantations effectuées\n", i);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdi, 0x20(%rsp)
cmpl $0x0, 0xf8(%rsp)
jle 0x4597
movq %rsi, %rbp
leaq 0xf8(%rsp), %rdx
leaq 0xe0(%rsp), %r13
movl $0xffffffff, 0x38(%rsp) # imm = 0xFFFFFFFF
xorl %r14d, %r14d
movq $0x0, 0x40(%rsp)
movq %rsi, 0x50(%rsp)
movq %r14, 0x30(%rsp)
leaq 0xc8(%rsp), %rax
movq (%rax), %rax
movq 0x10(%rdx), %rcx
movslq (%rcx,%r14,4), %rcx
leaq (%rcx,%rcx,4), %rcx
movl 0x1c(%rax,%rcx,8), %r12d
cmpl 0x38(%rsp), %r12d
jne 0x4278
movl 0x48(%rsp), %r14d
movq %r13, %rbx
jmp 0x4339
movq 0x40(%rsp), %rdi
testq %rdi, %rdi
je 0x4287
callq 0x1030
movslq %r12d, %rax
leaq 0xc8(%rsp), %r15
movq (%r15), %rcx
leaq (%rax,%rax,4), %rax
movl (%rcx,%rax,8), %ebp
movl 0x10(%rcx,%rax,8), %edx
movl %edx, 0x4c(%rsp)
movl 0x14(%rcx,%rax,8), %eax
movq %rax, 0x38(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
movups (%r15), %xmm0
movups %xmm0, (%rsp)
movq 0x20(%rsp), %rdi
movl %r12d, %esi
callq 0x2980
movq %rax, %r13
addq 0x8(%r15), %rbp
leaq 0xe0(%rsp), %rbx
movq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
movups (%rbx), %xmm0
movups %xmm0, (%rsp)
movq %rbp, %rdi
callq 0x2617
movl %eax, %r14d
movq (%rbx), %rax
movslq %r14d, %rcx
leaq (%rcx,%rcx,4), %rcx
movl 0xc(%rax,%rcx,8), %esi
movq 0x50(%rsp), %rbp
movq %rbp, %rdi
xorl %edx, %edx
callq 0x1110
movl $0x1, %edx
movq %r13, 0x40(%rsp)
movq %r13, %rdi
movq 0x38(%rsp), %rsi
movq %rbp, %rcx
callq 0x1170
movl %r12d, 0x38(%rsp)
leaq 0xf8(%rsp), %rdx
movq 0x8(%rdx), %r15
movq 0x30(%rsp), %r12
movl (%r15,%r12,8), %esi
addl 0x4c(%rsp), %esi
movq 0x20(%rsp), %rdi
xorl %edx, %edx
callq 0x1110
movq (%rbx), %rax
movl %r14d, 0x48(%rsp)
movslq %r14d, %rcx
movq %r12, %r14
leaq (%rcx,%rcx,4), %r12
movl (%r15,%r14,8), %esi
addl 0xc(%rax,%r12,8), %esi
movq %rbp, %rdi
xorl %edx, %edx
callq 0x1110
movzbl 0x4(%r15,%r14,8), %eax
cmpl $0x7, %eax
jle 0x4443
cmpl $0x8, %eax
je 0x44b4
cmpl $0x1c, %eax
je 0x43a1
cmpl $0x1d, %eax
jne 0x4565
leaq 0xe0(%rsp), %rax
movq (%rax), %rax
movq %rbp, %rbx
movl 0xc(%rax,%r12,8), %ebp
leaq 0x110(%rsp), %rax
movq (%rax), %r14
leaq 0x90(%rsp), %rax
movzbl 0x5(%rax), %ecx
movl %ecx, 0x2c(%rsp)
movq 0x30(%rsp), %rax
movzbl 0x5(%r15,%rax,8), %r12d
movsbl %cl, %edi
movq 0x20(%rsp), %rsi
movl $0x4, %edx
callq 0x18e8
leal (,%rax,4), %edx
shll $0x4, %r12d
addl 0x4(%r14,%r12), %edx
movq 0x30(%rsp), %r14
addl (%r15,%r14,8), %ebp
subl %ebp, %edx
leaq 0xe0(%rsp), %r13
movq %rbx, %rbp
movl %edx, %ecx
andl $-0x4, %ecx
shll $0x16, %ecx
movl %edx, %esi
shll $0x6, %esi
andl $0xff0000, %esi         # imm = 0xFF0000
orl %ecx, %esi
shrl $0xa, %edx
andl $0xff00, %edx           # imm = 0xFF00
orl %esi, %edx
shrl $0x18, %eax
orl %eax, %edx
movq %rbx, %rdi
movl 0x2c(%rsp), %esi
jmp 0x455e
cmpl $0x2, %eax
je 0x450c
cmpl $0x5, %eax
jne 0x4565
leaq 0x110(%rsp), %rax
movq %rbp, %rbx
movq (%rax), %rbp
leaq 0x90(%rsp), %rax
movzbl 0x5(%rax), %r12d
movzbl 0x5(%r15,%r14,8), %r13d
movsbl %r12b, %edi
movq 0x20(%rsp), %rsi
movl $0x2, %edx
callq 0x18e8
shll $0x4, %r13d
addl 0x4(%rbp,%r13), %eax
leaq 0xe0(%rsp), %r13
movq %rbx, %rbp
rolw $0x8, %ax
movzwl %ax, %edx
movq %rbx, %rdi
movl %r12d, %esi
callq 0x3bc6
jmp 0x4579
leaq 0x110(%rsp), %rax
movq %rbp, %rbx
movq (%rax), %rbp
leaq 0x90(%rsp), %rax
movzbl 0x5(%rax), %r12d
movzbl 0x5(%r15,%r14,8), %r13d
movsbl %r12b, %edi
movq 0x20(%rsp), %rsi
movl $0x1, %edx
callq 0x18e8
shll $0x4, %r13d
addl 0x4(%rbp,%r13), %eax
leaq 0xe0(%rsp), %r13
movq %rbx, %rbp
movzbl %al, %edx
movq %rbx, %rdi
movl %r12d, %esi
callq 0x3bef
jmp 0x4579
leaq 0x110(%rsp), %rax
movq %rbp, %rbx
movq (%rax), %rbp
leaq 0x90(%rsp), %rax
movzbl 0x5(%rax), %r12d
movzbl 0x5(%r15,%r14,8), %r13d
movsbl %r12b, %edi
movq 0x20(%rsp), %rsi
movl $0x4, %edx
callq 0x18e8
shll $0x4, %r13d
addl 0x4(%rbp,%r13), %eax
leaq 0xe0(%rsp), %r13
movq %rbx, %rbp
bswapl %eax
movq %rbx, %rdi
movl %r12d, %esi
movl %eax, %edx
callq 0x3ba0
jmp 0x4579
leaq 0x1dd0(%rip), %rdi      # 0x633c
callq 0x1060
leaq 0xe0(%rsp), %r13
incq %r14
leaq 0xf8(%rsp), %rdx
movslq (%rdx), %rax
cmpq %rax, %r14
jl 0x4243
movq 0x40(%rsp), %rdi
jmp 0x459c
xorl %r14d, %r14d
xorl %edi, %edi
testq %rdi, %rdi
je 0x45a6
callq 0x1030
leaq 0x1d6f(%rip), %rdi      # 0x631c
movl %r14d, %esi
xorl %eax, %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1090
nopl (%rax)