Http://blog.chinaunix.net/uid-9525959-id-2001833.html
Readelf <option (s) > Elf-file (s)
[function]
Used to display information about an elf format file.
[description]
Readelf is used to display information about one or more object files in the elf format, and you can control which information is displayed by its options. The Elf-file (s) here represents the files that were checked. Supports 32-bit, 64-bit ELF-format files, and documents containing ELF files (generally referred to as "Static library" files that are generated after packaging some elf files with the AR command, such as LIB*.A).
This program is similar to the functionality provided by Objdump, but it displays more specific information, and it does not rely on the BFD library (the BFD library is a GNU project that aims to process different target files through a unified interface). So even if there are any bugs in the BFD library, it will not affect the Readelf program.
When running readelf, other options must be specified except for-V and-H. Introduction to Elf Files
Elf File type:
A relocatable file: Users and other target files create executable files or share target files, such as lib*.a files.
b executable file: Used to generate the process image, load memory execution, such as the compiled executable file a.out.
c) Shared destination file: Used to generate Elf target files with other shared target files or relocatable files or create process images, such as lib*.so files, along with the execution files.
elf file function:
The elf file participates in the program's connection (establishing a program) and the execution of the program (running a program), so you can view the files in the ELF format from different perspectives:
A if used for compilation and linking (relocatable files), the compiler and linker will think of the elf file as a collection of sections described in the section Header table, optional for the Program Header table.
b if used to load execution (executable), the loader will think of the elf file as a collection of segments described in the Program Header table, one segment may contain multiple sections, and a section header table optional.
C If the file is shared, both are included.
elf Files Overall composition:
The elf file header describes the overall information for the elf file. Including:
system-related, type-related, load-related, link-related.
System-related representations: The magic number of elf file identification, as well as hardware and platform-related information, the increase of the Elf file portability, so that the possibility of cross compilation.
Type-Related: This is the Elf file type mentioned earlier.
Load Related: Includes information about the Header table of the program.
Link Related: section Header table related information.
readelf command Options (given in long and short format, respectively):
-A
--all displays all information, equivalent to-h-l-s-s-r-d-v-a-I.
-H
--file-header Displays the file header information that the elf file starts with.
-L
--program-headers
--segments Displays the program header (segment header) information, if any.
-S
--section-headers
--sections Displays the section header information (if any).
-G
--section-groups Displays the section group information (if any).
-T
--section-details Displays the details of the section (-s).
-S
--syms
--symbols Displays the items (if any) in the symbol table segment.
-E
--headers Display all header information, equivalent to:-h-l-S
-N
--notes Displays the information for the note segment (kernel annotation).
-R
--relocs displays information about the relocatable segment.
-U
--unwind Displays unwind segment information. Only the unwind segment information of the IA64 Elf is currently supported.
-D
--dynamic displays information about the dynamic segment.
-V
--version-info Displays the information for the version segment.
-A
--ARCH-SPECIFIC Displays CPU architecture information.
-D
Instead of using symbol segments,--use-dynamic uses symbol tables in dynamic segments to display symbols.
-X <number or name>
--hex-dump=<number or name> Displays the contents of the specified paragraph in 16-way. Number specifies the index in the middle of the section table, or the string specifies the name of the segment in the file.
-w[liaprmffsor] or
--debug-dump[=line,=info,=abbrev,=pubnames,=aranges,=macro,=frames,=frames-interp,=str,=loc,=ranges]
Displays the contents specified in the Debug segment.
-I.
--histogram displays a histogram of the length of the bucket list when the symbol is displayed.
-V
--version Displays the Readelf version information.
-H
--HELP Displays the command line options supported by Readelf.
-W
--wide wide row output.
@file can centralize options in one file and then use this @file option to load.
[For example]
First give the following examples:
1, for an executable file in the form of an elf format file:
1 View the source code of the executable program as follows:
[Quietheart@lv-k cppdemo]$ Cat Main.cpp
#include <iostream>
Using Std::cout;
Using Std::endl;
void My_print ();
int main (int argc, char *argv[])
{
My_print ();
cout<< "Hello!" <<endl;
return 0;
}
void My_print ()
{
cout<< "print!" <<endl;
}
2) compiled as follows:
[Quietheart@lv-k cppdemo]$ g++ Main.cpp-o Main
[Quietheart@lv-k cppdemo]$ g++-G main.cpp-o main.debug
3 after compiling, view the generated files:
[Quietheart@lv-k cppdemo]$ Ls-l
Total 64
-rwxr-xr-x 1 quietheart quietheart 6700 07-07 18:04 Main
-rw-r--r--1 Quietheart quietheart 201 07-07 18:02 Main.cpp
-rwxr-xr-x 1 quietheart quietheart 38932 07-07 18:04 main.debug
Here, Main.debug is an executable file with debug information, and main is a generic executable file.
2, for the library file form of the elf format file:
1 View the source code of the library as follows:
MyFile.h
#ifndef __myfile_h
#define __myfile_h
void Printinfo ();
#endif
MyFile.cpp
#include "myfile.h"
#include <iostream>
Using Std::cout;
Using Std::endl;
void Printinfo ()
{
cout<< "Hello" <<endl;
}
2) compiled as follows:
[Quietheart@lv-k bak]$ g++-C myfile.cpp
[Quietheart@lv-k bak]$ g++-shared-fpci-o libmy.so MYFILE.O
[Quietheart@lv-k bak]$ ar-r libmy.a MYFILE.O
Ar:creating libmy.a
3 after compiling, view the generated files:
[Quietheart@lv-k bak]$ Ls-l
Total 44
-rw-r--r--1 quietheart quietheart 2154 07-08 16:14 libmy.a
-rwxr-xr-x 1 quietheart quietheart 5707 07-08 16:08 libmy.so
-rwxr-xr-x 1 Quietheart quietheart 117 07-08 16:06 MyFile.cpp
-rwxr-xr-x 1 quietheart quietheart 07-08 16:08 myfile.h
-rw-r--r--1 quietheart quietheart 07-08 16:08 MYFILE.O
Libmy.a libmy.so myfile.cpp myfile.h MYFILE.O
Here, the target file myfile.o, shared library file libmy.so, and static library file libmy.a are generated separately.
Based on the above executable files and libraries, here are some common commands.
* Read the Elf file header information in the executable file form:
[Quietheart@lv-k cppdemo]$ readelf-h Main
After the input, the output information is as follows:
ELF Header:
magic:7f 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class:elf32
Data:2 ' s complement, little endian
Version:1 (current)
Os/abi:unix-system V
ABI version:0
Type:exec (executable file)
Machine:intel 80386
version:0x1
Entry Point address:0x8048580
Start of program headers:52 (bytes into file)
Start of section headers:3232 (bytes to file)
flags:0x0
Size of this header:52 (bytes)
Size of Program headers:32 (bytes)
Number of program Headers:8
Size of section headers:40 (bytes)
Number of section headers:29
Section header string Table index:26
Here, the elf file of the executable file is visible, and its type is exec (executable file). In addition, "Main.debug" with debug information and "Main" without debugging information are the same content except for some size information. And this shows that the file's architecture is Intel 80386.
* Read the Elf file header information in the target file form:
[Quietheart@lv-k bak]$ readelf-h MYFILE.O
After the input, the output information is roughly as follows:
ELF Header:
magic:7f 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class:elf32
Data:2 ' s complement, little endian
Version:1 (current)
Os/abi:unix-system V
ABI version:0
Type:rel (relocatable file)
Machine:intel 80386
version:0x1
Entry Point address:0x0
Start of program headers:0 (bytes into file)
Start of section headers:516 (bytes to file)
flags:0x0
Size of this header:52 (bytes)
Size of Program headers:0 (bytes)
Number of program headers:0
Size of section headers:40 (bytes)