OSDN Git Service

2019/04/09(Tue) 20:00
[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                         if( SHF_ALLOC & p_sectbl->qw_flag )
110                                 { p_globsec->ptr_addr.value     = p_sectbl->ptr_addr.value + p_binfo->ptr_loadbase; }
111                         else
112                                 { p_globsec->ptr_addr.value     = p_sectbl->ptr_addr.value; }
113                 }
114
115         }
116
117         p_globsec       = NULL;
118
119         for( i_cnt = LIBGOBLIN_SECTION_ID_DEFAULT_MAX;
120                                                 i_cnt < LIBGOBLIN_SECTION_ID_MAX; i_cnt++ )             {
121
122                 p_sectbl        = &(p_bfile->t_section[i_cnt]);
123                 if( NULL == p_sectbl->pb_sechdr )       { break; }
124
125                 for( i_globid = LIBGOBLIN_SECTION_ID_DEFAULT_MAX;
126                                                 i_globid < LIBGOBLIN_SECTION_ID_MAX; i_globid++ )               {
127                         
128                         p_globsec       = &(p_binfo->t_section[i_globid]);
129                         if( NULL == p_globsec->pb_sechdr )      { break; }
130
131                         if( !strncmp( p_globsec->pstr_secname,
132                                                                 p_sectbl->pstr_secname, 40 ))   { break; }
133                 }
134
135                 p_globsec->pb_sechdr            = p_sectbl->pb_sechdr;
136                 p_globsec->pb_data                      = p_sectbl->pb_data;
137                 p_globsec->pstr_secname         = p_sectbl->pstr_secname;
138                 p_globsec->qw_size                      = p_sectbl->qw_size;
139                 p_globsec->qw_flag                      = p_sectbl->qw_flag;
140                 p_globsec->qw_entsize           = p_sectbl->qw_entsize;
141                 p_globsec->i_binfile            = p_sectbl->i_binfile;
142                 if( SHF_ALLOC & p_sectbl->qw_flag )
143                         { p_globsec->ptr_addr.value     = p_sectbl->ptr_addr.value + p_binfo->ptr_loadbase; }
144                 else
145                         { p_globsec->ptr_addr.value     = p_sectbl->ptr_addr.value; }
146         }
147
148         return 0x00;
149 }
150
151
152 /*----------------------------------------------------------------------
153 ----------------------------------------------------------------------*/
154 LIBGOBLIN_SECTION_EXTERN
155 int
156         Section_InsetObjectInfo_fromSection(
157                 LibGoblin_BinaryInfo    *p_binfo )
158 {
159         int                     i_cnt;
160         LibGoblin_SectionInfo   *p_section;
161         LibGoblin_ObjectInfo    *p_objinfo;
162         LibGoblin_ProgramInfo   *p_pginfo;
163         Elf64_Shdr                              *p_sechdr64;
164         Elf32_Shdr                              *p_sechdr32;
165
166         assert( NULL != p_binfo );
167         p_pginfo        = ProgInfo_GetProgInfo( p_binfo->i_pginfo );
168         assert( NULL != p_pginfo );
169
170         // Update Global SectionTable ---
171         for( i_cnt = 0; i_cnt < LIBGOBLIN_SECTION_ID_MAX; i_cnt++ )     {
172                 p_section       = &(p_binfo->t_section[i_cnt]);
173
174                 if( NULL == p_section->pb_sechdr )      { continue; }
175
176                 if( 0x00 == (SHF_ALLOC & p_section->qw_flag) )  { continue; }
177
178                 // sh_addr - Address in memory image.
179                 // sh_size - Size in bytes.
180                 p_objinfo       = ObjectInfo_InsetObject(
181                                                         p_pginfo, p_section->ptr_addr.value, p_section->qw_size, NULL,
182                                                         OBJINFO_INSETMODE_INSET | OBJINFO_INSETMODE_ADOPT );
183
184                 p_objinfo->b_type                       = OBJINFO_TYPE_SECTION;
185
186                 p_objinfo->file.i_binfile       = p_section->i_binfile;
187                 // sh_offset - Offset in file.
188                 p_objinfo->file.pv_offset       = (void *)p_section->pb_data;
189                 // sh_size - Size in bytes.
190                 p_objinfo->file.qw_size         = p_section->qw_size;
191
192                 // sh_name - Section name (index into the section header string table).
193                 p_objinfo->pstr_name            = p_section->pstr_secname;
194                 p_objinfo->dw_hash                      = Common_CalcDJBhash( p_section->pstr_secname );
195
196                 p_objinfo->info.section.pv_sechdr       = (void *)(p_section->pb_sechdr);
197                         
198                 if( IsBinfoType_Elf64( p_binfo ) )      {
199                         p_sechdr64      = (Elf64_Shdr *)(p_section->pb_sechdr);
200                         // sh_flags - Section flags.
201                         p_objinfo->info.section.qw_flag         = p_sechdr64->sh_flags;
202                         // sh_type - Section type.
203                         p_objinfo->info.section.dw_type         = p_sechdr64->sh_type;
204                         // sh_addralign - Alignment in bytes.
205                         p_objinfo->info.section.qw_align        = p_sechdr64->sh_addralign;
206                         // sh_link - Index of a related section.
207                         p_objinfo->info.section.dw_link         = p_sechdr64->sh_link;
208                         // sh_info - Depends on section type.
209                         p_objinfo->info.section.dw_info         = p_sechdr64->sh_info;
210                         // sh_entsize - Size of each entry in section.
211                         p_objinfo->info.section.qw_entsize      = p_sechdr64->sh_entsize;
212                 }
213                 else    {
214                         p_sechdr32      = (Elf32_Shdr *)(p_section->pb_sechdr);
215                         // sh_flags - Section flags.
216                         p_objinfo->info.section.qw_flag         = p_sechdr32->sh_flags;
217                         // sh_type - Section type.
218                         p_objinfo->info.section.dw_type         = p_sechdr32->sh_type;
219                         // sh_addralign - Alignment in bytes.
220                         p_objinfo->info.section.qw_align        = p_sechdr32->sh_addralign;
221                         // sh_link - Index of a related section.
222                         p_objinfo->info.section.dw_link         = p_sechdr32->sh_link;
223                         // sh_info - Depends on section type.
224                         p_objinfo->info.section.dw_info         = p_sechdr32->sh_info;
225                         // sh_entsize - Size of each entry in section.
226                         p_objinfo->info.section.qw_entsize      = p_sechdr32->sh_entsize;
227                 }
228         }
229
230         return 0x00;
231 }
232
233
234 /* EOF of drd64_.c ----------------------------------- */