Subject: [PATCH] unbreak Emacs on Linux/M68K

From: Mikael Pettersson <[email-masked]>
Date: Tue, 1 Jan 2013 22:51:12 +0100

Attempting to bootstrap Emacs 23.x or 24.x on Linux/M68K fails with
a SEGV as soon as temacs is run.  Inspecting the core dump shows:

Core was generated by `/home/mikpe/emacs-24.2/build-nox/src/temacs --batch --load loadu'.
Program terminated with signal 11, Segmentation fault.
#0  0x800d5444 in mark_object (arg=1174437910) at /home/mikpe/emacs-24.2/src/alloc.c:5720
5720		if (CONS_MARKED_P (ptr))
(gdb) bt
#0  0x800d5444 in mark_object (arg=1174437910) at /home/mikpe/emacs-24.2/src/alloc.c:5720
#1  0x800d55ba in mark_object (arg=-2143958762) at /home/mikpe/emacs-24.2/src/alloc.c:5731
#2  0x800d5c3a in mark_vectorlike (ptr=0x8035b120) at /home/mikpe/emacs-24.2/src/alloc.c:5420
#3  0x800d5d62 in Fgarbage_collect () at /home/mikpe/emacs-24.2/src/alloc.c:5128
#4  0x800eb7be in Ffuncall (nargs=3, args=0xefcdfad4) at /home/mikpe/emacs-24.2/src/eval.c:2938
#5  0x800ebc46 in call2 (fn=0, arg1=0, arg2=-2143972138) at /home/mikpe/emacs-24.2/src/eval.c:2785
#6  0x800ebf82 in Fsignal (error_symbol=0, data=-2143972138) at /home/mikpe/emacs-24.2/src/eval.c:1710
#7  0x800ec412 in xsignal (error_symbol=0, data=-2143972138) at /home/mikpe/emacs-24.2/src/eval.c:1785
#8  0x800ecdc8 in xsignal1 (error_symbol=0, arg=-2143955495) at /home/mikpe/emacs-24.2/src/eval.c:1800
#9  0x800ece32 in verror (m=0x8015f90d "Bad data in guts of obarray", ap=0xefce0b24) at /home/mikpe/emacs-24.2/src/eval.c:1998
#10 0x800ece44 in error (m=0x8015f90d "Bad data in guts of obarray") at /home/mikpe/emacs-24.2/src/eval.c:2010
#11 0x80108eec in oblookup (obarray=<optimized out>, ptr=0x80156383 "char-table-extra-slots", size=22, size_byte=22) at /home/mikpe/emacs-24.2/src/lread.c:3912
#12 0x8010c578 in intern_c_string (str=0x80156383 "char-table-extra-slots") at /home/mikpe/emacs-24.2/src/lread.c:3728
#13 0x8005ce76 in init_category_once () at /home/mikpe/emacs-24.2/src/category.c:461
#14 0x80006558 in main (argc=<optimized out>, argv=0xefce0ef4) at /home/mikpe/emacs-24.2/src/emacs.c:1265
(gdb) q

This turns out to be caused by the well-known issue of data alignment
on Linux/M68K: the ABI only aligns 32-bit (and larger) primitive types
to 16-bit boundaries, which doesn't provide enough known-zero bits in
object pointers for Emacs' tagging scheme, causing major confusion and
breakage at runtime.

This patch introduces two new integer types in src/m/m68k.h that are
exactly like int and unsigned int, except they are explicitly aligned
to 32-bit boundaries; gcc's attribute aligned is used for this purpose.
It then defines EMACS_INT and EMACS_UINT to these types to override the
default choices in src/lisp.h.

With this patch in place bootstrap succeeds and the final emacs
executable works fine.  Tested with Emacs-24.2 on Linux/M68K.

/Mikael Pettersson

[I consider this 6-line patch (not counting the comment) to be trivial so
I hope it will be acceptable even without a formal copyright assignment.]

src/ChangeLog:

2013-01-01  Mikael Pettersson  <[email-masked]>

	* m/m68k.h [GNU_LINUX] (m68k_aligned_int_t, m68k_aligned_uint_t):
	New typedefs.
	(EMACS_INT, EMACS_UINT, BITS_PER_EMACS_INT, pI): Define.

--- emacs-24.2/src/m/m68k.h.~1~	2012-08-23 07:33:42.000000000 +0200
+++ emacs-24.2/src/m/m68k.h	2013-01-01 14:26:10.000000000 +0100
@@ -28,5 +28,19 @@ along with GNU Emacs.  If not, see <http
 #define DATA_SEG_BITS 0x80000000
 #endif
 
+/* Define the type to use.
+   Emacs cannot use the default type (plain int) since the ABI on
+   Linux/M68K only aligns 32-bit (and larger) primitive types to
+   16-bit boundaries.  Define new explicitly aligned integer types
+   and use them for EMACS_INT and EMACS_UINT.  Compiling Emacs
+   with -malign-int does not work since it changes data layout in
+   external interfaces (system calls, libraries).  */
+typedef int m68k_aligned_int_t __attribute__ ((__aligned__ (4)));
+typedef unsigned int m68k_aligned_uint_t __attribute__ ((__aligned__ (4)));
+#define EMACS_INT m68k_aligned_int_t
+#define EMACS_UINT m68k_aligned_uint_t
+#define BITS_PER_EMACS_INT 32
+#define pI ""
+
 #endif