View Full Version : c compiler/linker and snippet creator
homersux
July 24th, 2004, 09:10
is there a compiler/linker that can generate binary image relocated at arbitrary memory address? in vc, #pragma comment (linker, "/BASE:address) only allow address to be 64k aligned. I want to have something more flexible. Consider if the following function is the first function in a file:
int y = 0;
int foo(int x){
y = x*3;
return y;
}
the generated binary code most likely is this:
00401000:
push ebp
mov ebp, esp
mov eax, [ebp+8]
imul eax, 3
mov [00410342], eax ; 00410342->y
pop ebp
ret
Is it possible to force y and a bunch of other variables to be generated in a segment starting, say 01007600. Yeah, it's closely related to my notepad mod project. I also got a handcrafted notepad working with colors. But it was major pain to relocate the global variables (cannot use stack because they are global) everytime I recompile. Another approach would be to code the entire thing in assembly but there is still relocation problem. You'll have to either hardcode in the address youself or trying what I am trying now: is there a way to tell linker where a portion of code or data should start in memory.
I am having trouble with snippet creator, the code cannot be assembled. The temporary .asm file doesn't seem to be a valid assembly code.
lifewire
July 24th, 2004, 11:51
i don't think that it is possible. but maybe you can do this: the .obj's generated by the compiler contain fixup tables for relocation. you can code a parser for those tables which automatically relocates. it is a very simple job, i coded it when i was a real newbie.
dELTA
July 24th, 2004, 15:20
Or you can create a macro that inserts 0-64k nops/junk data in front of the object you want to exactly specify the address for, hence complementing the 64k-only align option in the VC compiler, effectively going around it and making it do the job for you after all.

homersux
July 24th, 2004, 19:52
another way to circumwent this problem is to have
a function body taking up arbitrary length in a C code,
that's pretty hard to do as well. In asm, it's easy.
dELTA
July 25th, 2004, 05:56
Isn't that sorta kinda what I said?

And if you use macros with inline asm, which should be no problem in vc, it should not be hard to do directly in the C program either, should it?
doug
July 25th, 2004, 11:04
I don't understand why you would go through that much pain, really.
How much code do you want to relocate?
When I want relocatable code, I either code in ASM something that uses no absolute addressing OR
I program a DLL.
You probably want to code a DLL and use the .reloc section to rebase your code wherever you want. (You can add any kind of delta offset, not just image bases - as long as its the same delta you add everywhere). Parsing the .reloc is very easy. Use the PECOFF.pdf document for that matter.
dELTA
July 25th, 2004, 18:06
Yep, that would be the next thing I'd suggest, but I'm not completely sure I agree that writing a program that parses reloc-data and patches the code is less work than adding one compiler directive and a macro of a few lines to the source code, and then having the compiler/linker doing all the rest for you.

doug
July 25th, 2004, 19:03
I think a .reloc parser is something you will use a lot. For example if you have to inject a large amount of code in your unpacked targets, and (obviously) it's never at the same VA; then you'll code a DLL, inject the code+data & relocate them using the reloc table that's been prepared for you already.
Writing it once is not wasted time.
Besides, parsing relocs is extremely simple. You don't need to get fancy; two nested while loops. We're talking about 25 lines of asm code - maybe 7 in C.
homersux
July 25th, 2004, 20:11
Worth a shot, I'll have something hopefully ready by the end of this week. I'm gonna think it over how to do it well. Any existing tools out there for this purpose?
dELTA
July 26th, 2004, 06:24
There is a tool called ReloX by MackT, which can mess with relocations. You can find a link to it on CrackZ's news page (http://www.woodmann.com/crackz/Whatsnew.htm).
homersux
July 26th, 2004, 09:55
delta, it's easy to say but diffcult to do it with the compilers. I have tried to use compiler directives
and macros, but they don't seem to work well, at least not with VS 6.0
Show me how you can relocate my example code snippt with just compiler directives and macros,
relocate code to 01007700, and relocate y to 01007600(ideally, but this seems really bizzar for a compiler to have data in code segment, so i'll relax this to 01008000).
I'd love to see how anyone can do that without modifying the binary code.
dELTA
July 27th, 2004, 06:24
I can't drop any exact example off the top of my head, I just know that is should be theoretically possible the way I say above, sorry.
Peres
July 27th, 2004, 13:34
Hi! Nobody remembers of the old .org assembly directive? Am I completely dejected?
doug
July 27th, 2004, 22:12
but you then you still need to know where it's going to be loaded at, at time you are building it. For homersux' problem though, it's probably the best solution.
homersux
July 31st, 2004, 22:16
please read my post in mini project regarding changing notepad's color. I have included a couple tools that will make projects like this much easier in the future, alone with the source code for patching.
Powered by vBulletin® Version 4.2.2 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.