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

Mingw Corrupted Executable #58

Open
sonich2401 opened this issue Jan 20, 2023 · 2 comments
Open

Mingw Corrupted Executable #58

sonich2401 opened this issue Jan 20, 2023 · 2 comments

Comments

@sonich2401
Copy link

sonich2401 commented Jan 20, 2023

Compiling on Mingw-W64 x86_64-ucrt-posix-seh 11.2.0 causes the program to do undefined things such as crash when creating a const struct and corrupts data that is imported.

I am trying to include image and audio resources via Incbin and then placing them into a struct so I get the file size and the pointer to the data into one varible. This is done as follows

#define RES_INC(name, path) INCBIN(name, path); const struct file_struct s_ ## name = {g ## name ## Data, g ## name ## Size};

Looking at the program in gdb, it crashes when making the struct rater than the INC bin. It crashes at

__static_initialization_and_destruction0

This happens after the first included file was added successfully. The file_struct struct is as follows

struct file_struct { const unsigned char * data; size_t size; };

if I add a bunch of padding ints after each struct the program runs but other varibles that are in other .cpp files are being altered

@sonich2401
Copy link
Author

sonich2401 commented Jan 21, 2023

Update: creating the structs at runtime now gives the error of a corrupt stack frame

Update2:

Removing all of my code and just keeping the incbin statements still crashes the program

@sonich2401
Copy link
Author

I was able to give a "band-aid" fix to this by lagging the compiler. I was looking at the assembly generated by my program and it was writing the offsets of the data to be 0x52 and other random numbers. This was not relative from the instruction pointer but rather from 0x0. Usually, the assemby should show that the offset is 0x0 and the instruction should just look like this in binary

0x45 0x00 0x00 0x00 0x00

but it had something like

0x45 0x52 0x00 0x00 0x00

Usually the 4 zeros means that the linker will take care of it later and fill in what the address should be. My theory is that the inline assembler gives control back to the .c file but has not yet finished the .incbin statement. Therefor the data is not ready and the compiler just puts junk in there. Putting a bunch of statements that do nothing after the INCBIN statement seemed to fix everything so that is why I believe that. I dont know how compilers work so I could be wrong but I feel like that is a good theory.

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

No branches or pull requests

1 participant