OSDN Git Service

2019/04/09(Tue) 20:00
[drdeamon64/drdeamon64.git] / libgoblin / drd64_libgoblin_dwarf.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_DWARF
38 #include"drd64_libgoblin.h"
39
40 /*----------------------------------------------------------------------
41 ----------------------------------------------------------------------*/
42 LIBGOBLIN_DWARF_EXTERN
43 int
44         LibGoblin_IsDwarf(
45                 LibGoblin_BinaryInfo    *p_bin )
46 {
47         Byte    *pb_data_abbrev;
48         QWord   qw_size_abbrev;
49
50         assert( NULL != p_bin );
51
52         /* Get .debug_abbrev section Info */
53         pb_data_abbrev  = ELF_GetSection(
54                                                 &qw_size_abbrev, p_bin,
55                                                 LIBGOBLIN_SECTION_ID_DEBUG_ABBREV );
56         if( NULL == pb_data_abbrev )    { return 0x00; }
57
58         return 0x01;
59 }
60
61
62 /*----------------------------------------------------------------------
63 ----------------------------------------------------------------------*/
64 LIBGOBLIN_DWARF_EXTERN
65 int
66         LibGoblin_FreeDwarfInfo(
67                 LibGoblin_BinaryInfo    *p_bin )
68 {
69         LibGoblin_Dwarf_AbbrevInfo              *p_abbrev;
70         LibGoblin_Dwarf_SourceFile              *p_src;
71         LibGoblin_Debug_Dwarf                   *p_dwarf;
72         
73         assert( NULL != p_bin );
74
75         if( NULL == p_bin->p_debug )    { return 0x00; }
76         p_dwarf = (LibGoblin_Debug_Dwarf *)p_bin->p_debug;
77
78         p_src   = p_dwarf->p_src;
79         p_abbrev        = p_src->p_abbrev_info;
80         free( p_abbrev );
81         free( p_src );
82         p_dwarf->p_src  = NULL;
83
84         free( p_bin->p_debug );
85         p_bin->p_debug  = NULL;
86
87
88         return 0x00;
89 }
90
91
92 /*----------------------------------------------------------------------
93 ----------------------------------------------------------------------*/
94 LIBGOBLIN_DWARF_EXTERN
95 int
96         LibGoblin_AnalyzeDwarf(
97                 LibGoblin_BinaryInfo    *p_bin )
98 {
99         int             i_srcfilenum;
100         int             i_abbrevs;
101         int             i_err;
102         LibGoblin_Debug_Dwarf   *p_dwarf;
103         LibGoblin_Dwarf_SourceFile      *p_src;
104
105         assert( NULL != p_bin );
106
107         /* Alloc Debug-Info(Dwarf) Struct Memory */
108         p_dwarf = (LibGoblin_Debug_Dwarf *)malloc( sizeof(LibGoblin_Debug_Dwarf) );
109         if( NULL == p_dwarf )   { return 0x01; }
110         memset( p_dwarf, 0x00, sizeof(LibGoblin_Debug_Dwarf) );
111         p_bin->p_debug  = (void *)p_dwarf;
112
113         /* Get Source File Nums & Abbrev. Sturcture Nums */
114         i_err   = LibGoblin_DwarfAbbrev_GetItems(
115                                         &i_srcfilenum, &i_abbrevs, p_bin );
116         if(( 0x00 != i_err ) || ( 0 == i_srcfilenum ))  { return 0x02; }
117
118         /* Alloc SourceFile Debug-Info Struct. Memory */
119         p_src   = (LibGoblin_Dwarf_SourceFile *)malloc(
120                                                 sizeof( LibGoblin_Dwarf_SourceFile) * i_srcfilenum );
121         if( NULL == p_src )             { return 0x03; }
122         memset( p_src, 0x00, sizeof(LibGoblin_Dwarf_SourceFile) * i_srcfilenum );
123         p_dwarf->p_src                  = p_src;
124         p_dwarf->i_srcfilenum   = i_srcfilenum;
125         
126         /* Read .debug_abbrev section */
127         LibGoblin_DwarfAbbrev_Read( p_bin, i_abbrevs );
128
129         /* Read .debug_abbrev section */
130         LibGoblin_DwarfInfo_Analyze( p_bin );
131
132         /* Read .debug_line section */
133         LibGoblin_DwarfLine_Analyze( p_bin );
134         
135         puts("OK");
136         
137
138         return 0x00;
139 }
140
141
142 /* EOF of drd64_.c ----------------------------------- */