2

I would like to know if there is a away how to modify assembly generated by compiler before the linking stage. As I have no idea how to do that?

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
Martin Kosicky
  • 471
  • 4
  • 12
  • Modify in what way? On Linux you can use `objdump` and `readelf` to play with binaries. – banach-space May 24 '15 at 17:28
  • 2
    You can generate assembly listings, edit them and compile back, or diassembly the object files and compile them back. Anyway, why would you modify that assembly? Isn't just adding your own code better? – user35443 May 24 '15 at 17:31
  • i would like to modify pointer accessing. I want to save application state to a file then load back without making serialization etc. ofc handles wont work but that would be performed by lazy loading – Martin Kosicky May 24 '15 at 18:08
  • Before proceeding with this, you should read about [ASLR](http://en.wikipedia.org/wiki/Address_space_layout_randomization#Microsoft_Windows)... – hyde May 25 '15 at 19:23

1 Answers1

1

The assembly code generated by the Microsoft's C/C++ compiler is meant to be informational only. In general it cannot be assembled by MASM into an .OBJ file equivalent to what the compiler would generate directly.

For example, consider this simple C++ hello world program:

#include <iostream>

int
main() {
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

When I compile it using Microsoft's C++ compiler (using /Fa to generate assembly) and then use MASM to try assemble the generated .ASM file this is what I get:

t146.asm(1277) : error A2008:syntax error : _Args_0
t146.asm(1339) : error A2008:syntax error : _Args_0
t146.asm(1773) : error A2008:syntax error : _Args_0
t146.asm(2176) : fatal error A1010:unmatched block nesting : ??$?6U?$char_traits@D@std@@@std@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@0@AAV10@PBD@Z

There'd be even more errors, but the last error causes the assembler to bail out and give up trying to assemble the rest of the file.

In addition to the errors there are also certain things missing from the generated assembly that exist in the generated object file. One example are COMDAT sections which show up in generated assembly as comments because MASM doesn't support them.

So if you were hopping to have Visual Studio generate assembly that you would then programically modify and assemble, it won't work. You could use the GNU C/C++ compiler instead since it doesn't generate object files directly. Instead GCC generates assembly which is turned into an object file by the GNU assembler. So it always generates valid assembly. An other option would be to modify the generated object files, which could actually be easier.

However the big problem is that what you're ultimately trying to accomplish, "modify pointer accessing", is a much more difficult task than you apparently think it is. There all sorts of ways pointers can be used in the code generated by the compiler. You would both need to recognize all of them and then identify which are actually the kind of pointer accesses you're interested in.

I recommend just using explicit serialization in your program. Even if you have to write your serialization code from scratch, it should be a lot easier than messing about the code generated by the compiler.

Community
  • 1
  • 1
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
  • the pointer modifucation involves having a custom allocator and the pointer accessing would be something like offset + base... anyway it is too complex – Martin Kosicky May 27 '15 at 17:02