OS X中的GNU Linker等效命令 [英] GNU Linker equivalent command in OS X

查看:107
本文介绍了OS X中的GNU Linker等效命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于操作系统的下一本书.在第43页中,他们使用以下命令将带注释的机器代码转换为原始机器代码文件:

$ ld -o basic.bin -Ttext 0x0 --oformat binary basic.o

在MacBook Pro(运行Mavericks)中运行该命令时,我得到:

ld: unknown option: -Ttext

我进行了一些研究,发现OS X的链接器不允许使用脚本文件作为链接器脚本.

互联网上的其他一些帖子建议使用以下正确"格式:

$ ld -T text 0x0 --o format binary -o basic.bin basic.o

尽管这对我也不起作用.

我还尝试通过homebrew安装binutils,但是它似乎不随GNU链接器一起提供.

该命令可以在Ubuntu 14.04中正确运行,但是如果可能的话,我想继续在OS X中进行开发.

是否有一种方法可以通过OS X的链接器获得相同的结果,并可能带有不同的标志?

更新:

使用binutils中的gobjcopy,我可以使用以下命令生成bin:

$ gobjcopy -j .text -O binary basic.o basic.bin

但是,我找不到在代码中偏移标签地址的方法,例如我可以使用带有-Ttext 0x1000的GNU ld.

我尝试了--set-start <hex>却没有任何运气:

$ gobjcopy -j .text --set-start 0x1000 -O binary basic.o basic.bin

解决方案

我正在遵循相同的os-dev.pdf指南,并且遇到了与您相同的问题.

问题的根本是,无论如何我们都需要编译交叉编译的gcc,因此解决方案就是这样做.

在OSDev上有一个很好的指南 ,但是如果您正在运行最新版本的OSX我为此文件在Github上准备了具体的指南

以下是命令,不过在将整个文本粘贴到计算机上之前,请先对其进行测试!在Github链接上可以找到完整的说明,但是由于Stack Overflow似乎喜欢答案中嵌入的解决方案,因此就在这里.

此外,如果您遇到任何错误,请将其报告给我(此处或有Github问题),以便我为其他人解决.

brew install gmp
brew install mpfr
brew install libmpc
brew install gcc

export CC=/usr/local/bin/gcc-4.9
export LD=/usr/local/bin/gcc-4.9

export PREFIX="/usr/local/i386elfgcc"
export TARGET=i386-elf
export PATH="$PREFIX/bin:$PATH"

mkdir /tmp/src
cd /tmp/src
curl -O http://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.gz # If the link 404's, look for a more recent version
tar xf binutils-2.24.tar.gz
mkdir binutils-build
cd binutils-build
../binutils-2.24/configure --target=$TARGET --enable-interwork --enable-multilib --disable-nls --disable-werror --prefix=$PREFIX 2>&1 | tee configure.log
make all install 2>&1 | tee make.log

cd /tmp/src
curl -O http://mirror.bbln.org/gcc/releases/gcc-4.9.1/gcc-4.9.1.tar.bz2
tar xf gcc-4.9.1.tar.bz2
mkdir gcc-build
cd gcc-build
../gcc-4.9.1/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --disable-libssp --enable-languages=c --without-headers
make all-gcc 
make all-target-libgcc 
make install-gcc 
make install-target-libgcc 

您可以在/usr/local/i386elfgcc/bin

找到GNU的binutils和交叉编译的gcc.

I'm reading the following book about operating systems. In Page 43, they use the following command to convert annotated machine code into a raw machine code file:

$ ld -o basic.bin -Ttext 0x0 --oformat binary basic.o

When running that command in my MacBook Pro (running Mavericks), I get:

ld: unknown option: -Ttext

I've did some research and found out that OS X's linker doesn't allow using a script file as the linker script.

Some other posts on the internet recommend using the following "correct" format:

$ ld -T text 0x0 --o format binary -o basic.bin basic.o

Although it didn't work for me neither.

I also tried installing binutils via homebrew, but it doesn't seems to ship with GNU linker.

The command correctly runs in Ubuntu 14.04, but I'd like to continue developing in OS X if possible.

Is there a way to obtain the same results with OS X's linker, potentially with different flags?

UPDATE:

I was able to generate a bin with the following command, using gobjcopy from binutils:

$ gobjcopy -j .text -O binary basic.o basic.bin

However I couldn't find a way to offset label addresses in the code, as I could with GNU ld with -Ttext 0x1000 for example.

I tried with --set-start <hex> without any luck:

$ gobjcopy -j .text --set-start 0x1000 -O binary basic.o basic.bin

解决方案

I am following the same os-dev.pdf guide and encountered the same problem as you.

The bottom of the issue is that we need to compile a cross-compiled gcc anyway, so the solution is just to do so.

There is a good guide at OSDev but if you're running a recent version of OSX I prepared a specific guide for this on Github

Here are the commands, though please test them before pasting the whole wall of text on your computer! At the Github link you will find the full explanations, but since Stack Overflow seems to like the solution embedded on the answer, here it is.

Also, if you encounter any error, please report it back to me (here or with a Github issue) so that I can fix it for other people.

brew install gmp
brew install mpfr
brew install libmpc
brew install gcc

export CC=/usr/local/bin/gcc-4.9
export LD=/usr/local/bin/gcc-4.9

export PREFIX="/usr/local/i386elfgcc"
export TARGET=i386-elf
export PATH="$PREFIX/bin:$PATH"

mkdir /tmp/src
cd /tmp/src
curl -O http://ftp.gnu.org/gnu/binutils/binutils-2.24.tar.gz # If the link 404's, look for a more recent version
tar xf binutils-2.24.tar.gz
mkdir binutils-build
cd binutils-build
../binutils-2.24/configure --target=$TARGET --enable-interwork --enable-multilib --disable-nls --disable-werror --prefix=$PREFIX 2>&1 | tee configure.log
make all install 2>&1 | tee make.log

cd /tmp/src
curl -O http://mirror.bbln.org/gcc/releases/gcc-4.9.1/gcc-4.9.1.tar.bz2
tar xf gcc-4.9.1.tar.bz2
mkdir gcc-build
cd gcc-build
../gcc-4.9.1/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --disable-libssp --enable-languages=c --without-headers
make all-gcc 
make all-target-libgcc 
make install-gcc 
make install-target-libgcc 

You will find GNU's binutils and your cross-compiled gcc at /usr/local/i386elfgcc/bin

这篇关于OS X中的GNU Linker等效命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆