Skip to content

Instantly share code, notes, and snippets.

@xiangzhai
Last active July 10, 2020 08:15
Show Gist options
  • Select an option

  • Save xiangzhai/97f8cbe487c13a283264ab8c521463cb to your computer and use it in GitHub Desktop.

Select an option

Save xiangzhai/97f8cbe487c13a283264ab8c521463cb to your computer and use it in GitHub Desktop.
#include <stdio.h>
struct foo {
unsigned char bar[8];
};
int main(int argc, char *argv[]) {
struct foo x = {0, 1, 2, 3, 4, 5, 6, 7};
unsigned int *p = (unsigned int *)(x.bar + 3);
int i;
if (argc > 1) {
#if defined(__GNUC__)
#if defined(__i386__)
/* Enable Alignment Checking on x86 */
__asm__ __volatile__("pushf\norl $0x40000,(%esp)\npopf");
#elif defined(__x86_64__)
/* Enable Alignment Checking on x86_64 */
__asm__ __volatile__("pushf\norl $0x40000,(%rsp)\npopf");
#elif defined(__mips64__)
/* Enable Alignment Checking on mips64el */
#include <sys/sysmips.h>
sysmips(MIPS_FIXADE, 0);
#endif
#endif
}
printf("*p = %08lx\n", *p);
*p = 0xdeadface;
for (i = 0; i <= 7; i++)
printf("%02x ", x.bar[i]);
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment