V
V
Vadim Ushakov2021-08-26 06:30:21
Operating Systems
Vadim Ushakov, 2021-08-26 06:30:21

Is it possible to change the executable file during execution*?

In general, the essence of the question in the title of the question. Is it possible to change its contents on the hard disk during the execution of an executable file?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Adamos, 2021-08-26
@Adamos

Unix-way : in an executable file - only executable code, it is read into memory at startup, after that the file is free and you can do everything with it that your access allows.
Windows-way : three boxes of resources can be piled up in an executable, up to degenerate cases like a self-extracting archive, when the code is just a small insert into a giant executable. So only the code is read into memory, but it is assumed that it can pull resources from the file when they are needed. Therefore, the file is write-locked until this code finishes executing.

G
gd1xza, 2021-08-26
@gd1xza

yes you can

O
old2ev, 2021-08-26
@old2ev

From what comes to mind, only a crutch ala open ofstream rewrite the file with it, set the compiler on it through the system and then restart the program through exec from the compiled file. Yes, it could be done, but this is nonsense. Changing C/C++ code at runtime is like using a microscope to drive nails. It would be easier to connect V8 and change JavaScript in runtime, it seems to be able to do it out of the box.
In general, I would like to clarify why you need it? For I can’t even think of a reason for these code frauds. Such nonsense popped up in my head when I invented a system of modules for C ++, but the issue was resolved through the loading of dynamic libraries at runtime, so if C and C ++ can do anything.
There is also an option for the completely repulsed: write machine code to the executable file via ofstream (having previously copied the executable itself on systems of the Windows family) and restart with exec, but there are more than enough reasons not to do this - at least it will be painful and scary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question