Equivalent GNU Linker Command in OS X

I am reading the next book on operating systems. On page 43, they use the following command to convert the annotated machine code to a native machine code file:

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

      

When I run this command on my MacBook Pro (Mavericks is running) I get:

ld: unknown option: -Ttext

      

I did some research and found out that the OS X linker does not allow a script file to be used as a script linker.

Several 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.

I also tried to install binutils

via homebrew

, but it doesn't seem to come with the GNU linker.

The command works correctly on Ubuntu 14.04, but I would like to continue development on OS X if possible.

Is there a way to get the same results with the OS X linker, possibly with different flags?

UPDATE:

I was able to create 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 the addresses of the labels in the code as I could with GNU ld s -Ttext 0x1000

for example.

I tried with --set-start <hex>

no luck:

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

      

+4


source to share


2 answers


I am following the same tutorial os-dev.pdf

and faced the same problem as you.

At the heart of the problem is the need to compile the compiled gcc, so the solution is simple for that.

OSDev has a nice guide on Github

Here are the commands, but please check them before you paste an entire wall of text onto your computer! You will find a full explanation in the Github link, but since Stack Overflow looks like a solution embedded in the answer, here it is.



Also, if you run into any error, please let me know (here or with the Github question) so 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 binutils and your compiled gcc at /usr/local/i386elfgcc/bin

+8


source


manual installation binutils

always throws errors on my macOS.

The solution is to use homebrew

:



brew tap nativeos/i386-elf-toolchain 
brew install i386-elf-binutils i386-elf-gcc

      

then you can use the command i386-elf-ld

insteadld

+1


source







All Articles