C++関数署名解析

67318 ワード

2013-11-29実際にはこんなに煩わしいロックは違います.objdump-Cまたはc++file sourcefileを直接使えばいいです.
 
 
orgmangle
優先順位
 1 #! /bin/sh                                                                                                         

 2 

 3 elfname=$1

 4 objname=$2

 5 targetname=""

 6 

 7 thispid=$$

 8 

 9 filename=/tmp/jerry.${thispid}

10 

11 rm -f $filename

12 

13 case $elfname in

14     *.[sS]) 

15         targetname=$elfname;

16         ;;      

17     *)

18         objdump -D -z $elfname > ${elfname}.s;

19         targetname=${elfname}.s;

20         ;;      

21     esac    

22 

23 sed -n -e '/'${objname}'/p' $targetname | awk '{print $NF}' | sed -n -e 's/<\(.*\)>\(.*\)/\1/p' | sort -d | uniq >> $filename

24 

25 for line in `sed -n -e 'p' $filename`

26 do

27     dename=`c++filt $line`

28     sed -i -e 's/'"${line}"'/'"${line}"'\t'"${dename}"'/' ${targetname} 

29 done

30 

31 rm -f $filename

vtable再構築、次のスクリプトの使用
 1 #! /bin/bash                                                                                                       

 2 

 3 filename=$1

 4 startline=$2

 5 endline=$3

 6 

 7 #vtable_parse         ,    

 8 temp=`./vtable_parse $filename $startline $endline`

 9 

10 mv $temp $filename
 
   
1 //Makefile

2 PPFLAGS = -I./include                                                                                             

3 

4 vtable_parse: vtable.cpp vfile.cpp basic_info.cpp

5     g++ $(CPPFLAGS) -g $^ -o $@
 
   
 
   
 1 // ./include/basic_info.h

 2 #ifndef __JERRY_BASIC_INFO

 3 #define __JERRY_BASIC_INFO

 4 

 5 

 6 #include <iostream>

 7 #include <fstream>                                                                                                 

 8 #include <unistd.h>

 9 #include <sys/types.h>

10 #include <string.h>

11 #include <stdlib.h>

12 

13 using namespace std;

14 

15 class BASIC_INFO

16 {

17     private:

18         char *strpid;

19         char *infilename;

20         char *outfilename;

21         unsigned int start_line;

22         unsigned int end_line;

23 

24         BASIC_INFO(const BASIC_INFO&);

25         BASIC_INFO& operator= (const BASIC_INFO&);

26 

27         void pid_str(char **thiz, int value, int base);

28 

29     public: 

30         BASIC_INFO(void);

31 

32         BASIC_INFO(const char *myinfile, const char* mystartline, const char *myendline);

33 

34         ~BASIC_INFO(void);

35 

36         void str_new(char **thiz, int length);

37 

38         void int_to_str(char *thiz, int value, int base);

39 

40         unsigned int str_to_int(const char *thiz, int base);

41                                                                                                                    

42         char *getinfilename(void)const;

43 

44         unsigned int getstart_line(void)const;

45 

46         unsigned int getend_line(void)const;

47 

48         char *getoutfilename(void)const;

49 };

50 

51 #endif
 
   
 1 // ./include/vfile.h

 2 

 3 #ifndef __JERRY_VFILE                                                                                              

 4 #define __JERRY_VFILE

 5 

 6 #include <basic_info.h>

 7 

 8 class VFILE:public BASIC_INFO

 9 {

10 #define infile_open_mode  (std::ios_base::in)

11 #define outfile_open_mode (std::ios_base::out)

12     private:

13         ifstream infile; 

14         ofstream outfile;

15 

16         char *alloc_pool;

17         char *alloc_use_end;

18         unsigned int pool_last;

19         unsigned int start_label;

20         unsigned int start_line;

21         unsigned int end_line;

22 

23         void infile_read(void);

24 

25         void outfile_write(void);

26 

27         void file_fix_changed(int line_num);

28 

29         int get_label(void);

30 

31         void skip_blank(char **ptr); 

32 

33         unsigned int length_to_blank(const char *src);

34 

35         void copy_to_blank(char **src); 

36 

37         void skip_to_punc(char **ptr, char value); 

38 

39         int is_line_end(char *ptr);

40 

41         VFILE(void);

42         VFILE(const VFILE&);

43     public:

44         

45         VFILE(const char *myinfilename, const char *mystartline, 

46               const char *myendline);

47              

48 

49         void parse_file(void);

50 

51         void format_alloc(void);

52 

53         void parse_write(void);

54 

55         void last_write(void);

56 

57         ~VFILE(void);

58 };

59 

60 #endif  
 1 // vtable.cpp

 2 #include <basic_info.h>                                                                                            

 3 #include <vfile.h>

 4 int main(int argc, char *argv[])

 5 {

 6 

 7     VFILE vtable(argv[1], argv[2], argv[3]);

 8 

 9     vtable.parse_file();

10     vtable.format_alloc();

11     vtable.parse_write();

12     vtable.last_write();

13 

14     cout << vtable.getoutfilename() << endl;

15 

16     return 0;

17 }
  1 // basic_info.cpp

  2 #include <basic_info.h>                                                                                            

  3 

  4 BASIC_INFO::BASIC_INFO(void):

  5     strpid(NULL), infilename(NULL), outfilename(NULL),

  6     start_line(0), end_line(0){}

  7 

  8 BASIC_INFO::BASIC_INFO(const char *myinfile, const char* mystartline, const char* myendline):

  9     strpid(NULL), infilename(NULL), outfilename(NULL),

 10     start_line(0), end_line(0)

 11 {

 12     str_new(&strpid, 32);

 13     pid_str(&strpid, getpid(), 10);

 14 

 15     str_new(&infilename, strlen(myinfile) + 1);

 16     strcpy(infilename, myinfile);

 17 

 18     str_new(&outfilename, strlen("/tmp/") + strlen(infilename) + strlen(strpid) + 1);

 19     strcpy(outfilename, "/tmp/");

 20     strcat(outfilename, infilename);

 21     strcat(outfilename, strpid);

 22 

 23     start_line = str_to_int(mystartline, 10);

 24     end_line = str_to_int(myendline, 10);

 25 }

 26 

 27 BASIC_INFO::~BASIC_INFO(void)

 28 {

 29     delete []strpid;

 30     delete []infilename;

 31     delete []outfilename;

 32 

 33     strpid = NULL;

 34     infilename = NULL;

 35     outfilename = NULL;

 36     start_line = 0;

 37     end_line = 0;

 38 }

 39 

 40 void BASIC_INFO::pid_str(char **thiz, int value, int base)

 41 {

 42     int pid_seq = getpid();

 43 

 44     int_to_str(*thiz, value, base);

 45 }

 46 

 47 void BASIC_INFO::str_new(char **thiz, int length)

 48 {

 49     if(*thiz != NULL)

 50         delete[](*thiz);

 51 

 52     *thiz = new char[length];

 53 

 54     if(*thiz == NULL)

 55     {

 56         cout << "allocate failed" << endl;

 57         exit(1);

 58     }

 59 }

 60 

 61 

 62 void BASIC_INFO::int_to_str(char *thiz, int value, int base)

 63 {

 64     char *temp  = NULL;

 65     str_new(&temp, 32);

 66 

 67     char *start = temp;

 68 

 69     while(value)

 70     {

 71         *temp++ ="0123456789abcdef"[value % base]; 

 72         value = value / base;

 73     }

 74 

 75     *temp = 0;

 76 

 77     while(--temp >= start)

 78     {

 79         *thiz++ = *temp; 

 80     }

 81 

 82     *thiz = 0;

 83 

 84     delete[]start;

 85 }

 86 

 87 unsigned int BASIC_INFO::str_to_int(const char *thiz, int base)

 88 {

 89     unsigned int temp = 0;

 90     while(*thiz)

 91     {

 92         if(!isalnum(*thiz))

 93             break;

 94 

 95         if(0 <= *thiz && *thiz <= '9')

 96             temp = (*thiz - '0') + temp * base;

 97 

 98         if('a'<= *thiz && *thiz <= 'f')

 99             temp = (*thiz - 'a' + 10) + temp * base;

100 

101         if('A'<= *thiz && *thiz <= 'F')

102             temp = (*thiz - 'A' + 10) + temp * base;

103 

104         thiz++;

105     }

106 

107     return temp;

108 }

109 

110 char * BASIC_INFO::getinfilename(void)const

111 {

112     return infilename;

113 }

114 

115 unsigned int BASIC_INFO::getstart_line(void)const

116 {

117     return start_line;

118 }

119 

120 unsigned int BASIC_INFO::getend_line(void)const

121 {

122     return end_line;

123 }

124 

125 char * BASIC_INFO::getoutfilename(void)const

126 {

127     return outfilename;

128 }            
  1 // vtable.cpp

  2 

  3 #include "vfile.h"                                                                                                 

  4 

  5 VFILE::VFILE(void){} 

  6 

  7 VFILE::VFILE(const VFILE&){}

  8 

  9 VFILE::VFILE(const char *myinfilename, const char *mystartline, const char *myendline): 

 10              BASIC_INFO(myinfilename, mystartline, myendline),

 11              alloc_pool(NULL), start_label(0)

 12 {

 13     infile.open(getinfilename(), infile_open_mode);

 14     outfile.open(getoutfilename(), outfile_open_mode);

 15     if(NULL == infile || NULL == outfile)

 16     {

 17         cout << "open file failed" << endl;

 18         exit(1);

 19     }

 20     infile.unsetf(ios::skipws);

 21 

 22     start_line = getstart_line();

 23     end_line = getend_line();

 24 

 25     start_label = get_label();

 26 }

 27 

 28 VFILE::~VFILE(void)

 29 {

 30     if(infile)

 31         infile.close();

 32     if(outfile)

 33         outfile.close();

 34     if(alloc_pool)

 35         delete[]alloc_pool;

 36 

 37     alloc_pool = NULL; 

 38 }

 39 

 40 void VFILE::infile_read(void)

 41 {

 42     infile.getline(alloc_pool, 300);

 43 }

 44 

 45 void VFILE::outfile_write(void)

 46 {

 47     outfile << alloc_pool ;

 48 }

 49 

 50 void VFILE::file_fix_changed(int line_num)

 51 {

 52     if(line_num)

 53     {

 54         int i = 0;

 55         for(i = 0; i < line_num; i++)

 56         {

 57             if(!infile.eof())

 58             {

 59                 infile_read();

 60                 outfile_write();

 61                 outfile << '
'; 62 } 63 } 64 } 65 66 else 67 { 68 while(!infile.eof()) 69 { 70 infile_read(); 71 outfile_write(); 72 outfile << '
'; 73 } 74 } 75 76 } 77 78 int VFILE::get_label(void) 79 { 80 str_new(&alloc_pool, 300); 81 file_fix_changed(start_line); 82 int temp = str_to_int(alloc_pool, 16); 83 delete[]alloc_pool; 84 alloc_pool = NULL; 85 return temp; 86 } 87 88 void VFILE::skip_blank(char **ptr) 89 { 90 char *temp = *ptr; 91 92 while(isblank(*temp++)) 93 *ptr = *ptr + 1; 94 } 95 96 unsigned int VFILE::length_to_blank(const char *src) 97 { 98 unsigned int length = 0; 99 100 while((!isblank(*src)) && (*src != 0)) 101 { 102 length++; 103 src++; 104 } 105 106 // while(!isblank(*src++)) 107 // length++; 108 109 return length; 110 111 } 112 113 void VFILE::copy_to_blank(char **src) 114 { 115 unsigned int length = length_to_blank(*src); 116 117 if(length > pool_last) 118 { 119 char *temp = new char[strlen(alloc_pool) + 1 + 50]; 120 strcpy(temp, alloc_pool); 121 delete[]alloc_pool; 122 alloc_pool = temp; 123 alloc_use_end = alloc_pool + strlen(alloc_pool) + 1; 124 pool_last = pool_last + 50; 125 } 126 127 pool_last = pool_last - length; 128 129 for(int i = 0; i < length; i++) 130 { 131 *alloc_use_end++ = **src; 132 *src = *src + 1; 133 } 134 135 *alloc_use_end = 0; 136 } 137 138 void VFILE::skip_to_punc(char **ptr, char value) 139 { 140 char *temp = *ptr; 141 142 while(*temp++ != value) 143 *ptr = *ptr + 1; 144 145 *ptr = *ptr + 1; 146 } 147 148 int VFILE::is_line_end(char *ptr) 149 { 150 char *temp = ptr; 151 if(isblank(*temp) && isblank(*(temp+1))) 152 return 1; 153 return 0; 154 } 155 156 void VFILE::parse_file(void) 157 { 158 pool_last = 300; 159 str_new(&alloc_pool, pool_last + 1); 160 *alloc_pool = 0; 161 alloc_use_end = alloc_pool; 162 163 164 char *temp = NULL; 165 str_new(&temp, 300); 166 char *temp_start = temp; 167 for(unsigned int line = start_line + 1; line <= end_line; line++) 168 { 169 infile.getline(temp, 300); 170 skip_blank(&temp); 171 skip_to_punc(&temp,':'); 172 173 do 174 { 175 skip_blank(&temp); 176 copy_to_blank(&temp); 177 178 if(*temp == 0 || *(temp + 1) == 0) 179 break; 180 if(*temp == '
' || *(temp + 1) == '
') 181 break; 182 183 }while(!is_line_end(temp)); 184 } 185 186 delete[]temp_start; 187 temp = NULL; 188 } 189 190 void VFILE::format_alloc(void) 191 { 192 unsigned int alloc_used = alloc_use_end - alloc_pool; 193 alloc_use_end = alloc_pool; 194 int add_line= (alloc_used / 8); 195 196 char *temp = NULL; 197 str_new (&temp, alloc_used + add_line * 11 + 2); 198 *temp = 0; 199 *(temp + alloc_used + add_line * 11 ) = '
'; 200 *(temp + alloc_used + add_line * 11 + 1) = 0; 201 202 int label = start_label; 203 204 for(int i = 0; i < add_line; i++) 205 { 206 char *str_label = NULL; 207 str_new(&str_label, 32); 208 *str_label = 0; 209 int_to_str(str_label, label, 16); 210 211 strcat(temp, " "); 212 strcat(temp,str_label); 213 strcat(temp,":\t"); 214 215 { 216 char str_array[8]; 217 218 for(int k = 3; k >=0; --k) 219 { 220 ((short*)str_array)[3-k] = ((short*)alloc_use_end)[k]; 221 } 222 strncpy(str_label, str_array, 8); 223 } 224 alloc_use_end += 8; 225 *(str_label + 8) = '
'; 226 *(str_label + 9) = 0; 227 228 strcat(temp, str_label); 229 delete[]str_label; 230 231 label = label + 4; 232 } 233 234 delete[]alloc_pool; 235 alloc_pool = temp; 236 } 237 void VFILE::parse_write(void) 238 { 239 outfile_write(); 240 } 241 242 void VFILE::last_write(void) 243 { 244 str_new(&alloc_pool, 300); 245 file_fix_changed(0); 246 delete[]alloc_pool; 247 alloc_pool = NULL; 248 249 }