[go: up one dir, main page]

WO2017067175A1 - Method, apparatus and device for loading elf file, and computer storage medium - Google Patents

Method, apparatus and device for loading elf file, and computer storage medium Download PDF

Info

Publication number
WO2017067175A1
WO2017067175A1 PCT/CN2016/084304 CN2016084304W WO2017067175A1 WO 2017067175 A1 WO2017067175 A1 WO 2017067175A1 CN 2016084304 W CN2016084304 W CN 2016084304W WO 2017067175 A1 WO2017067175 A1 WO 2017067175A1
Authority
WO
WIPO (PCT)
Prior art keywords
segment
file
valid
loading
address
Prior art date
Application number
PCT/CN2016/084304
Other languages
French (fr)
Chinese (zh)
Inventor
秦松
陈鑫
位广军
马家智
桂敬文
王博通
王晓卿
张洪卫
常磊
张治�
李新开
Original Assignee
百度在线网络技术(北京)有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 百度在线网络技术(北京)有限公司 filed Critical 百度在线网络技术(北京)有限公司
Publication of WO2017067175A1 publication Critical patent/WO2017067175A1/en

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/44Arrangements for executing specific programs
    • G06F9/445Program loading or initiating

Definitions

  • the present invention relates to the field of computers, and in particular, to a method, an apparatus, a device, and a computer storage medium for loading an ELF file.
  • ELF Executable and Linking Format
  • ELF Executable and Linking Format
  • the existing embedded operating system uses the static link method to generate the executable file for the ELF file loading, and burns the executable file directly into the memory (Flash) by using the burning method, these processes must be performed. It is done under the ARM instruction set, and some processors do not use the ARM instruction set, and do not support the instruction set to the ARM instruction set. Therefore, the prior art cannot provide ELF file loading for such processors. .
  • the invention provides a method, a device, a device and a computer storage medium for loading an ELF file, so as to solve the problem that the prior art cannot record an ELF file in some embedded systems.
  • a method of loading an ELF file having an executable and link format comprising:
  • the extracted valid segment comprises: an executable code segment, an application data segment or a relocation segment.
  • the valid segment is extracted according to the ELF file header and the segment file header table of the object file.
  • the loading the valid segment comprises: loading different types of valid segments into different regions respectively.
  • the loading different types of valid segments into different regions separately includes:
  • the application data segment is directly loaded into the RAM according to the application data segment and the segment information of the corresponding relocation segment.
  • the segment information comprises a segment number, a segment offset or a segment size.
  • the executable code segment includes a .text segment
  • the application data segment includes a .data segment, a .bss segment, or a .rodata segment
  • the relocation segment includes a .rel.data segment. .rel.bss section, or .rel.rodata section.
  • the valid segment of the object file is marked with at least one of the following: a starting address of the segment, a segment offset, and a segment size.
  • the mapping table entry of the object mapping table includes an object name And an object address, the object address is obtained by searching an export object in the valid segment according to the address of the export object;
  • the mapping table entry of the function mapping table includes a function name and a function address, and the function address is passed Find the export function in the valid segment, based on the address where the exported function is located.
  • the address of the object in the object pool is searched for by the address of the current object in the object mapping table, and the text is The address of the jump object in the pool is modified to the address of the current object.
  • the address of the current function runtime and the address of the function that needs to be jumped from the function mapping table are used, and the jump instruction is obtained through calculation.
  • the previous jump instruction is updated with the calculated jump instruction to jump to the function address to be executed according to the updated jump instruction.
  • An apparatus for loading an ELF file comprising:
  • a parsing device configured to parse the static library file in the ELF file to obtain an object file
  • An extracting device configured to extract a valid segment from the object file
  • the loading device is configured to load a valid segment, and respectively establish an object mapping table and a function mapping table for the objects and functions in the object file, and are used for relocating the objects and functions in the loaded valid segment.
  • the valid segment extracted in the extracting device comprises: an executable code segment, an application data segment or a relocation segment.
  • the extracting means is configured to extract a valid segment according to an ELF file header and a segment file header table of the object file.
  • the loading device loading the valid segment comprises: loading different types of valid segments into different regions respectively.
  • the loading device when loading the different types of valid segments into different regions, performs:
  • the application data segment is directly loaded into the RAM according to the application data segment and the segment information of the corresponding relocation segment.
  • the segment information comprises a segment number, a segment offset or a segment size.
  • the executable code segment includes a .text segment
  • the application data segment includes a .data segment, a .bss segment, or a .rodata segment
  • the relocation segment includes a .rel.data segment. .rel.bss section, or .rel.rodata section.
  • the loading device is further configured to mark at least one of the following valid segments of the object file: a starting address of the segment, a segment offset, Segment size.
  • the mapping table entry of the object mapping table includes an object name and an object address, and the object address is obtained by searching for an export object in the valid segment according to the address of the export object;
  • the mapping table entry of the mapping table includes a function name and a function address, and the function address is obtained by searching for an export function in the valid segment according to the address where the derived function is located.
  • the loading device when the loading device relocates an object in the effective segment, the loading device specifically executes:
  • the address of the jump object of the object in the text pool is searched for by the address of the current object in the object mapping table, and the address of the jump object in the text pool is modified to the address of the current object.
  • the loading device when the loading device relocates the function in the valid segment, the loading device performs: using the address of the current function runtime and the function address that needs to be jumped from the function mapping table. After the calculation, the jump instruction is obtained, and the jump instruction obtained by the calculation is used to update the previous jump instruction, so as to jump to the function address to be executed according to the updated jump instruction.
  • the present invention adopts parsing a static library (.a) file and extracts a valid segment, loads the valid segment, and establishes an object and function mapping table for respectively respectively for objects and functions in the relocatable segment.
  • these processing methods are not limited to a specific instruction set, thus solving the problem that the prior art cannot load ELF files in some embedded systems.
  • FIG. 1 is a schematic flowchart of a method for dynamically loading an ELF file according to an embodiment of the present invention
  • FIG. 2 is a schematic diagram of a format of an ELF header according to an embodiment of the present invention.
  • FIG. 3 is a schematic structural diagram of an apparatus for loading an ELF file according to an embodiment of the present invention.
  • FIG. 1 is a schematic flowchart diagram of an embodiment of a method for loading an ELF file having an executable and a link format according to the present invention. As shown in Figure 1, the process includes:
  • the object file (.o) of the present invention is a re-directable file saved in ELF format, and the contents of the object file may contain entry marks and descriptions of the respective functions to form machine-executable instructions.
  • a link is also required. The link is to link multiple .o files into one executable file.
  • the ELF file can also contain an executable object file and an object file that can be shared.
  • the above three object files can be represented in the form of an object file .o, and multiple. o
  • the object file can be archived into a .a static library file, thereby finally forming an ELF file. Therefore, the purpose of parsing the static library (.a) file in the present invention is to obtain an object file (.o) constituting the ELF file.
  • the process of parsing the static library file is similar to the decompression (restore) process of a compressed package rar in the Windows operating system, that is, analyzing the static library (.a) generated by the object file (.o) package, and parsing each of them.
  • a valid segment can be extracted from each object file and segment information can be recorded.
  • the valid sections are in the ELF file and are used to load the smallest container of content data.
  • a valid segment is extracted according to an ELF header and a SHT (Section Header Table) of the object file, and each piece of information is recorded.
  • the specific type of the valid segment may include: an executable code segment, an application data segment, or a relocation segment.
  • the executable code segment may specifically include a .text segment, etc.
  • the application data segment may include a .data segment, a .bss segment, or a .rodata segment
  • the relocation segment may include .rel.data. Segment, .rel.bss section, or rel.rodata section.
  • the valid segment information of the record includes but is not limited to: each segment number, segment offset, or segment size, and the like.
  • the ELF Header in this embodiment can be represented in the format shown in FIG. 2.
  • the ELF Header represents the format of the relocatable object file (.o).
  • the corresponding sections and serial numbers can be known from the ELF Header, and what specific sections are in an ELF file are reflected in the SHT in the ELF file.
  • the structure definition of an entry in an SHT table can be:
  • an entry is set to describe the corresponding section, the content of which mainly includes the name, type, size of the section, and the byte offset position in the entire ELF file.
  • Off(sh_offset) in the structure of the entry in the above SHT table indicates the distance of the section from the head position of the file, that is, the offset; Size (sh_size) indicates the segment size of the section.
  • each valid segment can be extracted based on the relevant sections indicated by the ELF Header and SHT.
  • the above 101 and 102 are preparatory processes for loading before dynamically loading the ELF file, and the steps may be performed simultaneously or sequentially.
  • the present invention does not strictly define the order of execution thereof.
  • the process further proceeds through 103. Implements loading of valid segments of the object file.
  • For 103 it can be further divided into loading valid segments, establishing mapping tables, and The three steps are re-positioned, wherein the first two steps can be performed simultaneously or sequentially.
  • the order of the execution of the present invention is not rigidly specified, and the corresponding functions are all within the scope of the present invention.
  • Step 1 Load a valid segment.
  • the executable code segment, the application data segment, and the relocation segment are three types, wherein the executable code segment and the application data segment belong to a valid segment to be loaded, and the relocation segment does not need to be loaded.
  • the valid segment to be loaded it can be loaded into different regions in two ways according to its type.
  • the first way is to load the executable code segment (.text segment):
  • the .text segment can be relocated first, and then written to Flash.
  • the relocation method before writing to the flash may be: the identified .rel.
  • the .text segment recorded in the text segment needs to be relocated to create relocation information, relocate .text, and then copy and write the .text segment to Flash.
  • the second way is to load the application data segments (.data, .bss, and .rodata):
  • the loading methods for .data, .bss, and .rodata are slightly different from the loading method for .text segments. Compared to the loading of .text segments, the relocation process for .text segments is included before writing to Flash. In other words, the loading of .data, .bss, and .rodata does not require relocation before loading the segments into random access memory (RAM), but in accordance with .data, .bss, and .rodata. The valid segment information of the corresponding relocation segments (..rel.data, .rel.bss, and .rel.rodata) loads the .data, .bss, and .rodata segments directly into RAM.
  • RAM random access memory
  • each segment of each object file (.o) can be marked accordingly, and the tag can include but is not limited to the segment start address and the segment offset. , segment size and other information.
  • the starting address of the segment is obtained according to the initialization of the valid segment after loading, and the segment offset and the segment size are obtained based on the extracted valid segment when the valid segment is extracted.
  • a memory map is generated by separating the .text segment loaded with executable code from the application data of other application data segments and by creating relocation information for the target file of the executable code and loading the memory. The process avoids the process of copying the code data multiple times when loading the ELF file, which improves the running efficiency.
  • the second step is to establish an object mapping table and a function mapping table for the objects and functions in the object file.
  • the object mapping table (obj_map) and the function mapping table (func_map) are respectively established according to the difference between the object and the function, and the created mapping table is used for subsequent relocation and external calling.
  • the mapping table entry of the object mapping table includes an object name and an object address, and the object address in the mapping table is obtained by searching an export object in a valid segment of the object file, according to an address of the export object; a mapping table of the function mapping table
  • the item includes the function name and the function address, which is obtained by looking up the export function in the valid section of the object file, according to the address where the function is derived.
  • .text segments are functions, and .text segments, .data segments, .bss segments, .rodata segments, file variables, function variables, and other global variables, function elements, etc. Can be represented by an object. Since the division of objects and functions is prior art, it will not be described here.
  • the third step is to relocate the objects and functions in the valid segment loaded.
  • the object in the valid segment is relocated using the object mapping table, or the function in the valid segment is relocated using the function mapping table.
  • Two different relocation methods can be used based on the objects and function types extracted from the object file.
  • the first way is to relocate the object (obj):
  • the object is used in the .o file by writing the object's real address directly into the literal pool.
  • the address of the object in the object pool is searched for by the address of the current object in the object mapping table, and the address of the jump object in the text pool is modified to the current object. the address of.
  • the compiler puts the address of the object into the Literal Pool area.
  • the Literal Pool only stores absolute addresses or constants.
  • the text pool is obtained from the current address of the object recorded in the object mapping table according to the object relocation information contained in .rel.text, .rel.data, .rel.bss, and .rel.rodata.
  • the absolute address of the jump object recorded in the file find the absolute address from the Literal Pool, in the dynamic loading, the absolute address in the text pool where the object is located is modified to the address where the current object is located, thus .text, .data,
  • the objects contained in the .bss and .rodata sections are relocated.
  • the function call jump uses a corresponding jump instruction, such as B/BL, etc., when the function is relocated, the function is relocated based on the function included in .rel.text, etc., and the function mapping table needs to jump.
  • the real address of the function according to the current address where the function is running, combined with the real address of the function that needs to be jumped from the function mapping table, after a series of calculations, the jump instruction is obtained, and the correct jump instruction is obtained.
  • the jump instruction is updated to load the relocated function according to the updated jump instruction.
  • the object mapping table it is not only based on the object mapping table and the function mapping table to find the address. Since the object may refer to an external object or the function uses a dynamic library function when dynamically loaded, the corresponding information cannot be found in the object or function mapping table.
  • the address information indicates that the object or function refers to an external object or a dynamic library function, it is possible to dynamically find the address of the object or function for relocation, thereby implementing dynamic loading.
  • FIG. 3 is a schematic structural diagram of an apparatus for loading an ELF file according to the present invention.
  • the device includes: a parsing device 301, an extracting device 302, and a loading device 303. The following describes each device in detail:
  • the parsing device 301 is configured to parse a static library (.a) file in the ELF file to obtain an object file (.o).
  • the object file (.o) of the present invention is a re-directable file saved in ELF format, and the contents of the object file may contain entry marks and descriptions of the respective functions to form machine-executable instructions.
  • a link is also required. The link is to link multiple .o files into one executable file. Multiple .o object files can be archived into .a static Library files, and static library file lines become ELF files.
  • the purpose of the parsing device to parse the static library (.a) file is to obtain the object file (.o) therein.
  • the process of parsing the static library file is similar to the decompression (restore) process of a compressed package rar in the Windows operating system, that is, analyzing the static library (.a) generated by the object file (.o) package, and parsing each of them.
  • the extracting device 302 is configured to extract a valid section from the acquired object file.
  • a valid segment can be extracted from each object file and segment information can be recorded.
  • the valid sections are in the ELF file and are used to load the smallest container of content data.
  • each section is loaded with the same properties.
  • a valid segment is extracted according to an ELF header and a SHT (Section Header Table) of the object file, and each piece of information is recorded.
  • the specific type of the valid segment may include: an executable code segment, an application data segment, or a relocation segment.
  • the executable code segment may specifically include a .text segment, etc.
  • the application data segment may include a .data segment, a .bss segment, or a .rodata segment
  • the relocation segment may include a .rel.data segment and a .rel. Bss segment, or rel.rodata segment, etc.
  • the valid segment information of the record includes but is not limited to: each segment number, segment offset, or segment size, and the like.
  • the corresponding sections and serial numbers are known from the ELF Header. From the contents of the SHT, information such as the segment offset and the segment size are obtained according to the fields Off(sh_offset) and Size(sh_size) in the SHT.
  • the devices 301 and 302 are configured to perform pre-loading preparation on the pre-loaded ELF file before the ELF file is dynamically loaded, and the preparation process may be performed simultaneously or sequentially, and the present invention does not strictly define the order of execution thereof. After execution, the loading of the valid segment of the object file is further implemented by the device 303.
  • the loading device 303 is configured to load a valid segment, establish an object mapping table and a function mapping table for the objects and functions in the object file, and re-locate the objects and functions in the valid segment loaded.
  • the loading device 303 can be further divided into three units: a valid segment loading unit 3031, a mapping table establishing unit 3032, and a relocating unit 3033.
  • the first two units can perform corresponding operations simultaneously or in sequence, and the order in which the present invention is executed. Without stipulations, as long as the corresponding functions can be completed, they are all within the scope of the present invention.
  • the valid segment unit 3031 is loaded.
  • the valid segment extracted by the extracting unit 302 there are three types of executable code segments, application data segments, and relocation segments, wherein the executable code segment and the application data segment belong to a valid segment that needs to be loaded, and the relocation segment does not need to be Loading, for the valid segment to be loaded, it can be loaded into different regions in two ways according to its type.
  • the first way is to load the executable code segment (.text segment):
  • the .text segment can be relocated first, and then written to Flash.
  • the relocation method before writing to the flash may be: the identified .rel.
  • the .text segment recorded in the text segment needs to be relocated to create relocation information, relocate .text, and then copy and write the .text segment to Flash.
  • the second way is to load the application data segments (.data, .bss, and .rodata):
  • the loading methods for .data, .bss, and .rodata are slightly different from the loading method for .text segments. Compared to the loading of .text segments, the relocation process for .text segments is included before writing to Flash. In other words, the loading of .data, .bss, and .rodata does not require relocation before loading the segments into random access memory (RAM), but in accordance with .data, .bss, and .rodata.
  • the valid segment information of the corresponding relocation segments loads the .data, .bss, and .rodata segments directly into RAM.
  • each segment of each object file (.o) can be marked accordingly, and the tag can include but is not limited to the segment start address and the segment offset. , segment size and other information.
  • the starting address of the segment is obtained based on the initialization of the valid segment after loading.
  • the quantity and segment size are obtained based on the extracted valid segment when the valid segment is extracted.
  • the .text segment loaded with the executable code is separated from the application data of other application data segments, and the relocation information is created and loaded into the memory by the target file of the executable code.
  • the mapping table establishing unit 3032 is configured to separately establish an object mapping table and a function mapping table for objects and functions in the object file.
  • This unit is used to establish an object mapping table (obj_map) and a function mapping table (func_map) according to the difference between the object and the function.
  • the created mapping table is used for subsequent relocation and external calling.
  • the mapping table entry of the object mapping table includes an object name and an object address, and the object address in the mapping table is obtained by searching an export object in a valid segment of the object file, according to an address of the export object; a mapping table of the function mapping table
  • the item includes the function name and the function address, which is obtained by looking up the export function in the valid section of the object file, according to the address where the function is derived.
  • .text segments are functions, and .text segments, .data segments, .bss segments, .rodata segments, file variables, function variables, and other global variables, function elements, etc. Can be represented by an object. Since the division of objects and functions is prior art, it will not be described here.
  • the relocating unit 3033 is configured to relocate the objects and functions in the valid segment loaded.
  • This unit is used to relocate an object in a valid segment using an object mapping table, or to relocate a function in a valid segment using the function mapping table.
  • Two different relocation methods can be used based on the objects and function types extracted from the object file.
  • the first way is to relocate the object (obj):
  • the object is used in the .o file by writing the object's real address directly into the literal pool.
  • the address of the object in the object pool is searched for by the address of the current object in the object mapping table, and the address of the jump object in the text pool is modified to the current object. the address of.
  • the jump instruction is obtained, and the jump instruction obtained by the calculation is used to jump to the previous jump.
  • the instruction is updated to jump to the address of the function to be executed in accordance with the updated jump instruction.
  • the object mapping table it is not only based on the object mapping table and the function mapping table to find the address. Since the object may refer to an external object or the function uses a dynamic library function when dynamically loaded, the corresponding information cannot be found in the object or function mapping table.
  • the address information indicates that the object or function refers to an external object or a dynamic library function, the address of the object or function can be searched for relocation, thereby implementing dynamic loading.
  • the method and device for dynamically recording the ELF file of the present invention can be applied to various scenarios such as a set top box, a smart wearable device, and a smart home cloud. Take a set-top box as an example, when the user makes When the ELF file is started or installed by the set top box, the method and apparatus provided by the present invention can be used to implement dynamic loading of the ELF file by parsing and loading and relocating the static library file (.a).
  • the present invention adopts a method of parsing a static library (.a) file and extracting a valid segment, loading a valid segment, and establishing an object and a function mapping table for relocating objects and functions in the relocatable segment respectively.
  • These processing methods are not limited to a specific instruction set, thus solving the problem that the prior art cannot load ELF files in some embedded systems.
  • the present invention is particularly useful for supporting Cortex-M series processor embedded systems employing the Thumb-2 instruction set.
  • Mbed OS is an operating system based on the ARM Cortex-M processor
  • Mbed os has a unique advantage as a system specifically tailored for embedded development.
  • the Cortex-M series processors are already capable of supporting Mbed os, but currently Mbed os cannot provide ELF file loaders for the Cortex-M processors, thus limiting the Mbed os on the Cortex-M series processors. use. This is because the existing embedded operating system generates a executable file by static linking for the loading of ELF files, and burns the executable file to Flash.
  • the method and apparatus provided by the present invention converts an idea not to generate an executable file by static linking and directly load the memory, nor to parse and load the ELF file through a specific ARM instruction set. Instead, parse the static library file of the ELF file. To obtain a valid segment of the ELF relocatable object file and relocate it so that objects and functions in the ELF file can be dynamically loaded in a relocatable manner.
  • the processes of the present invention are not limited to a particular ARM instruction set and are therefore particularly well-suited for supporting Cortex-M series processor embedded systems employing the Thumb-2 instruction set.
  • an executable file is generated by static linking, and the executable file is burned to Flash, so the number of applications running is limited by the number of executable files burned in Flash.
  • the present invention adopts a dynamic loading mode, based on which dynamic linking can be implemented, and the number of running applications is no longer limited by the number of executable files in the Flash.
  • the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, may be located in one place, or may be distributed to multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of the embodiment.
  • each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
  • the above integrated unit can be implemented in the form of hardware or in the form of hardware plus software functional units.

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Stored Programmes (AREA)
  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

A method, apparatus and device for dynamically loading an executable and linking format (ELF) file, and a computer storage medium. The method comprises: parsing a static library file in an ELF file to obtain an object file (101); extracting an effective segment from the object file, and recording effective segment information (102); and loading the effective segment, and separately establishing an object mapping table and a function mapping table for objects and functions in the object file to re-position the objects and the functions in the loaded effective segment (103). By means of the method and device for dynamically loading an ELF file provided in the solution, the troubles of an embedded system needing to depend on a specific instruction set when the embedded system parses and loads an ELF file and the embedded system is not be suitable for all embedded processor architectures in the prior art can be eliminated, applications of multiple ELF files can be simultaneously operated under a specific processor system structure, and dynamic linking can be realized, thereby greatly reducing a file volume generated by using static linking before, and reducing power consumption.

Description

一种加载ELF文件的方法、装置、设备和计算机存储介质Method, device, device and computer storage medium for loading ELF file
本申请要求了申请日为2015年10月21日,申请号为201510688388.3发明名称为“一种加载ELF文件的方法和装置”的中国专利申请的优先权。The present application claims priority to Chinese Patent Application No. 201510688388.3, entitled "A Method and Apparatus for Loading ELF Files", filed on October 21, 2015.
技术领域Technical field
本发明涉及计算机领域,尤其涉及一种加载ELF文件的方法、装置、设备和计算机存储介质。The present invention relates to the field of computers, and in particular, to a method, an apparatus, a device, and a computer storage medium for loading an ELF file.
背景技术Background technique
具有可执行和链接格式的ELF(Executable and Linking Format)是一种对象文件的格式,用于定义不同类型的对象文件(Object files)中的内容及其格式。由于现有的嵌入式操作系统对于ELF文件的加载采用的是静态链接的方式生成可执行文件,并采用烧录的方式将可执行文件直接烧录至内存(Flash)中,这些处理过程都必须在ARM指令集下完成的,而有些处理器采用的并非ARM指令集,且不支持其所采用的指令集到ARM指令集的切换,因此现有技术无法为此类处理器提供ELF文件的加载。ELF (Executable and Linking Format) with executable and link format is a format of an object file for defining content and its format in different types of object files. Since the existing embedded operating system uses the static link method to generate the executable file for the ELF file loading, and burns the executable file directly into the memory (Flash) by using the burning method, these processes must be performed. It is done under the ARM instruction set, and some processors do not use the ARM instruction set, and do not support the instruction set to the ARM instruction set. Therefore, the prior art cannot provide ELF file loading for such processors. .
发明内容Summary of the invention
本发明提出了一种加载ELF文件的方法、装置、设备和计算机存储介质,以便于解决现有技术在一些嵌入式系统中无法记载ELF文件的问题。The invention provides a method, a device, a device and a computer storage medium for loading an ELF file, so as to solve the problem that the prior art cannot record an ELF file in some embedded systems.
具体技术方案如下:The specific technical solutions are as follows:
一种加载具有可执行和链接格式ELF文件的方法,所述方法包括:A method of loading an ELF file having an executable and link format, the method comprising:
对所述ELF文件中的静态库文件进行解析以获取对象文件;Parsing a static library file in the ELF file to obtain an object file;
从所述对象文件中提取有效段; Extracting a valid segment from the object file;
加载有效段,并为所述对象文件中的对象及函数分别建立对象映射表和函数映射表,用于对加载的有效段中的对象及函数进行重定位。Loading a valid segment, and establishing an object mapping table and a function mapping table for the objects and functions in the object file, respectively, for relocating the objects and functions in the loaded valid segment.
根据本发明一优选实施例,提取的有效段包括:可执行代码段、应用数据段或重定位段。According to a preferred embodiment of the invention, the extracted valid segment comprises: an executable code segment, an application data segment or a relocation segment.
根据本发明一优选实施例,根据所述对象文件的ELF文件头和段文件头表来提取有效段。According to a preferred embodiment of the invention, the valid segment is extracted according to the ELF file header and the segment file header table of the object file.
根据本发明一优选实施例,所述加载有效段包括:将不同类型的有效段分别加载到不同的区域。According to a preferred embodiment of the present invention, the loading the valid segment comprises: loading different types of valid segments into different regions respectively.
根据本发明一优选实施例,所述将不同类型的有效段分别加载到不同的区域包括:According to a preferred embodiment of the present invention, the loading different types of valid segments into different regions separately includes:
依照可执行代码段及相应重定位段的段信息,对可执行代码段进行重定位,将重定位后的可执行代码段写入Flash;或者Relocating the executable code segment according to the executable code segment and the segment information of the corresponding relocation segment, and writing the re-executed executable code segment to Flash; or
依照应用数据段及相应重定位段的段信息,将应用数据段直接加载到RAM。The application data segment is directly loaded into the RAM according to the application data segment and the segment information of the corresponding relocation segment.
根据本发明一优选实施例,所述段信息包括段序号、段偏移量或段大小。According to a preferred embodiment of the invention, the segment information comprises a segment number, a segment offset or a segment size.
根据本发明一优选实施例,所述可执行代码段包括.text段,所述应用数据段包括.data段、.bss段、或.rodata段,所述重定位段包括.rel.data段、.rel.bss段、或.rel.rodata段。According to a preferred embodiment of the present invention, the executable code segment includes a .text segment, and the application data segment includes a .data segment, a .bss segment, or a .rodata segment, and the relocation segment includes a .rel.data segment. .rel.bss section, or .rel.rodata section.
根据本发明一优选实施例,在所述加载有效段之后,对所述对象文件的有效段进行以下至少一个的标记:段的起始地址、段偏移量、段大小。According to a preferred embodiment of the present invention, after the valid segment is loaded, the valid segment of the object file is marked with at least one of the following: a starting address of the segment, a segment offset, and a segment size.
根据本发明一优选实施例,所述对象映射表的映射表项包括对象名 称和对象地址,所述对象地址是通过查找有效段中的导出对象,根据导出对象所在地址而得到的;所述函数映射表的映射表项包括函数名称和函数地址,所述函数地址是通过查找有效段中的导出函数,根据导出函数所在的地址而得到的。According to a preferred embodiment of the present invention, the mapping table entry of the object mapping table includes an object name And an object address, the object address is obtained by searching an export object in the valid segment according to the address of the export object; the mapping table entry of the function mapping table includes a function name and a function address, and the function address is passed Find the export function in the valid segment, based on the address where the exported function is located.
根据本发明一优选实施例,在对有效段中的对象进行重定位时,利用当前对象在所述对象映射表中的地址查找所述对象的跳转对象在文字池中的地址,并将文字池中所述跳转对象的地址修改为当前对象的地址。According to a preferred embodiment of the present invention, when the object in the valid segment is relocated, the address of the object in the object pool is searched for by the address of the current object in the object mapping table, and the text is The address of the jump object in the pool is modified to the address of the current object.
根据本发明一优选实施例,在对有效段中的函数进行重定位时,利用当前函数运行时的地址以及从函数映射表中查找到的需要跳转到的函数地址,经过计算得到跳转指令,利用计算得到的跳转指令对之前的跳转指令进行更新,以便于根据所述更新的跳转指令跳转到即将要执行的函数地址。According to a preferred embodiment of the present invention, when the function in the valid segment is relocated, the address of the current function runtime and the address of the function that needs to be jumped from the function mapping table are used, and the jump instruction is obtained through calculation. The previous jump instruction is updated with the calculated jump instruction to jump to the function address to be executed according to the updated jump instruction.
一种加载ELF文件的装置,所述装置包括:An apparatus for loading an ELF file, the apparatus comprising:
解析装置,用于对所述ELF文件中的静态库文件进行解析以获取对象文件;a parsing device, configured to parse the static library file in the ELF file to obtain an object file;
提取装置,用于从所述对象文件中提取有效段;An extracting device, configured to extract a valid segment from the object file;
加载装置,用于加载有效段,为所述对象文件中的对象及函数分别建立对象映射表和函数映射表,并且用于对加载的有效段中的对象及函数进行重定位。The loading device is configured to load a valid segment, and respectively establish an object mapping table and a function mapping table for the objects and functions in the object file, and are used for relocating the objects and functions in the loaded valid segment.
根据本发明一优选实施例,所述提取装置中提取的有效段包括:可执行代码段、应用数据段或重定位段。According to a preferred embodiment of the present invention, the valid segment extracted in the extracting device comprises: an executable code segment, an application data segment or a relocation segment.
根据本发明一优选实施例,所述提取装置用于根据所述对象文件的ELF文件头和段文件头表来提取有效段。 According to a preferred embodiment of the present invention, the extracting means is configured to extract a valid segment according to an ELF file header and a segment file header table of the object file.
根据本发明一优选实施例,所述加载装置加载有效段包括:将不同类型的有效段分别加载到不同的区域。According to a preferred embodiment of the present invention, the loading device loading the valid segment comprises: loading different types of valid segments into different regions respectively.
根据本发明一优选实施例,所述加载装置在将不同类型的有效段分别加载到不同的区域时,具体执行:According to a preferred embodiment of the present invention, when loading the different types of valid segments into different regions, the loading device performs:
依照可执行代码段及相应重定位段的段信息,对可执行代码段进行重定位,将重定位后的可执行代码段写入Flash;或者Relocating the executable code segment according to the executable code segment and the segment information of the corresponding relocation segment, and writing the re-executed executable code segment to Flash; or
依照应用数据段及相应重定位段的段信息,将应用数据段直接加载到RAM。The application data segment is directly loaded into the RAM according to the application data segment and the segment information of the corresponding relocation segment.
根据本发明一优选实施例,所述段信息包括段序号、段偏移量或段大小。According to a preferred embodiment of the invention, the segment information comprises a segment number, a segment offset or a segment size.
根据本发明一优选实施例,所述可执行代码段包括.text段,所述应用数据段包括.data段、.bss段、或.rodata段,所述重定位段包括.rel.data段、.rel.bss段、或.rel.rodata段。According to a preferred embodiment of the present invention, the executable code segment includes a .text segment, and the application data segment includes a .data segment, a .bss segment, or a .rodata segment, and the relocation segment includes a .rel.data segment. .rel.bss section, or .rel.rodata section.
根据本发明一优选实施例,所述加载装置在执行所述加载有效段之后,进一步用于对所述对象文件的有效段进行以下至少一个的标记:段的起始地址、段偏移量、段大小。According to a preferred embodiment of the present invention, after the loading valid segment is executed, the loading device is further configured to mark at least one of the following valid segments of the object file: a starting address of the segment, a segment offset, Segment size.
根据本发明一优选实施例,所述对象映射表的映射表项包括对象名称和对象地址,所述对象地址是通过查找有效段中的导出对象,根据导出对象所在地址而得到的;所述函数映射表的映射表项包括函数名称和函数地址,所述函数地址是通过查找有效段中的导出函数,根据导出函数所在的地址而得到的。According to a preferred embodiment of the present invention, the mapping table entry of the object mapping table includes an object name and an object address, and the object address is obtained by searching for an export object in the valid segment according to the address of the export object; The mapping table entry of the mapping table includes a function name and a function address, and the function address is obtained by searching for an export function in the valid segment according to the address where the derived function is located.
根据本发明一优选实施例,所述加载装置在对有效段中的对象进行重定位时,具体执行: According to a preferred embodiment of the present invention, when the loading device relocates an object in the effective segment, the loading device specifically executes:
利用当前对象在所述对象映射表中的地址查找所述对象的跳转对象在文字池中的地址,并将文字池中所述跳转对象的地址修改为当前对象的地址。The address of the jump object of the object in the text pool is searched for by the address of the current object in the object mapping table, and the address of the jump object in the text pool is modified to the address of the current object.
根据本发明一优选实施例,所述加载装置在对有效段中的函数进行重定位时,具体执行:利用当前函数运行时的地址以及从函数映射表中查找到的需要跳转到的函数地址,经过计算得到跳转指令,利用计算得到的跳转指令对之前的跳转指令进行更新,以便于根据所述更新的跳转指令跳转到即将要执行的函数地址。According to a preferred embodiment of the present invention, when the loading device relocates the function in the valid segment, the loading device performs: using the address of the current function runtime and the function address that needs to be jumped from the function mapping table. After the calculation, the jump instruction is obtained, and the jump instruction obtained by the calculation is used to update the previous jump instruction, so as to jump to the function address to be executed according to the updated jump instruction.
由以上技术方案可以看出,本发明采用了解析静态库(.a)文件并且提取有效段,对有效段进行加载,建立对象及函数映射表用于对可重定位段中的对象和函数分别进行重定位的方式,这些处理方式并不受限于特定指令集,因此解决了现有技术在一些嵌入式系统中无法加载ELF文件的问题。It can be seen from the above technical solution that the present invention adopts parsing a static library (.a) file and extracts a valid segment, loads the valid segment, and establishes an object and function mapping table for respectively respectively for objects and functions in the relocatable segment. In the way of relocation, these processing methods are not limited to a specific instruction set, thus solving the problem that the prior art cannot load ELF files in some embedded systems.
附图说明DRAWINGS
图1为本发明实施例提供的一种对ELF文件动态加载方法的流程示意图;FIG. 1 is a schematic flowchart of a method for dynamically loading an ELF file according to an embodiment of the present invention;
图2为本发明实施例提供的ELF Header的格式示意图;2 is a schematic diagram of a format of an ELF header according to an embodiment of the present invention;
图3为本发明实施例提供的一种加载ELF文件的装置的结构示意图。FIG. 3 is a schematic structural diagram of an apparatus for loading an ELF file according to an embodiment of the present invention.
具体实施方式detailed description
为了使本发明的目的、技术方案和优点更加清楚,下面结合附图和具体实施例对本发明进行详细描述。The present invention will be described in detail below with reference to the drawings and specific embodiments.
请参考图1,图1为本发明提供的一种加载具有可执行和链接格式的ELF文件的方法实施例的流程示意图。如图1所示,该流程包括: Please refer to FIG. 1. FIG. 1 is a schematic flowchart diagram of an embodiment of a method for loading an ELF file having an executable and a link format according to the present invention. As shown in Figure 1, the process includes:
101:对ELF文件中的静态库(.a)文件进行解析以获取对象文件(.o)。101: Parse the static library (.a) file in the ELF file to obtain an object file (.o).
本发明的对象文件(.o)是以ELF格式保存的可重定向文件,对象文件中的内容可以包含对各个函数的入口标记,描述,以形成机器可执行的指令。当程序要执行时还需要进行链接(link),链接就是把多个.o文件链成一个可执行文件。The object file (.o) of the present invention is a re-directable file saved in ELF format, and the contents of the object file may contain entry marks and descriptions of the respective functions to form machine-executable instructions. When the program is to be executed, a link is also required. The link is to link multiple .o files into one executable file.
ELF文件除了可以包含可重定位的对象文件外,还可以包含可执行的对象文件和可被共享的对象文件,上述三种对象文件由于都可以以对象文件.o的形式表示,而多个.o对象文件可以归档(archive)成.a静态库文件,从而最终组成ELF文件,因此本发明中解析静态库(.a)文件的目的就是为了获取组成ELF文件的对象文件(.o)。In addition to the relocatable object file, the ELF file can also contain an executable object file and an object file that can be shared. The above three object files can be represented in the form of an object file .o, and multiple. o The object file can be archived into a .a static library file, thereby finally forming an ELF file. Therefore, the purpose of parsing the static library (.a) file in the present invention is to obtain an object file (.o) constituting the ELF file.
具体地,解析静态库文件的过程与windows操作系统中一个压缩包rar的解压缩(还原)过程类似,也就是分析由对象文件(.o)打包生成的静态库(.a),解析其中每个对象文件(.o)。Specifically, the process of parsing the static library file is similar to the decompression (restore) process of a compressed package rar in the Windows operating system, that is, analyzing the static library (.a) generated by the object file (.o) package, and parsing each of them. Object files (.o).
102:从获取的对象文件中提取有效段(section)。102: Extract a valid section from the obtained object file.
当获取对象文件(.o)后,可以针对每一个对象文件,从中提取有效段并且记录段信息。After the object file (.o) is acquired, a valid segment can be extracted from each object file and segment information can be recorded.
有效段(sections)在ELF文件里面,用以装载内容数据的最小容器。The valid sections are in the ELF file and are used to load the smallest container of content data.
优选地,可以针对每个获取的对象文件,根据所述对象文件的ELF文件头(ELF Header)和段文件头表(SHT,Section Header Table)来提取有效段并记录各段信息。Preferably, for each acquired object file, a valid segment is extracted according to an ELF header and a SHT (Section Header Table) of the object file, and each piece of information is recorded.
该有效段的具体类型可以包括:可执行代码段,应用数据段,或重定位段。其中该可执行代码段又可以具体包括.text段等,该应用数据段可以包括.data段、.bss段、或.rodata段等,该重定位段可以包括.rel.data 段、.rel.bss段、或rel.rodata段等。The specific type of the valid segment may include: an executable code segment, an application data segment, or a relocation segment. The executable code segment may specifically include a .text segment, etc., and the application data segment may include a .data segment, a .bss segment, or a .rodata segment, and the relocation segment may include .rel.data. Segment, .rel.bss section, or rel.rodata section.
该记录的有效段信息包括但不限于:各段序号、段偏移量、或段大小等。The valid segment information of the record includes but is not limited to: each segment number, segment offset, or segment size, and the like.
其中among them
1).text段里装载了可执行代码;1) The executable file is loaded in the .text section;
2).data段里装载了被初始化的数据;2) The .data section is loaded with the initialized data;
3).bss段里装载了未被初始化的数据;3) The .bss section is loaded with uninitialized data;
4).rodata段里装载了相应字符串和定义的常量4) The .rodata section is loaded with the corresponding string and the defined constant
5)以.rel打头的重定位段rel.text段、.rel.data段、.rel.bss段、.rel.rodata段里面装载了重定位条目,该重定位段是用于帮助可执行代码段和应用数据段进行重定位的段;5) The relocation segment starting with .rel, the rel.text segment, the .rel.data segment, the .rel.bss segment, and the .rel.rodata segment, are loaded with relocation entries, which are used to help the executable code. Segments and segments in which the application data segment is relocated;
例如:.rel.text中示出了.text段中哪些地方需要做重定位。For example: .rel.text shows where in the .text section you need to do relocation.
以下举个具体实例以说明从对象文件的ELF Header和SHT中提取有效段及各段信息的具体方式:The following is a specific example to illustrate the specific way to extract valid segments and pieces of information from the ELF Header and SHT of the object file:
本实施例中的ELF Header可以以图2所示的格式表示。The ELF Header in this embodiment can be represented in the format shown in FIG. 2.
如图2所示,该ELF Header表示了可重定位对象文件(.o)的格式。可以从该ELF Header中得知相应的sections及序号,而一个ELF文件中到底有哪些具体的sections,则反映在这个ELF文件中的SHT中。As shown in Figure 2, the ELF Header represents the format of the relocatable object file (.o). The corresponding sections and serial numbers can be known from the ELF Header, and what specific sections are in an ELF file are reflected in the SHT in the ELF file.
SHT表中条目的结构定义可以为:The structure definition of an entry in an SHT table can be:
typedef struct{Typedef struct{
Elf32_Word  sh_name;Elf32_Word sh_name;
Elf32_Word  sh_type;Elf32_Word sh_type;
Elf32_Word  sh_flags; Elf32_Word sh_flags;
Elf32_Addr  sh_addr;Elf32_Addr sh_addr;
Elf32_Off   sh_offset;Elf32_Off sh_offset;
Elf32_Word  sh_size;Elf32_Word sh_size;
Elf32_Word  sh_link;Elf32_Word sh_link;
Elf32_Word  sh_info;Elf32_Word sh_info;
Elf32_Word  sh_addralign;Elf32_Word sh_addralign;
Elf32_Word  sh_entsize;Elf32_Word sh_entsize;
}Elf32_Shdr;}Elf32_Shdr;
在SHT中,针对每一个section,都设置有一个条目,用来描述对应的这个section,其内容主要包括该section的名称、类型、大小以及在整个ELF文件中的字节偏移位置等等。In SHT, for each section, an entry is set to describe the corresponding section, the content of which mainly includes the name, type, size of the section, and the byte offset position in the entire ELF file.
上述SHT表中条目的结构中字段Off(sh_offset)表示了该section离开文件头部位置的距离,即偏移量;Size(sh_size)表示section的段大小。The field Off(sh_offset) in the structure of the entry in the above SHT table indicates the distance of the section from the head position of the file, that is, the offset; Size (sh_size) indicates the segment size of the section.
从而根据ELF Header和SHT表示的相关section从而能够提取各有效段的信息。Thereby, the information of each valid segment can be extracted based on the relevant sections indicated by the ELF Header and SHT.
以上的101和102是在动态加载ELF文件之前对加载的准备过程,其步骤可以同时或者分先后执行,本发明对其执行的顺序不做硬性规定,当上述准备过程执行后,则通过103进一步实现对对象文件的有效段的加载。The above 101 and 102 are preparatory processes for loading before dynamically loading the ELF file, and the steps may be performed simultaneously or sequentially. The present invention does not strictly define the order of execution thereof. When the above preparation process is performed, the process further proceeds through 103. Implements loading of valid segments of the object file.
103:加载有效段,并为所述对象文件中的对象及函数分别建立对象映射表和函数映射表,用于对加载到的有效段中的对象及函数进行重定位。103: Load a valid segment, and establish an object mapping table and a function mapping table for the objects and functions in the object file, respectively, for relocating the objects and functions in the valid segment loaded.
对于103,还可以将其进一步划分为加载有效段、建立映射表以及 重定位三个步骤,其中前两个步骤可以同时或者分先后执行,本发明对其执行的顺序不做硬性规定,只要能完成相应的功能,则均在本发明保护范围之内。For 103, it can be further divided into loading valid segments, establishing mapping tables, and The three steps are re-positioned, wherein the first two steps can be performed simultaneously or sequentially. The order of the execution of the present invention is not rigidly specified, and the corresponding functions are all within the scope of the present invention.
以下对三个步骤进行详细描述:The three steps are described in detail below:
第一步:加载有效段。Step 1: Load a valid segment.
对于在102所提取的有效段,包括可执行代码段、应用数据段以及重定位段三种类型,其中可执行代码段和应用数据段属于需要加载的有效段,而重定位段不需要加载,针对该需要加载的有效段,可以依据其类型的不同可以对应的采用两种方式将其加载到不同的区域。For the valid segment extracted at 102, the executable code segment, the application data segment, and the relocation segment are three types, wherein the executable code segment and the application data segment belong to a valid segment to be loaded, and the relocation segment does not need to be loaded. For the valid segment to be loaded, it can be loaded into different regions in two ways according to its type.
第一种方式,对于可执行代码段(.text段)加载:The first way is to load the executable code segment (.text segment):
由于针对于不同的产品其flash的读写不一致,因此为了便于重定位修改.text段内容,可以先对.text段进行重定位,然后再将其写入Flash。Since the flash reading and writing are inconsistent for different products, in order to facilitate relocation and modify the content of the .text segment, the .text segment can be relocated first, and then written to Flash.
由于提取的与.text段相应的重定位段(.rel.text段)中包含.text段需要重定位的信息,因此在写入flash之前的重定位方式可以为:将识别出的.rel.text段中记录的.text段需要重定位的部分记录下来创建重定位信息,对.text做重定位,然后再将.text段拷贝并写入到Flash中。Since the extracted relocation segment (.rel.text segment) corresponding to the .text segment contains the information that the .text segment needs to be relocated, the relocation method before writing to the flash may be: the identified .rel. The .text segment recorded in the text segment needs to be relocated to create relocation information, relocate .text, and then copy and write the .text segment to Flash.
第二种方式,对于应用数据段(.data、.bss及.rodata)的加载:The second way is to load the application data segments (.data, .bss, and .rodata):
对于.data、.bss及.rodata的加载方式与对.text段的加载方式略有不同,相比于对.text段的加载在写入到Flash之前还包括对.text段的重定位过程而言,对.data、.bss及.rodata的加载在将各段加载到随机存取存储器(Random Access Memory,RAM)之前并不需要进行重定位,而是依照.data、.bss及.rodata及相应重定位段(..rel.data、.rel.bss及.rel.rodata)的有效段信息,将.data、.bss及.rodata段直接加载到RAM。 The loading methods for .data, .bss, and .rodata are slightly different from the loading method for .text segments. Compared to the loading of .text segments, the relocation process for .text segments is included before writing to Flash. In other words, the loading of .data, .bss, and .rodata does not require relocation before loading the segments into random access memory (RAM), but in accordance with .data, .bss, and .rodata. The valid segment information of the corresponding relocation segments (..rel.data, .rel.bss, and .rel.rodata) loads the .data, .bss, and .rodata segments directly into RAM.
在通过第一种和第二种方式加载有效段后,可以对各个对象文件(.o)的各个段做好相应的标记,该标记可以包括但不限于段的起始地址、段偏移量、段大小等信息。After the valid segment is loaded by the first and second methods, each segment of each object file (.o) can be marked accordingly, and the tag can include but is not limited to the segment start address and the segment offset. , segment size and other information.
其中段的起始地址是根据对加载后的有效段初始化获得的,段偏移量、段大小是在提取有效段时基于该提取的有效段获得的。The starting address of the segment is obtained according to the initialization of the valid segment after loading, and the segment offset and the segment size are obtained based on the extracted valid segment when the valid segment is extracted.
在第一步中,通过将装载了可执行代码的.text段与其他的应用数据段的应用数据分离,并通过对可执行代码的目标文件创建重定位信息并载入内存,从而生成内存映射过程,避免了在加载ELF文件时多次拷贝代码数据的过程,提高了运行效率。In the first step, a memory map is generated by separating the .text segment loaded with executable code from the application data of other application data segments and by creating relocation information for the target file of the executable code and loading the memory. The process avoids the process of copying the code data multiple times when loading the ELF file, which improves the running efficiency.
第二步,为对象文件中的对象及函数分别建立对象映射表和函数映射表。The second step is to establish an object mapping table and a function mapping table for the objects and functions in the object file.
在本步骤中,按照对象及函数的区别分别建立对象映射表(obj_map)及函数映射表(func_map),所建立的映射表供后续重定位及外部调用使用。In this step, the object mapping table (obj_map) and the function mapping table (func_map) are respectively established according to the difference between the object and the function, and the created mapping table is used for subsequent relocation and external calling.
其中对象映射表的映射表项包括对象名称和对象地址,该映射表中的对象地址是通过查找对象文件的有效段中的导出对象,根据导出对象所在地址而得到的;函数映射表的映射表项包括函数名称和函数地址,该函数地址是通过查找对象文件的有效段中的导出函数,根据导出函数所在的地址而得到的。The mapping table entry of the object mapping table includes an object name and an object address, and the object address in the mapping table is obtained by searching an export object in a valid segment of the object file, according to an address of the export object; a mapping table of the function mapping table The item includes the function name and the function address, which is obtained by looking up the export function in the valid section of the object file, according to the address where the function is derived.
关于对象和函数,举个例子:其中.text段中包含的大部分为函数,而.text段、.data段、.bss段、.rodata段中文件变量、函数变量等全局变量、函数元素等都可以以对象表示。由于对象和函数的划分为现有技术,在此不再赘述。 For objects and functions, for example, most of the .text segments are functions, and .text segments, .data segments, .bss segments, .rodata segments, file variables, function variables, and other global variables, function elements, etc. Can be represented by an object. Since the division of objects and functions is prior art, it will not be described here.
第三步,对加载到的有效段中的对象及函数进行重定位。The third step is to relocate the objects and functions in the valid segment loaded.
在本步骤中,使用对象映射表对有效段中的对象进行重定位,或使用所述函数映射表对有效段中的函数进行重定位。In this step, the object in the valid segment is relocated using the object mapping table, or the function in the valid segment is relocated using the function mapping table.
具体地,在链接时,需要对所有目标文件的可重定位段进行重定位,建立符号引用规则,同时为对象,函数等分配地址。程序执行时,把代码加载到链接时指定的地址空间,以保证程序在执行过程中对对象,函数等符号的正确引用,使程序正常运行。Specifically, when linking, it is necessary to relocate relocatable segments of all target files, establish symbol reference rules, and assign addresses to objects, functions, and the like. When the program is executed, the code is loaded into the address space specified by the link to ensure that the program correctly references the objects, functions and other symbols during the execution process, so that the program runs normally.
基于对象文件中提取出的对象及函数类型的不同,可以采用两种不同的重定位方式。Two different relocation methods can be used based on the objects and function types extracted from the object file.
第一种方式,对对象(obj)进行重定位:The first way is to relocate the object (obj):
对象在.o文件中的使用方式是,直接将对象的真实地址写入文字池。在动态加载此对象时,利用当前对象在所述对象映射表中的地址查找所述对象的跳转对象在文字池中的地址,并将文字池中所述跳转对象的地址修改为当前对象的地址。The object is used in the .o file by writing the object's real address directly into the literal pool. When the object is dynamically loaded, the address of the object in the object pool is searched for by the address of the current object in the object mapping table, and the address of the jump object in the text pool is modified to the current object. the address of.
具体地,编译器会把对象的地址都放到文字池(Literal Pool)区域。其中Literal Pool里面只存放绝对地址或常量。当需要进行重定位时,根据.rel.text、.rel.data、.rel.bss及.rel.rodata所包含的对象重定位信息,通过对象映射表中所记录的对象的当前地址获取文字池中所记录的跳转对象的绝对地址,从Literal Pool中查找绝对地址,在动态加载时,将对象所在的文字池中的绝对地址修改为当前对象所在的地址,从而对.text、.data、.bss及.rodata段包含的对象做重定位。Specifically, the compiler puts the address of the object into the Literal Pool area. The Literal Pool only stores absolute addresses or constants. When relocation is required, the text pool is obtained from the current address of the object recorded in the object mapping table according to the object relocation information contained in .rel.text, .rel.data, .rel.bss, and .rel.rodata. The absolute address of the jump object recorded in the file, find the absolute address from the Literal Pool, in the dynamic loading, the absolute address in the text pool where the object is located is modified to the address where the current object is located, thus .text, .data, The objects contained in the .bss and .rodata sections are relocated.
第二种方式,对函数(func)进行重定位:The second way is to relocate the function (func):
对于函数的重定位,可以利用当前函数运行时的地址以及从函数映 射表中查找到的需要跳转到的函数地址,经过计算得到跳转指令,利用计算得到的跳转指令对之前的跳转指令进行更新,以便于根据所述更新的跳转指令跳转到即将要执行的函数地址。For function relocation, you can use the address of the current function runtime and the function map The function address found in the shot table needs to jump to, the jump instruction is obtained through calculation, and the jump instruction obtained by the calculation is used to update the previous jump instruction, so as to jump to the jump instruction according to the update The address of the function to be executed.
具体地,函数调用跳转采用相应的跳转指令,例如B/BL等,对函数进行重新定位时,基于.rel.text等所包含的函数重定位信息,从函数映射表中查找需要跳转到的函数的真实地址,根据函数运行时所在的当前地址结合从函数映射表中查找到的需要跳转到的函数真实地址,经过一系列计算得到跳转指令,获取正确的跳转指令对之前的跳转指令进行更新,从而根据更新后的跳转指令加载重定位的函数。Specifically, the function call jump uses a corresponding jump instruction, such as B/BL, etc., when the function is relocated, the function is relocated based on the function included in .rel.text, etc., and the function mapping table needs to jump. The real address of the function, according to the current address where the function is running, combined with the real address of the function that needs to be jumped from the function mapping table, after a series of calculations, the jump instruction is obtained, and the correct jump instruction is obtained. The jump instruction is updated to load the relocated function according to the updated jump instruction.
当然,并非只能依据对象映射表和函数映射表来查找地址,由于动态加载时对象可能引用的是外部对象或者函数使用的是动态库函数,因此当在对象或函数映射表中无法找到相应的地址信息时,则表明对象或函数引用的是外部对象或者动态库函数,此时可以去动态的查找对象或函数的地址以便重定位,从而实现动态加载。Of course, it is not only based on the object mapping table and the function mapping table to find the address. Since the object may refer to an external object or the function uses a dynamic library function when dynamically loaded, the corresponding information cannot be found in the object or function mapping table. When the address information indicates that the object or function refers to an external object or a dynamic library function, it is possible to dynamically find the address of the object or function for relocation, thereby implementing dynamic loading.
请参考图3,图3为本发明提供的一种加载ELF文件的装置实施例的结构示意图。如图3所示,该装置包括:解析装置301,提取装置302和加载装置303,以下对各装置进行详细描述:Please refer to FIG. 3. FIG. 3 is a schematic structural diagram of an apparatus for loading an ELF file according to the present invention. As shown in FIG. 3, the device includes: a parsing device 301, an extracting device 302, and a loading device 303. The following describes each device in detail:
解析装置301,用于对ELF文件中的静态库(.a)文件进行解析以获取对象文件(.o)。The parsing device 301 is configured to parse a static library (.a) file in the ELF file to obtain an object file (.o).
本发明的对象文件(.o)是以ELF格式保存的可重定向文件,对象文件中的内容可以包含对各个函数的入口标记,描述,以形成机器可执行的指令。当程序要执行时还需要进行链接(link),链接就是把多个.o文件链成一个可执行文件。多个.o对象文件可以归档(archive)成.a静态 库文件,而静态库文件行成了ELF文件。The object file (.o) of the present invention is a re-directable file saved in ELF format, and the contents of the object file may contain entry marks and descriptions of the respective functions to form machine-executable instructions. When the program is to be executed, a link is also required. The link is to link multiple .o files into one executable file. Multiple .o object files can be archived into .a static Library files, and static library file lines become ELF files.
其中解析装置解析静态库(.a)文件的目的是为了获取其中的对象文件(.o)。The purpose of the parsing device to parse the static library (.a) file is to obtain the object file (.o) therein.
具体地,解析静态库文件的过程与windows操作系统中一个压缩包rar的解压缩(还原)过程类似,也就是分析由对象文件(.o)打包生成的静态库(.a),解析其中每个对象文件(.o)。Specifically, the process of parsing the static library file is similar to the decompression (restore) process of a compressed package rar in the Windows operating system, that is, analyzing the static library (.a) generated by the object file (.o) package, and parsing each of them. Object files (.o).
提取装置302,用于从获取的对象文件中提取有效段(section)。The extracting device 302 is configured to extract a valid section from the acquired object file.
当获取对象文件(.o)后,可以针对每一个对象文件,从中提取有效段并且记录段信息。After the object file (.o) is acquired, a valid segment can be extracted from each object file and segment information can be recorded.
有效段(sections)在ELF文件里面,用以装载内容数据的最小容器。在ELF文件里,每一个sections内都装载了性质属性相同的内容。,The valid sections are in the ELF file and are used to load the smallest container of content data. In the ELF file, each section is loaded with the same properties. ,
优选地,可以针对每个获取的对象文件,根据所述对象文件的ELF文件头(ELF Header)和段文件头表(SHT,Section Header Table)来提取有效段并记录各段信息。Preferably, for each acquired object file, a valid segment is extracted according to an ELF header and a SHT (Section Header Table) of the object file, and each piece of information is recorded.
该有效段的具体类型可以包括:可执行代码段,应用数据段,或重定位段。其中该可执行代码段又可以具体包括.text段等,该应用数据段可以包括.data段、.bss段、或.rodata段等,该重定位段可以包括.rel.data段、.rel.bss段、或rel.rodata段等。The specific type of the valid segment may include: an executable code segment, an application data segment, or a relocation segment. The executable code segment may specifically include a .text segment, etc., and the application data segment may include a .data segment, a .bss segment, or a .rodata segment, and the relocation segment may include a .rel.data segment and a .rel. Bss segment, or rel.rodata segment, etc.
该记录的有效段信息包括但不限于:各段序号、段偏移量、或段大小等。The valid segment information of the record includes but is not limited to: each segment number, segment offset, or segment size, and the like.
其中among them
1).text段里装载了可执行代码;1) The executable file is loaded in the .text section;
2).data段里装载了被初始化的数据; 2) The .data section is loaded with the initialized data;
3).bss段里装载了未被初始化的数据;3) The .bss section is loaded with uninitialized data;
4).rodata段里装载了相应字符串和定义的常量4) The .rodata section is loaded with the corresponding string and the defined constant
5)以.rel打头的重定位段rel.text段、.rel.data段、.rel.bss段、.rel.rodata段里面装载了重定位条目,该重定位段是用于帮助可执行代码段和应用数据段进行重定位的段;5) The relocation segment starting with .rel, the rel.text segment, the .rel.data segment, the .rel.bss segment, and the .rel.rodata segment, are loaded with relocation entries, which are used to help the executable code. Segments and segments in which the application data segment is relocated;
例如:.rel.text中示出了.text段中哪些地方需要做重定位。For example: .rel.text shows where in the .text section you need to do relocation.
从对象文件的ELF Header和SHT中提取有效段及各段信息的具体方式可以为:The specific way to extract valid segments and pieces of information from the ELF Header and SHT of the object file can be:
从ELF Header中得知相应的sections及序号,从SHT的内容中,根据SHT中的字段Off(sh_offset)、Size(sh_size)来获取段偏移量、段大小等信息。The corresponding sections and serial numbers are known from the ELF Header. From the contents of the SHT, information such as the segment offset and the segment size are obtained according to the fields Off(sh_offset) and Size(sh_size) in the SHT.
装置301和302用于在动态加载ELF文件之前对预加载的ELF文件执行加载前的准备,其准备过程可以同时或者分先后执行,本发明对其执行的顺序不做硬性规定,当上述准备过程执行后,则通过装置303进一步实现对对象文件的有效段的加载。The devices 301 and 302 are configured to perform pre-loading preparation on the pre-loaded ELF file before the ELF file is dynamically loaded, and the preparation process may be performed simultaneously or sequentially, and the present invention does not strictly define the order of execution thereof. After execution, the loading of the valid segment of the object file is further implemented by the device 303.
加载装置303,用于加载有效段,为所述对象文件中的对象及函数分别建立对象映射表和函数映射表,并且用于对加载到的有效段中的对象及函数进行重定位。The loading device 303 is configured to load a valid segment, establish an object mapping table and a function mapping table for the objects and functions in the object file, and re-locate the objects and functions in the valid segment loaded.
还可以将加载装置303进一步划分为有效段加载单元3031、映射表建立单元3032以及重定位单元3033三个单元,其中前两个单元可以同时或者分先后执行相应操作,本发明对其执行的顺序不做硬性规定,只要能完成相应的功能,则均在本发明保护范围之内。The loading device 303 can be further divided into three units: a valid segment loading unit 3031, a mapping table establishing unit 3032, and a relocating unit 3033. The first two units can perform corresponding operations simultaneously or in sequence, and the order in which the present invention is executed. Without stipulations, as long as the corresponding functions can be completed, they are all within the scope of the present invention.
以下对三个单元进行详细描述: The three units are described in detail below:
加载有效段单元3031。The valid segment unit 3031 is loaded.
对于由提取单元302所提取的有效段,包括可执行代码段、应用数据段以及重定位段三种类型,其中可执行代码段和应用数据段属于需要加载的有效段,而重定位段不需要加载,针对该需要加载的有效段,可以依据其类型的不同可以对应的采用两种方式将其加载到不同的区域。For the valid segment extracted by the extracting unit 302, there are three types of executable code segments, application data segments, and relocation segments, wherein the executable code segment and the application data segment belong to a valid segment that needs to be loaded, and the relocation segment does not need to be Loading, for the valid segment to be loaded, it can be loaded into different regions in two ways according to its type.
第一种方式,对于可执行代码段(.text段)加载:The first way is to load the executable code segment (.text segment):
由于针对于不同的产品其flash的读写不一致,因此为了便于重定位修改.text段内容,可以先对.text段进行重定位,然后再将其写入Flash。Since the flash reading and writing are inconsistent for different products, in order to facilitate relocation and modify the content of the .text segment, the .text segment can be relocated first, and then written to Flash.
由于提取的与.text段相应的重定位段(.rel.text段)中包含.text段需要重定位的信息,因此在写入flash之前的重定位方式可以为:将识别出的.rel.text段中记录的.text段需要重定位的部分记录下来创建重定位信息,对.text做重定位,然后再将.text段拷贝并写入到Flash中。Since the extracted relocation segment (.rel.text segment) corresponding to the .text segment contains the information that the .text segment needs to be relocated, the relocation method before writing to the flash may be: the identified .rel. The .text segment recorded in the text segment needs to be relocated to create relocation information, relocate .text, and then copy and write the .text segment to Flash.
第二种方式,对于应用数据段(.data、.bss及.rodata)的加载:The second way is to load the application data segments (.data, .bss, and .rodata):
对于.data、.bss及.rodata的加载方式与对.text段的加载方式略有不同,相比于对.text段的加载在写入到Flash之前还包括对.text段的重定位过程而言,对.data、.bss及.rodata的加载在将各段加载到随机存取存储器(Random Access Memory,RAM)之前并不需要进行重定位,而是依照.data、.bss及.rodata及相应重定位段(.rel.data、.rel.bss及.rel.rodata)的有效段信息,将.data、.bss及.rodata段直接加载到RAM。The loading methods for .data, .bss, and .rodata are slightly different from the loading method for .text segments. Compared to the loading of .text segments, the relocation process for .text segments is included before writing to Flash. In other words, the loading of .data, .bss, and .rodata does not require relocation before loading the segments into random access memory (RAM), but in accordance with .data, .bss, and .rodata. The valid segment information of the corresponding relocation segments (.rel.data, .rel.bss, and .rel.rodata) loads the .data, .bss, and .rodata segments directly into RAM.
在通过第一种和第二种方式加载有效段后,可以对各个对象文件(.o)的各个段做好相应的标记,该标记可以包括但不限于段的起始地址、段偏移量、段大小等信息。After the valid segment is loaded by the first and second methods, each segment of each object file (.o) can be marked accordingly, and the tag can include but is not limited to the segment start address and the segment offset. , segment size and other information.
其中段的起始地址是根据对加载后的有效段初始化获得的,段偏移 量、段大小是在提取有效段时基于该提取的有效段获得的。The starting address of the segment is obtained based on the initialization of the valid segment after loading. The quantity and segment size are obtained based on the extracted valid segment when the valid segment is extracted.
本实施例通过加载有效段单元的操作,将装载了可执行代码的.text段与其他的应用数据段的应用数据分离,并通过对可执行代码的目标文件创建重定位信息并载入内存,从而生成内存映射过程,避免了在加载ELF文件时多次拷贝代码数据的过程,提高了运行效率。In this embodiment, by loading an effective segment unit, the .text segment loaded with the executable code is separated from the application data of other application data segments, and the relocation information is created and loaded into the memory by the target file of the executable code. Thereby generating a memory mapping process, avoiding the process of copying the code data multiple times when loading the ELF file, and improving the running efficiency.
映射表建立单元3032,用于为对象文件中的对象及函数分别建立对象映射表和函数映射表。The mapping table establishing unit 3032 is configured to separately establish an object mapping table and a function mapping table for objects and functions in the object file.
本单元用于按照对象及函数的区别分别建立对象映射表(obj_map)及函数映射表(func_map),所建立的映射表供后续重定位及外部调用使用。This unit is used to establish an object mapping table (obj_map) and a function mapping table (func_map) according to the difference between the object and the function. The created mapping table is used for subsequent relocation and external calling.
其中对象映射表的映射表项包括对象名称和对象地址,该映射表中的对象地址是通过查找对象文件的有效段中的导出对象,根据导出对象所在地址而得到的;函数映射表的映射表项包括函数名称和函数地址,该函数地址是通过查找对象文件的有效段中的导出函数,根据导出函数所在的地址而得到的。The mapping table entry of the object mapping table includes an object name and an object address, and the object address in the mapping table is obtained by searching an export object in a valid segment of the object file, according to an address of the export object; a mapping table of the function mapping table The item includes the function name and the function address, which is obtained by looking up the export function in the valid section of the object file, according to the address where the function is derived.
关于对象和函数,举个例子:其中.text段中包含的大部分为函数,而.text段、.data段、.bss段、.rodata段中文件变量、函数变量等全局变量、函数元素等都可以以对象表示。由于对象和函数的划分为现有技术,在此不再赘述。For objects and functions, for example, most of the .text segments are functions, and .text segments, .data segments, .bss segments, .rodata segments, file variables, function variables, and other global variables, function elements, etc. Can be represented by an object. Since the division of objects and functions is prior art, it will not be described here.
重定位单元3033,用于对加载到的有效段中的对象及函数进行重定位。The relocating unit 3033 is configured to relocate the objects and functions in the valid segment loaded.
本单元用于使用对象映射表对有效段中的对象进行重定位,或使用所述函数映射表对有效段中的函数进行重定位。 This unit is used to relocate an object in a valid segment using an object mapping table, or to relocate a function in a valid segment using the function mapping table.
具体地,在链接时,需要对所有目标文件的可重定位段进行重定位,建立符号引用规则,同时为对象,函数等分配地址。程序执行时,把代码加载到链接时指定的地址空间,以保证程序在执行过程中对对象,函数等符号的正确引用,使程序正常运行。Specifically, when linking, it is necessary to relocate relocatable segments of all target files, establish symbol reference rules, and assign addresses to objects, functions, and the like. When the program is executed, the code is loaded into the address space specified by the link to ensure that the program correctly references the objects, functions and other symbols during the execution process, so that the program runs normally.
基于对象文件中提取出的对象及函数类型的不同,可以采用两种不同的重定位方式。Two different relocation methods can be used based on the objects and function types extracted from the object file.
第一种方式,对对象(obj)进行重定位:The first way is to relocate the object (obj):
对象在.o文件中的使用方式是,直接将对象的真实地址写入文字池。在动态加载此对象时,利用当前对象在所述对象映射表中的地址查找所述对象的跳转对象在文字池中的地址,并将文字池中所述跳转对象的地址修改为当前对象的地址。The object is used in the .o file by writing the object's real address directly into the literal pool. When the object is dynamically loaded, the address of the object in the object pool is searched for by the address of the current object in the object mapping table, and the address of the jump object in the text pool is modified to the current object. the address of.
第二种方式,对函数(func)进行重定位:The second way is to relocate the function (func):
对于函数的重定位,可以利用当前函数运行时的地址以及从函数映射表中查找到的需要跳转到的函数地址,经过计算得到跳转指令,利用计算得到的跳转指令对之前的跳转指令进行更新,以便于根据所述更新的跳转指令跳转到即将要执行的函数地址。For the relocation of the function, you can use the address of the current function runtime and the function address that needs to be jumped from the function mapping table. After the calculation, the jump instruction is obtained, and the jump instruction obtained by the calculation is used to jump to the previous jump. The instruction is updated to jump to the address of the function to be executed in accordance with the updated jump instruction.
当然,并非只能依据对象映射表和函数映射表来查找地址,由于动态加载时对象可能引用的是外部对象或者函数使用的是动态库函数,因此当在对象或函数映射表中无法找到相应的地址信息时,则表明对象或函数引用的是外部对象或者动态库函数,此时可以查找对象或函数的地址以便重定位,从而实现动态加载。Of course, it is not only based on the object mapping table and the function mapping table to find the address. Since the object may refer to an external object or the function uses a dynamic library function when dynamically loaded, the corresponding information cannot be found in the object or function mapping table. When the address information indicates that the object or function refers to an external object or a dynamic library function, the address of the object or function can be searched for relocation, thereby implementing dynamic loading.
本发明的ELF文件动态记载的方式和装置,可以应用于机顶盒、智能可穿戴式设备、智能家居云等多种场景中。以机顶盒为例,当用户使 用机顶盒启动或者安装ELF文件时,可以使用本发明所提供的方法和装置,通过对静态库文件(.a)进行解析并进行加载和重定位,从而实现ELF文件的动态加载。The method and device for dynamically recording the ELF file of the present invention can be applied to various scenarios such as a set top box, a smart wearable device, and a smart home cloud. Take a set-top box as an example, when the user makes When the ELF file is started or installed by the set top box, the method and apparatus provided by the present invention can be used to implement dynamic loading of the ELF file by parsing and loading and relocating the static library file (.a).
本发明提供的方法和装置可以具备下优点:The method and apparatus provided by the present invention can have the following advantages:
1)本发明采用了解析静态库(.a)文件并且提取有效段,对有效段进行加载,建立对象及函数映射表用于对可重定位段中的对象和函数分别进行重定位的方式,这些处理方式并不受限于特定指令集,因此解决了现有技术在一些嵌入式系统中无法加载ELF文件的问题。1) The present invention adopts a method of parsing a static library (.a) file and extracting a valid segment, loading a valid segment, and establishing an object and a function mapping table for relocating objects and functions in the relocatable segment respectively. These processing methods are not limited to a specific instruction set, thus solving the problem that the prior art cannot load ELF files in some embedded systems.
举一个例子,本发明尤其适用于支持采用Thumb-2指令集的Cortex-M系列处理器嵌入式系统。由于嵌入式Mbed OS是基于ARM Cortex-M处理器所设计的操作系统,Mbed os作为专门为嵌入式开发定制的系统具有独特的优势。目前Cortex-M系列处理器已经能够支持Mbed os,但目前Mbed os还无法提供适用于Cortex-M处理器的ELF文件加载器(Loader),因此限制了Mbed os在Cortex-M系列处理器上的使用。这是因为现有的嵌入式操作系统对于ELF文件的加载采用静态链接的方式生成可执行文件,并将可执行文件烧录至Flash,这些操作都必须在ARM指令集下完成,但是由于Cortex-M系列处理器指令集采用的是Thumb-2指令集,该指令集不支持切换到ARM指令集的状态,因此现有技术无法为此类处理器提供动态解析加载ELF文件的加载器。As an example, the present invention is particularly useful for supporting Cortex-M series processor embedded systems employing the Thumb-2 instruction set. Because the embedded Mbed OS is an operating system based on the ARM Cortex-M processor, Mbed os has a unique advantage as a system specifically tailored for embedded development. Currently, the Cortex-M series processors are already capable of supporting Mbed os, but currently Mbed os cannot provide ELF file loaders for the Cortex-M processors, thus limiting the Mbed os on the Cortex-M series processors. use. This is because the existing embedded operating system generates a executable file by static linking for the loading of ELF files, and burns the executable file to Flash. These operations must be completed under the ARM instruction set, but due to Cortex- The M-series processor instruction set uses the Thumb-2 instruction set, which does not support switching to the state of the ARM instruction set, so the prior art cannot provide a loader that dynamically parses the loaded ELF file for such processors.
而本发明提供的方法和装置转换了一种思路,不是通过静态链接的方式生成可执行文件并直接载入内存的方式,也不需要通过特定的ARM指令集来解析和加载ELF文件。而是对ELF文件的静态库文件进行解析 以获取ELF可重定位的对象文件的有效段并进行重定位,以便使ELF文件中的对象及函数能够以可重定位的方式被动态加载。本发明的这些处理过程并不受限于特定的ARM指令集,因此尤其适用于支持采用Thumb-2指令集的Cortex-M系列处理器嵌入式系统。The method and apparatus provided by the present invention converts an idea not to generate an executable file by static linking and directly load the memory, nor to parse and load the ELF file through a specific ARM instruction set. Instead, parse the static library file of the ELF file. To obtain a valid segment of the ELF relocatable object file and relocate it so that objects and functions in the ELF file can be dynamically loaded in a relocatable manner. The processes of the present invention are not limited to a particular ARM instruction set and are therefore particularly well-suited for supporting Cortex-M series processor embedded systems employing the Thumb-2 instruction set.
2)现有技术中采用静态链接的方式生成可执行文件,并将可执行文件烧录至Flash,因此其运行的应用数量受限于Flash中烧录的可执行文件数量。而本发明采用动态加载方式,基于此能够实现动态链接,运行的应用数量不再受限于Flash中烧录可执行文件数量。2) In the prior art, an executable file is generated by static linking, and the executable file is burned to Flash, so the number of applications running is limited by the number of executable files burned in Flash. The present invention adopts a dynamic loading mode, based on which dynamic linking can be implemented, and the number of running applications is no longer limited by the number of executable files in the Flash.
3)静态链接的方式需要生成可执行文件并载入Flash,这就造成了文件体积较大,功耗较大。本申请的动态加载方式相比较现有技术大大较小了文件体积和功耗。3) The way of static linking requires generating an executable file and loading it into Flash, which results in a larger file size and higher power consumption. The dynamic loading method of the present application greatly reduces file size and power consumption compared to the prior art.
在本发明所提供的几个实施例中,应该理解到,所揭露的系统,装置和方法,可以通过其它的方式实现。例如,以上所描述的装置实施例仅仅是示意性的,例如,所述单元的划分,仅仅为一种逻辑功能划分,实际实现时可以有另外的划分方式。In the several embodiments provided by the present invention, it should be understood that the disclosed system, apparatus, and method may be implemented in other manners. For example, the device embodiments described above are merely illustrative. For example, the division of the unit is only a logical function division, and the actual implementation may have another division manner.
所述作为分离部件说明的单元可以是或者也可以不是物理上分开的,作为单元显示的部件可以是或者也可以不是物理单元,即可以位于一个地方,或者也可以分布到多个网络单元上。可以根据实际的需要选择其中的部分或者全部单元来实现本实施例方案的目的。The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, may be located in one place, or may be distributed to multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of the embodiment.
另外,在本发明各个实施例中的各功能单元可以集成在一个处理单元中,也可以是各个单元单独物理存在,也可以两个或两个以上单元集成在一个单元中。上述集成的单元既可以采用硬件的形式实现,也可以采用硬件加软件功能单元的形式实现。 In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above integrated unit can be implemented in the form of hardware or in the form of hardware plus software functional units.
以上所述仅为本发明的较佳实施例而已,并不用以限制本发明,凡在本发明的精神和原则之内,所做的任何修改、等同替换、改进等,均应包含在本发明保护的范围之内。 The above are only the preferred embodiments of the present invention, and are not intended to limit the present invention. Any modifications, equivalents, improvements, etc., which are made within the spirit and principles of the present invention, should be included in the present invention. Within the scope of protection.

Claims (24)

  1. 一种加载具有可执行和链接格式ELF文件的方法,其特征在于,所述方法包括:A method of loading an ELF file having an executable and link format, the method comprising:
    对所述ELF文件中的静态库文件进行解析以获取对象文件;Parsing a static library file in the ELF file to obtain an object file;
    从所述对象文件中提取有效段;Extracting a valid segment from the object file;
    加载有效段,并为所述对象文件中的对象及函数分别建立对象映射表和函数映射表,用于对加载的有效段中的对象及函数进行重定位。Loading a valid segment, and establishing an object mapping table and a function mapping table for the objects and functions in the object file, respectively, for relocating the objects and functions in the loaded valid segment.
  2. 根据权利要求1所述的方法,其特征在于,提取的有效段包括:可执行代码段、应用数据段或重定位段。The method of claim 1 wherein the extracted valid segment comprises: an executable code segment, an application data segment, or a relocation segment.
  3. 根据权利要求1或2所述的方法,其特征在于,根据所述对象文件的ELF文件头和段文件头表来提取有效段。The method according to claim 1 or 2, wherein the valid segment is extracted according to the ELF file header and the segment file header table of the object file.
  4. 根据权利要求1或2所述的方法,其特征在于,所述加载有效段包括:将不同类型的有效段分别加载到不同的区域。The method according to claim 1 or 2, wherein the loading the valid segment comprises: loading different types of valid segments into different regions respectively.
  5. 根据权利要求4所述的方法,其特征在于,所述将不同类型的有效段分别加载到不同的区域包括:The method according to claim 4, wherein the loading different types of valid segments into different regions separately comprises:
    依照可执行代码段及相应重定位段的段信息,对可执行代码段进行重定位,将重定位后的可执行代码段写入Flash;或者Relocating the executable code segment according to the executable code segment and the segment information of the corresponding relocation segment, and writing the re-executed executable code segment to Flash; or
    依照应用数据段及相应重定位段的段信息,将应用数据段直接加载到RAM。The application data segment is directly loaded into the RAM according to the application data segment and the segment information of the corresponding relocation segment.
  6. 根据权利要求5所述的方法,其特征在于,所述段信息包括段序号、段偏移量或段大小。The method according to claim 5, wherein the segment information comprises a segment number, a segment offset or a segment size.
  7. 根据权利要求5所述的方法,其特征在于,所述可执行代码段包括.text段,所述应用数据段包括.data段、.bss段、或.rodata段,所述重 定位段包括.rel.data段、.rel.bss段、或.rel.rodata段。The method according to claim 5, wherein said executable code segment comprises a .text segment, and said application data segment comprises a .data segment, a .bss segment, or a .rodata segment, said weight The positioning segment includes a .rel.data segment, a .rel.bss segment, or a .rel.rodata segment.
  8. 根据权利要求1或2所述的方法,其特征在于,在所述加载有效段之后,对所述对象文件的有效段进行以下至少一个的标记:段的起始地址、段偏移量、段大小。The method according to claim 1 or 2, wherein after the loading of the valid segment, the valid segment of the object file is marked with at least one of the following: a starting address of the segment, a segment offset, a segment size.
  9. 根据权利要求1所述的方法,其特征在于:The method of claim 1 wherein:
    所述对象映射表的映射表项包括对象名称和对象地址,所述对象地址是通过查找有效段中的导出对象,根据导出对象所在地址而得到的;The mapping table entry of the object mapping table includes an object name and an object address, and the object address is obtained by searching for an export object in the valid segment according to the address of the export object;
    所述函数映射表的映射表项包括函数名称和函数地址,所述函数地址是通过查找有效段中的导出函数,根据导出函数所在的地址而得到的。The mapping table entry of the function mapping table includes a function name and a function address, and the function address is obtained by searching for an export function in the valid segment according to an address where the derived function is located.
  10. 根据权利要求1所述的方法,其特征在于,在对有效段中的对象进行重定位时,利用当前对象在所述对象映射表中的地址查找所述对象的跳转对象在文字池中的地址,并将文字池中所述跳转对象的地址修改为当前对象的地址。The method according to claim 1, wherein when the object in the valid segment is relocated, the address of the current object in the object mapping table is used to find the jump object of the object in the text pool. Address and modify the address of the jump object in the literal pool to the address of the current object.
  11. 根据权利要求1所述的方法,其特征在于,在对有效段中的函数进行重定位时,利用当前函数运行时的地址以及从函数映射表中查找到的需要跳转到的函数地址,经过计算得到跳转指令,利用计算得到的跳转指令对之前的跳转指令进行更新,以便于根据所述更新的跳转指令跳转到即将要执行的函数地址。The method according to claim 1, wherein when the function in the valid segment is relocated, the address of the current function runtime and the address of the function to be jumped from the function mapping table are used. The jump instruction is calculated, and the jump instruction obtained by the calculation is used to update the previous jump instruction, so as to jump to the function address to be executed according to the updated jump instruction.
  12. 一种加载ELF文件的装置,其特征在于,所述装置包括:An apparatus for loading an ELF file, the apparatus comprising:
    解析装置,用于对所述ELF文件中的静态库文件进行解析以获取对象文件;a parsing device, configured to parse the static library file in the ELF file to obtain an object file;
    提取装置,用于从所述对象文件中提取有效段;An extracting device, configured to extract a valid segment from the object file;
    加载装置,用于加载有效段,为所述对象文件中的对象及函数分别 建立对象映射表和函数映射表,并且用于对加载的有效段中的对象及函数进行重定位。a loading device for loading a valid segment, respectively for objects and functions in the object file An object mapping table and a function mapping table are created and used to relocate objects and functions in the loaded valid segment.
  13. 根据权利要求12所述的加载ELF文件的装置,其特征在于,所述提取装置中提取的有效段包括:可执行代码段、应用数据段或重定位段。The apparatus for loading an ELF file according to claim 12, wherein the valid segment extracted in the extracting device comprises: an executable code segment, an application data segment or a relocation segment.
  14. 根据权利要求12或13所述的加载ELF文件的装置,其特征在于,所述提取装置用于根据所述对象文件的ELF文件头和段文件头表来提取有效段。The apparatus for loading an ELF file according to claim 12 or 13, wherein said extracting means is configured to extract a valid segment based on an ELF file header and a segment header table of said object file.
  15. 根据权利要求12或13所述的加载ELF文件的装置,其特征在于,所述加载装置加载有效段包括:将不同类型的有效段分别加载到不同的区域。The apparatus for loading an ELF file according to claim 12 or 13, wherein the loading device loading the valid segment comprises: loading different types of valid segments into different regions respectively.
  16. 根据权利要求15所述的加载ELF文件的装置,其特征在于,所述加载装置在将不同类型的有效段分别加载到不同的区域时,具体执行:The apparatus for loading an ELF file according to claim 15, wherein the loading device performs: when loading different types of valid segments into different regions, respectively:
    依照可执行代码段及相应重定位段的段信息,对可执行代码段进行重定位,将重定位后的可执行代码段写入Flash;或者Relocating the executable code segment according to the executable code segment and the segment information of the corresponding relocation segment, and writing the re-executed executable code segment to Flash; or
    依照应用数据段及相应重定位段的段信息,将应用数据段直接加载到RAM。The application data segment is directly loaded into the RAM according to the application data segment and the segment information of the corresponding relocation segment.
  17. 根据权利要求16所述的加载ELF文件的装置,其特征在于,所述段信息包括段序号、段偏移量或段大小。The apparatus for loading an ELF file according to claim 16, wherein the segment information comprises a segment number, a segment offset or a segment size.
  18. 根据权利要求16所述的加载ELF文件的装置,其特征在于,所述可执行代码段包括.text段,所述应用数据段包括.data段、.bss段、或.rodata段,所述重定位段包括.rel.data段、.rel.bss段、或.rel.rodata段。The apparatus for loading an ELF file according to claim 16, wherein the executable code segment comprises a .text segment, and the application data segment comprises a .data segment, a .bss segment, or a .rodata segment, the weight The positioning segment includes a .rel.data segment, a .rel.bss segment, or a .rel.rodata segment.
  19. 根据权利要求12或13所述的加载ELF文件的装置,其特征在于, 所述加载装置在执行所述加载有效段之后,进一步用于对所述对象文件的有效段进行以下至少一个的标记:段的起始地址、段偏移量、段大小。The apparatus for loading an ELF file according to claim 12 or 13, wherein After performing the loading valid segment, the loading device is further configured to perform at least one of the following markings on the valid segment of the object file: a starting address of the segment, a segment offset, and a segment size.
  20. 根据权利要求12所述的加载ELF文件的装置,其特征在于:The apparatus for loading an ELF file according to claim 12, wherein:
    所述对象映射表的映射表项包括对象名称和对象地址,所述对象地址是通过查找有效段中的导出对象,根据导出对象所在地址而得到的;The mapping table entry of the object mapping table includes an object name and an object address, and the object address is obtained by searching for an export object in the valid segment according to the address of the export object;
    所述函数映射表的映射表项包括函数名称和函数地址,所述函数地址是通过查找有效段中的导出函数,根据导出函数所在的地址而得到的。The mapping table entry of the function mapping table includes a function name and a function address, and the function address is obtained by searching for an export function in the valid segment according to an address where the derived function is located.
  21. 根据权利要求12所述的加载ELF文件的装置,其特征在于,所述加载装置在对有效段中的对象进行重定位时,具体执行:The apparatus for loading an ELF file according to claim 12, wherein the loading device performs: when relocating an object in the valid segment, specifically:
    利用当前对象在所述对象映射表中的地址查找所述对象的跳转对象在文字池中的地址,并将文字池中所述跳转对象的地址修改为当前对象的地址。The address of the jump object of the object in the text pool is searched for by the address of the current object in the object mapping table, and the address of the jump object in the text pool is modified to the address of the current object.
  22. 根据权利要求12所述的加载ELF文件的装置,其特征在于,所述加载装置在对有效段中的函数进行重定位时,具体执行:利用当前函数运行时的地址以及从函数映射表中查找到的需要跳转到的函数地址,经过计算得到跳转指令,利用计算得到的跳转指令对之前的跳转指令进行更新,以便于根据所述更新的跳转指令跳转到即将要执行的函数地址。The apparatus for loading an ELF file according to claim 12, wherein the loading device performs: when relocating a function in the valid segment, using the address of the current function runtime and searching from the function mapping table. The address of the function to be jumped to, the jump instruction is calculated, and the jump instruction obtained by the calculation is used to update the previous jump instruction, so as to jump to the upcoming execution according to the updated jump instruction. Function address.
  23. 一种设备,包括a device, including
    一个或者多个处理器;One or more processors;
    存储器;Memory
    一个或者多个程序,所述一个或者多个程序存储在所述存储器中,当被所述一个或者多个处理器执行时:One or more programs, the one or more programs being stored in the memory, when executed by the one or more processors:
    对所述ELF文件中的静态库文件进行解析以获取对象文件; Parsing a static library file in the ELF file to obtain an object file;
    从所述对象文件中提取有效段;Extracting a valid segment from the object file;
    加载有效段,并为所述对象文件中的对象及函数分别建立对象映射表和函数映射表,用于对加载的有效段中的对象及函数进行重定位。Loading a valid segment, and establishing an object mapping table and a function mapping table for the objects and functions in the object file, respectively, for relocating the objects and functions in the loaded valid segment.
  24. 一种计算机存储介质,所述计算机存储介质被编码有计算机程序,所述程序在被一个或多个计算机执行时,使得所述一个或多个计算机执行如下操作:A computer storage medium encoded with a computer program, when executed by one or more computers, causes the one or more computers to perform the following operations:
    对所述ELF文件中的静态库文件进行解析以获取对象文件;Parsing a static library file in the ELF file to obtain an object file;
    从所述对象文件中提取有效段;Extracting a valid segment from the object file;
    加载有效段,并为所述对象文件中的对象及函数分别建立对象映射表和函数映射表,用于对加载的有效段中的对象及函数进行重定位。 Loading a valid segment, and establishing an object mapping table and a function mapping table for the objects and functions in the object file, respectively, for relocating the objects and functions in the loaded valid segment.
PCT/CN2016/084304 2015-10-21 2016-06-01 Method, apparatus and device for loading elf file, and computer storage medium WO2017067175A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201510688388.3A CN105224370B (en) 2015-10-21 2015-10-21 A kind of method and apparatus of loading ELF document
CN201510688388.3 2015-10-21

Publications (1)

Publication Number Publication Date
WO2017067175A1 true WO2017067175A1 (en) 2017-04-27

Family

ID=54993362

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2016/084304 WO2017067175A1 (en) 2015-10-21 2016-06-01 Method, apparatus and device for loading elf file, and computer storage medium

Country Status (2)

Country Link
CN (1) CN105224370B (en)
WO (1) WO2017067175A1 (en)

Cited By (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111488557A (en) * 2019-01-28 2020-08-04 中国移动通信有限公司研究院 A kind of encryption and decryption method, device, electronic device and readable storage medium
CN111666102A (en) * 2020-05-20 2020-09-15 新华三半导体技术有限公司 File format conversion method, chip verification method, related device and network chip
CN113672238A (en) * 2021-09-07 2021-11-19 广东九联科技股份有限公司 Operation method, device and equipment based on real-time operating system modular management
CN114546515A (en) * 2022-01-20 2022-05-27 上海合宙通信科技有限公司 Module, firmware and equipment for dynamically loading static library and method for converting C library into Lua library
CN116909656A (en) * 2023-09-14 2023-10-20 北京中天星控科技开发有限公司 Method for realizing embedded dynamic APP loading based on elf file
CN117075960A (en) * 2023-10-17 2023-11-17 统信软件技术有限公司 Program reconstruction method, application cross-platform migration method, device and computing equipment

Families Citing this family (14)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105224370B (en) * 2015-10-21 2019-03-08 安一恒通(北京)科技有限公司 A kind of method and apparatus of loading ELF document
CN107315599B (en) * 2016-04-27 2020-09-01 龙芯中科技术有限公司 Method and device for extracting graphic library
CN107247579B (en) * 2016-08-19 2020-09-11 北京金山安全管理系统技术有限公司 Method and device for calculating ELF file mapping base address
CN106354536A (en) * 2016-08-31 2017-01-25 北京奇虎科技有限公司 Method and device of loading ELF file of Linux system in Windows system
CN106598674A (en) * 2016-12-16 2017-04-26 中南大学 Relocatable ELF file-based software quick loading method
CN107450960A (en) * 2017-09-18 2017-12-08 珠海亿智电子科技有限公司 A kind of method for linking and loading again to can perform ELF files
CN108874468B (en) * 2018-06-20 2021-03-26 深圳市腾讯网络信息技术有限公司 Application program loading method and device, computer equipment and storage medium
CN109324838B (en) * 2018-08-31 2022-05-10 深圳市元征科技股份有限公司 Execution method and execution device of single chip microcomputer program and terminal
CN110673850A (en) * 2019-08-28 2020-01-10 五八有限公司 Method and device for obtaining size of static library
CN113590138B (en) * 2020-04-30 2025-06-17 华为技术有限公司 Executable file processing method and device
CN111856989A (en) * 2020-06-19 2020-10-30 中电科航空电子有限公司 Dynamic linking method for single chip microcomputer
CN113010186B (en) * 2021-02-05 2023-03-21 武汉联特科技股份有限公司 Firmware and application data integration method and system applied to optical module
CN114385184B (en) * 2021-12-22 2025-03-21 苏州迈科网络安全技术股份有限公司 A compilation method, device, terminal and storage medium for Lua code embedding
CN115658275B (en) * 2022-11-21 2023-03-28 统信软件技术有限公司 Executable program migration method and device and computing equipment

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
EP0962859A2 (en) * 1998-05-29 1999-12-08 Sun Microsystems, Inc. Global register systems, methods, and computer program products
CN1892602A (en) * 2005-07-01 2007-01-10 中国科学院计算技术研究所 Method for processing library function call in binary translation
CN101470619A (en) * 2007-12-29 2009-07-01 安凯(广州)软件技术有限公司 Application program dynamic loading method based on microkernel operating system
CN101697131A (en) * 2009-11-04 2010-04-21 中兴通讯股份有限公司 Method and device for dynamically loading relocatable file
CN105224370A (en) * 2015-10-21 2016-01-06 安一恒通(北京)科技有限公司 A kind of method and apparatus of loading ELF document

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
EP0962859A2 (en) * 1998-05-29 1999-12-08 Sun Microsystems, Inc. Global register systems, methods, and computer program products
CN1892602A (en) * 2005-07-01 2007-01-10 中国科学院计算技术研究所 Method for processing library function call in binary translation
CN101470619A (en) * 2007-12-29 2009-07-01 安凯(广州)软件技术有限公司 Application program dynamic loading method based on microkernel operating system
CN101697131A (en) * 2009-11-04 2010-04-21 中兴通讯股份有限公司 Method and device for dynamically loading relocatable file
CN105224370A (en) * 2015-10-21 2016-01-06 安一恒通(北京)科技有限公司 A kind of method and apparatus of loading ELF document

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
EMMOBLIN: "Understanding of Executable and Linking Format", pages 1 - 7, Retrieved from the Internet <URL:blog.chinaunix.net/uid-20778443-id-99714.html> *

Cited By (11)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111488557A (en) * 2019-01-28 2020-08-04 中国移动通信有限公司研究院 A kind of encryption and decryption method, device, electronic device and readable storage medium
CN111488557B (en) * 2019-01-28 2024-01-12 中国移动通信有限公司研究院 Encryption and decryption method and device, electronic equipment and readable storage medium
CN111666102A (en) * 2020-05-20 2020-09-15 新华三半导体技术有限公司 File format conversion method, chip verification method, related device and network chip
CN113672238A (en) * 2021-09-07 2021-11-19 广东九联科技股份有限公司 Operation method, device and equipment based on real-time operating system modular management
CN113672238B (en) * 2021-09-07 2024-03-12 广东九联开鸿科技发展有限公司 Operation method, device and equipment based on real-time operating system modularized management
CN114546515A (en) * 2022-01-20 2022-05-27 上海合宙通信科技有限公司 Module, firmware and equipment for dynamically loading static library and method for converting C library into Lua library
CN114546515B (en) * 2022-01-20 2023-09-12 上海合宙通信科技有限公司 Module, firmware and equipment for dynamically loading static library and method for converting C library into Lua library
CN116909656A (en) * 2023-09-14 2023-10-20 北京中天星控科技开发有限公司 Method for realizing embedded dynamic APP loading based on elf file
CN116909656B (en) * 2023-09-14 2023-12-08 北京中天星控科技开发有限公司 Method for realizing embedded dynamic APP loading based on elf file
CN117075960A (en) * 2023-10-17 2023-11-17 统信软件技术有限公司 Program reconstruction method, application cross-platform migration method, device and computing equipment
CN117075960B (en) * 2023-10-17 2024-01-23 统信软件技术有限公司 Program reconstruction method, application cross-platform migration method, device and computing equipment

Also Published As

Publication number Publication date
CN105224370B (en) 2019-03-08
CN105224370A (en) 2016-01-06

Similar Documents

Publication Publication Date Title
WO2017067175A1 (en) Method, apparatus and device for loading elf file, and computer storage medium
CN103559449B (en) The detection method of a kind of code change and device
US8868848B2 (en) Sharing virtual memory-based multi-version data between the heterogenous processors of a computer platform
US9563446B2 (en) Binary file generation
US9507613B2 (en) Methods and apparatus for dynamically preloading classes
WO2018040270A1 (en) Method and device for loading linux-system elf file in windows system
US10754825B2 (en) Path resolver for client access to distributed file systems
CN110941456B (en) Load dynamic scripting language code on demand to reduce memory usage
CN111258666B (en) Computer file reading method, device, computer system and storage medium
CN106484719B (en) Method and terminal for expanding mobile phone storage
CN106598674A (en) Relocatable ELF file-based software quick loading method
CN110663082A (en) Data processing system and method
CN105653906A (en) Anti-kernel-hook method based on address randomization
US10083125B2 (en) Method to efficiently implement synchronization using software managed address translation
WO2023179298A1 (en) Application program updating method and apparatus, application program development method and apparatus, and computer device
JP5536593B2 (en) Optimization device, optimization method, and compiler program
CN104978221A (en) Method and system for implementing downloading and deleting of program
US8185885B2 (en) Providing recovery data for program code
CN108446186B (en) Method for recovering Dex source file from shelled Android application program
US8577936B2 (en) Fixup cache tool for object memory compaction in an information handling system
US9841976B2 (en) Lock free streaming of executable code data
US9880612B2 (en) Execution control method and execution control apparatus
KR20240090928A (en) Artificial intelligence-based integration framework
CN107239395A (en) The lookup method and equipment of invalid function in a kind of source code
CN114968328B (en) A function search method, computing device and readable storage medium

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 16856616

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 16856616

Country of ref document: EP

Kind code of ref document: A1