UefiPayloadPkg: Apply uncrustify changes

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3737

Apply uncrustify changes to .c/.h files in the UefiPayloadPkg package

Cc: Andrew Fish <afish@apple.com>
Cc: Leif Lindholm <leif@nuviainc.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
This commit is contained in:
Michael Kubacki
2021-12-05 14:54:18 -08:00
committed by mergify[bot]
parent 053e878bfb
commit e5efcf8be8
76 changed files with 4281 additions and 4072 deletions

View File

@@ -35,8 +35,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
*/
#ifndef _SYS_ELF32_H_
#define _SYS_ELF32_H_ 1
#define _SYS_ELF32_H_ 1
/*
* ELF definitions common to all 32-bit architectures.
@@ -49,31 +48,31 @@ typedef INT32 Elf32_Sword;
typedef UINT32 Elf32_Word;
typedef UINT64 Elf32_Lword;
typedef Elf32_Word Elf32_Hashelt;
typedef Elf32_Word Elf32_Hashelt;
/* Non-standard class-dependent datatype used for abstraction. */
typedef Elf32_Word Elf32_Size;
typedef Elf32_Sword Elf32_Ssize;
typedef Elf32_Sword Elf32_Ssize;
/*
* ELF header.
*/
typedef struct {
unsigned char e_ident[EI_NIDENT]; /* File identification. */
Elf32_Half e_type; /* File type. */
Elf32_Half e_machine; /* Machine architecture. */
Elf32_Word e_version; /* ELF format version. */
Elf32_Addr e_entry; /* Entry point. */
Elf32_Off e_phoff; /* Program header file offset. */
Elf32_Off e_shoff; /* Section header file offset. */
Elf32_Word e_flags; /* Architecture-specific flags. */
Elf32_Half e_ehsize; /* Size of ELF header in bytes. */
Elf32_Half e_phentsize; /* Size of program header entry. */
Elf32_Half e_phnum; /* Number of program header entries. */
Elf32_Half e_shentsize; /* Size of section header entry. */
Elf32_Half e_shnum; /* Number of section header entries. */
Elf32_Half e_shstrndx; /* Section name strings section. */
unsigned char e_ident[EI_NIDENT]; /* File identification. */
Elf32_Half e_type; /* File type. */
Elf32_Half e_machine; /* Machine architecture. */
Elf32_Word e_version; /* ELF format version. */
Elf32_Addr e_entry; /* Entry point. */
Elf32_Off e_phoff; /* Program header file offset. */
Elf32_Off e_shoff; /* Section header file offset. */
Elf32_Word e_flags; /* Architecture-specific flags. */
Elf32_Half e_ehsize; /* Size of ELF header in bytes. */
Elf32_Half e_phentsize; /* Size of program header entry. */
Elf32_Half e_phnum; /* Number of program header entries. */
Elf32_Half e_shentsize; /* Size of section header entry. */
Elf32_Half e_shnum; /* Number of section header entries. */
Elf32_Half e_shstrndx; /* Section name strings section. */
} Elf32_Ehdr;
/*
@@ -81,17 +80,17 @@ typedef struct {
*/
typedef struct {
Elf32_Word sh_name; /* Section name (index into the
Elf32_Word sh_name; /* Section name (index into the
section header string table). */
Elf32_Word sh_type; /* Section type. */
Elf32_Word sh_flags; /* Section flags. */
Elf32_Addr sh_addr; /* Address in memory image. */
Elf32_Off sh_offset; /* Offset in file. */
Elf32_Word sh_size; /* Size in bytes. */
Elf32_Word sh_link; /* Index of a related section. */
Elf32_Word sh_info; /* Depends on section type. */
Elf32_Word sh_addralign; /* Alignment in bytes. */
Elf32_Word sh_entsize; /* Size of each entry in section. */
Elf32_Word sh_type; /* Section type. */
Elf32_Word sh_flags; /* Section flags. */
Elf32_Addr sh_addr; /* Address in memory image. */
Elf32_Off sh_offset; /* Offset in file. */
Elf32_Word sh_size; /* Size in bytes. */
Elf32_Word sh_link; /* Index of a related section. */
Elf32_Word sh_info; /* Depends on section type. */
Elf32_Word sh_addralign; /* Alignment in bytes. */
Elf32_Word sh_entsize; /* Size of each entry in section. */
} Elf32_Shdr;
/*
@@ -99,14 +98,14 @@ typedef struct {
*/
typedef struct {
Elf32_Word p_type; /* Entry type. */
Elf32_Off p_offset; /* File offset of contents. */
Elf32_Addr p_vaddr; /* Virtual address in memory image. */
Elf32_Addr p_paddr; /* Physical address (not used). */
Elf32_Word p_filesz; /* Size of contents in file. */
Elf32_Word p_memsz; /* Size of contents in memory. */
Elf32_Word p_flags; /* Access permission flags. */
Elf32_Word p_align; /* Alignment in memory and file. */
Elf32_Word p_type; /* Entry type. */
Elf32_Off p_offset; /* File offset of contents. */
Elf32_Addr p_vaddr; /* Virtual address in memory image. */
Elf32_Addr p_paddr; /* Physical address (not used). */
Elf32_Word p_filesz; /* Size of contents in file. */
Elf32_Word p_memsz; /* Size of contents in memory. */
Elf32_Word p_flags; /* Access permission flags. */
Elf32_Word p_align; /* Alignment in memory and file. */
} Elf32_Phdr;
/*
@@ -114,10 +113,10 @@ typedef struct {
*/
typedef struct {
Elf32_Sword d_tag; /* Entry type. */
Elf32_Sword d_tag; /* Entry type. */
union {
Elf32_Word d_val; /* Integer value. */
Elf32_Addr d_ptr; /* Address value. */
Elf32_Word d_val; /* Integer value. */
Elf32_Addr d_ptr; /* Address value. */
} d_un;
} Elf32_Dyn;
@@ -127,19 +126,19 @@ typedef struct {
/* Relocations that don't need an addend field. */
typedef struct {
Elf32_Addr r_offset; /* Location to be relocated. */
Elf32_Word r_info; /* Relocation type and symbol index. */
Elf32_Addr r_offset; /* Location to be relocated. */
Elf32_Word r_info; /* Relocation type and symbol index. */
} Elf32_Rel;
/* Relocations that need an addend field. */
typedef struct {
Elf32_Addr r_offset; /* Location to be relocated. */
Elf32_Word r_info; /* Relocation type and symbol index. */
Elf32_Sword r_addend; /* Addend. */
Elf32_Addr r_offset; /* Location to be relocated. */
Elf32_Word r_info; /* Relocation type and symbol index. */
Elf32_Sword r_addend; /* Addend. */
} Elf32_Rela;
/* Macros for accessing the fields of r_info. */
#define ELF32_R_SYM(info) ((info) >> 8)
#define ELF32_R_SYM(info) ((info) >> 8)
#define ELF32_R_TYPE(info) ((unsigned char)(info))
/* Macro for constructing r_info from field values. */
@@ -154,11 +153,11 @@ typedef Elf_Note Elf32_Nhdr;
* Move entry
*/
typedef struct {
Elf32_Lword m_value; /* symbol value */
Elf32_Word m_info; /* size + index */
Elf32_Word m_poffset; /* symbol offset */
Elf32_Half m_repeat; /* repeat count */
Elf32_Half m_stride; /* stride info */
Elf32_Lword m_value; /* symbol value */
Elf32_Word m_info; /* size + index */
Elf32_Word m_poffset; /* symbol offset */
Elf32_Half m_repeat; /* repeat count */
Elf32_Half m_stride; /* stride info */
} Elf32_Move;
/*
@@ -168,18 +167,18 @@ typedef struct {
* size = ELF32_M_SIZE(M.m_info)
* M.m_info = ELF32_M_INFO(sym, size)
*/
#define ELF32_M_SYM(info) ((info)>>8)
#define ELF32_M_SIZE(info) ((unsigned char)(info))
#define ELF32_M_SYM(info) ((info)>>8)
#define ELF32_M_SIZE(info) ((unsigned char)(info))
#define ELF32_M_INFO(sym, size) (((sym)<<8)+(unsigned char)(size))
/*
* Hardware/Software capabilities entry
*/
typedef struct {
Elf32_Word c_tag; /* how to interpret value */
Elf32_Word c_tag; /* how to interpret value */
union {
Elf32_Word c_val;
Elf32_Addr c_ptr;
Elf32_Word c_val;
Elf32_Addr c_ptr;
} c_un;
} Elf32_Cap;
@@ -188,17 +187,17 @@ typedef struct {
*/
typedef struct {
Elf32_Word st_name; /* String table index of name. */
Elf32_Addr st_value; /* Symbol value. */
Elf32_Word st_size; /* Size of associated object. */
unsigned char st_info; /* Type and binding information. */
unsigned char st_other; /* Reserved (not used). */
Elf32_Half st_shndx; /* Section index of symbol. */
Elf32_Word st_name; /* String table index of name. */
Elf32_Addr st_value; /* Symbol value. */
Elf32_Word st_size; /* Size of associated object. */
unsigned char st_info; /* Type and binding information. */
unsigned char st_other; /* Reserved (not used). */
Elf32_Half st_shndx; /* Section index of symbol. */
} Elf32_Sym;
/* Macros for accessing the fields of st_info. */
#define ELF32_ST_BIND(info) ((info) >> 4)
#define ELF32_ST_TYPE(info) ((info) & 0xf)
#define ELF32_ST_BIND(info) ((info) >> 4)
#define ELF32_ST_TYPE(info) ((info) & 0xf)
/* Macro for constructing st_info from field values. */
#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
@@ -207,46 +206,42 @@ typedef struct {
#define ELF32_ST_VISIBILITY(oth) ((oth) & 0x3)
/* Structures used by Sun & GNU symbol versioning. */
typedef struct
{
Elf32_Half vd_version;
Elf32_Half vd_flags;
Elf32_Half vd_ndx;
Elf32_Half vd_cnt;
Elf32_Word vd_hash;
Elf32_Word vd_aux;
Elf32_Word vd_next;
typedef struct {
Elf32_Half vd_version;
Elf32_Half vd_flags;
Elf32_Half vd_ndx;
Elf32_Half vd_cnt;
Elf32_Word vd_hash;
Elf32_Word vd_aux;
Elf32_Word vd_next;
} Elf32_Verdef;
typedef struct
{
Elf32_Word vda_name;
Elf32_Word vda_next;
typedef struct {
Elf32_Word vda_name;
Elf32_Word vda_next;
} Elf32_Verdaux;
typedef struct
{
Elf32_Half vn_version;
Elf32_Half vn_cnt;
Elf32_Word vn_file;
Elf32_Word vn_aux;
Elf32_Word vn_next;
typedef struct {
Elf32_Half vn_version;
Elf32_Half vn_cnt;
Elf32_Word vn_file;
Elf32_Word vn_aux;
Elf32_Word vn_next;
} Elf32_Verneed;
typedef struct
{
Elf32_Word vna_hash;
Elf32_Half vna_flags;
Elf32_Half vna_other;
Elf32_Word vna_name;
Elf32_Word vna_next;
typedef struct {
Elf32_Word vna_hash;
Elf32_Half vna_flags;
Elf32_Half vna_other;
Elf32_Word vna_name;
Elf32_Word vna_next;
} Elf32_Vernaux;
typedef Elf32_Half Elf32_Versym;
typedef struct {
Elf32_Half si_boundto; /* direct bindings - symbol bound to */
Elf32_Half si_flags; /* per symbol flags */
Elf32_Half si_boundto; /* direct bindings - symbol bound to */
Elf32_Half si_flags; /* per symbol flags */
} Elf32_Syminfo;
#endif /* !_SYS_ELF32_H_ */

View File

@@ -18,13 +18,13 @@
**/
Elf32_Shdr *
GetElf32SectionByIndex (
IN UINT8 *ImageBase,
IN UINT32 Index
IN UINT8 *ImageBase,
IN UINT32 Index
)
{
Elf32_Ehdr *Ehdr;
Elf32_Ehdr *Ehdr;
Ehdr = (Elf32_Ehdr *)ImageBase;
Ehdr = (Elf32_Ehdr *)ImageBase;
if (Index >= Ehdr->e_shnum) {
return NULL;
}
@@ -42,13 +42,13 @@ GetElf32SectionByIndex (
**/
Elf32_Phdr *
GetElf32SegmentByIndex (
IN UINT8 *ImageBase,
IN UINT32 Index
IN UINT8 *ImageBase,
IN UINT32 Index
)
{
Elf32_Ehdr *Ehdr;
Elf32_Ehdr *Ehdr;
Ehdr = (Elf32_Ehdr *)ImageBase;
Ehdr = (Elf32_Ehdr *)ImageBase;
if (Index >= Ehdr->e_phnum) {
return NULL;
}
@@ -67,24 +67,26 @@ GetElf32SegmentByIndex (
**/
Elf32_Shdr *
GetElf32SectionByRange (
IN UINT8 *ImageBase,
IN UINT32 Offset,
IN UINT32 Size
IN UINT8 *ImageBase,
IN UINT32 Offset,
IN UINT32 Size
)
{
UINT32 Index;
Elf32_Ehdr *Ehdr;
Elf32_Shdr *Shdr;
UINT32 Index;
Elf32_Ehdr *Ehdr;
Elf32_Shdr *Shdr;
Ehdr = (Elf32_Ehdr *)ImageBase;
Shdr = (Elf32_Shdr *) (ImageBase + Ehdr->e_shoff);
Shdr = (Elf32_Shdr *)(ImageBase + Ehdr->e_shoff);
for (Index = 0; Index < Ehdr->e_shnum; Index++) {
if ((Shdr->sh_offset == Offset) && (Shdr->sh_size == Size)) {
return Shdr;
}
Shdr = ELF_NEXT_ENTRY (Elf32_Shdr, Shdr, Ehdr->e_shentsize);
}
return NULL;
}
@@ -102,27 +104,28 @@ GetElf32SectionByRange (
**/
EFI_STATUS
ProcessRelocation32 (
IN Elf32_Rela *Rela,
IN UINT32 RelaSize,
IN UINT32 RelaEntrySize,
IN UINT32 RelaType,
IN INTN Delta,
IN BOOLEAN DynamicLinking
IN Elf32_Rela *Rela,
IN UINT32 RelaSize,
IN UINT32 RelaEntrySize,
IN UINT32 RelaType,
IN INTN Delta,
IN BOOLEAN DynamicLinking
)
{
UINTN Index;
UINT32 *Ptr;
UINT32 Type;
UINTN Index;
UINT32 *Ptr;
UINT32 Type;
for ( Index = 0
; RelaEntrySize * Index < RelaSize
; Index++, Rela = ELF_NEXT_ENTRY (Elf32_Rela, Rela, RelaEntrySize)
) {
; RelaEntrySize * Index < RelaSize
; Index++, Rela = ELF_NEXT_ENTRY (Elf32_Rela, Rela, RelaEntrySize)
)
{
//
// r_offset is the virtual address of the storage unit affected by the relocation.
//
Ptr = (UINT32 *)(UINTN)(Rela->r_offset + Delta);
Type = ELF32_R_TYPE(Rela->r_info);
Ptr = (UINT32 *)(UINTN)(Rela->r_offset + Delta);
Type = ELF32_R_TYPE (Rela->r_info);
switch (Type) {
case R_386_NONE:
case R_386_PC32:
@@ -139,8 +142,9 @@ ProcessRelocation32 (
DEBUG ((DEBUG_INFO, "Unsupported relocation type %02X\n", Type));
ASSERT (FALSE);
} else {
*Ptr += (UINT32) Delta;
*Ptr += (UINT32)Delta;
}
break;
case R_386_RELATIVE:
@@ -164,12 +168,12 @@ ProcessRelocation32 (
// Calculation: B + A
//
if (RelaType == SHT_RELA) {
*Ptr = (UINT32) Delta + Rela->r_addend;
*Ptr = (UINT32)Delta + Rela->r_addend;
} else {
//
// A is stored in the field of relocation for REL type.
//
*Ptr = (UINT32) Delta + *Ptr;
*Ptr = (UINT32)Delta + *Ptr;
}
} else {
//
@@ -178,12 +182,14 @@ ProcessRelocation32 (
DEBUG ((DEBUG_INFO, "Unsupported relocation type %02X\n", Type));
ASSERT (FALSE);
}
break;
default:
DEBUG ((DEBUG_INFO, "Unsupported relocation type %02X\n", Type));
}
}
return EFI_SUCCESS;
}
@@ -197,19 +203,19 @@ ProcessRelocation32 (
**/
EFI_STATUS
RelocateElf32Dynamic (
IN ELF_IMAGE_CONTEXT *ElfCt
IN ELF_IMAGE_CONTEXT *ElfCt
)
{
UINT32 Index;
Elf32_Phdr *Phdr;
Elf32_Shdr *DynShdr;
Elf32_Shdr *RelShdr;
Elf32_Dyn *Dyn;
UINT32 RelaAddress;
UINT32 RelaCount;
UINT32 RelaSize;
UINT32 RelaEntrySize;
UINT32 RelaType;
UINT32 Index;
Elf32_Phdr *Phdr;
Elf32_Shdr *DynShdr;
Elf32_Shdr *RelShdr;
Elf32_Dyn *Dyn;
UINT32 RelaAddress;
UINT32 RelaCount;
UINT32 RelaSize;
UINT32 RelaEntrySize;
UINT32 RelaType;
//
// 1. Locate the dynamic section.
@@ -239,6 +245,7 @@ RelocateElf32Dynamic (
if (DynShdr == NULL) {
return EFI_UNSUPPORTED;
}
ASSERT (DynShdr->sh_type == SHT_DYNAMIC);
ASSERT (DynShdr->sh_entsize >= sizeof (*Dyn));
@@ -250,10 +257,11 @@ RelocateElf32Dynamic (
RelaCount = 0;
RelaEntrySize = 0;
RelaType = 0;
for ( Index = 0, Dyn = (Elf32_Dyn *) (ElfCt->FileBase + DynShdr->sh_offset)
; Index < DynShdr->sh_size / DynShdr->sh_entsize
; Index++, Dyn = ELF_NEXT_ENTRY (Elf32_Dyn, Dyn, DynShdr->sh_entsize)
) {
for ( Index = 0, Dyn = (Elf32_Dyn *)(ElfCt->FileBase + DynShdr->sh_offset)
; Index < DynShdr->sh_size / DynShdr->sh_entsize
; Index++, Dyn = ELF_NEXT_ENTRY (Elf32_Dyn, Dyn, DynShdr->sh_entsize)
)
{
switch (Dyn->d_tag) {
case DT_RELA:
case DT_REL:
@@ -265,7 +273,7 @@ RelocateElf32Dynamic (
// For consistency, files do not contain relocation entries to ``correct'' addresses in the dynamic structure.
//
RelaAddress = Dyn->d_un.d_ptr;
RelaType = (Dyn->d_tag == DT_RELA) ? SHT_RELA: SHT_REL;
RelaType = (Dyn->d_tag == DT_RELA) ? SHT_RELA : SHT_REL;
break;
case DT_RELACOUNT:
case DT_RELCOUNT:
@@ -304,12 +312,14 @@ RelocateElf32Dynamic (
if ((RelShdr->sh_addr == RelaAddress) && (RelShdr->sh_size == RelaSize)) {
break;
}
RelShdr = NULL;
}
if (RelShdr == NULL) {
return EFI_UNSUPPORTED;
}
ASSERT (RelShdr->sh_type == RelaType);
ASSERT (RelShdr->sh_entsize == RelaEntrySize);
@@ -317,9 +327,11 @@ RelocateElf32Dynamic (
// 3. Process the relocation section.
//
ProcessRelocation32 (
(Elf32_Rela *) (ElfCt->FileBase + RelShdr->sh_offset),
RelShdr->sh_size, RelShdr->sh_entsize, RelShdr->sh_type,
(UINTN) ElfCt->ImageAddress - (UINTN) ElfCt->PreferredImageAddress,
(Elf32_Rela *)(ElfCt->FileBase + RelShdr->sh_offset),
RelShdr->sh_size,
RelShdr->sh_entsize,
RelShdr->sh_type,
(UINTN)ElfCt->ImageAddress - (UINTN)ElfCt->PreferredImageAddress,
TRUE
);
return EFI_SUCCESS;
@@ -335,22 +347,22 @@ RelocateElf32Dynamic (
**/
EFI_STATUS
RelocateElf32Sections (
IN ELF_IMAGE_CONTEXT *ElfCt
IN ELF_IMAGE_CONTEXT *ElfCt
)
{
EFI_STATUS Status;
Elf32_Ehdr *Ehdr;
Elf32_Shdr *RelShdr;
Elf32_Shdr *Shdr;
UINT32 Index;
UINTN Delta;
EFI_STATUS Status;
Elf32_Ehdr *Ehdr;
Elf32_Shdr *RelShdr;
Elf32_Shdr *Shdr;
UINT32 Index;
UINTN Delta;
Ehdr = (Elf32_Ehdr *)ElfCt->FileBase;
Ehdr = (Elf32_Ehdr *)ElfCt->FileBase;
if (Ehdr->e_machine != EM_386) {
return EFI_UNSUPPORTED;
}
Delta = (UINTN) ElfCt->ImageAddress - (UINTN) ElfCt->PreferredImageAddress;
Delta = (UINTN)ElfCt->ImageAddress - (UINTN)ElfCt->PreferredImageAddress;
ElfCt->EntryPoint = (UINTN)(Ehdr->e_entry + Delta);
//
@@ -373,22 +385,27 @@ RelocateElf32Sections (
// The below relocation is needed in this case.
//
DEBUG ((DEBUG_INFO, "EXEC ELF: Fix actual/preferred base address delta ...\n"));
for ( Index = 0, RelShdr = (Elf32_Shdr *) (ElfCt->FileBase + Ehdr->e_shoff)
; Index < Ehdr->e_shnum
; Index++, RelShdr = ELF_NEXT_ENTRY (Elf32_Shdr, RelShdr, Ehdr->e_shentsize)
) {
for ( Index = 0, RelShdr = (Elf32_Shdr *)(ElfCt->FileBase + Ehdr->e_shoff)
; Index < Ehdr->e_shnum
; Index++, RelShdr = ELF_NEXT_ENTRY (Elf32_Shdr, RelShdr, Ehdr->e_shentsize)
)
{
if ((RelShdr->sh_type != SHT_REL) && (RelShdr->sh_type != SHT_RELA)) {
continue;
}
Shdr = GetElf32SectionByIndex (ElfCt->FileBase, RelShdr->sh_info);
if ((Shdr->sh_flags & SHF_ALLOC) == SHF_ALLOC) {
//
// Only fix up sections that occupy memory during process execution.
//
ProcessRelocation32 (
(Elf32_Rela *)((UINT8*)Ehdr + RelShdr->sh_offset),
RelShdr->sh_size, RelShdr->sh_entsize, RelShdr->sh_type,
Delta, FALSE
(Elf32_Rela *)((UINT8 *)Ehdr + RelShdr->sh_offset),
RelShdr->sh_size,
RelShdr->sh_entsize,
RelShdr->sh_type,
Delta,
FALSE
);
}
}
@@ -411,13 +428,13 @@ RelocateElf32Sections (
**/
EFI_STATUS
LoadElf32Image (
IN ELF_IMAGE_CONTEXT *ElfCt
IN ELF_IMAGE_CONTEXT *ElfCt
)
{
Elf32_Ehdr *Ehdr;
Elf32_Phdr *Phdr;
UINT16 Index;
UINTN Delta;
Elf32_Ehdr *Ehdr;
Elf32_Phdr *Phdr;
UINT16 Index;
UINTN Delta;
ASSERT (ElfCt != NULL);
@@ -427,14 +444,16 @@ LoadElf32Image (
Ehdr = (Elf32_Ehdr *)ElfCt->FileBase;
for ( Index = 0, Phdr = (Elf32_Phdr *)(ElfCt->FileBase + Ehdr->e_phoff)
; Index < Ehdr->e_phnum
; Index++, Phdr = ELF_NEXT_ENTRY (Elf32_Phdr, Phdr, Ehdr->e_phentsize)
) {
; Index < Ehdr->e_phnum
; Index++, Phdr = ELF_NEXT_ENTRY (Elf32_Phdr, Phdr, Ehdr->e_phentsize)
)
{
//
// Skip segments that don't require load (type tells, or size is 0)
//
if ((Phdr->p_type != PT_LOAD) ||
(Phdr->p_memsz == 0)) {
(Phdr->p_memsz == 0))
{
continue;
}
@@ -442,7 +461,7 @@ LoadElf32Image (
// The memory offset of segment relative to the image base
// Note: CopyMem() does nothing when the dst equals to src.
//
Delta = Phdr->p_paddr - (UINT32) (UINTN) ElfCt->PreferredImageAddress;
Delta = Phdr->p_paddr - (UINT32)(UINTN)ElfCt->PreferredImageAddress;
CopyMem (ElfCt->ImageAddress + Delta, ElfCt->FileBase + Phdr->p_offset, Phdr->p_filesz);
ZeroMem (ElfCt->ImageAddress + Delta + Phdr->p_filesz, Phdr->p_memsz - Phdr->p_filesz);
}

View File

@@ -34,8 +34,7 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
*/
#ifndef _SYS_ELF64_H_
#define _SYS_ELF64_H_ 1
#define _SYS_ELF64_H_ 1
/*
* ELF definitions common to all 64-bit architectures.
@@ -57,31 +56,31 @@ typedef UINT64 Elf64_Xword;
* typedef is required.
*/
typedef Elf64_Word Elf64_Hashelt;
typedef Elf64_Word Elf64_Hashelt;
/* Non-standard class-dependent datatype used for abstraction. */
typedef Elf64_Xword Elf64_Size;
typedef Elf64_Sxword Elf64_Ssize;
typedef Elf64_Xword Elf64_Size;
typedef Elf64_Sxword Elf64_Ssize;
/*
* ELF header.
*/
typedef struct {
unsigned char e_ident[EI_NIDENT]; /* File identification. */
Elf64_Half e_type; /* File type. */
Elf64_Half e_machine; /* Machine architecture. */
Elf64_Word e_version; /* ELF format version. */
Elf64_Addr e_entry; /* Entry point. */
Elf64_Off e_phoff; /* Program header file offset. */
Elf64_Off e_shoff; /* Section header file offset. */
Elf64_Word e_flags; /* Architecture-specific flags. */
Elf64_Half e_ehsize; /* Size of ELF header in bytes. */
Elf64_Half e_phentsize; /* Size of program header entry. */
Elf64_Half e_phnum; /* Number of program header entries. */
Elf64_Half e_shentsize; /* Size of section header entry. */
Elf64_Half e_shnum; /* Number of section header entries. */
Elf64_Half e_shstrndx; /* Section name strings section. */
unsigned char e_ident[EI_NIDENT]; /* File identification. */
Elf64_Half e_type; /* File type. */
Elf64_Half e_machine; /* Machine architecture. */
Elf64_Word e_version; /* ELF format version. */
Elf64_Addr e_entry; /* Entry point. */
Elf64_Off e_phoff; /* Program header file offset. */
Elf64_Off e_shoff; /* Section header file offset. */
Elf64_Word e_flags; /* Architecture-specific flags. */
Elf64_Half e_ehsize; /* Size of ELF header in bytes. */
Elf64_Half e_phentsize; /* Size of program header entry. */
Elf64_Half e_phnum; /* Number of program header entries. */
Elf64_Half e_shentsize; /* Size of section header entry. */
Elf64_Half e_shnum; /* Number of section header entries. */
Elf64_Half e_shstrndx; /* Section name strings section. */
} Elf64_Ehdr;
/*
@@ -89,17 +88,17 @@ typedef struct {
*/
typedef struct {
Elf64_Word sh_name; /* Section name (index into the
Elf64_Word sh_name; /* Section name (index into the
section header string table). */
Elf64_Word sh_type; /* Section type. */
Elf64_Xword sh_flags; /* Section flags. */
Elf64_Addr sh_addr; /* Address in memory image. */
Elf64_Off sh_offset; /* Offset in file. */
Elf64_Xword sh_size; /* Size in bytes. */
Elf64_Word sh_link; /* Index of a related section. */
Elf64_Word sh_info; /* Depends on section type. */
Elf64_Xword sh_addralign; /* Alignment in bytes. */
Elf64_Xword sh_entsize; /* Size of each entry in section. */
Elf64_Word sh_type; /* Section type. */
Elf64_Xword sh_flags; /* Section flags. */
Elf64_Addr sh_addr; /* Address in memory image. */
Elf64_Off sh_offset; /* Offset in file. */
Elf64_Xword sh_size; /* Size in bytes. */
Elf64_Word sh_link; /* Index of a related section. */
Elf64_Word sh_info; /* Depends on section type. */
Elf64_Xword sh_addralign; /* Alignment in bytes. */
Elf64_Xword sh_entsize; /* Size of each entry in section. */
} Elf64_Shdr;
/*
@@ -107,14 +106,14 @@ typedef struct {
*/
typedef struct {
Elf64_Word p_type; /* Entry type. */
Elf64_Word p_flags; /* Access permission flags. */
Elf64_Off p_offset; /* File offset of contents. */
Elf64_Addr p_vaddr; /* Virtual address in memory image. */
Elf64_Addr p_paddr; /* Physical address (not used). */
Elf64_Xword p_filesz; /* Size of contents in file. */
Elf64_Xword p_memsz; /* Size of contents in memory. */
Elf64_Xword p_align; /* Alignment in memory and file. */
Elf64_Word p_type; /* Entry type. */
Elf64_Word p_flags; /* Access permission flags. */
Elf64_Off p_offset; /* File offset of contents. */
Elf64_Addr p_vaddr; /* Virtual address in memory image. */
Elf64_Addr p_paddr; /* Physical address (not used). */
Elf64_Xword p_filesz; /* Size of contents in file. */
Elf64_Xword p_memsz; /* Size of contents in memory. */
Elf64_Xword p_align; /* Alignment in memory and file. */
} Elf64_Phdr;
/*
@@ -122,10 +121,10 @@ typedef struct {
*/
typedef struct {
Elf64_Sxword d_tag; /* Entry type. */
Elf64_Sxword d_tag; /* Entry type. */
union {
Elf64_Xword d_val; /* Integer value. */
Elf64_Addr d_ptr; /* Address value. */
Elf64_Xword d_val; /* Integer value. */
Elf64_Addr d_ptr; /* Address value. */
} d_un;
} Elf64_Dyn;
@@ -135,26 +134,26 @@ typedef struct {
/* Relocations that don't need an addend field. */
typedef struct {
Elf64_Addr r_offset; /* Location to be relocated. */
Elf64_Xword r_info; /* Relocation type and symbol index. */
Elf64_Addr r_offset; /* Location to be relocated. */
Elf64_Xword r_info; /* Relocation type and symbol index. */
} Elf64_Rel;
/* Relocations that need an addend field. */
typedef struct {
Elf64_Addr r_offset; /* Location to be relocated. */
Elf64_Xword r_info; /* Relocation type and symbol index. */
Elf64_Sxword r_addend; /* Addend. */
Elf64_Addr r_offset; /* Location to be relocated. */
Elf64_Xword r_info; /* Relocation type and symbol index. */
Elf64_Sxword r_addend; /* Addend. */
} Elf64_Rela;
/* Macros for accessing the fields of r_info. */
#define ELF64_R_SYM(info) ((UINT32) RShiftU64 ((info), 32))
#define ELF64_R_SYM(info) ((UINT32) RShiftU64 ((info), 32))
#define ELF64_R_TYPE(info) ((info) & 0xffffffffL)
/* Macro for constructing r_info from field values. */
#define ELF64_R_INFO(sym, type) (((sym) << 32) + ((type) & 0xffffffffL))
#define ELF64_R_TYPE_DATA(info) (((Elf64_Xword)(info)<<32)>>40)
#define ELF64_R_TYPE_ID(info) (((Elf64_Xword)(info)<<56)>>56)
#define ELF64_R_TYPE_ID(info) (((Elf64_Xword)(info)<<56)>>56)
#define ELF64_R_TYPE_INFO(data, type) \
(((Elf64_Xword)(data)<<8)+(Elf64_Xword)(type))
@@ -167,25 +166,25 @@ typedef Elf_Note Elf64_Nhdr;
* Move entry
*/
typedef struct {
Elf64_Lword m_value; /* symbol value */
Elf64_Xword m_info; /* size + index */
Elf64_Xword m_poffset; /* symbol offset */
Elf64_Half m_repeat; /* repeat count */
Elf64_Half m_stride; /* stride info */
Elf64_Lword m_value; /* symbol value */
Elf64_Xword m_info; /* size + index */
Elf64_Xword m_poffset; /* symbol offset */
Elf64_Half m_repeat; /* repeat count */
Elf64_Half m_stride; /* stride info */
} Elf64_Move;
#define ELF64_M_SYM(info) ((info)>>8)
#define ELF64_M_SIZE(info) ((unsigned char)(info))
#define ELF64_M_SYM(info) ((info)>>8)
#define ELF64_M_SIZE(info) ((unsigned char)(info))
#define ELF64_M_INFO(sym, size) (((sym)<<8)+(unsigned char)(size))
/*
* Hardware/Software capabilities entry
*/
typedef struct {
Elf64_Xword c_tag; /* how to interpret value */
Elf64_Xword c_tag; /* how to interpret value */
union {
Elf64_Xword c_val;
Elf64_Addr c_ptr;
Elf64_Xword c_val;
Elf64_Addr c_ptr;
} c_un;
} Elf64_Cap;
@@ -194,17 +193,17 @@ typedef struct {
*/
typedef struct {
Elf64_Word st_name; /* String table index of name. */
unsigned char st_info; /* Type and binding information. */
unsigned char st_other; /* Reserved (not used). */
Elf64_Half st_shndx; /* Section index of symbol. */
Elf64_Addr st_value; /* Symbol value. */
Elf64_Xword st_size; /* Size of associated object. */
Elf64_Word st_name; /* String table index of name. */
unsigned char st_info; /* Type and binding information. */
unsigned char st_other; /* Reserved (not used). */
Elf64_Half st_shndx; /* Section index of symbol. */
Elf64_Addr st_value; /* Symbol value. */
Elf64_Xword st_size; /* Size of associated object. */
} Elf64_Sym;
/* Macros for accessing the fields of st_info. */
#define ELF64_ST_BIND(info) ((info) >> 4)
#define ELF64_ST_TYPE(info) ((info) & 0xf)
#define ELF64_ST_BIND(info) ((info) >> 4)
#define ELF64_ST_TYPE(info) ((info) & 0xf)
/* Macro for constructing st_info from field values. */
#define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
@@ -214,41 +213,41 @@ typedef struct {
/* Structures used by Sun & GNU-style symbol versioning. */
typedef struct {
Elf64_Half vd_version;
Elf64_Half vd_flags;
Elf64_Half vd_ndx;
Elf64_Half vd_cnt;
Elf64_Word vd_hash;
Elf64_Word vd_aux;
Elf64_Word vd_next;
Elf64_Half vd_version;
Elf64_Half vd_flags;
Elf64_Half vd_ndx;
Elf64_Half vd_cnt;
Elf64_Word vd_hash;
Elf64_Word vd_aux;
Elf64_Word vd_next;
} Elf64_Verdef;
typedef struct {
Elf64_Word vda_name;
Elf64_Word vda_next;
Elf64_Word vda_name;
Elf64_Word vda_next;
} Elf64_Verdaux;
typedef struct {
Elf64_Half vn_version;
Elf64_Half vn_cnt;
Elf64_Word vn_file;
Elf64_Word vn_aux;
Elf64_Word vn_next;
Elf64_Half vn_version;
Elf64_Half vn_cnt;
Elf64_Word vn_file;
Elf64_Word vn_aux;
Elf64_Word vn_next;
} Elf64_Verneed;
typedef struct {
Elf64_Word vna_hash;
Elf64_Half vna_flags;
Elf64_Half vna_other;
Elf64_Word vna_name;
Elf64_Word vna_next;
Elf64_Word vna_hash;
Elf64_Half vna_flags;
Elf64_Half vna_other;
Elf64_Word vna_name;
Elf64_Word vna_next;
} Elf64_Vernaux;
typedef Elf64_Half Elf64_Versym;
typedef struct {
Elf64_Half si_boundto; /* direct bindings - symbol bound to */
Elf64_Half si_flags; /* per symbol flags */
Elf64_Half si_boundto; /* direct bindings - symbol bound to */
Elf64_Half si_flags; /* per symbol flags */
} Elf64_Syminfo;
#endif /* !_SYS_ELF64_H_ */

View File

@@ -18,13 +18,13 @@
**/
Elf64_Shdr *
GetElf64SectionByIndex (
IN UINT8 *ImageBase,
IN UINT32 Index
IN UINT8 *ImageBase,
IN UINT32 Index
)
{
Elf64_Ehdr *Ehdr;
Elf64_Ehdr *Ehdr;
Ehdr = (Elf64_Ehdr *)ImageBase;
Ehdr = (Elf64_Ehdr *)ImageBase;
if (Index >= Ehdr->e_shnum) {
return NULL;
}
@@ -42,13 +42,13 @@ GetElf64SectionByIndex (
**/
Elf64_Phdr *
GetElf64SegmentByIndex (
IN UINT8 *ImageBase,
IN UINT32 Index
IN UINT8 *ImageBase,
IN UINT32 Index
)
{
Elf64_Ehdr *Ehdr;
Elf64_Ehdr *Ehdr;
Ehdr = (Elf64_Ehdr *)ImageBase;
Ehdr = (Elf64_Ehdr *)ImageBase;
if (Index >= Ehdr->e_phnum) {
return NULL;
}
@@ -67,24 +67,26 @@ GetElf64SegmentByIndex (
**/
Elf64_Shdr *
GetElf64SectionByRange (
IN UINT8 *ImageBase,
IN UINT64 Offset,
IN UINT64 Size
IN UINT8 *ImageBase,
IN UINT64 Offset,
IN UINT64 Size
)
{
UINT32 Index;
Elf64_Ehdr *Ehdr;
Elf64_Shdr *Shdr;
UINT32 Index;
Elf64_Ehdr *Ehdr;
Elf64_Shdr *Shdr;
Ehdr = (Elf64_Ehdr *)ImageBase;
Shdr = (Elf64_Shdr *) (ImageBase + Ehdr->e_shoff);
Shdr = (Elf64_Shdr *)(ImageBase + Ehdr->e_shoff);
for (Index = 0; Index < Ehdr->e_shnum; Index++) {
if ((Shdr->sh_offset == Offset) && (Shdr->sh_size == Size)) {
return Shdr;
}
Shdr = ELF_NEXT_ENTRY (Elf64_Shdr, Shdr, Ehdr->e_shentsize);
}
return NULL;
}
@@ -102,27 +104,28 @@ GetElf64SectionByRange (
**/
EFI_STATUS
ProcessRelocation64 (
IN Elf64_Rela *Rela,
IN UINT64 RelaSize,
IN UINT64 RelaEntrySize,
IN UINT64 RelaType,
IN INTN Delta,
IN BOOLEAN DynamicLinking
IN Elf64_Rela *Rela,
IN UINT64 RelaSize,
IN UINT64 RelaEntrySize,
IN UINT64 RelaType,
IN INTN Delta,
IN BOOLEAN DynamicLinking
)
{
UINTN Index;
UINT64 *Ptr;
UINT32 Type;
UINTN Index;
UINT64 *Ptr;
UINT32 Type;
for ( Index = 0
; MultU64x64 (RelaEntrySize, Index) < RelaSize
; Index++, Rela = ELF_NEXT_ENTRY (Elf64_Rela, Rela, RelaEntrySize)
) {
; MultU64x64 (RelaEntrySize, Index) < RelaSize
; Index++, Rela = ELF_NEXT_ENTRY (Elf64_Rela, Rela, RelaEntrySize)
)
{
//
// r_offset is the virtual address of the storage unit affected by the relocation.
//
Ptr = (UINT64 *)(UINTN)(Rela->r_offset + Delta);
Type = ELF64_R_TYPE(Rela->r_info);
Ptr = (UINT64 *)(UINTN)(Rela->r_offset + Delta);
Type = ELF64_R_TYPE (Rela->r_info);
switch (Type) {
case R_X86_64_NONE:
case R_X86_64_PC32:
@@ -142,6 +145,7 @@ ProcessRelocation64 (
} else {
*Ptr += Delta;
}
break;
case R_X86_64_32:
@@ -187,12 +191,14 @@ ProcessRelocation64 (
DEBUG ((DEBUG_INFO, "Unsupported relocation type %02X\n", Type));
ASSERT (FALSE);
}
break;
default:
DEBUG ((DEBUG_INFO, "Unsupported relocation type %02X\n", Type));
}
}
return EFI_SUCCESS;
}
@@ -206,19 +212,19 @@ ProcessRelocation64 (
**/
EFI_STATUS
RelocateElf64Dynamic (
IN ELF_IMAGE_CONTEXT *ElfCt
IN ELF_IMAGE_CONTEXT *ElfCt
)
{
UINT32 Index;
Elf64_Phdr *Phdr;
Elf64_Shdr *DynShdr;
Elf64_Shdr *RelShdr;
Elf64_Dyn *Dyn;
UINT64 RelaAddress;
UINT64 RelaCount;
UINT64 RelaSize;
UINT64 RelaEntrySize;
UINT64 RelaType;
UINT32 Index;
Elf64_Phdr *Phdr;
Elf64_Shdr *DynShdr;
Elf64_Shdr *RelShdr;
Elf64_Dyn *Dyn;
UINT64 RelaAddress;
UINT64 RelaCount;
UINT64 RelaSize;
UINT64 RelaEntrySize;
UINT64 RelaType;
//
// 1. Locate the dynamic section.
@@ -248,21 +254,23 @@ RelocateElf64Dynamic (
if (DynShdr == NULL) {
return EFI_UNSUPPORTED;
}
ASSERT (DynShdr->sh_type == SHT_DYNAMIC);
ASSERT (DynShdr->sh_entsize >= sizeof (*Dyn));
//
// 2. Locate the relocation section from the dynamic section.
//
RelaAddress = MAX_UINT64;
RelaAddress = MAX_UINT64;
RelaSize = 0;
RelaCount = 0;
RelaEntrySize = 0;
RelaType = 0;
for ( Index = 0, Dyn = (Elf64_Dyn *) (ElfCt->FileBase + DynShdr->sh_offset)
; Index < DivU64x64Remainder (DynShdr->sh_size, DynShdr->sh_entsize, NULL)
; Index++, Dyn = ELF_NEXT_ENTRY (Elf64_Dyn, Dyn, DynShdr->sh_entsize)
) {
for ( Index = 0, Dyn = (Elf64_Dyn *)(ElfCt->FileBase + DynShdr->sh_offset)
; Index < DivU64x64Remainder (DynShdr->sh_size, DynShdr->sh_entsize, NULL)
; Index++, Dyn = ELF_NEXT_ENTRY (Elf64_Dyn, Dyn, DynShdr->sh_entsize)
)
{
switch (Dyn->d_tag) {
case DT_RELA:
case DT_REL:
@@ -274,7 +282,7 @@ RelocateElf64Dynamic (
// For consistency, files do not contain relocation entries to ``correct'' addresses in the dynamic structure.
//
RelaAddress = Dyn->d_un.d_ptr;
RelaType = (Dyn->d_tag == DT_RELA) ? SHT_RELA: SHT_REL;
RelaType = (Dyn->d_tag == DT_RELA) ? SHT_RELA : SHT_REL;
break;
case DT_RELACOUNT:
case DT_RELCOUNT:
@@ -313,12 +321,14 @@ RelocateElf64Dynamic (
if ((RelShdr->sh_addr == RelaAddress) && (RelShdr->sh_size == RelaSize)) {
break;
}
RelShdr = NULL;
}
if (RelShdr == NULL) {
return EFI_UNSUPPORTED;
}
ASSERT (RelShdr->sh_type == RelaType);
ASSERT (RelShdr->sh_entsize == RelaEntrySize);
@@ -326,9 +336,11 @@ RelocateElf64Dynamic (
// 3. Process the relocation section.
//
ProcessRelocation64 (
(Elf64_Rela *) (ElfCt->FileBase + RelShdr->sh_offset),
RelShdr->sh_size, RelShdr->sh_entsize, RelShdr->sh_type,
(UINTN) ElfCt->ImageAddress - (UINTN) ElfCt->PreferredImageAddress,
(Elf64_Rela *)(ElfCt->FileBase + RelShdr->sh_offset),
RelShdr->sh_size,
RelShdr->sh_entsize,
RelShdr->sh_type,
(UINTN)ElfCt->ImageAddress - (UINTN)ElfCt->PreferredImageAddress,
TRUE
);
return EFI_SUCCESS;
@@ -344,22 +356,22 @@ RelocateElf64Dynamic (
**/
EFI_STATUS
RelocateElf64Sections (
IN ELF_IMAGE_CONTEXT *ElfCt
IN ELF_IMAGE_CONTEXT *ElfCt
)
{
EFI_STATUS Status;
Elf64_Ehdr *Ehdr;
Elf64_Shdr *RelShdr;
Elf64_Shdr *Shdr;
UINT32 Index;
UINTN Delta;
EFI_STATUS Status;
Elf64_Ehdr *Ehdr;
Elf64_Shdr *RelShdr;
Elf64_Shdr *Shdr;
UINT32 Index;
UINTN Delta;
Ehdr = (Elf64_Ehdr *)ElfCt->FileBase;
Ehdr = (Elf64_Ehdr *)ElfCt->FileBase;
if (Ehdr->e_machine != EM_X86_64) {
return EFI_UNSUPPORTED;
}
Delta = (UINTN) ElfCt->ImageAddress - (UINTN) ElfCt->PreferredImageAddress;
Delta = (UINTN)ElfCt->ImageAddress - (UINTN)ElfCt->PreferredImageAddress;
ElfCt->EntryPoint = (UINTN)(Ehdr->e_entry + Delta);
//
@@ -382,22 +394,27 @@ RelocateElf64Sections (
// The below relocation is needed in this case.
//
DEBUG ((DEBUG_INFO, "EXEC ELF: Fix actual/preferred base address delta ...\n"));
for ( Index = 0, RelShdr = (Elf64_Shdr *) (ElfCt->FileBase + Ehdr->e_shoff)
; Index < Ehdr->e_shnum
; Index++, RelShdr = ELF_NEXT_ENTRY (Elf64_Shdr, RelShdr, Ehdr->e_shentsize)
) {
for ( Index = 0, RelShdr = (Elf64_Shdr *)(ElfCt->FileBase + Ehdr->e_shoff)
; Index < Ehdr->e_shnum
; Index++, RelShdr = ELF_NEXT_ENTRY (Elf64_Shdr, RelShdr, Ehdr->e_shentsize)
)
{
if ((RelShdr->sh_type != SHT_REL) && (RelShdr->sh_type != SHT_RELA)) {
continue;
}
Shdr = GetElf64SectionByIndex (ElfCt->FileBase, RelShdr->sh_info);
if ((Shdr->sh_flags & SHF_ALLOC) == SHF_ALLOC) {
//
// Only fix up sections that occupy memory during process execution.
//
ProcessRelocation64 (
(Elf64_Rela *)((UINT8*)Ehdr + RelShdr->sh_offset),
RelShdr->sh_size, RelShdr->sh_entsize, RelShdr->sh_type,
Delta, FALSE
(Elf64_Rela *)((UINT8 *)Ehdr + RelShdr->sh_offset),
RelShdr->sh_size,
RelShdr->sh_entsize,
RelShdr->sh_type,
Delta,
FALSE
);
}
}
@@ -420,13 +437,13 @@ RelocateElf64Sections (
**/
EFI_STATUS
LoadElf64Image (
IN ELF_IMAGE_CONTEXT *ElfCt
IN ELF_IMAGE_CONTEXT *ElfCt
)
{
Elf64_Ehdr *Ehdr;
Elf64_Phdr *Phdr;
UINT16 Index;
UINTN Delta;
Elf64_Ehdr *Ehdr;
Elf64_Phdr *Phdr;
UINT16 Index;
UINTN Delta;
ASSERT (ElfCt != NULL);
@@ -436,14 +453,16 @@ LoadElf64Image (
Ehdr = (Elf64_Ehdr *)ElfCt->FileBase;
for ( Index = 0, Phdr = (Elf64_Phdr *)(ElfCt->FileBase + Ehdr->e_phoff)
; Index < Ehdr->e_phnum
; Index++, Phdr = ELF_NEXT_ENTRY (Elf64_Phdr, Phdr, Ehdr->e_phentsize)
) {
; Index < Ehdr->e_phnum
; Index++, Phdr = ELF_NEXT_ENTRY (Elf64_Phdr, Phdr, Ehdr->e_phentsize)
)
{
//
// Skip segments that don't require load (type tells, or size is 0)
//
if ((Phdr->p_type != PT_LOAD) ||
(Phdr->p_memsz == 0)) {
(Phdr->p_memsz == 0))
{
continue;
}
@@ -451,9 +470,9 @@ LoadElf64Image (
// The memory offset of segment relative to the image base
// Note: CopyMem() does nothing when the dst equals to src.
//
Delta = (UINTN) Phdr->p_paddr - (UINTN) ElfCt->PreferredImageAddress;
CopyMem (ElfCt->ImageAddress + Delta, ElfCt->FileBase + (UINTN) Phdr->p_offset, (UINTN) Phdr->p_filesz);
ZeroMem (ElfCt->ImageAddress + Delta + (UINTN) Phdr->p_filesz, (UINTN) (Phdr->p_memsz - Phdr->p_filesz));
Delta = (UINTN)Phdr->p_paddr - (UINTN)ElfCt->PreferredImageAddress;
CopyMem (ElfCt->ImageAddress + Delta, ElfCt->FileBase + (UINTN)Phdr->p_offset, (UINTN)Phdr->p_filesz);
ZeroMem (ElfCt->ImageAddress + Delta + (UINTN)Phdr->p_filesz, (UINTN)(Phdr->p_memsz - Phdr->p_filesz));
}
//

File diff suppressed because it is too large Load Diff

View File

@@ -18,11 +18,11 @@
**/
BOOLEAN
IsElfFormat (
IN CONST UINT8 *ImageBase
IN CONST UINT8 *ImageBase
)
{
Elf32_Ehdr *Elf32Hdr;
Elf64_Ehdr *Elf64Hdr;
Elf32_Ehdr *Elf32Hdr;
Elf64_Ehdr *Elf64Hdr;
ASSERT (ImageBase != NULL);
@@ -35,7 +35,8 @@ IsElfFormat (
(Elf32Hdr->e_ident[EI_MAG1] != ELFMAG1) ||
(Elf32Hdr->e_ident[EI_MAG1] != ELFMAG1) ||
(Elf32Hdr->e_ident[EI_MAG2] != ELFMAG2)
) {
)
{
return FALSE;
}
@@ -101,6 +102,7 @@ IsElfFormat (
return FALSE;
}
}
return TRUE;
}
@@ -116,17 +118,17 @@ IsElfFormat (
**/
EFI_STATUS
CalculateElfFileSize (
IN ELF_IMAGE_CONTEXT *ElfCt,
OUT UINTN *FileSize
IN ELF_IMAGE_CONTEXT *ElfCt,
OUT UINTN *FileSize
)
{
EFI_STATUS Status;
UINTN FileSize1;
UINTN FileSize2;
Elf32_Ehdr *Elf32Hdr;
Elf64_Ehdr *Elf64Hdr;
UINTN Offset;
UINTN Size;
EFI_STATUS Status;
UINTN FileSize1;
UINTN FileSize2;
Elf32_Ehdr *Elf32Hdr;
Elf64_Ehdr *Elf64Hdr;
UINTN Offset;
UINTN Size;
if ((ElfCt == NULL) || (FileSize == NULL)) {
return EFI_INVALID_PARAMETER;
@@ -134,22 +136,23 @@ CalculateElfFileSize (
// Use last section as end of file
Status = GetElfSectionPos (ElfCt, ElfCt->ShNum - 1, &Offset, &Size);
if (EFI_ERROR(Status)) {
if (EFI_ERROR (Status)) {
return EFI_UNSUPPORTED;
}
FileSize1 = Offset + Size;
// Use end of section header as end of file
FileSize2 = 0;
if (ElfCt->EiClass == ELFCLASS32) {
Elf32Hdr = (Elf32_Ehdr *)ElfCt->FileBase;
Elf32Hdr = (Elf32_Ehdr *)ElfCt->FileBase;
FileSize2 = Elf32Hdr->e_shoff + Elf32Hdr->e_shentsize * Elf32Hdr->e_shnum;
} else if (ElfCt->EiClass == ELFCLASS64) {
Elf64Hdr = (Elf64_Ehdr *)ElfCt->FileBase;
Elf64Hdr = (Elf64_Ehdr *)ElfCt->FileBase;
FileSize2 = (UINTN)(Elf64Hdr->e_shoff + Elf64Hdr->e_shentsize * Elf64Hdr->e_shnum);
}
*FileSize = MAX(FileSize1, FileSize2);
*FileSize = MAX (FileSize1, FileSize2);
return EFI_SUCCESS;
}
@@ -168,14 +171,14 @@ CalculateElfFileSize (
**/
EFI_STATUS
GetElfSegmentInfo (
IN UINT8 *ImageBase,
IN UINT32 EiClass,
IN UINT32 Index,
OUT SEGMENT_INFO *SegInfo
IN UINT8 *ImageBase,
IN UINT32 EiClass,
IN UINT32 Index,
OUT SEGMENT_INFO *SegInfo
)
{
Elf32_Phdr *Elf32Phdr;
Elf64_Phdr *Elf64Phdr;
Elf32_Phdr *Elf32Phdr;
Elf64_Phdr *Elf64Phdr;
if ((ImageBase == NULL) || (SegInfo == NULL)) {
return EFI_INVALID_PARAMETER;
@@ -184,22 +187,22 @@ GetElfSegmentInfo (
if (EiClass == ELFCLASS32) {
Elf32Phdr = GetElf32SegmentByIndex (ImageBase, Index);
if (Elf32Phdr != NULL) {
SegInfo->PtType = Elf32Phdr->p_type;
SegInfo->Offset = Elf32Phdr->p_offset;
SegInfo->Length = Elf32Phdr->p_filesz;
SegInfo->MemLen = Elf32Phdr->p_memsz;
SegInfo->MemAddr = Elf32Phdr->p_paddr;
SegInfo->PtType = Elf32Phdr->p_type;
SegInfo->Offset = Elf32Phdr->p_offset;
SegInfo->Length = Elf32Phdr->p_filesz;
SegInfo->MemLen = Elf32Phdr->p_memsz;
SegInfo->MemAddr = Elf32Phdr->p_paddr;
SegInfo->Alignment = Elf32Phdr->p_align;
return EFI_SUCCESS;
}
} else if (EiClass == ELFCLASS64) {
Elf64Phdr = GetElf64SegmentByIndex (ImageBase, Index);
if (Elf64Phdr != NULL) {
SegInfo->PtType = Elf64Phdr->p_type;
SegInfo->Offset = (UINTN)Elf64Phdr->p_offset;
SegInfo->Length = (UINTN)Elf64Phdr->p_filesz;
SegInfo->MemLen = (UINTN)Elf64Phdr->p_memsz;
SegInfo->MemAddr = (UINTN)Elf64Phdr->p_paddr;
SegInfo->PtType = Elf64Phdr->p_type;
SegInfo->Offset = (UINTN)Elf64Phdr->p_offset;
SegInfo->Length = (UINTN)Elf64Phdr->p_filesz;
SegInfo->MemLen = (UINTN)Elf64Phdr->p_memsz;
SegInfo->MemAddr = (UINTN)Elf64Phdr->p_paddr;
SegInfo->Alignment = (UINTN)Elf64Phdr->p_align;
return EFI_SUCCESS;
}
@@ -224,24 +227,25 @@ GetElfSegmentInfo (
EFI_STATUS
EFIAPI
ParseElfImage (
IN VOID *ImageBase,
OUT ELF_IMAGE_CONTEXT *ElfCt
IN VOID *ImageBase,
OUT ELF_IMAGE_CONTEXT *ElfCt
)
{
Elf32_Ehdr *Elf32Hdr;
Elf64_Ehdr *Elf64Hdr;
Elf32_Shdr *Elf32Shdr;
Elf64_Shdr *Elf64Shdr;
EFI_STATUS Status;
UINT32 Index;
SEGMENT_INFO SegInfo;
UINTN End;
UINTN Base;
Elf32_Ehdr *Elf32Hdr;
Elf64_Ehdr *Elf64Hdr;
Elf32_Shdr *Elf32Shdr;
Elf64_Shdr *Elf64Shdr;
EFI_STATUS Status;
UINT32 Index;
SEGMENT_INFO SegInfo;
UINTN End;
UINTN Base;
if (ElfCt == NULL) {
return EFI_INVALID_PARAMETER;
}
ZeroMem (ElfCt, sizeof(ELF_IMAGE_CONTEXT));
ZeroMem (ElfCt, sizeof (ELF_IMAGE_CONTEXT));
if (ImageBase == NULL) {
return (ElfCt->ParseStatus = EFI_INVALID_PARAMETER);
@@ -252,30 +256,34 @@ ParseElfImage (
return (ElfCt->ParseStatus = EFI_UNSUPPORTED);
}
Elf32Hdr = (Elf32_Ehdr *)ElfCt->FileBase;
Elf32Hdr = (Elf32_Ehdr *)ElfCt->FileBase;
ElfCt->EiClass = Elf32Hdr->e_ident[EI_CLASS];
if (ElfCt->EiClass == ELFCLASS32) {
if ((Elf32Hdr->e_type != ET_EXEC) && (Elf32Hdr->e_type != ET_DYN)) {
return (ElfCt->ParseStatus = EFI_UNSUPPORTED);
}
Elf32Shdr = (Elf32_Shdr *)GetElf32SectionByIndex (ElfCt->FileBase, Elf32Hdr->e_shstrndx);
if (Elf32Shdr == NULL) {
return (ElfCt->ParseStatus = EFI_UNSUPPORTED);
}
ElfCt->EntryPoint = (UINTN)Elf32Hdr->e_entry;
ElfCt->ShNum = Elf32Hdr->e_shnum;
ElfCt->PhNum = Elf32Hdr->e_phnum;
ElfCt->ShStrLen = Elf32Shdr->sh_size;
ElfCt->ShStrOff = Elf32Shdr->sh_offset;
} else {
Elf64Hdr = (Elf64_Ehdr *)Elf32Hdr;
Elf64Hdr = (Elf64_Ehdr *)Elf32Hdr;
if ((Elf64Hdr->e_type != ET_EXEC) && (Elf64Hdr->e_type != ET_DYN)) {
return (ElfCt->ParseStatus = EFI_UNSUPPORTED);
}
Elf64Shdr = (Elf64_Shdr *)GetElf64SectionByIndex (ElfCt->FileBase, Elf64Hdr->e_shstrndx);
if (Elf64Shdr == NULL) {
return (ElfCt->ParseStatus = EFI_UNSUPPORTED);
}
ElfCt->EntryPoint = (UINTN)Elf64Hdr->e_entry;
ElfCt->ShNum = Elf64Hdr->e_shnum;
ElfCt->PhNum = Elf64Hdr->e_phnum;
@@ -286,8 +294,8 @@ ParseElfImage (
//
// Get the preferred image base and required memory size when loaded to new location.
//
End = 0;
Base = MAX_UINT32;
End = 0;
Base = MAX_UINT32;
ElfCt->ReloadRequired = FALSE;
for (Index = 0; Index < ElfCt->PhNum; Index++) {
Status = GetElfSegmentInfo (ElfCt->FileBase, ElfCt->EiClass, Index, &SegInfo);
@@ -307,18 +315,20 @@ ParseElfImage (
if (Base > (SegInfo.MemAddr & ~(EFI_PAGE_SIZE - 1))) {
Base = SegInfo.MemAddr & ~(EFI_PAGE_SIZE - 1);
}
if (End < ALIGN_VALUE (SegInfo.MemAddr + SegInfo.MemLen, EFI_PAGE_SIZE) - 1) {
End = ALIGN_VALUE (SegInfo.MemAddr + SegInfo.MemLen, EFI_PAGE_SIZE) - 1;
}
}
//
// 0 - MAX_UINT32 + 1 equals to 0.
//
ElfCt->ImageSize = End - Base + 1;
ElfCt->PreferredImageAddress = (VOID *) Base;
ElfCt->PreferredImageAddress = (VOID *)Base;
CalculateElfFileSize (ElfCt, &ElfCt->FileSize);
return (ElfCt->ParseStatus = EFI_SUCCESS);;
return (ElfCt->ParseStatus = EFI_SUCCESS);
}
/**
@@ -339,10 +349,10 @@ ParseElfImage (
EFI_STATUS
EFIAPI
LoadElfImage (
IN ELF_IMAGE_CONTEXT *ElfCt
IN ELF_IMAGE_CONTEXT *ElfCt
)
{
EFI_STATUS Status;
EFI_STATUS Status;
if (ElfCt == NULL) {
return EFI_INVALID_PARAMETER;
@@ -366,7 +376,6 @@ LoadElfImage (
return Status;
}
/**
Get a ELF section name from its index.
@@ -381,14 +390,14 @@ LoadElfImage (
EFI_STATUS
EFIAPI
GetElfSectionName (
IN ELF_IMAGE_CONTEXT *ElfCt,
IN UINT32 SectionIndex,
OUT CHAR8 **SectionName
IN ELF_IMAGE_CONTEXT *ElfCt,
IN UINT32 SectionIndex,
OUT CHAR8 **SectionName
)
{
Elf32_Shdr *Elf32Shdr;
Elf64_Shdr *Elf64Shdr;
CHAR8 *Name;
Elf32_Shdr *Elf32Shdr;
Elf64_Shdr *Elf64Shdr;
CHAR8 *Name;
if ((ElfCt == NULL) || (SectionName == NULL)) {
return EFI_INVALID_PARAMETER;
@@ -419,7 +428,6 @@ GetElfSectionName (
return EFI_SUCCESS;
}
/**
Get the offset and size of x-th ELF section.
@@ -436,14 +444,14 @@ GetElfSectionName (
EFI_STATUS
EFIAPI
GetElfSectionPos (
IN ELF_IMAGE_CONTEXT *ElfCt,
IN UINT32 Index,
OUT UINTN *Offset,
OUT UINTN *Size
IN ELF_IMAGE_CONTEXT *ElfCt,
IN UINT32 Index,
OUT UINTN *Offset,
OUT UINTN *Size
)
{
Elf32_Shdr *Elf32Shdr;
Elf64_Shdr *Elf64Shdr;
Elf32_Shdr *Elf32Shdr;
Elf64_Shdr *Elf64Shdr;
if ((ElfCt == NULL) || (Offset == NULL) || (Size == NULL)) {
return EFI_INVALID_PARAMETER;

View File

@@ -20,7 +20,6 @@
#define ELF_NEXT_ENTRY(EntryType, Current, EntrySize) \
((EntryType *) ((UINT8 *)Current + EntrySize))
/**
Return the section header specified by Index.
@@ -31,8 +30,8 @@
**/
Elf32_Shdr *
GetElf32SectionByIndex (
IN UINT8 *ImageBase,
IN UINT32 Index
IN UINT8 *ImageBase,
IN UINT32 Index
);
/**
@@ -45,8 +44,8 @@ GetElf32SectionByIndex (
**/
Elf64_Shdr *
GetElf64SectionByIndex (
IN UINT8 *ImageBase,
IN UINT32 Index
IN UINT8 *ImageBase,
IN UINT32 Index
);
/**
@@ -59,8 +58,8 @@ GetElf64SectionByIndex (
**/
Elf32_Phdr *
GetElf32SegmentByIndex (
IN UINT8 *ImageBase,
IN UINT32 Index
IN UINT8 *ImageBase,
IN UINT32 Index
);
/**
@@ -73,8 +72,8 @@ GetElf32SegmentByIndex (
**/
Elf64_Phdr *
GetElf64SegmentByIndex (
IN UINT8 *ImageBase,
IN UINT32 Index
IN UINT8 *ImageBase,
IN UINT32 Index
);
/**
@@ -88,7 +87,7 @@ GetElf64SegmentByIndex (
**/
EFI_STATUS
LoadElf32Image (
IN ELF_IMAGE_CONTEXT *ElfCt
IN ELF_IMAGE_CONTEXT *ElfCt
);
/**
@@ -103,7 +102,7 @@ LoadElf32Image (
**/
EFI_STATUS
LoadElf64Image (
IN ELF_IMAGE_CONTEXT *ElfCt
IN ELF_IMAGE_CONTEXT *ElfCt
);
#endif