2

I am trying to use this code to allocate a slice of code to a independent section:

#ifdef _MSC_VER #pragma section(".evil",execute) #pragma code_seg(".evil") #endif #ifdef __GNUC__ static __attribute__((section (".evil"))) #elif defined _MSC_VER static __declspec(allocate(".evil")) #endif void __invoke__start() {//... 

But that does not work and the compiler says

The __declspec( allocate()) syntax can be used for static data only.

I do this because I have to write some code to a new file ,and that file is a executable file.

Actually I can not find a way to get the exact address of a function in the memory when the program is running,if the program is compiled with MS VC++ debug mode For a full example,please see this code : full example

Now, the above problem has been solved, but I still want to make it clear that if it is possible to put some code to a independent section. There's other benefit when it is possible for my work, after all.

When I link two object file (COFF format), how can I make sure different code from different obj file will be in different section? Or is there another way to do this?

I am so sorry for my poor English.

2 Answers 2

2

you can find the memory address of the beginning of a function with inline assembler, then call a function to use the memory address, like:

void foo(){ __asm{ CALL 0h \\Put current address on top of stack CALL myFunction \\Actually make a funciton call } ... } int myFunction( int addrFromASM){ \\do something with addrFromASM+4, which will be where the rest of foo starts. } 
Sign up to request clarification or add additional context in comments.

6 Comments

Wow, that is a good way to do it. So myFunction can get the address of foo...and do anything..Thanks for your help!
The return address is stored by CALL myFunction, and it can be pulled off the stack, but not from an argument. CALL 0h is going to be nothing but trouble. Calling a function that requires an argument is going to be trouble too, and it isn't clear what the proper calling sequence is, because the calling convention is unspecified. In fact, inline assembly isn't required at all. Marking the function as __declspec(naked) might be a good idea, though, otherwise the compiler may adjust the stack pointer and make it more difficult to locate the return address.
In summary, the idea is reasonable, but the example code stinks.
"When the CALL instruction is used with a displacement of zero, it is recognized and treated specially; the RAS remains consistent even if there is not a corresponding RET instruction. " - CALL 0h is not trouble. cdecl is the default calling convention.
The idea is good and I know how to do it now. Thank Motes again. I'll mark it as accepted answer.
|
1

Apart from the fact that your code has some nice UB in it (you are assuming that the compiler will always put the functions in the order you except), what you want to do with the sections can be done by allocating a new section in the PE header that you write and putting your code there (see the paragraph on The Section Table, found here), you would need to set the BaseOfCode to this section as well and adjust NumberOfSections accordingly.

In terms of the funny address for functions, this because of Edit & Continue being on when compiling in debug mode, just turn it off in the project options and your addresses will be correct.

1 Comment

true. I tested for all day and found that the problem is about the linker in some way. Maybe symbol solving or that sort of staff. Anyway, it shocked me that I only need to pass a /RELEASE option to the linker and everything was solved. It does not make sense at all....Another way is to set /LTCG or /ltcg option to the linker. Both way works. So I do not have to close the debugging feature now. Your answer is enlightening, too. And thanks for your help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.