GNU binutils notes

Source: Internet
Author: User
Tags srec

GNU binutils is a set of binary tools. Including addr2line ar GPROF nm objcopy objdump ranlib size strings strip. This article summarizes their common methods.
Ar

Ar is used to create, modify, and extract archive files ). Archive is a single file (also called a library file) that contains multiple files. Its structure ensures that the original contained files (called member in archive) can be retrieved and obtained ). Attributes such as the content, mode (permission), timestamp, and all operators and groups of the original member files are stored in archive. After member is extracted, their attributes are restored to the initial state.
Ar is mainly used to create C library files (for details about generation of. O target files and shared libraries, refer to GCC notes)
Create a static library
(1) generate the target file:

$ Gcc-wall-C file1.c file2.c file3.c

You do not need to specify the file name to generate. O (file1.o, file2.o, and file3.o are generated by default ).
(2) create a static connection library from the. O target file:

$ Ar RV libname. A file1.o file2.o file3.o

Ar generates libname. A library, which is used together with files in the warehouse.
R: insert flie1.o, file2, O, file3.o to archive. If a file already exists in archive, delete it first.
V: displays additional information about the AR operation, such as the processed member file name)
NOTE: For the BSD system, you also need to create an index after creating a static library: $ ranlib libname. A Linux does not need this step (it is harmless to run it ).
Create a dynamic library (using gcc without Ar)
(1) generate the target file

$ Gcc-wall-C-FPIC file1.c file2.c file3.c

-FPIC: specifies that the generated. O target file can be reconfigured. PIC is the abbreviation of position idependent code: location-independent code.
(2) generate a dynamic library file

$ Gcc-shared-O libname. So file1.o file2.o file3.o

Generally, the connector uses the main () function as the program entry, but there is no such entry in the dynamic shared library. Therefore, you must specify the-shared option to prevent the compiler from displaying error information.
In fact, the above two commands can be combined into the following:

$ Gcc-wall-shared-FPIC-O libname. So file1.c file2.c file3.c

After that, connect the program where the main function is located to libname. So (note that the library connection path and header file contain the path and the connection sequence! Refer to GCC notes)
At this point, the function connected to the dynamic library is compiled into an executable file. It seems to have succeeded, but it is still the last step. If you run the program directly, the following error message is displayed:

Error while loading shared libraries: libhello. So:
Cannot open shared object file: no such file or directory

This is because the program connected to the dynamic library first loads the dynamic library into the memory at runtime, while the GCC default directory for loading the dynamic library file is/usr/local/lib, /usr/lib. Although the program can be compiled successfully, if the dynamic library we created is not in the default directory, the execution will fail because it cannot be found.
Solution: change the environment variable corresponding to the loading path and then execute the command.

Export LD_LIBRARY_PATH = directory of the dynamic library: $ LD_LIBRARY_PATH

View archive content

$ Ar TV archivename

T: displays the content of the Member in archive. If no member is specified, it lists all.
V: displays detailed information about Member when used with T.
For details about the AR options, refer to the on-line manual of AR.
Nm

Nm is used to list symbols in the target file. It helps programmers locate and analyze the symbolic information and its attributes in the execution program and target file.
If no target file is passed as a parameter to nm, Nm assumes that the target file is a. Out.
Here we use a simple example program to introduce the usage of NM:
Main. C:

Int main (INT argc, char * argv [])
{
Hello ();
Bye ();
Return 0;
}

Hello. C:

Void Hello (void)
{
Printf ("Hello! /N ");
}

Bye. C:

Void Bye (void)
{
Printf ("good bye! /N ");
}

Run the following command:
$ Gcc-wall-C main. c hello. c bye. c
GCC generates three target files, Main. O, hello. O, and bye. O. (The function prototype is not declared here. If-wall is added, GCC will give a warning)
$ Nm main. O hello. O bye. o
The result is as follows:

Main. O:
U bye
U hello
00000000 t Main
Hello. O:
00000000 t hello
U puts
Bye. O:
00000000 t bye
U puts

Based on these output results and program code, you can know:
Main. O, bye, and hello are not defined, and main is defined
Hello. O, hello is defined, puts is not defined
For bye. O, bye is defined, and puts is not defined
Several noteworthy issues:
(1) "target file" refers to the. o file, library file, and final Executable File
. O: The compiled target file contains the final compiled machine code, but the memory location of the function in other files referenced in it is not yet defined.
(2) If you use nm to view executable files, there will be a lot of output. If you study the output carefully, you can have a clearer understanding of the usage of nm.
(3) In the preceding hello. c, bye. in C, printf () is called, and the nm output shows that puts () is called, which indicates that the final program actually calls puts (). If hello. C or bye. if printf () in C uses formatted output, the NM display calls printf (). (For example, printf ("% d", 1 );)
For more information about the NM Parameter options, see on-line manual.
Objcopy

Objcopy can convert a target file in one format to a target file in another format. it uses the gnu bfd library to read/write the target file. with BFD, objcopy can convert the target file in the original format to a target file in different formats.
Take the hello. O target file and hello executable file used in nm as an example:

$ File hello. O hello

The file command is used to determine the file type. The output is as follows:
Hello. O: Elf 32-bit LSB relocatable, Intel 80386, Version 1 (sysv), not stripped
Hello: Elf 32-bit LSB executable, Intel 80386, Version 1 (sysv), for GNU/Linux 2.2.0, dynamically linked (uses SHARED libs), not stripped
Now run objcopy to change the file type of Hello: It was originally an executable program in ELF format and is now converted to SREC format. the SREC format file is a Motolora S-record file, which is mainly used to transmit data between the host and the target machine.

$ Objcopy-o srec Hello hello_srec
$ File hello. O hello

FILE command result: hello_srec: Motorola S-record; binary data in text format
Note the objcopy format. "-o" specifies the output file type. The input file name and output file name are at the end of the command. For more information about the objcopy command, see on-line manual.
Objdump

Objdump is used to display the information of the target file. you can use option control to display specific information. the biggest use of objdump is probably to disassemble the C code. during embedded software development, you can also use it to view information about execution files or library files.
The following uses the hello executable file and hello_srec executable file mentioned above as an example to describe the simple usage of objdump:

$ Objdump-F Hello hello_srec

The output is as follows:
Hello: File Format elf32-i386
Architecture: i386, flags 0x00000112:
Exec_p, has_syms, d_paged
Start address 0x080482c0
Hello_srec: File Format SREC
Architecture: Unknown !, Flags 0x00000000:
Start address 0x00000000080482c0
-F: displays the overview of the header file of the target file.
Generate disassembly code:

$ Objdump-d hello. o

Shown as follows:
Hello. O: File Format elf32-i386
Disassembly of section. Text:
00000000:
0: 55 push % EBP
1: 89 E5 mov % ESP, % EBP
3: 83 EC 08 Sub $0x8, % ESP
6: 83 EC 0C sub $ 0xc, % ESP
9: 68 00 00 00 push $0x0
E: E8 fc ff call f
13: 83 C4 10 Add $0x10, % ESP
16: C9 leave
17: C3 RET
-D: displays the assembly language used by machine commands in the target file. only disassemble the sections that should contain the instruction machine code (display. text Segment); If-D is used, the contents of all sections will be disassembled.
For detailed options of the objcopy command, refer to on-line manual
Readelf

Readelf is used to display the information of the target file in ELF format. you can use Parameter options to control specific information displayed. (Note: readelf does not support displaying archive documents or 64-bit elf files ).
The following uses the preceding Hello executable file to demonstrate the simple usage of readelf:

$ Readelf-H hello

Elf header:
Magic: 7f 45 4C 46 01 01 00 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: 0x80482c0
Start of program headers: 52 (bytes into file)
Start of section headers: 3848 (bytes into file)
Flags: 0x0
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 7
Size of section headers: 40 (bytes)
Number of section headers: 34
Section header string table index: 31
Note: readelf can only be used for the target file in ELF format. You must specify at least one (except V and h) option in the option!
GPROF

GPROF is used to measure program performance. it records the number of calls of each function and the corresponding execution time. in this way, the most time-consuming part of the program execution can be locked, and the optimization of the program can be concentrated on their optimization.
Use a simple numeric program to conceal GPROF usage:
Collatz. C:

# Include
/* Computes the length of Collatz sequences */
Unsigned int step (unsigned int X)
{
If (X % 2 = 0)
{
Return (X/2 );
}
Else
{
Return (3 * x + 1 );
}
}
Unsigned int nseq (unsigned int X0)
{
Unsigned int I = 1, x;
If (X0 = 1 | X0 = 0)
Return I;
X = step (x0 );
While (X! = 1 & X! = 0)
{
X = step (X );
I ++;
}
Return I;
}
Int main (void)
{
Unsigned int I, m = 0, Im = 0;
For (I = 1; I <500000; I ++)
{
Unsigned int K = nseq (I );
If (k> m)
{
M = K;
Im = I;
Printf ("sequence length = % u for % u/N", M, Im );
}
}
Return 0;
}

First, compile Collatz. c into the target file Collatz. O. GCC uses the-PG option to enable GPROF support:

$ Gcc-wall-C-PG Collatz. c

$ Gcc-wall-PG-O Collatz. o

Note: The "-PG" option must be added to both commands. The previous command generates the Collatz. O target file. The next command generates an executable file that contains commands that record the function execution time.
After the Collatz executable file is generated, the result is undoubtedly the same as that of the general program. However, a file named "gmon. Out" is generated in the PWD directory, and GPROF uses it to analyze program execution.
If you use GPROF to analyze the program without executing it, the message "gmon. Out: no such file or directory" is displayed ".
GPROF usage:

$ GPROF./Collatz

For more details about GPROF, refer to GPROF's on-line manual.

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.