ELF: Difference between revisions

253 bytes added ,  3 years ago
Removed irrelevant information relating to GeekOS and added generic steps to loading an ELF executable.
[unchecked revision][unchecked revision]
(german grammar mistake correction)
(Removed irrelevant information relating to GeekOS and added generic steps to loading an ELF executable.)
Line 122:
 
[[Image:Elfdiagram.png|frame|Executable image and elf binary can being mapped onto each other]]
The ELF file format is described in the [http://www.skyfree.org/linux/references/ELF_Format.pdf ELF Specification]. The most relevant sections for this project are 1.1 to 1.4 and 2.1 to 2.7.
 
The steps involved in identifying the sections of the ELF file are:
 
* Read the ELF Header. The ELF header will always be at the very beginning of an ELF file. The ELF header contains information about how the rest of the file is laid out. YouAn areexecutable interestedloader is only inconcerned with the program headers.
* Find the Program Headers,. whichThese specify where in the file to find the text and data sections and where they should end up in the executable image.
 
The following is a rough outline of the steps that an ELF executable loader must perform:
There are a few simplifying assumptions you can make about the types and location of program headers. In the files you will be working with, there will always be one text header and one data header. The text header will be the first program header and the data header will be the second program header. This is not generally true of ELF files, but it will be true of the programs you will be responsible for.
 
#* CheckVerify that the file starts with the ELF magic number (4 bytes) as described in figure 1-4 (and subsequent table) on page 11 in the ELF specification.
The file geekos/include/geekos/elf.h provides data types for structures which match the format of the ELF and program headers.
* Parse the ELF executable's program headers to determine the number of program segments that must be loaded. Each program header has an associated type, as described in Figure 2-2 of the ELF specification. Only those with a type of <tt>PT_LOAD</tt> describe a loadable segment.
 
* Load each of the loadable segments. This is performed as follows:
This is a rough guideline for what Parse_ELF_Executable() has to do:
** Allocate virtual memory for each segment, at the address specified by the <tt>p_vaddr</tt> member in the program header. The size of the segment in memory is specified by the <tt>p_memsz</tt> member.
 
** Copy the segment data from the file offset specified by the <tt>p_offset</tt> member to the virtual memory address specified by the <tt>p_vaddr</tt> member. The size of the segment in the file is contained in the <tt>p_filesz</tt> member. This can be zero.
# Check that exeFileData is non-null and exeFileLength is large enough to accommodate the ELF headers and phnum program headers.
** The <tt>p_memsz</tt> member specifies the size the segment occupies in memory. This can be zero. If the <tt>p_filesz</tt> and <tt>p_memsz</tt> members differ, this indicates that the segment is padded with zeros. All bytes in memory between the ending offset of the file size, and the segment's virtual memory size are to be cleared with zeros.
# Check that the file starts with the ELF magic number (4 bytes) as described in figure 1-4 (and subsequent table) on page 11 in the ELF specification.
* Read the executable's entry point from the ELF header.
# Check that the ELF file has no more than EXE_MAX_SEGMENTS program headers (phnum field of the elfHeader).
* Jump to the executable's entry point in the newly loaded memory.
# Fill in numSegments and entryAddr fields of the exeFormat output variable.
# For each program header k in turn, fill in the corresponding segmentList[k] array element of exeFormat with offsetInFile, lengthInFile, startAddress, sizeInMemory, protFlags with information from that program header k. See figure 2-1 on page 33 in the ELF specification.
 
==Relocation==
Anonymous user