aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/uzlib/src/nasm/nasmlcm.inc
blob: 5cbe7e0d37a6601fef6453ac395775a2f40ae5ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
;;
;; NASM linker compatibility macros 2002.07.24
;;
;; Copyright (c) 2001-2003 by Joergen Ibsen / Jibz
;; All Rights Reserved
;;
;; http://www.ibsensoftware.com/
;;

; define _ELF_ for ELF32 object files
; define _OBJ_ for OMF object files
; define _OBJ_ and _DLL_ for OMF object files for a dll (stdcall)
; define _MSLIBS_ for MS style Win32 import libs (lcmwinextern)
; default is DJGPP/WIN32 COFF object files

; remember to do lcm*extern before lcmimport

; ====================================================================
;
; There are differences between how the object formats that NASM
; supports work, and what features they support. Similarly there
; are differences between how complete and standard compliant the
; support for these formats are in linkers.
;
; The NASM linker compatibility macros (nasmlcm) were put together
; to ease my work by allowing a single source file to be assembled
; for use with a number of compilers/linkers.
;
; Currently obj/omf, win32/coff, djgpp/coff and elf32 output formats
; are supported. The following macros are available:
;
;   lcmtext       - section name for the code section
;   lcmdata       - section name for the initialized data section
;   lcmbss        - section name for the uninitialized data section
;
;   lcmglobal     - declare a function (two arguments) or data (one
;                   argument) as global in the current format
;   lcmcglobal    - same as lcmglobal, but uses C name decoration
;
;   lcmextern     - declare a function (two arguments) or data (one
;                   argument) as extern in the current format
;   lcmcextern    - same as lcmextern, but uses C name decoration
;   lcmdllextern  - same as lcmextern, but uses dll name decoration
;   lcmwinextern  - same as lcmextern, but uses name decoration for
;                   calling Win32 Api functions (see _MSLIBS_)
;
;   lcmimport     - declares a function (two arguments) or data (one
;                   argument) as imported in the current format
;   lcmexport     - declares a function (two arguments) or data (one
;                   argument) as exported in the current format
;
;   lcmlabel      - start label for a function in the current format
;   lcmclabel     - start label for a function with C name decoration
;   lcmadjust     - adjust stack after a function call in the current
;                   format
;   lcmret        - return from a function in the current format
;   lcmcret       - return from a C function
;
; The following defines change the format and behaviour:
;
;   _ELF_     -  the lcm*global macro adds :function and :data type
;                specifiers
;
;   _OBJ_     -  section names are similar to those produced by
;                Borland tools to increase compatibility with
;                various OMF compatible linkers
;
;   _DLL_     -  functions are exported and imported with added
;                size specifiers (_SomeFunction@12), lcmret adjusts
;                stack (stdcall)
;
;   _MSLIBS_  -  the lcmwinextern macro prepends an underscore and
;                adds size specification for functions, allowing
;                the object file to be linked with MS libraries.
;
; ====================================================================

%ifndef NASMLCM_INC_INCLUDED
%define NASMLCM_INC_INCLUDED

%ifdef _DLL_
  %ifndef _OBJ_
    %error "_DLL_ needs _OBJ_ defined!"
  %endif
%endif

; --- define lcm- section names ---
;
;  a number of linkers require omf objects where the section
;  names are equal to those produces by tasm.

%ifdef _OBJ_

  %define lcmtext _TEXT class=CODE public use32 align=4 FLAT
  %define lcmdata _DATA class=DATA public use32 align=4
  %define lcmbss _BSS class=BSS public use32 align=4 FLAT

  group FLAT
  group DGROUP _DATA

%else ; _OBJ_

  %define lcmtext .text
  %define lcmdata .data
  %define lcmbss .bss

%endif ; _OBJ_

; --- define lcmglobal and lcm*extern macros ---
;
;  special handling of functions and data for ELF32

%ifdef _ELF_

  %macro lcmglobal 2
    global %{1}:function
  %endmacro
  %macro lcmglobal 1
    global %{1}:data
  %endmacro

  %define lcmcglobal lcmglobal

  %macro lcmextern 1-2
    extern %1
  %endmacro

  %macro lcmcextern 0
    %error lcmcextern not supported in ELF format
  %endmacro

  %macro lcmdllextern 0
    %error lcmdllextern not supported in ELF format
  %endmacro

%else ; _ELF_

  %ifdef _DLL_

    %macro lcmglobal 2
      global _%1
      global _%1@%2
    %endmacro
    %macro lcmglobal 1
      global _%1
      %define %1 _%1
    %endmacro

    %macro lcmcglobal 2
      global _%1
    %endmacro
    %macro lcmcglobal 1
      global _%1
      %define %1 _%1
    %endmacro

    %macro lcmextern 2
      extern _%1@%2
      %define %1 _%1@%2
    %endmacro
    %macro lcmextern 1
      extern _%1
      %define %1 _%1
    %endmacro

  %else

    %macro lcmglobal 2
      global _%1
    %endmacro
    %macro lcmglobal 1
      global _%1
      %define %1 _%1
    %endmacro

    %define lcmcglobal lcmglobal

    %macro lcmextern 1-2
      extern _%1
      %define %1 _%1
    %endmacro

  %endif

  %macro lcmcextern 1-2
    extern _%1
    %define %1 _%1
  %endmacro

  %macro lcmdllextern 2
    extern _%1@%2
    %define %1 _%1@%2
  %endmacro
  %macro lcmdllextern 1
    extern _%1
    %define %1 _%1
  %endmacro

  %macro lcmwinextern 2
    %ifdef _MSLIBS_
      extern _%1@%2
      %define %1 _%1@%2
    %else
      extern %1
    %endif
  %endmacro

%endif ; _ELF_

; --- define lcmimport and lcmexport ---
;

%ifdef _OBJ_

  %macro lcmimport 2-3
    import %1 %2 %3
    %rotate 1
  %endmacro

  %ifdef _DLL_

    %macro lcmexport 2
      export _%1
      export _%1@%2
    %endmacro
    %macro lcmexport 1
      export _%1
    %endmacro

  %else

    %macro lcmexport 1-2
    %endmacro

  %endif

%else ; _OBJ_

  %macro lcmimport 2-3
  %endmacro

  %macro lcmexport 1-2
  %endmacro

%endif ; _OBJ_

; --- define lcmlabel, lcmadjust and lcmret macros ---
;
;  we need special labels and stdcall calling convention when
;  assembling for a dll

%ifdef _ELF_

  %macro lcmlabel 2
    %1:
  %endmacro

  %define lcmclabel lcmlabel

  %macro lcmadjust 1
    %if %1 < 128
      add esp, byte %1
    %else
      add esp, %1
    %endif
  %endmacro

  %macro lcmret 1
    ret
  %endmacro

  %define lcmcret lcmret

%else ; _ELF_

  %ifdef _DLL_
    %macro lcmlabel 2
      _%1:
      _%1@%2:
    %endmacro

    %macro lcmclabel 2
      _%1:
    %endmacro

    %macro lcmadjust 1
    %endmacro

    %macro lcmret 1
      %if %1 > 0
        ret %1
      %else
        ret
      %endif
    %endmacro

    %macro lcmcret 1
      ret
    %endmacro

  %else

    %macro lcmlabel 2
      _%1:
    %endmacro

    %define lcmclabel lcmlabel

    %macro lcmadjust 1
      %if %1 < 128
        add esp, byte %1
      %else
        add esp, %1
      %endif
    %endmacro

    %macro lcmret 1
      ret
    %endmacro

    %define lcmcret lcmret
  %endif

%endif ; _ELF_

%endif ; NASMLCM_INC_INCLUDED