28 if (Index >= Ehdr->e_shnum) {
32 return (
Elf32_Shdr *)(ImageBase + Ehdr->e_shoff + Index * Ehdr->e_shentsize);
52 if (Index >= Ehdr->e_phnum) {
56 return (
Elf32_Phdr *)(ImageBase + Ehdr->e_phoff + Index * Ehdr->e_phentsize);
81 Shdr = (
Elf32_Shdr *)(ImageBase + Ehdr->e_shoff);
82 for (Index = 0; Index < Ehdr->e_shnum; Index++) {
83 if ((Shdr->sh_offset == Offset) && (Shdr->sh_size == Size)) {
87 Shdr = ELF_NEXT_ENTRY (
Elf32_Shdr, Shdr, Ehdr->e_shentsize);
109 IN UINT32 RelaEntrySize,
112 IN BOOLEAN DynamicLinking
120 ; RelaEntrySize * Index < RelaSize
121 ; Index++, Rela = ELF_NEXT_ENTRY (
Elf32_Rela, Rela, RelaEntrySize)
127 Ptr = (UINT32 *)(
UINTN)(Rela->r_offset + Delta);
128 Type = ELF32_R_TYPE (Rela->r_info);
138 if (DynamicLinking) {
142 DEBUG ((DEBUG_INFO,
"Unsupported relocation type %02X\n", Type));
145 *Ptr += (UINT32)Delta;
151 if (DynamicLinking) {
170 if (RelaType == SHT_RELA) {
171 *Ptr = (UINT32)Delta + Rela->r_addend;
176 *Ptr = (UINT32)Delta + *Ptr;
182 DEBUG ((DEBUG_INFO,
"Unsupported relocation type %02X\n", Type));
189 DEBUG ((DEBUG_INFO,
"Unsupported relocation type %02X\n", Type));
217 UINT32 RelaEntrySize;
229 for (Index = 0; Index < ElfCt->PhNum; Index++) {
231 ASSERT (Phdr !=
NULL);
232 if (Phdr->p_type == PT_DYNAMIC) {
244 ASSERT (DynShdr !=
NULL);
245 if (DynShdr ==
NULL) {
246 return EFI_UNSUPPORTED;
249 ASSERT (DynShdr->sh_type == SHT_DYNAMIC);
250 ASSERT (DynShdr->sh_entsize >= sizeof (*Dyn));
255 RelaAddress = MAX_UINT32;
260 for ( Index = 0, Dyn = (
Elf32_Dyn *)(ElfCt->FileBase + DynShdr->sh_offset)
261 ; Index < DynShdr->sh_size / DynShdr->sh_entsize
262 ; Index++, Dyn = ELF_NEXT_ENTRY (
Elf32_Dyn, Dyn, DynShdr->sh_entsize)
265 switch (Dyn->d_tag) {
275 RelaAddress = Dyn->d_un.d_ptr;
276 RelaType = (Dyn->d_tag == DT_RELA) ? SHT_RELA : SHT_REL;
280 RelaCount = Dyn->d_un.d_val;
284 RelaEntrySize = Dyn->d_un.d_val;
288 RelaSize = Dyn->d_un.d_val;
295 if (RelaAddress == MAX_UINT32) {
296 ASSERT (RelaCount == 0);
297 ASSERT (RelaEntrySize == 0);
298 ASSERT (RelaSize == 0);
309 for (Index = 0; Index < ElfCt->ShNum; Index++) {
311 ASSERT (RelShdr !=
NULL);
312 if ((RelShdr->sh_addr == RelaAddress) && (RelShdr->sh_size == RelaSize)) {
319 if (RelShdr ==
NULL) {
320 return EFI_UNSUPPORTED;
323 ASSERT (RelShdr->sh_type == RelaType);
324 ASSERT (RelShdr->sh_entsize == RelaEntrySize);
330 (
Elf32_Rela *)(ElfCt->FileBase + RelShdr->sh_offset),
334 (
UINTN)ElfCt->ImageAddress - (
UINTN)ElfCt->PreferredImageAddress,
361 if (Ehdr->e_machine != EM_386) {
362 return EFI_UNSUPPORTED;
365 Delta = (
UINTN)ElfCt->ImageAddress - (
UINTN)ElfCt->PreferredImageAddress;
366 ElfCt->EntryPoint = (
UINTN)(Ehdr->e_entry + Delta);
371 if (Ehdr->e_type == ET_DYN) {
372 DEBUG ((DEBUG_INFO,
"DYN ELF: Relocate using dynamic sections...\n"));
387 DEBUG ((DEBUG_INFO,
"EXEC ELF: Fix actual/preferred base address delta ...\n"));
388 for ( Index = 0, RelShdr = (
Elf32_Shdr *)(ElfCt->FileBase + Ehdr->e_shoff)
389 ; Index < Ehdr->e_shnum
390 ; Index++, RelShdr = ELF_NEXT_ENTRY (
Elf32_Shdr, RelShdr, Ehdr->e_shentsize)
393 if ((RelShdr->sh_type != SHT_REL) && (RelShdr->sh_type != SHT_RELA)) {
398 if ((Shdr->sh_flags & SHF_ALLOC) == SHF_ALLOC) {
403 (
Elf32_Rela *)((UINT8 *)Ehdr + RelShdr->sh_offset),
439 ASSERT (ElfCt !=
NULL);
446 for ( Index = 0, Phdr = (
Elf32_Phdr *)(ElfCt->FileBase + Ehdr->e_phoff)
447 ; Index < Ehdr->e_phnum
448 ; Index++, Phdr = ELF_NEXT_ENTRY (
Elf32_Phdr, Phdr, Ehdr->e_phentsize)
454 if ((Phdr->p_type != PT_LOAD) ||
455 (Phdr->p_memsz == 0))
464 Delta = Phdr->p_paddr - (UINT32)(
UINTN)ElfCt->PreferredImageAddress;
465 CopyMem (ElfCt->ImageAddress + Delta, ElfCt->FileBase + Phdr->p_offset, Phdr->p_filesz);
466 ZeroMem (ElfCt->ImageAddress + Delta + Phdr->p_filesz, Phdr->p_memsz - Phdr->p_filesz);
472 if (ElfCt->ImageAddress != ElfCt->PreferredImageAddress) {
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
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)
EFI_STATUS ProcessRelocation32(IN Elf32_Rela *Rela, IN UINT32 RelaSize, IN UINT32 RelaEntrySize, IN UINT32 RelaType, IN INTN Delta, IN BOOLEAN DynamicLinking)
EFI_STATUS RelocateElf32Sections(IN ELF_IMAGE_CONTEXT *ElfCt)
EFI_STATUS RelocateElf32Dynamic(IN ELF_IMAGE_CONTEXT *ElfCt)
Elf32_Shdr * GetElf32SectionByRange(IN UINT8 *ImageBase, IN UINT32 Offset, IN UINT32 Size)
Elf32_Shdr * GetElf32SectionByIndex(IN UINT8 *ImageBase, IN UINT32 Index)
#define ASSERT_EFI_ERROR(StatusParameter)
#define DEBUG(Expression)