// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
pushq %rbx
subq $0x118, %rsp            # imm = 0x118
leaq 0x90(%rbp), %rax
movq %rax, -0x80(%rbp)
leaq 0x78(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq 0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq 0x48(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq 0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq $0x0, -0x20(%rbp)
movl $0xffffffff, -0x4c(%rbp) # imm = 0xFFFFFFFF
movl $0x0, -0x50(%rbp)
movq -0x78(%rbp), %rcx
movl -0x50(%rbp), %eax
cmpl (%rcx), %eax
jge 0x63f5
movq -0x78(%rbp), %rcx
movq -0x68(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rcx), %rcx
movslq -0x50(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %eax
cmpl -0x4c(%rbp), %eax
je 0x60a0
movq -0x78(%rbp), %rcx
movq -0x68(%rbp), %rax
movq (%rax), %rax
movq 0x10(%rcx), %rcx
movslq -0x50(%rbp), %rdx
movslq (%rcx,%rdx,4), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %eax
movl %eax, -0x4c(%rbp)
cmpq $0x0, -0x20(%rbp)
je 0x5fe2
movq -0x20(%rbp), %rdi
callq 0x1030
movq -0x68(%rbp), %rax
movq (%rax), %rsi
movslq -0x4c(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rsi
leaq -0x48(%rbp), %rdi
movl $0x28, %edx
callq 0x10e0
movq -0x68(%rbp), %rax
movq -0x10(%rbp), %rdi
movl -0x4c(%rbp), %esi
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x3600
movq -0x68(%rbp), %rcx
movq %rax, %rdx
movq -0x70(%rbp), %rax
movq %rdx, -0x20(%rbp)
movq 0x8(%rcx), %rdi
movl -0x48(%rbp), %ecx
addq %rcx, %rdi
movq (%rax), %rcx
movq %rcx, (%rsp)
movq 0x8(%rax), %rcx
movq %rcx, 0x8(%rsp)
movq 0x10(%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x3110
movl %eax, %ecx
movq -0x70(%rbp), %rax
movl %ecx, -0x54(%rbp)
movq -0x18(%rbp), %rdi
movq (%rax), %rax
movslq -0x54(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %eax
movl %eax, %esi
xorl %edx, %edx
callq 0x1100
movq -0x20(%rbp), %rdi
movl -0x34(%rbp), %eax
movl %eax, %esi
movq -0x18(%rbp), %rcx
movl $0x1, %edx
callq 0x1160
movq -0x78(%rbp), %rcx
movq -0x10(%rbp), %rdi
movl -0x38(%rbp), %eax
movq 0x8(%rcx), %rcx
movslq -0x50(%rbp), %rdx
movl (%rcx,%rdx,8), %ecx
addl %ecx, %eax
movl %eax, %esi
xorl %edx, %edx
movl %edx, -0x8c(%rbp)
callq 0x1100
movl -0x8c(%rbp), %edx
movq -0x78(%rbp), %rcx
movq -0x70(%rbp), %rax
movq -0x18(%rbp), %rdi
movq (%rax), %rax
movslq -0x54(%rbp), %rsi
leaq (%rsi,%rsi,4), %rsi
movl 0xc(%rax,%rsi,8), %eax
movq 0x8(%rcx), %rcx
movslq -0x50(%rbp), %rsi
movl (%rcx,%rsi,8), %ecx
addl %ecx, %eax
movl %eax, %esi
callq 0x1100
movq -0x78(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x50(%rbp), %rcx
movzbl 0x4(%rax,%rcx,8), %eax
movw %ax, -0x56(%rbp)
movzwl -0x56(%rbp), %eax
addl $-0x2, %eax
movl %eax, %ecx
movq %rcx, -0x88(%rbp)
subl $0x1b, %eax
ja 0x63d7
movq -0x88(%rbp), %rax
leaq 0x2254(%rip), %rcx      # 0x838c
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq -0x60(%rbp), %r9
movq -0x80(%rbp), %r10
movq -0x78(%rbp), %r11
movq -0x70(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x50(%rbp), %edx
movq (%rax), %rax
movslq -0x54(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
movq -0x20(%rbp), %r8
movq 0x10(%r11), %rbx
movq %rsp, %rax
movq %rax, -0x98(%rbp)
movq %rbx, 0x60(%rax)
movups (%r11), %xmm0
movups %xmm0, 0x50(%rax)
movq 0x10(%r10), %r11
movq %r11, 0x48(%rax)
movups (%r10), %xmm0
movups %xmm0, 0x38(%rax)
movl 0x30(%r9), %r10d
movl %r10d, 0x30(%rax)
movups (%r9), %xmm0
movups 0x10(%r9), %xmm1
movups 0x20(%r9), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x5aa0
jmp 0x63e5
movq -0x60(%rbp), %r9
movq -0x80(%rbp), %r10
movq -0x78(%rbp), %r11
movq -0x70(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x50(%rbp), %edx
movq (%rax), %rax
movslq -0x54(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
movq -0x20(%rbp), %r8
movq 0x10(%r11), %rbx
movq %rsp, %rax
movq %rax, -0xa0(%rbp)
movq %rbx, 0x60(%rax)
movups (%r11), %xmm0
movups %xmm0, 0x50(%rax)
movq 0x10(%r10), %r11
movq %r11, 0x48(%rax)
movups (%r10), %xmm0
movups %xmm0, 0x38(%rax)
movl 0x30(%r9), %r10d
movl %r10d, 0x30(%rax)
movups (%r9), %xmm0
movups 0x10(%r9), %xmm1
movups 0x20(%r9), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x5ba0
jmp 0x63e5
movq -0x60(%rbp), %r9
movq -0x80(%rbp), %r10
movq -0x78(%rbp), %r11
movq -0x70(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x50(%rbp), %edx
movq (%rax), %rax
movslq -0x54(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
movq -0x20(%rbp), %r8
movq 0x10(%r11), %rbx
movq %rsp, %rax
movq %rax, -0xa8(%rbp)
movq %rbx, 0x60(%rax)
movups (%r11), %xmm0
movups %xmm0, 0x50(%rax)
movq 0x10(%r10), %r11
movq %r11, 0x48(%rax)
movups (%r10), %xmm0
movups %xmm0, 0x38(%rax)
movl 0x30(%r9), %r10d
movl %r10d, 0x30(%rax)
movups (%r9), %xmm0
movups 0x10(%r9), %xmm1
movups 0x20(%r9), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x5c80
jmp 0x63e5
movq -0x60(%rbp), %r9
movq -0x80(%rbp), %r10
movq -0x78(%rbp), %r11
movq -0x70(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x50(%rbp), %edx
movq (%rax), %rax
movslq -0x54(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
movq -0x20(%rbp), %r8
movq 0x10(%r11), %rbx
movq %rsp, %rax
movq %rax, -0xb0(%rbp)
movq %rbx, 0x60(%rax)
movups (%r11), %xmm0
movups %xmm0, 0x50(%rax)
movq 0x10(%r10), %r11
movq %r11, 0x48(%rax)
movups (%r10), %xmm0
movups %xmm0, 0x38(%rax)
movl 0x30(%r9), %r10d
movl %r10d, 0x30(%rax)
movups (%r9), %xmm0
movups 0x10(%r9), %xmm1
movups 0x20(%r9), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x5e90
jmp 0x63e5
movq -0x60(%rbp), %r9
movq -0x80(%rbp), %r10
movq -0x78(%rbp), %r11
movq -0x70(%rbp), %rax
movq -0x10(%rbp), %rdi
movq -0x18(%rbp), %rsi
movl -0x50(%rbp), %edx
movq (%rax), %rax
movslq -0x54(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
movq -0x20(%rbp), %r8
movq 0x10(%r11), %rbx
movq %rsp, %rax
movq %rax, -0xb8(%rbp)
movq %rbx, 0x60(%rax)
movups (%r11), %xmm0
movups %xmm0, 0x50(%rax)
movq 0x10(%r10), %r11
movq %r11, 0x48(%rax)
movups (%r10), %xmm0
movups %xmm0, 0x38(%rax)
movl 0x30(%r9), %r10d
movl %r10d, 0x30(%rax)
movups (%r9), %xmm0
movups 0x10(%r9), %xmm1
movups 0x20(%r9), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x5d40
jmp 0x63e5
leaq 0x201e(%rip), %rdi      # 0x83fc
movb $0x0, %al
callq 0x1070
jmp 0x63e7
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x5f75
cmpq $0x0, -0x20(%rbp)
je 0x6405
movq -0x20(%rbp), %rdi
callq 0x1030
movl -0x50(%rbp), %esi
leaq 0x2013(%rip), %rdi      # 0x8422
movb $0x0, %al
callq 0x1070
addq $0x118, %rsp            # imm = 0x118
popq %rbx
popq %rbp
retq