// File: /workspace/github_c_makefile_star2_O3/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 $0x48, %rsp
movq %rsi, 0x28(%rsp)
movq %rdi, 0x20(%rsp)
cmpl $0x0, 0xe8(%rsp)
jle 0x45cd
leaq 0xe8(%rsp), %rdx
leaq 0xb8(%rsp), %r15
movl $0xffffffff, 0x30(%rsp) # imm = 0xFFFFFFFF
xorl %ebx, %ebx
movq $0x0, 0x38(%rsp)
movq (%r15), %rax
movq 0x10(%rdx), %rcx
movslq (%rcx,%rbx,4), %rcx
leaq (%rcx,%rcx,4), %rcx
movl 0x1c(%rax,%rcx,8), %r12d
cmpl 0x30(%rsp), %r12d
jne 0x422c
movslq 0x44(%rsp), %r13
leaq 0xd0(%rsp), %r15
jmp 0x42f4
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x423e
callq 0x1030
movq (%r15), %rax
movslq %r12d, %rcx
leaq (%rcx,%rcx,4), %rcx
movl (%rax,%rcx,8), %r13d
movl 0x10(%rax,%rcx,8), %edx
movl %edx, 0x1c(%rsp)
movl 0x14(%rax,%rcx,8), %eax
movq %rax, 0x30(%rsp)
movq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
movups (%r15), %xmm0
movups %xmm0, (%rsp)
movq 0x20(%rsp), %rdi
movl %r12d, %esi
callq 0x2989
movq %rax, %r14
addq 0x8(%r15), %r13
leaq 0xd0(%rsp), %r15
movq 0x10(%r15), %rax
movq %rax, 0x10(%rsp)
movups (%r15), %xmm0
movups %xmm0, (%rsp)
movq %r13, %rdi
callq 0x2610
movq (%r15), %rdx
movl %eax, 0x44(%rsp)
movslq %eax, %r13
leaq (,%r13,4), %rcx
addq %r13, %rcx
movl 0xc(%rdx,%rcx,8), %esi
movq 0x28(%rsp), %rbp
movq %rbp, %rdi
xorl %edx, %edx
callq 0x1110
movl $0x1, %edx
movq %r14, 0x38(%rsp)
movq %r14, %rdi
movl 0x1c(%rsp), %r14d
movq 0x30(%rsp), %rsi
movq %rbp, %rcx
callq 0x1170
movl %r12d, 0x30(%rsp)
leaq 0xe8(%rsp), %rdx
movq 0x8(%rdx), %rbp
movl (%rbp,%rbx,8), %esi
addl %r14d, %esi
movq 0x20(%rsp), %rdi
xorl %edx, %edx
callq 0x1110
movq (%r15), %rax
leaq (,%r13,4), %r12
addq %r13, %r12
movl (%rbp,%rbx,8), %esi
addl 0xc(%rax,%r12,8), %esi
movq 0x28(%rsp), %rdi
xorl %edx, %edx
callq 0x1110
movl 0x4(%rbp,%rbx,8), %r13d
movzbl %r13b, %eax
cmpl $0x7, %eax
jle 0x4401
cmpl $0x8, %eax
leaq 0xd0(%rsp), %rcx
leaq 0xb8(%rsp), %r15
je 0x446d
cmpl $0x1c, %eax
je 0x44bb
cmpl $0x1d, %eax
jne 0x4599
movq (%rcx), %rax
movl %r14d, 0x1c(%rsp)
movl 0xc(%rax,%r12,8), %r14d
leaq 0x100(%rsp), %rax
movq (%rax), %r15
leaq 0x80(%rsp), %rax
movzbl 0x5(%rax), %r12d
movsbl %r12b, %edi
movq 0x20(%rsp), %rsi
movl $0x4, %edx
callq 0x18e8
leal (,%rax,4), %edx
shrl $0x4, %r13d
andl $0xff0, %r13d           # imm = 0xFF0
addl 0x4(%r15,%r13), %edx
leaq 0xb8(%rsp), %r15
addl (%rbp,%rbx,8), %r14d
subl %r14d, %edx
movl 0x1c(%rsp), %r14d
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 0x28(%rsp), %rdi
movl %r12d, %esi
jmp 0x4592
cmpl $0x2, %eax
leaq 0xb8(%rsp), %r15
je 0x454d
cmpl $0x5, %eax
jne 0x4599
leaq 0x100(%rsp), %rax
movq (%rax), %r12
leaq 0x80(%rsp), %rax
movzbl 0x5(%rax), %ebp
movsbl %bpl, %edi
movq 0x20(%rsp), %rsi
movl $0x2, %edx
callq 0x18e8
shrl $0x4, %r13d
andl $0xff0, %r13d           # imm = 0xFF0
addl 0x4(%r12,%r13), %eax
rolw $0x8, %ax
movzwl %ax, %edx
movq 0x28(%rsp), %rdi
movl %ebp, %esi
callq 0x3b72
jmp 0x45a5
leaq 0x100(%rsp), %rax
movq (%rax), %r12
leaq 0x80(%rsp), %rax
movzbl 0x5(%rax), %ebp
movsbl %bpl, %edi
movq 0x20(%rsp), %rsi
movl $0x1, %edx
callq 0x18e8
shrl $0x4, %r13d
andl $0xff0, %r13d           # imm = 0xFF0
addl 0x4(%r12,%r13), %eax
movzbl %al, %edx
movq 0x28(%rsp), %rdi
movl %ebp, %esi
callq 0x3b9b
jmp 0x45a5
movq (%rcx), %rax
movl 0xc(%rax,%r12,8), %r15d
leaq 0x100(%rsp), %rax
movl %r14d, %r12d
movq (%rax), %r14
leaq 0x80(%rsp), %rax
movzbl 0x5(%rax), %eax
movl %eax, 0x1c(%rsp)
movsbl %al, %edi
movq 0x20(%rsp), %rsi
movl $0x4, %edx
callq 0x18e8
leal (,%rax,4), %edx
shrl $0x4, %r13d
andl $0xff0, %r13d           # imm = 0xFF0
addl 0x4(%r14,%r13), %edx
movl %r12d, %r14d
addl (%rbp,%rbx,8), %r15d
subl %r15d, %edx
leaq 0xb8(%rsp), %r15
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 0x28(%rsp), %rdi
movl 0x1c(%rsp), %esi
jmp 0x4592
leaq 0x100(%rsp), %rax
movq (%rax), %r12
leaq 0x80(%rsp), %rax
movzbl 0x5(%rax), %ebp
movsbl %bpl, %edi
movq 0x20(%rsp), %rsi
movl $0x4, %edx
callq 0x18e8
shrl $0x4, %r13d
andl $0xff0, %r13d           # imm = 0xFF0
addl 0x4(%r12,%r13), %eax
bswapl %eax
movq 0x28(%rsp), %rdi
movl %ebp, %esi
movl %eax, %edx
callq 0x3b4c
jmp 0x45a5
leaq 0x1d5c(%rip), %rdi      # 0x62fc
callq 0x1060
incq %rbx
leaq 0xe8(%rsp), %rdx
movslq (%rdx), %rax
cmpq %rax, %rbx
jl 0x41ff
movq 0x38(%rsp), %rdi
testq %rdi, %rdi
je 0x45cf
callq 0x1030
jmp 0x45cf
xorl %ebx, %ebx
leaq 0x1d06(%rip), %rdi      # 0x62dc
movl %ebx, %esi
xorl %eax, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x1090
nopl (%rax)