// File: /workspace/github_c_makefile_star2_O2/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, 0x5c(%rsp)
movq %rsi, %r15
movq %rdi, %r12
movslq 0x180(%rsp), %rbp
movq %rbp, %rdi
shlq $0x4, %rdi
callq 0x1100
movq %rax, (%r12)
testq %rax, %rax
je 0x3bda
movq %rax, %r14
leaq 0x158(%rsp), %r13
leaq 0x140(%rsp), %rbx
movq 0x10(%rbx), %rax
movq %rax, 0x10(%rsp)
movups (%rbx), %xmm0
movups %xmm0, (%rsp)
leaq 0x2162(%rip), %rdi      # 0x5b73
callq 0x253f
movq 0x10(%rbx), %rcx
movq %rcx, 0x10(%rsp)
movups (%rbx), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
movl %eax, %esi
callq 0x2c14
movq %r12, 0x68(%rsp)
movq %rax, 0x8(%r12)
leaq 0x170(%rsp), %rax
movq (%rax), %rbx
xorl %r12d, %r12d
testl %ebp, %ebp
cmovlel %r12d, %ebp
movq %rbp, 0x60(%rsp)
shlq $0x4, %rbp
cmpq %r12, %rbp
je 0x3b73
movl (%rbx,%r12), %eax
movl %eax, (%r14,%r12)
movl 0x8(%rbx,%r12), %eax
movl %eax, 0x8(%r14,%r12)
movb 0xc(%rbx,%r12), %al
movb %al, 0xc(%r14,%r12)
movb 0xd(%rbx,%r12), %al
movb %al, 0xd(%r14,%r12)
movzwl 0xe(%rbx,%r12), %eax
testq %rax, %rax
je 0x3aa2
cmpw $-0xf, %ax
jb 0x3aae
movw %ax, 0xe(%r14,%r12)
jmp 0x3b64
andw $0x0, 0xe(%r14,%r12)
jmp 0x3b37
leaq 0x140(%rsp), %rdx
movq (%rdx), %rcx
movq 0x8(%rdx), %rdi
imulq $0x28, %rax, %rax
movq 0x20(%rcx,%rax), %rdx
movq %rdx, 0x90(%rsp)
movups (%rcx,%rax), %xmm0
movups 0x10(%rcx,%rax), %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 0x1fe2
movq %rax, %r15
movq 0x10(%r13), %rax
movq %rax, 0x10(%rsp)
movups (%r13), %xmm0
movups %xmm0, (%rsp)
movq %r15, %rdi
callq 0x253f
movw %ax, 0xe(%r14,%r12)
movq %r15, %rdi
callq 0x1030
movzwl 0xe(%r14,%r12), %eax
cmpq $0xffff, %rax           # imm = 0xFFFF
je 0x3b5d
movq (%r13), %rcx
imulq $0x28, %rax, %rax
movl 0xc(%rcx,%rax), %eax
addl 0x4(%rbx,%r12), %eax
movl %eax, 0x4(%r14,%r12)
jmp 0x3b6a
andw $0x0, 0xe(%r14,%r12)
andl $0x0, 0x4(%r14,%r12)
addq $0x10, %r12
jmp 0x3a57
movq 0x68(%rsp), %rbx
movq 0x60(%rsp), %rax
movl %eax, 0x10(%rbx)
cmpl $0x0, 0x5c(%rsp)
jne 0x3bc5
leaq 0x108(%rsp), %rcx
movl 0x30(%rcx), %eax
movl %eax, 0x48(%rsp)
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %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 0x2c8d
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x2030(%rip), %rdi      # 0x5c11
callq 0x1060
pushq $0x1
popq %rdi
callq 0x1160