0

I have an asm file test.S:

.text

.globl start
start:

movw %cs, %ax
movw %ax, %ds

Then I execute

clang -m32 -fno-builtin -Wall -ggdb -nostdinc -fno-stack-protector -O0 -nostdinc -c test.S -o test.o
ld test.o -o test.bin
gobjdump -S test.bin

I get

test.bin:     file format mach-o-i386


Disassembly of section .text:

00001ffa <start>:
    1ffa:   66 8c c8                mov    %cs,%ax
    1ffd:   66 8e d8                mov    %ax,%ds

Notice that the address of start is 0x00001ffa, my question is how can I specify this address? I tried to use ld -segaddr, but it doesn't work. My OS is OS X 10.11

Shangtong Zhang
  • 1,579
  • 1
  • 11
  • 18
  • 1
    What are you trying to do? – fuz Jul 06 '16 at 08:06
  • @FUZxxl I try to write a bootloader for my toy OS. After getting test.bin, I will use gobjcopy to get pure binary version of this program, so I need to know where the program starts. – Shangtong Zhang Jul 06 '16 at 08:10
  • For application programs, this is not important as the Mach-O header tells the program loader where he should start the program. i found out that the easiest way to get what you want is to put the entry point into an object file on its own and make that the first file on the linker command line. You also need to write your own linker script so the data comes out where you need it. – fuz Jul 06 '16 at 08:24
  • @FUZxxl Sounds like a good idea, put the entry point in a standalone file and never modify it so the address of `start` gets fixed. Is my understanding right? – Shangtong Zhang Jul 06 '16 at 08:35
  • @FUZxxl : Apple's _LD_ will not support linker scripts, and options like `-Ttext=0x7c00` etc. Apple's _LD_ is not based on Linux _LD_. Personally I'd Homebrew and then build a proper i686-elf cross compiler. That cross compiler would support GNU tools(including _LD_) and ELF objects rather than MachO. Advantage is most tutorials would work as expected. – Michael Petch Jul 06 '16 at 14:49
  • There is a tutorial on building an i686-elf cross compiler on OS/X here: https://github.com/cfenollosa/os-tutorial/tree/master/11-kernel-crosscompiler – Michael Petch Jul 06 '16 at 14:53
  • @MichaelPetch Yeah I tried cross-compiler before, however my initial vision is to use native Apple compile tools. There are so many tutorials about linux. – Shangtong Zhang Jul 07 '16 at 00:26

0 Answers0