- If You Use Linux, You Lose Out If You Can’t Do This #3 – Executable files and ways of using shell variables
- What are Linux executable files?
- What is the ELF format?
- Prior to ELF
- ELF format
- Static Links and Dynamic Links
- Features of static links
- Features of dynamic links
- To be continued in the next column
- How To Make A File Executable In Linux
- Method 1: Using The Command Terminal
- Method 2: Using the GUI
- Understanding How File Execution Works
- Additional Information
- About the author
- Zeeman Memon
- Understanding the Linux Kernel, Second Edition by
- Executable Formats
If You Use Linux, You Lose Out If You Can’t Do This #3 – Executable files and ways of using shell variables
In the last column, I shared some nifty ways of using shell variables. In this installment, I’d like to introduce Linux executable files.
What are Linux executable files?
In general, Linux executable files contain programs written in a language like C and compiled so they can run in the Linux environment. In recent years, programs are often compiled in modern languages like Python, Java, Golang, and node.js, and you don’t have to care much about what the executable format is. But many kernels and GNU commands are still written in C, so it is important to know about this basic foundation.
` ` `
$ file /bin/ls
/bin/ls: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 3.2.0, BuildID[sha1]=9567f9a28e66f4d7ec4baf31cfbf68d0410f0ae6, stripped
` ` `
Let’s take a look at items of note here.
|/bin/ls||file command’s target file|
|ELF 64bit||Format of the executable file|
|LSB||Linux Standard Base-compliant|
|shared object||The file uses a shared library.|
|dynamically linked||Uses dynamically-linked shared libraries|
|stripped||Information about symbols are removed from the file.|
Using the command ` ` ` elfread -h /bin/ls ` ` ` here gives useful information.
` ` `
$ readelf -h /bin/ls
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Data: 2’s complement, little endian
Version: 1 (current)
OS/ABI: UNIX – System V
ABI Version: 0
Type: DYN (Shared object file)
Machine: Advanced Micro Devices X86-64
Entry point address: 0x5850
Start of program headers: 64 (bytes into file)
Start of section headers: 132000 (bytes into file)
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 9
Size of section headers: 64 (bytes)
Number of section headers: 28
Section header string table index: 27
` ` `
For more information, read elfread’s documentation with ` ` ` man elfread ` ` ` .
What is the ELF format?
ELF (Executable and Linkable Format) is an executable file format for compiler-generated objects and executable files linked to libraries. ELF format is the successor of UNIX’s first executable file format, a.out, and its extension, the Common Object File Format (COFF). ELF Format is widely used by System VR4 and later UNIX systems and Linux.
Prior to ELF
COFF is a format used on 32-bit System V platforms. It allows the use of shared libraries and debugs information. However, it has shortcomings in its limits on the maximum number of sections and length of section names. It also couldn’t provide symbolic debugging information for languages like C++. However, extensions like XCOFF (AIX) and ECOFF (DEC, SGI) overcame these weaknesses, and there are versions of UNIX using these formats. Window’s PE+ format is also based on COFF.
There are three types of ELF files:
- Relocatable files
- Contain sections with code and data
- Applicable for creating executable files, shared objects, and other relocatable objects.
- Executable files
- Contain executable programs
- Designate the program’s image creation method with exec(2).
- Shared object files
- A shared object file Contains code and data to be linked in two contexts:
- The link-editor can process it with other relocatable and shared object files to create other object files.
- The runtime linker combines it with a dynamic executable file and other shared objects to create a process image.
ELF’s object file format is as follows:
|Linking View||Execution View|
|ELF header||ELF header|
|Program header table (optional)||Program header table|
|Section 1||Segment 1|
|Section header table||Section header table (optional)|
- ELF header
- Usually resides at the beginning of the object file. It maintains a roadmap describing the file’s organization.
- Except for the ELF header, there is no specified order for header tables, sections, and segments
- Program header table
- Tells the system how to create a process image.
- Files used to build a process image (execute a program) must have a program header table.
- Relocatable files do not need a program header
- Smallest unit that can be processed within an ELF file
- Holds the bulk of object file information for the linking view (instructions, data, symbol table, and relocation information)
- Smallest individual unit that can be mapped to a memory image by exec(2) or by the runtime linker.
- Section header table
- Every section has an entry in the header table.
- Each entry gives information such as the section name and section size.
- Files that are used in link-editing must have a section header table.
- A section header table is not required in other object files.
Static Links and Dynamic Links
With current versions of Linux, almost all binaries are created by dynamically linking ELF binaries. A dynamic link is a format composed of the executable format and libraries (shared objects). A static link, on the other hand, contains libraries in executable format.
Features of static links
When creating static links, a library in *.a format and compiled objects are linked by the ld command. A library in the *.a format is an archive of objects compiled using the ar command.
A statically-linked binary has the following features:
1. Because a file contains multiple libraries, the file size becomes great.
2. You don’t have to be mindful of the libraries’ dependencies.
3. The version of the library used can be locked.
4. You can execute the program just by copying the file.
Features of dynamic links
When dynamic links are created, libraries in .so (shared object) format and compiled objects are linked. .so is dynamically read during execution time.
1. Because shared libraries (so) are dynamically linked, the size of the executable file is small.
2. The dependencies of libraries are automatically resolved.
3. Updating the version is possible at the library level.
4. Not only files but also dependent libraries must be copied. Otherwise, the program won’t work.
To be continued in the next column
In this installment, I explained the ELF format. In the next column, I’ll explain how ELF is executed and how to handle the ELF format.
How To Make A File Executable In Linux
However, when you make the change to a Linux distribution, this division of executable vs. non-executable does not apply. Any file can be made executable in Linux since it does not require a certain extension to be declared as an executable.
This provides a lot of flexibility when it comes to file management.
If you’re someone looking to learn how to make a file executable in Linux, then this guide is meant for you. We’ll provide a step-by-step solution on how you can make any file executable, either using the CLI or the GUI method.
Let’s take a look at the steps.
Method 1: Using The Command Terminal
The first method makes use of the Command Terminal. You can make any file executable by typing certain commands in the Terminal.
Although we’re going to be using Ubuntu 20.04 LTS in this guide, the steps shouldn’t be any different for other Linux distributions. With that said, just follow the steps explained with the help of an example.
First and foremost, open the Command Terminal on your system. The shortcut for Ubuntu is Ctrl + Alt + T.
For this guide, we’ll create a sample file using the echo command by using the following command:
This command will create a string literal with the text “This is a guide on how to make a file executable in Linux” and store it in a file called Test1.
To view the content of the file, type the following command:
You should notice that the file doesn’t end with an extension name. This means that you have the option to make the file executable.
In order to make Test1 an executable file, we’ll use the following command:
In case of a file extension, the command becomes:
Now you can call your file by typing its name in the Terminal as follow:
You can also execute the command in the following manner:
This will provide appropriate permission for the file to be executed.
If the CLI method seems complicated to you, worry not as you can achieve the same results with the help of the GUI.
Method 2: Using the GUI
Unlike the CLI method, the GUI method is much less daunting and simplified to understand what’s going on.
Follow these steps to make a file executable using the GUI.
Start by navigating to the file of your choice. Once you’ve navigated to the file, right-click on it and select “Properties”. A new window should appear.
Once a window opens, click on the Permissions tab.
In the Permissions tab, you should see an option titled “Allow executing file as program.”
You should now have the desired file in an executable format if you followed the steps correctly.
Understanding How File Execution Works
Learning how file execution works in Linux has its benefits as it provides more flexibility when it comes to an understanding of how the file works.
In Method 1, we used the command chmod +x. This was necessary in order to make the file executable since the file required “read” privileges. The “./” tells the Terminal to search for the location of the file.
Aside from Method 1, there are other ways to use the $ chmod command. This flexibility makes $ chmod extremely valuable. A list of options for the $ chmod command is given below:
- $ chmod 775 . This mode allows anyone to execute the file. However, only the owner of the file has permission to write in that file.
- $ chmod 0010 . Only users of a group will be allowed to execute the file.
- $ chmod 0100 . Permission to execute the file belongs exclusively to the user.
- $ chmod 777 . Provides permission to execute the file to all Linux users.
- $ chmod -777 . Doesn’t allow any user to execute the file.
Although the $ chmod command works for files without extension type, it should be noted that you’ll need to specify the file type in case it’s mentioned. For example, if you are dealing with a file that has a .run or .bin extension. The syntax for the execution command would be:
Additionally, make sure you have the correct name, file type, and file location before making any file executable.
If you followed the steps in the guide correctly, then the good news is you are now aware of how to make a file executable in Linux.
This guide covered different methods to make the file executable in Linux. We started by explaining the method involving the Command Terminal and followed up with the method to achieve the same with the help of the Graphical User Interface(GUI). We also covered additional uses of the $chmod command and the different permissions.
With this, we wish you all the best in your journey to master Linux.
About the author
Hi there! I’m a Software Engineer who loves to write about tech. You can reach out to me on LinkedIn.
Understanding the Linux Kernel, Second Edition by
Get full access to Understanding the Linux Kernel, Second Edition and 60K+ other titles, with free 10-day trial of O’Reilly.
There’s also live online events, interactive content, certification prep materials, and more.
The standard Linux executable format is named Executable and Linking Format ( ELF ). It was developed by Unix System Laboratories and is now the most widely used format in the Unix world. Several well-known Unix operating systems, such as System V Release 4 and Sun’s Solaris 2, have adopted ELF as their main executable format.
Older Linux versions supported another format named Assembler OUTput Format ( a.out ); actually, there were several versions of that format floating around the Unix world. It is seldom used now, since ELF is much more practical.
Linux supports many other different formats for executable files; in this way, it can run programs compiled for other operating systems, such as MS-DOS EXE programs or BSD Unix’s COFF executables. A few executable formats, like Java or bash scripts, are platform-independent.
An executable format is described by an object of type linux_binfmt , which essentially provides three methods:
Sets up a new execution environment for the current process by reading the information stored in an executable file.
Dynamically binds a shared library to an already running process; it is activated by the uselib( ) system call.
Stores the execution context of the current process in a file named core . This file, whose format depends on the type of executable of the program being executed, is usually created when a process receives a signal whose default action is “dump” (see Section 10.1.1).
Get Understanding the Linux Kernel, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.