Gentoo Archives: gentoo-commits

From: Ian Stakenvicius <axs@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] repo/gentoo:master commit in: sys-boot/grub/files/, sys-boot/grub/
Date: Tue, 15 Dec 2015 17:05:59
Message-Id: 1450199136.9236cac3ad29a31f428157026801827d58c3b8a3.axs@gentoo
1 commit: 9236cac3ad29a31f428157026801827d58c3b8a3
2 Author: Ian Stakenvicius <axs <AT> gentoo <DOT> org>
3 AuthorDate: Tue Dec 15 17:05:11 2015 +0000
4 Commit: Ian Stakenvicius <axs <AT> gentoo <DOT> org>
5 CommitDate: Tue Dec 15 17:05:36 2015 +0000
6 URL: https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=9236cac3
7
8 sys-boot/grub:0 - Properly fix misoptimization by -fcombine-stack-adjustmemts (gcc-4.9+)
9
10 GCC assumes that it can combine stacks from outer "grub_stage2()"
11 and inner trampoline "doit()" functions (optimisation -fcombine-stack-adjustments).
12
13 But doit() function clobbers %esp in inline assebly statement as:
14
15 asm volatile ("movl %%esp, %0\n\tmovl %1, %%esp\n"
16 : "=&r" (realstack) : "r" (simstack));
17
18 which tricks gcc into computing invalid local variable offsets from current %esp value.
19
20 Fix by hinting gcc about %esp change in clobber list.
21
22 Thanks to slyfox for his debugging and patch! This properly fixes bug 564890, allowing
23 for the -O0 hack to be removed.
24
25 Package-Manager: portage-2.2.24
26
27 ...97-Add-esp-to-list-of-clobbered-registers.patch | 53 ++++
28 sys-boot/grub/grub-0.97-r17.ebuild | 289 +++++++++++++++++++++
29 2 files changed, 342 insertions(+)
30
31 diff --git a/sys-boot/grub/files/grub-0.97-Add-esp-to-list-of-clobbered-registers.patch b/sys-boot/grub/files/grub-0.97-Add-esp-to-list-of-clobbered-registers.patch
32 new file mode 100644
33 index 0000000..c550aab
34 --- /dev/null
35 +++ b/sys-boot/grub/files/grub-0.97-Add-esp-to-list-of-clobbered-registers.patch
36 @@ -0,0 +1,53 @@
37 +From 5a52ee4b5cd152fa4ccd1c1f938ce2eba652af52 Mon Sep 17 00:00:00 2001
38 +From: Sergei Trofimovich <slyfox@g.o>
39 +Date: Mon, 14 Dec 2015 23:59:17 +0000
40 +Subject: [PATCH] Add %esp to list of clobbered registers
41 +
42 +GCC assumes that it can combine stacks from
43 +outer
44 + grub_stage2()
45 +and inner
46 + trampoline doit()
47 +functions (optimisation -fcombine-stack-adjustments).
48 +
49 +But doit() function clobbers %esp in inline
50 +assebly statement as:
51 +
52 + asm volatile ("movl %%esp, %0\n\tmovl %1, %%esp\n"
53 + : "=&r" (realstack) : "r" (simstack));
54 +
55 +which tricks gcc into computing invalid local variable
56 +offsets from current %esp value.
57 +
58 +Fix by hinting gcc about %esp change in clobber list.
59 +
60 +Signed-off-by: Sergei Trofimovich <slyfox@g.o>
61 +---
62 + grub/asmstub.c | 4 ++--
63 + 1 file changed, 2 insertions(+), 2 deletions(-)
64 +
65 +diff --git a/grub/asmstub.c b/grub/asmstub.c
66 +index 6354806..44b056f 100644
67 +--- a/grub/asmstub.c
68 ++++ b/grub/asmstub.c
69 +@@ -174,7 +174,7 @@ grub_stage2 (void)
70 + {
71 + /* Make sure our stack lives in the simulated memory area. */
72 + asm volatile ("movl %%esp, %0\n\tmovl %1, %%esp\n"
73 +- : "=&r" (realstack) : "r" (simstack));
74 ++ : "=&r" (realstack) : "r" (simstack) : "%esp");
75 +
76 + /* Do a setjmp here for the stop command. */
77 + if (! setjmp (env_for_exit))
78 +@@ -191,7 +191,7 @@ grub_stage2 (void)
79 + }
80 +
81 + /* Replace our stack before we use any local variables. */
82 +- asm volatile ("movl %0, %%esp\n" : : "r" (realstack));
83 ++ asm volatile ("movl %0, %%esp\n" : : "r" (realstack) : "%esp");
84 + }
85 +
86 + assert (grub_scratch_mem == 0);
87 +--
88 +2.6.3
89 +
90
91 diff --git a/sys-boot/grub/grub-0.97-r17.ebuild b/sys-boot/grub/grub-0.97-r17.ebuild
92 new file mode 100644
93 index 0000000..3b19b8e
94 --- /dev/null
95 +++ b/sys-boot/grub/grub-0.97-r17.ebuild
96 @@ -0,0 +1,289 @@
97 +# Copyright 1999-2015 Gentoo Foundation
98 +# Distributed under the terms of the GNU General Public License v2
99 +# $Id$
100 +
101 +# XXX: we need to review menu.lst vs grub.conf handling. We've been converting
102 +# all systems to grub.conf (and symlinking menu.lst to grub.conf), but
103 +# we never updated any of the source code (it still all wants menu.lst),
104 +# and there is no indication that upstream is making the transition.
105 +
106 +# If you need to roll a new grub-static distfile, here is how.
107 +# - Robin H. Johnson <robbat2@g.o> - 29 Nov 2010
108 +# FEATURES='-noauto -noinfo -nodoc -noman -splitdebug nostrip' \
109 +# USE='static -ncurses -netboot -custom-cflags' \
110 +# PORTAGE_COMPRESS=true GRUB_STATIC_PACKAGE_BUILDING=1 ebuild \
111 +# grub-${PVR}.ebuild clean package && \
112 +# qtbz2 -s -j ${PKGDIR}/${CAT}/${PF}.tbz2 && \
113 +# mv ${PF}.tar.bz2 ${DISTDIR}/grub-static-${PVR}.tar.bz2
114 +
115 +EAPI="5"
116 +
117 +inherit eutils mount-boot toolchain-funcs linux-info flag-o-matic autotools pax-utils multiprocessing
118 +
119 +PATCHVER="1.14" # Should match the revision ideally
120 +DESCRIPTION="GNU GRUB Legacy boot loader"
121 +HOMEPAGE="https://www.gnu.org/software/grub/"
122 +SRC_URI="mirror://gentoo/${P}.tar.gz
123 + mirror://gnu-alpha/${PN}/${P}.tar.gz
124 + mirror://gentoo/splash.xpm.gz
125 + mirror://gentoo/${P}-patches-${PATCHVER}.tar.bz2"
126 +
127 +LICENSE="GPL-2"
128 +SLOT="0"
129 +KEYWORDS="~amd64 ~x86 ~x86-fbsd"
130 +IUSE="custom-cflags ncurses netboot static"
131 +
132 +LIB_DEPEND="ncurses? ( >=sys-libs/ncurses-5.9-r3:0[static-libs(+),abi_x86_32(-)] )"
133 +RDEPEND="!static? ( ${LIB_DEPEND//[static-libs(+),/=[} )"
134 +DEPEND="${RDEPEND}
135 + static? ( ${LIB_DEPEND} )"
136 +
137 +pkg_setup() {
138 + case $(tc-arch) in
139 + amd64) CONFIG_CHECK='~IA32_EMULATION' check_extra_config ;;
140 + esac
141 +}
142 +
143 +src_prepare() {
144 + # Grub will not handle a kernel larger than EXTENDED_MEMSIZE Mb as
145 + # discovered in bug 160801. We can change this, however, using larger values
146 + # for this variable means that Grub needs more memory to run and boot. For a
147 + # kernel of size N, Grub needs (N+1)*2. Advanced users should set a custom
148 + # value in make.conf, it is possible to make kernels ~16Mb in size, but it
149 + # needs the kitchen sink built-in.
150 + local t="custom"
151 + if [[ -z ${GRUB_MAX_KERNEL_SIZE} ]] ; then
152 + case $(tc-arch) in
153 + amd64) GRUB_MAX_KERNEL_SIZE=9 ;;
154 + x86) GRUB_MAX_KERNEL_SIZE=5 ;;
155 + esac
156 + t="default"
157 + fi
158 + einfo "Grub will support the ${t} maximum kernel size of ${GRUB_MAX_KERNEL_SIZE} Mb (GRUB_MAX_KERNEL_SIZE)"
159 +
160 + sed -i \
161 + -e "/^#define.*EXTENDED_MEMSIZE/s,3,${GRUB_MAX_KERNEL_SIZE},g" \
162 + "${S}"/grub/asmstub.c \
163 + || die
164 +
165 + EPATCH_SUFFIX="patch" epatch "${WORKDIR}"/patch
166 + # bug 564890, 566638
167 + epatch "${FILESDIR}"/grub-0.97-Add-esp-to-list-of-clobbered-registers.patch
168 +
169 + rm -f "${S}"/aclocal.m4 # seems to keep bug 418287 away
170 + eautoreconf
171 +}
172 +
173 +src_configure() {
174 + filter-flags -fPIE #168834
175 +
176 + use amd64 && multilib_toolchain_setup x86
177 +
178 + unset BLOCK_SIZE #73499
179 +
180 + ### i686-specific code in the boot loader is a bad idea; disabling to ensure
181 + ### at least some compatibility if the hard drive is moved to an older or
182 + ### incompatible system.
183 +
184 + # grub-0.95 added -fno-stack-protector detection, to disable ssp for stage2,
185 + # but the objcopy's (faulty) test fails if -fstack-protector is default.
186 + # create a cache telling configure that objcopy is ok, and add -C to econf
187 + # to make use of the cache.
188 + #
189 + # CFLAGS has to be undefined running econf, else -fno-stack-protector detection fails.
190 + # STAGE2_CFLAGS is not allowed to be used on emake command-line, it overwrites
191 + # -fno-stack-protector detected by configure, removed from netboot's emake.
192 + use custom-cflags || unset CFLAGS
193 +
194 + tc-ld-disable-gold #439082 #466536 #526348
195 +
196 + export grub_cv_prog_objcopy_absolute=yes #79734
197 + use static && append-ldflags -static
198 +
199 + if use amd64 && use static ; then
200 + if [[ -n ${GRUB_STATIC_PACKAGE_BUILDING} ]] ; then
201 + eerror "You have set GRUB_STATIC_PACKAGE_BUILDING. This"
202 + eerror "is specifically intended for building the tarballs for the"
203 + eerror "grub-static package via USE='static -ncurses'."
204 + eerror "All bets are now off."
205 + fi
206 + fi
207 +
208 + multijob_init
209 +
210 + # build the net-bootable grub first, but only if "netboot" is set
211 + if use netboot ; then
212 + (
213 + multijob_child_init
214 + mkdir -p "${WORKDIR}"/netboot
215 + pushd "${WORKDIR}"/netboot >/dev/null
216 + ECONF_SOURCE=${S} \
217 + econf \
218 + --libdir=/lib \
219 + --datadir=/usr/lib/grub \
220 + --exec-prefix=/ \
221 + --disable-auto-linux-mem-opt \
222 + --enable-diskless \
223 + --enable-{3c{5{03,07,09,29,95},90x},cs89x0,davicom,depca,eepro{,100}} \
224 + --enable-{epic100,exos205,ni5210,lance,ne2100,ni{50,65}10,natsemi} \
225 + --enable-{ne,ns8390,wd,otulip,rtl8139,sis900,sk-g16,smc9000,tiara} \
226 + --enable-{tulip,via-rhine,w89c840}
227 + popd >/dev/null
228 + ) &
229 + multijob_post_fork
230 + fi
231 +
232 + # Now build the regular grub
233 + # Note that FFS and UFS2 support are broken for now - stage1_5 files too big
234 + econf \
235 + --libdir=/lib \
236 + --datadir=/usr/lib/grub \
237 + --exec-prefix=/ \
238 + --disable-auto-linux-mem-opt \
239 + $(use_with ncurses curses)
240 +
241 + # sanity check due to common failure
242 + use ncurses && ! grep -qs "HAVE_LIBCURSES.*1" config.h && die "USE=ncurses but curses not found"
243 +
244 + multijob_finish
245 +}
246 +
247 +src_compile() {
248 + use netboot && emake -C "${WORKDIR}"/netboot w89c840_o_CFLAGS="-O"
249 + emake
250 +}
251 +
252 +src_test() {
253 + # non-default block size also give false pass/fails.
254 + unset BLOCK_SIZE
255 + emake -j1 check
256 +}
257 +
258 +src_install() {
259 + default
260 + if use netboot ; then
261 + exeinto /usr/lib/grub/${CHOST}
262 + doexe "${WORKDIR}"/netboot/stage2/{nbgrub,pxegrub}
263 + newexe "${WORKDIR}"/netboot/stage2/stage2 stage2.netboot
264 + fi
265 +
266 + pax-mark -m "${D}"/sbin/grub #330745
267 +
268 + newdoc docs/menu.lst grub.conf.sample
269 + dodoc "${FILESDIR}"/grub.conf.gentoo
270 +
271 + [[ -n ${GRUB_STATIC_PACKAGE_BUILDING} ]] && \
272 + mv "${D}"/usr/share/doc/{${PF},grub-static-${PF/grub-}}
273 +
274 + insinto /usr/share/grub
275 + doins "${DISTDIR}"/splash.xpm.gz
276 +}
277 +
278 +setup_boot_dir() {
279 + local boot_dir=$1
280 + local dir=${boot_dir}
281 +
282 + mkdir -p "${dir}"
283 + [[ ! -L ${dir}/boot ]] && ln -s . "${dir}/boot"
284 + dir="${dir}/grub"
285 + if [[ ! -e ${dir} ]] ; then
286 + mkdir "${dir}" || die
287 + fi
288 +
289 + # change menu.lst to grub.conf
290 + if [[ ! -e ${dir}/grub.conf ]] && [[ -e ${dir}/menu.lst ]] ; then
291 + mv -f "${dir}"/menu.lst "${dir}"/grub.conf
292 + ewarn "*** IMPORTANT NOTE: menu.lst has been renamed to grub.conf"
293 + echo
294 + fi
295 +
296 + if [[ ! -e ${dir}/menu.lst ]]; then
297 + einfo "Linking from new grub.conf name to menu.lst"
298 + ln -snf grub.conf "${dir}"/menu.lst
299 + fi
300 +
301 + if [[ -e ${dir}/stage2 ]] ; then
302 + mv "${dir}"/stage2{,.old}
303 + ewarn "*** IMPORTANT NOTE: you must run grub and install"
304 + ewarn "the new version's stage1 to your MBR. Until you do,"
305 + ewarn "stage1 and stage2 will still be the old version, but"
306 + ewarn "later stages will be the new version, which could"
307 + ewarn "cause problems such as an unbootable system."
308 + ewarn
309 + ewarn "This means you must use either grub-install or perform"
310 + ewarn "root/setup manually."
311 + ewarn
312 + ewarn "For more help, see the handbook:"
313 + ewarn "https://www.gentoo.org/doc/en/handbook/handbook-${ARCH}.xml?part=1&chap=10#grub-install-auto"
314 + echo
315 + fi
316 +
317 + einfo "Copying files from /lib/grub and /usr/share/grub to ${dir}"
318 + for x in \
319 + "${ROOT}"/lib*/grub/*/* \
320 + "${ROOT}"/usr/share/grub/* ; do
321 + [[ -f ${x} ]] && cp -p "${x}" "${dir}"/
322 + done
323 +
324 + if [[ ! -e ${dir}/grub.conf ]] ; then
325 + s="${ROOT}/usr/share/doc/${PF}/grub.conf.gentoo"
326 + [[ -e "${s}" ]] && cat "${s}" >${dir}/grub.conf
327 + [[ -e "${s}.gz" ]] && zcat "${s}.gz" >${dir}/grub.conf
328 + [[ -e "${s}.bz2" ]] && bzcat "${s}.bz2" >${dir}/grub.conf
329 + fi
330 +
331 + # Per bug 218599, we support grub.conf.install for users that want to run a
332 + # specific set of Grub setup commands rather than the default ones.
333 + grub_config=${dir}/grub.conf.install
334 + [[ -e ${grub_config} ]] || grub_config=${dir}/grub.conf
335 + if [[ -e ${grub_config} ]] ; then
336 + egrep \
337 + -v '^[[:space:]]*(#|$|default|fallback|initrd|password|splashimage|timeout|title)' \
338 + "${grub_config}" | \
339 + /sbin/grub --batch \
340 + --device-map="${dir}"/device.map \
341 + > /dev/null
342 + fi
343 +
344 + # the grub default commands silently piss themselves if
345 + # the default file does not exist ahead of time
346 + if [[ ! -e ${dir}/default ]] ; then
347 + grub-set-default --root-directory="${boot_dir}" default
348 + fi
349 + einfo "Grub has been installed to ${boot_dir} successfully."
350 +}
351 +
352 +pkg_postinst() {
353 + mount-boot_mount_boot_partition
354 +
355 + if [[ -n ${DONT_MOUNT_BOOT} ]]; then
356 + elog "WARNING: you have DONT_MOUNT_BOOT in effect, so you must apply"
357 + elog "the following instructions for your /boot!"
358 + elog "Neglecting to do so may cause your system to fail to boot!"
359 + elog
360 + else
361 + setup_boot_dir "${ROOT}"/boot
362 + # Trailing output because if this is run from pkg_postinst, it gets mixed into
363 + # the other output.
364 + einfo ""
365 + fi
366 + elog "To interactively install grub files to another device such as a USB"
367 + elog "stick, just run the following and specify the directory as prompted:"
368 + elog " emerge --config =${PF}"
369 + elog "Alternately, you can export GRUB_ALT_INSTALLDIR=/path/to/use to tell"
370 + elog "grub where to install in a non-interactive way."
371 +
372 + # needs to be after we call setup_boot_dir
373 + mount-boot_pkg_postinst
374 +}
375 +
376 +pkg_config() {
377 + local dir
378 + if [ ! -d "${GRUB_ALT_INSTALLDIR}" ]; then
379 + einfo "Enter the directory where you want to setup grub:"
380 + read dir
381 + else
382 + dir="${GRUB_ALT_INSTALLDIR}"
383 + fi
384 + setup_boot_dir "${dir}"
385 +}