|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU Lesser General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU Lesser General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Lesser General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #include <iostream> |
|
20 #include "dwarfmanager.h" |
|
21 #include "inputfile.h" |
|
22 |
|
23 const string DwarfFrameManager::iFrameSectionName(".debug_frame"); |
|
24 |
|
25 static inline size_t SizeOfEncodedValue (int encoding) |
|
26 { |
|
27 switch (encoding & 0x7) |
|
28 { |
|
29 default: /* ??? */ |
|
30 case 0: return ENCODED_POINTER_SIZE; |
|
31 case 2: return 2; |
|
32 case 3: return 4; |
|
33 case 4: return 8; |
|
34 } |
|
35 } |
|
36 |
|
37 static inline bool ValueFitsSize(Dwarf_Word val, size_t size){ |
|
38 switch (size){ |
|
39 default: |
|
40 case 0: { |
|
41 cerr << "Error: size of " << size << " not allowed\n"; |
|
42 exit(EXIT_FAILURE); |
|
43 } |
|
44 case 2: |
|
45 return val <= 0xffff; |
|
46 case 4: |
|
47 return val <= 0xffffffff; |
|
48 case 8: |
|
49 return true; |
|
50 } |
|
51 } |
|
52 |
|
53 |
|
54 void DwarfFrameManager::ProcessSection(FileShdrPair & aPair, Dwarf_Byte_Ptr aStart, Dwarf_Byte_Ptr aEnd){ |
|
55 CiePtrEncodingMap ptrEncodingMap; |
|
56 CieAugmentationMap augmentationMap; |
|
57 size_t encoded_ptr_size = ENCODED_POINTER_SIZE; |
|
58 size_t bytes_read; |
|
59 Dwarf_Byte_Ptr start = aStart; |
|
60 Dwarf_Byte_Ptr section_start = start; |
|
61 Dwarf_Byte_Ptr end = aEnd; |
|
62 while (start < end) { |
|
63 unsigned char *augmentation_data = NULL; |
|
64 unsigned long augmentation_data_len = 0; |
|
65 size_t offset_size; |
|
66 size_t initial_length_size; |
|
67 |
|
68 Dwarf_Byte_Ptr saved_start = start; |
|
69 Dwarf_Word length = READ_UNALIGNED4(start); |
|
70 start += 4; |
|
71 |
|
72 if (length == 0){ // ZERO terminator - shouldn't see this |
|
73 continue; |
|
74 } |
|
75 |
|
76 if (length >= 0xfffffff0u) { |
|
77 cerr << "Error: 64 bit DWARF not supported\n"; |
|
78 exit(EXIT_FAILURE); |
|
79 } else { |
|
80 offset_size = 4; |
|
81 initial_length_size = 4; |
|
82 } |
|
83 |
|
84 Dwarf_Byte_Ptr block_end = saved_start + length + initial_length_size; |
|
85 if (block_end > end) { |
|
86 cerr << "Warning: Invalid length " << length << " in FDE at 0x" |
|
87 << (unsigned long)(saved_start - section_start) << " in file " << aPair.iXIPFileDetails.iElfFile << "\n"; |
|
88 block_end = end; |
|
89 } |
|
90 Dwarf_Word cie_id = READ_UNALIGNED4(start); |
|
91 if (cie_id != (Dwarf_Word)DW_CIE_ID) |
|
92 WRITE_UNALIGNED4(start, cie_id + GetSectionOffset(aPair.iXIPFileDetails.iElfFile)); |
|
93 start += offset_size; |
|
94 |
|
95 if (cie_id == (Dwarf_Word)DW_CIE_ID) { |
|
96 Dwarf_Ubyte version = *start++; |
|
97 |
|
98 char * augmentation = (char *) start; |
|
99 augmentation_data = NULL; |
|
100 start = (Dwarf_Byte_Ptr) strchr ((char *) start, '\0') + 1; |
|
101 |
|
102 if (augmentation[0] == 'z'){ |
|
103 ULEB128(start, bytes_read); |
|
104 ULEB128(start, bytes_read); |
|
105 |
|
106 if (version == 1){ |
|
107 // fc->ra = GET (1); |
|
108 start++; |
|
109 } else { |
|
110 // fc->ra = LEB (); |
|
111 ULEB128(start, bytes_read); |
|
112 } |
|
113 augmentation_data_len = ULEB128(start, bytes_read); |
|
114 augmentation_data = start; |
|
115 augmentationMap[saved_start] = augmentation_data_len; |
|
116 start += augmentation_data_len; |
|
117 } else if (strcmp (augmentation, "eh") == 0){ |
|
118 //start += eh_addr_size; |
|
119 start += 4; |
|
120 // fc->code_factor = LEB (); |
|
121 // fc->data_factor = SLEB (); |
|
122 ULEB128(start, bytes_read); |
|
123 ULEB128(start, bytes_read); |
|
124 if (version == 1){ |
|
125 //c->ra = GET (1); |
|
126 start++; |
|
127 } else { |
|
128 // fc->ra = LEB (); |
|
129 ULEB128(start, bytes_read); |
|
130 } |
|
131 } else { |
|
132 ULEB128(start, bytes_read); |
|
133 ULEB128(start, bytes_read); |
|
134 |
|
135 if (version == 1){ |
|
136 // fc->ra = GET (1); |
|
137 start++; |
|
138 } else { |
|
139 // fc->ra = LEB (); |
|
140 ULEB128(start, bytes_read); |
|
141 } |
|
142 } |
|
143 |
|
144 if (augmentation_data_len){ |
|
145 unsigned char *p, *q; |
|
146 p = (unsigned char *) augmentation + 1; |
|
147 q = augmentation_data; |
|
148 Dwarf_Ubyte encoding = 0; |
|
149 while (1){ |
|
150 if (*p == 'L') |
|
151 q++; |
|
152 else if (*p == 'P') |
|
153 q += 1 + SizeOfEncodedValue(*q); |
|
154 else if (*p == 'R') |
|
155 encoding = *q++; |
|
156 else |
|
157 break; |
|
158 p++; |
|
159 } |
|
160 if (encoding) |
|
161 ptrEncodingMap[saved_start] = encoding; |
|
162 } |
|
163 |
|
164 } else { |
|
165 Dwarf_Byte_Ptr look_for = section_start + cie_id; |
|
166 Dwarf_Ubyte encoding = 0; |
|
167 CiePtrEncodingMap::iterator iE = ptrEncodingMap.find(look_for); |
|
168 if (iE != ptrEncodingMap.end()){ |
|
169 encoding = iE->second; |
|
170 encoded_ptr_size = SizeOfEncodedValue(encoding); |
|
171 } |
|
172 if ((encoding & 0x70) != DW_EH_PE_pcrel){ |
|
173 // do the nasty |
|
174 LinearAddr addr = GetValue(start, encoded_ptr_size); |
|
175 LinearAddr relocatedAddr = aPair.iXIPFileDetails.Relocate(addr); |
|
176 if (ValueFitsSize(relocatedAddr, encoded_ptr_size)){ |
|
177 WriteValue(start, relocatedAddr, encoded_ptr_size); |
|
178 } else { |
|
179 cerr << "Warning: relocated addresses in " << GetSectionName().c_str() |
|
180 << " section of " << aPair.iXIPFileDetails.iElfFile.c_str() |
|
181 << " too large for encoding. Backtraces may be misleading.\n"; |
|
182 } |
|
183 } |
|
184 start += encoded_ptr_size; |
|
185 // skip the range size |
|
186 start += encoded_ptr_size; |
|
187 |
|
188 CieAugmentationMap::iterator iP = augmentationMap.find(look_for); |
|
189 if (iP != augmentationMap.end()){ |
|
190 ULEB128(start, bytes_read); |
|
191 start += bytes_read; |
|
192 } |
|
193 } |
|
194 |
|
195 Dwarf_Word tmp = 0; |
|
196 while (start < block_end){ |
|
197 unsigned op, opa; |
|
198 op = *start++; |
|
199 opa = op & 0x3f; |
|
200 if (op & 0xc0) |
|
201 op &= 0xc0; |
|
202 switch (op){ |
|
203 case DW_CFA_advance_loc: |
|
204 break; |
|
205 case DW_CFA_offset: |
|
206 ULEB128(start, bytes_read); |
|
207 break; |
|
208 case DW_CFA_restore: |
|
209 break; |
|
210 case DW_CFA_set_loc: |
|
211 start += encoded_ptr_size; |
|
212 break; |
|
213 case DW_CFA_advance_loc1: |
|
214 start += 1; |
|
215 break; |
|
216 case DW_CFA_advance_loc2: |
|
217 start += 2; |
|
218 break; |
|
219 case DW_CFA_advance_loc4: |
|
220 start += 4; |
|
221 break; |
|
222 case DW_CFA_offset_extended: |
|
223 case DW_CFA_val_offset: |
|
224 ULEB128(start, bytes_read); |
|
225 ULEB128(start, bytes_read); |
|
226 break; |
|
227 case DW_CFA_restore_extended: |
|
228 case DW_CFA_undefined: |
|
229 case DW_CFA_same_value: |
|
230 ULEB128(start, bytes_read); |
|
231 break; |
|
232 case DW_CFA_register: |
|
233 case DW_CFA_def_cfa: |
|
234 ULEB128(start, bytes_read); |
|
235 ULEB128(start, bytes_read); |
|
236 break; |
|
237 case DW_CFA_def_cfa_register: |
|
238 case DW_CFA_def_cfa_offset: |
|
239 ULEB128(start, bytes_read); |
|
240 break; |
|
241 case DW_CFA_def_cfa_expression: |
|
242 tmp = ULEB128(start, bytes_read); |
|
243 EditLocationExpression (start, encoded_ptr_size, tmp, aPair); |
|
244 start += tmp; |
|
245 break; |
|
246 case DW_CFA_expression: |
|
247 case DW_CFA_val_expression: |
|
248 ULEB128(start, bytes_read); |
|
249 tmp = ULEB128(start, bytes_read); |
|
250 EditLocationExpression (start, encoded_ptr_size, tmp, aPair); |
|
251 start += tmp; |
|
252 break; |
|
253 #ifndef DW_CFA_offset_extended_sf |
|
254 // seems to be type in dwarf.h |
|
255 #define DW_CFA_offset_extended_sf 0x11 |
|
256 //DW_CFA_cfa_offset_extended_sf |
|
257 #endif |
|
258 case DW_CFA_offset_extended_sf: |
|
259 case DW_CFA_val_offset_sf: |
|
260 case DW_CFA_def_cfa_sf: |
|
261 ULEB128(start, bytes_read); |
|
262 ULEB128(start, bytes_read); |
|
263 break; |
|
264 case DW_CFA_def_cfa_offset_sf: |
|
265 ULEB128(start, bytes_read); |
|
266 break; |
|
267 case DW_CFA_MIPS_advance_loc8: |
|
268 start += 8; |
|
269 break; |
|
270 case DW_CFA_GNU_args_size: |
|
271 ULEB128(start, bytes_read); |
|
272 break; |
|
273 case DW_CFA_GNU_negative_offset_extended: |
|
274 ULEB128(start, bytes_read); |
|
275 ULEB128(start, bytes_read); |
|
276 break; |
|
277 default: |
|
278 break; |
|
279 } |
|
280 } |
|
281 } |
|
282 |
|
283 } |