00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <stdio.h>
00041 #if defined(BUILD_OS_DARWIN)
00042 #include <darwin_elf.h>
00043 #else
00044 #include <elf.h>
00045 #endif
00046 #include <sys/types.h>
00047 #include <sys/stat.h>
00048 #include <sys/mman.h>
00049 #include <fcntl.h>
00050
00051 #include "obj_info.h"
00052
00053 #include <stdlib.h>
00054 void
00055 error () {
00056 perror ("Error");
00057 exit (1);
00058 }
00059
00060 File_Info::File_Info (char *name)
00061 {
00062 int fd = open (name, O_RDONLY);
00063 struct stat stat_buf;
00064
00065 if (fstat (fd, &stat_buf) != 0)
00066 error ();
00067
00068 if (stat_buf.st_size == 0)
00069 error ();
00070
00071 file_size = stat_buf.st_size;
00072
00073 mmap_handle = mmap (0, file_size, PROT_READ, MAP_SHARED, fd, 0);
00074
00075 if (mmap_handle == MAP_FAILED)
00076 error ();
00077
00078 process_file ();
00079 }
00080
00081
00082 template <class HEADER, class SHDR>
00083 void
00084 File_Info::fill_file_info (HEADER *ehdr, SHDR *shdr)
00085 {
00086 shdr = (SHDR *) ((char *)mmap_handle + ehdr->e_shoff);
00087
00088 strtab = (char *) mmap_handle + shdr[ehdr->e_shstrndx].sh_offset;
00089
00090 shdr_end = shdr + ehdr->e_shnum;
00091
00092 while (shdr < shdr_end && (shdr->sh_type != SHT_PROGBITS ||
00093 (shdr->sh_flags & SHF_EXECINSTR) == 0))
00094 ++shdr;
00095
00096 shdr_begin = shdr;
00097
00098 }
00099
00100
00101 inline void
00102 File_Info::process_file ()
00103 {
00104 unsigned char *ident = (unsigned char *) mmap_handle;
00105 obj_class = ident[EI_CLASS];
00106 switch (obj_class) {
00107 case ELFCLASS32:
00108 fill_file_info ((Elf32_Ehdr *) mmap_handle, Shdr32_tag ());
00109 return;
00110 case ELFCLASS64:
00111 fill_file_info ((Elf64_Ehdr *) mmap_handle, Shdr64_tag ());
00112 return;
00113 default:
00114 error ();
00115 }
00116 }
00117