Comments (46)
It looks like a bug appeared after release of binutils 2.36.1. See https://www.linuxfromscratch.org/lfs/view/stable/chapter08/grub.html , maybe the fix of issue in the beginning of this step will be useful.
from cmlfs.
Prefixing
configure
withCC=gcc.gnu CXX=g++.gnu AR=llvm-ar AS=llvm-as
yielded the same wrong file sizes
Well, we have to use dear old GCC+Binutils toolchain for GRUB2 again. At least, we tried another way. I just needed to recognize if this error appears again. GRUB 2.06 release promised that it could be built correctly with clang 10. We tried clang 12. It failed. For now, at least.
P.S. When I will build grub 2.06, I will tell you how I would have been built it and what kind of errors I have got.
from cmlfs.
Oops. I did: GCC+binutils also generated wrong files sizes.
My progress in building of final system is 34-flex for now. But I have found very small mistake on this step. HELPMAN equals to /tools/bin/true in configure script, but in CMLFS we don't use /tools, do we? We use /llvmtools. And HELPMAN should be equal to /llvmtools/bin/true. When I will build 72-grub, I would tell about that what I would find.
from cmlfs.
Hmmm... I am working another project using MLFS as base and using optimizations from Clear Linux. I just finished building GCC (version 11.2.0!) and binutils (version 2.37) for the final system. I will see when I get to GRUB, if the boot images are still bloated or not.
Okay. I used binutils 2.37, too. The size of diskboot.img should be 512 bytes, not kbytes, due to the first sector size
from cmlfs.
Well, I found the сause of bloated images. This is linker. When we use ld.lld, it gives 23 additional bytes to each *.img and bloats them. I have found it when I forgot to change link /usr/bin/ld from ld.lld to ld.bfd and built grub with GCC. All of *.img files were bloated.
from cmlfs.
ld.gold can also be used as linker for correct build of grub. All of images are unbloated. kernel.img is bigger than built with ld.bfd, but other *.img files have the same sizes like they was built with ld.bfd.
from cmlfs.
I'll try that and check the those images
from cmlfs.
I'll try that and check the those images
Try to use binutils LD instead of LLVM ld.lld if images wouldn't be correct
from cmlfs.
Try to use binutils LD instead of LLVM ld.lld if images wouldn't be correct
Good point. I haven't tried that. Will do first
from cmlfs.
Try to use binutils LD instead of LLVM ld.lld if images wouldn't be correct
Good point. I haven't tried that. Will do first
Fix an issue with binutils first, if you would do this.
from cmlfs.
Prefixing configure
with CC=gcc.gnu CXX=g++.gnu AR=llvm-ar AS=llvm-as
yielded the same wrong file sizes
from cmlfs.
I also tried compiling with ld.bfd
(binutils) and same issue.
I'll try a patch for binutils
from cmlfs.
My current CMLFS build is llvm12 with binutils and GCC in /usr
. I suffixed GCC binaries with .gnu
and made links to Clang.
So I tried LLVM12, LLVM12+Binutils,GCC+LLVM12-binary-tools... I think I forgot to try GCC+binutils.
from cmlfs.
Oops. I did: GCC+binutils also generated wrong files sizes.
from cmlfs.
I forgot to mention, building grub with clang as compiler...it yields an error when configuring:
none of __bb_start, edata, or _edata is defined [by the compiler]
from cmlfs.
I did check Alpine Linux and Void Linux... I didnt see any patches or fixes
from cmlfs.
Well, I have built and installed GRUB 2.06. It builds fine, installation works fine as well. Generation of config fails due to disabled os-prober. I have built my GRUB for BIOS, so you could get bug with EFI.
During the build I used scripts for changing of toolchain mode:
/llvmtools/bin/set-gcc-mode
/llvmtools/bin/set-gnu-bin-mode
/usr/bin/set-gnu-bin-mode
/usr/bin/set-gcc-mode
and used fix of issue from LFS grub 2.04 built recipe placed after applying of patches and before executing of autogen.sh:
sed "s/gold-version/& -R .note.gnu.property/" \
-i Makefile.in grub-core/Makefile.in
At the very first time, when I used current way configure decided that gcc could not produce executables, so I decided to make a new gcc. But when I switched to gnu toolchain and begin building of new gcc, I decided to stop this madness and build GRUB. GRUB built fine, but os-prober doesn't work.
When I tried to use clang toolchain, configure fails due to absense of some symbols in compiler. GRUB 2.06 should build with clang, but doesn't do it.
from cmlfs.
Hmmm... I am working another project using MLFS as base and using optimizations from Clear Linux. I just finished building GCC (version 11.2.0!) and binutils (version 2.37) for the final system. I will see when I get to GRUB, if the boot images are still bloated or not.
from cmlfs.
There is an error when we build GRUB 2.06 with clang:
from cmlfs.
Yes, i did notice that last time i built grub with clang
from cmlfs.
Yes, i did notice that last time i built grub with clang
I found the way to surpass this error. Create config.cache with cached yes for __bss_start and some others variables:
cat > config.cache << "EOF"
grub_cv_check_uscore_uscore_bss_start_symbol=yes
grub_cv_check_edata_symbol=yes (should be no)
grub_cv_check_uscore_edata_symbol=yes (should be no)
grub_cv_check_end_symbol=yes
grub_cv_check_uscore_end_symbol=yes (should be no)
EOF
This wouldn't be enough. I will try to build grub 2.06 now.
P.S. Added all of options for config.cache that I used for build of grub-2.06. All of these symbols are present in LLVM LLD, but configure doesn't find it. So we must take one of symbols from _bss_start, edata and _edata, and one of end and _end. But not all of them
from cmlfs.
I have successfully built GRUB 2.06 with clang, installed it to system, and successfully installed bootloader.
Process of build:
- Apply patches:
patch -Np1 -i ../patches/grub-alpine/lang-C.UTF-8.patch && \
patch -Np1 -i ../patches/grub-alpine/fix-gcc-no-pie-specs.patch && \
patch -Np1 -i ../patches/grub-alpine/alpine-mkconfig.patch
- Fix issue from Grub installation from LFS 10.1:
sed "s/gold-version/& -R .note.gnu.property/" \
-i Makefile.in grub-core/Makefile.in
- Re-generate Configure script:
PYTHON=python3 ./autogen.sh
- Create config.cache for surpassing broken tests of symbols __bss_start, edata, _edata, end, _end. I put there that symbols that I used during the build. You can try all yes variant, but if you do so, compilation may fail when compilator will detect include/grub/symbol.h due to that fact it could not define HAVE_ASM_USCORE, which should be defined with __bss_start, edata or _edata symbol (all of them are defined in LLD since 2017):
cat > config.cache << "EOF"
grub_cv_check_uscore_uscore_bss_start_symbol=yes
grub_cv_check_edata_symbol=yes
grub_cv_check_uscore_edata_symbol=no
grub_cv_check_end_symbol=yes
grub_cv_check_uscore_end_symbol=no
EOF
- Configure GRUB 2.06 for compilation. Due to the fact my PC is very old and uses BIOS, I don't need EFI. Also, I tried to use x86_64-pc-musl-linux as target:
CC=clang CXX=clang++ \
CFLAGS="-Wno-error" \
./configure --prefix=/usr \
--target=x86_64-pc-linux-musl \
--sbindir=/sbin \
--sysconfdir=/etc \
--disable-efiemu \
--disable-werror \
--cache-file=config.cache
- Modify project root Makefile and grub-core/Makefile and add --no-pie option to LDFLAGS_MODULE, LDFLAGS_IMAGE, TARGET_IMG_LDFLAGS and TARGET_LDFLAGS_OLDMAGIC. -r linker option must be added to LDFLAGS_IMAGE as well, otherwise you will get bloated images:
LDFLAGS_MODULE and LDFLAGS_IMAGE were:
...
CFLAGS_MODULE = $(CFLAGS_PLATFORM) -ffreestanding
LDFLAGS_MODULE = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC) -Wl,-r,-d
CPPFLAGS_MODULE = $(CPPFLAGS_CPU) $(CPPFLAGS_PLATFORM)
CCASFLAGS_MODULE = $(CCASFLAGS_CPU) $(CCASFLAGS_PLATFORM)
CFLAGS_IMAGE = $(CFLAGS_PLATFORM) -fno-builtin
LDFLAGS_IMAGE = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC) -Wl,-S
CPPFLAGS_IMAGE = $(CPPFLAGS_CPU) $(CPPFLAGS_PLATFORM)
...
And become:
...
CFLAGS_MODULE = $(CFLAGS_PLATFORM) -ffreestanding
LDFLAGS_MODULE = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC) -Wl,-r,--no-pie,-d
CPPFLAGS_MODULE = $(CPPFLAGS_CPU) $(CPPFLAGS_PLATFORM)
CCASFLAGS_MODULE = $(CCASFLAGS_CPU) $(CCASFLAGS_PLATFORM)
CFLAGS_IMAGE = $(CFLAGS_PLATFORM) -fno-builtin
LDFLAGS_IMAGE = $(LDFLAGS_PLATFORM) -nostdlib $(TARGET_LDFLAGS_OLDMAGIC) -Wl,-r,--no-pie,-S
CPPFLAGS_IMAGE = $(CPPFLAGS_CPU) $(CPPFLAGS_PLATFORM)
...
TARGET_IMG_LDFLAGS and TARGET_LDFLAGS_OLDMAGIC were:
...
TARGET_IMG_CFLAGS =
TARGET_IMG_LDFLAGS = -Wl,-N
TARGET_LDFLAGS = -m32 -Wl,-melf_i386 -Wl,--build-id=none
TARGET_LDFLAGS_OLDMAGIC = -Wl,-N
TARGET_LINK_ADDR =
...
and become:
...
TARGET_IMG_CFLAGS =
TARGET_IMG_LDFLAGS = -Wl,-N,--no-pie
TARGET_LDFLAGS = -m32 -Wl,-melf_i386 -Wl,--build-id=none
TARGET_LDFLAGS_OLDMAGIC = -Wl,-N,--no-pie
TARGET_LINK_ADDR =
...
- Make project.
make
make install
P.S. That's why you must add -r to LDFLAGS_IMAGE:
from cmlfs.
Well, clang-built grub doesn't work. Maybe because of e2fsprogs isn't installed
from cmlfs.
Every of these .img files has additional 23 bytes. For what reason they were added?
from cmlfs.
So if GRUB was built with ld.bfd, no bloat?
from cmlfs.
So if GRUB was built with ld.bfd, no bloat?
Yes.
from cmlfs.
So if GRUB was built with ld.bfd, no bloat?
I have tried to reboot with clang-built grub and gcc-built images. Image loaded well, I've got "Welcome to GRUB!", but after that I have got black screen. Therefore, modules must be gcc-built as well. I will try to build grub 2.06 with clang and ld.bfd. And after that I will report about results.
from cmlfs.
Well, I have copied all of *.img, *.module and *.mod files from gcc-built grub to clang-built grub. After reinstallation of grub in mbr, I successfully logged in my ubuntu 21.04 via CMLFS GRUB. I did not try build with clang and ld.bfd for now. I will do this now.
from cmlfs.
I cannot remember if i was able to build GRUB with Clang+LLD and just copied the [unbloated] boot images from an older build (from MLFS). Or if I had to build GRUB with GCC+Binutils with unbloated images from an old MLFS build
from cmlfs.
I cannot remember if i was able to build GRUB with Clang+LLD and just copied the [unbloated] boot images from an older build (from MLFS). Or if I had to build GRUB with GCC+Binutils with unbloated images from an old MLFS build
Make two builds: one built with clang, one built with gcc. All .img, .module and .mod files in clang will be broken. Copy these file from gcc build and it will work
from cmlfs.
Make two builds: one built with clang, one built with gcc. All .img, .module and .mod files in clang will be broken. Copy these file from gcc build and it will work
Most likely I will update the built and use that method.
from cmlfs.
So if GRUB was built with ld.bfd, no bloat?
I have just built and installed clang+ld.bfd built GRUB 2.06. It works.
from cmlfs.
072-grub recipe should be:
# Final System: Grub
# Source: https://ftp.gnu.org/gnu/grub/grub-2.06.tar.xz
# This section is done in Chroot environment
# Grub has to be compiled with GCC and binutils, not LLVM - Ha! Nu tupye
# Remove ld link to ld.lld and change it ld.bfd
rm -v /usr/bin/ld
ln -svf ld.bfd /usr/bin/ld
# Apply fixes
patch -Np1 -i ../patches/grub-alpine/lang-C.UTF-8.patch && \
patch -Np1 -i ../patches/grub-alpine/fix-gcc-no-pie-specs.patch && \
patch -Np1 -i ../patches/grub-alpine/alpine-mkconfig.patch
# Fix issue
sed "s/gold-version/& -R .note.gnu.property/" \
-i Makefile.in grub-core/Makefile.in
# Re-Generate configure script
PYTHON=python3 ./autogen.sh
# Create config.cache:
cat > config.cache << "EOF"
grub_cv_check_uscore_uscore_bss_start_symbol=yes
grub_cv_check_edata_symbol=yes
grub_cv_check_uscore_edata_symbol=no
grub_cv_check_end_symbol=yes
grub_cv_check_uscore_end_symbol=no
EOF
# for BIOS-based PCs:
CC=clang \
CXX=clang++ \
CFLAGS="-Wno-error --ld-path=/usr/bin/ld.bfd" \
CXXFLAGS="--ld-path=/usr/bin/ld.bfd" \
LD=ld.bfd \
./configure --prefix=/usr \
--sbindir=/sbin \
--sysconfdir=/etc \
--disable-efiemu \
--disable-werror \
--cache-file=config.cache
# for UEFI-based PCs:
# Configure scripts expect x86_64-gcc.
# Now configure
CC=clang \
CXX=clang++ \
CFLAGS="-Wno-error --ld-path=/usr/bin/ld.bfd" \
CXXFLAGS="--ld-path=/usr/bin/ld.bfd" \
LD=ld.bfd \
./configure --prefix=/usr \
--target=x86_64 \
--with-platform=efi \
--sbindir=/sbin \
--sysconfdir=/etc \
--disable-werror \
--cache-file=config.cache
# Modify project root Makefile and grub-core Makefile
sed -i 's/TARGET_IMG_LDFLAGS = -Wl,-N/TARGET_IMG_LDFLAGS = -Wl,-N,--no-pie/' Makefile
sed -i 's/TARGET_LDFLAGS_OLDMAGIC = -Wl,-N/TARGET_LDFLAGS_OLDMAGIC = -Wl,-N,--no-pie/' Makefile
sed -i 's/TARGET_IMG_LDFLAGS = -Wl,-N/TARGET_IMG_LDFLAGS = -Wl,-N,--no-pie/' grub-core/Makefile
sed -i 's/TARGET_LDFLAGS_OLDMAGIC = -Wl,-N/TARGET_LDFLAGS_OLDMAGIC = -Wl,-N,--no-pie/' grub-core/Makefile
# Make & install grub
make
make install
# Restore old linker
rm /usr/bin/ld
ln -svf ld.lld /usr/bin/ld
Check if I am right or not.
from cmlfs.
ld.gold can also be used as linker for correct build of grub. All of images are unbloated. kernel.img is bigger than built with ld.bfd, but other *.img files have the same sizes like they was built with ld.bfd.
Oops. I have got error with ld.gold.
ld.bfd must be appropriated.
from cmlfs.
So in short, GRUB still needs to be built with GCC + Binutils's ld.bfd, right?
Would be nice to clang-build GRUB but then use GCC+ld.bfd for the boot images.
from cmlfs.
So in short, GRUB still needs to be built with GCC + Binutils's ld.bfd, right?
Would be nice to clang-build GRUB but then use GCC+ld.bfd for the boot images.
In short, you should use clang+ld.bfd.
from cmlfs.
I think that besides GRUB being ubiquitous in modern UNIX, it would be interesting to have an alternative too for ones who don't want to make workarounds.
I know LILO/ELILO, but there's little to nothing documentation on how to cross-build them on a musl chroot.
from cmlfs.
In short, you should use clang+ld.bfd.
Chimera Linux uses clang, libc++, LLD, and compiler-rt as defaults like CMLFS. So I checked how grub is compiled via their cports repo.
Looks like grub is built with LLVM without binutils! There are patches, so I will try and see
from cmlfs.
Even with patches from Chimera linux, configure tests for symbols __bss_start, edata, _edata, end, _end still fail. Therefore a config.cache has to be used as mentioned earlier:
cat > config.cache << "EOF"
grub_cv_check_uscore_uscore_bss_start_symbol=yes
grub_cv_check_edata_symbol=yes
grub_cv_check_uscore_edata_symbol=no
grub_cv_check_end_symbol=yes
grub_cv_check_uscore_end_symbol=no
EOF
Configure script executes with no errors. But when compiling, build breaks. To get the build to compile, modify the makefiles, as mentioned before, right after running configure
:
sed -i 's/TARGET_IMG_LDFLAGS = -Wl,-N/TARGET_IMG_LDFLAGS = -Wl,-N,--no-pie/' Makefile
sed -i 's/TARGET_LDFLAGS_OLDMAGIC = -Wl,-N/TARGET_LDFLAGS_OLDMAGIC = -Wl,-N,--no-pie/' Makefile
sed -i 's/TARGET_IMG_LDFLAGS = -Wl,-N/TARGET_IMG_LDFLAGS = -Wl,-N,--no-pie/' grub-core/Makefile
sed -i 's/TARGET_LDFLAGS_OLDMAGIC = -Wl,-N/TARGET_LDFLAGS_OLDMAGIC = -Wl,-N,--no-pie/' grub-core/Makefile
Build completes without error. For the i386-pc build, the images look fine. Not exactly the same size as mentioned earlier:
$ ls -l /BUILD/usr/lib/grub/i386-pc/*.img
-rw-r--r-- 1 root root 512 Feb 2 21:59 /BUILD/usr/lib/grub/i386-pc/boot.img
-rw-r--r-- 1 root root 512 Feb 2 21:59 /BUILD/usr/lib/grub/i386-pc/boot_hybrid.img
-rw-r--r-- 1 root root 2048 Feb 2 21:59 /BUILD/usr/lib/grub/i386-pc/cdboot.img
-rw-r--r-- 1 root root 512 Feb 2 21:59 /BUILD/usr/lib/grub/i386-pc/diskboot.img
-rw-r--r-- 1 root root 34976 Feb 2 21:59 /BUILD/usr/lib/grub/i386-pc/kernel.img
-rw-r--r-- 1 root root 1024 Feb 2 21:59 /BUILD/usr/lib/grub/i386-pc/lnxboot.img
-rw-r--r-- 1 root root 3472 Feb 2 21:59 /BUILD/usr/lib/grub/i386-pc/lzma_decompress.img
-rw-r--r-- 1 root root 1024 Feb 2 21:59 /BUILD/usr/lib/grub/i386-pc/pxeboot.img
This was built using LLVM-15.0.6 without binutils... clang+LLD
from cmlfs.
So, with patches from Chimera Linux, you don't have any bloats anymore?
That's wonderful.
You can make pull request. Modify recipe, it can be used for the project
from cmlfs.
Did you check work capability of brand new images? If not, you should check their capability for correct work
from cmlfs.
You can make pull request. Modify recipe, it can be used for the project
It's actually a local commit that I havent uploaded yet for the bsd-userland branch
Did you check work capability of brand new images? If not, you should check its capability
I'm almost done with my build. I got a small BIOS-based PC i can test on.
from cmlfs.
OK, good luck with checking!
from cmlfs.
So far grub-install /dev/sda
reports no errors!
Next, let's boot with grub...
Edit: Yes! Boots grub!
from cmlfs.
Clang+elftoolchain cannot build grub:
grub_script.yy.c:1077:35: error: expected ';' after expression
if ( yy_current_state >= 188 )
^
;
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make[2]: *** [Makefile:8095: libgrubmods_a-grub_script.yy.o] Error 1
make[2]: Leaving directory '/sources/grub-2.06'
make[1]: *** [Makefile:11514: all-recursive] Error 1
make[1]: Leaving directory '/sources/grub-2.06'
make: *** [Makefile:3372: all] Error 2
Do note, my CMLFS build does not have bison... i replaced it with byacc.
I will attempt it again with bison installed
from cmlfs.
If I build bison and install it in /opt/gnu
and add /opt/gnu/bin
to path, LLVM+elftools+bison compile GRUB without errors!
Next, lets see if it GRUB can boot...
from cmlfs.
Related Issues (20)
- References to `systemctl` during boot HOT 2
- utmps utmpx.h already includes utmps/utmpx.h HOT 4
- Broken stage2 (final system) clang+LLVM HOT 3
- Could we also have Heirloom NG as an alternative to GNU Coreutils in the future? HOT 63
- Python3 need to be rebuilt after final system clang is built HOT 8
- Clarity on CMLFS_TARGET at 1-cgnutools/4-musl HOT 2
- Compiler Warning: "-fuse-ld=' taking a path is deprecated"
- LLVM Compilation Failed HOT 1
- Unable to Compile Kernel HOT 25
- What are the packages that cannot be build without GCC? HOT 4
- Fail at Stage-0 (1-Mussel) HOT 6
- What is the state of CMLFS methods for cross-compiling? HOT 34
- Patch for musl's utmp.h is incomplete HOT 5
- OpenSSL needs certificates HOT 1
- LLVM-17.0.5: Stage 2 Clang has sysroot set as /llvmtools HOT 11
- Udev [from systemd] wont configure for final system; expects util-linux installed HOT 2
- Mention of `systemd-hwdb` when executing `udevadm` HOT 1
- Drop GNU's cpio for bsdcpio [provided by libarchive] HOT 14
- Autotools Broken -- `autoreconf` runs but exits with errors HOT 1
- Binutils: libctf fails to compile HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cmlfs.