{"id":726,"date":"2010-01-01T15:15:59","date_gmt":"2010-01-01T14:15:59","guid":{"rendered":"http:\/\/sahits.ch\/blog\/?p=726"},"modified":"2010-01-01T15:16:21","modified_gmt":"2010-01-01T14:16:21","slug":"compiling-for-synology-ds509","status":"publish","type":"post","link":"http:\/\/sahits.ch\/blog\/blog\/2010\/01\/01\/compiling-for-synology-ds509\/","title":{"rendered":"Compiling for Synology DS509+"},"content":{"rendered":"<p>This article describes my infutile first attempt to compile a gcc 4 toolchain for my NAS DS 509+.  Every step described works, but the endresult are binaries that do not run on my NAS. Never the less, this article may be worthwhile.<!--more--><br \/>\nI purchased a NAS a Synology DiskStation 509+ to use as a Server. This NAS, as well as all others of the + series are based on a Freescale mpc8533 PowerPC with 64-Bit Memory Bus. The j series use the Freescale mpc8241 PowerPC with 32-Bit Memory Bus. Which are basically the same architecture (besides the Bus width). For the completeness sake is here a list of the products (up to 28.12.2009):<br \/>\n<strong>+-Series<\/strong><\/p>\n<ul>\n<li>DS109+<\/li>\n<li>DS209+<\/li>\n<li>DS209+II<\/li>\n<li>DS409+ <\/li>\n<li>DS509+<\/li>\n<li>RS409+ <\/li>\n<li>RS409RP+ <\/li>\n<\/ul>\n<p><strong>j-Series<\/strong><\/p>\n<ul>\n<li>DS109j <\/li>\n<li>DS209j<\/li>\n<\/ul>\n<p>[Source: <a href=\"http:\/\/forum.synology.com\/wiki\/index.php\/What_kind_of_CPU_does_my_NAS_have\">http:\/\/forum.synology.com\/wiki\/index.php\/What_kind_of_CPU_does_my_NAS_have<\/a>]<br \/>\nNow guess what: There is are no Java binaries out there that work. Therefore you have to compile your own. But surprise, the NAS has no compiler. (Well there is a <a href=\"http:\/\/download.synology.com\/download\/ds\/userguide\/Synology%20NAS%20Server%203rd-Party%20Apps%20Integration%20Guide.pdf\">guide<\/a> for cross-compiling from Synology).<\/p>\n<h3>Getting a compiler<\/h3>\n<p>The first step is to enable the NAS to compile the sources on its own. For that  I need a GCC compiler tool-chain on the NAS. The easiest way is probably to use the steps in the aforementioned document to cross-compile the compiler (we need binutils, gcc and glibc possible also the Linux headers). The problem with this is the need to manually tweak the configure script to comply to the target architecture.<br \/>\nAnother path may be the use of sections of the <a href=\"http:\/\/cross-lfs.org\/view\/1.1.0\/ppc64\/index.html\">Cross-Compiled Linux From Scratch<\/a>, which is just a bit heavy weight since you don&#8217;t want to compile a whole system. Therefore this description must be tweaked also.<br \/>\nA third path may be the lightweight description found at <a href=\"http:\/\/www.pages.drexel.edu\/~sg64\/stuff\/cross-compile.htm\">http:\/\/www.pages.drexel.edu\/~sg64\/stuff\/cross-compile.htm<\/a>.<br \/>\nAs usual I tried the easy way out but ended up following the Linux from Scratch way. Using an Ubuntu 32bit on a 64bit machine I had to install several packages to comply:<\/p>\n<ul>\n<li>flex<\/li>\n<li>texinfo<\/li>\n<li>gawk<\/li>\n<li>bison<\/li>\n<\/ul>\n<p>On the NAS I created a tools directory that I mounted on my host under \/media\/tools. If you map your share to another directory or compile into a local directory be sure to replace &#8218;\/media\/tools&#8216; with the appropriate value.<br \/>\nI used the following steps after downloading the needed packages and extracting them. For further details please referre to the <a href=\"http:\/\/cross-lfs.org\/view\/1.1.0\/ppc64\/index.html\">CLFS<\/a>.<\/p>\n<pre>\r\nunset CFLAGS\r\nunset CFLAGS\r\nexport CLFS_HOST=\"$(echo $MACHTYPE | \\\r\n    sed \"s\/$(echo $MACHTYPE | cut -d- -f2)\/cross\/\")\"\r\nexport CLFS_TARGET=\"powerpc64-unknown-linux-gnu\"\r\nexport CLFS_TARGET32=\"powerpc-unknown-linux-gnu\"\r\nexport BUILD32=\"-m32\"\r\nexport BUILD64=\"-m64\"\r\ncd linux-2.6.24.7\r\ninstall -dv ..\/tools\/include\r\nmake mrproper\r\n<\/pre>\n<p>In scripts\/unifdef.c I needed to replace all three instances of getline with lineread.<\/p>\n<pre>\r\nmake ARCH=powerpc headers_check\r\nmake ARCH=powerpc INSTALL_HDR_PATH=dest headers_install\r\ncp -rv dest\/include\/* ..\/tools\/include\r\ncd ..\r\nmkdir cross-tools\r\ncd file-4.23\r\n.\/configure --prefix=\/media\/tools\/cross-tools\r\nmake\r\nmake install\r\ncd ..\r\ncd binutils-2.18\r\npatch -Np1 -i ..\/binutils-2.18-posix-1.patch\r\npatch -Np1 -i ..\/binutils-2.18-branch_update-3.patch\r\npatch -Np1 -i ..\/binutils-2.18-genscripts_multilib-1.patch\r\nmkdir -v ..\/binutils-build\r\ncd ..\/binutils-build\r\nAR=ar AS=as ..\/binutils-2.18\/configure --prefix=\/media\/tools\/cross-tools --host=${CLFS_HOST} --target=${CLFS_TARGET} \\\r\n     --with-lib-path=\/media\/tools\/tools\/lib --disable-nls --enable-shared --enable-64-bit-bfd \\\r\n     --disable-info\r\nmake configure-host\r\nexport CFLAGS=\"-Wno-error -g -O2\"\r\nmake\r\nexport CFLAGS=\r\nmake install\r\ncp -v ..\/binutils-2.18\/include\/libiberty.h \/media\/tools\/tools\/include\r\ncd ..\r\ncd gcc-4.2.4\r\npatch -Np1 -i ..\/gcc-4.2.4-specs-1.patch\r\npatch -Np1 -i ..\/gcc-4.2.4-posix-1.patch\r\npatch -Np1 -i ..\/gcc-4.2.4-cross_search_paths-1.patch\r\necho \"\r\n#undef STARTFILE_PREFIX_SPEC\r\n#define STARTFILE_PREFIX_SPEC \\\"\/media\/tools\/tools\/lib\/\\\"\" >> gcc\/config\/rs6000\/linux.h\r\necho \"\r\n#undef STARTFILE_PREFIX_SPEC\r\n#define STARTFILE_PREFIX_SPEC \\\"\/media\/tools\/tools\/lib\/\\\"\" >> gcc\/config\/rs6000\/linux64.h\r\ncp -v gcc\/Makefile.in{,.orig}\r\nsed -e \"s@\\(^CROSS_SYSTEM_HEADER_DIR =\\).*@\\1 \/media\/tools\/tools\/include@g\" \\\r\n    gcc\/Makefile.in.orig > gcc\/Makefile.in\r\nmkdir -v ..\/gcc-build\r\ncd ..\/gcc-build\r\n..\/gcc-4.2.4\/configure --prefix=\/media\/tools\/cross-tools \\\r\n    --host=${CLFS_HOST} --target=${CLFS_TARGET} --with-local-prefix=\/media\/tools\/tools --disable-nls --disable-shared \\\r\n    --disable-threads --enable-languages=c\r\nmake all-gcc\r\nmake install-gcc\r\ncd ..\r\ncd glibc-2.7\r\npatch -Np1 -i ..\/glibc-2.7-branch_update-1A.patch\r\npatch -Np1 -i ..\/glibc-2.7-libgcc_eh-1.patch\r\npatch -Np1 -i ..\/glibc-2.7-localedef_segfault-1.patch\r\nmkdir -v ..\/glibc-build\r\nexport PATH=$PATH:\/media\/tools\/cross-tools\/bin\r\ncd ..\/glibc-build\r\necho \"libc_cv_forced_unwind=yes\" > config.cache\r\necho \"libc_cv_c_cleanup=yes\" >> config.cache\r\nBUILD_CC=\"gcc\" CC=\"${CLFS_TARGET}-gcc ${BUILD32}\" \\\r\n    AR=\"${CLFS_TARGET}-ar\" RANLIB=\"${CLFS_TARGET}-ranlib\" \\\r\n    ..\/glibc-2.7\/configure --prefix=\/media\/tools\/tools \\\r\n    --host=${CLFS_TARGET32} --build=${CLFS_HOST} \\\r\n    --disable-profile --enable-add-ons \\\r\n    --with-tls --enable-kernel=2.6.0 --with-__thread \\\r\n    --with-binutils=\/media\/tools\/cross-tools\/bin --with-headers=\/media\/tools\/tools\/include \\\r\n    --cache-file=config.cache\r\nmake\r\nmake install\r\ncd ..\r\ncd glibc-2.7\r\npatch -Np1 -i ..\/glibc-2.7-branch_update-1A.patch\r\npatch -Np1 -i ..\/glibc-2.7-libgcc_eh-1.patch\r\npatch -Np1 -i ..\/glibc-2.7-localedef_segfault-1.patch\r\nmkdir -v ..\/glibc-build\r\ncd ..\/glibc-build\r\necho \"libc_cv_forced_unwind=yes\" > config.cache\r\necho \"libc_cv_c_cleanup=yes\" >> config.cache\r\necho \"slibdir=\/media\/tools\/tools\/lib64\" >> configparms\r\nBUILD_CC=\"gcc\" CC=\"${CLFS_TARGET}-gcc ${BUILD64}\" \\\r\n    AR=\"${CLFS_TARGET}-ar\" RANLIB=\"${CLFS_TARGET}-ranlib\" \\\r\n    ..\/glibc-2.7\/configure --prefix=\/media\/tools\/tools \\\r\n    --host=${CLFS_TARGET} --build=${CLFS_HOST} --libdir=\/media\/tools\/tools\/lib64 \\\r\n    --disable-profile --enable-add-ons \\\r\n    --with-tls --enable-kernel=2.6.0 --with-__thread \\\r\n    --with-binutils=\/media\/tools\/cross-tools\/bin --with-headers=\/media\/tools\/tools\/include \\\r\n    --cache-file=config.cache\r\nmake\r\nmake install\r\ncd ..\r\ncd gcc-4.2.4\r\npatch -Np1 -i ..\/gcc-4.2.4-PR31490-1.patch\r\ncp -v gcc\/Makefile.in{,.orig}\r\nsed -e \"s@\\(^CROSS_SYSTEM_HEADER_DIR =\\).*@\\1 \/media\/tools\/tools\/include@g\" \\\r\n    gcc\/Makefile.in.orig > gcc\/Makefile.in\r\ncp -v configure{,.orig}\r\nsed -e  '\/FLAGS_FOR_TARGET.*\\\/lib\\\/\/s@-B[^ ]*\/lib\/@@g' configure.orig > \\\r\n    configure\r\ncd ..\/gcc-build\r\n..\/gcc-4.2.4\/configure --prefix=\/media\/tools\/cross-tools \\\r\n    --target=${CLFS_TARGET} --host=${CLFS_HOST} \\\r\n    --with-local-prefix=\/media\/tools\/tools --disable-nls --enable-shared \\\r\n    --enable-languages=c,c++ --enable-__cxa_atexit \\\r\n    --enable-c99 --enable-long-long --enable-threads=posix\r\nmake AS_FOR_TARGET=\"${CLFS_TARGET}-as\" \\\r\n    LD_FOR_TARGET=\"${CLFS_TARGET}-ld\"\r\nmake install\r\n<\/pre>\n<p>While configuring the glibc library I run into a snag that let me believe that I had to install autoconf for succesfull compilation. With autoconf installed the configuration would hang on &#8222;checking whether autoconf works&#8220;, which means that cd ..\/glibc-2.7 autoconf hangs. After removing autoconf from the system and fixing the original error (by installing gawk) everything was fine.<br \/>\nWith that the cross compile tool chain is complete and this tool chain can now be set to use to compile the tools for the target system.<\/p>\n<pre>\r\nexport CC=\"${CLFS_TARGET}-gcc\"\r\nexport CXX=\"${CLFS_TARGET}-g++\"\r\nexport AR=\"${CLFS_TARGET}-ar\"\r\nexport AS=\"${CLFS_TARGET}-as\"\r\nexport RANLIB=\"${CLFS_TARGET}-ranlib\"\r\nexport LD=\"${CLFS_TARGET}-ld\"\r\nexport STRIP=\"${CLFS_TARGET}-strip\"\r\nrm -Rdf binutils-build\r\nmkdir binutils-build\r\ncd binutils-build\r\nCC=\"${CC} ${BUILD64}\" \\\r\n    ..\/binutils-2.18\/configure --prefix=\/media\/tools\/tools \\\r\n    --libdir=\/media\/tools\/tools\/lib64 --with-lib-path=\/media\/tools\/tools\/lib \\\r\n    --build=${CLFS_HOST} --host=${CLFS_TARGET} --target=${CLFS_TARGET} \\\r\n    --disable-nls --enable-shared --enable-64-bit-bfd --disable-info\r\nmake configure-host\r\nmake\r\nmake install\r\ncd ..\r\n<\/pre>\n<p>I had to replace the constant SSIZE_MAX in gcc-4.2.4\/gcc\/config\/host-linux.c on line 207 with the constant LONG_MAX.<\/p>\n<pre>\r\nrm -Rdf gcc-build\r\ncd gcc-4.2.4\r\ncp -v gcc\/cppdefault.c{,.orig}\r\nsed -e '\/#define STANDARD_INCLUDE_DIR\/s@\"\/usr\/include\"@0@g' \\\r\n    gcc\/cppdefault.c.orig > gcc\/cppdefault.c\r\ncp -v gcc\/Makefile.in{,.orig}\r\nsed -e 's@\\(^NATIVE_SYSTEM_HEADER_DIR =\\).*@\\1 \/media\/tools\/tools\/include@g' \\\r\n    gcc\/Makefile.in.orig > gcc\/Makefile.in\r\ncp -v gcc\/Makefile.in{,.orig2}\r\nsed -e \"\/MULTILIBS\/s@\\$(GCC_FOR_TARGET)@\/media\/tools\/cross-tools\/bin\/${CC}@g\" \\\r\n   gcc\/Makefile.in.orig2 > gcc\/Makefile.in\r\nCC=\"${CC} ${BUILD64}\" CXX=\"${CXX} ${BUILD64}\" \\\r\n    ..\/gcc-4.2.4\/configure --prefix=\/media\/tools\/tools --libdir=\/media\/tools\/tools\/lib64 \\\r\n    --build=${CLFS_HOST} --host=${CLFS_TARGET} --target=${CLFS_TARGET} \\\r\n    --with-local-prefix=\/media\/tools\/tools --enable-long-long --enable-c99 \\\r\n    --enable-shared --enable-threads=posix --enable-__cxa_atexit \\\r\n    --disable-nls --enable-languages=c,c++ --disable-libstdcxx-pch\r\nmake AS_FOR_TARGET=\"${AS}\" \\\r\n    LD_FOR_TARGET=\"${LD}\"\r\nmake install\r\ncd ..\r\n<\/pre>\n<p>Now we are done with the compiling section. The next part is a bit tricky: We must integrate the compiled binaries into the running system. Because the &#8218;\/media\/tools&#8216; directory was a mapped directory on my NAS all binaries are already there (in a directory \/volume1\/cross_compile\/tools). We have to ensure that the next action can be undone. There are two sets of possibilities:<\/p>\n<ol>\n<li>The binary does not already exist: Therefore it should be possible to remove the binary<\/li>\n<li>A binary with the same name (possibly another version) exists: The original must be backed up so that it can be restored.<\/li>\n<\/ol>\n<p>For the purpose of the backup I created a directory under \/root named backup_sys_gcc and a script rollback that can either remove the new version or replace it from the backup directory. As I walked through the directories only some libraries in lib were also in \/lib. I figured to leave the original and delay any potential problems that may arise when they occur. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article describes my infutile first attempt to compile a gcc 4 toolchain for my NAS DS 509+. Every step described works, but the endresult are binaries that do not run on my NAS. Never the less, this article may be worthwhile.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,138,7,9,6],"tags":[27,301,302,141],"class_list":["post-726","post","type-post","status-publish","format-standard","hentry","category-computer","category-it","category-java","category-linux","category-programmieren","tag-compile","tag-java","tag-linux","tag-nas"],"_links":{"self":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/726","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/comments?post=726"}],"version-history":[{"count":28,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/726\/revisions"}],"predecessor-version":[{"id":764,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/posts\/726\/revisions\/764"}],"wp:attachment":[{"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/media?parent=726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/categories?post=726"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sahits.ch\/blog\/wp-json\/wp\/v2\/tags?post=726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}