Initial commit of Mach-O object format.

Contributed by: Henryk Richter <henryk.richter@comlab.uni-rostock.de>

This adds 3 object format keywords: macho, macho32, macho64.  These work in
the same way as elf, elf32, and elf64.  The object format is still a work
in progress; amongst other things it does not yet support full cross-section
references (othersym1-othersym2), dynamic linking, or GAS input syntax.  We
will continue to improve and work on these features in the near future.

svn path=/trunk/yasm/; revision=1732
0.6.0
Peter Johnson 18 years ago
parent cb57404518
commit 7f68927283
  1. 2
      modules/objfmts/Makefile.inc
  2. 9
      modules/objfmts/macho/Makefile.inc
  3. 1907
      modules/objfmts/macho/macho-objfmt.c
  4. 7
      modules/objfmts/macho/tests/Makefile.inc
  5. 9
      modules/objfmts/macho/tests/nasm32/Makefile.inc
  6. 4
      modules/objfmts/macho/tests/nasm32/macho32_test.sh
  7. 83
      modules/objfmts/macho/tests/nasm32/machotest.asm
  8. 45
      modules/objfmts/macho/tests/nasm32/machotest.c
  9. 776
      modules/objfmts/macho/tests/nasm32/machotest.hex
  10. 24
      modules/objfmts/macho/tests/nasm32/reloc.asm
  11. 410
      modules/objfmts/macho/tests/nasm32/reloc.hex
  12. 9
      modules/objfmts/macho/tests/nasm64/Makefile.inc
  13. 4
      modules/objfmts/macho/tests/nasm64/macho64_test.sh
  14. 107
      modules/objfmts/macho/tests/nasm64/machotest64.asm
  15. 56
      modules/objfmts/macho/tests/nasm64/machotest64.c
  16. 1046
      modules/objfmts/macho/tests/nasm64/machotest64.hex
  17. 33
      modules/objfmts/macho/tests/nasm64/reloc64-err.asm
  18. 2
      modules/objfmts/macho/tests/nasm64/reloc64-err.errwarn

@ -5,6 +5,7 @@ EXTRA_DIST += modules/objfmts/bin/Makefile.inc
EXTRA_DIST += modules/objfmts/elf/Makefile.inc
#!EXTRA_DIST += modules/objfmts/omf/Makefile.inc
EXTRA_DIST += modules/objfmts/coff/Makefile.inc
EXTRA_DIST += modules/objfmts/macho/Makefile.inc
EXTRA_DIST += modules/objfmts/rdf/Makefile.inc
EXTRA_DIST += modules/objfmts/win32/Makefile.inc
EXTRA_DIST += modules/objfmts/win64/Makefile.inc
@ -15,6 +16,7 @@ include modules/objfmts/bin/Makefile.inc
include modules/objfmts/elf/Makefile.inc
#!include modules/objfmts/omf/Makefile.inc
include modules/objfmts/coff/Makefile.inc
include modules/objfmts/macho/Makefile.inc
include modules/objfmts/rdf/Makefile.inc
include modules/objfmts/win32/Makefile.inc
include modules/objfmts/win64/Makefile.inc

@ -0,0 +1,9 @@
# $Id$
libyasm_a_SOURCES += modules/objfmts/macho/macho-objfmt.c
YASM_MODULES += objfmt_macho objfmt_macho32 objfmt_macho64
EXTRA_DIST += modules/objfmts/macho/tests/Makefile.inc
include modules/objfmts/macho/tests/Makefile.inc

File diff suppressed because it is too large Load Diff

@ -0,0 +1,7 @@
# $Id$
EXTRA_DIST += modules/objfmts/macho/tests/nasm32/Makefile.inc
EXTRA_DIST += modules/objfmts/macho/tests/nasm64/Makefile.inc
include modules/objfmts/macho/tests/nasm32/Makefile.inc
include modules/objfmts/macho/tests/nasm64/Makefile.inc

@ -0,0 +1,9 @@
# $Id$
TESTS += modules/objfmts/macho/tests/nasm32/macho32_test.sh
EXTRA_DIST += modules/objfmts/macho/tests/nasm32/machotest.c
EXTRA_DIST += modules/objfmts/macho/tests/nasm32/machotest.asm
EXTRA_DIST += modules/objfmts/macho/tests/nasm32/machotest.hex
EXTRA_DIST += modules/objfmts/macho/tests/nasm32/reloc.asm
EXTRA_DIST += modules/objfmts/macho/tests/nasm32/reloc.hex

@ -0,0 +1,4 @@
#! /bin/sh
# $Id$
${srcdir}/out_test.sh macho_test modules/objfmts/macho/tests/nasm32 "32-bit macho objfmt" "-f macho32" ".o"
exit $?

@ -0,0 +1,83 @@
; test source file for assembling to MACH-O
; build with :
; yasm -f macho machotest.asm
; gcc -o machotest machotest.c machotest.o
; This file should test the following:
; [1] Define and export a global text-section symbol
; [2] Define and export a global data-section symbol
; [3] Define and export a global BSS-section symbol
; [4] Define a non-global text-section symbol
; [5] Define a non-global data-section symbol
; [6] Define a non-global BSS-section symbol
; [7] Define a COMMON symbol
; [8] Define a NASM local label
; [9] Reference a NASM local label
; [10] Import an external symbol (note: printf replaced by another call)
; [11] Make a PC-relative call to an external symbol
; [12] Reference a text-section symbol in the text section
; [13] Reference a data-section symbol in the text section
; [14] Reference a BSS-section symbol in the text section
; [15] Reference a text-section symbol in the data section
; [16] Reference a data-section symbol in the data section
; [17] Reference a BSS-section symbol in the data section
[BITS 32]
[GLOBAL _lrotate] ; [1]
[GLOBAL _greet] ; [1]
[GLOBAL _asmstr] ; [2]
[GLOBAL _textptr] ; [2]
[GLOBAL _selfptr] ; [2]
[GLOBAL _integer] ; [3]
[EXTERN _druck] ; [10]
[COMMON _commvar 4] ; [7]
[SECTION .text]
; prototype: long lrotate(long x, int num);
_lrotate: ; [1]
push ebp
mov ebp,esp
mov eax,[ebp+8]
mov ecx,[ebp+12]
.label rol eax,1 ; [4] [8]
loop .label ; [9] [12]
mov esp,ebp
pop ebp
ret
; prototype: void greet(void);
_greet
mov eax,[_integer] ; [14]
inc eax
mov [localint],eax ; [14]
push dword [_commvar]
mov eax,[localptr] ; [13]
push dword [eax]
push dword [_integer] ; [1] [14]
push dword _printfstr ; [13]
call _druck ; [11]
add esp,16
ret
[SECTION .data]
; a string
_asmstr db 'hello, world', 0 ; [2]
; a string for Printf
_printfstr db "integer==%d, localint==%d, commvar=%d"
db 10, 0
; some pointers
localptr dd localint ; [5] [17]
_textptr dd _greet ; [15]
_selfptr dd _selfptr ; [16]
[SECTION .bss]
; an integer
_integer resd 1 ; [3]
; a local integer
localint resd 1 ; [6]

@ -0,0 +1,45 @@
/*
* test source file for assembling to ELF
* copied from cofftest.c; s/coff/elf/g
* build with (under Linux, for example):
* yasm -f elf elftest.asm
* gcc -o elftest elftest.c elftest.o
*/
#include <stdio.h>
extern int lrotate(long, int);
extern void greet(void);
extern char asmstr[];
extern void *selfptr;
extern void *textptr;
extern int integer, commvar;
int main(void) {
printf("Testing lrotate: should get 0x00400000, 0x00000001\n");
printf("lrotate(0x00040000, 4) = 0x%08lx\n", lrotate(0x40000,4));
printf("lrotate(0x00040000, 14) = 0x%08lx\n", lrotate(0x40000,14));
printf("This string should read `hello, world': `%s'\n", asmstr);
printf("The integers here should be 1234, 1235 and 4321:\n");
integer = 1234;
commvar = 4321;
greet();
printf("These pointers should be equal: %p and %p\n",
&greet, textptr);
printf("So should these: %p and %p\n", selfptr, &selfptr);
}
/*
there is no support for dynamically linkable objects in current
mach-o module. Therefore put "printf" statement here and redirect
the asm call to druck()
*/
void druck( char *string, int a, int b, int c )
{
printf(string,a,b,c);
}

@ -0,0 +1,776 @@
ce
fa
ed
fe
07
00
00
00
03
00
00
00
01
00
00
00
02
00
00
00
1c
01
00
00
00
00
00
00
01
00
00
00
04
01
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
85
00
00
00
38
01
00
00
7d
00
00
00
07
00
00
00
07
00
00
00
03
00
00
00
00
00
00
00
5f
5f
74
65
78
74
00
2e
63
6f
6e
73
74
00
5f
5f
5f
5f
54
45
58
54
00
5f
5f
74
65
78
74
00
2e
63
00
00
00
00
3d
00
00
00
38
01
00
00
00
00
00
00
b8
01
00
00
07
00
00
00
00
03
00
80
00
00
00
00
00
00
00
00
5f
5f
64
61
74
61
00
2e
73
74
61
74
69
63
5f
64
5f
5f
44
41
54
41
00
5f
5f
6d
6f
64
5f
69
6e
69
3d
00
00
00
40
00
00
00
75
01
00
00
00
00
00
00
f0
01
00
00
03
00
00
00
00
01
00
00
00
00
00
00
00
00
00
00
5f
5f
62
73
73
00
2e
6f
62
6a
63
5f
63
6c
61
73
5f
5f
44
41
54
41
00
5f
5f
6d
6f
64
5f
69
6e
69
7d
00
00
00
08
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
01
00
00
00
00
00
00
00
00
00
00
00
02
00
00
00
18
00
00
00
08
02
00
00
0c
00
00
00
98
02
00
00
70
00
00
00
55
89
e5
8b
45
08
8b
4d
0c
d1
c0
e2
fc
89
ec
5d
c3
a1
00
00
00
00
40
a3
04
00
00
00
ff
35
00
00
00
00
a1
71
00
00
00
ff
30
ff
35
00
00
00
00
68
4a
00
00
00
e8
c7
ff
ff
ff
83
c4
10
c3
68
65
6c
6c
6f
2c
20
77
6f
72
6c
64
00
69
6e
74
65
67
65
72
3d
3d
25
64
2c
20
6c
6f
63
61
6c
69
6e
74
3d
3d
25
64
2c
20
63
6f
6d
6d
76
61
72
3d
25
64
0a
00
04
00
00
00
11
00
00
00
79
00
00
00
00
00
00
12
00
00
00
03
00
00
04
18
00
00
00
03
00
00
04
1e
00
00
00
07
00
00
0c
23
00
00
00
02
00
00
04
2b
00
00
00
03
00
00
04
30
00
00
00
02
00
00
04
35
00
00
00
06
00
00
0d
34
00
00
00
03
00
00
04
38
00
00
00
01
00
00
04
3c
00
00
00
02
00
00
04
01
00
00
00
0f
01
00
00
00
00
00
00
0a
00
00
00
0f
01
00
00
11
00
00
00
11
00
00
00
0f
02
00
00
3d
00
00
00
19
00
00
00
0f
02
00
00
75
00
00
00
22
00
00
00
0f
02
00
00
79
00
00
00
2b
00
00
00
0f
03
00
00
00
00
00
00
34
00
00
00
01
00
01
00
00
00
00
00
3b
00
00
00
01
00
00
00
04
00
00
00
44
00
00
00
0e
01
00
00
09
00
00
00
53
00
00
00
0e
03
00
00
04
00
00
00
5c
00
00
00
0e
02
00
00
71
00
00
00
65
00
00
00
0e
02
00
00
4a
00
00
00
00
5f
6c
72
6f
74
61
74
65
00
5f
67
72
65
65
74
00
5f
61
73
6d
73
74
72
00
5f
74
65
78
74
70
74
72
00
5f
73
65
6c
66
70
74
72
00
5f
69
6e
74
65
67
65
72
00
5f
64
72
75
63
6b
00
5f
63
6f
6d
6d
76
61
72
00
5f
6c
72
6f
74
61
74
65
2e
6c
61
62
65
6c
00
6c
6f
63
61
6c
69
6e
74
00
6c
6f
63
61
6c
70
74
72
00
5f
70
72
69
6e
74
66
73
74
72
00

@ -0,0 +1,24 @@
[SECTION .data]
uhoh db 5
[GLOBAL blah]
blah dw 5
[SECTION .text]
[EXTERN hi]
[EXTERN hi]
[EXTERN bye]
mov eax, hi+2
mov eax, bye
mov eax, [hi]
mov eax, [bye+2]
mov eax, $$
mov eax, $
mov eax, $+4
mov eax, $-$$
;mov eax, uhoh wrt $$
;mov eax, hi+bye
;mov eax, bye+$
;mov eax, hi-$

@ -0,0 +1,410 @@
ce
fa
ed
fe
07
00
00
00
03
00
00
00
01
00
00
00
02
00
00
00
d8
00
00
00
00
00
00
00
01
00
00
00
c0
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
2b
00
00
00
f4
00
00
00
2b
00
00
00
07
00
00
00
07
00
00
00
02
00
00
00
00
00
00
00
5f
5f
74
65
78
74
00
2e
63
6f
6e
73
74
00
5f
5f
5f
5f
54
45
58
54
00
5f
5f
74
65
78
74
00
2e
63
00
00
00
00
28
00
00
00
f4
00
00
00
00
00
00
00
20
01
00
00
07
00
00
00
00
03
00
80
00
00
00
00
00
00
00
00
5f
5f
64
61
74
61
00
2e
73
74
61
74
69
63
5f
64
5f
5f
44
41
54
41
00
5f
5f
6d
6f
64
5f
69
6e
69
28
00
00
00
03
00
00
00
1c
01
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
02
00
00
00
18
00
00
00
58
01
00
00
04
00
00
00
88
01
00
00
12
00
00
00
b8
02
00
00
00
b8
00
00
00
00
a1
00
00
00
00
a1
02
00
00
00
b8
00
00
00
00
b8
19
00
00
00
b8
22
00
00
00
b8
23
00
00
00
05
05
00
00
01
00
00
00
02
00
00
0c
06
00
00
00
03
00
00
0c
0b
00
00
00
02
00
00
0c
10
00
00
00
03
00
00
0c
15
00
00
00
01
00
00
04
1a
00
00
00
01
00
00
04
1f
00
00
00
01
00
00
04
01
00
00
00
0e
02
00
00
28
00
00
00
06
00
00
00
0f
02
00
00
29
00
00
00
0b
00
00
00
01
00
01
00
00
00
00
00
0e
00
00
00
01
00
01
00
00
00
00
00
00
75
68
6f
68
00
62
6c
61
68
00
68
69
00
62
79
65
00

@ -0,0 +1,9 @@
# $Id$
TESTS += modules/objfmts/macho/tests/nasm64/macho64_test.sh
EXTRA_DIST += modules/objfmts/macho/tests/nasm64/machotest64.c
EXTRA_DIST += modules/objfmts/macho/tests/nasm64/machotest64.asm
EXTRA_DIST += modules/objfmts/macho/tests/nasm64/machotest64.hex
EXTRA_DIST += modules/objfmts/macho/tests/nasm64/reloc64-err.asm
EXTRA_DIST += modules/objfmts/macho/tests/nasm64/reloc64-err.errwarn

@ -0,0 +1,4 @@
#! /bin/sh
# $Id$
${srcdir}/out_test.sh macho_test modules/objfmts/macho/tests/nasm64 "64-bit macho objfmt" "-f macho64" ".o"
exit $?

@ -0,0 +1,107 @@
; test source file for assembling to MACH-O
; build with :
; yasm -f macho -m amd64 machotest64.asm
; gcc -m64 -o machotest64 machotest64.c machotest64.o
; This file should test the following:
; [1] Define and export a global text-section symbol
; [2] Define and export a global data-section symbol
; [3] Define and export a global BSS-section symbol
; [4] Define a non-global text-section symbol
; [5] Define a non-global data-section symbol
; [6] Define a non-global BSS-section symbol
; [7] Define a COMMON symbol
; [8] Define a NASM local label
; [9] Reference a NASM local label
; [10] Import an external symbol (note: printf replaced by another call)
; [11] Make a PC-relative call to an external symbol
; [12] Reference a text-section symbol in the text section
; [13] Reference a data-section symbol in the text section
; [14] Reference a BSS-section symbol in the text section
; [15] Reference a text-section symbol in the data section
; [16] Reference a data-section symbol in the data section
; [17] Reference a BSS-section symbol in the data section
; [18] Perform a 64 Bit relocation in the text section
[BITS 64]
[GLOBAL _lrotate] ; [1]
[GLOBAL _greet] ; [1]
[GLOBAL _asmstr] ; [2]
[GLOBAL _textptr] ; [2]
[GLOBAL _selfptr] ; [2]
[GLOBAL _integer] ; [3]
[EXTERN _druck] ; [10]
[COMMON _commvar 4] ; [7]
[GLOBAL _getstr] ;
[GLOBAL _readgreet] ;
[SECTION .text]
; prototype: long lrotate(long x, int num);
_lrotate: ; [1]
push rcx
mov rax,rdi
mov rcx,rsi
.label rol rax,1 ; [4] [8]
loop .label ; [9] [12]
pop rcx
ret
_getstr:
mov rax,qword _asmstr
ret
_readgreet:
mov rax,[qword localint] ; [18]
ret
_retrievelabel:
mov rax,[qword localptr]
ret
; prototype: void greet(void);
; calls "void druck(a,b,c,d);
_greet mov rax,[_integer wrt rip] ; [14]
inc rax
mov [localint wrt rip],rax ; [14]
push rdi
push rsi
push rdx
push rcx
mov rdi,qword _printfstr
mov rsi,[_integer wrt rip]
mov rdx,[localptr wrt rip]
mov rdx,[rdx]
mov rcx,[_commvar wrt rip]
call _druck
pop rcx
pop rdx
pop rsi
pop rdi
ret
[SECTION .data]
; a string for Printf
_printfstr db "integer==%d, localint==%d, commvar=%d"
db 10, 0
; some pointers
localptr dq localint ; [5] [17]
_textptr dq _greet ; [15]
_selfptr dq _selfptr ; [16]
;[section .data2 align=16]
; a string
_asmstr db 'hello, world', 0 ; [2]
[SECTION .bss]
; an integer
_integer resq 1 ; [3]
; a local integer
localint resq 1 ; [6]

@ -0,0 +1,56 @@
/*
* test source file for assembling to Mach-O
* copied from cofftest.c, adapted to current limitations
* in Mach-O module
* build with (under OSX Tiger/Leopard, for example):
* yasm -f macho -m amd64 machotest64.asm
* gcc -m64 -o machotest64 machotest64.c machotest64.o
*/
#include <stdio.h>
extern long lrotate(long, long);
extern void greet(void);
extern long readgreet(void);
extern char asmstr[];
extern void *selfptr;
extern void *textptr;
extern int integer, commvar;
extern char *getstr(void);
int main(void) {
printf("Testing lrotate: should get 0x0000000000400000, 0x0000000000000001\n");
printf("lrotate(0x00040000, 4 ) = 0x%016lx\n", lrotate(0x40000,4));
printf("lrotate(0x00040000, 46) = 0x%016lx\n", lrotate(0x40000,46));
printf("This string should read `hello, world': `%s'\n", asmstr);
{
long a,b;
a = (long)asmstr;
b = (long)getstr();
printf("The pointers %lx and %lx should be equal\n",a,b);
}
printf("This string should read `hello, world': `%s'\n", getstr());
printf("The integers here should be 1234, 1235 and 4321:\n");
integer = 1234;
commvar = 4321;
greet();
printf("The absolute addressing to the asm-local integer should yield in 1235:\n%ld\n",readgreet());
printf("These pointers should be equal: %p and %p\n",
&greet, textptr);
printf("So should these: %p and %p\n", selfptr, &selfptr);
}
/*
there is no support for dynamically linkable objects in current
mach-o module. Therefore put "printf" statement here and redirect
the asm call to druck()
*/
void druck( char *string, int a, int b, int c )
{
printf(string,a,b,c);
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,33 @@
[BITS 64]
[SECTION .data]
uhoh db 5
[GLOBAL blah]
blah dw 5
[GLOBAL aha]
aha dq blah
aha2 dq blah+4
aha3 dq blah-uhoh
[SECTION .text]
[EXTERN hi]
[EXTERN hi]
[EXTERN bye]
[BITS 64]
mov rax, hi+2
mov rax, bye
mov rax, [qword hi]
mov rdi, [rip+ hi]
mov rax, [bye+2]
mov rax, $$
mov rax, $
mov rax, $+4
; the line below crashes yasm...
; mov rax, $-$$
;mov eax, uhoh wrt $$
;mov eax, hi+bye
;mov eax, bye+$
;mov eax, hi-$

@ -0,0 +1,2 @@
-:20: macho: sorry, cannot apply 32 bit absolute relocations in 64 bit mode.
Loading…
Cancel
Save