This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Goto Label xxx in other file...

Dear all,
I have a question about jumping to some specific label in other file(in Keil C51 project)

Ex. In file A there is one label named _xxx. In file B can we use

goto _xxx

?
(maybe _xxx should be declared as extern, right ?)

The reason why I want this is: We did NOT want to do the tasks before label _xxx because they are wrong...! And unfornately file A is located in ROM and we can not modify it...

So we want to just jump to label _xxx and skip wrong codes....

  • If you do what you intend to do, you will cause more damage than anything else. Ignoring calling convention, stack load etc. will get in into deadly trouble. Don't do it. Work on the software, change it and fix it. And above all: UNDERSTAND WHAT THE PROBLEM IS BEFORE STARTING!

  • How do y our program get started? You can't circumvent the bad part of early main() unless something before main() calls your new code stored somewhere else.

    But yes - if you do reverse-engineer the bad section, then you can basically copy that instruction sequence into an assembler file. Modify to correct for bugs. Then perform a jump to the absolute address where the good code continues.

    Just note that there may be a bit of overlapping between good/bad because the compiler optimization may have changed order of instructions.

    This concept only works if you have the skill to:
    - duplicate the route into main()
    - finds a suitable target spot to jump to, where you have managed to lock down all assumtions made by the compiler for the following code. You need to understand exactly what happens earlier. And you need to know a bit about the following code too.
    - duplicate any required registers, local and global variables the "good" code expects to have been assigned.

    You can normally not do this in C, because you can't instruct the compiler to produce the exact conditions you need. You can - if you have the same compiler - write the code fix in C, to look at the generated instructions. But the bandage needs to be applied using assembler.

    Patchning code like this have been done a number of times. The only two requirements are:
    - the skills to understand exactly what happens and what is needed.
    - a means to "steal" the execution. On a PC, programs are run in RAM so it's trivial to patch in a jump/call at start of the bad code or befure the bad code is about to be executed. With a microcontroller with the code in flash - and autostarting - it is very much trickier. All the way up to impossible depending on chip and code design.

  • Per,

    What you described is Stuxnet stuff :-)