{"id":131,"date":"2020-06-12T19:52:35","date_gmt":"2020-06-13T02:52:35","guid":{"rendered":"http:\/\/junsun.net\/wordpress\/?p=131"},"modified":"2020-08-23T22:33:40","modified_gmt":"2020-08-24T05:33:40","slug":"develop-32bit-applications-in-64bit-linux","status":"publish","type":"post","link":"https:\/\/junsun.net\/wordpress\/2020\/06\/develop-32bit-applications-in-64bit-linux\/","title":{"rendered":"Develop 32bit Applications on 64bit Linux Machines"},"content":{"rendered":"\n<h2>Overview<\/h2>\n\n\n\n<p>Nowadays almost all PC and servers are 64bit.  Once in a while one may need to develop and run 32bit apps. In this post we will go through several methods for both x86_64 and ARM64 machines.<\/p>\n\n\n\n<p>I can think of 4 ways of doing this:<\/p>\n\n\n\n<ul><li>Run a virtual machine, which in turn runs a 32bit OS.  Not covered in this post.<\/li><li>Set up 32bit chroot environment.  (Recommended)<\/li><li>Set up 32bit architecture with Debian <a href=\"https:\/\/wiki.debian.org\/Multiarch\/HOWTO\">multi-arch support<\/a> (Recommended)<\/li><li>Use gcc multi-lib feature to build 32bit binaries. <\/li><\/ul>\n\n\n\n<h2>X86_64\/Ubuntu 20.04<\/h2>\n\n\n\n<p>Here we go through some detailed steps for those approaches on X86_64\/Ubuntu 20.04.  Instructions should apply to other Debian-based distributions and different versions as well.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container\">\n<h3>GCC Multi-Lib Approach<\/h3>\n\n\n\n<p>Although this is the simplest approach, I don&#8217;t recommend it because the GCC support varies for different CPU architectures and the command line options also varies, making it hard to generalize and port existing software.  I believe mult-arch is the future direction.<\/p>\n\n\n\n<ul><li>Install GCC multi-lib package <\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install gcc-multilib<\/code><\/pre>\n\n\n\n<ul><li>Compile with -m32 option<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>gcc -m32 hello.c -o hello\nfile hello\n.\/hello<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container\">\n<h3>Multi-Arch Approach<\/h3>\n\n\n\n<ul><li>Add 32bit as secondary architecture<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo dpkg --add-architecture i386<\/code><\/pre>\n\n\n\n<ul><li>Install dev tools and 32bit specific dev tools and libraries<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt install -y build-essential\nsudo apt install -y crossbuild-essential-i386\nsudo apt install -y libc6:i386\nsudo apt install -y libstdc++6:i386<\/code><\/pre>\n\n\n\n<ul><li>Develop 32bit apps with i686-linux-gnu- cross-compile prefix<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>i686-linux-gnu-gcc hello.c -o hello\nfile hello\n.\/hello<\/code><\/pre>\n<\/div><\/div>\n\n\n\n<ul><li>Note for Ubuntu 18.04, crossbuild-essential-i386 meta-package does not exist.  One needs to install below packages explicityly<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install -y dpkg-cross g++-i686-linux-gnu  gcc-i686-linux-gnu<\/code><\/pre>\n\n\n\n<h3>Chroot Approach<\/h3>\n\n\n\n<p><em>For more detailed descriptions, please refer to <a href=\"http:\/\/logan.tw\/posts\/2018\/02\/24\/manage-chroot-environments-with-schroot\/\">http:\/\/logan.tw\/posts\/2018\/02\/24\/manage-chroot-environments-with-schroot\/<\/a><\/em><\/p>\n\n\n\n<ul><li>Install packages<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install schroot debootstrap<\/code><\/pre>\n\n\n\n<ul><li>Install 32bit environment (e.g., Ubuntu 20.04) (note the last URL arg can be ommited)<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo debootstrap --arch=i386 focal \/var\/chroot\/focal32 http:\/\/archive.ubuntu.com\/ubuntu<\/code><\/pre>\n\n\n\n<ul><li>Edit \/etc\/schroot\/schroot.conf file<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;focal32]\ndescription=Ubuntu 20.04 Focal Fossil 32bit\ndirectory=\/var\/chroot\/focal32\nroot-users=ubuntu,jsun\nusers=ubuntu,jsun\ntype=directory\npersonality=linux32<\/code><\/pre>\n\n\n\n<ul><li>Create and enter 32bit chroot session<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>schroot -c focal32          ; enter as normal user\nschroot -c focal32 -u root  ; enter as root user<\/code><\/pre>\n\n\n\n<ul><li>From inside the session, one can install build-essential meta-package and develop 32bit apps as if inside a 32bit machine.<\/li><li>(Optional)  One can even run 32bit graphic apps from inside the chroot environment. <\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>export DISPLAY=:0.0<\/code><\/pre>\n\n\n\n<h2>ARM64\/Ubuntu18.04<\/h2>\n\n\n\n<p>ARM64 is also called aarch64.  &#8220;ARM64&#8221; is rooted from Linux community and &#8220;aarch64&#8221; is official name from ARM.   We will use those 2 terms inter-changeably.<\/p>\n\n\n\n<h3>Multi-Lib approach<\/h3>\n\n\n\n<p>GCC for aarch64 does not support multi-lib.  So this option is not available.<\/p>\n\n\n\n<h3>Multi-Arch Approach<\/h3>\n\n\n\n<p>It is similar to i386 case, except that architecture is &#8220;armhf&#8221; and 32bit cross-compile prefix is &#8220;arm-linux-gunueabihf-&#8220;.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo dpkg --add-architecture armhf\nsudo apt update\nsudo apt install -y build-essential\nsudo apt install -y crossbuild-essential-armhf\nsudo apt install -y libc6:armhf\nsudo apt install -y libstdc++6:armhf\n\narm-linux-gnueabihf-gcc hello.c -o hello\nfile hello\n.\/hello<\/code><\/pre>\n\n\n\n<h3>Chroot Approach<\/h3>\n\n\n\n<p>For chroot approach, it is similar to i386 case, with 2 exceptions:<\/p>\n\n\n\n<ul><li>The architecture name is &#8220;armhf&#8221; instead of &#8220;i386&#8221;<\/li><li>The repo URL is &#8220;http:\/\/ports.ubuntu.com\/ubuntu-ports&#8221;, instead of &#8220;http:\/\/archive.ubuntu.com\/ubuntu&#8221;<\/li><\/ul>\n\n\n\n<p>Note we can also set up the root filesystem from debian repos.  Below is an example to set up debian sid for armhf 32bit architecture:<\/p>\n\n\n\n<ul><li>Install packages and bootstrap root filesystem<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install schroot debootstrap\nsudo debootstrap --arch=armhf sid \/var\/chroot\/sid-armhf http:\/\/ftp.debian.org\/debian\/<\/code><\/pre>\n\n\n\n<ul><li>Modify and add to \/etc\/schroot\/schroot.conf file<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;sid32]\ndescription=Debian Sid 32bit\ndirectory=\/var\/chroot\/sid-armhf\nroot-users=ubuntu\nusers=ubuntu\ntype=directory\npersonality=linux32<\/code><\/pre>\n\n\n\n<ul><li>Create and enter sid32 session, &#8220;schroot -c sid32&#8221;<\/li><\/ul>\n\n\n\n<h2>About MIPS64<\/h2>\n\n\n\n<ul><li>Multilib works for MIPS64.  <ul><li>sudo apt install gcc-multilib g++-multilib<\/li><li>compile with -mabi=32 flags, i.e., &#8220;gcc -mabi=32 hello.c&#8221;<\/li><\/ul><\/li><li>Multi-Arch does not work for MIPS64, at least from 32bit app development perspective<\/li><li>I have not tried chroot approach on MIPS64 yet.  Let me know if you have had any success.<\/li><\/ul>\n\n\n\n<h2>Last Words<\/h2>\n\n\n\n<ul><li>In Ubuntu chroot environment, the initial apt repo source is limited to main.  You may find many packages are missing.  To fix this, edit \/etc\/apt\/source.list<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>deb http:\/\/archive.ubuntu.com\/ubuntu xenial main universe multiverse<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Overview Nowadays almost all PC and servers are 64bit. Once in a while one may need to develop and run 32bit apps. In this post we will go through several methods for both x86_64 and ARM64 machines. I can think of 4 ways of doing this: Run a virtual machine, which in turn runs a &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/junsun.net\/wordpress\/2020\/06\/develop-32bit-applications-in-64bit-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Develop 32bit Applications on 64bit Linux Machines&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[3],"tags":[38,42,39,40,41],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/posts\/131"}],"collection":[{"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/comments?post=131"}],"version-history":[{"count":23,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":175,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/posts\/131\/revisions\/175"}],"wp:attachment":[{"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/junsun.net\/wordpress\/wp-json\/wp\/v2\/tags?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}