summaryrefslogtreecommitdiff
path: root/bootstrap/stage3.sh
blob: 567a4ad197a507a9c663158f3d2e3781010f6821 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash

clean_build () {
    rm -rf ${chroot}/build
    mkdir ${chroot}/build
}

build_headers () {
    src "https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-$LINUX_VER.tar.xz"
    make mrproper

    ptch "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/kernel/include-uapi-linux-swab-Fix-potentially-missing-__always_inline.patch"

    cat > ${chroot}/build/build.sh << "EOF"
#!/bin/bash
cd /build/
cp -r */* .

make headers
mkdir /usr/include
cp -r usr/include/* /usr/include
find /usr/include -name '.*' -exec rm -f {} \; 
rm /usr/include/Makefile
EOF
    chmod +x ${chroot}/build/build.sh
    tchroot  /build/build.sh
}

build_musl () {
    src "https://musl.libc.org/releases/musl-$MUSL_VER.tar.gz"
    ptch "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/musl-mlfs/fix-utmp-wtmp-paths.patch"
    ptch "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/musl-mlfs/change-scheduler-functions-Linux-compatib.patch"
    ptch "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/musl-alpine/0001-riscv64-define-ELF_NFPREG.patch"
    ptch "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/musl-alpine/handle-aux-at_base.patch
"
    ptch "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/patches/musl-alpine/syscall-cp-epoll.patch"
    curl "https://raw.githubusercontent.com/dslm4515/Musl-LFS/master/files/__stack_chk_fail_local.c" > __stack_chk_fail_local.c

    cat > ${chroot}/build/build.sh << "EOF"
#!/bin/bash
cd /build/
cp -r */* .
LDFLAGS="$LDFLAGS -Wl,-soname,libc.musl-${CARCH}.so.1" \
./configure --prefix=/usr \
        --sysconfdir=/etc \
        --localstatedir=/var \
        --disable-gcc-wrapper

make && make  install


/tools/bin/x86_64-linux-musl-gcc -fpie -c __stack_chk_fail_local.c -o __stack_chk_fail_local.o
/tools/bin/x86_64-linux-musl-gcc-ar r libssp_nonshared.a __stack_chk_fail_local.o

cp libssp_nonshared.a /usr/lib/

export ARCH="x86_64"

ln -s /lib/ld-musl-$ARCH.so.1 /bin/ldd
EOF
    chmod +x ${chroot}/build/build.sh
    tchroot /build/build.sh
}

adjust_tools() {
    
    cat > ${chroot}/build/build.sh << "EOF"
#!/bin/bash
export TARGET="x86_64-linux-musl"
mv /tools/bin/{ld,ld-old}
mv /tools/${TARGET}/bin/{ld,ld-old}
mv /tools/bin/{ld-new,ld}
ln -s /tools/bin/ld /tools/${TARGET}/bin/ld

export SPECFILE=`dirname $(gcc -print-libgcc-file-name)`/specs
gcc -dumpspecs | sed -e 's@/tools@@g'                   \
    -e '/\*startfile_prefix_spec:/{n;s@.*@/usr/lib/ @}' \
    -e '/\*cpp:/{n;s@$@ -isystem /usr/include@}' >  tempspecfile
mv -f tempspecfile $SPECFILE &&
unset SPECFILE  TARGET

echo 'int main(){}' > dummy.c
cc dummy.c -v -Wl,--verbose > dummy.log 2>&1 
readelf -l a.out | grep ': /lib'
printf "above should be:\n\033[0;33m[Requesting program interpreter: /lib/ld-musl-x86_64.so.1]\033[0m\n"
printf "######################\n"
read wait

grep -o '/usr/lib.*/crt[1in].*succeeded' dummy.log
printf "above should be:\033[0;33m\n"
printf "/usr/lib/crt1.o succeeded\n"
printf "/usr/lib/crti.o succeeded\n"
printf "/usr/lib/crtn.o succeeded\n"
printf "\033[0m\n"
printf "######################\n"
read wait


grep -B1 '^ /usr/include' dummy.log
printf "above should be:\033[0;33m\n"
printf "#include <...> search starts here:\n"
printf "/usr/include\n"
printf "\033[0m\n"
printf "######################\n"
read wait

grep 'SEARCH.*/usr/lib' dummy.log |sed 's|; |\n|g'
printf "above should be:\033[0;33m\n"
printf "SEARCH_DIR(\"=/tools/x86_64-mlfs-linux-musl/lib64\")\n"
printf "SEARCH_DIR(\"/usr/lib\")\n"
printf "SEARCH_DIR(\"/lib\")\n"
printf "SEARCH_DIR(\"=/tools/x86_64-mlfs-linux-musl/lib\")\n"
printf "\033[0m\n"
printf "######################\n"
read wait
rm dummy.c a.out dummy.log
EOF
    chmod +x ${chroot}/build/build.sh
    tchroot /build/build.sh
}

umount_chroot
rm -r ${chroot}
create_chroot
mount_chroot
export WD=${chroot}/build
clean_build
build_headers
clean_build
build_musl
clean_build
adjust_tools
clean_build

printf "${GREEN}Completed stage3\n"