Common ELF File Format segment names and custom segments

Source: Internet
Author: User

In addition to the. Text,. Data, And. BSS segments, there are other common segments, as shown below:

. Strtab: string table, used to store various strings used in elf files.

. Symtab: symbol table. Each symbol in the file can be used here.

. Shstrtab: The name table of each segment. It is actually a string array composed of the names of each segment.

. Hash: Symbol hash table.

. Line: The line number table during debugging, that is, the table corresponding to the source code line number and the compiled command.

. Dynamic: dynamic link information.

. Debug: debugging information.

. Comment: stores the compiler version information, such as "GCC :( GNU) 4.2.0 ".

. PLT and. Got: Dynamic Link jump table and global portal table.

. Init and. Fini: Program initialization and termination code segment.

. Rodata1: Read Only data, read-only data segment, which stores string constants and global const variables. This segment is the same as. rodata.

It can be seen from the above that the names of these segments have a '.' dot symbol prefix. Its Meaning indicates that the names of these segments are reserved by the system. The application can use a non-system reserved name as its own custom segment name, but the custom segment name cannot use '. the 'symbol is used as the prefix. Otherwise, it is easy to conflict with the segment names reserved by the system.

Two or more same-name segments are allowed in an elf file. For example, multiple. Text segments may be displayed.

You can use the objcopy command to take a common binary file, film, multimedia, and other things as a segment in the target file. The method is as follows:

[Beyes @ beyes elf] $ objcopy-I Binary-O elf32-i386-B i386 mypic.jpg temp. o
[Beyes @ beyes elf] $ objdump-T temp. o
Temp. O: File Format elf32-i386
Symbol table:
00000000 l d. Data 00000000. Data
00000000G. Data 00000000 _ binary_mypic_jpg_start
00011855G. Data 00000000 _ binary_mypic_jpg_end
00011855g * ABS * 00000000 _ binary_mypic_jpg_size

The objcopy command is used to copy part or all of the content of the target file to another target file and convert the format.
In the above objcopy command:
-I indicates the format of the input target file.

-O indicates the format of the target output file.

-Option B specifies the applicable platform for the input file to be transferred to the target file. I386 indicates that the image format is applicable to the i386 platform. However, because the BFD library used by objcopy has recognized the i386 platform, it is also possible to use the-B option here.

Convert the image file to the target file temp. by default, _ binary_objfile_start and _ binary_objfile_end are used to identify the start address and end address of the converted file in the memory, and _ binary_objfile_size is used to indicate the size.

Custom segments

Generally, in the target file compiled by GCC, the code is usually put in the. Text Segment, and the global variables and static variables are put in the. Data and. BSS segments. This is only a default action. Sometimes we may want variables or some code to be placed in a custom segment to implement certain functions, for example, in order to meet the memory and I/O address layout of some hardware, or for example, an error exception occurs during initialization and user control replication in the Linux kernel. GCC provides an extension mechanism that allows us to place variables in our custom segments. Example code:

 
#include <stdio.h> __attribute__((section("myvarsection"))) int global_var = 18; __attribute__((section("myfuncsection"))) void hello(void); void hello(void){        printf ("hello world\n");         printf ("my global_var:%d\n", global_var); } int main(){        hello();         return 0;}

View the target file:

[Beyes @ beyes elf] $ objdump-H sefsec. o

Sefsec. O: File Format elf32-i386

Sections:
Idx name size vma lma file off algn
0. Text 00000012 00000000 00000000 2 ** 2
Contents, alloc, load, reloc, readonly, code
1. Data 00000000 00000000 00000000 00000048 2 ** 2
Contents, alloc, load, Data
2. BSS 00000000 00000000 00000000 00000048 2 ** 2
Alloc
3 myvarsection 00000004 00000000 00000000 2 ** 2
Contents, alloc, load, Data
4. rodata 0000001e 00000000 00000000 0000004c 2 ** 0
Contents, alloc, load, readonly, Data
5 myfuncsection 0000002b 00000000 00000000 0000006a 2 ** 0
Contents, alloc, load, reloc, readonly, code
6. Comment 0000002e 00000000 00000000 00000095 2 ** 0
Contents, readonly
7. Note. GNU-stack 00000000 00000000 00000000 000000c3 2 ** 0
Contents, readonly
8. eh_frame 00000058 00000000 00000000 running 00c4 2 *** 2
Contents, alloc, load, reloc, readonly, Data

The program uses the GCC extension mechanism _ attribute _ (Section () to customize its own segments.


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.