From: Frank K. <fbk...@co...> - 2006-06-23 18:42:27
|
pnk...@gm... wrote: > Hello all- > > I'm trying to understand some weird behavior I'm seeing with nasm on an > Intel Mac. Nice to see someone using the new code! I don't know much about "macho". I'll cc this to the nasm-devel list - probably the author of the code will have some insight. Thanks for the feedback!!! Best, Frank > I have two assembly files, bar.asm and baz.asm. bar references labels > defined by baz. But it doesn't seem like the object files are being > linked together properly, unless I am misinterpreting what gdb is > printing and what my C code is doing. In particular, the references in > the data of bar do not correspond to the actual addresses of the data > in baz. > > Perhaps I have misunderstood something about the macho format or about > the Mac OS X ABI. Does dynamic linking get in the way of doing this > sort of resolution of addresses in the data section? > > I have replicated a simpler version of the problem with four files (the > two assembly files, some C code to print out the bad values, and a > Makefile to build it all and print the output to a text file). > I have copied the four files below, along with the output that is > printed to problem.txt. The key problem is that &baz is determined by > the C code to be 0x00002024, but the entry in the bar array > corresponding to the label for baz is the number 0x00001cdf. > > Does anyone have an idea why this might be happening, and whether the > problem is in the OS X linker (unlikely), Nasm's mach-o support, or in > my mental model of how the two assembly files should be linking > together? > > ;;; baz.asm > section .data > align 8 > global _baz > _baz: > dd 0xdeadbead > ;;; end of baz.asm > > ;;; bar.asm > section .data > align 8 > global _bar > extern _foo > extern _foil > extern _baz > _bar: > dd 0x12345678 > dd _foo > dd _foil > dd _baz > dd _bar > > ;;; end of bar.asm > > /* foo.c */ > #include <stdio.h> > > int foiled(int x) { return 4*x + 7; } > > int foil(int x) { return 3*x + 2; } > > int foo(int x) { return 2*x + 1; } > > extern int *bar; > extern int *baz; > > int main() { > printf(" &foil: 0x%08x\n", &foil); > printf(" &foo: 0x%08x\n", &foo); > printf(" bar: 0x%08x\n", bar); > printf(" &bar: 0x%08x\n", &bar); > printf(" baz: 0x%08x\n", baz); > printf(" &baz: 0x%08x\n", &baz); > printf(" (&bar)[0] 0x12345678: 0x%08x\n", (&bar)[0]); > printf(" (&bar)[1] _foo : 0x%08x\n", (&bar)[1]); > printf(" (&bar)[2] _foil : 0x%08x\n", (&bar)[2]); > printf(" (&bar)[3] _baz : 0x%08x\n", (&bar)[3]); > printf(" (&bar)[4] _bar : 0x%08x\n", (&bar)[4]); > return 0; > } > /* end of foo.c */ > > # Makefile > problem.txt: foo > echo problem.txt > problem.txt > nasm -v >> problem.txt > ./foo >> problem.txt > echo end of problem.txt >> problem.txt > > foo: foo.c bar.o baz.o > gcc -o foo foo.c bar.o baz.o > > bar.o: bar.asm > nasm -f macho bar.asm > > baz.o: baz.asm > nasm -f macho baz.asm > # End of Makefile > > problem.txt > NASM version 0.98.40 (Apple Computer, Inc. build 9) compiled on Apr 6 > 2006 > &foil: 0x00001cdf > &foo: 0x00001cf3 > bar: 0x12345678 > &bar: 0x00002010 > baz: 0xdeadbead > &baz: 0x00002024 > (&bar)[0] 0x12345678: 0x12345678 > (&bar)[1] _foo : 0x00001cf3 > (&bar)[2] _foil : 0x00001cdf > (&bar)[3] _baz : 0x00001cdf > (&bar)[4] _bar : 0x00002010 > end of problem.txt > |