-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstaticStackAnalyzer.c
513 lines (468 loc) · 16.9 KB
/
staticStackAnalyzer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/**
* @file
* @brief MIPS32 static stack analyzer
* @author Florian Kaup, i.A. Magnetic Sense GmbH (kaup.florian@sentinelsw.de)
* @date 2020
* @copyright GPLv3 (https://www.gnu.org/licenses/gpl-3.0)
* @details
*
* staticStackAnalyzer
* -------------------
*
* This program parses an ELF file and calculates the estimated stack usage of it.
* It is build for ELF files compiled by mips gcc in general and xc32 from
* Microchip Technology Inc. in special with MIPS32 release 5 target architecture.
* (This should apply to whole PIC32MZ-family, maybe even more)
*
* After parsing the ELF file, this function prints a markdown compatible table
* of the results, sorted and limited for your needs. This can be directly
* piped into a *.md file, so doxygen can include it into your program documentation.
*
* @bug This program cannot handle recursive function calls and will hang attempting to resolve one.
*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
/**
* Linked List Entry for function information.
* This struct is used for collecting information about functions.
* Each function has its own struct, all are linked together
* as a linked list.
*/
typedef struct functionInfo
{
char * name; ///< Name of the function
uint32_t start; ///< Start address of the function
uint32_t end; ///< End address of the function
uint32_t ownStack; ///< Estimated maximum stack usage
uint32_t deepest; ///< Estimated amount of stack bytes for this function and the deepest call tree
uint32_t * jumpsTo; ///< Array of addresses, where the function jumps and branches to
uint32_t numJumpsTo; ///< The number of entries in ::jumpsTo
bool usesFunctionPointers; ///< Flag for potential function pointer usage
bool isProcessed; ///< Flag if deepest stack usage is calculated
struct functionInfo * next; ///< Pointer to the next list member
} functionInfo_t;
/**
* Pointer to first element.
*/
functionInfo_t * firstFunctionInfo;
/**
* Wait for time.
* This function hangs the program for a desired amount of time.
* @param milliseconds The time in milliseconds to wait.
*/
void busywait (uint32_t milliseconds)
{
uint32_t start = clock();
while ( (clock()-start) * (1000 / CLOCKS_PER_SEC) < milliseconds);
}
/**
* Create handle for reading disassembly.
* This function calls xc32 objdump for disassembling the passed elf file.
* Additionally it gives objdump a head start, so buffer underrun is prevented.
* @note If you are using a different xc32 version or have a different install location, adapt the path in this function!
* @param filename The file to disassemble
* @return The file handle for reading the disassembly
*/
FILE* openDisassembly (char * filename)
{
char command[500];
sprintf(command, "\"C:\\Program Files (x86)\\Microchip\\xc32\\v2.30\\bin\\xc32-objdump.exe\" -d %s", filename);
FILE* commandHandle = popen(command, "r");
busywait(100);
return commandHandle;
}
/**
* Skip to next text section.
* Call this function for skipping over sections, which are not ".text".
* This is useful for skipping .rodata, .vectors and anything else with
* no interest. The file handle is moved to the next line after the magic
* pattern, or EOF if not found.
* @param disassemblyInput The file handle to use
*/
void findNextTextSection (FILE* disassemblyInput)
{
static const char * expectedString = "Disassembly of section .text";
char inputBuffer[500];
fgets(inputBuffer, sizeof(inputBuffer), disassemblyInput);
while (
(feof(disassemblyInput) == 0) &&
(strncmp(inputBuffer, expectedString, strlen(expectedString)) != 0)
) fgets(inputBuffer, sizeof(inputBuffer), disassemblyInput);
// jump over and ignore newline after expected string
fgets(inputBuffer, sizeof(inputBuffer), disassemblyInput);
}
/**
* Cleaning jumpTo table for function info.
* The table containing jump and branch targets is cleaned and trimmed
* by this function. All jump targets, which points into own address range
* are discarded (hence also recursive calls), and memory is trimmed to
* fit the final amount of jumps.
* @param functionInfo The function info struct to clean up
*/
void cleanupFunctionInfo (functionInfo_t* functionInfo)
{
uint32_t jumpsTo[1000];
uint32_t numJumpsTo = 0;
for (uint32_t i = 0; i<functionInfo->numJumpsTo; i++)
{
if (
(functionInfo->jumpsTo[i] > functionInfo->end) ||
(functionInfo->jumpsTo[i] < functionInfo->start)
)
{
jumpsTo[numJumpsTo] = functionInfo->jumpsTo[i];
numJumpsTo++;
}
}
free(functionInfo->jumpsTo);
functionInfo->jumpsTo = malloc(numJumpsTo * sizeof(uint32_t));
memcpy(functionInfo->jumpsTo, jumpsTo, numJumpsTo * sizeof(uint32_t));
functionInfo->numJumpsTo = numJumpsTo;
}
/**
* Create a new function info list element.
* A function info list element is allocated and filled with
* information from disassembly label informatio0n.
* @param disassemblyInput The disassembly label to parse
* @return Pointer to a function info, filled with name and start address
*/
functionInfo_t* createNewFunctionInfo (char * label)
{
uint32_t address = strtoul(label, NULL, 16);
char * name = strchr(label, '<');
char * nameEnd = strchr(label, '>');
if ( (name == 0) || (nameEnd == 0) )
{
return 0;
}
functionInfo_t* newOne = malloc(sizeof(functionInfo_t));
newOne->name = malloc((nameEnd-name)*sizeof(char));
memcpy(newOne->name, name+1, (nameEnd-name)-1);
newOne->name[(nameEnd-name)-1] = 0;
newOne->start = address;
newOne->end = address;
newOne->deepest = 0;
newOne->ownStack = 0;
newOne->jumpsTo = malloc(10000*sizeof(uint32_t));
newOne->numJumpsTo = 0;
newOne->usesFunctionPointers = false;
newOne->isProcessed = false;
newOne->next = 0;
return newOne;
}
/**
* Find function information by memory address.
* This function searches the linked list for memory address.
* The given address is checked, if it matches the address
* range of function information.
* @param address The address to find in function information
* @return The function information containing the requested address
*/
functionInfo_t * findFunctionByAddress (uint32_t address)
{
functionInfo_t * current = firstFunctionInfo;
while (current)
{
if ( (current->start <= address) && (current->end >= address) )
{
break;
}
current = current->next;
}
return current;
}
/**
* Calculate and return deepest stack usage.
* If the deepest stack usage is not yet calculated, it is calculated
* synchronously. Else the precalculated stack usage is returned.
* This function is used to update all stack information elements
* with their deepest stack usage. This function is recursive.
* @param target The stack information for which the stack usage has to be calculated and returned
* @return The deepest stack usage of the given stack information
*/
uint32_t getDeepestStackUsage (functionInfo_t * target)
{
if (target->isProcessed == false)
{
// avoid recursive call to this function information
target->isProcessed = true;
for (uint32_t i = 0; i<target->numJumpsTo; i++)
{
functionInfo_t * jumpTarget = findFunctionByAddress(target->jumpsTo[i]);
if (jumpTarget == 0)
{
printf("Error: Jump Target not found! Function %s jumps to 0x%x\n", target->name, target->jumpsTo[i]);
continue;
}
uint32_t thisBranch = getDeepestStackUsage(jumpTarget);
if (thisBranch > target->deepest)
{
target->deepest = thisBranch;
}
}
target->deepest += target->ownStack;
}
return target->deepest;
}
/**
* Sorts stack information linked list for deepest stack usage.
* Descending. List pointed by ::firstStackInfo.
*/
void sortForDeepest (void)
{
functionInfo_t * current = firstFunctionInfo;
uint32_t count = 0;
while (current)
{
count ++;
current = current->next;
}
for (uint32_t i = count-1; i > 0; i--)
{
if (firstFunctionInfo->deepest < firstFunctionInfo->next->deepest)
{
functionInfo_t * swap = firstFunctionInfo;
firstFunctionInfo = firstFunctionInfo->next;
swap->next = firstFunctionInfo->next;
firstFunctionInfo->next = swap;
}
current = firstFunctionInfo;
for (uint32_t j = 1; j < i; j++)
{
if (current->next->deepest < current->next->next->deepest)
{
functionInfo_t * swap = current->next;
current->next = current->next->next;
swap->next = current->next->next;
current->next->next = swap;
}
current = current->next;
}
}
}
/**
* Sorts stack information linked list for own stack usage.
* Descending. List pointed by ::firstStackInfo.
*/
void sortForOwn (void)
{
functionInfo_t * current = firstFunctionInfo;
uint32_t count = 0;
while (current)
{
count ++;
current = current->next;
}
for (uint32_t i = count-1; i > 0; i--)
{
if (firstFunctionInfo->ownStack < firstFunctionInfo->next->ownStack)
{
functionInfo_t * swap = firstFunctionInfo;
firstFunctionInfo = firstFunctionInfo->next;
swap->next = firstFunctionInfo->next;
firstFunctionInfo->next = swap;
}
current = firstFunctionInfo;
for (uint32_t j = 1; j < i; j++)
{
if (current->next->ownStack < current->next->next->ownStack)
{
functionInfo_t * swap = current->next;
current->next = current->next->next;
swap->next = current->next->next;
current->next->next = swap;
}
current = current->next;
}
}
}
/**
* Print stack information.
* The first elements of stack information linked list are printed
* to console. It is formated as a markdown compliant table, so you
* can easily import it into doxygen or anything else documentary
* related.
* @param num The number of elements to print
*/
void printStackInfo (uint32_t num)
{
printf("\n|%-50s|%-15s|%-15s|%-15s|\n", "Name", "Own", "Deepest", "Indirect Calls");
printf("|--------------------------------------------------|---------------|---------------|---------------|\n");
functionInfo_t * current = firstFunctionInfo;
for (uint32_t i = 0; i<num && current; i++)
{
printf("|%-50s|%-15i|%-15i|%-15c|\n", current->name, current->ownStack, current->deepest, current->usesFunctionPointers ? '*' : ' ');
current = current->next;
}
}
/**
* Main entry point.
* Should I say more?
* @param argc number of arguments in argv
* @param argv arguments from command line
* @return always 0
*/
int main (int argc, char** argv)
{
char sorting = 'd';
uint32_t printcount = 10;
char * filename = 0;
for (int i=1; i<argc; i++)
{
if (0 == strncmp("-s",argv[i], 2))
{
sorting = argv[i][2];
}
else if (0 == strncmp("-n",argv[i], 2))
{
printcount = strtol(argv[i]+2, 0, 0);
}
else
{
filename = argv[i];
}
}
if (filename == 0)
{
printf("Usage: %s [-s<sorting>] [-n<number>] <input file>\n"
"Options:\n"
" -s<sorting> Sorting of results, d=Deepest o=Own (default is %c)\n"
" -n<number> The number of entries printed, -1 for all (default is %i)\n"
" <input file> The ELF file to parse\n"
"\n"
"Report is printed as markdown table.\n"
"Content:\n"
" Name: The name of the function as the label in ELF file states.\n"
" Own: The stack usage of this function by itself.\n"
" Deepest: The maximum stack usage of this function and all called function.\n"
" Indirect Calls: This function uses function pointers, so the deepest stack usage cannot be determined.\n"
"\n",
argv[0],
sorting,
printcount);
return -1;
}
//-----------------------------------------------------------------------------------
// parse elf disassembly and gather stack and calltree information
FILE* disassemblyInput = openDisassembly(filename);
{
findNextTextSection(disassemblyInput);
char inputBuffer[500];
fgets(inputBuffer, sizeof(inputBuffer), disassemblyInput);
firstFunctionInfo = createNewFunctionInfo(inputBuffer);
}
functionInfo_t* currentFunctionInfo = firstFunctionInfo;
while(feof(disassemblyInput) == 0)
{
if (currentFunctionInfo == 0)
{
printf("something went wrong...\n");
return -1;
}
char inputBuffer[500];
fgets(inputBuffer, sizeof(inputBuffer), disassemblyInput);
// a new section begins
static const char * sectionString = "Disassembly of section ";
if ( 0 != strstr(inputBuffer, sectionString) )
{
cleanupFunctionInfo(currentFunctionInfo);
static const char * textSectionString = "Disassembly of section .text";
if ( 0 == strstr(inputBuffer, textSectionString) )
{
findNextTextSection(disassemblyInput);
}
fgets(inputBuffer, sizeof(inputBuffer), disassemblyInput);
currentFunctionInfo->next = createNewFunctionInfo(inputBuffer);
currentFunctionInfo = currentFunctionInfo->next;
continue;
}
// a new label was found
static const char * labelEndString = ">:";
if ( 0 != strstr(inputBuffer, labelEndString) )
{
char * labelName = strchr(inputBuffer, '<') + 1;
// ignore internal labels
if (labelName[0] != '.')
{
cleanupFunctionInfo(currentFunctionInfo);
currentFunctionInfo->next = createNewFunctionInfo(inputBuffer);
currentFunctionInfo = currentFunctionInfo->next;
continue;
}
}
// remember current address as end address of function
uint32_t address = strtoul(inputBuffer, NULL, 16);
if (address > currentFunctionInfo->end)
{
currentFunctionInfo->end = address;
}
// a stack pointer manipulation
static const char * stackPointerString = " \taddiu\tsp,sp,";
if ( 0 != strstr(inputBuffer, stackPointerString) )
{
int32_t movement = strtol(strstr(inputBuffer, stackPointerString) + strlen(stackPointerString), NULL, 10);
// only stack growing is considered
if (movement<0)
{
currentFunctionInfo->ownStack -= movement;
}
continue;
}
// a jump or branch
if (
(strstr(inputBuffer, " \tb") != 0) ||
(strstr(inputBuffer, " \tj") != 0)
)
{
if (strstr(inputBuffer, " \tjr\tra") != 0)
{
// ignore jump to return address
}
else if (strstr(inputBuffer, " \tjalr\t") != 0)
{
currentFunctionInfo->usesFunctionPointers = true;
}
else if (strstr(inputBuffer, " \tjr\t") != 0)
{
// suspected switch case usage, ignore
}
else
{
char * addressPosition = strrchr(inputBuffer, ',');
if (addressPosition == 0)
{
addressPosition = strrchr(inputBuffer, '\t');
}
currentFunctionInfo->jumpsTo[currentFunctionInfo->numJumpsTo] = strtoul(addressPosition+1, NULL, 16);
currentFunctionInfo->numJumpsTo++;
}
}
}
//---------------------------------------------------------------------------------------------
// calculate deepest stack usage for each function, calls recursively
currentFunctionInfo = firstFunctionInfo;
while (currentFunctionInfo)
{
getDeepestStackUsage(currentFunctionInfo);
currentFunctionInfo = currentFunctionInfo->next;
}
//-----------------------------------------------------------------------------------
// sort and print
switch(sorting)
{
case 'o':
sortForOwn();
break;
case 'd':
sortForDeepest();
break;
}
printStackInfo(printcount);
printf("\tdone\n");
fflush(stdout);
return 0;
}