1 ;; --------------------------------------------------------------------------
3 ;; Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 ;; See the file AUTHORS included with the NASM distribution for
5 ;; the specific copyright holders.
7 ;; Redistribution and use in source and binary forms, with or without
8 ;; modification, are permitted provided that the following
11 ;; * Redistributions of source code must retain the above copyright
12 ;; notice, this list of conditions and the following disclaimer.
13 ;; * Redistributions in binary form must reproduce the above
14 ;; copyright notice, this list of conditions and the following
15 ;; disclaimer in the documentation and/or other materials provided
16 ;; with the distribution.
18 ;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 ;; CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 ;; INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 ;; MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 ;; DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 ;; CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 ;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 ;; NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 ;; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 ;; HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 ;; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 ;; OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 ;; EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;; --------------------------------------------------------------------------
35 ; Smart alignment macros
39 %imacro alignmode 1-2.nolist
41 %define __ALIGN_JMP_THRESHOLD__ 16
43 %define __ALIGN_16BIT_1B__ 0x90
44 %define __ALIGN_16BIT_GROUP__ 1
46 %define __ALIGN_32BIT_1B__ 0x90
47 %define __ALIGN_32BIT_GROUP__ 1
49 %define __ALIGN_64BIT_1B__ 0x90
50 %define __ALIGN_64BIT_GROUP__ 1
52 %define __ALIGN_JMP_THRESHOLD__ 8
54 %define __ALIGN_16BIT_1B__ 0x90
55 %define __ALIGN_16BIT_2B__ 0x89,0xf6
56 %define __ALIGN_16BIT_3B__ 0x8d,0x74,0x00
57 %define __ALIGN_16BIT_4B__ 0x8d,0xb4,0x00,0x00
58 %define __ALIGN_16BIT_5B__ 0x8d,0xb4,0x00,0x00,0x90
59 %define __ALIGN_16BIT_6B__ 0x8d,0xb4,0x00,0x00,0x89,0xff
60 %define __ALIGN_16BIT_7B__ 0x8d,0xb4,0x00,0x00,0x8d,0x7d,0x00
61 %define __ALIGN_16BIT_8B__ 0x8d,0xb4,0x00,0x00,0x8d,0xbd,0x00,0x00
62 %define __ALIGN_16BIT_GROUP__ 8
64 %define __ALIGN_32BIT_1B__ 0x90
65 %define __ALIGN_32BIT_2B__ 0x89,0xf6
66 %define __ALIGN_32BIT_3B__ 0x8d,0x76,0x00
67 %define __ALIGN_32BIT_4B__ 0x8d,0x74,0x26,0x00
68 %define __ALIGN_32BIT_5B__ 0x90,0x8d,0x74,0x26,0x00
69 %define __ALIGN_32BIT_6B__ 0x8d,0xb6,0x00,0x00,0x00,0x00
70 %define __ALIGN_32BIT_7B__ 0x8d,0xb4,0x26,0x00,0x00,0x00,0x00
71 %define __ALIGN_32BIT_GROUP__ 7
73 %define __ALIGN_64BIT_1B__ 0x90
74 %define __ALIGN_64BIT_2B__ 0x66,0x90
75 %define __ALIGN_64BIT_3B__ 0x66,0x66,0x90
76 %define __ALIGN_64BIT_4B__ 0x66,0x66,0x66,0x90
77 %define __ALIGN_64BIT_GROUP__ 4
79 %define __ALIGN_JMP_THRESHOLD__ 16
81 %define __ALIGN_16BIT_1B__ 0x90
82 %define __ALIGN_16BIT_2B__ 0x66,0x90
83 %define __ALIGN_16BIT_3B__ 0x66,0x66,0x90
84 %define __ALIGN_16BIT_4B__ 0x66,0x66,0x66,0x90
85 %define __ALIGN_16BIT_GROUP__ 4
87 %define __ALIGN_32BIT_1B__ 0x90
88 %define __ALIGN_32BIT_2B__ 0x66,0x90
89 %define __ALIGN_32BIT_3B__ 0x66,0x66,0x90
90 %define __ALIGN_32BIT_4B__ 0x66,0x66,0x66,0x90
91 %define __ALIGN_32BIT_GROUP__ 4
93 %define __ALIGN_64BIT_1B__ 0x90
94 %define __ALIGN_64BIT_2B__ 0x66,0x90
95 %define __ALIGN_64BIT_3B__ 0x66,0x66,0x90
96 %define __ALIGN_64BIT_4B__ 0x66,0x66,0x66,0x90
97 %define __ALIGN_64BIT_GROUP__ 4
99 %define __ALIGN_JMP_THRESHOLD__ 16
101 %define __ALIGN_16BIT_1B__ 0x90
102 %define __ALIGN_16BIT_2B__ 0x66,0x90
103 %define __ALIGN_16BIT_3B__ 0x66,0x66,0x90
104 %define __ALIGN_16BIT_4B__ 0x66,0x66,0x66,0x90
105 %define __ALIGN_64BIT_GROUP__ 4
107 %define __ALIGN_32BIT_1B__ 0x90
108 %define __ALIGN_32BIT_2B__ 0x8b,0xc0
109 %define __ALIGN_32BIT_3B__ 0x8d,0x04,0x20
110 %define __ALIGN_32BIT_4B__ 0x8d,0x44,0x20,0x00
111 %define __ALIGN_32BIT_5B__ 0x8d,0x44,0x20,0x00,0x90
112 %define __ALIGN_32BIT_6B__ 0x8d,0x80,0x00,0x00,0x00,0x00
113 %define __ALIGN_32BIT_7B__ 0x8d,0x04,0x05,0x00,0x00,0x00,0x00
114 %define __ALIGN_32BIT_GROUP__ 7
116 %define __ALIGN_64BIT_1B__ 0x90
117 %define __ALIGN_64BIT_2B__ 0x66,0x90
118 %define __ALIGN_64BIT_3B__ 0x66,0x66,0x90
119 %define __ALIGN_64BIT_4B__ 0x66,0x66,0x66,0x90
120 %define __ALIGN_64BIT_GROUP__ 4
122 %define __ALIGN_JMP_THRESHOLD__ 16
124 %define __ALIGN_16BIT_1B__ 0x90
125 %define __ALIGN_16BIT_2B__ 0x66,0x90
126 %define __ALIGN_16BIT_3B__ 0x0f,0x1f,0x00
127 %define __ALIGN_16BIT_4B__ 0x0f,0x1f,0x40,0x00
128 %define __ALIGN_16BIT_GROUP__ 4
130 %define __ALIGN_32BIT_1B__ 0x90
131 %define __ALIGN_32BIT_2B__ 0x66,0x90
132 %define __ALIGN_32BIT_3B__ 0x0f,0x1f,0x00
133 %define __ALIGN_32BIT_4B__ 0x0f,0x1f,0x40,0x00
134 %define __ALIGN_32BIT_5B__ 0x0f,0x1f,0x44,0x00,0x00
135 %define __ALIGN_32BIT_6B__ 0x66,0x0f,0x1f,0x44,0x00,0x00
136 %define __ALIGN_32BIT_7B__ 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00
137 %define __ALIGN_32BIT_8B__ 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
138 %define __ALIGN_32BIT_GROUP__ 8
140 %define __ALIGN_64BIT_1B__ 0x90
141 %define __ALIGN_64BIT_2B__ 0x66,0x90
142 %define __ALIGN_64BIT_3B__ 0x0f,0x1f,0x00
143 %define __ALIGN_64BIT_4B__ 0x0f,0x1f,0x40,0x00
144 %define __ALIGN_64BIT_5B__ 0x0f,0x1f,0x44,0x00,0x00
145 %define __ALIGN_64BIT_6B__ 0x66,0x0f,0x1f,0x44,0x00,0x00
146 %define __ALIGN_64BIT_7B__ 0x0f,0x1f,0x80,0x00,0x00,0x00,0x00
147 %define __ALIGN_64BIT_8B__ 0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00
148 %define __ALIGN_64BIT_GROUP__ 8
150 %error unknown alignment mode: %1
154 %xdefine __ALIGN_JMP_THRESHOLD__ -1
156 %xdefine __ALIGN_JMP_THRESHOLD__ %2
159 %xdefine __ALIGNMODE__ %1,__ALIGN_JMP_THRESHOLD__
162 %unimacro align 1-2+.nolist
163 %imacro align 1-2+.nolist
164 sectalign %1 ; align a segment as well
166 times (((%1) - (($-$$) % (%1))) % (%1)) %2
169 %assign %$pad (((%1) - (($-$$) % (%1))) % (%1))
170 %if __ALIGN_JMP_THRESHOLD__ != -1 && %$pad > __ALIGN_JMP_THRESHOLD__
172 ; We can't re-use %$pad here as $ will have changed!
173 times (((%1) - (($-$$) % (%1))) % (%1)) nop
176 times (%$pad / __ALIGN_%[__BITS__]BIT_GROUP__) \
177 db __ALIGN_%[__BITS__]BIT_%[__ALIGN_%[__BITS__]BIT_GROUP__]B__
178 %assign %$pad %$pad % __ALIGN_%[__BITS__]BIT_GROUP__
180 db __ALIGN_%[__BITS__]BIT_%[%$pad]B__