LoginSignup
1

More than 3 years have passed since last update.

Cコードをビッグエンディアン環境で実行する

Last updated at Posted at 2019-08-15

C言語はエンディアンによっては正しく動作しない実装もできてしまうので,リトルエンディアン・ビッグエンディアンの両者で動作確認したいことがあります.Windows10/64bit環境でリトルエンディアン・ビッグエンディアンの両方でCコードを実行する方法1について説明します.

リトルエンディアン

x86/x64はリトルエンディアンなので,WSLのUbuntuでOKです.

Windows Subsystem for Linuxを有効化

  • Windows PowerShellを管理者モードで実行
  • 以下コマンド実行
> Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Ubuntu16.04インストール

gccインストール2

$ sudo apt-get install build-essential

テストコード用意

実行するCコードをテキストエディタで作成して保存します.

test.c
#include <stdio.h>

int main(void) {
    unsigned int uint32 = 0x12345678U;
    unsigned char * p_uint8;

    printf("uint32: 0x%x\n", uint32);

    p_uint8 = (unsigned char *)&uint32;
    printf("p_uint8[0]: 0x%x\n", p_uint8[0]);
    printf("p_uint8[1]: 0x%x\n", p_uint8[1]);
    printf("p_uint8[2]: 0x%x\n", p_uint8[2]);
    printf("p_uint8[3]: 0x%x\n", p_uint8[3]);

    return 0;
}

コンパイル&実行

gccでtest.cをコンパイルして作成されたファイル(a.out)を実行します.

$ gcc test.c && ./a.out

リトルエンディアンのため,以下のような実行結果になります.

$ gcc test.c && ./a.out
uint32: 0x12345678
p_uint8[0]: 0x78
p_uint8[1]: 0x56
p_uint8[2]: 0x34
p_uint8[3]: 0x12

ビッグエンディアン

Debian上のQEMUで動作するPowerPCを使用します.

VMware Workstation Player 12をダウンロードしてインストール

Debian8.11をダウンロードしてVMwareへインストール

https://www.debian.org/releases/jessie/debian-installer/
※ネットワークインストールCDイメージ(amd64)でOK

Debian8.11を起動してQEMUインストール

以下サイトを参考にQEMUをインストールし,PowerPCを起動する.
https://blog.bitmeister.jp/?p=3633

# sudo apt-get update
# sudo apt-get install uml-utilities bridge-utils qemu
  • /etc/network/interfacesへ以下を追記.
iface eth0 inet manual
auto br0
iface br0 inet dhcp
    bridge_ports    eth0
    bridge_stp      off
    bridge_maxwait  0
    bridge_fd       0
  • /etc/qemu/bridge.confへ以下を記載.
allow br0
  • 権限設定.
# sudo chmod 0644 /etc/qemu/bridge.conf
# sudo chmod u+s /usr/lib/qemu/qemu-bridge-helper
  • 一旦Debianを再起動.
# sudo shutdown -r now

PowerPCイメージをダウンロード

# sudo wget https://people.debian.org/~aurel32/qemu/powerpc/debian_wheezy_powerpc_standard.qcow2

PowerPCを起動

# sudo qemu-system-ppc -hda debian_wheezy_powerpc_standard.qcow2 -nographic -net nic -net bridge,br=br0

※ログインユーザ/パス等は以下の通り
 https://people.debian.org/~aurel32/qemu/powerpc/README.txt

PowerPC用バイナリをコンパイル

  • QEMU上のPowerPCにはコンパイラが無かったので,WSLのUbuntuにPowerPC用gccをインストール.
$ sudo apt-get install gcc-4.8-powerpc-linux-gnu
  • テストコードをコンパイル
$ powerpc-linux-gnu-gcc-4.8 ./test.c

バイナリをPowerPCへ転送

  • WSLのUbuntuから送信
$ nc -l 10000 < a.out

※ポート(10000)は未使用なら何番でもOKです.

  • PowerPCで受信
# nc 192.168.229.1 10000 > a.out
# chmod +x a.out

※IPアドレス(192.168.229.1)は,VMwareと接続されるWindows上のIPアドレスを指定します.

権限設定して実行

# chmod +x a.out
# ./a.out

ビッグエンディアンのため,以下のような実行結果になります.

# ./a.out
uint32: 0x12345678
p_uint8[0]: 0x12
p_uint8[1]: 0x34
p_uint8[2]: 0x56
p_uint8[3]: 0x78

  1. 各ソフトウェアのバージョンは動作確認時に使用したものなので,別バージョンでも動作するかもしれません. 

  2. https://qiita.com/moris/items/02c01a239b9701336260 

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1