// File: /workspace/github_c_makefile_star2_O1/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 $0x88, %rsp
movl %edx, %ebp
movq %rsi, %r14
movq %rdi, %rbx
movslq 0x170(%rsp), %r15
movq %r15, %rdi
shlq $0x4, %rdi
callq 0x1100
movq %rax, (%rbx)
testq %rax, %rax
je 0x3e31
leaq 0x130(%rsp), %r12
movq 0x10(%r12), %rax
movq %rax, 0x10(%rsp)
movups (%r12), %xmm0
movups %xmm0, (%rsp)
leaq 0x1f5d(%rip), %rdi      # 0x5bc3
callq 0x2617
movq 0x10(%r12), %rcx
movq %rcx, 0x10(%rsp)
movups (%r12), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
movl %eax, %esi
callq 0x2cd8
movq %rax, 0x8(%rbx)
testl %r15d, %r15d
jle 0x3dd3
movl %ebp, 0x5c(%rsp)
leaq 0x160(%rsp), %rax
movq (%rax), %r12
movl $0xe, %ebp
xorl %r13d, %r13d
movl -0xe(%r12,%rbp), %ecx
movq (%rbx), %rax
movl %ecx, -0xe(%rax,%rbp)
movl -0x6(%r12,%rbp), %ecx
movl %ecx, -0x6(%rax,%rbp)
movb -0x2(%r12,%rbp), %cl
movb %cl, -0x2(%rax,%rbp)
movb -0x1(%r12,%rbp), %cl
movb %cl, -0x1(%rax,%rbp)
movzwl (%r12,%rbp), %ecx
testq %rcx, %rcx
je 0x3cec
cmpw $-0xf, %cx
jb 0x3cf7
movw %cx, (%rax,%rbp)
jmp 0x3db5
movw $0x0, (%rax,%rbp)
jmp 0x3d81
leaq 0x130(%rsp), %rdx
movq (%rdx), %rax
movq 0x8(%rdx), %rdi
leaq (%rcx,%rcx,4), %rcx
movq 0x20(%rax,%rcx,8), %rdx
movq %rdx, 0x80(%rsp)
movups (%rax,%rcx,8), %xmm0
movups 0x10(%rax,%rcx,8), %xmm1
movaps %xmm1, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x20(%rsp)
movaps 0x60(%rsp), %xmm0
movaps 0x70(%rsp), %xmm1
movups %xmm1, 0x10(%rsp)
movups %xmm0, (%rsp)
callq 0x20b1
movq %rax, %r14
leaq 0x148(%rsp), %rcx
movq 0x10(%rcx), %rax
movq %rax, 0x10(%rsp)
movups (%rcx), %xmm0
movups %xmm0, (%rsp)
movq %r14, %rdi
callq 0x2617
movq (%rbx), %rcx
movw %ax, (%rcx,%rbp)
movq %r14, %rdi
callq 0x1030
movq (%rbx), %rax
movzwl (%rax,%rbp), %ecx
cmpq $0xffff, %rcx           # imm = 0xFFFF
je 0x3daf
leaq 0x148(%rsp), %rdx
movq (%rdx), %rdx
leaq (%rcx,%rcx,4), %rcx
movl 0xc(%rdx,%rcx,8), %ecx
addl -0xa(%r12,%rbp), %ecx
movl %ecx, -0xa(%rax,%rbp)
jmp 0x3dbd
movw $0x0, (%rax,%rbp)
movl $0x0, -0xa(%rax,%rbp)
incq %r13
addq $0x10, %rbp
cmpq %r13, %r15
jne 0x3cac
movl 0x5c(%rsp), %ebp
jmp 0x3dd6
xorl %r13d, %r13d
movl %r13d, 0x10(%rbx)
testl %ebp, %ebp
jne 0x3e1c
leaq 0xf8(%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 0x2d5a
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e29(%rip), %rdi      # 0x5c61
callq 0x1060
movl $0x1, %edi
callq 0x1160