Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a C header file?

Tags:

Does anyone know a spiffy way to use C header files in Python? For example I have a C program that includes a global variable:

typedef struct ImageInfo {     uint8_t revisionMajor;     uint8_t revisionMinor;     uint16_t checksum;      } ImageInfo;  ImageInfo gImageInfo;   /* Placed at a specific address by the linker */ 

I would like to be able to take the binary generated by the C compiler/linker and parse this structure (and possibly modify it) with a Python script.

like image 759
waffleman Avatar asked Dec 23 '09 08:12

waffleman


People also ask

How do C header files work?

A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

Can we make custom header files in C?

Yes, the user can create his/her own custom header files in C. It helps you to manage the user-defined methods, global variables, and structures in a separate file, which can be used in different modules.

Can C++ use C header files?

You can use C Standard I/O from the standard C header <stdio. h> in C++ programs because C Standard I/O is part of C++.


1 Answers

This was mentioned on SO yesterday; I haven't had a chance to check it out more thoroughly yet, but I'm meaning to. The pycparser, a "C parser and AST generator written in Python".

https://github.com/eliben/pycparser

like image 77
Matt Anderson Avatar answered Oct 18 '22 22:10

Matt Anderson