Question

I have written this simple library in c:

library.h:

int sum(int a, int b);

library.c:

#include "library.h"
int sum(int a, int b) {
    return a+b;
}

I compiled it with cl.exe (visual studio 2012) using these commands:

cl /c /EHsc library.cpp
lib library.obj

which compiled it as a static link .lib library file. Now I would like to have a look at how the compiler generated the assembly code, for learning/academic purposes. Please note, I don't want to decompile it, I just want to read the generated assembly. I have tried to open the .lib with w32dasm but I get a lot of strange symbols and it looks like the tool is unable to read the file. I've HAVE done a similar task with a dynamic link library (generated from the same source) and it worked; in that I was able to view the assembly code using w32dasm. So, my question is: is possible to view the assembly code of a static link library as you can do with a dynamic link library? If so, what would be the correct tool to use, since w32dasm does not appear to be the correct tool.

Was it helpful?

Solution 2

This is technically a little bit different because its not going to disassemble the .lib file, but for most purposes should be good enough:

You can use /FA switch, as described in detail here. More precisely you will probably want to use /FAs which should produce .asm file for each source file containing both assembly and source code, which is helpful for reference. For example if you add this to your cl command:

cl /c /EHsc /FAs library.cpp

This should, in addition to its normal function, create a file library.asm. This file can possibly contain a lot of stuff, so searching through it is a good idea.

If you are using Visual Studio IDE you can also set a breakpoint, start debugging and then right click a source line and choose Go To Disassembly. This option shows up in the context menu only during debugging and is active only during a break, this is why setting up a breakpoint is useful, but you can also break manually.

OTHER TIPS

If you do want to disassemble the library instead of looking at the compiler-generated assembler, you can use the DUMPBIN tool:

dumpbin /disasm library.lib

IDA can also handle static libraries and object files (and dozens of other file formats). Disclaimer: I work for Hex-Rays.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top