// File: /workspace/github_c_makefile_star2_O0/Astor-Bizard[P]ProjetS5_Groupe2/src/renum_section.c
// Function: renumerote_section
SectionsHeadersList renumerote_section(FILE *f_read, 
						FILE *f_write,
						Elf32_Ehdr elfHeaders, 
						SectionsHeadersList section_headers, 
						Elf32_Ehdr *elfHeaders_mod,
						Table_Donnees tab_donnees
						)
{
	int i,k; 
	int nbRec, nbSecVide;
	int nb_Sec_A_Traiter = 0;
	SectionsHeadersList section_headers_mod;
	
	//Modification du Headers
	nb_Sec_A_Traiter = nbSecRel(elfHeaders,section_headers.headers);
	
	*elfHeaders_mod=elfHeaders;

	elfHeaders_mod->e_type = ET_EXEC;

	elfHeaders_mod->e_shnum = elfHeaders.e_shnum - nb_Sec_A_Traiter;

	for(i=0;i<tab_donnees.nbSecRel;i++){
		if(tab_donnees.table_Num_Addr[i]<elfHeaders.e_shstrndx){
			elfHeaders_mod->e_shstrndx--;
		}
	}

	
	section_headers_mod.headers = (Elf32_Shdr*) malloc(sizeof(Elf32_Shdr)*elfHeaders.e_shnum);
	if (section_headers_mod.headers==NULL) {
		printf("\nErreur lors de l'allocation initiale de section_headers_mod.headers.\n");
		exit(1);
	}



	//Modification table des sections

	//Copie (sauf REL)
	nbRec=0;
	for(i=0;i<elfHeaders.e_shnum;i++)
	{
		if(section_headers.headers[i].sh_type != SHT_REL)
		{
			section_headers_mod.headers[i-nbRec] = section_headers.headers[i];
		}
		else nbRec++;
	}

	//Application des modifications de relocation
	nbRec=0;
	for(i=0;i<elfHeaders.e_shnum;i++)
	{
		if(section_headers.headers[i].sh_type == SHT_REL)
		{
			nbRec++;
			k= 0;
			while(k < tab_donnees.nbSecRel && tab_donnees.table_Num_Addr[k] !=  section_headers.headers[i].sh_info)
			{
				k++;
			}
			if(k == tab_donnees.nbSecRel)
			{
				printf("Entete de section %i non trouvée , erreur d'argument\nIl faut fournir une adresse pour cette table de la forme : <section>=0x<addr>\n",section_headers.headers[i].sh_info);
				displaySectionsHeaders(elfHeaders, section_headers);
				exit(1);
			}
			else
			{
				if(tab_donnees.table_Addr[k]%section_headers_mod.headers[i-nbRec].sh_addralign != 0)
				{
					printf("La valeurs de l'adresse : %06x ne correspond pas à l'alignement de la section\nIl faut l'aligner à mod[%i]\n",tab_donnees.table_Addr[k],section_headers_mod.headers[i-nbRec].sh_addralign);
					exit(1);
				}
				section_headers_mod.headers[i-nbRec].sh_addr = tab_donnees.table_Addr[k];
			}
		}
	}
	// Decalage des offset et suppresion des sections vide 
	nbSecVide = 0;
	for(i=1;i<elfHeaders_mod->e_shnum;i++)
	{
		if(section_headers_mod.headers[i].sh_addr == 0)
		{
			if(section_headers_mod.headers[i].sh_size == 0)
			{
				nbSecVide ++;
				if(i<elfHeaders_mod->e_shstrndx)
				{
					elfHeaders_mod->e_shstrndx--;
				}
			}
			else
			{
				section_headers_mod.headers[i-nbSecVide] = section_headers_mod.headers[i];
				section_headers_mod.headers[i-nbSecVide].sh_offset = section_headers_mod.headers[i-nbSecVide-1].sh_offset + section_headers_mod.headers[i-nbSecVide-1].sh_size; 
			}
		}
		else
		{
			section_headers_mod.headers[i].sh_offset = section_headers_mod.headers[i].sh_addr; 
			if( section_headers_mod.headers[i].sh_offset <  section_headers_mod.headers[i-1].sh_offset + section_headers_mod.headers[i-1].sh_size )
			{
				printf("Erreur l'adresse (%06x) de la section numero %i est trop petite\nil faut quel soit superieur à %06x + %06x\n",section_headers_mod.headers[i].sh_offset,i,section_headers_mod.headers[i-1].sh_offset,section_headers_mod.headers[i-1].sh_size);
				exit(1);
			}
		}
	}
	// on remet à jour la taille de la table des sections en supprimant les section vide et les 2 derniers sections (table de symboles et nom qui sont inutile)
	
	elfHeaders_mod->e_shnum -= (nbSecVide + 2);
	
	elfHeaders_mod->e_shoff =  section_headers_mod.headers[elfHeaders_mod->e_shnum].sh_offset;


	// copie et maj des anciens valeurs de la struture section_headers 
	section_headers_mod.size = elfHeaders_mod->e_shnum;
	
	section_headers_mod.names = malloc(sizeof(char)*section_headers.headers[elfHeaders.e_shstrndx].sh_size);
	if (section_headers_mod.names==NULL) {
		printf("\nErreur lors de l'allocation initiale de section_headers_mod.headers.\n");
		exit(1);
	}
	for(i=0;i<section_headers.headers[elfHeaders.e_shstrndx].sh_size;i++)
	{
		section_headers_mod.names[i] = section_headers.names[i];
	}
	

	fwrite(elfHeaders_mod,sizeof(Elf32_Ehdr),1,f_write);

	return section_headers_mod;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xd0, %rsp
movq %rcx, -0x70(%rbp)
movq %rdx, %rax
movq -0x70(%rbp), %rdx
movq %rax, -0x68(%rbp)
movq %rsi, %rax
movq -0x68(%rbp), %rsi
movq %rax, -0x60(%rbp)
movq %rdi, %rax
movq -0x60(%rbp), %rdi
movq %rax, -0x58(%rbp)
movq %rax, -0x50(%rbp)
leaq 0x60(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq 0x48(%rbp), %rax
movq %rax, -0x40(%rbp)
leaq 0x10(%rbp), %rcx
movq %rcx, -0x38(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x0, -0x2c(%rbp)
movq (%rax), %rdi
movl 0x30(%rcx), %edx
movq %rsp, %rax
movl %edx, 0x30(%rax)
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x5200
movq -0x38(%rbp), %rsi
movl %eax, -0x2c(%rbp)
movq -0x18(%rbp), %rdi
movl $0x34, %edx
callq 0x10e0
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movw $0x2, 0x10(%rcx)
movzwl 0x30(%rax), %eax
subl -0x2c(%rbp), %eax
movw %ax, %cx
movq -0x18(%rbp), %rax
movw %cx, 0x30(%rax)
movl $0x0, -0x1c(%rbp)
movq -0x48(%rbp), %rcx
movl -0x1c(%rbp), %eax
cmpl (%rcx), %eax
jge 0x4cb5
movq -0x38(%rbp), %rcx
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x1c(%rbp), %rdx
movl (%rax,%rdx,4), %eax
movzwl 0x32(%rcx), %ecx
cmpl %ecx, %eax
jge 0x4ca8
movq -0x18(%rbp), %rax
movw 0x32(%rax), %cx
addw $-0x1, %cx
movw %cx, 0x32(%rax)
jmp 0x4caa
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x4c72
movq -0x38(%rbp), %rax
movzwl 0x30(%rax), %eax
imulq $0x28, %rax, %rdi
callq 0x10f0
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, (%rax)
cmpq $0x0, (%rax)
jne 0x4cee
leaq 0x3463(%rip), %rdi      # 0x8140
movb $0x0, %al
callq 0x1070
movl $0x1, %edi
callq 0x1150
movl $0x0, -0x24(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x38(%rbp), %rcx
movl -0x1c(%rbp), %eax
movzwl 0x30(%rcx), %ecx
cmpl %ecx, %eax
jge 0x4d6e
movq -0x40(%rbp), %rax
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x9, 0x4(%rax)
je 0x4d58
movq -0x40(%rbp), %rax
movq -0x58(%rbp), %rcx
movq (%rcx), %rdi
movl -0x1c(%rbp), %ecx
subl -0x24(%rbp), %ecx
movslq %ecx, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rdi
movq (%rax), %rsi
movslq -0x1c(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rsi
movl $0x28, %edx
callq 0x10e0
jmp 0x4d61
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x4d63
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x4cfc
movl $0x0, -0x24(%rbp)
movl $0x0, -0x1c(%rbp)
movq -0x38(%rbp), %rcx
movl -0x1c(%rbp), %eax
movzwl 0x30(%rcx), %ecx
cmpl %ecx, %eax
jge 0x4f28
movq -0x40(%rbp), %rax
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x9, 0x4(%rax)
jne 0x4f18
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl $0x0, -0x20(%rbp)
movq -0x48(%rbp), %rdx
movl -0x20(%rbp), %ecx
xorl %eax, %eax
cmpl (%rdx), %ecx
movb %al, -0x71(%rbp)
jge 0x4df5
movq -0x40(%rbp), %rcx
movq -0x48(%rbp), %rax
movq 0x10(%rax), %rax
movslq -0x20(%rbp), %rdx
movl (%rax,%rdx,4), %eax
movq (%rcx), %rcx
movslq -0x1c(%rbp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x1c(%rcx), %eax
setne %al
movb %al, -0x71(%rbp)
movb -0x71(%rbp), %al
testb $0x1, %al
jne 0x4dfe
jmp 0x4e09
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x4dbb
movq -0x48(%rbp), %rcx
movl -0x20(%rbp), %eax
cmpl (%rcx), %eax
jne 0x4e7c
movq -0x40(%rbp), %rax
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x1c(%rax), %esi
leaq 0x3357(%rip), %rdi      # 0x8187
movb $0x0, %al
callq 0x1070
movq -0x40(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq 0x10(%rdx), %rsi
movq %rsp, %rax
movq %rsi, 0x48(%rax)
movups (%rdx), %xmm0
movups %xmm0, 0x38(%rax)
movl 0x30(%rcx), %edx
movl %edx, 0x30(%rax)
movups (%rcx), %xmm0
movups 0x10(%rcx), %xmm1
movups 0x20(%rcx), %xmm2
movups %xmm2, 0x20(%rax)
movups %xmm1, 0x10(%rax)
movups %xmm0, (%rax)
callq 0x29a0
movl $0x1, %edi
callq 0x1150
movq -0x58(%rbp), %rcx
movq -0x48(%rbp), %rax
movq 0x8(%rax), %rax
movslq -0x20(%rbp), %rdx
movl (%rax,%rdx,4), %eax
movq (%rcx), %rcx
movl -0x1c(%rbp), %edx
subl -0x24(%rbp), %edx
movslq %edx, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
xorl %edx, %edx
divl 0x20(%rcx)
cmpl $0x0, %edx
je 0x4eed
movq -0x58(%rbp), %rax
movq -0x48(%rbp), %rcx
movq 0x8(%rcx), %rcx
movslq -0x20(%rbp), %rdx
movl (%rcx,%rdx,4), %esi
movq (%rax), %rax
movl -0x1c(%rbp), %ecx
subl -0x24(%rbp), %ecx
movslq %ecx, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x20(%rax), %edx
leaq 0x3330(%rip), %rdi      # 0x820c
movb $0x0, %al
callq 0x1070
movl $0x1, %edi
callq 0x1150
movq -0x58(%rbp), %rax
movq -0x48(%rbp), %rcx
movq 0x8(%rcx), %rcx
movslq -0x20(%rbp), %rdx
movl (%rcx,%rdx,4), %ecx
movq (%rax), %rax
movl -0x1c(%rbp), %edx
subl -0x24(%rbp), %edx
movslq %edx, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0xc(%rax)
jmp 0x4f18
jmp 0x4f1a
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x4d7c
movl $0x0, -0x28(%rbp)
movl $0x1, -0x1c(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
movzwl 0x30(%rcx), %ecx
cmpl %ecx, %eax
jge 0x50fd
movq -0x58(%rbp), %rax
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0xc(%rax)
jne 0x5027
movq -0x58(%rbp), %rax
movq (%rax), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
cmpl $0x0, 0x14(%rax)
jne 0x4fa7
movl -0x28(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x28(%rbp)
movl -0x1c(%rbp), %eax
movq -0x18(%rbp), %rcx
movzwl 0x32(%rcx), %ecx
cmpl %ecx, %eax
jge 0x4fa5
movq -0x18(%rbp), %rax
movw 0x32(%rax), %cx
addw $-0x1, %cx
movw %cx, 0x32(%rax)
jmp 0x5022
movq -0x58(%rbp), %rax
movq (%rax), %rdi
movl -0x1c(%rbp), %ecx
subl -0x28(%rbp), %ecx
movslq %ecx, %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rdi
movq (%rax), %rsi
movslq -0x1c(%rbp), %rax
imulq $0x28, %rax, %rax
addq %rax, %rsi
movl $0x28, %edx
callq 0x10e0
movq -0x58(%rbp), %rax
movq (%rax), %rcx
movl -0x1c(%rbp), %edx
subl -0x28(%rbp), %edx
subl $0x1, %edx
movslq %edx, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movl 0x10(%rcx), %ecx
movq (%rax), %rdx
movl -0x1c(%rbp), %esi
subl -0x28(%rbp), %esi
subl $0x1, %esi
movslq %esi, %rsi
imulq $0x28, %rsi, %rsi
addq %rsi, %rdx
addl 0x14(%rdx), %ecx
movq (%rax), %rax
movl -0x1c(%rbp), %edx
subl -0x28(%rbp), %edx
movslq %edx, %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x10(%rax)
jmp 0x50ed
movq -0x58(%rbp), %rdx
movq (%rdx), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0xc(%rax), %ecx
movq (%rdx), %rax
movslq -0x1c(%rbp), %rsi
imulq $0x28, %rsi, %rsi
addq %rsi, %rax
movl %ecx, 0x10(%rax)
movq (%rdx), %rax
movslq -0x1c(%rbp), %rcx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x10(%rax), %eax
movq (%rdx), %rcx
movl -0x1c(%rbp), %esi
subl $0x1, %esi
movslq %esi, %rsi
imulq $0x28, %rsi, %rsi
addq %rsi, %rcx
movl 0x10(%rcx), %ecx
movq (%rdx), %rdx
movl -0x1c(%rbp), %esi
subl $0x1, %esi
movslq %esi, %rsi
imulq $0x28, %rsi, %rsi
addq %rsi, %rdx
addl 0x14(%rdx), %ecx
cmpl %ecx, %eax
jae 0x50eb
movq -0x58(%rbp), %rax
movq (%rax), %rcx
movslq -0x1c(%rbp), %rdx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
movl 0x10(%rcx), %esi
movl -0x1c(%rbp), %edx
movq (%rax), %rcx
movl -0x1c(%rbp), %edi
subl $0x1, %edi
movslq %edi, %rdi
imulq $0x28, %rdi, %rdi
addq %rdi, %rcx
movl 0x10(%rcx), %ecx
movq (%rax), %rax
movl -0x1c(%rbp), %edi
subl $0x1, %edi
movslq %edi, %rdi
imulq $0x28, %rdi, %rdi
addq %rdi, %rax
movl 0x14(%rax), %r8d
leaq 0x319f(%rip), %rdi      # 0x8279
movb $0x0, %al
callq 0x1070
movl $0x1, %edi
callq 0x1150
jmp 0x50ed
jmp 0x50ef
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x4f36
movq -0x58(%rbp), %rdx
movq -0x38(%rbp), %rcx
movq -0x40(%rbp), %rax
movl -0x28(%rbp), %r8d
addl $0x2, %r8d
movq -0x18(%rbp), %rsi
movzwl 0x30(%rsi), %edi
subl %r8d, %edi
movw %di, 0x30(%rsi)
movq (%rdx), %rsi
movq -0x18(%rbp), %rdi
movzwl 0x30(%rdi), %edi
imulq $0x28, %rdi, %rdi
addq %rdi, %rsi
movl 0x10(%rsi), %edi
movq -0x18(%rbp), %rsi
movl %edi, 0x20(%rsi)
movq -0x18(%rbp), %rsi
movzwl 0x30(%rsi), %esi
movl %esi, 0x10(%rdx)
movq (%rax), %rax
movzwl 0x32(%rcx), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movl 0x14(%rax), %eax
movl %eax, %edi
shlq $0x0, %rdi
callq 0x10f0
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, 0x8(%rax)
cmpq $0x0, 0x8(%rax)
jne 0x518d
leaq 0x2fc4(%rip), %rdi      # 0x8140
movb $0x0, %al
callq 0x1070
movl $0x1, %edi
callq 0x1150
movl $0x0, -0x1c(%rbp)
movq -0x38(%rbp), %rdx
movq -0x40(%rbp), %rcx
movl -0x1c(%rbp), %eax
movq (%rcx), %rcx
movzwl 0x32(%rdx), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpl 0x14(%rcx), %eax
jae 0x51db
movq -0x58(%rbp), %rax
movq -0x40(%rbp), %rcx
movq 0x8(%rcx), %rcx
movslq -0x1c(%rbp), %rdx
movb (%rcx,%rdx), %dl
movq 0x8(%rax), %rax
movslq -0x1c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x5194
movq -0x18(%rbp), %rdi
movq -0x10(%rbp), %rcx
movl $0x34, %esi
movl $0x1, %edx
callq 0x1160
movq -0x50(%rbp), %rax
addq $0xd0, %rsp
popq %rbp
retq
nop