|
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 <iomanip> |
|
21 |
|
22 #include "dwarfmanager.h" |
|
23 #include "inputfile.h" |
|
24 #include "outputfile.h" |
|
25 #include "filefragment.h" |
|
26 |
|
27 void DwarfSectionManager::AddSection(XIPFileDetails & aXIPFileDetails, Elf32_Shdr * aShdr){ |
|
28 if (iRomDetails->iTrace){ |
|
29 cout << " " << GetSectionName() << " - DWARF\n"; |
|
30 } |
|
31 |
|
32 FileShdrPair aFileShdrPair(aXIPFileDetails, aShdr); |
|
33 iFileShdrList.push_back(aFileShdrPair); |
|
34 } |
|
35 |
|
36 void DwarfSectionManager::SetupSection(){ |
|
37 if (!iFileShdrList.empty()){ |
|
38 ElfSectionElfData * aDwarfSectionData = new ElfSectionElfData(*this); |
|
39 Elf32_Shdr aDwarfShdr; |
|
40 aDwarfShdr.sh_name = 0; // for now. |
|
41 aDwarfShdr.sh_type = SHT_PROGBITS; |
|
42 aDwarfShdr.sh_flags = 0; |
|
43 aDwarfShdr.sh_addr = 0; |
|
44 aDwarfShdr.sh_offset = 0; // for now |
|
45 aDwarfShdr.sh_size = 0; // for now. |
|
46 aDwarfShdr.sh_link = 0; |
|
47 aDwarfShdr.sh_info = 0; |
|
48 aDwarfShdr.sh_addralign = 4; |
|
49 aDwarfShdr.sh_entsize = sizeof(Elf32_Sym); |
|
50 |
|
51 ElfSection aDwarfSection(aDwarfSectionData, GetSectionName().c_str(), aDwarfShdr); |
|
52 iElfSectionManager.AddSection(aDwarfSection); |
|
53 } |
|
54 } |
|
55 |
|
56 Dwarf_Unsigned DwarfSectionManager::DecodeUnsignedLeb128(Dwarf_Byte_Ptr leb128, size_t & leb128_length){ |
|
57 Dwarf_Ubyte byte; |
|
58 Dwarf_Word word_number; |
|
59 Dwarf_Unsigned number; |
|
60 Dwarf_Sword shift; |
|
61 Dwarf_Sword byte_length; |
|
62 |
|
63 /* The following unrolls-the-loop for the first few bytes and |
|
64 unpacks into 32 bits to make this as fast as possible. |
|
65 word_number is assumed big enough that the shift has a defined |
|
66 result. */ |
|
67 if ((*leb128 & 0x80) == 0) { |
|
68 leb128_length = 1; |
|
69 return *leb128; |
|
70 } else if ((*(leb128 + 1) & 0x80) == 0) { |
|
71 leb128_length = 2; |
|
72 |
|
73 word_number = *leb128 & 0x7f; |
|
74 word_number |= (*(leb128 + 1) & 0x7f) << 7; |
|
75 return word_number; |
|
76 } else if ((*(leb128 + 2) & 0x80) == 0) { |
|
77 leb128_length = 3; |
|
78 |
|
79 word_number = *leb128 & 0x7f; |
|
80 word_number |= (*(leb128 + 1) & 0x7f) << 7; |
|
81 word_number |= (*(leb128 + 2) & 0x7f) << 14; |
|
82 return word_number; |
|
83 } else if ((*(leb128 + 3) & 0x80) == 0) { |
|
84 leb128_length = 4; |
|
85 |
|
86 word_number = *leb128 & 0x7f; |
|
87 word_number |= (*(leb128 + 1) & 0x7f) << 7; |
|
88 word_number |= (*(leb128 + 2) & 0x7f) << 14; |
|
89 word_number |= (*(leb128 + 3) & 0x7f) << 21; |
|
90 return word_number; |
|
91 } |
|
92 |
|
93 /* The rest handles long numbers Because the 'number' may be larger |
|
94 than the default int/unsigned, we must cast the 'byte' before |
|
95 the shift for the shift to have a defined result. */ |
|
96 number = 0; |
|
97 shift = 0; |
|
98 byte_length = 1; |
|
99 byte = *(leb128); |
|
100 for (;;) { |
|
101 number |= ((Dwarf_Unsigned) (byte & 0x7f)) << shift; |
|
102 |
|
103 if ((byte & 0x80) == 0) { |
|
104 leb128_length = byte_length; |
|
105 return number; |
|
106 } |
|
107 shift += 7; |
|
108 |
|
109 byte_length++; |
|
110 ++leb128; |
|
111 byte = *leb128; |
|
112 } |
|
113 } |
|
114 |
|
115 #define BITSINBYTE 8 |
|
116 Dwarf_Signed DwarfSectionManager::DecodeSignedLeb128(Dwarf_Byte_Ptr leb128, size_t & leb128_length){ |
|
117 Dwarf_Signed number = 0; |
|
118 Dwarf_Bool sign = 0; |
|
119 Dwarf_Word shift = 0; |
|
120 Dwarf_Ubyte byte = *leb128; |
|
121 Dwarf_Word byte_length = 1; |
|
122 |
|
123 /* byte_length being the number of bytes of data absorbed so far in |
|
124 turning the leb into a Dwarf_Signed. */ |
|
125 |
|
126 for (;;) { |
|
127 sign = byte & 0x40; |
|
128 number |= ((Dwarf_Signed) ((byte & 0x7f))) << shift; |
|
129 shift += 7; |
|
130 |
|
131 if ((byte & 0x80) == 0) { |
|
132 break; |
|
133 } |
|
134 ++leb128; |
|
135 byte = *leb128; |
|
136 byte_length++; |
|
137 } |
|
138 |
|
139 if ((shift < sizeof(Dwarf_Signed) * BITSINBYTE) && sign) { |
|
140 number |= -((Dwarf_Signed) 1 << shift); |
|
141 } |
|
142 |
|
143 leb128_length = byte_length; |
|
144 return number; |
|
145 } |
|
146 |
|
147 Dwarf_Byte_Ptr DwarfSectionManager::GetSectionData(FileShdrPair & aPair){ |
|
148 |
|
149 InputFile in(aPair.iXIPFileDetails.iElfFile); |
|
150 in.SetOffset(aPair.iShdr.sh_offset); |
|
151 return (Dwarf_Byte_Ptr)in.GetData(aPair.iShdr.sh_size); |
|
152 } |
|
153 |
|
154 void DwarfConcatenatedSectionManager::GetFileFragmentData(FileFragmentData & aFileFragmentData ){ |
|
155 ConcatenateData(); |
|
156 if (iRomDetails->iTrace){ |
|
157 cout << "\nGenerating DWARF section " << GetSectionName() << " size = " |
|
158 << dec << Size() << " bytes\n"; |
|
159 } |
|
160 FileShdrList::iterator e = iFileShdrList.end(); |
|
161 for (FileShdrList::iterator i = iFileShdrList.begin(); i < e; i++){ |
|
162 if (iRomDetails->iTrace){ |
|
163 cout << " " << i->iXIPFileDetails.iElfFile << " " << dec << i->iShdr.sh_size << " bytes\n" << flush; |
|
164 } |
|
165 ProcessSection(*i); |
|
166 } |
|
167 SetFileFragmentData(aFileFragmentData, iSize, reinterpret_cast<char *>(iData)); |
|
168 } |
|
169 |
|
170 void DwarfConcatenatedSectionManager::ConcatenateData(){ |
|
171 if (iData == NULL) { |
|
172 size_t sectionSize = Size(); |
|
173 iData = new Dwarf_Ubyte[sectionSize]; |
|
174 Dwarf_Byte_Ptr p = iData; |
|
175 FileShdrList::iterator e = iFileShdrList.end(); |
|
176 for (FileShdrList::iterator i = iFileShdrList.begin(); i < e; i++){ |
|
177 size_t off = p - iData; |
|
178 size_t soff = GetSectionOffset(i->iXIPFileDetails.iElfFile); |
|
179 if (off >= sectionSize) |
|
180 assert(off < sectionSize); |
|
181 if (off != soff) |
|
182 assert(off == soff); |
|
183 size_t n = i->iShdr.sh_size; |
|
184 Dwarf_Byte_Ptr contrib = GetSectionData(*i); |
|
185 memcpy(p, contrib, n); |
|
186 p += n; |
|
187 delete [] contrib; |
|
188 } |
|
189 } |
|
190 } |
|
191 |
|
192 size_t DwarfConcatenatedSectionManager::Size(){ |
|
193 if (iSizeValid) |
|
194 return iSize; |
|
195 //cerr << "Size for " << GetSectionName() << "\n"; |
|
196 size_t offset = 0; |
|
197 FileShdrList::iterator e = iFileShdrList.end(); |
|
198 for (FileShdrList::iterator i = iFileShdrList.begin(); i != e; i++){ |
|
199 //cerr << "offset = 0x" << offset << "\t"; |
|
200 SetSectionOffset(i->iXIPFileDetails.iElfFile, offset); |
|
201 size_t size = i->iShdr.sh_size; |
|
202 //cerr << "section size for " << i->iXIPFileDetails.iElfFile << " 0x" << size << "\n"; |
|
203 SetSectionSize(i->iXIPFileDetails.iElfFile, size); |
|
204 size_t newOffset = offset + size; |
|
205 if (newOffset < offset){ |
|
206 cerr << "Error: The combined section " << GetSectionName() |
|
207 << " requires translation from 32 to 64 bit Dwarf which is not currently supported.\n" |
|
208 << "Exclude the following files (or their equivalent in terms of their contribution to Dwarf):\n"; |
|
209 for (; i != e; i++){ |
|
210 cerr << i->iXIPFileDetails.iE32File << "\n"; |
|
211 } |
|
212 exit(EXIT_FAILURE); |
|
213 } |
|
214 offset = newOffset; |
|
215 } |
|
216 //cerr << "Size = 0x" << offset << "\n"; |
|
217 iSizeValid = true; |
|
218 return iSize = offset; |
|
219 } |
|
220 |
|
221 void DwarfConcatenatedSectionManager::ProcessSection(FileShdrPair & aPair){ |
|
222 Dwarf_Byte_Ptr start = GetSection(aPair.iXIPFileDetails.iElfFile); |
|
223 Dwarf_Byte_Ptr end = start + aPair.iShdr.sh_size; |
|
224 ProcessSection(aPair, start, end); |
|
225 } |
|
226 |
|
227 void DwarfConcatenatedSectionManager::SetSectionOffset(PathName & aPathName, size_t aOffset) { |
|
228 iPathNameSectionOffsetMap[aPathName] = aOffset; |
|
229 } |
|
230 |
|
231 void DwarfConcatenatedSectionManager::InitOffsetMap(){ |
|
232 Size(); // forces the map to be set up if it hasn't been already |
|
233 } |
|
234 |
|
235 size_t DwarfConcatenatedSectionManager::GetSectionOffset(PathName & aPathName){ |
|
236 if (!iSizeValid) // if the size is valid then so must be the offsets. |
|
237 InitOffsetMap(); |
|
238 return iPathNameSectionOffsetMap[aPathName]; |
|
239 } |
|
240 |
|
241 void DwarfConcatenatedSectionManager::SetSectionSize(PathName & aPathName, size_t aSize) { |
|
242 iPathNameSectionSizeMap[aPathName] = aSize; |
|
243 } |
|
244 |
|
245 size_t DwarfConcatenatedSectionManager::GetSectionSize(PathName & aPathName){ |
|
246 if (!iSizeValid) // if the size is valid then so must be the offsets. |
|
247 InitOffsetMap(); |
|
248 return iPathNameSectionSizeMap[aPathName]; |
|
249 } |
|
250 |
|
251 Dwarf_Byte_Ptr DwarfConcatenatedSectionManager::GetSection(PathName & aPathName){ |
|
252 ConcatenateData(); |
|
253 size_t offset = GetSectionOffset(aPathName); |
|
254 return iData + offset; |
|
255 } |
|
256 |
|
257 class ElfSectionFragmentedDwarfData : public ElfSectionElfData { |
|
258 public: |
|
259 ElfSectionFragmentedDwarfData(FileFragmentOwner & aSource) : |
|
260 ElfSectionElfData(aSource) |
|
261 {} |
|
262 |
|
263 ElfSectionFragmentedDwarfData(const ElfSectionElfData & aData) : |
|
264 ElfSectionElfData(aData) |
|
265 {} |
|
266 |
|
267 // ElfSection protocol |
|
268 virtual ElfSectionFragmentedDwarfData * Clone(){ |
|
269 return new ElfSectionFragmentedDwarfData(*this); |
|
270 } |
|
271 |
|
272 virtual void AddData(OutputFile & aOutputFile){ |
|
273 return; |
|
274 } |
|
275 }; |
|
276 |
|
277 class DwarfSectionFragment : public FileFragmentOwner { |
|
278 public: |
|
279 DwarfSectionFragment(DwarfFragmentedSectionManager & aSource, |
|
280 FileShdrPair & aPair): |
|
281 iSource(aSource), |
|
282 iPair(aPair), |
|
283 iData(NULL) |
|
284 {} |
|
285 |
|
286 // Bitwise copy is OK so don't need to write our own copy ctor etc. |
|
287 |
|
288 // The FileFragmentOwner protocol |
|
289 virtual void GetFileFragmentData(FileFragmentData & aFileFragmentData ); |
|
290 virtual size_t Size(); |
|
291 virtual void DeleteFileFragmentData(); |
|
292 |
|
293 private: |
|
294 DwarfSectionFragment(); |
|
295 |
|
296 private: |
|
297 DwarfFragmentedSectionManager & iSource; |
|
298 FileShdrPair & iPair; |
|
299 Dwarf_Byte_Ptr iData; |
|
300 }; |
|
301 |
|
302 void DwarfSectionFragment::GetFileFragmentData(FileFragmentData & aFileFragmentData ){ |
|
303 iSource.ProcessSection(iPair, iData); |
|
304 SetFileFragmentData(aFileFragmentData, Size(), reinterpret_cast<char *>(iData)); |
|
305 } |
|
306 |
|
307 size_t DwarfSectionFragment::Size(){ |
|
308 return iPair.iShdr.sh_size; |
|
309 } |
|
310 |
|
311 void DwarfSectionFragment::DeleteFileFragmentData(){ |
|
312 delete [] iData; |
|
313 // |
|
314 delete this; |
|
315 } |
|
316 |
|
317 void DwarfFragmentedSectionManager::SetupSection(){ |
|
318 if (!iFileShdrList.empty()){ |
|
319 ElfSectionFragmentedDwarfData * aDwarfSectionData = new ElfSectionFragmentedDwarfData(*this); |
|
320 Elf32_Shdr aDwarfShdr; |
|
321 aDwarfShdr.sh_name = 0; // for now. |
|
322 aDwarfShdr.sh_type = SHT_PROGBITS; |
|
323 aDwarfShdr.sh_flags = 0; |
|
324 aDwarfShdr.sh_addr = 0; |
|
325 aDwarfShdr.sh_offset = 0; // for now |
|
326 aDwarfShdr.sh_size = 0; // for now. |
|
327 aDwarfShdr.sh_link = 0; |
|
328 aDwarfShdr.sh_info = 0; |
|
329 aDwarfShdr.sh_addralign = 4; |
|
330 aDwarfShdr.sh_entsize = sizeof(Elf32_Sym); |
|
331 |
|
332 // aDwarfSectionData will ask the secton manager (i.e. its source) for the offset of the section. |
|
333 // So we better record it here. We assume that it is the current size of the output file. |
|
334 // As long as we are single threaded and all the fragments get added consecutively as below |
|
335 // this is a safe assumption. |
|
336 SetOffset(iDwarfManager.GetOutputFile().Size()); |
|
337 |
|
338 ElfSection aDwarfSection(aDwarfSectionData, GetSectionName().c_str(), aDwarfShdr); |
|
339 iElfSectionManager.AddSection(aDwarfSection); |
|
340 |
|
341 for (FileShdrList::iterator i = iFileShdrList.begin(); i < iFileShdrList.end(); i++ ){ |
|
342 DwarfSectionFragment * aFrag = new DwarfSectionFragment(*this, *i); |
|
343 aFrag->AddData(iDwarfManager.GetOutputFile()); |
|
344 } |
|
345 } |
|
346 } |
|
347 |
|
348 // NB the section itself doesn't write any data |
|
349 // The FileFragmentOwner protocol |
|
350 |
|
351 void DwarfFragmentedSectionManager::GetFileFragmentData(FileFragmentData & aFileFragmentData ){ |
|
352 SetFileFragmentData(aFileFragmentData, 0, reinterpret_cast<char *>(NULL)); |
|
353 } |
|
354 |
|
355 // This should never get called |
|
356 void DwarfFragmentedSectionManager::ConcatenateData(){ |
|
357 assert(1 == 0); |
|
358 return; |
|
359 } |
|
360 |
|
361 void DwarfFragmentedSectionManager::ProcessSection(FileShdrPair & aPair, Dwarf_Byte_Ptr & aData){ |
|
362 if (iInitialTraceMessage & iRomDetails->iTrace){ |
|
363 cout << "\nGenerating DWARF section " << GetSectionName() << " size = " |
|
364 << dec << Size() << " bytes\n"; |
|
365 iInitialTraceMessage = false; |
|
366 } |
|
367 if (iRomDetails->iTrace){ |
|
368 cout << " " << aPair.iXIPFileDetails.iElfFile << " size = " |
|
369 << dec << aPair.iShdr.sh_size << "\n" << flush; |
|
370 } |
|
371 Dwarf_Byte_Ptr start = GetSectionData(aPair); |
|
372 aData = start; |
|
373 Dwarf_Byte_Ptr end = start + aPair.iShdr.sh_size; |
|
374 ProcessSection(aPair, start, end); |
|
375 } |
|
376 |
|
377 #if 0 |
|
378 // This should never get called |
|
379 void DwarfFragmentedSectionManager::ProcessSection(FileShdrPair & aPair, Dwarf_Byte_Ptr start, Dwarf_Byte_Ptr end){ |
|
380 assert(1 == 0); |
|
381 return; |
|
382 } |
|
383 #endif |
|
384 |
|
385 const string DwarfMacinfoManager::iMacinfoSectionName(".debug_macinfo"); |
|
386 const string DwarfStrManager::iStrSectionName(".debug_str"); |
|
387 |
|
388 |
|
389 void DwarfManager::AddSection(XIPFileDetails & aXIPFileDetails, string aSectionName, Elf32_Shdr * aShdr){ |
|
390 if (iDwarfAbbrevManager.GetSectionName() == aSectionName) |
|
391 iDwarfAbbrevManager.AddSection(aXIPFileDetails, aShdr); |
|
392 else if (iDwarfArangesManager.GetSectionName() == aSectionName) |
|
393 iDwarfArangesManager.AddSection(aXIPFileDetails, aShdr); |
|
394 else if (iDwarfFrameManager.GetSectionName() == aSectionName) |
|
395 iDwarfFrameManager.AddSection(aXIPFileDetails, aShdr); |
|
396 else if (iDwarfInfoManager.GetSectionName() == aSectionName) |
|
397 iDwarfInfoManager.AddSection(aXIPFileDetails, aShdr); |
|
398 else if (iDwarfLineManager.GetSectionName() == aSectionName) |
|
399 iDwarfLineManager.AddSection(aXIPFileDetails, aShdr); |
|
400 else if (iDwarfLocManager.GetSectionName() == aSectionName) |
|
401 iDwarfLocManager.AddSection(aXIPFileDetails, aShdr); |
|
402 else if (iDwarfMacinfoManager.GetSectionName() == aSectionName) |
|
403 iDwarfMacinfoManager.AddSection(aXIPFileDetails, aShdr); |
|
404 else if (iDwarfPubnamesManager.GetSectionName() == aSectionName) |
|
405 iDwarfPubnamesManager.AddSection(aXIPFileDetails, aShdr); |
|
406 else if (iDwarfPubtypesManager.GetSectionName() == aSectionName) |
|
407 iDwarfPubtypesManager.AddSection(aXIPFileDetails, aShdr); |
|
408 else if (iDwarfRangesManager.GetSectionName() == aSectionName) |
|
409 iDwarfRangesManager.AddSection(aXIPFileDetails, aShdr); |
|
410 else if (iDwarfStrManager.GetSectionName() == aSectionName) |
|
411 iDwarfStrManager.AddSection(aXIPFileDetails, aShdr); |
|
412 #if 0 |
|
413 else |
|
414 cerr << "Warning: unrecognised debug section name " << aSectionName << " ignored\n"; |
|
415 #endif |
|
416 } |
|
417 |
|
418 void DwarfManager::SetupSections(){ |
|
419 // The order here is important for fix up |
|
420 // first the purely concatenated 'leaf' sections |
|
421 // See the diagram on p.182 of the Dwarf 3 spec |
|
422 // to understand the 'dependenices' |
|
423 iDwarfAbbrevManager.SetupSection(); |
|
424 iDwarfFrameManager.SetupSection(); |
|
425 iDwarfPubnamesManager.SetupSection(); |
|
426 iDwarfPubtypesManager.SetupSection(); |
|
427 iDwarfArangesManager.SetupSection(); |
|
428 iDwarfMacinfoManager.SetupSection(); |
|
429 |
|
430 iDwarfInfoManager.SetupSection(); |
|
431 iDwarfLineManager.SetupSection(); |
|
432 iDwarfLocManager.SetupSection(); |
|
433 |
|
434 iDwarfRangesManager.SetupSection(); |
|
435 iDwarfStrManager.SetupSection(); |
|
436 } |