Ar, nm, LDD command

Source: Internet
Author: User

When there are frequently used modules in our program and such modules are also used in other programs, we should generate libraries based on the idea of software reuse, so that later programming can reduce the amount of development code. Here we will introduce the two commands AR and Nm for database operations.

1. Basic ar usage
2. Basic usage commands of Nm

When there are frequently used modules in our program and such modules are also used in other programs, we should generate libraries based on the idea of software reuse, so that later programming can reduce the amount of development code. Here we will introduce the two commands AR and Nm for database operations.

1. Basic ar usage

The ar command can be used to create and modify a database, or to propose a single module from the database. The library is a separate file that contains other files organized according to a specific structure (called the member of the library file ). Attributes of the original file, such as content, mode, timestamp, owner, and group, are kept in the library file.

The format of the Ar command is as follows:

Ar [-] {dmpqrtx} [abcfilnopssuvv] [membername] [count] archive files...

For example, we can use ar rv libtest. A hello. O hello1.o to generate a library named test, which can be linked with-ltest. The library contains two modules: Hello. O and hello1.o. The option can contain '-' or none. Next let's take a look at the operation options and any options of the command. Now we call the {dmpqrtx} part as an operation option, and the [abcfilnopssuvv] Part as any option.

The operation options in {dmpqrtx} can only be used in the command and must use one of them. Their meanings are as follows:

D: delete a module from the database. Specify the module to be deleted based on the original file name of the module. If any option V is used, each module to be deleted is listed.
M: This operation is a member moving in a library. If several modules in the library have the same symbolic definition (such as function definition), the order of member positions is very important. If no option is specified, any specified member will be moved to the end of the database. You can also use the 'A', 'B', or 'I' option to move to the specified position.
P: displays the specified members in the database to the standard output. If any option V is specified, the member name is displayed before the content of the output member. If no member name is specified, all files in the database are displayed.
Q: Fast append. Add a new module to the end of the database. Do not check whether replacement is required. The 'A', 'B', or 'I' option does not affect this operation. The module always appends the end of the library. If any option V is used, each module is listed. The symbol table of the library is not updated. You can use 'ar s' or ranlib to update the index of the symbol table of the library.
R: Insert a module (replace) into the database ). If the inserted Module name already exists in the Database, replace the module with the same name. If one of the modules does not exist in the library, ar displays an error message and does not replace other modules with the same name. By default, new members are added at the end of the database. You can use other options to change the position of the added member.
T: displays the list of modules in the database. Generally, only the module name is displayed.
X: extract a member from the database. If the module to be extracted is not specified, all modules in the library are extracted.
The following describes any options that can be used with operation options:

A: Add a new file to an existing member of the database. If you use any option A, you should specify an existing member name for the membername parameter in the command line.
B: Add a new file before an existing member of the database. If option B is used, an existing member name should be specified for the membername parameter in the command line.
C: Create a database. Whether or not the database exists will be created.
F: truncates the specified name in the database. By default, the file name length is unrestricted. You can use this parameter to shorten the file name to ensure compatibility with other systems.
I: Add a new file before an existing member of the database. If any option I is used, you should specify an existing member name (similar to any option B) for the membername parameter in the command line ).
L: Not used yet
N: used with the Count parameter to specify the number of extracted or output files when multiple identical file names exist in the database.
O: when a Member is extracted, the original data of the member is retained. If this option is not specified, the extracted module time is marked as the extracted time.
P: use the full path name for file name matching. Ar cannot use full path names when creating a library (such library files do not comply with POSIX standards), but some tools are acceptable.
S: Write a target file index to the database, or update an existing target file index. This action is even performed for libraries that have not changed. Doing ar s for a library is equivalent to doing ranlib for the library.
S: do not create the target file index, which can speed up the time when creating a large database.
U: Generally, the command ar r... insert all listed files to the database. If you only want to insert files in the list file that are new to files with the same name as the files in the database, you can use this option. This option is only used for r operation.
V: This option is used to display additional information about the operation options.
V: displays the AR version.
2. Basic usage commands of Nm

Nm is used to list the symbols of the target file. The following is the format of the NM command:

NM [-A | -- debug-Syms] [-G | -- extern-only] [-B] [-c | -- demangle] [-d | -- Dynamic] [-S | -- print-armap] [-o | -- print-file-name] [-N | -- Numeric-Sort] [-p | -- no-Sort] [-r | -- reverse-Sort] [-- size-Sort] [-u | -- undefined-only] [-L | -- line-numbers] [-- help] [-- version] [-T Radix | -- Radix = Radix] [-p | -- portability] [-F format | -- format = format] [-- target = bfdname] [objfile...]

If the target file is not specified for the NM command, Nm assumes that the target file is a. Out. The following lists any options for this command. Most options include short formats starting with "-" And long formats starting.

-A,-o, or -- print-file-Name: add the file name before the names of the symbols found, instead of displaying the file name only once before all the symbols in the file.
For example, the output of NM libtest. A is as follows:

Cpthread. O:
00000068 t Main _ 8 cpthreadpv
00000038 t start _ 8 cpthread
00000014 T _. _ 8 cpthread
00000000 T _ 8 cpthread
00000000? _ Frame_begin __
.......................................

The output of nm-A is as follows:

Libtest. A: cpthread. O: 00000068 t Main _ 8 cpthreadpv
Libtest. A: cpthread. O: 00000038 t start _ 8 cpthread
Libtest. A: cpthread. O: 00000014 T _. _ 8 cpthread
Libtest. A: cpthread. O: 00000000 T _ 8 cpthread
Libtest. A: cpthread. O: 00000000? _ Frame_begin __
........................................ ..........................

-A or -- debug-Syms: displays debugging symbols.
-B: equivalent to -- format = BSD, used to be compatible with MIPS nm.
-C or -- demangle: decodes a low-level symbolic name (demangle) into a user-level name. This makes the C ++ function name readable.
-D or -- Dynamic: displays dynamic symbols. This option is only valid for dynamic targets (such as shared libraries of a specific type.
-F format: Output in format. Format can select BSD, sysv, or POSIX. This option is useful in GNU nm. The default value is BSD.
-G or -- extern-only: displays only external symbols.
-N,-V, or -- Numeric-sort: sort by the address corresponding to the symbol, rather than by the character order of the symbol name.
-P or -- no-sort: it is displayed in the order of symbols encountered in the target file, not sorted.
-P or -- Portability: Use the posix.2 standard output format instead of the default output format. It is equivalent to using any option-f posix.
-S or -- print-armap: contains the index when the symbol of the member in the column warehouse picking. The index content includes: Which modules contain the Mappings of names.
-R or -- reverse-sort: reverse the order (for example, ascending to descending ).
-- Size-sort: Sort symbols by size. The size is calculated based on the value of a symbol and the value of its next symbol.
-T Radix or -- Radix = Radix: Use the radix hexadecimal notation to display the symbolic value. Radix can only be "D" in decimal format, "O" in octal format, or "x" in hexadecimal format.
-- Target = bfdname: Specify the format of the target code, rather than the default format of the system.
-U or -- undefined-only: Only undefined symbols (those external symbols) are displayed ).
-L or -- line-numbers: Use debugging information for each symbol to find the file name and row number. For the defined symbols, find the line number of the symbol address. For undefined symbols, find the row number pointing to the symbol relocation entry. If you can find the row number information, it is displayed after the symbol information.
-V or -- version: displays the NM version number.
-- Help: displays any options of nm.

//////////////////////////////////////// //////////////////////////////////////// ////////////////////

1. LDD is not an executable program, but a shell script.

2. LDD can display the dependency of the executable module.

Principle: set a series of environment variables, such as ld_trace_loaded_objects, ld_warn, ld_bind_now, ld_library_version, and ld_verbose. When the ld_trace_loaded_objects environment variable is not empty, only the dependency of the module is displayed when any executable program is running, and the program is not actually executed. We can test it on the shell terminal. The specific test is as follows:

(1) Export ld_trace_loaded_objects = 1

(2) execute any program, such as LS, and check the running result of the program.

3, LDD display executable module dependency working principle, the essence is through the ld-linux.so (ELF dynamic library loader) to achieve.

As we all know, the ld-linux.so module will work prior to the executable module program and gain control, so when those environment variables above are set, the ld-linux.so selects the dependency that shows the executable module.

4, in fact we can directly execute the ld-linux.so module.For example:/lib/ld-linux.so.2 -- list Program (which is equivalent to LDD Program)

How to Use the LDD command (from LDD -- Help)

Name LDD-print the dependency of the Shared Library

Outline LDD [Option]... file...

Describes the shared libraries required for each program or shared library specified by LDD output on the command line.

Option

-- Version

Print the version number of LDD.

-V -- verbose

Print all information, such as the version information of the symbol.

-D -- Data-relocs

Execute the symbol re-deployment and report the missing target object (only applicable to the ELF format)

-R -- function-relocs

Redeploy the target object and function and report the missing target object and function (applicable only to the ELF format)

-- Help usage information

Note:

The standard version of LDD is provided together with glibc2. Libc5 and earlier versions are available in some systems. Long options are not supported in libc5. On the other hand, glibc2 does not support the-V option, but only provides the equivalent -- version option.

If the given library name in the command line contains '/', the libc5 version of this program uses it as the Library name; otherwise, it searches for the library in the standard location. Run a shared library under the current directory with the prefix "./".

5. Instructions:

LDD cannot work on A. out shared library.

LDD cannot work on some very old A. out programs that have been created before the release of LDD compilers. If you use LDD in this type of program, the program will try the running mode of argc = 0, and the result is unpredictable.

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.