OSDN Git Service

* [libintel64asm] Create Test Binary File.
[drdeamon64/drdeamon64.git] / libgoblin / drd64_libgoblin_elf_proghdr.c
1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
2
3                          D r . D e a m o n  6 4
4                         for INTEL64(R), AMD64(R)
5         
6    Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
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.
16
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.
28
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
30
31 /* File Info -----------------------------------------------------------
32 File: drd64_.c
33 Function: 
34 Comment: 
35 ----------------------------------------------------------------------*/
36
37 #define DRD64_SRC_LIBGOBLIN_ELF_PROGHDR
38 #include"drd64_libgoblin.h"
39
40
41 /*----------------------------------------------------------------------
42 ----------------------------------------------------------------------*/
43 LIBGOBLIN_ELF_PROGHDR_EXTERN
44 int
45         ELF_ProgramHeader_Read(
46                 LibGoblin_ProgramInfo   *p_pginfo,
47                 LibGoblin_BinaryInfo    *p_binfo )
48 {
49         int                                             i_cnt;
50         int                                             i_phdrnum;
51         int                                             i_phdrsize;
52         Byte                                    *pb_data;
53         PtrValue                                ptr_start;
54         PtrValue                                ptr_end;
55         Elf64_Ehdr                              *p_elfhdr;
56         Elf64_Phdr                              *p_proghdr;
57         LibGoblin_BinaryFile    *p_bfile;
58         LibGoblin_ObjectInfo    *p_objinfo;
59
60         assert( NULL != p_pginfo );
61         assert( NULL != p_binfo );
62
63         p_bfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
64         pb_data = p_bfile->pb_binary;
65         assert( NULL != pb_data );
66
67         p_elfhdr                = (Elf64_Ehdr *)pb_data;
68         i_phdrnum               = p_elfhdr->e_phnum;
69         i_phdrsize              = p_elfhdr->e_phentsize;
70
71         // Regist Program ObjectInfo *************************
72         p_proghdr               = (Elf64_Phdr *)(pb_data + p_elfhdr->e_phoff);
73         ptr_start               = 0xffffffffffffffff;
74         ptr_end                 = 0x0000000000000000;
75         
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));
78
79                 if(( 0x00000000 == p_proghdr->p_vaddr ) && ( 0 == p_proghdr->p_memsz ))
80                         { continue; }
81
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); }
86         }
87
88         p_objinfo       = ObjectInfo_InsetObject(
89                                                         p_pginfo, ptr_start, (ptr_end - ptr_start), NULL,
90                                                         OBJINFO_INSETMODE_PUT, OBJINFO_TYPE_PROGRAM, NULL );
91         if( NULL == p_objinfo ) {
92                 return -0x01;
93         }
94
95         p_objinfo->pstr_name    = p_binfo->str_filename;
96         p_objinfo->info.program.i_binfo_id      = p_binfo->i_id;
97
98
99         // Regist ProgramHeader to ObjectInfo *****************
100         p_proghdr               = (Elf64_Phdr *)(pb_data + p_elfhdr->e_phoff);
101         
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));
104
105                 if(( 0x00000000 == p_proghdr->p_vaddr ) && ( 0 == p_proghdr->p_memsz ))
106                         { continue; }
107
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, NULL );
116                 if( NULL == p_objinfo ) {
117                         return -0x02;
118                 }
119
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;
126
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;
135         }
136
137         return 0x00;
138 }
139
140
141 /* EOF of drd64_.c ----------------------------------- */