Fix the unalignment issue for RODATA section when converting ELF image to PE image.

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@2531 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
klu2 2007-04-04 05:57:31 +00:00
parent 265bfe7cd4
commit 189985640d
1 changed files with 22 additions and 14 deletions

View File

@ -1,6 +1,6 @@
/*++ /*++
Copyright (c) 2004, Intel Corporation Copyright (c) 2004 - 2007, Intel Corporation
All rights reserved. This program and the accompanying materials All rights reserved. This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at which accompanies this distribution. The full text of the license may be found at
@ -240,7 +240,7 @@ INTN
CheckElfHeader( CheckElfHeader(
VOID VOID
) )
{ {
// //
// Note: Magic has already been tested. // Note: Magic has already been tested.
// //
@ -254,11 +254,15 @@ CheckElfHeader(
return 0; return 0;
if (Ehdr->e_version != EV_CURRENT) if (Ehdr->e_version != EV_CURRENT)
return 0; return 0;
//
// Find the section header table
//
ShdrBase = (Elf_Shdr *)((UINT8 *)Ehdr + Ehdr->e_shoff); ShdrBase = (Elf_Shdr *)((UINT8 *)Ehdr + Ehdr->e_shoff);
CoffSectionsOffset = (UINT32 *)malloc(Ehdr->e_shnum * sizeof (UINT32)); CoffSectionsOffset = (UINT32 *)malloc(Ehdr->e_shnum * sizeof (UINT32));
memset(CoffSectionsOffset, 0, Ehdr->e_shnum * sizeof(UINT32));
memset(CoffSectionsOffset, 0, Ehdr->e_shnum * sizeof(UINT32));
return 1; return 1;
} }
@ -269,7 +273,7 @@ IsTextShdr(
{ {
return (Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == SHF_ALLOC; return (Shdr->sh_flags & (SHF_WRITE | SHF_ALLOC)) == SHF_ALLOC;
} }
int int
IsDataShdr( IsDataShdr(
Elf_Shdr *Shdr Elf_Shdr *Shdr
@ -331,18 +335,22 @@ ScanSections(
TextOffset = CoffOffset; TextOffset = CoffOffset;
for (i = 0; i < Ehdr->e_shnum; i++) { for (i = 0; i < Ehdr->e_shnum; i++) {
Elf_Shdr *shdr = GetShdrByIndex(i); Elf_Shdr *shdr = GetShdrByIndex(i);
if (IsTextShdr(shdr)) { if (IsTextShdr(shdr)) {
//
// Align the coff offset
//
CoffOffset = (CoffOffset + shdr->sh_addralign - 1) & ~(shdr->sh_addralign - 1);
/* Relocate entry. */ /* Relocate entry. */
if (Ehdr->e_entry >= shdr->sh_addr if ((Ehdr->e_entry >= shdr->sh_addr) &&
&& Ehdr->e_entry < shdr->sh_addr + shdr->sh_size) { (Ehdr->e_entry < shdr->sh_addr + shdr->sh_size)) {
CoffEntry = CoffOffset + Ehdr->e_entry - shdr->sh_addr; CoffEntry = CoffOffset + Ehdr->e_entry - shdr->sh_addr;
} }
CoffSectionsOffset[i] = CoffOffset; CoffSectionsOffset[i] = CoffOffset;
CoffOffset += shdr->sh_size; CoffOffset += shdr->sh_size;
} }
} }
CoffOffset = CoffAlign(CoffOffset); CoffOffset = CoffAlign(CoffOffset);
// //
// Then data sections. // Then data sections.
// //