// File: /workspace/github_c_makefile_star2_O2/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 $0xa8, %rsp
movq %rsi, 0x98(%rsp)
movq %rdi, %r14
leaq 0x148(%rsp), %r13
leaq 0x118(%rsp), %r15
pushq $-0x1
popq %rax
movl %eax, 0x74(%rsp)
movl (%r13), %eax
movl %eax, 0x78(%rsp)
xorl %ebx, %ebx
movq $0x0, 0x88(%rsp)
movq %rdi, 0x80(%rsp)
movslq 0x78(%rsp), %rax
cmpq %rax, %rbx
jge 0x4273
movq (%r15), %rax
movq 0x10(%r13), %rcx
movq %rbx, 0xa0(%rsp)
movslq (%rcx,%rbx,4), %rcx
imulq $0x28, %rcx, %rcx
movl 0x1c(%rax,%rcx), %r12d
cmpl 0x74(%rsp), %r12d
jne 0x3f2b
movslq 0x94(%rsp), %rbp
movq %r14, %rbx
leaq 0x130(%rsp), %r14
movq 0x98(%rsp), %r15
jmp 0x3ff8
movslq %r12d, %r13
movq 0x88(%rsp), %rdi
callq 0x1030
movq (%r15), %rax
imulq $0x28, %r13, %rcx
movl (%rax,%rcx), %ebp
movl 0x10(%rax,%rcx), %edx
movl %edx, 0x70(%rsp)
movl 0x14(%rax,%rcx), %eax
movq %rax, 0x78(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
movups (%r15), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movl %r12d, %esi
callq 0x2890
movq %rax, %r13
addq 0x8(%r15), %rbp
movq %r14, %rbx
leaq 0x130(%rsp), %r14
movq 0x10(%r14), %rax
movq %rax, 0x10(%rsp)
movups (%r14), %xmm0
movups %xmm0, (%rsp)
movq %rbp, %rdi
callq 0x253f
movq (%r14), %rdx
movl %eax, 0x94(%rsp)
movslq %eax, %rbp
imulq $0x28, %rbp, %rcx
movl 0xc(%rdx,%rcx), %esi
movq 0x98(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x1110
movq %r13, 0x88(%rsp)
movq %r13, %rdi
movq 0x78(%rsp), %rsi
leaq 0x148(%rsp), %r13
pushq $0x1
popq %rdx
movq %r15, %rcx
callq 0x1170
movl 0x70(%rsp), %edx
movl (%r13), %eax
movl %eax, 0x78(%rsp)
movl %r12d, 0x74(%rsp)
movq 0x8(%r13), %r13
movq 0xa0(%rsp), %rax
movl (%r13,%rax,8), %esi
movl %edx, 0x70(%rsp)
addl %edx, %esi
movq %rbx, %rdi
movq %rax, %rbx
xorl %edx, %edx
callq 0x1110
movq (%r14), %rax
imulq $0x28, %rbp, %r12
movl (%r13,%rbx,8), %esi
addl 0xc(%rax,%r12), %esi
movq %r15, %rdi
xorl %edx, %edx
callq 0x1110
movl 0x4(%r13,%rbx,8), %ebp
cmpb $0x1d, %bpl
je 0x41de
movzbl %bpl, %eax
cmpl $0x5, %eax
je 0x40bb
cmpl $0x8, %eax
movq 0x80(%rsp), %r14
je 0x4180
cmpl $0x1c, %eax
je 0x410f
cmpl $0x2, %eax
jne 0x41c5
leaq 0x160(%rsp), %rax
movq (%rax), %r13
leaq 0xe0(%rsp), %rax
movzbl 0x5(%rax), %r12d
movsbl %r12b, %edi
movq %r14, %rsi
pushq $0x4
popq %rdx
callq 0x188c
shrl $0x4, %ebp
andl $0xff0, %ebp            # imm = 0xFF0
addl 0x4(%r13,%rbp), %eax
bswapl %eax
movq %r15, %rdi
movl %r12d, %esi
movl %eax, %edx
callq 0x394c
jmp 0x41d1
leaq 0x160(%rsp), %rax
movq (%rax), %r13
leaq 0xe0(%rsp), %rax
movzbl 0x5(%rax), %r12d
movsbl %r12b, %edi
movq 0x80(%rsp), %r14
movq %r14, %rsi
pushq $0x2
popq %rdx
callq 0x188c
shrl $0x4, %ebp
andl $0xff0, %ebp            # imm = 0xFF0
addl 0x4(%r13,%rbp), %eax
rolw $0x8, %ax
movzwl %ax, %edx
movq %r15, %rdi
movl %r12d, %esi
callq 0x396e
jmp 0x41d1
leaq 0x130(%rsp), %rax
movq (%rax), %rax
movl 0xc(%rax,%r12), %ecx
leaq 0x148(%rsp), %r13
movq 0x10(%r13), %rax
movq %rax, 0x60(%rsp)
movups (%r13), %xmm0
movups %xmm0, 0x50(%rsp)
leaq 0x160(%rsp), %rdx
movq 0x10(%rdx), %rax
movq %rax, 0x48(%rsp)
movups (%rdx), %xmm0
movups %xmm0, 0x38(%rsp)
leaq 0xe0(%rsp), %rdx
movl 0x30(%rdx), %eax
movl %eax, 0x30(%rsp)
movups (%rdx), %xmm0
movups 0x10(%rdx), %xmm1
movups 0x20(%rdx), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
jmp 0x4252
leaq 0x160(%rsp), %rax
movq (%rax), %r13
leaq 0xe0(%rsp), %rax
movzbl 0x5(%rax), %r12d
movsbl %r12b, %edi
movq %r14, %rsi
pushq $0x1
popq %rdx
callq 0x188c
shrl $0x4, %ebp
andl $0xff0, %ebp            # imm = 0xFF0
addl 0x4(%r13,%rbp), %eax
movzbl %al, %edx
movq %r15, %rdi
movl %r12d, %esi
callq 0x3993
jmp 0x41d1
leaq 0x2120(%rip), %rdi      # 0x62ec
callq 0x1060
leaq 0x148(%rsp), %r13
jmp 0x425f
leaq 0x130(%rsp), %rax
movq (%rax), %rax
movl 0xc(%rax,%r12), %ecx
leaq 0x148(%rsp), %r13
movq 0x10(%r13), %rax
movq %rax, 0x60(%rsp)
movups (%r13), %xmm0
movups %xmm0, 0x50(%rsp)
leaq 0x160(%rsp), %rdx
movq 0x10(%rdx), %rax
movq %rax, 0x48(%rsp)
movups (%rdx), %xmm0
movups %xmm0, 0x38(%rsp)
leaq 0xe0(%rsp), %rdx
movl 0x30(%rdx), %eax
movl %eax, 0x30(%rsp)
movups (%rdx), %xmm0
movups 0x10(%rdx), %xmm1
movups 0x20(%rdx), %xmm2
movups %xmm2, 0x20(%rsp)
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
movq 0x80(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
movl %ebx, %edx
callq 0x3df5
leaq 0x118(%rsp), %r15
incq %rbx
movl 0x70(%rsp), %edx
jmp 0x3eda
movq 0x88(%rsp), %rdi
callq 0x1030
leaq 0x2045(%rip), %rdi      # 0x62cc
movl %ebx, %esi
xorl %eax, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1090
nopl (%rax)