22

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.

martineau
  • 119,623
  • 25
  • 170
  • 301
waffleman
  • 4,159
  • 10
  • 39
  • 63
  • 1
    The structure probably doesn't exist "in the binary" as such--it doesn't really "exist" until execution time after the program has been loaded into RAM by the OS. So what exactly are you trying to do? – Craig McQueen Dec 23 '09 at 23:53
  • Whoops I forgot to mention the the global variable is placed in read only (i.e. Flash or ROM) memory. In the above example gImageInfo would be placed at the offset 0x1000 in the binary and I want to be able to read out the structure with Python and get the revision info the verify the checksum. – waffleman Dec 28 '09 at 13:53

4 Answers4

31

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

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
Matt Anderson
  • 19,311
  • 11
  • 41
  • 57
  • While ostensibly ancient history, this answer deserves at least 2 more up votes -- it provides the ability to do exactly what the original post requests. – synthesizerpatel Nov 20 '10 at 13:03
17

Have a look at this C++ header parser written in Python. You can also write your own parser using any of these tools:

Brunisboy
  • 123
  • 1
  • 19
Vijay Mathew
  • 26,737
  • 4
  • 62
  • 93
  • 1
    Great references: I didn't know about a bunch of those modules. I'd like to know how full-featured they are compared to pygccxml (which clearly has a leg-up since it's backed by GCC). Writing a C++ parser is notoriously hard (though the question was about C parsing). It would be nice to use something lighter-weight than the full GCCXML based stack if possible. – jkp Dec 23 '09 at 08:55
9

Take a look at pygccxml. I use it to build in-memory graphs of my C / C++ source code that I can use as the basis for many code generation tasks.

PS: When I first started out with Python based code-generation I actually tried to write a parser myself: save yourself the pain and don't even go there! (looks like your are clued up already though...) pygccxml is everything you want and more :)

ikicic
  • 98
  • 1
  • 7
jkp
  • 78,960
  • 28
  • 103
  • 104
7

For the same purpose I'm considering pyclibrary, it is not a complete C parser but it is aimed at parsing C header files, so it is much easier to use than pycparser or some gccxml-based parser: although weakly documented, just try CParser.py testHeader.h and you'll see how it works.

Depends on pyparsing and, as far as I understand, it is pure python.

Davide
  • 196
  • 1
  • 4
  • 1
    pyclibrary can now be installed through pip : [pyclibrary on PyPI](https://pypi.org/project/pyclibrary/) – Jona Nov 18 '19 at 07:51