Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the instruction of an executable go to?

Please do not mark as Duplicate : I know that executable files such us .exe are a set of instructions in machine language (binary) but what I don't know if those instruction are targeted to the system (then to the kernel) and then to the CPU or they're read from the memory directly by the CPU ? I'm a bit confused

like image 940
Mekacher Anis Avatar asked Feb 08 '23 12:02

Mekacher Anis


2 Answers

Arranging for executable files to run is done by the process loader, usually a part of, or executed by, the UI shell.

The exe file contains header metadata and executable code.

The loader reads the executable file header, allocates an initial working set of kernel and other resources that a process will need to run and creates a thread to run code at the process entry point. If that code has not been read into memory by the loader, it will happen now in an immediate page pault loads it.

The process then exists, and a thread is running it.

Summary: the image file, (.exe in Wondows), contains both metadata for interpretation by the OS loader AND executable code. The executable code instructions are read into memory by the loader and the CPU fetches and executes those instructions from memory.

like image 103
Martin James Avatar answered Mar 05 '23 17:03

Martin James


Your question starts with a mistake.

I know that executable files such us .exe are a set of instructions in machine language (binary)

Usually, an executable file is set of instructions how a program or library is to be loaded into memory. (There are some executables that are loaded as it into memory but those are normally used for operating system and embedded systems.)

A program or library will have data that is:

  • Read Only
  • Read Write
  • Read Write initialized to zero
  • Execute

A linker usually organizes the parts of a program in program sections that correspond to those groups.

The program load reads the instructions in the executable file and creates these sections. For read only data, the loader creates the appropriate pages, loads the data into them, and marks the pages a read write. Some systems actually map the memory page directly to the executable and uses the executable for paging those sections.

For a read write, initialized to zero section, the executable does not need to contain data. I just needs to tell the loader to create the pages and set everything to zero.

The executable will also define the dynamic libraries that need to be loaded and addresses fix ups (addresses that need to be fixed, depending upon where things are loaded).

If the executable defines a program (rather than a library) it will indicate the starting address of the program so that it can execute.

like image 26
user3344003 Avatar answered Mar 05 '23 18:03

user3344003