NetBSD/i386 port
PATCH 1/1 NetBSD/i386 port
Hi,
I've made some effort to port GNU GRUB to NetBSD/i386, possibly making it more system independent and portable.
Most of my work is a couple of small bugfixes, but there are some bigger issues that have to be dealt with and undoubtly need some more work and discussion. Anyways, with my patches whole grub builds, but since I didn't do any extensive testing I can't tell whether it behaves correctly.
My environment is: NetBSD/i386 3.99.9 (gcc 3.3.3)
Well, here is summary of issues:
================================
1.) Non-executable stack mappings.
----------------------------------
Files affected: kern/mm.c, normal/misc.c
This is probably more a gcc issue (my is gcc version 3.3.3 (NetBSD nb3 20040520)). As of 2.0, NetBSD/i386 uses non-executable mapping of stack and thus when nested functions are used it generates call to __enable_execute_stack() (which is actually in libgcc) even if -nostdlib is used. Since I know no way to disable this behavior (but there might be some) only workaround known to me is providing dumb __enable_execute_stack() functions where apropriate.
Anyways, kern/mm.c and normal/misc.c are really not the best places for such a
hack to take place.
2.) memalign()
--------------
Files affected: util/misc.c configure.ac Makefile.in conf/i386-pc.rmk
The missing memalign() issue is was already discussed some time ago, and mr. Okuji also provided some solution here:
http://lists.gnu.org/archive/html/grub-devel/2004-03/msg00048.html
http://lists.gnu.org/archive/html/grub-devel/2004-03/msg00053.html
I don't know about other systems, but NetBSD base installation contains GNU malloc library, which provides memalign(). Unlike some other commercial UNIX environments Microsoft SFU's Interix has no memalign() implementation, but
I think there is no need and probably no way to support this plattform.
3.) argp
--------
Files affected: util/grub-emu.c, configure.ac, Makefile.in, conf/i386-pc.rmk
Argp framework is also GNU libc specific. For it is not present in other libcs we might have to exclude grub-emu from nonGNU builds, replace argp with getopt, or provide argp routines. As excluding grub-emu is undoubtly unacceptable and there was certainly some reason why argp was used, glibc's argp needs some replacement.
The same problem rose in effort to make LSH portable. LSH authors decided to include argp code in their distribution. Their version is a hacked glibc splitout that is not even dependent on getopt. Fortunatelly, they provide and maintain the agrp code also separately from LSH.
LSH and their way to handle argp dependency:
http://www.lysator.liu.se/~nisse/lsh/
http://cvs.lysator.liu.se/viewcvs/viewcvs.cgi/lsh/src/argp/?root=lsh
Argp-standalone:
http://www.lysator.liu.se/~nisse/misc/
http://lists.gnu.org/archive/html/bug-gnulib/2003-03/msg00002.html
http://www.freshports.org/devel/argp-standalone/
The way that seems most wise to me is not to incude argp code, but make GRUB on nonGNU platforms dependent on arpg-standalone. Assuming that package systems (such as Free|OpenBSD ports and pkgsrc) will be primary way to install GRUB, we will need argp-standalone package to appear in them. There already exists FreeBSD package for it and I may write pkgsrc package (for NetBSD and DragonFly).
And small bugfixes:
===================
1.) BUILD_ options handling
---------------------------
Files affected: configure.ac, Makefile.in
I found no better way to specify build flags than passsing BUILD_* options. This is what my configure line looks like:
(i have liblzo in /usr/pkg and argp-standalone in /usr/local)
./configure \
BUILD_LDFLAGS='-L/usr/pkg/lib -L/usr/local/lib'
\
BUILD_CPPFLAGS='-I/usr/pkg/include -I/usr/local/include -Wall -W'
\
BUILD_CFLAGS='-g'Another issue is that -O2 doesn't work correctly for me.
In util/i386/pc/getroot.c:find_root_device() dev variable is stored in ebx register (when level 2 optimalizations used) and this register is modified by call to lstat(). And call to lstat() also clobbers ent structure ... I haven’t investigated that yet. This is likely not a GRUB bug,
but to work it around I had to override BUILD_CFLAGS ommiting -O2.
2.) GNU bison dependency
------------------------
Files affected: configure.ac, Makefile.in
Set $(YACC) to bison if bison is found.
This is currently mandatory on platforms where 'yacc' binary is not bison, because only bison works. Should this be changed (corrected?).
3.) bindir's not sbindir
------------------------
Files affected: util/i386/pc/grub-install.in
4.) GNU uniq
------------
Files affected: geninit.sh
Still haven't found out what uniq's -W1 and -t':' parameters do. Found them in some manuals (ok, didn't read them :) and in some no. Are they still part of current coreutils? And if yes, they are certainly not standard and should be replaced by something else.
My opinion about using utils like uniq is that only SUSv2 or POSIX or ...I mean that only standard and portable switches should be used.
Compile-time Warnings:
======================
$ gmake -s 2>&1 | sort | uniq
...Will certainly need some more work
uniq: unknown option -- W
usage: uniq [-c | -du] [-f fields] [-s chars] [input [output]]
Patch follows:
==============
Machine generated files (configure, i386-pc.mk) are ommited, so one should autoreconf before ./configure -ing.
Index: INSTALL
===================================================================
RCS file: /usr/home/lkundrak/cvs/grub/grub/INSTALL,v retrieving revision 1.3
retrieving revision 1.3.2.2
INSTALL 29 Nov 2005 20:41:45 -0000 1.3.2.2 renamed from INSTALL 4 Apr 2004 13:45:59 -0000 1.3 +2 -0
@@ -14,6 +14,8 @@ * GCC 2.95 or later * GNU Make * GNU binutils 2.9.1.0.23 or later +* GNU glibc or argp-standalone and memalign +* GNU bison * Other standard GNU/Unix tools * LZO 1.02 or later
Index: Makefile.in
===================================================================
RCS file: /usr/home/lkundrak/cvs/grub/grub/Makefile.in,v retrieving revision 1.16
retrieving revision 1.16.2.5
Makefile.in 30 Nov 2005 08:46:24 -0000 1.16.2.5 renamed from Makefile.in 18 Nov 2005 14:56:55 -0000 1.16 +9 -4
@@ -57,17 +57,23 @@ CC = @CC@ CFLAGS = @CFLAGS@ CPPFLAGS = -I. -Iinclude -I$(srcdir)/include -Wall -W + BUILD_CC = @BUILD_CC@ -BUILD_CFLAGS = -g -O2 -BUILD_CPPFLAGS = -I. -Iinclude -I$(srcdir)/include -Wall -W \ - -DGRUB_DATADIR=\"$(pkgdatadir)\" +BUILD_CFLAGS = @BUILD_CFLAGS@ +BUILD_CPPFLAGS = @BUILD_CPPFLAGS@ +BUILD_CPPFLAGS += -I. -Iinclude -I$(srcdir)/include -DGRUB_DATADIR=\"$(pkgdatadir)\" +BUILD_LDFLAGS = @BUILD_LDFLAGS@ + OBJCOPY = @OBJCOPY@ STRIP = @STRIP@ NM = @NM@ LD = @LD@ RUBY = @RUBY@ +YACC = @YACC@ LIBCURSES = @LIBCURSES@ LIBLZO = @LIBLZO@ +LIBARGP = @LIBARGP@ +LIBMALLOC = @LIBMALLOC@ ### General variables.
Index: configure.ac
===================================================================
RCS file: /usr/home/lkundrak/cvs/grub/grub/configure.ac,v retrieving revision 1.16
retrieving revision 1.16.2.4
configure.ac 30 Nov 2005 08:46:25 -0000 1.16.2.4 renamed from configure.ac 6 Nov 2005 22:19:58 -0000 1.16 +0 -0
@@ -122,7 +122,6 @@ [AC_MSG_ERROR([none of gcc, egcs and cc is found. set
BUILD_CC
manually.])])
else
BUILD_CC="$CC"
- AC_SUBST(BUILD_CC)
fi
# Test the C compiler for the build environment.
@@ -133,7 +132,20 @@
CC="$BUILD_CC"
CFLAGS="$BUILD_CFLAGS"
CPPFLAGS="$BUILD_CPPFLAGS"
-LDFLAGS="$BUILD_LDDFLAGS"
+LDFLAGS="$BUILD_LDFLAGS"
+
+if test "x$BUILD_CFLAGS" = x; then
+ BUILD_CFLAGS="-g -O2"
+fi
+
+if test "x$BUILD_CPPFLAGS" = x; then
+ BUILD_CPPFLAGS="-Wall -W"
+fi
+
+AC_SUBST(BUILD_CC)
+AC_SUBST(BUILD_CFLAGS)
+AC_SUBST(BUILD_CPPFLAGS)
+AC_SUBST(BUILD_LDFLAGS)
# Identify characteristics of the build architecture. AC_C_BIGENDIAN
@@ -162,6 +174,18 @@
[AC_CHECK_LIB(curses, wgetch, [LIBCURSES="-lcurses"])])
AC_SUBST(LIBCURSES)
+# Check for argp
+AC_CHECK_FUNC(argp_parse,,
+ AC_CHECK_LIB(argp, argp_parse, [LIBARGP="-largp"],
+ AC_MSG_ERROR([Argp required. Either use glibc or install
argp-standalone.])))
+AC_SUBST(LIBARGP)
+
+# Check for memalign()
+AC_CHECK_FUNC(memalign,,
+ AC_CHECK_LIB(gnumalloc, memalign, [LIBMALLOC="-lgnumalloc"],
+ AC_MSG_ERROR([memalign() required. Either use glibc or install
libgnumalloc.])))
+AC_SUBST(LIBMALLOC)
+
# Check for headers. AC_CHECK_HEADERS(ncurses/curses.h ncurses.h curses.h)
Index: conf/i386-pc.rmk
===================================================================
RCS file: /usr/home/lkundrak/cvs/grub/grub/conf/i386-pc.rmk,v retrieving revision 1.55
retrieving revision 1.55.2.2
i386-pc.rmk 29 Nov 2005 20:27:29 -0000 1.55.2.2 renamed from i386-pc.rmk 18 Nov 2005 14:56:55 -0000 1.55 +1 -1
@@ -57,7 +57,7 @@ # For grub-mkimage. grub_mkimage_SOURCES = util/i386/pc/grub-mkimage.c util/misc.c \ util/resolve.c -grub_mkimage_LDFLAGS = $(LIBLZO) +grub_mkimage_LDFLAGS = $(LIBLZO) $(LIBMALLOC) # For grub-setup. grub_setup_SOURCES = util/i386/pc/grub-setup.c util/i386/pc/biosdisk.c \
@@ -65,9 +65,11 @@ kern/err.c kern/misc.c fs/fat.c fs/ext2.c fs/xfs.c fs/affs.c \ fs/sfs.c kern/parser.c kern/partition.c partmap/pc.c \ fs/ufs.c fs/minix.c fs/hfs.c fs/jfs.c kern/file.c kern/fs.c
kern/env.cfs/fshelp.c
+grub_setup_LDFLAGS = $(LIBMALLOC)
# For grub-mkdevicemap.
grub_mkdevicemap_SOURCES = util/i386/pc/grub-mkdevicemap.c util/misc.c
+grub_mkdevicemap_LDFLAGS = $(LIBMALLOC)
# For grub-probefs.
grub_probefs_SOURCES = util/i386/pc/grub-probefs.c \
@@ -76,6 +78,7 @@
fs/ext2.c kern/parser.c kern/partition.c partmap/pc.c fs/ufs.c
\
fs/minix.c fs/hfs.c fs/jfs.c kern/fs.c kern/env.c fs/fshelp.c
\
fs/xfs.c fs/affs.c fs/sfs.c
+grub_probefs_LDFLAGS = $(LIBMALLOC)
# For grub-emu.
grub_emu_SOURCES = commands/boot.c commands/cat.c commands/cmp.c
\
@@ -100,7 +103,7 @@
util/i386/pc/biosdisk.c util/i386/pc/getroot.c
\
util/i386/pc/misc.c grub_emu_init.c-grub_emu_LDFLAGS = $(LIBCURSES) +grub_emu_LDFLAGS = $(LIBCURSES) $(LIBARGP) $(LIBMALLOC)
# For genmoddep.
genmoddep_SOURCES = util/genmoddep.c
Index: kern/mm.c
===================================================================
RCS file: /usr/home/lkundrak/cvs/grub/grub/kern/mm.c,v retrieving revision 1.13
retrieving revision 1.13.2.1
mm.c 28 Nov 2005 19:36:06 -0000 1.13.2.1 renamed from mm.c 21 Aug 2005 18:42:54 -0000 1.13 +10 -0
@@ -420,3 +420,13 @@ grub_printf ("\n"); } #endif /* MM_DEBUG */ + +/* + * XXX: gcc on systems using non-executable mapping of stack generate + * reference to __enable_execute_stack() (libgcc) + * Probably there's better way to solve this, than such an ugly workaround. + */ + +void __enable_execute_stack (void); +void __enable_execute_stack (void) {} +
Index: normal/misc.c
===================================================================
RCS file: /usr/home/lkundrak/cvs/grub/grub/normal/misc.c,v retrieving revision 1.2
retrieving revision 1.2.2.1
misc.c 28 Nov 2005 19:36:08 -0000 1.2.2.1 renamed from misc.c 19 Aug 2005 00:32:01 -0000 1.2 +10 -0
@@ -69,3 +69,13 @@ grub_printf ("\n"); return grub_errno; } + +/* + * XXX: gcc on systems using non-executable mapping of stack generate + * reference to __enable_execute_stack() (libgcc) + * Probably there's better way to solve this, than such an ugly workaround. + */ + +void __enable_execute_stack (void); +void __enable_execute_stack (void) {} +
Index: util/i386/pc/grub-install.in
===================================================================
RCS file: /usr/home/lkundrak/cvs/grub/grub/util/i386/pc/grub-install.in,v retrieving revision 1.4
retrieving revision 1.4.2.1
i386/pc/grub-install.in 27 Nov 2005 13:03:56 -0000 renamed from i386/pc/grub-install.in 7 Aug 2005 17:12:52 -0000 1.4 +0 -0
1.4.2.1
@@ -21,6 +21,7 @@
prefix=@prefix@
exec_prefix=@exec_prefix@
sbindir=@sbindir@
+bindir=@bindir@
datadir=@datadir@
PACKAGE_NAME=@PACKAGE_NAME@ PACKAGE_TARNAME=@PACKAGE_TARNAME@
@@ -31,7 +32,7 @@
pkgdatadir=${datadir}/${PACKAGE_TARNAME}/${host_cpu}-${host_vendor}
grub_setup=${sbindir}/grub-setup
-grub_mkimage=${sbindir}/grub-mkimage
+grub_mkimage=${bindir}/grub-mkimage
grub_mkdevicemap=${sbindir}/grub-mkdevicemap
grub_probefs=${sbindir}/grub-probefs
rootdir=
Cheers!
---------------------------------------------------------------------------
o Lubomir Kundrak
°O° <[email protected]>, http://skosi.org/~lkundrak/
(_) SKOSI -- Slovak OpenSource Initiative
---------------------------------------------------------------------------
"Lubomir Kundrak" <[email protected]> writes:
Hi Lubomir,
Quoted 2 lines
I've made some effort to port GNU GRUB to NetBSD/i386, possibly making it more system independent and portable.
Nice! thanks a lot for doing this.
Quoted 4 lines
Most of my work is a couple of small bugfixes, but there are some bigger issues that have to be dealt with and undoubtly need some more work and discussion. Anyways, with my patches whole grub builds, but since I didn't do any extensive testing I can't tell whether it behaves correctly.
Have you at least tried grub-emu or so?
Quoted 10 lines
My environment is: NetBSD/i386 3.99.9 (gcc 3.3.3)
Well, here is summary of issues:
================================1.) Non-executable stack mappings.
----------------------------------Files affected:
kern/mm.c,normal/misc.c
Quoted 7 lines
This is probably more a gcc issue (my is gcc version 3.3.3 (NetBSD nb3 20040520)). As of 2.0, NetBSD/i386 uses non-executable mapping of stack and thus when nested functions are used it generates call to
__enable_execute_stack()(which is actually in libgcc) even if -nostdlib is used. Since I know no way to disable this behavior (but there might be some) only workaround known to me is providing dumb__enable_execute_stack()functions where apropriate.
When is this required? When using grub-emu or any other userland utility? In that case GRUB is linked to the C library.
In the case it is used for GRUB itself, it is useless. GRUB executes within its own environment. So in that case we should just make sure this doesn't show up. Perhaps by using different compiler flags.
Quoted 2 lines
Anyways,
kern/mm.candnormal/misc.care really not the best places for such a hack to take place.
Agreed.
Quoted 10 lines
2.)
memalign()
--------------Files affected:
util/misc.cconfigure.ac Makefile.in conf/i386-pc.rmkThe missing
memalign()issue is was already discussed some time ago, and mr. Okuji also provided some solution here:
http://lists.gnu.org/archive/html/grub-devel/2004-03/msg00048.html
http://lists.gnu.org/archive/html/grub-devel/2004-03/msg00053.html
Quoted 5 lines
I don't know about other systems, but NetBSD base installation contains GNU malloc library, which provides
memalign(). Unlike some other commercial UNIX environments Microsoft SFU's Interix has nomemalign()implementation, but
I think there is no need and probably no way to support this plattform.
If the GNU malloc library helps here and it is available for FreeBSD and OpenBSD this seems like the best solution to me. IIRC one of these two systems didn't have memalign.
Quoted 10 lines
3.) argp
--------Files affected:
util/grub-emu.c, configure.ac, Makefile.in, conf/i386-pc.rmkArgp framework is also GNU libc specific. For it is not present in other libcs we might have to exclude grub-emu from nonGNU builds, replace argp with getopt, or provide argp routines. As excluding grub-emu is undoubtly unacceptable and there was certainly some reason why argp was used, glibc's argp needs some replacement.
As GRUB, just like glibc is a GNU package this is a good choice. Besides that, I wonder if BSD supports getopt_long.
Quoted 14 lines
The same problem rose in effort to make LSH portable. LSH authors decided to include argp code in their distribution. Their version is a hacked glibc splitout that is not even dependent on getopt. Fortunatelly, they provide and maintain the agrp code also separately from LSH.
LSH and their way to handle argp dependency:
http://www.lysator.liu.se/~nisse/lsh/
http://cvs.lysator.liu.se/viewcvs/viewcvs.cgi/lsh/src/argp/?root=lshArgp-standalone:
http://www.lysator.liu.se/~nisse/misc/
http://lists.gnu.org/archive/html/bug-gnulib/2003-03/msg00002.html
http://www.freshports.org/devel/argp-standalone/
Quoted 6 lines
The way that seems most wise to me is not to incude argp code, but make GRUB on nonGNU platforms dependent on arpg-standalone. Assuming that package systems (such as Free|OpenBSD ports and pkgsrc) will be primary way to install GRUB, we will need argp-standalone package to appear in them. There already exists FreeBSD package for it and I may write pkgsrc package (for NetBSD and DragonFly).
If it can be solved this way, that would be great. Another option is including argp from GNU lib. But not including external sourcecode seems like the better option, because we don't have to keep things up-to-date.
Quoted 19 lines
And small bugfixes:
===================1.)
BUILD_options handling
---------------------------Files affected: configure.ac, Makefile.in
I found no better way to specify build flags than passsing BUILD_* options. This is what my configure line looks like:
(i have liblzo in/usr/pkgand argp-standalone in/usr/local)./configure \ BUILD_LDFLAGS='-L/usr/pkg/lib -L/usr/local/lib' \ BUILD_CPPFLAGS='-I/usr/pkg/include -I/usr/local/include -Wall -W' \ BUILD_CFLAGS='-g'
Quoted 7 lines
Another issue is that -O2 doesn't work correctly for me.
In util/i386/pc/getroot.c:find_root_device() dev variable is stored in ebx register (when level 2 optimalizations used) and this register is modified by call tolstat(). And call tolstat()also clobbers ent structure ... I haven’t investigated that yet. This is likely not a GRUB bug,
but to work it around I had to overrideBUILD_CFLAGSommiting -O2.
I agree that overriding LDFLAGS and CPPFLAGS is quite useful. The problem with using random other flags like optimizations might seriously disrupt GRUB.
I don't know a good way to fix this problem.
Quoted 8 lines
2.) GNU bison dependency
------------------------Files affected: configure.ac, Makefile.in
Set $(YACC) to bison if bison is found.
This is currently mandatory on platforms where 'yacc' binary is not bison, because only bison works. Should this be changed (corrected?).
If bison isn't used the code most likely doesn't work. The best fix would just be showing an error when it was not found. Relying on something other than bison doesn't seem the best solution to me. I want to use bison specific features and I don't want to provide workarounds for other yacc implementations.
Quoted 4 lines
3.) bindir's not sbindir
------------------------Files affected: util/i386/pc/grub-install.in
Can you explain this in more detail?
Quoted 5 lines
4.) GNU uniq
------------Files affected:
geninit.sh
Quoted 7 lines
Still haven't found out what uniq's -W1 and -t':' parameters do. Found them in some manuals (ok, didn't read them :) and in some no. Are they still part of current coreutils? And if yes, they are certainly not standard and should be replaced by something else.
My opinion about using utils like uniq is that only SUSv2 or POSIX or ...I mean that only standard and portable switches should be used.
Personally I don't care much about using non-GNU utilities in the build process. I expect the people who actually build GRUB 2 themselves can install the required builddeps. It's not really that strange when GNU programs rely on other GNU programs in their build process.
It's weird that you don't have the -t option, I thought it was POSIX. It means that :' is used as separator instead of '. The argument `-W1' means that it only compares once per line.
Can you provide something which does the same and doesn't break the build process for other ports? Or can you install coreutils?
Quoted 12 lines
Index: Makefile.in
===================================================================
RCS file:/usr/home/lkundrak/cvs/grub/grub/Makefile.in,vretrieving revision 1.16
retrieving revision 1.16.2.5
diff -u -r1.16 -r1.16.2.5Makefile.in 30 Nov 2005 08:46:24 -0000 1.16.2.5 renamed from Makefile.in 18 Nov 2005 14:56:55 -0000 1.16 +0
-0@@ -57,17 +57,23 @@ CC = @CC@ CFLAGS = @CFLAGS@ CPPFLAGS = -I. -Iinclude -I$(srcdir)/include -Wall -W
Makefile.in 30 Nov 2005 08:46:24 -0000 1.16.2.5 line 65
Quoted 10 lines
Makefile.in 30 Nov 2005 08:46:24 -0000 1.16.2.5 renamed from Makefile.in 18 Nov 2005 14:56:55 -0000 1.16 +5
-4+ BUILD_CC = @BUILD_CC@ -BUILD_CFLAGS = -g -O2 -BUILD_CPPFLAGS = -I. -Iinclude -I$(srcdir)/include -Wall -W \ - -DGRUB_DATADIR=\"$(pkgdatadir)\" +BUILD_CFLAGS = @BUILD_CFLAGS@ +BUILD_CPPFLAGS = @BUILD_CPPFLAGS@ +BUILD_CPPFLAGS += -I. -Iinclude -I$(srcdir)/include -DGRUB_DATADIR=\"$(pkgdatadir)\" +BUILD_LDFLAGS = @BUILD_LDFLAGS@
I am not yet sure if this really is the right thing to do. It fixes things for you, but I am more interested in if other things horribly break.
Quoted 2 lines
Index: conf/i386-pc.rmk
===================================================================
[...]
I suppose you can't test this on the PPC? :-)
Thanks,
Marco
Quoted 11 lines · Marco Gerards
Quoted 10 lines
Files affected:
kern/mm.c,normal/misc.cThis is probably more a gcc issue (my is gcc version 3.3.3 (NetBSD nb3 20040520)). As of 2.0, NetBSD/i386 uses non-executable mapping of stack and thus when nested functions are used it generates call to
__enable_execute_stack()(which is actually in libgcc) even if -nostdlib
is used. Since I know no way to disable this behavior (but there might be some) only workaround known to me is providing dumb__enable_execute_stack()functions where apropriate.
Quoted 6 lines
When is this required? When using grub-emu or any other userland utility? In that case GRUB is linked to the C library.
In the case it is used for GRUB itself, it is useless. GRUB executes within its own environment. So in that case we should just make sure this doesn't show up. Perhaps by using different compiler flags.
I think this is a problem Peter Jones was telling me about with Fedora as well (non-executable stack conflicting with nested functions).
Peter, do you know anything about that __enable_execute_stack()? http://gcc.gnu.org/ml/gcc-patches/2004-07/msg01700.html seems to indicate it's only for Solaris, NetBSD, FreeBSD/SPARC64 and OSF, but it sounds like we need it on Linux as well?
-Hollis
Quoted 22 lines · Hollis Blanchard
Quoted 14 lines · Marco Gerards
Quoted 13 lines
Files affected:
kern/mm.c,normal/misc.cThis is probably more a gcc issue (my is gcc version 3.3.3 (NetBSD nb3 20040520)). As of 2.0, NetBSD/i386 uses non-executable mapping of stack
and thus when nested functions are used it generates call to__enable_execute_stack()(which is actually in libgcc) even if -nostdlib
is used. Since I know no way to disable this behavior (but there might be
some) only workaround known to me is providing dumb__enable_execute_stack()
functions where apropriate.Quoted 6 lines
When is this required? When using grub-emu or any other userland utility? In that case GRUB is linked to the C library.
In the case it is used for GRUB itself, it is useless. GRUB executes within its own environment. So in that case we should just make sure this doesn't show up. Perhaps by using different compiler flags.
Quoted 7 lines
I think this is a problem Peter Jones was telling me about with Fedora as well (non-executable stack conflicting with nested functions).
Peter, do you know anything about that
__enable_execute_stack()?
http://gcc.gnu.org/ml/gcc-patches/2004-07/msg01700.html seems to indicate it's only for Solaris, NetBSD, FreeBSD/SPARC64 and OSF, but it sounds like we need it on Linux as well?
In that case we only need it in util/, I assume? I wonder if it is possible to check this using autoconf and add it to util/misc.c in some initialization function (that yet has to be written).
I was told that -ftrampolines might fix our problem, so looking at that flag might be useful.
--
Marco
Quoted 15 lines · Hollis Blanchard
Quoted 14 lines · Marco Gerards
Quoted 13 lines
Files affected:
kern/mm.c,normal/misc.cThis is probably more a gcc issue (my is gcc version 3.3.3 (NetBSD nb3 20040520)). As of 2.0, NetBSD/i386 uses non-executable mapping of stack
and thus when nested functions are used it generates call to__enable_execute_stack()(which is actually in libgcc) even if -nostdlib
is used. Since I know no way to disable this behavior (but there might be
some) only workaround known to me is providing dumb__enable_execute_stack()
functions where apropriate.
Quoted 2 lines
Quoted 2 lines
When is this required? When using grub-emu or any other userland utility? In that case GRUB is linked to the C library.
It hits when you use any userland utility that has nested functions, so I think we're talking about grub-emu and grub-install. GCC (and possibly other compilers, I don't really know) uses stack trampolines generated during execution to implement nested functions. So whether you're using libc or not, you need to write to the stack. In recent versions of Fedora and RHEL, a program by default doesn't have an executable stack, and that's enforced by the hardware. So when we hit one of these trampolines, if the stack isn't executable, you get a segfault.
On Linux with GCC, executables may have an elf note that states that they need an executable stack, and that should work. For nested functions it should be automatically generated with any recent version of GCC. In general this is true of any code GCC generates that uses a writable stack, but untrue in the case where e.g. inline assembly writes to the stack. There's a utility called "execstack" that can query, set, or clear the flags for a binary.
Even though the elf notes provide us with the ability to annotate that a program needs an executable stack, we'd prefer not to do that. We strongly prefer not to ship *any* programs that use an executable stack at all. With GRUB Legacy, which we're currently shipping on x86 and x86_64, we patch grub in such a way that it no longer has nested functions in the paths that grub-install hits. We also ship a patch I sent to bug-grub several months ago which mprotects grub's "test" stack correctly.
Quoted 3 lines
Quoted 3 lines
In the case it is used for GRUB itself, it is useless. GRUB executes within its own environment. So in that case we should just make sure this doesn't show up. Perhaps by using different compiler flags.
Yeah, it shouldn't matter with grub itself at all, because no memory the bootloader is mapped into should have any protection.
Quoted 7 lines
I think this is a problem Peter Jones was telling me about with Fedora as well (non-executable stack conflicting with nested functions).
Peter, do you know anything about that
__enable_execute_stack()?
http://gcc.gnu.org/ml/gcc-patches/2004-07/msg01700.html seems to indicate it's only for Solaris, NetBSD, FreeBSD/SPARC64 and OSF, but it sounds like we need it on Linux as well?
I don't know what, if anything, this libgcc call does on linux. In general to use an executable stack here, we need either the elf notes in the binary to say they need it. It's also possible (but not really recommended, of course) to allocate your own stack and mark it PROT_EXEC with mprotect(), but that should really only be used on memory that you have access to as the result of a malloc() call from within the program.
Signature · 1 line
Peter