"Programmer self-cultivation" read notes-what's in the target file?

Source: Internet
Author: User

The target file (. o file) under Linux is in elf format. The target file is stored in the format of section sections, such as code snippets (stored code), data segments (initialized global variables and local static variables), BSS (uninitialized global and local static variables), read-only data segments (strings used in the program), and so on. In all segments, there is an important structure called the file header. So here it starts with the file header.

1 The file header mainly contains the following (nothing to write about it):

(1) E_ident: An important part of this is the number of magic numbers and the number of bits of the machine. Magic number is used to identify the type of file, such as Elf, a.out Ah, MZ ah and so on. The number of machine bits is 32 or 64.

(2) E_type: There are three kinds, can relocate the file (is the target file), executable file, share the target file (that is, dynamic link library file)

(3) E_shoff: Cong offset in the file (next, the section table related content)

(4) E_ehsize: The size of the file header itself

(5) E_shentsize: The size of the segment table descriptor. There are a lot of items in the table, and this is what the size of each item is (the next section will say something about the table)

(6) E_shnum: The number of paragraph table descriptors (next, the section table related content)

(7) E_shstrndx: The subscript of the paragraph table string table in the paragraph table. The Cong string table is a table that stores the names of all the segments. The name of the general Segment table string table may be. shstrtab (next section table related content)

From here you can see the position of the segment table (E_shoff), the number of items (E_shnum) in the segment table, so that you can parse all the segments from the segment table. The following is the relevant content of the paragraph table.

2 Section table. There are many items in the paragraph table. Each item mainly contains the following content:

(1) Sh_name: This value records the name of the segment subscript in the Segment table string table

(2) Sh_type: The type of segment. The main types are program segments, symbol table segments, string tables, relocation tables, hashtable, dynamic link information, etc.

(3) Sh_flag: section of the flag, there are three kinds: writable, need to allocate space, executable

(4) SH_ADDR: Segment virtual address, which is loaded after the address of the process space;

(5) Sh_offset: Offset of segment

(6) Sh_size: Length of segment

(7) Sh_link,sh_info: Link information for paragraph. If the content of a segment is related to a link, these two values make sense. such as dynamic link symbol table Ah, reposition the table Ah, sh_link that these tables use a string table or symbol table in the paragraph table subscript, Sh_info represents the relocation table role of the segment in the table subscript.

(8) Sh_addralign: Alignment Information

(9) Sh_entsize: The length of the item. Some segments, such as the symbol table, have the same size for each item, so each item size is required.

3 Relocation Table : If you need to reposition the code snippet, there will be a relocation segment for the code snippet, such as. rel.text; If you need to relocate the data segment, you also need a dedicated relocation table, such as. Rel.data. Each relocation segment is a dedicated segment. Its sh_link represents the subscript of the symbol table, and Sh_info indicates which segment it acts on. The detailed structure of the relocation table is described in the next reading note.

4 string Table: The string table is the string used to put together, and then each string used in the place only need to give the string table subscript. The General string table holds the string used in the program, while the Segment table string table is used to store the string in the segment table, such as the segment name.

5 symbol table. a symbol is an interface for linking. A symbol table is made up of many items. Each item contains the following content:

(1) St_name: symbol names are subscript in the string table

(2) St_value: To put it simply, this represents the address of a symbol (function, variable, and so on), and most of the time this value represents the offset of the symbol relative to the segment in which it is located. In the executable file, this represents the virtual address of the symbol (this is useful for dynamic linking).

(3) St_size: symbol size, such as double size is 8 bytes

(4) St_info: Its fourth bit represents the binding information of the symbol (local symbol, global symbol, weak reference), high 28-bit notation type (data ah, function ah, etc.)

(5) Sh_shndx: If the symbol is defined in this target file, this represents the subscript of the segment where the symbol is located.

"Programmer self-cultivation" read notes-what's in the target file?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.