Elf64Convert.c revision 4fd606d1f5abe38e1f42c38de1d2e895166bd0f4
/** @file
Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials 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
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "WinNtInclude.h"
#ifndef __GNUC__
#include <windows.h>
#include <io.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <Common/UefiBaseTypes.h>
#include <IndustryStandard/PeImage.h>
#include "PeCoffLib.h"
#include "EfiUtilityMsgs.h"
#include "GenFw.h"
#include "ElfConvert.h"
#include "Elf64Convert.h"
);
);
);
);
);
);
//
// Rename ELF32 strucutres to common names to help when porting to ELF64.
//
typedef Elf64_Shdr Elf_Shdr;
typedef Elf64_Ehdr Elf_Ehdr;
typedef Elf64_Rela Elf_Rela;
typedef Elf64_Phdr Elf_Phdr;
#define ELFCLASS ELFCLASS64
#define ELF_R_TYPE(r) ELF64_R_TYPE(r)
#define ELF_R_SYM(r) ELF64_R_SYM(r)
//
// Well known ELF structures.
//
//
// Coff information
//
//
// PE section alignment.
//
//
// ELF sections to offset in Coff file.
//
//
// Offsets in COFF file
//
//
// Initialization Function
//
)
{
//
// Initialize data pointer and structures.
//
VerboseMsg ("Set EHDR");
//
// Check the ELF64 specific header information.
//
VerboseMsg ("Check ELF64 Header Information");
return FALSE;
}
return FALSE;
}
return FALSE;
}
return FALSE;
}
Error (NULL, 0, 3000, "Unsupported", "ELF e_version (%u) not EV_CURRENT (%d)", (unsigned) mEhdr->e_version, EV_CURRENT);
return FALSE;
}
//
// Update section header pointers
//
VerboseMsg ("Update Header Pointers");
//
// Create COFF Section offset buffer and zero.
//
VerboseMsg ("Create COFF Section Offset Buffer");
//
// Fill in function pointers.
//
VerboseMsg ("Fill in Function Pointers");
return TRUE;
}
//
// Header by Index functions
//
)
{
return NULL;
}
)
{
}
//
// filter functions
//
)
{
}
)
{
return (BOOLEAN) (strcmp((CHAR8*)mEhdr + Namedr->sh_offset + Shdr->sh_name, ELF_HII_SECTION_NAME) == 0);
}
)
{
if (IsHiiRsrcShdr(Shdr)) {
return FALSE;
}
}
//
// Elf functions interface implementation
//
)
{
UINT32 i;
CoffEntry = 0;
mCoffOffset = 0;
//
// Coff file start with a DOS header.
//
case EM_X86_64:
case EM_IA_64:
mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS64);
break;
default:
mCoffOffset += sizeof (EFI_IMAGE_NT_HEADERS64);
break;
}
//
// First text sections.
//
SectionCount = 0;
if (IsTextShdr(shdr)) {
// the alignment field is valid
// ARM RVCT tools have behavior outside of the ELF specification to try
// and make images smaller. If sh_addr is not aligned to sh_addralign
// then the section needs to preserve sh_addr MOD sh_addralign.
// Normally doing nothing here works great.
}
}
/* Relocate entry. */
}
SectionCount ++;
}
}
}
Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 text section. Source level debug might not work correctly.", mInImageName);
}
//
// Then data sections.
//
SectionCount = 0;
if (IsDataShdr(shdr)) {
// the alignment field is valid
// ARM RVCT tools have behavior outside of the ELF specification to try
// and make images smaller. If sh_addr is not aligned to sh_addralign
// then the section needs to preserve sh_addr MOD sh_addralign.
// Normally doing nothing here works great.
}
}
SectionCount ++;
}
}
Warning (NULL, 0, 0, NULL, "Mulitple sections in %s are merged into 1 data section. Source level debug might not work correctly.", mInImageName);
}
//
// The HII resource sections.
//
if (IsHiiRsrcShdr(shdr)) {
// the alignment field is valid
// ARM RVCT tools have behavior outside of the ELF specification to try
// and make images smaller. If sh_addr is not aligned to sh_addralign
// then the section needs to preserve sh_addr MOD sh_addralign.
// Normally doing nothing here works great.
}
}
}
break;
}
}
//
// Allocate base Coff file. Will be expanded later for relocations.
//
//
// Fill headers.
//
case EM_X86_64:
break;
case EM_IA_64:
break;
default:
}
//
// Section headers.
//
if ((mDataOffset - mTextOffset) > 0) {
} else {
// Don't make a section of size 0.
}
if ((mHiiRsrcOffset - mDataOffset) > 0) {
} else {
// Don't make a section of size 0.
}
if ((mRelocOffset - mHiiRsrcOffset) > 0) {
NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].Size = mRelocOffset - mHiiRsrcOffset;
NtHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress = mHiiRsrcOffset;
} else {
// Don't make a section of size 0.
}
}
)
{
//
// Initialize filter pointer
//
switch (FilterType) {
case SECTION_TEXT:
Filter = IsTextShdr;
break;
case SECTION_HII:
break;
case SECTION_DATA:
Filter = IsDataShdr;
break;
default:
return FALSE;
}
//
// First: copy sections.
//
case SHT_PROGBITS:
/* Copy. */
break;
case SHT_NOBITS:
break;
default:
//
// Ignore for unkown section type.
//
VerboseMsg ("%s unknown section type %x. We directly copy this section into Coff file", mInImageName, (unsigned)Shdr->sh_type);
break;
}
}
}
//
// Second: apply relocations.
//
VerboseMsg ("Applying Relocations...");
continue;
}
}
//
// Note: r_offset in a memory address.
// Convert it to a pointer in the coff file.
//
case R_X86_64_NONE:
break;
case R_X86_64_64:
//
// Absolute relocation.
//
VerboseMsg ("R_X86_64_64");
VerboseMsg ("Offset: 0x%08X, Addend: 0x%016LX",
break;
case R_X86_64_32:
VerboseMsg ("R_X86_64_32");
VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
*(UINT32 *)Targ = (UINT32)((UINT64)(*(UINT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
break;
case R_X86_64_32S:
VerboseMsg ("R_X86_64_32S");
VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
*(INT32 *)Targ = (INT32)((INT64)(*(INT32 *)Targ) - SymShdr->sh_addr + mCoffSectionsOffset[Sym->st_shndx]);
break;
case R_X86_64_PC32:
//
// Relative relocation: Symbol - Ip + Addend
//
VerboseMsg ("R_X86_64_PC32");
VerboseMsg ("Offset: 0x%08X, Addend: 0x%08X",
break;
default:
Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
}
} else {
}
}
}
}
return TRUE;
}
)
{
case R_X86_64_NONE:
case R_X86_64_PC32:
break;
case R_X86_64_64:
VerboseMsg ("EFI_IMAGE_REL_BASED_DIR64 Offset: 0x%08X",
break;
case R_X86_64_32S:
case R_X86_64_32:
VerboseMsg ("EFI_IMAGE_REL_BASED_HIGHLOW Offset: 0x%08X",
break;
default:
Error (NULL, 0, 3000, "Invalid", "%s unsupported ELF EM_X86_64 relocation 0x%x.", mInImageName, (unsigned) ELF_R_TYPE(Rel->r_info));
}
} else {
Error (NULL, 0, 3000, "Not Supported", "This tool does not support relocations for ELF with e_machine %u (processor type).", (unsigned) mEhdr->e_machine);
}
}
}
}
}
//
// Pad by adding empty entries.
//
}
// If no relocations, null out the directory entry and don't add the .reloc section
Dir->VirtualAddress = 0;
} else {
}
}
)
{
mCoffOffset += sizeof(EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)
+ sizeof(EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY)
+ Len;
// If no debug, null out the directory entry and don't add the .debug section
DataDir->VirtualAddress = 0;
} else {
}
}
)
{
//
// Set image size
//
}
)
{
if (mCoffSectionsOffset != NULL) {
}
}