In addition to the most common 3 paragraphs for. Text,. Data,. BSS, there are some other common segments, as follows:
The. strtab:string table, which is used to store the various strings used in the ELF file.
. symtab:symbol table symbol, from here you can make each symbol in the file.
. Shstrtab: Is the name table of each segment, which is actually an array of strings consisting of the names of each segment.
. Hash: symbolic hash table.
. Line: The row number table when debugging, that is, the source code line number and the corresponding table of the compiled instruction.
. Dynamic: Dynamically linked information.
. Debug: Debugging information.
. Comment: Holds compiler version information, such as "GCC: (GNU) 4.2.0".
. Plt and. Got: Dynamically linked jump tables and global entry tables.
. Init and. Fini: Program initialization and finalization of code snippets.
. rodata1:read only data, read-only segments, holding string constants, global const variables, which are the same as. Rodata.
Visible from the above, the names of these segments have a '. ' dot symbol prefix, meaning that the names of these segments are reserved by the system. An application can use a name that is not a system reserved for its own custom segment name, but the custom segment name cannot be prefixed with the '. ' Symbol, otherwise it is easy to conflict with the system-reserved segment name.
An ELF file allows two or more of the same name to exist, for example, you may see multiple. Text segments.
You can use the objcopy command to make a common binary file, such as pictures, multimedia, and so on as a segment of the target file. The production 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. Data 00000000. Data
00000000 G. Data 00000000 _binary_mypic_jpg_start
00011855 g. Data 00000000 _binary_mypic_jpg_end
00011855 g *abs* 00000000 _binary_mypic_jpg_size
The objcopy command is used to copy some or all of the contents of the target file to another destination file and to implement a format transformation.
In the objcopy command above:
The-I option indicates the destination file format for the specified input.
The-o option represents the destination file format for the specified output.
The-B option specifies the appropriate platform for the input file to be transferred to the destination file. i386 specifies that the picture format applies to the I386 platform. However, because the i386 platform is already recognized in the BFD library used by Objcopy, it is also possible to use the-B option here.
After converting the picture file into the target file TEMP.O using Objcopy, _binary_objfile_start and _binary_objfile_end are used to identify the starting address of the converted file in memory, the ending address, and the use of _binary _objfile_size to indicate size.
Custom Segment
Typically, in the object file that GCC compiles, the code is typically placed in the. Text segment, and global and static variables are placed in the. Data and. BSS segments. This is just the default behavior, we may sometimes want a variable or some code to be placed in a custom segment to implement certain features, such as the memory and I/O address layouts for certain hardware, or, as in the Linux kernel, to perform some initialization and error exceptions that occur with user control replication. GCC provides an extension mechanism that allows us to place variables in our custom segment. The following code example:
#include <stdio.h>
__attribute__ ((Section ("Myvarsection")) int global_var =;
__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;
}
To view the appropriate 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 00000034 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 00000048 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 000000c4 2**2
CONTENTS, ALLOC, LOAD, RELOC, READONLY, DATA
The extension mechanism __attribute__ (section ()) that uses GCC in your program comes from defining your own segments.