SIPP code learning notes 1-compress

Source: Internet
Author: User

SIPP does not yet support compress, but it has been included in the plan, and the interface for calling the dynamic link library is also provided in the Code.

++

# Define comp_main

# Include "comp. H"

# Include <dlfcn. h>

# Include <string. h>

Char * comp_load ()

{

Void * handle;

Char * error;

Comp_error [0] = 0;

Handle = dlopen (comp_pluggin, rtld_lazy );

If (! Handle)

{

Strcpy (comp_error, dlerror ());

Return comp_error;

}

* (Void **) (& comp_compress) = dlsym (handle, "comp_compress ");

If (error = (char *) dlerror ()))

{

Strcpy (comp_error, error );

Return comp_error;

}

* (Void **) (& comp_uncompress) = dlsym (handle, "comp_uncompress"); If (error = (char *) dlerror ()))

{

Strcpy (comp_error, error );

Return comp_error;

}

* (Void **) (& comp_free) = dlsym (handle, "comp_free ");

If (error = (char *) dlerror ()))

{

Strcpy (comp_error, error );

Return comp_error;

}

Return 0;

}

++

The following is an explanation of some knowledge points from other articles,

The related functions are described as follows: (1) dlopen () first parameter: specify the name of the shared library. The specified shared library will be searched in the following position. -The environment variable LD_LIBRARY_PATH lists all directories separated by semicolons. -Use ldconfig to maintain the list of databases found in the/etc/lD. So. cache file. -Directory usr/lib. -Directory/lib. -Current directory. Second parameter: specify how to open the shared library. Among them, flag includes: rtld_lazy rtld_now rtld_global, which means: rtld_lazy: Undefined variables in the dynamic library (such as the external variable extern or function) before dlopen returns) if no Parsing is performed, the address of the variable is not parsed. Rtld_now: different from above, he needs to parse the address of each undefined variable before dlopen returns. If the address cannot be parsed, null is returned in dlopen with the error: Undefined symbol: XXXX ....... rtld_global: it means that the definition variables parsed in the library can be used in other subsequent linked libraries. (2) When dlsym () calls dlsym, it uses the phandle and function name of the Shared Library returned by dlopen () as the parameter to return the entry address of the function to be loaded. (3) dlerror () This function is used to check errors related to function calls to the shared library. Gcc-FPIC-shared-O libstr. So strlen. c strnlen. C-FPIC enables the output object module to be generated in the form of relocated addresses. -Shared specifies to generate the corresponding dynamic link library file libstr. So for the corresponding source file. NM command

The nm command can list the symbol tables in a function library file. It works for both static function libraries and shared function libraries. For a given function library, the NM command can list all the symbols defined in the function library, including the values and types of each symbol. You can also define the number of rows of the function (Symbol) in the original program. However, this requires that the "-l" option be added when compiling the function library.

Here we will discuss the types of symbols. The type of a symbol is displayed in the form of a letter. lowercase letters indicate that the symbol is local, while uppercase letters indicate that the symbol is global (externel ). Generally, there are several types: T, D, B, U, W. Their meanings are as follows: T indicates the general variable symbol defined in the Code segment; D indicates the initialized data segment; B indicates the initialized data segment; A indicates that the data segment is not defined, it is used in this library, but it is defined in other libraries. The abbreviation of W and weak indicates that if the symbol is also defined in other libraries, the definition of other symbols can overwrite this definition.

If you know the name of a function, but you do not know the library in which the function is defined, you can use the "-o" option of Mn and the grep command to find the library name. The-O option enables each row to display the file name of this function library. For example, if you want to find out where "Cos" is defined, you can use the following command:

Nm-O/lib/*/usr/local/lib/* 2>/dev/null

| Grep 'cos $'

For more detailed usage of nm, refer to the info document at Info: binutils # nm.

Special Functions _ init and _ fini

There are two special functions in the function library: _ init and _ fini. We have mentioned this before. It is mainly used to initialize the function library and perform some necessary processing when it is disabled. We can put the code we think we need into these two functions, they are executed when the function library is loaded and released. Specifically, if there is a function output named "_ init" in a function library, the function will be opened through the dlopen () function for the first time, or, when the function is simply opened as a shared function library, the _ init function is automatically called and executed. Correspondingly, It is the _ fini function. When a program calls dlclose () to release a reference to this function library, if the reference counter of this function library is 0, or when the function library is used as a common shared function library and the program that uses it Exits normally, _ fini will be called and executed. The C language defines their prototype as follows:

Void _ Init (void); void _ Fini (void );

When you use GCC to compile the source program as a ". O" file, you need to add the "-nostartfiles" option. This option does not allow the C compiler to link the startup function in the system's startup function library. Otherwise, a "Multiple-definition" error is returned.

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.