1 | /*
|
---|
2 | * TOPPERS/JSP Kernel
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
4 | * Just Standard Profile Kernel
|
---|
5 | *
|
---|
6 | * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
|
---|
7 | * Toyohashi Univ. of Technology, JAPAN
|
---|
8 | *
|
---|
9 | * ä¸è¨è使¨©è
|
---|
10 | ã¯ï¼ä»¥ä¸ã® (1)ã(4) ã®æ¡ä»¶ãï¼Free Software Foundation
|
---|
11 | * ã«ãã£ã¦å
|
---|
12 | ¬è¡¨ããã¦ãã GNU General Public License ã® Version 2 ã«è¨
|
---|
13 | * è¿°ããã¦ããæ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§ã¢ï¼æ¬ã½ããã¦ã§ã¢
|
---|
14 | * ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹å¤ã»åé
|
---|
15 | å¸ï¼ä»¥ä¸ï¼
|
---|
16 | * å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
17 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
18 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
19 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
20 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
21 | * ç¨ã§ããå½¢ã§åé
|
---|
22 | å¸ããå ´åã«ã¯ï¼åé
|
---|
23 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
24 | * è
|
---|
25 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è使¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
26 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
27 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
28 | * ç¨ã§ããªãå½¢ã§åé
|
---|
29 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
30 | * ã¨ï¼
|
---|
31 | * (a) åé
|
---|
32 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
33 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
34 | * 使¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
35 | * (b) åé
|
---|
36 | å¸ã®å½¢æ
|
---|
37 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
38 | * å ±åãããã¨ï¼
|
---|
39 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
40 | * 害ãããï¼ä¸è¨è使¨©è
|
---|
41 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
42 | 責ãããã¨ï¼
|
---|
43 | *
|
---|
44 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨è使¨©è
|
---|
45 | ã
|
---|
46 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ãã®é©ç¨å¯è½æ§ã
|
---|
47 | * å«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´
|
---|
48 | * æ¥çã¾ãã¯éæ¥çã«çãããããªãæå®³ã«é¢ãã¦ãï¼ãã®è²¬ä»»ãè² ããªãï¼
|
---|
49 | *
|
---|
50 | * @(#) $Id: fc_bfd.cpp,v 1.8 2003/12/15 07:32:13 takayuki Exp $
|
---|
51 | */
|
---|
52 |
|
---|
53 |
|
---|
54 | #include "base/filecontainer.h"
|
---|
55 |
|
---|
56 | #include "base/message.h"
|
---|
57 |
|
---|
58 | #include "bfd.h"
|
---|
59 | #include "libiberty.h"
|
---|
60 |
|
---|
61 | #include <string>
|
---|
62 | #include <map>
|
---|
63 |
|
---|
64 | using namespace std;
|
---|
65 |
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * BFDã使ã£ããã¡ã¤ã«ã³ã³ããã¯ã©ã¹
|
---|
69 | */
|
---|
70 | class filecontainer_BFD : public filecontainer
|
---|
71 | {
|
---|
72 | protected:
|
---|
73 | bfd * object; //ãªãã¸ã§ã¯ã
|
---|
74 | map<string, asymbol *> symbol_table; //æ¤ç´¢ç¨ããã·ã¥ãã©ã
|
---|
75 | asymbol ** symbol_container;
|
---|
76 |
|
---|
77 | public:
|
---|
78 | filecontainer_BFD(void) throw();
|
---|
79 | ~filecontainer_BFD(void) throw();
|
---|
80 |
|
---|
81 | virtual bool attach_module(const char *);
|
---|
82 | virtual bool change_endian(void * target, unsigned int size);
|
---|
83 | virtual bool load_contents(void * dest, unsigned long address, unsigned int size);
|
---|
84 | virtual unsigned long get_symbol_address(const char *);
|
---|
85 | virtual const char * get_architecture(void);
|
---|
86 |
|
---|
87 | } Instance_of_filecontainer_BFD;
|
---|
88 |
|
---|
89 | filecontainer_BFD::filecontainer_BFD(void) throw()
|
---|
90 | {
|
---|
91 | bfd_init();
|
---|
92 |
|
---|
93 | object = NULL;
|
---|
94 | symbol_table.clear();
|
---|
95 | symbol_container = NULL;
|
---|
96 | instance = this;
|
---|
97 | }
|
---|
98 |
|
---|
99 | filecontainer_BFD::~filecontainer_BFD(void) throw()
|
---|
100 | {
|
---|
101 | if(object != NULL)
|
---|
102 | bfd_close(object);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * attach_module : 対象ã¢ã¸ã¥ã¼ã«ãã¢ã¿ãããã
|
---|
107 | */
|
---|
108 | bool filecontainer_BFD::attach_module(const char * filename)
|
---|
109 | {
|
---|
110 | char ** target_list;
|
---|
111 | asymbol ** symbols;
|
---|
112 | asymbol * sym;
|
---|
113 | int num_syms;
|
---|
114 | boolean result;
|
---|
115 | int i;
|
---|
116 |
|
---|
117 | if(object != NULL)
|
---|
118 | bfd_close(object);
|
---|
119 | symbol_table.clear();
|
---|
120 |
|
---|
121 | //ã¢ã¸ã¥ã¼ã«ãªã¼ãã³ (èªè¾¼ç¨)
|
---|
122 | object = bfd_openr(filename, "default");
|
---|
123 |
|
---|
124 | //ã¿ã¼ã²ãã解決
|
---|
125 | target_list = (char **)bfd_target_list();
|
---|
126 | result = bfd_check_format_matches(object, bfd_object, &target_list);
|
---|
127 | if(result == 0)
|
---|
128 | ExceptionMessage("Internel error: BFD could not recognize the target file format.","å
|
---|
129 | é¨ã¨ã©ã¼: BFDã¯ãã¡ã¤ã«ã®èªã¿åºãã«å¤±æãã¾ãã").throwException();
|
---|
130 |
|
---|
131 | //ã·ã³ãã«ã®ããã·ã¥ãã©ã使
|
---|
132 | symbols = (asymbol **)xmalloc( bfd_get_symtab_upper_bound(object) );
|
---|
133 | num_syms = bfd_canonicalize_symtab(object, symbols);
|
---|
134 |
|
---|
135 | for(i=0;i<num_syms;i++)
|
---|
136 | {
|
---|
137 | sym = *(symbols+i);
|
---|
138 | if(sym != NULL && sym->name != NULL && *(sym->name) != '\x0')
|
---|
139 | symbol_table[string(sym->name)] = sym;
|
---|
140 | }
|
---|
141 |
|
---|
142 | symbol_container = symbols;
|
---|
143 | return true;
|
---|
144 | }
|
---|
145 |
|
---|
146 | bool filecontainer_BFD::change_endian(void * target, unsigned int size)
|
---|
147 | {
|
---|
148 | //ãã¹ãã¯ãªãã«ã¨ä»®å®
|
---|
149 | enum bfd_endian host_endian = BFD_ENDIAN_LITTLE;
|
---|
150 |
|
---|
151 | char * top, * tail;
|
---|
152 |
|
---|
153 | if(object == NULL)
|
---|
154 | return false;
|
---|
155 |
|
---|
156 | if(object->xvec->byteorder == BFD_ENDIAN_UNKNOWN)
|
---|
157 | return false;
|
---|
158 |
|
---|
159 | /*
|
---|
160 | unsigned int __work = 0x1;
|
---|
161 | if( *(char *)__work == 0)
|
---|
162 | host_endian = BFD_ENDIAN_BIG;
|
---|
163 | */
|
---|
164 |
|
---|
165 | if(object->xvec->byteorder == host_endian)
|
---|
166 | return true;
|
---|
167 |
|
---|
168 | /*
|
---|
169 | * ã¡ã¤ã³ã«ã¼ã : bswapããªãã§ãç´ ç´ã«æ¸ã
|
---|
170 | */
|
---|
171 |
|
---|
172 | top = (char *)target;
|
---|
173 | tail = (char *)target+size -1;
|
---|
174 |
|
---|
175 | while(top < tail)
|
---|
176 | {
|
---|
177 | *top ^= *tail, *tail ^= *top, *top ^= *tail;
|
---|
178 | top ++;
|
---|
179 | tail --;
|
---|
180 | }
|
---|
181 |
|
---|
182 | return true;
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | bool filecontainer_BFD::load_contents(void * dest, unsigned long address, unsigned int size)
|
---|
187 | {
|
---|
188 | struct sec * section;
|
---|
189 |
|
---|
190 | if(object == 0)
|
---|
191 | return false;
|
---|
192 |
|
---|
193 | //対象ã¢ãã¬ã¹ãä¿æããã»ã¯ã·ã§ã³ãæ¢ã
|
---|
194 | section = object->sections;
|
---|
195 | while(section != 0)
|
---|
196 | {
|
---|
197 | if(address - (unsigned long)section->vma <= section->_raw_size
|
---|
198 | && (section->flags & (SEC_ALLOC|SEC_HAS_CONTENTS)) == (SEC_ALLOC|SEC_HAS_CONTENTS))
|
---|
199 | {
|
---|
200 | //èªã¿åºã
|
---|
201 | bfd_get_section_contents(object, section, dest, address - (unsigned long)section->vma, size);
|
---|
202 | return true;
|
---|
203 | }
|
---|
204 | section = section->next;
|
---|
205 | }
|
---|
206 |
|
---|
207 | //ã©ãã«ããªã
|
---|
208 | ExceptionMessage("Internel error: Memory read with unmapped address","å
|
---|
209 | é¨ã¨ã©ã¼; ãããããã¦ãªãã¢ãã¬ã¹ã使ã£ã¦ã¡ã¢ãªãªã¼ããè¡ããã¾ãã").throwException();
|
---|
210 |
|
---|
211 | return false;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | /*
|
---|
216 | * get_symbol_address : ã·ã³ãã«åããã¢ãã¬ã¹å¤ãåå¾ãã
|
---|
217 | */
|
---|
218 | unsigned long filecontainer_BFD::get_symbol_address(const char * symbol)
|
---|
219 | {
|
---|
220 | map<string, asymbol *>::iterator scope;
|
---|
221 | string symbol_name;
|
---|
222 |
|
---|
223 | if(object == 0)
|
---|
224 | ExceptionMessage("Not initialized","åæåããã¦ã¾ãã").throwException();
|
---|
225 |
|
---|
226 | //ã·ã³ãã«åãçæãã ("_"ã¨ãã®å¦ç)
|
---|
227 | if(object->xvec->symbol_leading_char != '\x0')
|
---|
228 | symbol_name += object->xvec->symbol_leading_char;
|
---|
229 | symbol_name += symbol;
|
---|
230 |
|
---|
231 | scope = symbol_table.find(symbol_name);
|
---|
232 | if(scope == symbol_table.end())
|
---|
233 | return 0;
|
---|
234 | //Exception("Internal error: Unknown symbol [%s]","å
|
---|
235 | é¨ã¨ã©ã¼: 䏿ãªã·ã³ãã« [%s]").format(symbol_name.c_str());
|
---|
236 |
|
---|
237 | //Address = ã»ã¯ã·ã§ã³å
|
---|
238 | ãªãã»ããå¤ + ã»ã¯ã·ã§ã³ã®VMA
|
---|
239 | return (*scope).second->value + (*scope).second->section->vma;
|
---|
240 | }
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * get_architecture : ã¢ã¼ããã¯ãã£åã®åå¾
|
---|
244 | */
|
---|
245 | const char * filecontainer_BFD::get_architecture(void)
|
---|
246 | {
|
---|
247 | if(object == NULL)
|
---|
248 | return "Unknown";
|
---|
249 |
|
---|
250 | //ã¨ããããã¿ã¼ã²ããåãæã£ã¦ã¢ã¼ããã¯ãã£åã«ãã¦ããã
|
---|
251 | // #ã©ããããã¼ã«ãã使ã£ã¦ãªããã ä»ã®ã¨ãã
|
---|
252 | return object->xvec->name;
|
---|
253 | }
|
---|
254 |
|
---|