// File: /workspace/github_c_makefile_star2_O3/Astor-Bizard[P]ProjetS5_Groupe2/src/correctionSymboles.c
// Function: applySymbolsCorrections
ListeSymboles applySymbolsCorrections(FILE* oldFile, Elf32_Ehdr oldElfHeader, Elf32_Ehdr newElfHeader, SectionsHeadersList oldSHList, SectionsHeadersList newSHList, ListeSymboles oldSymbolsTable, int silent) {
	int i, j;
	char* originalName;
	ListeSymboles newSymbolsTable;
	
	// Allocation de la nouvelle table des symboles, de la même longueur de l'ancienne
	newSymbolsTable.symboles = (Elf32_Sym*) malloc(sizeof(Elf32_Sym)*oldSymbolsTable.nbSymboles);
	if(newSymbolsTable.symboles == NULL)
	{
		printf("Erreur d'allocation\n");
		exit(1);
	}

	// Copie et correction de oldSymbolsTable vers newSymbolsTable
	i = index_Shdr(".symtab", oldSHList);
	newSymbolsTable.names = fetchSymbolNames(oldFile, oldSHList, i);
	for(j=0; j<oldSymbolsTable.nbSymboles; j++)
	{
		newSymbolsTable.symboles[j].st_name = oldSymbolsTable.symboles[j].st_name;
		newSymbolsTable.symboles[j].st_size = oldSymbolsTable.symboles[j].st_size;
		newSymbolsTable.symboles[j].st_info = oldSymbolsTable.symboles[j].st_info;
		newSymbolsTable.symboles[j].st_other = oldSymbolsTable.symboles[j].st_other;

		// Recherche du nouvel id de la section du symbole courant
		if (oldSymbolsTable.symboles[j].st_shndx==0) {
			newSymbolsTable.symboles[j].st_shndx = 0;
		}
		else if(oldSymbolsTable.symboles[j].st_shndx>65520){
			newSymbolsTable.symboles[j].st_shndx = oldSymbolsTable.symboles[j].st_shndx;
			newSymbolsTable.symboles[j].st_value = 0;
			continue;
		}
		else {
			originalName = getSectionNameBis(oldSHList.names, oldSHList.headers[oldSymbolsTable.symboles[j].st_shndx]); 
			newSymbolsTable.symboles[j].st_shndx = index_Shdr(originalName, newSHList);
			free(originalName);
		}
		if(newSymbolsTable.symboles[j].st_shndx==65535) {
			newSymbolsTable.symboles[j].st_shndx = 0;
			newSymbolsTable.symboles[j].st_value = 0;
			continue;
		}
		// Nouvelle valeur du symbole = Ancienne valeur + Adresse de la section parente
		newSymbolsTable.symboles[j].st_value = oldSymbolsTable.symboles[j].st_value + newSHList.headers[newSymbolsTable.symboles[j].st_shndx].sh_addr;
	}
	newSymbolsTable.nbSymboles = j;

	// Affichage si necessaire
	if (!silent)
		afficherTableSymboles(newSymbolsTable, newElfHeader);

	return newSymbolsTable;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl %edx, %ebp
movq %rsi, 0x58(%rsp)
movq %rdi, %rbx
movslq 0x180(%rsp), %r13
movq %r13, %r14
shlq $0x4, %r14
movq %r14, %rdi
callq 0x1100
movq %rax, (%rbx)
testq %rax, %rax
je 0x3df0
movq %rax, %r15
leaq 0x140(%rsp), %r12
movq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
movups (%r12), %xmm0
movups %xmm0, (%rsp)
leaq 0x1f69(%rip), %rdi      # 0x5b83
callq 0x2610
movq 0x10(%r12), %rcx
movq %rcx, 0x10(%rsp)
movups (%r12), %xmm0
movups %xmm0, (%rsp)
movq 0x58(%rsp), %rdi
movl %eax, %esi
callq 0x2cee
movq %rax, 0x8(%rbx)
testl %r13d, %r13d
jle 0x3d94
movq %r13, 0x68(%rsp)
movl %ebp, 0x64(%rsp)
movq %rbx, 0x58(%rsp)
leaq 0x170(%rsp), %rax
leaq 0x158(%rsp), %r13
movq (%rax), %rbx
xorl %ebp, %ebp
movl (%rbx,%rbp), %eax
movl %eax, (%r15,%rbp)
movl 0x8(%rbx,%rbp), %eax
movl %eax, 0x8(%r15,%rbp)
movb 0xc(%rbx,%rbp), %al
movb %al, 0xc(%r15,%rbp)
movb 0xd(%rbx,%rbp), %al
movb %al, 0xd(%r15,%rbp)
movzwl 0xe(%rbx,%rbp), %eax
testq %rax, %rax
je 0x3cab
cmpw $-0xf, %ax
jb 0x3cb8
movw %ax, 0xe(%r15,%rbp)
jmp 0x3d6e
movw $0x0, 0xe(%r15,%rbp)
jmp 0x3d41
leaq 0x140(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdi
leaq (%rax,%rax,4), %rax
movq 0x20(%rcx,%rax,8), %rdx
movq %rdx, 0x90(%rsp)
movups (%rcx,%rax,8), %xmm0
movups 0x10(%rcx,%rax,8), %xmm1
movaps %xmm1, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x20(%rsp)
movaps 0x70(%rsp), %xmm0
movaps 0x80(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x2091
movq %rax, %r12
movq 0x10(%r13), %rax
movq %rax, 0x10(%rsp)
movups (%r13), %xmm0
movups %xmm0, (%rsp)
movq %r12, %rdi
callq 0x2610
movw %ax, 0xe(%r15,%rbp)
movq %r12, %rdi
callq 0x1030
movzwl 0xe(%r15,%rbp), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
je 0x3d66
movq (%r13), %rcx
leaq (%rax,%rax,4), %rax
movl 0xc(%rcx,%rax,8), %eax
addl 0x4(%rbx,%rbp), %eax
movl %eax, 0x4(%r15,%rbp)
jmp 0x3d77
movw $0x0, 0xe(%r15,%rbp)
movl $0x0, 0x4(%r15,%rbp)
addq $0x10, %rbp
cmpq %rbp, %r14
jne 0x3c6e
movq 0x58(%rsp), %rbx
movl 0x64(%rsp), %ebp
movq 0x68(%rsp), %rax
jmp 0x3d96
xorl %eax, %eax
movl %eax, 0x10(%rbx)
testl %ebp, %ebp
jne 0x3ddb
leaq 0x108(%rsp), %rax
movl 0x30(%rax), %ecx
movl %ecx, 0x48(%rsp)
movups (%rax), %xmm0
movups 0x10(%rax), %xmm1
movups 0x20(%rax), %xmm2
movups %xmm2, 0x38(%rsp)
movups %xmm1, 0x28(%rsp)
movups %xmm0, 0x18(%rsp)
movq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
movups (%rbx), %xmm0
movups %xmm0, (%rsp)
callq 0x2d6e
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e2a(%rip), %rdi      # 0x5c21
callq 0x1060
movl $0x1, %edi
callq 0x1160