1 | /*! @file ObjdumpProcessor.cc |
---|
2 | * @brief ObjdumpProcessor Implementation |
---|
3 | * |
---|
4 | * This file contains the implementation of the functions supporting |
---|
5 | * the reading of an objdump output file and adding nops to a |
---|
6 | * coverage map. |
---|
7 | */ |
---|
8 | |
---|
9 | #include <assert.h> |
---|
10 | #include <ctype.h> |
---|
11 | #include <stdio.h> |
---|
12 | #include <stdlib.h> |
---|
13 | #include <string.h> |
---|
14 | #include <algorithm> |
---|
15 | #include <string> |
---|
16 | |
---|
17 | #include "app_common.h" |
---|
18 | #include "ObjdumpProcessor.h" |
---|
19 | #include "CoverageMap.h" |
---|
20 | #include "ExecutableInfo.h" |
---|
21 | #include "SymbolTable.h" |
---|
22 | #include "TargetFactory.h" |
---|
23 | |
---|
24 | #include "rld.h" |
---|
25 | #include "rld-process.h" |
---|
26 | |
---|
27 | namespace Coverage { |
---|
28 | |
---|
29 | void finalizeSymbol( |
---|
30 | ExecutableInfo* const executableInfo, |
---|
31 | std::string& symbolName, |
---|
32 | ObjdumpProcessor::objdumpLines_t instructions |
---|
33 | ) { |
---|
34 | // Find the symbol's coverage map. |
---|
35 | CoverageMapBase& coverageMap = executableInfo->findCoverageMap( symbolName ); |
---|
36 | |
---|
37 | uint32_t lowAddress = coverageMap.getFirstLowAddress(); |
---|
38 | uint32_t size = coverageMap.getSize(); |
---|
39 | uint32_t highAddress = lowAddress + size; |
---|
40 | |
---|
41 | // If there are NOT already saved instructions, save them. |
---|
42 | SymbolInformation* symbolInfo = SymbolsToAnalyze->find( symbolName ); |
---|
43 | if (symbolInfo->instructions.empty()) { |
---|
44 | symbolInfo->sourceFile = executableInfo; |
---|
45 | symbolInfo->baseAddress = lowAddress; |
---|
46 | symbolInfo->instructions = instructions; |
---|
47 | } |
---|
48 | |
---|
49 | // Add the symbol to this executable's symbol table. |
---|
50 | SymbolTable* theSymbolTable = executableInfo->getSymbolTable(); |
---|
51 | theSymbolTable->addSymbol( |
---|
52 | symbolName, lowAddress, highAddress - lowAddress + 1 |
---|
53 | ); |
---|
54 | |
---|
55 | // Mark the start of each instruction in the coverage map. |
---|
56 | for (auto& instruction : instructions) { |
---|
57 | coverageMap.setIsStartOfInstruction( instruction.address ); |
---|
58 | } |
---|
59 | |
---|
60 | // Create a unified coverage map for the symbol. |
---|
61 | SymbolsToAnalyze->createCoverageMap( |
---|
62 | executableInfo->getFileName().c_str(), symbolName, size |
---|
63 | ); |
---|
64 | } |
---|
65 | |
---|
66 | ObjdumpProcessor::ObjdumpProcessor() |
---|
67 | { |
---|
68 | } |
---|
69 | |
---|
70 | ObjdumpProcessor::~ObjdumpProcessor() |
---|
71 | { |
---|
72 | } |
---|
73 | |
---|
74 | uint32_t ObjdumpProcessor::determineLoadAddress( |
---|
75 | ExecutableInfo* theExecutable |
---|
76 | ) |
---|
77 | { |
---|
78 | #define METHOD "ERROR: ObjdumpProcessor::determineLoadAddress - " |
---|
79 | FILE* loadAddressFile = NULL; |
---|
80 | char* cStatus; |
---|
81 | uint32_t offset; |
---|
82 | |
---|
83 | // This method should only be call for a dynamic library. |
---|
84 | if (!theExecutable->hasDynamicLibrary()) |
---|
85 | return 0; |
---|
86 | |
---|
87 | std::string dlinfoName = theExecutable->getFileName(); |
---|
88 | uint32_t address; |
---|
89 | char inLibName[128]; |
---|
90 | std::string Library = theExecutable->getLibraryName(); |
---|
91 | |
---|
92 | dlinfoName += ".dlinfo"; |
---|
93 | // Read load address. |
---|
94 | loadAddressFile = ::fopen( dlinfoName.c_str(), "r" ); |
---|
95 | if (!loadAddressFile) { |
---|
96 | std::ostringstream what; |
---|
97 | what << "Unable to open " << dlinfoName; |
---|
98 | throw rld::error( what, METHOD ); |
---|
99 | } |
---|
100 | |
---|
101 | // Process the dlinfo file. |
---|
102 | while ( 1 ) { |
---|
103 | |
---|
104 | // Get a line. |
---|
105 | cStatus = ::fgets( inputBuffer, MAX_LINE_LENGTH, loadAddressFile ); |
---|
106 | if (cStatus == NULL) { |
---|
107 | ::fclose( loadAddressFile ); |
---|
108 | std::ostringstream what; |
---|
109 | what << "library " << Library << " not found in " << dlinfoName; |
---|
110 | throw rld::error( what, METHOD ); |
---|
111 | } |
---|
112 | sscanf( inputBuffer, "%s %x", inLibName, &offset ); |
---|
113 | std::string tmp = inLibName; |
---|
114 | if ( tmp.find( Library ) != tmp.npos ) { |
---|
115 | // fprintf( stderr, "%s - 0x%08x\n", inLibName, offset ); |
---|
116 | address = offset; |
---|
117 | break; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | ::fclose( loadAddressFile ); |
---|
122 | return address; |
---|
123 | |
---|
124 | #undef METHOD |
---|
125 | } |
---|
126 | |
---|
127 | bool ObjdumpProcessor::IsBranch( |
---|
128 | const char *instruction |
---|
129 | ) |
---|
130 | { |
---|
131 | if ( !TargetInfo ) { |
---|
132 | fprintf( |
---|
133 | stderr, |
---|
134 | "ERROR: ObjdumpProcessor::IsBranch - unknown architecture\n" |
---|
135 | ); |
---|
136 | assert(0); |
---|
137 | return false; |
---|
138 | } |
---|
139 | |
---|
140 | return TargetInfo->isBranch( instruction ); |
---|
141 | } |
---|
142 | |
---|
143 | bool ObjdumpProcessor::isBranchLine( |
---|
144 | const char* const line |
---|
145 | ) |
---|
146 | { |
---|
147 | if ( !TargetInfo ) { |
---|
148 | fprintf( |
---|
149 | stderr, |
---|
150 | "ERROR: ObjdumpProcessor::isBranchLine - unknown architecture\n" |
---|
151 | ); |
---|
152 | assert(0); |
---|
153 | return false; |
---|
154 | } |
---|
155 | |
---|
156 | return TargetInfo->isBranchLine( line ); |
---|
157 | } |
---|
158 | |
---|
159 | bool ObjdumpProcessor::isNop( |
---|
160 | const char* const line, |
---|
161 | int& size |
---|
162 | ) |
---|
163 | { |
---|
164 | if ( !TargetInfo ){ |
---|
165 | fprintf( |
---|
166 | stderr, |
---|
167 | "ERROR: ObjdumpProcessor::isNop - unknown architecture\n" |
---|
168 | ); |
---|
169 | assert(0); |
---|
170 | return false; |
---|
171 | } |
---|
172 | |
---|
173 | return TargetInfo->isNopLine( line, size ); |
---|
174 | } |
---|
175 | |
---|
176 | void ObjdumpProcessor::getFile( |
---|
177 | std::string fileName, |
---|
178 | rld::process::tempfile& objdumpFile, |
---|
179 | rld::process::tempfile& err |
---|
180 | ) |
---|
181 | { |
---|
182 | rld::process::status status; |
---|
183 | rld::process::arg_container args = { TargetInfo->getObjdump(), |
---|
184 | "-Cda", "--section=.text", "--source", |
---|
185 | fileName }; |
---|
186 | try |
---|
187 | { |
---|
188 | status = rld::process::execute( TargetInfo->getObjdump(), |
---|
189 | args, objdumpFile.name(), err.name() ); |
---|
190 | if ( (status.type != rld::process::status::normal) |
---|
191 | || (status.code != 0) ) { |
---|
192 | throw rld::error( "Objdump error", "generating objdump" ); |
---|
193 | } |
---|
194 | } catch( rld::error& err ) |
---|
195 | { |
---|
196 | std::cout << "Error while running " << TargetInfo->getObjdump() |
---|
197 | << " on " << fileName << std::endl; |
---|
198 | std::cout << err.what << " in " << err.where << std::endl; |
---|
199 | return; |
---|
200 | } |
---|
201 | |
---|
202 | objdumpFile.open( true ); |
---|
203 | } |
---|
204 | |
---|
205 | uint32_t ObjdumpProcessor::getAddressAfter( uint32_t address ) |
---|
206 | { |
---|
207 | objdumpFile_t::iterator itr; |
---|
208 | |
---|
209 | itr = find ( objdumpList.begin(), objdumpList.end(), address ); |
---|
210 | if (itr == objdumpList.end()) { |
---|
211 | return 0; |
---|
212 | } |
---|
213 | |
---|
214 | itr++; |
---|
215 | if (itr == objdumpList.end()) { |
---|
216 | return 0; |
---|
217 | } |
---|
218 | |
---|
219 | return (*itr); |
---|
220 | |
---|
221 | } |
---|
222 | |
---|
223 | void ObjdumpProcessor::loadAddressTable ( |
---|
224 | ExecutableInfo* const executableInformation, |
---|
225 | rld::process::tempfile& objdumpFile, |
---|
226 | rld::process::tempfile& err |
---|
227 | ) |
---|
228 | { |
---|
229 | int items; |
---|
230 | uint32_t offset; |
---|
231 | char terminator; |
---|
232 | std::string line; |
---|
233 | |
---|
234 | // Obtain the objdump file. |
---|
235 | if ( !executableInformation->hasDynamicLibrary() ) |
---|
236 | getFile( executableInformation->getFileName(), objdumpFile, err ); |
---|
237 | else |
---|
238 | getFile( executableInformation->getLibraryName(), objdumpFile, err ); |
---|
239 | |
---|
240 | // Process all lines from the objdump file. |
---|
241 | while ( true ) { |
---|
242 | |
---|
243 | // Get the line. |
---|
244 | objdumpFile.read_line( line ); |
---|
245 | if ( line.empty() ) { |
---|
246 | break; |
---|
247 | } |
---|
248 | |
---|
249 | // See if it is the dump of an instruction. |
---|
250 | items = sscanf( |
---|
251 | line.c_str(), |
---|
252 | "%x%c", |
---|
253 | &offset, &terminator |
---|
254 | ); |
---|
255 | |
---|
256 | // If it looks like an instruction ... |
---|
257 | if ((items == 2) && (terminator == ':')) { |
---|
258 | objdumpList.push_back( |
---|
259 | executableInformation->getLoadAddress() + offset |
---|
260 | ); |
---|
261 | } |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | void ObjdumpProcessor::load( |
---|
266 | ExecutableInfo* const executableInformation, |
---|
267 | rld::process::tempfile& objdumpFile, |
---|
268 | rld::process::tempfile& err |
---|
269 | ) |
---|
270 | { |
---|
271 | std::string currentSymbol = ""; |
---|
272 | uint32_t endAddress; |
---|
273 | uint32_t instructionOffset; |
---|
274 | int items; |
---|
275 | int found; |
---|
276 | objdumpLine_t lineInfo; |
---|
277 | uint32_t offset; |
---|
278 | bool processSymbol = false; |
---|
279 | uint32_t startAddress = 0; |
---|
280 | char symbol[ MAX_LINE_LENGTH ]; |
---|
281 | char terminator1; |
---|
282 | char terminatorOne; |
---|
283 | char terminator2; |
---|
284 | objdumpLines_t theInstructions; |
---|
285 | char instruction[ MAX_LINE_LENGTH ]; |
---|
286 | char ID[ MAX_LINE_LENGTH ]; |
---|
287 | std::string call = ""; |
---|
288 | std::string jumpTableID = ""; |
---|
289 | std::string line = ""; |
---|
290 | |
---|
291 | // Obtain the objdump file. |
---|
292 | if ( !executableInformation->hasDynamicLibrary() ) |
---|
293 | getFile( executableInformation->getFileName(), objdumpFile, err ); |
---|
294 | else |
---|
295 | getFile( executableInformation->getLibraryName(), objdumpFile, err ); |
---|
296 | |
---|
297 | while ( true ) { |
---|
298 | // Get the line. |
---|
299 | objdumpFile.read_line( line ); |
---|
300 | if ( line.empty() ) { |
---|
301 | // If we are currently processing a symbol, finalize it. |
---|
302 | if (processSymbol) { |
---|
303 | finalizeSymbol( |
---|
304 | executableInformation, |
---|
305 | currentSymbol, |
---|
306 | theInstructions |
---|
307 | ); |
---|
308 | fprintf( |
---|
309 | stderr, |
---|
310 | "WARNING: ObjdumpProcessor::load - analysis of symbol %s \n" |
---|
311 | " may be incorrect. It was the last symbol in %s\n" |
---|
312 | " and the length of its last instruction is assumed " |
---|
313 | " to be one.\n", |
---|
314 | currentSymbol.c_str(), |
---|
315 | executableInformation->getFileName().c_str() |
---|
316 | ); |
---|
317 | } |
---|
318 | objdumpFile.close(); |
---|
319 | break; |
---|
320 | } |
---|
321 | |
---|
322 | lineInfo.line = line; |
---|
323 | lineInfo.address = 0xffffffff; |
---|
324 | lineInfo.isInstruction = false; |
---|
325 | lineInfo.isNop = false; |
---|
326 | lineInfo.nopSize = 0; |
---|
327 | lineInfo.isBranch = false; |
---|
328 | |
---|
329 | instruction[0] = '\0'; |
---|
330 | ID[0] = '\0'; |
---|
331 | |
---|
332 | // Look for the start of a symbol's objdump and extract |
---|
333 | // offset and symbol (i.e. offset <symbolname>:). |
---|
334 | items = sscanf( |
---|
335 | line.c_str(), |
---|
336 | "%x <%[^>]>%c", |
---|
337 | &offset, symbol, &terminator1 |
---|
338 | ); |
---|
339 | |
---|
340 | // See if it is a jump table. |
---|
341 | found = sscanf( |
---|
342 | line.c_str(), |
---|
343 | "%x%c\t%*[^\t]%c%s %*x %*[^+]%s", |
---|
344 | &instructionOffset, &terminatorOne, &terminator2, instruction, ID |
---|
345 | ); |
---|
346 | call = instruction; |
---|
347 | jumpTableID = ID; |
---|
348 | |
---|
349 | // If all items found, we are at the beginning of a symbol's objdump. |
---|
350 | if ((items == 3) && (terminator1 == ':')) { |
---|
351 | endAddress = executableInformation->getLoadAddress() + offset - 1; |
---|
352 | |
---|
353 | // If we are currently processing a symbol, finalize it. |
---|
354 | if (processSymbol) { |
---|
355 | finalizeSymbol( |
---|
356 | executableInformation, |
---|
357 | currentSymbol, |
---|
358 | theInstructions |
---|
359 | ); |
---|
360 | } |
---|
361 | |
---|
362 | // Start processing of a new symbol. |
---|
363 | startAddress = 0; |
---|
364 | currentSymbol = ""; |
---|
365 | processSymbol = false; |
---|
366 | theInstructions.clear(); |
---|
367 | |
---|
368 | // See if the new symbol is one that we care about. |
---|
369 | if (SymbolsToAnalyze->isDesired( symbol )) { |
---|
370 | startAddress = executableInformation->getLoadAddress() + offset; |
---|
371 | currentSymbol = symbol; |
---|
372 | processSymbol = true; |
---|
373 | theInstructions.push_back( lineInfo ); |
---|
374 | } |
---|
375 | } |
---|
376 | // If it looks like a jump table, finalize the symbol. |
---|
377 | else if ( (found == 5) && (terminatorOne == ':') && (terminator2 == '\t') |
---|
378 | && (call.find( "call" ) != std::string::npos) |
---|
379 | && (jumpTableID.find( "+0x" ) != std::string::npos) |
---|
380 | && processSymbol ) |
---|
381 | { |
---|
382 | endAddress = executableInformation->getLoadAddress() + offset - 1; |
---|
383 | |
---|
384 | // If we are currently processing a symbol, finalize it. |
---|
385 | if ( processSymbol ) { |
---|
386 | finalizeSymbol( |
---|
387 | executableInformation, |
---|
388 | currentSymbol, |
---|
389 | theInstructions |
---|
390 | ); |
---|
391 | } |
---|
392 | processSymbol = false; |
---|
393 | } |
---|
394 | else if (processSymbol) { |
---|
395 | |
---|
396 | // See if it is the dump of an instruction. |
---|
397 | items = sscanf( |
---|
398 | line.c_str(), |
---|
399 | "%x%c\t%*[^\t]%c", |
---|
400 | &instructionOffset, &terminator1, &terminator2 |
---|
401 | ); |
---|
402 | |
---|
403 | // If it looks like an instruction ... |
---|
404 | if ((items == 3) && (terminator1 == ':') && (terminator2 == '\t')) { |
---|
405 | // update the line's information, save it and ... |
---|
406 | lineInfo.address = |
---|
407 | executableInformation->getLoadAddress() + instructionOffset; |
---|
408 | lineInfo.isInstruction = true; |
---|
409 | lineInfo.isNop = isNop( line.c_str(), lineInfo.nopSize ); |
---|
410 | lineInfo.isBranch = isBranchLine( line.c_str() ); |
---|
411 | } |
---|
412 | |
---|
413 | // Always save the line. |
---|
414 | theInstructions.push_back( lineInfo ); |
---|
415 | } |
---|
416 | } |
---|
417 | } |
---|
418 | } |
---|