-
Notifications
You must be signed in to change notification settings - Fork 0
/
elf-tools.sh
472 lines (377 loc) · 13.2 KB
/
elf-tools.sh
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#!/bin/bash
# elf-tools.sh
# v1.4.1
# Define Global Variables
BINUTILS_VERSION=2.42
GCC_VERSION=14.1.0
GDB_VERSION=13.2
BUILD_TARGET="i686-elf"
set -e
ALL_PRODUCTS=true
# Parse Commandline Options
if [ $# -eq 0 ]; then
BUILD_BINUTILS=true
BUILD_GCC=true
BUILD_GDB=true
ZIP=true
PARALLEL=true
args="binutils gcc gdb zip"
else
args=$@
fi
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
binutils) BUILD_BINUTILS=true; ALL_PRODUCTS=false; shift ;;
gcc) BUILD_GCC=true; ALL_PRODUCTS=false; shift ;;
gdb) BUILD_GDB=true; ALL_PRODUCTS=false; shift ;;
win) WINDOWS_ONLY=true; shift ;;
linux) LINUX_ONLY=true; shift ;;
zip) ZIP=true; ALL_PRODUCTS=false; shift ;;
env) ENV_ONLY=true; shift ;;
-64) x64=true; shift ;;
-parallel) PARALLEL=true; shift ;;
-bv|--binutils-version) BINUTILS_VERSION="$2"; shift; shift ;;
-gv|--gcc-version) GCC_VERSION="$2"; shift; shift ;;
-dv|--gdb-version) GDB_VERSION="$2"; shift; shift ;;
*) shift ;;
esac
done
if [[ $x64 == true ]]; then
BUILD_TARGET="x86_64-elf"
fi
BUILD_DIR="$HOME/build-${BUILD_TARGET}"
export PATH="/opt/mxe/usr/bin:$BUILD_DIR/linux/output/bin:$BUILD_DIR/windows/output/bin:$PATH"
echo "BUILD_TARGET = ${BUILD_TARGET}"
echo "BUILD_DIR = ${BUILD_DIR}"
echo "BUILD_BINUTILS = ${BUILD_BINUTILS}"
echo "BUILD_GCC = ${BUILD_GCC}"
echo "BUILD_GDB = ${BUILD_GDB}"
echo "ZIP = ${ZIP}"
echo "WIN = ${WINDOWS_ONLY}"
echo "LINUX = ${LINUX_ONLY}"
echo "ENV = ${ENV_ONLY}"
echo "x64 = ${x64}"
echo "BINUTILS_VERSION = ${BINUTILS_VERSION}"
echo "GCC_VERSION = ${GCC_VERSION}"
echo "GDB_VERSION = ${GDB_VERSION}"
echo "PATH = ${PATH}"
echo "PARALLEL = ${PARALLEL}"
function main {
installPackages
installMXE
if [[ $ENV_ONLY == true ]]; then
echoColor "Successfully installed build environment. Exiting as 'env' only was specified"
return
fi
downloadSources
if [[ $WINDOWS_ONLY == true ]]; then
echoColor "Skipping compiling Linux as 'win' was specified in commandline args '$args'"
else
compileAll "linux"
fi
if [[ $LINUX_ONLY == true ]]; then
echoColor "Skipping compiling Windows as 'linux' was specified in commandline args '$args'"
else
compileAll "windows"
fi
finalize
}
function installPackages {
pkgList=(
git
autoconf
automake
autopoint
bash
bison
bzip2
flex
gettext
g++
gperf
intltool
libffi-dev
libgdk-pixbuf2.0-dev
libgmp-dev
libtool
libltdl-dev
libssl-dev
libxml-parser-perl
make
python3-mako
openssl
p7zip-full
patch
perl
pkg-config
ruby
scons
sed
unzip
wget
xz-utils
libtool-bin
texinfo
g++-multilib
lzip)
echoColor "Installing packages"
# Fix correct python packages on modern Ubuntu versions
if [[ $(lsb_release -a) =~ .*"Ubuntu".*$ ]]; then
pkgList+=(python3 python-is-python3)
else
pkgList+=(python)
fi
for pkg in ${pkgList[@]}; do
sudo -E DEBIAN_FRONTEND=noninteractive apt-get -qq install $pkg -y
done
}
# MXE
function installMXE {
echoColor "Installing MXE"
if [ ! -d "/opt/mxe/usr/bin" ]
then
echoColor " Cloning MXE and compiling mingw32.static GCC"
cd /opt
sudo -E git clone https://github.com/mxe/mxe.git
cd mxe
if [[ $PARALLEL == true ]]; then
sudo make -j4 gcc gmp
else
sudo make gcc gmp
fi
else
echoColor " MXE is already installed. You'd better make sure that you've previously made MXE's gcc! (/opt/mxe/usr/bin/i686-w64-mingw32.static-gcc)"
fi
}
# Downloads
function downloadSources {
mkdir -p $BUILD_DIR
cd $BUILD_DIR
echoColor "Downloading all sources"
if [[ $BUILD_BINUTILS == true || $ALL_PRODUCTS == true ]]; then
downloadAndExtract "binutils" $BINUTILS_VERSION
else
echoColor " Skipping binutils as 'binutils' was ommitted from commandline args '$args'"
fi
if [[ $BUILD_GCC == true || $ALL_PRODUCTS == true ]]; then
downloadAndExtract "gcc" $GCC_VERSION "http://ftp.gnu.org/gnu/gcc/gcc-$GCC_VERSION/gcc-$GCC_VERSION.tar.gz"
echoColor " Downloading GCC prerequisites"
# Automatically download GMP, MPC and MPFR. These will be placed into the right directories.
# You can also download these separately, and specify their locations as arguments to ./configure
if [[ $WINDOWS_ONLY != true ]]; then
echoColor " Linux"
cd ./linux/gcc-$GCC_VERSION
./contrib/download_prerequisites
fi
cd $BUILD_DIR
if [[ $LINUX_ONLY != true ]]; then
echoColor " Windows"
cd ./windows/gcc-$GCC_VERSION
./contrib/download_prerequisites
fi
cd $BUILD_DIR
else
echoColor " Skipping gcc as 'gcc' was ommitted from commandline args '$args'"
fi
if [[ $BUILD_GDB == true || $ALL_PRODUCTS == true ]]; then
downloadAndExtract "gdb" $GDB_VERSION
else
echoColor " Skipping gdb as 'gdb' was ommitted from commandline args '$args'"
fi
}
function downloadAndExtract {
name=$1
version=$2
override=$3
echoColor " Processing $name"
if [ ! -f $name-$version.tar.gz ]
then
echoColor " Downloading $name-$version.tar.gz"
if [ -z $3 ]
then
wget -q http://ftp.gnu.org/gnu/$name/$name-$version.tar.gz
else
wget -q $override
fi
else
echoColor " $name-$version.tar.gz already exists"
fi
if [[ $WINDOWS_ONLY == true ]]; then
echoColor " Skipping extracting Linux as 'win' was specified in commandline args '$args'"
else
mkdir -p linux
cd linux
if [ ! -d $name-$version ]
then
echoColor " [linux] Extracting $name-$version.tar.gz"
tar -xf ../$name-$version.tar.gz
else
echoColor " [linux] Folder $name-$version already exists"
fi
cd ..
fi
if [[ $LINUX_ONLY == true ]]; then
echoColor " Skipping extracting Linux as 'win' was specified in commandline args '$args'"
else
mkdir -p windows
cd windows
if [ ! -d $name-$version ]
then
echoColor " [windows] Extracting $name-$version.tar.gz"
tar -xf ../$name-$version.tar.gz
else
echoColor " [windows] Folder $name-$version already exists"
fi
cd ..
fi
}
function compileAll {
echoColor "Compiling all $1"
cd $1
mkdir -p output
compileBinutils $1
compileGCC $1
compileGDB $1
cd ..
}
function compileBinutils {
if [[ $BUILD_BINUTILS == true || $ALL_PRODUCTS == true ]]; then
echoColor " Compiling binutils [$1]"
mkdir -p build-binutils-$BINUTILS_VERSION
cd build-binutils-$BINUTILS_VERSION
configureArgs="--target=${BUILD_TARGET} --with-sysroot --disable-nls --disable-werror --prefix=$BUILD_DIR/$1/output"
if [ $1 == "windows" ]
then
configureArgs="--host=i686-w64-mingw32.static $configureArgs"
fi
# Configure
echoColor " Configuring binutils (binutils_configure.log)"
../binutils-$BINUTILS_VERSION/configure $configureArgs >> binutils_configure.log
# Make
echoColor " Making (binutils_make.log)"
if [[ $PARALLEL == true ]]; then
make -j4 >> binutils_make.log
else
make >> binutils_make.log
fi
# Install
echoColor " Installing (binutils_install.log)"
sudo make install >> binutils_install.log
cd ..
else
echoColor " Skipping binutils [$1] as 'binutils' was ommitted from commandline args '$args'"
fi
}
function compileGCC {
if [[ $BUILD_GCC == true || $ALL_PRODUCTS == true ]]; then
echoColor " Compiling gcc [$1]"
mkdir -p build-gcc-$GCC_VERSION
cd build-gcc-$GCC_VERSION
configureArgs="--target=${BUILD_TARGET} --disable-nls --enable-languages=c,c++ --without-headers --prefix=$BUILD_DIR/$1/output"
if [ $1 == "windows" ]
then
configureArgs="--host=i686-w64-mingw32.static $configureArgs"
fi
if [[ $x64 == true ]]; then
# https://wiki.osdev.org/Libgcc_without_red_zone#Preparations
echoColor " Installing config/i386/t-x86_64-elf"
echo -e "# Add libgcc multilib variant without red-zone requirement\n\nMULTILIB_OPTIONS += mno-red-zone\nMULTILIB_DIRNAMES += no-red-zone" > ../gcc-$GCC_VERSION/gcc/config/i386/t-x86_64-elf
echoColor " Patching gcc/config.gcc"
sed -i '/x86_64-\*-elf\*)/a \\ttmake_file="${tmake_file} i386/t-x86_64-elf" # include the new multilib configuration' ../gcc-$GCC_VERSION/gcc/config.gcc
fi
# Configure
echoColor " Configuring gcc (gcc_configure.log)"
../gcc-$GCC_VERSION/configure $configureArgs >> gcc_configure.log
# Make GCC
echoColor " Making gcc (gcc_make.log)"
if [[ $PARALLEL == true ]]; then
make -j4 all-gcc >> gcc_make.log
else
make all-gcc >> gcc_make.log
fi
# Install GCC
echoColor " Installing gcc (gcc_install.log)"
sudo make install-gcc >> gcc_install.log
# Make libgcc
echoColor " Making libgcc (libgcc_make.log)"
if [[ $PARALLEL == true ]]; then
make -j4 all-target-libgcc >> libgcc_make.log
else
make all-target-libgcc >> libgcc_make.log
fi
# Install libgcc
echoColor " Installing libgcc (libgcc_install.log)"
sudo make install-target-libgcc >> libgcc_install.log
if [[ $x64 == true ]]; then
if [ $1 == "windows" ]
then
# no-red-zone doesn't appear to get installed by make install-target-libgcc for some reason. Manually install it ourselves
cd "${BUILD_TARGET}/no-red-zone/libgcc"
sudo make install >> ../../../libgcc_install_noredzone.log
cd ../../..
fi
if [[ ! -d "../output/lib/gcc/x86_64-elf/$GCC_VERSION/no-red-zone" ]]; then
echoError "ERROR: no-red-zone was not created. x64 patching failed"
exit 1
else
echoColor " Successfully compiled for no-red-zone"
fi
fi
cd ..
else
echoColor " Skipping gcc [$1] as 'gcc' was ommitted from commandline args '$args'"
fi
}
function compileGDB {
if [[ $BUILD_GDB == true || $ALL_PRODUCTS == true ]]; then
echoColor " Compiling gdb [$1]"
configureArgs="--target=${BUILD_TARGET} --disable-nls --disable-werror --prefix=$BUILD_DIR/$1/output"
if [ $1 == "windows" ]
then
configureArgs="--host=i686-w64-mingw32.static $configureArgs"
fi
mkdir -p build-gdb-$GDB_VERSION
cd build-gdb-$GDB_VERSION
# Configure
echoColor " Configuring (gdb_configure.log)"
../gdb-$GDB_VERSION/configure $configureArgs >> gdb_configure.log
# Make
echoColor " Making (gdb_make.log)"
if [[ $PARALLEL == true ]]; then
make -j4 >> gdb_make.log
else
make >> gdb_make.log
fi
# Install
echoColor " Installing (gdb_install.log)"
sudo make install >> gdb_install.log
cd ..
else
echoColor " Skipping gdb [$1] as 'gdb' was ommitted from commandline args '$args'"
fi
}
function finalize {
if [[ $ZIP == true || $ALL_PRODUCTS == true ]]; then
echo "Zipping everything up!"
if [[ -d "$BUILD_DIR/windows/output" ]]; then
cd $BUILD_DIR/windows/output
zip -r "${BUILD_DIR}/${BUILD_TARGET}-tools-windows.zip" *
fi
if [[ -d "$BUILD_DIR/linux/output" ]]; then
cd $BUILD_DIR/linux/output
zip -r "${BUILD_DIR}/${BUILD_TARGET}-tools-linux.zip" *
fi
echo -e "\e[92mZipped everything to $BUILD_DIR/${BUILD_TARGET}-tools-[windows | linux].zip\e[39m"
else
echoColor " Skipping zipping 'zip' was ommitted from commandline args '$args'"
fi
}
function echoColor {
echo -e "\e[96m$1\e[39m"
}
function echoError {
echo -e "\e[31m$1\e[39m"
}
main