前几天写了一个程序,在Linux 上运行正常,在HP-UX B11.23 上却是bus error (core dump)
后来发现是由于地址对齐引起的:
$ cat pad.c
#include <stdlib.h>
int main(void)
{
char a[1024] = {0};
*(uint64_t*)(a+3) = 123;//让64 位整数不在4的整数地址上起始
}
$ cc -o pad pad.c -g +DD64 #开启64位代码编译
$ ./pad
Bus error(coredump)
$ gdb -core core
Detected 64-bit corefile.
Invoking /opt/langtools/bin/gdb64.
HP gdb 5.4.0 for PA-RISC 2.0 (wide), HP-UX 11.00
and target hppa2.0w-hp-hpux11.00.
Copyright 1986 - 2001 Free Software Foundation, Inc.
Hewlett-Packard Wildebeest 5.4.0 (based on GDB) is covered by the
GNU General Public License. Type "show copying" to see the conditions to
change it and/or distribute copies. Type "show warranty" for warranty/support.
Reading symbols from pad...done.
Core was generated by `pad'.
Program terminated with signal 10, Bus error.
BUS_UNKNOWN - Unknown Error
#0 0x40000000000023a0 in main () at pad.c:5
5 *(uint64_t*)(a+3) = 123;
(gdb) q
$
在pa-risc 2.0 上编译的代码,任何64 位整数起始地址如果不是 8的整数倍,访问就会产生bus error