OSDN Git Service

(LibGoblin)
[drdeamon64/drdeamon64.git] / libgoblin / drd64_libgoblin_section.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_SECTION
38 #include"drd64_libgoblin.h"
39
40
41 /*----------------------------------------------------------------------
42 ----------------------------------------------------------------------*/
43 LIBGOBLIN_SECTION_EXTERN
44 LibGoblin_SectionInfo *
45         Section_GetSectionInfo(
46                         LibGoblin_BinaryInfo    *p_binfo,
47                         Byte    b_secid )
48 {
49         LibGoblin_SectionInfo   *p_secinfo;
50
51         assert( NULL != p_binfo );
52
53         if( LIBGOBLIN_SECTION_ID_MAX <= b_secid )       { return NULL; }
54
55         p_secinfo       = &(p_binfo->t_section[b_secid]);
56
57         return p_secinfo;
58 }
59
60
61 /*----------------------------------------------------------------------
62 ----------------------------------------------------------------------*/
63 LIBGOBLIN_SECTION_EXTERN
64 LibGoblin_SectionInfo *
65         Section_GetSectionInfo_fromBinaryFile(
66                         LibGoblin_BinaryFile    *p_bfile,
67                         Byte    b_secid )
68 {
69         LibGoblin_SectionInfo   *p_secinfo;
70
71         assert( NULL != p_bfile );
72
73         if( LIBGOBLIN_SECTION_ID_MAX <= b_secid )       { return NULL; }
74
75         p_secinfo       = &(p_bfile->t_section[b_secid]);
76
77         return p_secinfo;
78 }
79
80
81 /*----------------------------------------------------------------------
82 ----------------------------------------------------------------------*/
83 int
84         Section_UpdateSectionInfo_inBinaryInfo(
85                 LibGoblin_BinaryInfo    *p_binfo )
86 {
87         int                     i_cnt;
88         int                     i_globid;
89         LibGoblin_BinaryFile    *p_bfile;
90         LibGoblin_SectionInfo   *p_sectbl;
91         LibGoblin_SectionInfo   *p_globsec;
92
93         assert( NULL != p_binfo );
94         p_bfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
95
96         // Update Global SectionTable ---
97         for( i_cnt = 0; i_cnt < LIBGOBLIN_SECTION_ID_DEFAULT_MAX; i_cnt++ )     {
98                 p_globsec       = &(p_binfo->t_section[i_cnt]);
99                 p_sectbl        = &(p_bfile->t_section[i_cnt]);
100
101                 if( NULL == p_globsec->pb_sechdr )      {
102                         p_globsec->pb_sechdr            = p_sectbl->pb_sechdr;
103                         p_globsec->pb_data                      = p_sectbl->pb_data;
104                         p_globsec->pstr_secname         = p_sectbl->pstr_secname;
105                         p_globsec->qw_size                      = p_sectbl->qw_size;
106                         p_globsec->qw_flag                      = p_sectbl->qw_flag;
107                         p_globsec->qw_entsize           = p_sectbl->qw_entsize;
108                         p_globsec->i_binfile            = p_sectbl->i_binfile;
109                         p_globsec->i_objid                      = p_sectbl->i_objid;
110
111                         if( SHF_ALLOC & p_sectbl->qw_flag )
112                                 { p_globsec->ptr_addr.value     = p_sectbl->ptr_addr.value + p_binfo->ptr_loadbase; }
113                         else
114                                 { p_globsec->ptr_addr.value     = p_sectbl->ptr_addr.value; }
115                 }
116
117         }
118
119         p_globsec       = NULL;
120
121         for( i_cnt = LIBGOBLIN_SECTION_ID_DEFAULT_MAX;
122                                                 i_cnt < LIBGOBLIN_SECTION_ID_MAX; i_cnt++ )             {
123
124                 p_sectbl        = &(p_bfile->t_section[i_cnt]);
125                 if( NULL == p_sectbl->pb_sechdr )       { break; }
126
127                 for( i_globid = LIBGOBLIN_SECTION_ID_DEFAULT_MAX;
128                                                 i_globid < LIBGOBLIN_SECTION_ID_MAX; i_globid++ )               {
129                         
130                         p_globsec       = &(p_binfo->t_section[i_globid]);
131                         if( NULL == p_globsec->pb_sechdr )      { break; }
132
133                         if( !strncmp( p_globsec->pstr_secname,
134                                                                 p_sectbl->pstr_secname, 40 ))   { break; }
135                 }
136
137                 p_globsec->pb_sechdr            = p_sectbl->pb_sechdr;
138                 p_globsec->pb_data                      = p_sectbl->pb_data;
139                 p_globsec->pstr_secname         = p_sectbl->pstr_secname;
140                 p_globsec->qw_size                      = p_sectbl->qw_size;
141                 p_globsec->qw_flag                      = p_sectbl->qw_flag;
142                 p_globsec->qw_entsize           = p_sectbl->qw_entsize;
143                 p_globsec->i_binfile            = p_sectbl->i_binfile;
144                 p_globsec->i_objid                      = p_sectbl->i_objid;
145
146                 if( SHF_ALLOC & p_sectbl->qw_flag )
147                         { p_globsec->ptr_addr.value     = p_sectbl->ptr_addr.value + p_binfo->ptr_loadbase; }
148                 else
149                         { p_globsec->ptr_addr.value     = p_sectbl->ptr_addr.value; }
150         }
151
152         return 0x00;
153 }
154
155
156 /*----------------------------------------------------------------------
157 ----------------------------------------------------------------------*/
158 LIBGOBLIN_SECTION_EXTERN
159 int
160         Section_InsetObjectInfo_fromSection(
161                 LibGoblin_BinaryInfo    *p_binfo )
162 {
163         int                     i_cnt;
164         LibGoblin_SectionInfo   *p_section;
165         LibGoblin_ObjectInfo    *p_objinfo;
166         LibGoblin_ProgramInfo   *p_pginfo;
167         Elf64_Shdr                              *p_sechdr64;
168         Elf32_Shdr                              *p_sechdr32;
169
170         assert( NULL != p_binfo );
171         p_pginfo        = ProgInfo_GetProgInfo( p_binfo->i_pginfo );
172         assert( NULL != p_pginfo );
173
174         // Update Global SectionTable ---
175         for( i_cnt = 0; i_cnt < LIBGOBLIN_SECTION_ID_MAX; i_cnt++ )     {
176                 p_section       = &(p_binfo->t_section[i_cnt]);
177
178                 if( NULL == p_section->pb_sechdr )      { continue; }
179
180                 if( 0x00 == (SHF_ALLOC & p_section->qw_flag) )  { continue; }
181
182                 // sh_addr - Address in memory image.
183                 // sh_size - Size in bytes.
184                 p_objinfo       = ObjectInfo_InsetObject(
185                                                         p_pginfo, p_section->ptr_addr.value, p_section->qw_size, NULL,
186                                                         OBJINFO_INSETMODE_INSET | OBJINFO_INSETMODE_ADOPT );
187
188                 p_objinfo->b_type                       = OBJINFO_TYPE_SECTION;
189
190                 p_objinfo->file.i_binfile       = p_section->i_binfile;
191                 // sh_offset - Offset in file.
192                 p_objinfo->file.pv_offset       = (void *)p_section->pb_data;
193                 // sh_size - Size in bytes.
194                 p_objinfo->file.qw_size         = p_section->qw_size;
195
196                 // sh_name - Section name (index into the section header string table).
197                 p_objinfo->pstr_name            = p_section->pstr_secname;
198                 p_objinfo->dw_hash                      = Common_CalcDJBhash( p_section->pstr_secname );
199
200                 p_objinfo->info.section.pv_sechdr       = (void *)(p_section->pb_sechdr);
201
202                 p_section->i_objid                      = p_objinfo->i_id;
203                         
204                 if( IsBinfoType_Elf64( p_binfo ) )      {
205                         p_sechdr64      = (Elf64_Shdr *)(p_section->pb_sechdr);
206                         // sh_flags - Section flags.
207                         p_objinfo->info.section.qw_flag         = p_sechdr64->sh_flags;
208                         // sh_type - Section type.
209                         p_objinfo->info.section.dw_type         = p_sechdr64->sh_type;
210                         // sh_addralign - Alignment in bytes.
211                         p_objinfo->info.section.qw_align        = p_sechdr64->sh_addralign;
212                         // sh_link - Index of a related section.
213                         p_objinfo->info.section.dw_link         = p_sechdr64->sh_link;
214                         // sh_info - Depends on section type.
215                         p_objinfo->info.section.dw_info         = p_sechdr64->sh_info;
216                         // sh_entsize - Size of each entry in section.
217                         p_objinfo->info.section.qw_entsize      = p_sechdr64->sh_entsize;
218                 }
219                 else    {
220                         p_sechdr32      = (Elf32_Shdr *)(p_section->pb_sechdr);
221                         // sh_flags - Section flags.
222                         p_objinfo->info.section.qw_flag         = p_sechdr32->sh_flags;
223                         // sh_type - Section type.
224                         p_objinfo->info.section.dw_type         = p_sechdr32->sh_type;
225                         // sh_addralign - Alignment in bytes.
226                         p_objinfo->info.section.qw_align        = p_sechdr32->sh_addralign;
227                         // sh_link - Index of a related section.
228                         p_objinfo->info.section.dw_link         = p_sechdr32->sh_link;
229                         // sh_info - Depends on section type.
230                         p_objinfo->info.section.dw_info         = p_sechdr32->sh_info;
231                         // sh_entsize - Size of each entry in section.
232                         p_objinfo->info.section.qw_entsize      = p_sechdr32->sh_entsize;
233                 }
234         }
235
236         return 0x00;
237 }
238
239
240 /* EOF of drd64_.c ----------------------------------- */