Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make pycparser work with linux kernel code #513

Open
mei-g opened this issue Jul 13, 2023 · 4 comments
Open

make pycparser work with linux kernel code #513

mei-g opened this issue Jul 13, 2023 · 4 comments

Comments

@mei-g
Copy link

mei-g commented Jul 13, 2023

Hi Eli,
Thanks for the information provided. I image it will be challenging task and tried quite a few different ways but without success.
Attached is a standard pre-compiled arch/x86/boot/cpu.c file cpu_pp.log which compiled with the fake lib headers. When running pycparser against that file, I got pycparser.plyparser.ParseError: xxx/kernel-source/include/linux/stdarg.h:5:27: before: va_list.

I think I am missing something because va_list is a gcc symbol and it should be recognised. Can you please have a quick look?

cpu_pp.log

Sorry, I was going to attach to the other ticket but it was closed. But really appreciated!

@eliben
Copy link
Owner

eliben commented Jul 14, 2023

I'm not sure this file was generated after preprocessing with the fake headers. I see no menion of "fake" in that file, and I'd expect to see many if indeed the fake headers were added with -I to the preprocessing step.

Another piece of evidence is that the fake headers actually include fake typedefs for __builtin_va_list and va_list, so they should compile properly.

For example, if you run from the root directory of pycparser:

$ gcc -E -I utils/fake_libc_include/ ./tests/c_files/year.c

You will see the preprocessing pull stuff from the fake headers, including va_list which is used inside the C file. The output of this preprocessing should be successfully parsed by pycparser. You can see this in action by running the examples/using_cpp_libc.py example.

@mei-g
Copy link
Author

mei-g commented Jul 14, 2023

So sorry. Uploaded the wrong file. Here it is:
cpu_pp.log

the compiling commands is the following:
gcc -E -D'__attribute__(x)=' -nostdinc -I/home/user/pycparser/utils/fake_libc_include -I/home/user/kernel-source/arch/x86/include -I./arch/x86/include/generated -I/home/user/kernel-source/include -I./include -I/home/user/kernel-source/arch/x86/include/uapi -I./arch/x86/include/generated/uapi -I/home/user/kernel-source/include/uapi -I./include/generated/uapi -include /home/user/kernel-source/include/linux/compiler-version.h -include /home/user/kernel-source/include/linux/kconfig.h -include /home/user/kernel-source/include/linux/compiler_types.h -D__KERNEL__ -m16 -g -Os -D_SETUP -I /home/user/kernel-source/arch/x86/boot -I./arch/x86/boot -o arch/x86/boot/cpu.o /home/user/kernel-source/arch/x86/boot/cpu.c

There were warnings of redefinition.
/home/user/pycparser/utils/fake_libc_include/_fake_defines.h:208: warning: "va_arg" redefined 208 | #define va_arg(_ap, _type) __builtin_va_arg((_ap)) | In file included from /home/user/kernel-source/arch/x86/boot/boot.h:21, from /home/user/kernel-source/arch/x86/boot/cpu.c:16: /home/user/kernel-source/include/linux/stdarg.h:8: note: this is the location of the previous definition 8 | #define va_arg(v, T) __builtin_va_arg(v, T)

However, the linux header includes have to be there. Otherwise it won't compile.
Any suggestions are really welcomed!

@eliben
Copy link
Owner

eliben commented Jul 17, 2023

You have to plow through the issues one by one. The Linux kernel is not - to the best of my knowledge - trying to be "standard C99", so it has some duplicate definitions and so on. You can move the __builtin_va_list around, but the next problem that pops is that https://codebrowser.dev/linux/linux/include/linux/stddef.h.html#false defines an enum using true and false, which in C99 are defined to be 1 and 0 already.

If I had time to go through the whole exercise now, I would try to create a separate version of the fake headers fine-tuned for the linux kernel, and knock the problems out one by one. There could be more difficult issues ahead like specific GNU extensions used by the kernel code which pycparser does not support, though.

@mei-g
Copy link
Author

mei-g commented Jul 18, 2023

okay, got it. Thanks very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants