OSDN Git Service

2019/02/24(Sun) 18:41
[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 char *
45         LibGoblin_Section_GetSectionName(
46                         LibGoblin_ProgramInfo   *p_pginfo,
47                         Byte    b_secid )
48 {
49         LibGoblin_SectionInfo   *p_secinfo;
50
51         if( LIBGOBLIN_SECTION_ID_MAX <= b_secid )       { return NULL; }
52
53         p_secinfo       = &(p_pginfo->t_section[b_secid]);
54
55         return p_secinfo->pstr_secname;
56 }
57
58
59 /*----------------------------------------------------------------------
60 ----------------------------------------------------------------------*/
61 LIBGOBLIN_SECTION_EXTERN
62 LibGoblin_SectionInfo *
63         LibGoblin_Section_GetSectionInfo(
64                         LibGoblin_ProgramInfo   *p_pginfo,
65                         Byte    b_secid )
66 {
67         LibGoblin_SectionInfo   *p_secinfo;
68
69         if( LIBGOBLIN_SECTION_ID_MAX <= b_secid )       { return NULL; }
70
71         p_secinfo       = &(p_pginfo->t_section[b_secid]);
72
73         return p_secinfo;
74 }
75
76
77 /*----------------------------------------------------------------------
78 ----------------------------------------------------------------------*/
79 LIBGOBLIN_SECTION_EXTERN
80 LibGoblin_SectionInfo *
81         LibGoblin_Section_GetSectionInfo_fid(
82                         LibGoblin_BinaryInfo    *p_binfo,
83                         Byte    b_secid )
84 {
85         LibGoblin_BinaryFile    *p_bfile;
86         LibGoblin_SectionInfo   *p_secinfo;
87
88         assert( NULL != p_binfo );
89         p_bfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
90
91         if( LIBGOBLIN_SECTION_ID_MAX <= b_secid )       { return NULL; }
92
93         p_secinfo       = &(p_bfile->t_section[b_secid]);
94
95         return p_secinfo;
96 }
97
98
99 /*----------------------------------------------------------------------
100 ----------------------------------------------------------------------*/
101 LIBGOBLIN_SECTION_EXTERN
102 Word
103         LibGoblin_Section_GetSectionID_fromSectionNumber_inElf(
104                         LibGoblin_BinaryInfo    *p_binfo,
105                         Word    w_secnumber )
106 {
107         Byte    b_secid;
108         Word    w_result        = LIBGOBLIN_SECTION_ID_INVALID;
109         LibGoblin_BinaryFile    *p_bfile;
110         LibGoblin_SectionInfo   *p_secinfo;
111
112         assert( NULL != p_binfo );
113
114         // Through out input value when w_secnumber is not Section Index Value.
115         if( LIBGOBLIN_SECTION_ID_MAX <= w_secnumber )   {
116                 return w_secnumber;
117         }
118
119         p_bfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
120         
121         for( b_secid = 0; b_secid < LIBGOBLIN_SECTION_ID_MAX; b_secid++ )       {
122                 p_secinfo       = &(p_bfile->t_section[ b_secid ]);
123
124                 if( NULL == p_secinfo->pb_sechdr )              { continue; }
125
126                 if( p_secinfo->i_fid == (int)w_secnumber )      {
127                         w_result        = (Word)b_secid;
128                         break;
129                 }
130         }
131
132         return w_result;
133 }
134
135
136 /*----------------------------------------------------------------------
137 ----------------------------------------------------------------------*/
138 int
139         Section_UpdateSectionInfo_inProgInfo(
140                 LibGoblin_ProgramInfo   *p_pginfo,
141                 LibGoblin_BinaryInfo    *p_binfo )
142 {
143         int                     i_cnt;
144         int                     i_globid;
145         LibGoblin_BinaryFile    *p_bfile;
146         LibGoblin_SectionInfo   *p_sectbl;
147         LibGoblin_SectionInfo   *p_globsec;
148
149         assert( NULL != p_pginfo );
150         assert( NULL != p_binfo );
151         p_bfile = BinaryFile_GetBinaryFile( p_binfo->i_binfile );
152
153         // Update Global SectionTable ---
154         for( i_cnt = 0; i_cnt < LIBGOBLIN_SECTION_ID_DEFAULT_MAX; i_cnt++ )     {
155                 p_globsec       = &(p_pginfo->t_section[i_cnt]);
156                 p_sectbl        = &(p_bfile->t_section[i_cnt]);
157
158                 if( NULL == p_globsec->pb_sechdr )      {
159                         p_globsec->pb_sechdr            = p_sectbl->pb_sechdr;
160                         p_globsec->pb_data                      = p_sectbl->pb_data;
161                         p_globsec->pstr_secname         = p_sectbl->pstr_secname;
162                         p_globsec->qw_size                      = p_sectbl->qw_size;
163                         p_globsec->ptr_addr.value       = p_sectbl->ptr_addr.value;
164                         p_globsec->i_fid                        = p_sectbl->i_fid;
165                 }
166
167         }
168
169         p_globsec       = NULL;
170         
171         for( i_cnt = LIBGOBLIN_SECTION_ID_DEFAULT_MAX;
172                                                 i_cnt < LIBGOBLIN_SECTION_ID_MAX; i_cnt++ )             {
173
174                 p_sectbl        = &(p_bfile->t_section[i_cnt]);
175                 if( NULL == p_sectbl->pb_sechdr )       { break; }
176
177                 for( i_globid = LIBGOBLIN_SECTION_ID_DEFAULT_MAX;
178                                                 i_globid < LIBGOBLIN_SECTION_ID_MAX; i_globid++ )               {
179                         
180                         p_globsec       = &(p_pginfo->t_section[i_globid]);
181                         if( NULL == p_globsec->pb_sechdr )      { break; }
182
183                         if( !strncmp( p_globsec->pstr_secname,
184                                                                 p_sectbl->pstr_secname, 20 ))   { break; }
185                 }
186
187                 p_globsec->pb_sechdr            = p_sectbl->pb_sechdr;
188                 p_globsec->pb_data                      = p_sectbl->pb_data;
189                 p_globsec->pstr_secname         = p_sectbl->pstr_secname;
190                 p_globsec->qw_size                      = p_sectbl->qw_size;
191                 p_globsec->ptr_addr.value       = p_sectbl->ptr_addr.value;
192                 p_globsec->i_fid                        = p_sectbl->i_fid;
193         }
194
195         return 0x00;
196 }
197
198
199 /*----------------------------------------------------------------------
200 ----------------------------------------------------------------------*/
201 LIBGOBLIN_SECTION_EXTERN
202 int
203         Section_InsetObjectInfo_fromSection(
204                 LibGoblin_ProgramInfo   *p_pginfo )
205 {
206         int                     i_cnt;
207         LibGoblin_BinaryInfo    *p_binfo;
208         LibGoblin_SectionInfo   *p_section;
209         LibGoblin_ObjectInfo    *p_objinfo;
210         Elf64_Shdr                              *p_sechdr64;
211         Elf32_Shdr                              *p_sechdr32;
212
213         assert( NULL != p_pginfo );
214
215         // Update Global SectionTable ---
216         for( i_cnt = 0; i_cnt < LIBGOBLIN_SECTION_ID_MAX; i_cnt++ )     {
217                 p_section       = &(p_pginfo->t_section[i_cnt]);
218
219                 if( NULL == p_section->pb_sechdr )      { continue; }
220
221                 if( 0x00000000 == p_section->ptr_addr.value )   { continue; }
222
223                 p_binfo = p_pginfo->p_binfo[ ((0x00 > p_section->i_fid) ? 0x00 : (p_section->i_fid) ) ];
224                 assert( NULL != p_binfo );
225
226                 // sh_addr - Address in memory image.
227                 // sh_size - Size in bytes.
228                 p_objinfo       = ObjectInfo_InsetObject(
229                                                         p_pginfo, p_section->ptr_addr.value, p_section->qw_size, NULL, 0x01 );
230
231                 p_objinfo->b_type                       = OBJINFO_TYPE_SECTION;
232
233                 p_objinfo->file.i_fid           = p_section->i_fid;
234                 // sh_offset - Offset in file.
235                 p_objinfo->file.pv_offset       = (void *)p_section->pb_data;
236                 // sh_size - Size in bytes.
237                 p_objinfo->file.qw_size         = p_section->qw_size;
238
239                 // sh_name - Section name (index into the section header string table).
240                 p_objinfo->pstr_name            = p_section->pstr_secname;
241                 p_objinfo->info.section.pv_sechdr       = (void *)(p_section->pb_sechdr);
242                         
243                 if( IsBinfoType_Elf64( p_binfo ) )      {
244                         p_sechdr64      = (Elf64_Shdr *)(p_section->pb_sechdr);
245                         // sh_flags - Section flags.
246                         p_objinfo->info.section.qw_flag         = p_sechdr64->sh_flags;
247                         // sh_type - Section type.
248                         p_objinfo->info.section.dw_type         = p_sechdr64->sh_type;
249                         // sh_addralign - Alignment in bytes.
250                         p_objinfo->info.section.qw_align        = p_sechdr64->sh_addralign;
251                         // sh_link - Index of a related section.
252                         p_objinfo->info.section.dw_link         = p_sechdr64->sh_link;
253                         // sh_info - Depends on section type.
254                         p_objinfo->info.section.dw_info         = p_sechdr64->sh_info;
255                         // sh_entsize - Size of each entry in section.
256                         p_objinfo->info.section.qw_entsize      = p_sechdr64->sh_entsize;
257                 }
258                 else    {
259                         p_sechdr32      = (Elf32_Shdr *)(p_section->pb_sechdr);
260                         // sh_flags - Section flags.
261                         p_objinfo->info.section.qw_flag         = p_sechdr32->sh_flags;
262                         // sh_type - Section type.
263                         p_objinfo->info.section.dw_type         = p_sechdr32->sh_type;
264                         // sh_addralign - Alignment in bytes.
265                         p_objinfo->info.section.qw_align        = p_sechdr32->sh_addralign;
266                         // sh_link - Index of a related section.
267                         p_objinfo->info.section.dw_link         = p_sechdr32->sh_link;
268                         // sh_info - Depends on section type.
269                         p_objinfo->info.section.dw_info         = p_sechdr32->sh_info;
270                         // sh_entsize - Size of each entry in section.
271                         p_objinfo->info.section.qw_entsize      = p_sechdr32->sh_entsize;
272                 }
273         }
274
275
276         return 0x00;
277 }
278
279
280 /* EOF of drd64_.c ----------------------------------- */