Elf File Headers

ELF file headers

I don't know of linker script commands that can do this, but you can do it post-link using the objcopy command. The --add-section option can be used to add a section containing arbitrary data to the ELF file. If the ELF header doesn't contain the fields you want, just make a new section and add them there.

ELF files - What is a section and why do we need it?

  1. How are ELF files generated? is it the compiler responsibility?

    They can be generated by a compiler, an assembler, or any other tool that can generate them. Even your own program you wrote for generating ELF files ;) They're just streams of bytes after all, so they can be generated by just writing bytes into a file in binary mode. You can do that too.

  2. What are sections and why do we need them?

    ELF files are subdivided into sections. Sections are the smallest continuous regions in the file. You can think of them as pages in an organizer, each with its own name and type that describes what does it contain inside. Linkers use this information to combine different parts of the program coming from different modules into one executable file or a library, by merging sections of the same type (gluing pages together, if you will).

    In executable files, sections are optional, but they're usually there to describe what's in the file and where does it begin, and how much bytes does it take.

  3. What are program headers and why do we need them?

    They're mostly for making executable files. In order to run a program, sections aren't enough, because you have to specify not only what's there in the file, but also where should it be loaded into memory in the running process. Program headers are just for that purpose: they describe segments, which are regions of memory in the running process, with different access privileges & stuff.

    Each program header describes one segment. It tells the loader where should it load a certain region in the file into memory and what permissions should it set for that region (e.g. should it be allowed to execute code from it? should it be writable or just for reading?)

    Segments can be further subdivided into sections. For example, if you have to specify that your code segment is further subdivided into code and static read-only strings for the messages the program displays. Or that your data segment is subdivided into funky data and hardcore data :J It's for you to decide.

    In executable files, sections are optional, but it's nice to have them, because they describe what's in the file and allow for dumping selected parts of it (e.g. with the objdump tool). Sometimes they are needed, though, for storing dynamic linking information, symbol tables, debugging information, stuff like that.

  4. Inside program headers, what's the meaning of the fields p_vaddr and p_paddr?

    Those are the addresses at which the data in the file will be loaded. They map the contents of the file into their corresponding memory locations. The first one is a virtual address, the second one is physical address.

    Physical addresses are the "raw" memory addresses. On modern operating systems, those are no longer used in the userland. Instead, userland programs use virtual addresses. The operating system deceives the userland program that it is alone in memory, and that the entire address space is available for it. Under the hood, the operating system maps those virtual addresses to physical ones in the actual memory, and it does it transparently to the program.

    Of course, not every address in the virtual address space is available at the same time. There are limitations imposed by the actual physical memory available. So the operating system just maps the memory for the segments the program actually uses (here's where the "segments" part from the ELF file's program headers comes into play). If the process tries to access some unmapped memory, the operating system steps in and says, "sorry, chap, this memory doesn't belong to you". (The program can address it, but it cannot access it.)

  5. Does each section have it's own section header?

    Yes. If it doesn't have an entry in the Section Headers Table, it's not a section :q Because they only way to tell if some part of the file is a section, is by looking in to the Section Headers Table which tells you what sections are defined in the file and where you can find them.

    You can think of the Section Headers Table as a table of contents in a book. Without the table of contents, there aren't any chapters after all, because they're not listed anywhere. The book may have headings, but the content is not subdivided into logical chapters that can be found through the table of contents. Same goes with sections in ELF files: there can be some regions of data, but you can't tell without the "table of contents" which is the SHT.

Some beginner questions about elf files, sections headers and how they work in general when we run an application

There are multiple inaccuracies in your description, and it's unclear whether you are imprecise in understanding the processing involved, or in describing them.

When you write a C application and you compile it(say with gcc) it gets translated into machine instructions which represent code and data.

This isn't entirely accurate: there is a difference between "machine instructions" and "machine code".

When you compile a .c file, some compilers will translate it into machine instructions (assembly), and then pass it to assembler to produce machine code (GCC does that). Other compilers have integrated assembler, and effectively skip the assembly generation step (Clang does that).

The output of invoking the compiler is an elf file.

On some but not all systems, the result of compilation is a relocatable ELF file. Other systems produce object files in a different format, such as XCOFF or Mach-O.

The elf file contains(among other things) a section header which is basically a series of Elf64_Shdr each for every section your compiled application contains.

The application is not built yet, so this is inaccurate. Also, Elf64_Shdr only applies to 64-bit ELF platforms; on 32-bit machines it's Elf32_Ehdr.

When we run the make command

The make command has nothing to do with anything. It just invokes compiler and linker (or other tools) as appropriate. You can replace it with a shell script, or just type the commands by hand.

and pass it the elf file

The link step involves one or more (usually more) relocatable ELF object files, archive libraries and dynamic libraries.

the linker comes into play and looks at all sections created by the compiler, at their names and attributes and groups them into 'segments' following the rules of a ld script file

To understand what the linker does you can read this series of blog posts.

Your description trivializes what the linker does. The linker is much more complicated, and performs relocation resolution which you didn't mention, and many other tasks.

and creates the executable file which we can run.

Usually true.

You can ask linker to combine several relocatable object files into a combined object file (with ld -r foo.o bar.o -o combined.o), and in that case the result will not be an executable file.

You can also ask the linker to link a shared library instead of linking an executable.

So basically segments are nothing more than sections of same attributes grouped togheter in a common section with a specific name.

False. There is lot more to linking than grouping sections together.

Then when we actually run the created executable the loader comes into play

The loader only comes into play for dynamically-linked executables. Fully-static executables do not have a loader, and are started directly by the kernel itself.

and looks at these segments created by the linker and by reading this information which they contain it maps the machine instructions to various memory locations so the process can run. This is what is called(in my understanding) a memory image.

Mostly correct. Some parts of the memory image do not come from disk at all (e.g. thread-local storage and contents of combined .bss sections)



Related Topics



Leave a reply



Submit