info:c_memory_structure
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
info:c_memory_structure [2012/10/15 03:54] – [Obtaining the debug section data] moritz | info:c_memory_structure [2012/10/16 15:23] (current) – [Using the debug info dump] moritz | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Determining C memory layout ====== | ====== Determining C memory layout ====== | ||
- | A while ago I had to write a Python program that generated C structures as C expects to find them in memory. The challenge was that the target system had a different alignment, endianness and word size than the computer running the Python scripts on. In this article I am referring to Python but the result can be transferred to any other programming language. | + | A while ago I had to write a Python program that generated C structures as C expects to find them in memory. The challenge was that the target system had a different alignment, endianness and word size than the computer running the Python scripts on. This is especially important when developing for multiple targets, like i386 and AMD64, different ARM platforms and MIPS. In this article I am referring to Python but the result can be transferred to any other programming language. |
===== Goals ===== | ===== Goals ===== | ||
Line 17: | Line 17: | ||
</ | </ | ||
- | On 32-bit machines, it is assumed that an < | + | On 32-bit machines, it is assumed that an '' |
On the Python side I want to be able to use the following code: | On the Python side I want to be able to use the following code: | ||
Line 50: | Line 50: | ||
The only program that really knows the structure layout in memory is the compiler. It decides about the in-memory structure. Hence, I started investigating if there was a way to extract this information from the compiler. | The only program that really knows the structure layout in memory is the compiler. It decides about the in-memory structure. Hence, I started investigating if there was a way to extract this information from the compiler. | ||
- | ===== The missing piece: .debug_section | + | ===== The missing piece: .debug_info section |
- | C compilers can store additional information to the compiled output in program sections. For ELF files, there is an optional '' | + | C compilers can store additional information to the compiled output in program sections. For ELF files, there is an optional '' |
For different compilers exist different tools to access the debug section content. For GCC, there is objdump, for IAR there exists ielfdump. Both allow to print the debug section in a human-readable form. However, the structure is not documented and requires the programmer to reverse engineer it. (For GCC it is open-source, | For different compilers exist different tools to access the debug section content. For GCC, there is objdump, for IAR there exists ielfdump. Both allow to print the debug section in a human-readable form. However, the structure is not documented and requires the programmer to reverse engineer it. (For GCC it is open-source, | ||
Line 99: | Line 99: | ||
Here is the output on my machine: | Here is the output on my machine: | ||
- | < | + | < |
- | + | ||
- | struct: | + | |
Contents of the .debug_info section: | Contents of the .debug_info section: | ||
Line 243: | Line 241: | ||
This is a lot of output. Let's explain the different parts. | This is a lot of output. Let's explain the different parts. | ||
+ | |||
+ | Each line represents either the start of a new entity or adds an attribute to one. All entities and attributes have a unique address, encoded in hex. Each entity has a type, which determines the attributes it has. The entity line consists of a nesting level, the unique address followed by the entity' | ||
+ | |||
+ | ==== Finding the size information ==== | ||
+ | |||
Fist, it tells us what format the file has. In this case it is a '' | Fist, it tells us what format the file has. In this case it is a '' | ||
Line 257: | Line 260: | ||
< | < | ||
- | Here, '' | + | Here, '' |
+ | |||
+ | |||
+ | ==== Problems ==== | ||
+ | |||
+ | This approach has the downside that it requires a certain format of the debug dump. Also, it does not specify the endianness of the types. | ||
+ | |||
+ | ===== IAR ===== | ||
+ | |||
+ | The same can be done with the tools that come with the IAR C/C++ compiler. The ELF dump utility is called '' | ||
+ | |||
+ |
info/c_memory_structure.1350273247.txt.gz · Last modified: 2012/10/15 03:54 by moritz