1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
4 for INTEL64(R), AMD64(R)
6 Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
11 1. Redistributions of source code must retain the above copyright notice,
12 this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 OF THE POSSIBILITY OF SUCH DAMAGE.
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
31 /* File Info -----------------------------------------------------------
35 ----------------------------------------------------------------------*/
37 #define DRD64_SRC_LIBGOBLIN_ELF_PROGHDR
38 #include"drd64_libgoblin.h"
41 /*----------------------------------------------------------------------
42 ----------------------------------------------------------------------*/
43 LIBGOBLIN_ELF_PROGHDR_EXTERN
45 ELF_ProgramHeader_Read(
46 LibGoblin_ProgramInfo *p_pginfo,
47 LibGoblin_BinaryInfo *p_binfo )
56 Elf64_Phdr *p_proghdr;
57 LibGoblin_BinaryFile *p_bfile;
58 LibGoblin_ObjectInfo *p_objinfo;
60 assert( NULL != p_pginfo );
61 assert( NULL != p_binfo );
63 p_bfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
64 pb_data = p_bfile->pb_binary;
65 assert( NULL != pb_data );
67 p_elfhdr = (Elf64_Ehdr *)pb_data;
68 i_phdrnum = p_elfhdr->e_phnum;
69 i_phdrsize = p_elfhdr->e_phentsize;
71 // Regist Program ObjectInfo *************************
72 p_proghdr = (Elf64_Phdr *)(pb_data + p_elfhdr->e_phoff);
73 ptr_start = 0xffffffffffffffff;
74 ptr_end = 0x0000000000000000;
76 for( i_cnt = 0; i_cnt < i_phdrnum; i_cnt++ ) {
77 p_proghdr = (Elf64_Phdr *)(pb_data + p_elfhdr->e_phoff + (i_cnt * i_phdrsize));
79 if(( 0x00000000 == p_proghdr->p_vaddr ) && ( 0 == p_proghdr->p_memsz ))
82 if( ptr_start > ((PtrValue)p_proghdr->p_vaddr + p_binfo->ptr_loadbase) )
83 { ptr_start = (PtrValue)p_proghdr->p_vaddr + p_binfo->ptr_loadbase; }
84 if( ptr_end < ((PtrValue)p_proghdr->p_vaddr + p_binfo->ptr_loadbase + p_proghdr->p_memsz) )
85 { ptr_end = ((PtrValue)p_proghdr->p_vaddr + p_binfo->ptr_loadbase + p_proghdr->p_memsz); }
88 p_objinfo = ObjectInfo_InsetObject(
89 p_pginfo, ptr_start, (ptr_end - ptr_start), NULL,
90 OBJINFO_INSETMODE_PUT, OBJINFO_TYPE_PROGRAM );
91 if( NULL == p_objinfo ) {
95 p_objinfo->pstr_name = p_binfo->str_filename;
96 p_objinfo->info.program.i_binfo_id = p_binfo->i_id;
99 // Regist ProgramHeader to ObjectInfo *****************
100 p_proghdr = (Elf64_Phdr *)(pb_data + p_elfhdr->e_phoff);
102 for( i_cnt = 0; i_cnt < i_phdrnum; i_cnt++ ) {
103 p_proghdr = (Elf64_Phdr *)(pb_data + p_elfhdr->e_phoff + (i_cnt * i_phdrsize));
105 if(( 0x00000000 == p_proghdr->p_vaddr ) && ( 0 == p_proghdr->p_memsz ))
108 // Inset ObjectInfo ---
109 // p_vaddr - Virtual address in memory image.
110 // p_memsz - Size of contents in memory.
111 p_objinfo = ObjectInfo_InsetObject(
112 p_pginfo, ((PtrValue)p_proghdr->p_vaddr + p_binfo->ptr_loadbase),
113 p_proghdr->p_memsz, NULL,
114 OBJINFO_INSETMODE_INSET | OBJINFO_INSETMODE_ADOPT,
115 OBJINFO_TYPE_PROGHDR );
116 if( NULL == p_objinfo ) {
120 // Set ProgramHdr Value to ObjectInfo ---
121 p_objinfo->file.i_binfile = p_binfo->i_binfile;
122 // p_offset - File offset of contents.
123 p_objinfo->file.pv_offset = (void *)(pb_data + p_proghdr->p_offset);
124 // p_filesz - Size of contents in file.
125 p_objinfo->file.qw_size = p_proghdr->p_filesz;
127 // p_type - Entry type.
128 p_objinfo->info.proghdr.dw_type = p_proghdr->p_type;
129 // p_flags - Access permission flags.
130 p_objinfo->info.proghdr.dw_flags = p_proghdr->p_flags;
131 // p_align - Alignment in memory and file.
132 p_objinfo->info.proghdr.qw_align = p_proghdr->p_align;
133 // ELF Program Header
134 p_objinfo->info.proghdr.pv_proghdr = (void *)p_proghdr;
141 /* EOF of drd64_.c ----------------------------------- */