Method and device for processing shared libraries with multiple versions
By replacing the symbols of the shared library during compilation, the symbol conflict problem between different versions of the shared library is solved, the peaceful coexistence of multiple shared library versions is achieved, the size of the executable file is reduced and the program efficiency is improved.
Patent Information
- Application Number
- CN202110124671.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-01-29
- Publication Date
- 2025-09-26
- Estimated Expiration
- 2041-01-29
AI Technical Summary
In the existing technology, when different versions of incompatible shared libraries are relied upon by different modules of the same program, symbol conflicts are prone to occur. In addition, existing solutions have the problems of generating large executable files, high coding complexity, or affecting program efficiency.
When an executable file simultaneously links multiple versions of shared libraries, the target symbol replacement table of the target version shared library is obtained, the original symbols are replaced, and the target version shared library containing the replaced symbols is generated. The symbol replacement table is used to rename the symbols of the shared library during compilation to avoid symbol conflicts.
Without modifying the program source code, the symbol conflict problem is solved, multiple shared library versions can coexist peacefully, the size of the executable file is reduced, the coding complexity is reduced, and the execution efficiency of the program is improved.
Smart Images

Figure CN112947987B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computers, and in particular to a method, apparatus, computer device, and computer-readable storage medium for processing a shared library with multiple versions. Background Art
[0002] The Linux platform has two different types of libraries: dynamic libraries (also called shared libraries) and static libraries. Static libraries are a combination of code and data that are linked into the program during compilation. Shared libraries exist as independent entities, loaded at program startup, and can be shared by different programs.
[0003] When different versions of incompatible shared libraries are depended on by different modules of the same program, symbol conflicts will eventually occur.
[0004] like Figure 1 As shown, the executable file a.out dynamically links the shared library libB.so (abbreviated as B) and the shared library libC.so (abbreviated as C), and B and C depend on two versions D_v1 and D_v2 of the shared library D respectively.
[0005] Since D_v1 and D_v2 are two versions of the same shared library, most of their symbols are the same. To avoid symbol conflicts at runtime, existing technologies provide the following two solutions:
[0006] Option 1: If Figure 2 As shown, the shared library D is made into a static library, and the shared libraries libB.so and libC.so are statically linked to different versions of the shared library D, libD_v1.a and libD_v2.a, respectively, and the symbols in the shared library D are hidden from the outside.
[0007] Option 2: If Figure 3 As shown, the shared libraries libB.so and libC.so are not directly dynamically linked to the two versions of the shared library D, but are dynamically loaded (dlopen) to use the shared libraries libD_v1.so and libD_v2.so respectively.
[0008] The above two solutions have the following defects:
[0009] The static linking method adopted in Solution 1 generates a large executable file containing the same common code, which causes waste and prevents different programs from sharing exactly the same code.
[0010] The dynamic loading (dlopen) method used in Solution 2 needs to be combined with dlsym to dynamically search for symbols in the shared library D. And to change the dynamic link to dynamic loading, it is necessary to modify the external call module (such as Figure 3 When there are too many symbols in the external shared library D, the coding complexity will increase significantly and the execution efficiency of the program will be affected.
[0011] Currently, in the related art, when different versions of incompatible shared libraries are relied upon by different modules of the same program, symbol conflicts will eventually occur, and no effective solution has been proposed. Summary of the Invention
[0012] The embodiments of the present application provide a method, apparatus, computer device, and computer-readable storage medium for processing shared libraries with multiple versions, so as to at least solve the problem in the related art of symbol conflicts that eventually occur when different versions of incompatible shared libraries are relied upon by different modules of the same program.
[0013] In a first aspect, an embodiment of the present application provides a method for processing a shared library with multiple versions, including:
[0014] When the executable file is linked to multiple versions of the first shared library at the same time, the following steps are performed for each version of the first shared library:
[0015] Step S1, obtaining a target symbol replacement table corresponding to a target version shared library, wherein the target version shared library is one of the versions of the first shared library, and the target symbol replacement table stores original symbols of the target version shared library and replacement symbols corresponding to the original symbols, wherein the replacement symbols are symbols obtained by adding an identifier for indicating the version of the target version shared library to the original symbols;
[0016] Step S2: performing symbol replacement on the original symbols in the target version shared library according to the target symbol replacement table to generate a target version shared library containing the replacement symbols.
[0017] In some embodiments, step S1, obtaining a target symbol replacement table corresponding to a target version shared library, includes:
[0018] Step S11, obtaining the original symbol in the target version shared library;
[0019] Step S12, adding the identifier for indicating the version of the target version shared library to the original symbol to obtain the replacement symbol;
[0020] Step S13, establishing a correspondence between the original symbol and the replacement symbol;
[0021] Step S14: storing the original symbol and the replacement symbol in the target symbol replacement table in correspondence with each other.
[0022] In some embodiments, step S2, performing symbol replacement on the original symbol in the target version shared library according to the target symbol replacement table to generate the target version shared library containing the replacement symbol, comprises:
[0023] Step S21, replacing the original symbol in the first file obtained by compiling the source code file of the target version shared library with the replacement symbol, to obtain the first file after the symbol replacement;
[0024] Step S22: Linking the first file after symbol replacement through a linker to generate the target version shared library containing the replacement symbols.
[0025] In some embodiments, when the executable file is linked to multiple shared libraries at the same time, and each of the multiple shared libraries is linked to one version of the first shared library, the steps are performed on each version of the first shared library. S After step 1 and step S2, the method further includes:
[0026] The following steps are performed for each shared library in the plurality of shared libraries:
[0027] Step S3: performing symbol replacement on the original symbol in the second shared library according to the target symbol replacement table to generate a second shared library containing the replacement symbol, wherein the second shared library is one of the multiple shared libraries, and the second shared library is linked to the target version shared library.
[0028] In some embodiments, step S3, performing symbol replacement on the original symbol in the second shared library according to the target symbol replacement table to generate the second shared library containing the replacement symbol, includes:
[0029] Step S31, replacing the original symbol in the second file obtained by compiling the source code file of the second shared library with the replacement symbol, to obtain the second file after the symbol is replaced;
[0030] Step S32: Linking the second file after symbol replacement through a linker to generate the second shared library containing the replacement symbol.
[0031] In a second aspect, an embodiment of the present application provides a processing device having multiple versions of a shared library, including:
[0032] The execution unit is configured to, when the executable file is simultaneously linked to multiple versions of the first shared library, perform the following steps on each version of the first shared library:
[0033] Step S1, obtaining a target symbol replacement table corresponding to a target version shared library, wherein the target version shared library is one of the versions of the first shared library, and the target symbol replacement table stores original symbols of the target version shared library and replacement symbols corresponding to the original symbols, wherein the replacement symbols are symbols obtained by adding an identifier for indicating the version of the target version shared library to the original symbols;
[0034] Step S2: performing symbol replacement on the original symbols in the target version shared library according to the target symbol replacement table to generate a target version shared library containing the replacement symbols.
[0035] In some embodiments, step S1, obtaining a target symbol replacement table corresponding to a target version shared library, includes:
[0036] Step S11, obtaining the original symbol in the target version shared library;
[0037] Step S12, adding the identifier for indicating the version of the target version shared library to the original symbol to obtain the replacement symbol;
[0038] Step S13, establishing a correspondence between the original symbol and the replacement symbol;
[0039] Step S14: storing the original symbol and the replacement symbol in the target symbol replacement table in correspondence with each other.
[0040] In some embodiments, step S2, performing symbol replacement on the original symbol in the target version shared library according to the target symbol replacement table to generate the target version shared library containing the replacement symbol, comprises:
[0041] Step S21, replacing the original symbol in the first file obtained by compiling the source code file of the target version shared library with the replacement symbol, to obtain the first file after the symbol replacement;
[0042] Step S22: Linking the first file after symbol replacement through a linker to generate the target version shared library containing the replacement symbols.
[0043] In a third aspect, an embodiment of the present application provides a computer device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the computer program, the method for processing a shared library with multiple versions as described in the first aspect above is implemented.
[0044] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method for processing a shared library with multiple versions as described in the first aspect above.
[0045] Compared with the related art, the processing method and device of a shared library with multiple versions provided in the embodiment of the present application performs the following steps on each version of the first shared library when the executable file simultaneously links to multiple versions of the first shared library: Step S1, obtaining a target symbol replacement table corresponding to the target version shared library, wherein the target version shared library is one of the versions of the first shared library, and the target symbol replacement table stores the original symbol of the target version shared library and a replacement symbol corresponding to the original symbol, and the replacement symbol is a symbol obtained by adding an identifier for indicating the version of the target version shared library to the original symbol; Step S2, according to the target symbol replacement table, performing symbol replacement on the original symbol in the target version shared library to generate a target version shared library containing the replacement symbol, which solves the problem of symbol conflict in the related art when different versions and incompatible shared libraries are relied upon by different modules of the same program, and realizes that by renaming the symbols in the file compiled by the shared library during compilation, the symbol conflict problem can be well solved, and multiple versions of the shared library can coexist peacefully without modifying the program source code.
[0046] The details of one or more embodiments of the present application are set forth in the following drawings and description to make other features, objects, and advantages of the present application more readily apparent. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0048] Figure 1 It is a schematic diagram of different modules of the same program depending on different versions of shared libraries according to the related art;
[0049] Figure 2 It is a schematic diagram of a static linking method according to the related art;
[0050] Figure 3 is a schematic diagram of a dynamic loading method according to related art;
[0051] Figure 4 is a structural block diagram of a mobile terminal according to an embodiment of the present application;
[0052] Figure 5is a flowchart of a method for processing a shared library with multiple versions according to an embodiment of the present application;
[0053] Figure 6 is a schematic diagram of an executable file simultaneously linking different versions of a shared library according to an embodiment of the present application;
[0054] Figure 7 is a schematic diagram of a shared library linked to an executable file according to an embodiment of the present application and linking different versions of the shared library;
[0055] Figure 8 is a flowchart of a method for processing the coexistence of multiple versions of a shared library according to an embodiment of the present application;
[0056] Figure 9 is a schematic diagram of a normal shared library construction process according to the prior art;
[0057] Figure 10 is a schematic diagram of an improved shared library construction process according to an embodiment of the present application;
[0058] Figure 11 is a schematic diagram of a processing device having multiple versions of a shared library according to an embodiment of the present application;
[0059] Figure 12 Schematic diagram of the hardware structure of a computer device according to an embodiment of the present application. DETAILED DESCRIPTION
[0060] In order to make the purpose, technical solutions and advantages of this application more clearly understood, the present application is described and illustrated below in conjunction with the accompanying drawings and examples. It should be understood that the specific embodiments described herein are merely used to explain this application and are not intended to limit this application. Based on the embodiments provided in this application, all other embodiments obtained by those of ordinary skill in the art without making any creative effort are within the scope of protection of this application.
[0061] Obviously, the drawings described below are merely examples or embodiments of the present application. Those skilled in the art can, without inventive effort, apply the present application to other similar scenarios based on these drawings. Furthermore, it is also understood that, although the effort involved in such a development process may be complex and lengthy, for those skilled in the art related to the content disclosed in this application, changes in design, manufacturing, or production based on the technical content disclosed in this application are merely conventional technical means and should not be construed as an insufficiency of the content disclosed in this application.
[0062] References to "embodiments" in this application mean that a particular feature, structure, or characteristic described in connection with the embodiment may be included in at least one embodiment of the application. The appearance of this phrase in various places in the specification does not necessarily refer to the same embodiment, nor does it refer to independent or alternative embodiments that are mutually exclusive of other embodiments. It is understood, both explicitly and implicitly, by those skilled in the art that the embodiments described in this application may be combined with other embodiments unless there is a conflict.
[0063] Unless otherwise defined, the technical or scientific terms used in this application should have the ordinary meaning understood by a person of ordinary skill in the technical field to which this application belongs. The words "one", "a", "the" and the like used in this application do not indicate a limit on quantity and may indicate the singular or plural. The terms "include", "comprise", "have" and any variations thereof used in this application are intended to cover non-exclusive inclusions; for example, a process, method, system, product or device that includes a series of steps or modules (units) is not limited to the listed steps or units, but may also include steps or units that are not listed, or may also include other steps or units that are inherent to these processes, methods, products or devices. The words "connect", "connected", "coupled" and the like used in this application are not limited to physical or mechanical connections, but may include electrical connections, whether direct or indirect. The word "multiple" used in this application refers to two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships can exist. For example, "A and / or B" can mean: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the objects before and after are in an "or" relationship. The terms "first", "second", "third", etc. involved in this application are only used to distinguish similar objects and do not represent a specific order for the objects.
[0064] This embodiment provides a mobile terminal. Figure 4 : is a structural block diagram of a mobile terminal according to an embodiment of the present application. Figure 4 As shown, the mobile terminal includes components such as a radio frequency (RF) circuit 410, a memory 420, an input unit 430, a display unit 440, a sensor 450, an audio circuit 460, a wireless fidelity (WiFi) module 470, a processor 480, and a power supply 490. Those skilled in the art will understand that Figure 4 The structure of the mobile terminal shown in the figure does not constitute a limitation to the mobile terminal, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.
[0065] The following combination Figure 4A detailed introduction to the various components of the mobile terminal:
[0066] The RF circuit 410 can be used to receive and send signals during information transmission or calls. In particular, after receiving downlink information from the base station, it is sent to the processor 480 for processing; in addition, the designed uplink data is sent to the base station. Generally, the RF circuit includes but is not limited to an antenna, at least one amplifier, a transceiver, a coupler, a low noise amplifier (LNA), a duplexer, etc. In addition, the RF circuit 410 can also communicate with the network and other devices through wireless communication. The above-mentioned wireless communication can use any communication standard or protocol, including but not limited to Global System of Mobile communication (GSM), General Packet Radio Service (GPRS), Code Division Multiple Access (CDMA), Wideband Code Division Multiple Access (WCDMA), Long Term Evolution (LTE), email, Short Messaging Service (SMS), etc.
[0067] The memory 420 can be used to store software programs and modules. The processor 480 executes various functional applications and data processing of the mobile terminal by running the software programs and modules stored in the memory 420. The memory 420 may mainly include a program storage area and a data storage area. The program storage area may store an operating system, at least one application required for a function (such as a sound playback function, an image playback function, etc.); the data storage area may store data created based on the use of the mobile terminal (such as audio data, a phone book, etc.). In addition, the memory 420 may include a high-speed random access memory and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage device.
[0068] The input unit 430 can be used to receive input digital or character information, and to generate key signal input related to the user settings and function control of the mobile terminal. Specifically, the input unit 430 may include a touch panel 431 and other input devices 432. The touch panel 431, also known as a touch screen, can collect user touch operations on or near it (such as operations performed by the user using any suitable object or accessory such as a finger, stylus, etc. on or near the touch panel 431) and drive the corresponding connection device according to a pre-set program. Optionally, the touch panel 431 may include two parts: a touch detection device and a touch controller. Among them, the touch detection device detects the user's touch direction, detects the signal caused by the touch operation, and transmits the signal to the touch controller; the touch controller receives the touch information from the touch detection device and converts it into touch point coordinates, which are then sent to the processor 480, and can receive and execute commands sent by the processor 480. In addition, the touch panel 431 can be implemented using various types such as resistive, capacitive, infrared, and surface acoustic wave. In addition to the touch panel 431, the input unit 430 may further include other input devices 432. Specifically, the other input devices 432 may include, but are not limited to, one or more of a physical keyboard, function keys (such as volume control keys, power keys, etc.), a trackball, a mouse, and a joystick.
[0069] The display unit 440 can be used to display information input by the user or information provided to the user and various menus of the mobile terminal. The display unit 440 may include a display panel 441. Optionally, the display panel 441 may be configured in the form of a liquid crystal display (LCD), an organic light-emitting diode (OLED), etc. Further, the touch panel 431 may cover the display panel 441. When the touch panel 431 detects a touch operation on or near it, it is transmitted to the processor 480 to determine the type of touch event. Subsequently, the processor 480 provides corresponding visual output on the display panel 441 according to the type of touch event. Although in Figure 4 In the embodiment, the touch panel 431 and the display panel 441 are used as two independent components to implement the input and output functions of the mobile terminal, but in some embodiments, the touch panel 431 and the display panel 441 can be integrated to implement the input and output functions of the mobile terminal.
[0070] The mobile terminal may also include at least one sensor 450, such as a light sensor, a motion sensor, and other sensors. Specifically, the light sensor may include an ambient light sensor and a proximity sensor, wherein the ambient light sensor may adjust the brightness of the display panel 441 according to the brightness of the ambient light, and the proximity sensor may turn off the display panel 441 and / or the backlight when the mobile terminal is moved to the ear. As a type of motion sensor, the accelerometer sensor can detect the magnitude of acceleration in all directions (generally three axes), and can detect the magnitude and direction of gravity when stationary. It can be used for applications that identify the posture of the mobile terminal (such as horizontal and vertical screen switching, related games, magnetometer posture calibration), vibration recognition related functions (such as pedometer, tapping), etc.; as for other sensors that can be configured in the mobile terminal, such as gyroscopes, barometers, hygrometers, thermometers, infrared sensors, etc., they will not be described here.
[0071] Speaker 461 and microphone 462 in audio circuit 460 provide an audio interface between the user and the mobile terminal. Audio circuit 460 converts received audio data into electrical signals and transmits them to speaker 461, which then converts them into sound signals for output. Microphone 462, on the other hand, converts collected sound signals into electrical signals, which are then received by audio circuit 460 and converted into audio data. The audio data is then processed by processor 480 and transmitted to, for example, another mobile terminal via RF circuit 410, or the audio data is output to memory 420 for further processing.
[0072] WiFi is a short-range wireless transmission technology. Mobile terminals can help users send and receive emails, browse web pages, and access streaming media through the WiFi module 470. It provides users with wireless broadband Internet access. Figure 4 A WiFi module 470 is shown, but it is understandable that it is not an essential component of the mobile terminal and can be omitted as needed without changing the essence of the invention, or replaced with other short-range wireless transmission modules, such as a Zigbee module or a WAPI module.
[0073] Processor 480 is the control center of the mobile terminal, connecting all components of the mobile terminal using various interfaces and circuits. By running or executing software programs and / or modules stored in memory 420 and accessing data stored in memory 420, it executes various functions of the mobile terminal and processes data, thereby providing overall monitoring of the mobile terminal. Optionally, processor 480 may include one or more processing units; preferably, processor 480 may integrate an application processor and a modem processor, with the application processor primarily processing the operating system, user interface, and application programs, while the modem processor primarily handles wireless communications. It is understood that the modem processor may not be integrated into processor 480.
[0074] The mobile terminal also includes a power supply 490 (such as a battery) for supplying power to various components. Preferably, the power supply can be logically connected to the processor 480 through a power management system, thereby implementing functions such as charging, discharging, and power consumption management through the power management system.
[0075] Although not shown, the mobile terminal may further include a camera, a Bluetooth module, etc., which will not be described in detail here.
[0076] In this embodiment, the processor 480 is configured to: when the executable file simultaneously links to multiple versions of the first shared library, perform the following steps for each version of the first shared library: Step S1, obtain a target symbol replacement table corresponding to the target version shared library, wherein the target version shared library is one of the versions of the first shared library, and the target symbol replacement table stores the original symbol of the target version shared library and a replacement symbol corresponding to the original symbol, and the replacement symbol is a symbol obtained by adding an identifier for indicating the version of the target version shared library to the original symbol; Step S2, according to the target symbol replacement table, perform symbol replacement on the original symbol in the target version shared library to generate a target version shared library containing the replacement symbol.
[0077] In some embodiments, the processor 480 is further configured to execute: step S11, obtaining the original symbol in the target version shared library; step S12, adding the identifier for indicating the version of the target version shared library to the original symbol to obtain the replacement symbol; step S13, establishing a correspondence between the original symbol and the replacement symbol; step S14, storing the original symbol and the replacement symbol in the target symbol replacement table.
[0078] In some embodiments, the processor 480 is further configured to execute: step S21, replacing the original symbol in the first file obtained after compiling the source code file of the target version shared library with the replacement symbol, to obtain the first file after symbol replacement; step S22, linking the first file after symbol replacement through a linker to generate the target version shared library containing the replacement symbol.
[0079] In some embodiments, the processor 480 is further configured to: when the executable file simultaneously links to multiple shared libraries, and each of the multiple shared libraries links to one version of the first shared library, after executing steps S1 and S2 for each version of the first shared library, execute the following steps for each of the multiple shared libraries: step S3, according to the target symbol replacement table, perform symbol replacement on the original symbol in the second shared library to generate a second shared library containing the replacement symbol, wherein the second shared library is one of the multiple shared libraries, and the second shared library links to the target version shared library.
[0080] In some embodiments, the processor 480 is further configured to execute: step S31, replacing the original symbol in the second file obtained after compiling the source code file of the second shared library with the replacement symbol to obtain the second file after symbol replacement; step S32, linking the second file after symbol replacement through a linker to generate the second shared library containing the replacement symbol.
[0081] This embodiment also provides a method for processing a shared library with multiple versions. Figure 5 is a flowchart of a method for processing a shared library with multiple versions according to an embodiment of the present application, such as Figure 5 As shown:
[0082] When the executable file is linked to multiple versions of the first shared library at the same time, the following steps are performed for each version of the first shared library:
[0083] Step S1, obtaining a target symbol replacement table corresponding to a target version shared library, wherein the target version shared library is one of the versions of the first shared library, and the target symbol replacement table stores original symbols of the target version shared library and replacement symbols corresponding to the original symbols, wherein the replacement symbols are symbols obtained by adding an identifier for indicating the version of the target version shared library to the original symbols;
[0084] Step S2: performing symbol replacement on the original symbols in the target version shared library according to the target symbol replacement table to generate a target version shared library containing the replacement symbols.
[0085] Specifically, step S1, obtaining the target symbol replacement table corresponding to the target version shared library includes:
[0086] Step S11, obtaining the original symbol in the target version shared library;
[0087] Step S12, adding the identifier for indicating the version of the target version shared library to the original symbol to obtain the replacement symbol;
[0088] Step S13, establishing a correspondence between the original symbol and the replacement symbol;
[0089] Step S14: storing the original symbol and the replacement symbol in the target symbol replacement table in correspondence with each other.
[0090] Specifically, step S2, performing symbol replacement on the original symbol in the target version shared library according to the target symbol replacement table, and generating the target version shared library containing the replacement symbol includes:
[0091] Step S21, replacing the original symbol in the first file obtained by compiling the source code file of the target version shared library with the replacement symbol, to obtain the first file after the symbol replacement;
[0092] Step S22: Linking the first file after symbol replacement through a linker to generate the target version shared library containing the replacement symbols.
[0093] Take the Chromium project as an example. Figure 6 As shown in the figure, the boringssl used in the Chromium source code is a branch of openssl fork by Google. The boringssl shared library can be regarded as another version of the openssl shared library. Most of the symbols of the two are the same. Figure 6 As shown, when Chromium links its own boringssl shared library and the system-provided openssl shared library at the same time, a symbol conflict occurs.
[0094] To resolve the symbol conflict issue, the present invention implements the following process:
[0095] Step 1. First, use the objdump tool to export the symbols of the boringssl shared library.
[0096] Step 2: Add a prefix like chromium_ to the exported symbols of the boringssl shared library to form new symbols to distinguish them from the symbols in the openssl shared library. The original symbols and the new symbols are arranged in two columns, one-to-one, to form the symbol replacement table of the boringssl shared library. As shown in the following table:
[0097]
[0098]
[0099] Step 3: Modify Chromium's build process and add the symbol replacement process.
[0100] Step 4: For the borinssl shared library and the modules that directly depend on the boringssl shared library, during the compilation process, use the symbol replacement table generated in step 2 to perform symbol replacement on the compiled files.
[0101] In some embodiments, when the executable file is linked to multiple shared libraries simultaneously, and each of the multiple shared libraries is linked to one version of the first shared library, after performing steps S1 and S2 on each version of the first shared library, the method further includes:
[0102] The following steps are performed for each shared library in the plurality of shared libraries:
[0103] Step S3: performing symbol replacement on the original symbol in the second shared library according to the target symbol replacement table to generate a second shared library containing the replacement symbol, wherein the second shared library is one of the multiple shared libraries, and the second shared library is linked to the target version shared library.
[0104] Specifically, step S3, performing symbol replacement on the original symbol in the second shared library according to the target symbol replacement table to generate a second shared library containing the replacement symbol, includes:
[0105] Step S31, replacing the original symbol in the second file obtained by compiling the source code file of the second shared library with the replacement symbol, to obtain the second file after the symbol is replaced;
[0106] Step S32: Linking the second file after symbol replacement through a linker to generate the second shared library containing the replacement symbol.
[0107] For example Figure 7 In the situation shown, without modifying the source code of modules B and C (that is, shared libraries B and C) and shared library D, when the executable file a.out is run, B and C can correctly use the symbols in the different versions of shared library D on which they depend, and no symbol conflict occurs.
[0108] The specific implementation process is as follows:
[0109] like Figure 8 As shown, the implementation process specifically includes the following steps:
[0110] Step S801: Export the symbols of the shared library into a column. In the Linux operating system, the objdump tool is generally used to complete the symbol export.
[0111] Step S802: Add a unified prefix (suffix) representing version information to the original symbol to form a new symbol. The original symbol and the new symbol together constitute a symbol replacement table.
[0112] Step S803, recompile the shared library and its dependent modules, and use the symbol replacement table in step S802 to perform symbol replacement for the target files in the intermediate compilation process. Under the Linux operating system, objcopy is generally used to perform symbol replacement, for example:
[0113] objcopy--redefine-syms symbols_file xxx.o
[0114] Among them, symbols_file is a file that records the symbol replacement table. The first column of each line is the original symbol, and the second column is the new symbol; xxx.o is the target file for symbol replacement.
[0115] Step S804: linking the target file after symbol replacement using a linker to generate a shared library module containing new symbols.
[0116] The normal construction process of the shared library is as follows Figure 9 As shown in the figure, since the symbols in the source code of the two versions D_v1 and D_v2 of the shared library D are basically the same, after compilation and linking, the generated shared libraries libD_v1.so and libD_v2.so contain the same symbols, which will cause symbol conflicts at runtime.
[0117] The improved construction process is as follows Figure 10 As shown in the figure, after the compiler and assembler compile the source code file into the target file, a symbol replacement table is used to perform a unified symbol replacement for each target file, so that the shared library generated by the linker will have different exported symbols for different versions.
[0118] At the same time, during their respective compilation processes, module B, which depends on shared library D_v1, and module C, which depends on shared library D_v2, also undergo symbol replacement identical to that of D_v1 or D_v2. This results in symbols in D_v1 and its dependent module B sharing the same suffix _v1, and symbols in D_v2 and its dependent module C sharing the same suffix _v2. Because the symbols in D_v1 and D_v2 are distinct, symbol conflicts will not occur when the executable file runs.
[0119] This application can effectively solve the symbol conflict problem by renaming the symbols in the files compiled from the source code files of the shared library during compilation, so that multiple versions of the shared library can coexist peacefully without modifying the program source code.
[0120] It should be noted that whether the executable file directly links to multiple versions of the same shared library at the same time, or the executable file links to multiple shared libraries (also called modules) at the same time, and each module depends on different versions of the same shared library, the shared library processing method of the embodiment of the present application is applicable, that is, symbol replacement operations can be performed on each version of the shared library and the shared library that depends on it, so as to ensure that there will be no symbol conflicts when the executable file is running.
[0121] It should be noted that the steps shown in the above process or the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0122] This embodiment also provides a processing device with multiple versions of a shared library, which is used to implement the above-mentioned embodiments and preferred embodiments. Details that have been described will not be repeated here. As used below, the terms "module," "unit," "subunit," etc. may refer to a combination of software and / or hardware that implements a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, implementation using hardware, or a combination of software and hardware, is also possible and contemplated.
[0123] Figure 11 is a schematic diagram of a processing device with multiple versions of a shared library according to an embodiment of the present application, such as Figure 11 As shown, the device includes:
[0124] The execution unit 1101 is configured to, when an executable file is linked to multiple versions of a first shared library simultaneously, perform the following steps on each version of the first shared library:
[0125] Step S1, obtaining a target symbol replacement table corresponding to a target version shared library, wherein the target version shared library is one of the versions of the first shared library, and the target symbol replacement table stores original symbols of the target version shared library and replacement symbols corresponding to the original symbols, wherein the replacement symbols are symbols obtained by adding an identifier for indicating the version of the target version shared library to the original symbols;
[0126] Step S2: performing symbol replacement on the original symbols in the target version shared library according to the target symbol replacement table to generate a target version shared library containing the replacement symbols.
[0127] In some embodiments, step S1, obtaining a target symbol replacement table corresponding to a target version shared library, includes:
[0128] Step S11, obtaining the original symbol in the target version shared library;
[0129] Step S12, adding the identifier for indicating the version of the target version shared library to the original symbol to obtain the replacement symbol;
[0130] Step S13, establishing a correspondence between the original symbol and the replacement symbol;
[0131] Step S14: storing the original symbol and the replacement symbol in the target symbol replacement table in correspondence with each other.
[0132] In some embodiments, step S2, performing symbol replacement on the original symbol in the target version shared library according to the target symbol replacement table to generate the target version shared library containing the replacement symbol, comprises:
[0133] Step S21, replacing the original symbol in the first file obtained by compiling the source code file of the target version shared library with the replacement symbol, to obtain the first file after the symbol replacement;
[0134] Step S22: Linking the first file after symbol replacement through a linker to generate the target version shared library containing the replacement symbols.
[0135] In some embodiments, the apparatus further includes: a processing unit configured to, when the executable file simultaneously links to multiple shared libraries and each of the multiple shared libraries links to one version of the first shared library, perform the following steps on each of the multiple shared libraries after performing steps S1 and S2 on each version of the first shared library:
[0136] Step S3: performing symbol replacement on the original symbol in the second shared library according to the target symbol replacement table to generate a second shared library containing the replacement symbol, wherein the second shared library is one of the multiple shared libraries, and the second shared library is linked to the target version shared library.
[0137] In some embodiments, step S3, performing symbol replacement on the original symbol in the second shared library according to the target symbol replacement table to generate the second shared library containing the replacement symbol, includes:
[0138] Step S31, replacing the original symbol in the second file obtained by compiling the source code file of the second shared library with the replacement symbol, to obtain the second file after the symbol is replaced;
[0139] Step S32: Linking the second file after symbol replacement through a linker to generate the second shared library containing the replacement symbol.
[0140] It should be noted that the above modules can be functional modules or program modules, and can be implemented through software or hardware. For modules implemented through hardware, the above modules can be located in the same processor; or the above modules can be located in different processors in any combination.
[0141] An embodiment of the present application further provides a computer device, and the method for processing a shared library with multiple versions in combination with the embodiment of the present application can be implemented by the computer device. Figure 12 Schematic diagram of the hardware structure of a computer device according to an embodiment of the present application.
[0142] The computer device may include a processor 1201 and a memory 1202 storing computer program instructions.
[0143] Specifically, the processor 1201 may include a central processing unit (CPU), or an application-specific integrated circuit (ASIC), or may be configured to implement one or more integrated circuits of the embodiments of the present application.
[0144] Among them, the memory 1202 may include a large-capacity memory for data or instructions. By way of example and not limitation, the memory 1202 may include a hard disk drive (HDD), a floppy disk drive, a solid-state drive (SSD), a flash memory, an optical disk, a magneto-optical disk, a magnetic tape, or a universal serial bus (USB) drive, or a combination of two or more of these. Where appropriate, the memory 1202 may include a removable or non-removable (or fixed) medium. Where appropriate, the memory 1202 may be inside or outside the data processing device. In a specific embodiment, the memory 1202 is a non-volatile memory. In a specific embodiment, the memory 1202 includes a read-only memory (ROM) and a random access memory (RAM). Where appropriate, the ROM may be a mask-programmed ROM, a programmable ROM (PROM), an erasable PROM (EPROM), an electrically erasable PROM (EEPROM), an electrically alterable ROM (EAROM) or a flash memory (FLASH), or a combination of two or more of these. Under appropriate circumstances, the RAM can be a static random access memory (SRAM) or a dynamic random access memory (DRAM), where the DRAM can be a fast page mode dynamic random access memory (FPMDRAM), an extended data output dynamic random access memory (EDODRAM), a synchronous dynamic random access memory (SDRAM), etc.
[0145] The memory 1202 may be used to store or cache various data files that need to be processed and / or used for communication, as well as possible computer program instructions executed by the processor 1201 .
[0146] The processor 1201 reads and executes computer program instructions stored in the memory 1202 to implement any one of the methods for processing a shared library with multiple versions in the above embodiments.
[0147] In some embodiments, the computer device may further include a communication interface 1203 and a bus 1204. Figure 12 As shown, the processor 1201 , the memory 1202 , and the communication interface 1203 are connected via a bus 1204 and communicate with each other.
[0148] The communication interface 1203 is used to implement communication between the various modules, devices, units, and / or devices in the embodiments of the present application. The communication interface 1203 can also implement data communication with other components such as external devices, image / data acquisition devices, databases, external storage, and image / data processing workstations.
[0149] The bus 1204 includes hardware, software, or both, and couples components of the computer device to each other. The bus 1204 includes, but is not limited to, at least one of the following: a data bus, an address bus, a control bus, an expansion bus, and a local bus. By way of example and not limitation, bus 1204 may include an Accelerated Graphics Port (AGP) or other graphics bus, an Extended Industry Standard Architecture (EISA) bus, a Front Side Bus (FSB), a Hyper Transport (HT) interconnect, an Industry Standard Architecture (ISA) bus, an InfiniBand interconnect, a Low Pin Count (LPC) bus, a memory bus, a Micro Channel Architecture (MCA) bus, a Peripheral Component Interconnect (PCI) bus, a PCI-Express (PCI-X) bus, a Serial Advanced Technology Attachment (SATA) bus, a Video Electronics Standards Association Local Bus (VLB) bus, or other suitable buses, or a combination of two or more of the foregoing. Bus 1204 may include one or more buses, where appropriate. Although embodiments herein describe and illustrate a particular bus, this application contemplates any suitable bus or interconnect.
[0150] In addition, in conjunction with the method for processing a shared library with multiple versions in the above embodiments, embodiments of the present application may provide a computer-readable storage medium for implementation. The computer-readable storage medium stores computer program instructions; when the computer program instructions are executed by a processor, any of the methods for processing a shared library with multiple versions in the above embodiments is implemented.
[0151] The technical features of the above-mentioned embodiments can be combined arbitrarily. In order to make the description concise, not all possible combinations of the technical features in the above-mentioned embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0152] The above-described embodiments merely represent several implementation methods of the present application. While the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present invention. It should be noted that a person skilled in the art could make various modifications and improvements without departing from the spirit of the present application, all of which fall within the scope of protection of the present application. Therefore, the scope of protection of the present patent application shall be determined by the appended claims.
Claims
1. A method for processing a shared library with multiple versions, characterized in that: include: When the executable file is linked to multiple versions of the first shared library at the same time, the following steps are performed for each version of the first shared library: Step S1, obtaining a target symbol replacement table corresponding to a target version shared library, wherein the target version shared library is one of the versions of the first shared library, and the target symbol replacement table stores original symbols of the target version shared library and replacement symbols corresponding to the original symbols, wherein the replacement symbols are symbols obtained by adding an identifier for indicating the version of the target version shared library to the original symbols; Step S2, performing symbol replacement on the original symbols in the target version shared library according to the target symbol replacement table, and generating a target version shared library containing the replacement symbols; Wherein, step S1 includes: Step S11, obtaining the original symbol in the target version shared library; Step S12, adding the identifier for indicating the version of the target version shared library to the original symbol to obtain the replacement symbol; Step S13, establishing a correspondence between the original symbol and the replacement symbol; Step S14, storing the original symbol and the replacement symbol in the target symbol replacement table in correspondence; Wherein, step S2 includes: Step S21, replacing the original symbol in the first file obtained by compiling the source code file of the target version shared library with the replacement symbol, to obtain the first file after the symbol replacement; Step S22, linking the first file after symbol replacement through a linker to generate the target version shared library containing the replacement symbols; Wherein, when the executable file is linked to multiple shared libraries at the same time, and each of the multiple shared libraries is linked to one version of the first shared library, after performing step S1 and step S2 on each version of the first shared library, the method further includes: The following steps are performed for each shared library in the plurality of shared libraries: Step S3, performing symbol replacement on the original symbol in the second shared library according to the target symbol replacement table to generate a second shared library containing the replacement symbol, wherein the second shared library is one of the multiple shared libraries, and the second shared library is linked to the target version shared library; Wherein, step S3 includes: Step S31, replacing the original symbol in the second file obtained by compiling the source code file of the second shared library with the replacement symbol, to obtain the second file after the symbol is replaced; Step S32: Linking the second file after symbol replacement through a linker to generate the second shared library containing the replacement symbol.
2. A processing device having multiple versions of a shared library, characterized in that include: The execution unit is configured to, when the executable file is simultaneously linked to multiple versions of the first shared library, perform the following steps on each version of the first shared library: Step S1, obtaining a target symbol replacement table corresponding to a target version shared library, wherein the target version shared library is one of the versions of the first shared library, and the target symbol replacement table stores original symbols of the target version shared library and replacement symbols corresponding to the original symbols, wherein the replacement symbols are symbols obtained by adding an identifier for indicating the version of the target version shared library to the original symbols; Step S2, performing symbol replacement on the original symbols in the target version shared library according to the target symbol replacement table, and generating a target version shared library containing the replacement symbols; Wherein, step S1 includes: Step S11, obtaining the original symbol in the target version shared library; Step S12, adding the identifier for indicating the version of the target version shared library to the original symbol to obtain the replacement symbol; Step S13, establishing a correspondence between the original symbol and the replacement symbol; Step S14, storing the original symbol and the replacement symbol in the target symbol replacement table in correspondence; Wherein, step S2 includes: Step S21, replacing the original symbol in the first file obtained by compiling the source code file of the target version shared library with the replacement symbol, to obtain the first file after the symbol replacement; Step S22, linking the first file after symbol replacement through a linker to generate the target version shared library containing the replacement symbols; The processing unit is configured to, when the executable file simultaneously links to a plurality of shared libraries, and each of the plurality of shared libraries links to one version of the first shared library, after performing steps S1 and S2 on each version of the first shared library, perform the following steps on each of the plurality of shared libraries: Step S3, performing symbol replacement on the original symbol in the second shared library according to the target symbol replacement table to generate a second shared library containing the replacement symbol, wherein the second shared library is one of the multiple shared libraries, and the second shared library is linked to the target version shared library; Wherein, step S3 includes: Step S31, replacing the original symbol in the second file obtained by compiling the source code file of the second shared library with the replacement symbol, to obtain the second file after the symbol is replaced; Step S32: Linking the second file after symbol replacement through a linker to generate the second shared library containing the replacement symbol.
3. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the method for processing a shared library with multiple versions according to claim 1 is implemented.
4. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method for processing a shared library with multiple versions as claimed in claim 1 is implemented.
Citation Information
Patent Citations
Shared software libraries for computing devices
CN109791483A