]> git.sesse.net Git - x264/blob - common/i386/i386inc.asm
c5a97b46c4508c7fb19cf2d20e8a612343d88729
[x264] / common / i386 / i386inc.asm
1 ;*****************************************************************************
2 ;* i386inc.asm: h264 encoder library
3 ;*****************************************************************************
4 ;* Copyright (C) 2006 x264 project
5 ;*
6 ;* Author: Sam Hocevar <sam@zoy.org>
7 ;*
8 ;* This program is free software; you can redistribute it and/or modify
9 ;* it under the terms of the GNU General Public License as published by
10 ;* the Free Software Foundation; either version 2 of the License, or
11 ;* (at your option) any later version.
12 ;*
13 ;* This program is distributed in the hope that it will be useful,
14 ;* but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;* GNU General Public License for more details.
17 ;*
18 ;* You should have received a copy of the GNU General Public License
19 ;* along with this program; if not, write to the Free Software
20 ;* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21 ;*****************************************************************************
22
23 BITS 32
24
25 ;=============================================================================
26 ; Macros and other preprocessor constants
27 ;=============================================================================
28
29 ; Symbol prefix for C linkage
30 %macro cglobal 1
31     %ifdef PREFIX
32         global _%1
33         %define %1 _%1
34     %else
35         global %1
36     %endif
37     align 16
38     %1:
39 %endmacro
40
41 %macro cextern 1
42     %ifdef PREFIX
43         extern _%1
44         %define %1 _%1
45     %else
46         extern %1
47     %endif
48 %endmacro
49
50 ; Name of the .rodata section. On OS X we cannot use .rodata because NASM
51 ; is unable to compute address offsets outside of .text so we use the .text
52 ; section instead until NASM is fixed.
53 %macro SECTION_RODATA 0
54     %ifidn __OUTPUT_FORMAT__,macho
55         SECTION .text align=16
56         fakegot:
57     %else
58         SECTION .rodata align=16
59     %endif
60 %endmacro
61
62 ; PIC support macros. All these macros are totally harmless when __PIC__ is
63 ; not defined but can ruin everything if misused in PIC mode. On x86, shared
64 ; objects cannot directly access global variables by address, they need to
65 ; go through the GOT (global offset table). Most OSes do not care about it
66 ; and let you load non-shared .so objects (Linux, Win32...). However, OS X
67 ; requires PIC code in its .dylib objects.
68 ;
69 ; - GLOBAL should be used as a suffix for global addressing, eg.
70 ;     picgetgot ebx
71 ;     mov eax, [foo GLOBAL]
72 ;   instead of
73 ;     mov eax, [foo]
74 ;
75 ; - picgetgot computes the GOT address into the given register in PIC
76 ;   mode, otherwise does nothing. You need to do this before using GLOBAL.
77 ;   Before in both execution order and compiled code order (so GLOBAL knows
78 ;   which register the GOT is in).
79 ;
80 ; - picpush and picpop respectively push and pop the given register
81 ;   in PIC mode, otherwise do nothing. You should always use them around
82 ;   picgetgot except when sure that the register is no longer used and is
83 ;   being restored later by other means.
84 ;
85 ; - picesp is defined to compensate the changing of esp when pushing
86 ;   a register into the stack, eg.
87 ;     mov eax, [esp + 8]
88 ;     pushpic  ebx
89 ;     mov eax, [picesp + 12]
90 ;   instead of
91 ;     mov eax, [esp + 8]
92 ;     pushpic  ebx
93 ;     mov eax, [esp + 12]
94 ;
95 %ifdef __PIC__
96     %ifidn __OUTPUT_FORMAT__,macho
97         ; There is no real global offset table on OS X, but we still
98         ; need to reference our variables by offset.
99         %define GOT_reg(x) - fakegot + x
100         %macro picgetgot 1
101             call %%getgot 
102           %%getgot: 
103             pop %1 
104             add %1, $$ - %%getgot
105             %undef GLOBAL
106             %define GLOBAL GOT_reg(%1)
107         %endmacro
108     %else
109         %ifidn __OUTPUT_FORMAT__,elf
110             %define GOT _GLOBAL_OFFSET_TABLE_
111         %else ; for a.out
112             %define GOT __GLOBAL_OFFSET_TABLE_
113         %endif
114         extern GOT
115         %define GOT_reg(x) + x wrt ..gotoff
116         %macro picgetgot 1
117             call %%getgot 
118           %%getgot: 
119             pop %1 
120             add %1, GOT + $$ - %%getgot wrt ..gotpc 
121             %undef GLOBAL
122             %define GLOBAL GOT_reg(%1)
123         %endmacro
124     %endif
125     %macro picpush 1
126         push %1
127     %endmacro
128     %macro picpop 1
129         pop %1
130     %endmacro
131     %define picesp esp+4
132 %else
133     %define GLOBAL
134     %macro picgetgot 1
135     %endmacro
136     %macro picpush 1
137     %endmacro
138     %macro picpop 1
139     %endmacro
140     %define picesp esp
141 %endif
142
143 %assign FENC_STRIDE 16
144 %assign FDEC_STRIDE 32
145
146 ; This is needed for ELF, otherwise the GNU linker assumes the stack is
147 ; executable by default.
148 %ifidn __OUTPUT_FORMAT__,elf
149 SECTION ".note.GNU-stack" noalloc noexec nowrite progbits
150 %endif
151