S
S
spry2010-12-25 19:25:44
Machine translation from one language to another
spry, 2010-12-25 19:25:44

GCC, adding code in a certain place makes the program broken (because of code size apparently)?

So, we have the code to debug what is happening:

#define x86_emit_byte(value) \<br/>
 *translation_ptr = value; \<br/>
 translation_ptr++ \<br/>
<br/>
void function_cc x86_emit_translation_exit(p_u8 *p_translation_ptr)<br/>
{<br/>
 u8* translation_ptr = *p_translation_ptr;<br/>
 x86_emit_byte(0x90);<br/>
.... и так 100 строк...<br/>
 x86_emit_byte(0x90);<br/>
 *p_translation_ptr = translation_ptr;<br/>
}<br/>

This is a piece of a dynamic translator, or a recompiler. Initially, a code of about 100 bytes in size should be emitted here, for the test, let's imagine that these are all nops. So. If you emit these 100 bytes like this, then the translator stops working, doesn't crash, it just stops. For unknown reasons. If this code is replaced with an emitting cycle of at least a kilobyte of nop-s, everything is fine. That is, it depends on the size of the translator itself and not on the code that it emits. The translator's objects are relatively small, up to a meter, all 10 pieces. I've been struggling with this for 2 days now, but nothing has worked so far. Initially, the translator is not mine, I edit it, so I can’t say much. Therefore, if something important is not specified, ask of course. What worries me is one function that calls this piece. It is about 0x70000 bytes in size in the object (o_O?). I don’t know what to sin on anymore, because the translator does not fall. It just stops working properly.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
mt_, 2010-12-25
@mt_

And if you write define in one line, will it work?
#define x86_emit_byte(value) *(translation_ptr++) = value;

M
mraleph, 2010-12-26
@mraleph

Information, of course, is not enough for meaningful advice, but here's what I would do:
0) to clear my conscience and eliminate the bug, gcc would disable optimizations -O0, most likely the problem will remain the same, and you can look through the unoptimized code of this function and make sure that GCC did not lie;
1) instead of eight nop (the smallest value at which it stops working), it would give 7 nop and 1 int 3 (0xCC). or many, many int 3 and looked at who is stepping on them, on these "knobs" and what follows them, where should the control go after these nops (maybe there is some kind of garbage?)

T
Trotil, 2010-12-25
@Trotil

And the pointer passed to the function, what exactly does it point to?
It can overwrite a piece of some code, and after overwriting a fragment of a certain size, it may not lead to bad consequences, but more or less will already cause a problem.

T
Trotil, 2010-12-26
@Trotil

Looked.
In short, you write assembler inserts at some address range *p_translation_ptr = 0x90, and then for some reason shove the address itself there with the last instruction:
*p_translation_ptr = translation_ptr;
It turns out some kind of unknown assembler instruction (if you tell me the address, you can probably say which one it turns out), in which some important bit is inverted every 128 passes of the loop, as a result of which the instruction constructed at the address either rolls, or not rolls.
Sorry if I misunderstood the code, I want to sleep.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question