Results 1 to 15 of 15

Thread: c compiler/linker and snippet creator

  1. #1

    c compiler/linker and snippet creator

    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.
    Last edited by homersux; July 24th, 2004 at 09:16.

  2. #2
    lifewire
    Guest
    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.
    I promise that I have read the FAQ and tried to use the Search to answer my question.

  3. #3
    Administrator dELTA's Avatar
    Join Date
    Oct 2000
    Location
    Ring -1
    Posts
    4,206
    Blog Entries
    5
    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.

  4. #4
    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.

  5. #5
    Administrator dELTA's Avatar
    Join Date
    Oct 2000
    Location
    Ring -1
    Posts
    4,206
    Blog Entries
    5
    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?

  6. #6
    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.

  7. #7
    Administrator dELTA's Avatar
    Join Date
    Oct 2000
    Location
    Ring -1
    Posts
    4,206
    Blog Entries
    5
    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.

  8. #8
    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.

  9. #9
    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?

  10. #10
    Administrator dELTA's Avatar
    Join Date
    Oct 2000
    Location
    Ring -1
    Posts
    4,206
    Blog Entries
    5
    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).

  11. #11
    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.

  12. #12
    Administrator dELTA's Avatar
    Join Date
    Oct 2000
    Location
    Ring -1
    Posts
    4,206
    Blog Entries
    5
    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.

  13. #13
    Peres
    Guest
    Hi! Nobody remembers of the old .org assembly directive? Am I completely dejected?
    I promise that I have read the FAQ and tried to use the Search to answer my question.

  14. #14

    Thumbs up


    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.

  15. #15
    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.

Similar Threads

  1. snippet [NODE.JS] Add section to PE EXE x86
    By CovetousEyes in forum Mini Project Area
    Replies: 1
    Last Post: February 12th, 2013, 14:26
  2. Trying to decompile a MFC snippet code
    By dion in forum Advanced Reversing and Programming
    Replies: 3
    Last Post: January 23rd, 2013, 07:53
  3. A snippet of time.. ;) uneditted ..
    By BanMe in forum Blogs Forum
    Replies: 0
    Last Post: July 5th, 2009, 16:17
  4. Replies: 0
    Last Post: January 12th, 2008, 00:08
  5. Unpacking Intall creator pro
    By freakboy in forum The Newbie Forum
    Replies: 1
    Last Post: April 8th, 2005, 16:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •