aboutsummaryrefslogtreecommitdiffstats
path: root/misc-host/ELF/ELF-loader-test/elfinfoprint.cpp
blob: d5dcbfda6966cbced962f3afe133b8014724189a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "elfinfoprint.h"

#include "bootloader_compat.h"


bool IsElf(Elf64_Ehdr * header)
{
  if(!header)
  {
    return false;
  }

  if(header->e_ident[EI_MAG0] != ELFMAG0) {
      return false;
    }
    if(header->e_ident[EI_MAG1] != ELFMAG1) {
      return false;
    }
    if(header->e_ident[EI_MAG2] != ELFMAG2) {
      return false;
    }
    if(header->e_ident[EI_MAG3] != ELFMAG3) {
      return false;
    }

    return true;
}

void ElfInfoPrint(Elf64_Ehdr * header)
{
  // check if actual elf file
  if(!IsElf(header))
  {
    Print(L"Not an ELF File\n");
    return;
  }

  if(header->e_type == ET_EXEC)
  {
    Print(L"Executable file\n");
  }

  if(header->e_machine == EM_X86_64)
  {
    Print(L"AMD64 Machine\n");
  } else
  {
    Print(L"Unrecorginzed Machine\n");
  }

  Print(L"Entry point: hx%x\n", header->e_entry);


  char * strtable = elf_str_table(header);
  Print(L"%s\n", strtable);
}

// functions to access section headers
Elf64_Shdr *elf_sheader(Elf64_Ehdr *hdr)
{
  return (Elf64_Shdr *)((uintptr_t)hdr + hdr->e_shoff);
}

Elf64_Shdr *elf_section(Elf64_Ehdr *hdr, int idx)
{
  return &elf_sheader(hdr)[idx];
}

char *elf_str_table(Elf64_Ehdr *hdr)
{
  if(hdr->e_shstrndx == SHN_UNDEF) return NULL;
  return (char *)hdr + elf_section(hdr, hdr->e_shstrndx)->sh_offset;
}