27 ASSERT (ImageBase !=
NULL);
34 if ((Elf32Hdr->e_ident[EI_MAG0] != ELFMAG0) ||
35 (Elf32Hdr->e_ident[EI_MAG1] != ELFMAG1) ||
36 (Elf32Hdr->e_ident[EI_MAG1] != ELFMAG1) ||
37 (Elf32Hdr->e_ident[EI_MAG2] != ELFMAG2)
46 if (Elf32Hdr->e_ident[EI_DATA] != ELFDATA2LSB) {
53 if (Elf32Hdr->e_ident[EI_CLASS] == ELFCLASS64) {
56 }
else if (Elf32Hdr->e_ident[EI_CLASS] == ELFCLASS32) {
62 if (Elf64Hdr !=
NULL) {
66 if (Elf64Hdr->e_machine != EM_X86_64) {
73 if ((Elf64Hdr->e_type != ET_EXEC) && (Elf64Hdr->e_type != ET_DYN)) {
80 if (Elf64Hdr->e_version != EV_CURRENT) {
87 if (Elf32Hdr->e_machine != EM_386) {
94 if ((Elf32Hdr->e_type != ET_EXEC) && (Elf32Hdr->e_type != ET_DYN)) {
101 if (Elf32Hdr->e_version != EV_CURRENT) {
133 if ((ElfCt ==
NULL) || (FileSize ==
NULL)) {
134 return EFI_INVALID_PARAMETER;
139 if (EFI_ERROR (Status)) {
140 return EFI_UNSUPPORTED;
143 FileSize1 = Offset + Size;
147 if (ElfCt->EiClass == ELFCLASS32) {
149 FileSize2 = Elf32Hdr->e_shoff + Elf32Hdr->e_shentsize * Elf32Hdr->e_shnum;
150 }
else if (ElfCt->EiClass == ELFCLASS64) {
152 FileSize2 = ((
UINTN)Elf64Hdr->e_shoff + (
UINTN)(Elf64Hdr->e_shentsize * Elf64Hdr->e_shnum));
155 *FileSize =
MAX (FileSize1, FileSize2);
183 if ((ImageBase ==
NULL) || (SegInfo ==
NULL)) {
184 return EFI_INVALID_PARAMETER;
187 if (EiClass == ELFCLASS32) {
189 if (Elf32Phdr !=
NULL) {
190 SegInfo->PtType = Elf32Phdr->p_type;
191 SegInfo->Offset = Elf32Phdr->p_offset;
192 SegInfo->Length = Elf32Phdr->p_filesz;
193 SegInfo->MemLen = Elf32Phdr->p_memsz;
194 SegInfo->MemAddr = Elf32Phdr->p_paddr;
195 SegInfo->Alignment = Elf32Phdr->p_align;
198 }
else if (EiClass == ELFCLASS64) {
200 if (Elf64Phdr !=
NULL) {
201 SegInfo->PtType = Elf64Phdr->p_type;
202 SegInfo->Offset = (
UINTN)Elf64Phdr->p_offset;
203 SegInfo->Length = (
UINTN)Elf64Phdr->p_filesz;
204 SegInfo->MemLen = (
UINTN)Elf64Phdr->p_memsz;
205 SegInfo->MemAddr = (
UINTN)Elf64Phdr->p_paddr;
206 SegInfo->Alignment = (
UINTN)Elf64Phdr->p_align;
211 return EFI_NOT_FOUND;
245 return EFI_INVALID_PARAMETER;
250 if (ImageBase ==
NULL) {
251 return (ElfCt->ParseStatus = EFI_INVALID_PARAMETER);
254 ElfCt->FileBase = (UINT8 *)ImageBase;
256 return (ElfCt->ParseStatus = EFI_UNSUPPORTED);
260 ElfCt->EiClass = Elf32Hdr->e_ident[EI_CLASS];
261 if (ElfCt->EiClass == ELFCLASS32) {
262 if ((Elf32Hdr->e_type != ET_EXEC) && (Elf32Hdr->e_type != ET_DYN)) {
263 return (ElfCt->ParseStatus = EFI_UNSUPPORTED);
267 if (Elf32Shdr ==
NULL) {
268 return (ElfCt->ParseStatus = EFI_UNSUPPORTED);
271 ElfCt->EntryPoint = (
UINTN)Elf32Hdr->e_entry;
272 ElfCt->ShNum = Elf32Hdr->e_shnum;
273 ElfCt->PhNum = Elf32Hdr->e_phnum;
274 ElfCt->ShStrLen = Elf32Shdr->sh_size;
275 ElfCt->ShStrOff = Elf32Shdr->sh_offset;
278 if ((Elf64Hdr->e_type != ET_EXEC) && (Elf64Hdr->e_type != ET_DYN)) {
279 return (ElfCt->ParseStatus = EFI_UNSUPPORTED);
283 if (Elf64Shdr ==
NULL) {
284 return (ElfCt->ParseStatus = EFI_UNSUPPORTED);
287 ElfCt->EntryPoint = (
UINTN)Elf64Hdr->e_entry;
288 ElfCt->ShNum = Elf64Hdr->e_shnum;
289 ElfCt->PhNum = Elf64Hdr->e_phnum;
290 ElfCt->ShStrLen = (UINT32)Elf64Shdr->sh_size;
291 ElfCt->ShStrOff = (UINT32)Elf64Shdr->sh_offset;
299 ElfCt->ReloadRequired =
FALSE;
300 for (Index = 0; Index < ElfCt->PhNum; Index++) {
304 if (SegInfo.PtType != PT_LOAD) {
308 if (SegInfo.MemLen != SegInfo.Length) {
312 ElfCt->ReloadRequired =
TRUE;
315 if (Base > (SegInfo.MemAddr & ~(EFI_PAGE_SIZE - 1))) {
316 Base = SegInfo.MemAddr & ~(EFI_PAGE_SIZE - 1);
319 if (End <
ALIGN_VALUE (SegInfo.MemAddr + SegInfo.MemLen, EFI_PAGE_SIZE) - 1) {
320 End =
ALIGN_VALUE (SegInfo.MemAddr + SegInfo.MemLen, EFI_PAGE_SIZE) - 1;
327 ElfCt->ImageSize = End - Base + 1;
328 ElfCt->PreferredImageAddress = (VOID *)Base;
358 return EFI_INVALID_PARAMETER;
361 if (EFI_ERROR (ElfCt->ParseStatus)) {
362 return ElfCt->ParseStatus;
365 if (ElfCt->ImageAddress ==
NULL) {
366 return EFI_INVALID_PARAMETER;
369 Status = EFI_UNSUPPORTED;
370 if (ElfCt->EiClass == ELFCLASS32) {
372 }
else if (ElfCt->EiClass == ELFCLASS64) {
394 IN UINT32 SectionIndex,
395 OUT CHAR8 **SectionName
402 if ((ElfCt ==
NULL) || (SectionName ==
NULL)) {
403 return EFI_INVALID_PARAMETER;
406 if (EFI_ERROR (ElfCt->ParseStatus)) {
407 return ElfCt->ParseStatus;
411 if (ElfCt->EiClass == ELFCLASS32) {
413 if ((Elf32Shdr !=
NULL) && (Elf32Shdr->sh_name < ElfCt->ShStrLen)) {
414 Name = (CHAR8 *)(ElfCt->FileBase + ElfCt->ShStrOff + Elf32Shdr->sh_name);
416 }
else if (ElfCt->EiClass == ELFCLASS64) {
418 if ((Elf64Shdr !=
NULL) && (Elf64Shdr->sh_name < ElfCt->ShStrLen)) {
419 Name = (CHAR8 *)(ElfCt->FileBase + ElfCt->ShStrOff + Elf64Shdr->sh_name);
424 return EFI_NOT_FOUND;
456 if ((ElfCt ==
NULL) || (Offset ==
NULL) || (Size ==
NULL)) {
457 return EFI_INVALID_PARAMETER;
460 if (EFI_ERROR (ElfCt->ParseStatus)) {
461 return ElfCt->ParseStatus;
464 if (ElfCt->EiClass == ELFCLASS32) {
466 if (Elf32Shdr !=
NULL) {
467 *Offset = (
UINTN)Elf32Shdr->sh_offset;
468 *Size = (
UINTN)Elf32Shdr->sh_size;
471 }
else if (ElfCt->EiClass == ELFCLASS64) {
473 if (Elf64Shdr !=
NULL) {
474 *Offset = (
UINTN)Elf64Shdr->sh_offset;
475 *Size = (
UINTN)Elf64Shdr->sh_size;
480 return EFI_NOT_FOUND;
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
Elf32_Phdr * GetElf32SegmentByIndex(IN UINT8 *ImageBase, IN UINT32 Index)
EFI_STATUS LoadElf32Image(IN ELF_IMAGE_CONTEXT *ElfCt)
Elf32_Shdr * GetElf32SectionByIndex(IN UINT8 *ImageBase, IN UINT32 Index)
Elf64_Shdr * GetElf64SectionByIndex(IN UINT8 *ImageBase, IN UINT32 Index)
EFI_STATUS LoadElf64Image(IN ELF_IMAGE_CONTEXT *ElfCt)
Elf64_Phdr * GetElf64SegmentByIndex(IN UINT8 *ImageBase, IN UINT32 Index)
EFI_STATUS EFIAPI LoadElfImage(IN ELF_IMAGE_CONTEXT *ElfCt)
EFI_STATUS EFIAPI GetElfSectionName(IN ELF_IMAGE_CONTEXT *ElfCt, IN UINT32 SectionIndex, OUT CHAR8 **SectionName)
EFI_STATUS GetElfSegmentInfo(IN UINT8 *ImageBase, IN UINT32 EiClass, IN UINT32 Index, OUT SEGMENT_INFO *SegInfo)
BOOLEAN IsElfFormat(IN CONST UINT8 *ImageBase)
EFI_STATUS CalculateElfFileSize(IN ELF_IMAGE_CONTEXT *ElfCt, OUT UINTN *FileSize)
EFI_STATUS EFIAPI GetElfSectionPos(IN ELF_IMAGE_CONTEXT *ElfCt, IN UINT32 Index, OUT UINTN *Offset, OUT UINTN *Size)
EFI_STATUS EFIAPI ParseElfImage(IN VOID *ImageBase, OUT ELF_IMAGE_CONTEXT *ElfCt)
#define ALIGN_VALUE(Value, Alignment)
#define ASSERT_EFI_ERROR(StatusParameter)