base/asm-common.h: Add include guards.
[catacomb] / base / asm-common.h
1 /// -*- mode: asm; asm-comment-char: ?/ -*-
2 ///
3 /// Common definitions for asesembler source files
4 ///
5 /// (c) 2015 Straylight/Edgeware
6 ///
7
8 ///----- Licensing notice ---------------------------------------------------
9 ///
10 /// This file is part of Catacomb.
11 ///
12 /// Catacomb is free software; you can redistribute it and/or modify
13 /// it under the terms of the GNU Library General Public License as
14 /// published by the Free Software Foundation; either version 2 of the
15 /// License, or (at your option) any later version.
16 ///
17 /// Catacomb is distributed in the hope that it will be useful,
18 /// but WITHOUT ANY WARRANTY; without even the implied warranty of
19 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 /// GNU Library General Public License for more details.
21 ///
22 /// You should have received a copy of the GNU Library General Public
23 /// License along with Catacomb; if not, write to the Free
24 /// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 /// MA 02111-1307, USA.
26
27 #ifndef CATACOMB_ASM_COMMON_H
28 #define CATACOMB_ASM_COMMON_H
29
30 ///--------------------------------------------------------------------------
31 /// General definitions.
32
33 // Preprocessor hacks.
34 #define STRINGY(x) _STRINGY(x, y)
35 #define _STRINGY(x) #x
36 #define GLUE(x, y) _GLUE(x, y)
37 #define _GLUE(x, y) x##y
38 #define _EMPTY
39
40 // Some useful variables.
41 .L$_subsec = 0
42
43 // Literal pools done the hard way.
44 #define _LIT .text .L$_subsec + 1
45 #define _ENDLIT .text .L$_subsec
46 #define _LTORG .L$_subsec = .L$_subsec + 2; .text .L$_subsec
47
48 // ELF section types.
49 #if __ELF__
50 # if CPUFAM_ARMEL
51 # define _SECTTY(ty) %ty
52 # else
53 # define _SECTTY(ty) @ty
54 # endif
55 #endif
56
57 // Section selection.
58 #define TEXT .text .L$_subsec
59 #if ABI_WIN
60 # define RODATA .section .rdata, "dr"
61 #elif __ELF__
62 # define RODATA .section .rodata, "a", _SECTTY(progbits)
63 #else
64 # define RODATA TEXT
65 #endif
66 #define DATA .data
67
68 // Announcing an internal function.
69 #define INTFUNC(name) \
70 TYPE_FUNC(name); \
71 .macro ENDFUNC; _ENDFUNC(name); .endm; \
72 .L$_prologue_p = 0; .L$_frameptr_p = 0; \
73 FUNC_PREHOOK(name); \
74 name: \
75 FUNC_POSTHOOK(name)
76
77 // Announcing an external function.
78 #define FUNC(name) \
79 .globl F(name); \
80 INTFUNC(F(name))
81
82 // Marking the end of a function.
83 #define _ENDFUNC(name) \
84 .if ~ .L$_prologue_p; .error "Missing `endprologue'"; .endif; \
85 .if .L$_frameptr_p; .purgem dropfp; .endif; \
86 .purgem ENDFUNC; \
87 SIZE_OBJ(name); \
88 ENDFUNC_HOOK(name); \
89 _LTORG
90
91 // Make a helper function, if necessary.
92 #define AUXFN(name) \
93 .ifndef .L$_auxfn_def.name; \
94 .text 7128; \
95 .macro _ENDAUXFN; _ENDAUXFN_TAIL(name); .endm; \
96 FUNC_PREHOOK(name); \
97 name:
98 #define _ENDAUXFN_TAIL(name) \
99 .purgem _ENDAUXFN; \
100 .text .L$_subsec; \
101 .L$_auxfn_def.name = 1
102 #define ENDAUXFN _ENDAUXFN; .endif
103
104 ///--------------------------------------------------------------------------
105 /// ELF-specific hacking.
106
107 #if __ELF__
108
109 #if __PIC__ || __PIE__
110 # define WANT_PIC 1
111 #endif
112
113 #define TYPE_FUNC(name) .type name, STT_FUNC
114
115 #define SIZE_OBJ(name) .size name, . - name
116
117 #endif
118
119 ///--------------------------------------------------------------------------
120 /// Windows-specific hacking.
121
122 #if ABI_WIN
123
124 #if CPUFAM_X86
125 # define F(name) _##name
126 #endif
127
128 #endif
129
130 ///--------------------------------------------------------------------------
131 /// x86- and amd64-specific hacking.
132 ///
133 /// It's (slightly) easier to deal with both of these in one go.
134
135 #if CPUFAM_X86 || CPUFAM_AMD64
136
137 // Word size.
138 #if CPUFAM_X86
139 # define WORDSZ 4
140 #endif
141 #if CPUFAM_AMD64
142 # define WORDSZ 8
143 #endif
144
145 // Set the function hooks.
146 #define FUNC_PREHOOK(_) .balign 16
147
148 // On Windows, arrange to install stack-unwinding data.
149 #if CPUFAM_AMD64 && ABI_WIN
150 # define FUNC_POSTHOOK(name) .seh_proc name
151 # define ENDFUNC_HOOK(_) .seh_endproc
152 // Procedures are expected to invoke `.seh_setframe' if necessary, and
153 // `.seh_pushreg' and friends, and `.seh_endprologue'.
154 #endif
155
156 #if __ELF__
157 # define FUNC_POSTHOOK(_) .cfi_startproc
158 # define ENDFUNC_HOOK(_) .cfi_endproc
159 #endif
160
161 // Don't use the wretched AT&T syntax. It's festooned with pointless
162 // punctuation, and all of the data movement is backwards. Ugh!
163 .intel_syntax noprefix
164
165 // Call external subroutine at ADDR, possibly via PLT.
166 .macro callext addr
167 #if WANT_PIC
168 call \addr@PLT
169 #else
170 call \addr
171 #endif
172 .endm
173
174 // Do I need to arrange a spare GOT register?
175 #if WANT_PIC && CPUFAM_X86
176 # define NEED_GOT 1
177 #endif
178 #define GOTREG ebx // Not needed in AMD64 so don't care.
179
180 // Maybe load GOT address into GOT.
181 .macro ldgot got=GOTREG
182 #if WANT_PIC && CPUFAM_X86
183 AUXFN(_ldgot.\got)
184 mov \got, [esp]
185 ret
186 ENDAUXFN
187 call _ldgot.\got
188 add \got, offset _GLOBAL_OFFSET_TABLE_
189 #endif
190 .endm
191
192 // Load address of external symbol ADDR into REG, maybe using GOT.
193 .macro leaext reg, addr, got=GOTREG
194 #if WANT_PIC
195 # if CPUFAM_X86
196 mov \reg, [\got + \addr@GOT]
197 # endif
198 # if CPUFAM_AMD64
199 mov \reg, \addr@GOTPCREL[rip]
200 # endif
201 #else
202 # if CPUFAM_X86
203 mov \reg, offset \addr
204 # endif
205 # if CPUFAM_AMD64
206 lea \reg, \addr[rip]
207 # endif
208 #endif
209 .endm
210
211 // Address expression (possibly using a base register, and a displacement)
212 // referring to ADDR, which is within our module, maybe using GOT.
213 #define INTADDR(...) INTADDR__0(__VA_ARGS__, GOTREG, dummy)
214 #define INTADDR__0(addr, got, ...) INTADDR__1(addr, got)
215 #if CPUFAM_AMD64
216 # define INTADDR__1(addr, got) addr + rip
217 #elif WANT_PIC
218 # define INTADDR__1(addr, got) got + addr@GOTOFF
219 #else
220 # define INTADDR__1(addr, got) addr
221 #endif
222
223 // Permutations for SIMD instructions. SHUF(A, B, C, D) is an immediate,
224 // suitable for use in `pshufd' or `shufpd', which copies element A
225 // (0 <= A < 4) of the source to element 0 of the destination, element B to
226 // element 1, element C to element 2, and element D to element 3.
227 #define SHUF(a, b, c, d) ((a) + 4*(b) + 16*(c) + 64*(d))
228
229 // Map register names to their individual pieces.
230
231 // Apply decoration decor to (internal) register name reg of type ty.
232 //
233 // See `R_...' for internal register names. Decorations are as follows.
234 //
235 // b low byte (e.g., `al', `r8b')
236 // h high byte (e.g., `ah')
237 // w word (e.g., `ax', `r8w')
238 // d doubleword (e.g., `eax', `r8d')
239 // q quadword (e.g., `rax', `r8')
240 // r whole register (doubleword on x86, quadword on amd64)
241 //
242 // And types are as follows.
243 //
244 // abcd the four traditional registers `a', `b', `c', `d'
245 // xp the four pointer registers `si', `di', `bp', `sp'
246 // ip the instruction pointer `ip'
247 // rn the AMD64 numbered registers `r8'--`r15'
248 #define _DECOR(ty, decor, reg) _DECOR_##ty##_##decor(reg)
249
250 // Internal macros: _DECOR_ty_decor(reg) applies decoration decor to
251 // (internal) register name reg of type ty.
252
253 #define _DECOR_abcd_b(reg) reg##l
254 #define _DECOR_abcd_h(reg) reg##h
255 #define _DECOR_abcd_w(reg) reg##x
256 #define _DECOR_abcd_d(reg) e##reg##x
257 #if CPUFAM_AMD64
258 # define _DECOR_abcd_q(reg) r##reg##x
259 #endif
260
261 #define _DECOR_xp_b(reg) reg##l
262 #define _DECOR_xp_w(reg) reg
263 #define _DECOR_xp_d(reg) e##reg
264 #if CPUFAM_AMD64
265 # define _DECOR_xp_q(reg) r##reg
266 #endif
267
268 #define _DECOR_ip_w(reg) reg
269 #define _DECOR_ip_d(reg) e##reg
270 #if CPUFAM_AMD64
271 # define _DECOR_ip_q(reg) r##reg
272 #endif
273
274 #if CPUFAM_AMD64
275 # define _DECOR_rn_b(reg) reg##b
276 # define _DECOR_rn_w(reg) reg##w
277 # define _DECOR_rn_d(reg) reg##d
278 # define _DECOR_rn_q(reg) reg
279 # define _DECOR_rn_r(reg) reg
280 #endif
281
282 #if CPUFAM_X86
283 # define _DECOR_abcd_r(reg) e##reg##x
284 # define _DECOR_xp_r(reg) e##reg
285 # define _DECOR_ip_r(reg) e##reg
286 #endif
287 #if CPUFAM_AMD64
288 # define _DECOR_abcd_r(reg) r##reg##x
289 # define _DECOR_xp_r(reg) r##reg
290 # define _DECOR_ip_r(reg) r##reg
291 #endif
292
293 #define _DECOR_mem_b(addr) byte ptr addr
294 #define _DECOR_mem_w(addr) word ptr addr
295 #define _DECOR_mem_d(addr) dword ptr addr
296 #if CPUFAM_AMD64
297 # define _DECOR_mem_q(addr) qword ptr addr
298 #endif
299
300 // R_r(decor) applies decoration decor to register r, which is an internal
301 // register name. The internal register names are: `ip', `a', `b', `c', `d',
302 // `si', `di', `bp', `sp', `r8'--`r15'.
303 #define R_ip(decor) _DECOR(ip, decor, ip)
304 #define R_a(decor) _DECOR(abcd, decor, a)
305 #define R_b(decor) _DECOR(abcd, decor, b)
306 #define R_c(decor) _DECOR(abcd, decor, c)
307 #define R_d(decor) _DECOR(abcd, decor, d)
308 #define R_si(decor) _DECOR(xp, decor, si)
309 #define R_di(decor) _DECOR(xp, decor, di)
310 #define R_bp(decor) _DECOR(xp, decor, bp)
311 #define R_sp(decor) _DECOR(xp, decor, sp)
312 #if CPUFAM_AMD64
313 # define R_r8(decor) _DECOR(rn, decor, r8)
314 # define R_r9(decor) _DECOR(rn, decor, r9)
315 # define R_r10(decor) _DECOR(rn, decor, r10)
316 # define R_r11(decor) _DECOR(rn, decor, r11)
317 # define R_r12(decor) _DECOR(rn, decor, r12)
318 # define R_r13(decor) _DECOR(rn, decor, r13)
319 # define R_r14(decor) _DECOR(rn, decor, r14)
320 # define R_r15(decor) _DECOR(rn, decor, r15)
321 #endif
322
323 // Refer to an in-memory datum of the type implied by decor residing at
324 // address addr (which should supply its own square-brackets).
325 #define MEM(decor, addr) _DECOR(mem, decor, addr)
326
327 // Applies decoration decor to assembler-level register name reg.
328 #define _REGFORM(reg, decor) _GLUE(_REGFORM_, reg)(decor)
329
330 // Internal macros: _REGFORM_r(decor) applies decoration decor to an
331 // assembler-level register name, in place of any decoration that register
332 // name has already.
333
334 #define _REGFORM_ip(decor) R_ip(decor)
335 #define _REGFORM_eip(decor) R_ip(decor)
336
337 #define _REGFORM_a(decor) R_a(decor)
338 #define _REGFORM_al(decor) R_a(decor)
339 #define _REGFORM_ah(decor) R_a(decor)
340 #define _REGFORM_ax(decor) R_a(decor)
341 #define _REGFORM_eax(decor) R_a(decor)
342
343 #define _REGFORM_b(decor) R_b(decor)
344 #define _REGFORM_bl(decor) R_b(decor)
345 #define _REGFORM_bh(decor) R_b(decor)
346 #define _REGFORM_bx(decor) R_b(decor)
347 #define _REGFORM_ebx(decor) R_b(decor)
348
349 #define _REGFORM_c(decor) R_c(decor)
350 #define _REGFORM_cl(decor) R_c(decor)
351 #define _REGFORM_ch(decor) R_c(decor)
352 #define _REGFORM_cx(decor) R_c(decor)
353 #define _REGFORM_ecx(decor) R_c(decor)
354
355 #define _REGFORM_d(decor) R_d(decor)
356 #define _REGFORM_dl(decor) R_d(decor)
357 #define _REGFORM_dh(decor) R_d(decor)
358 #define _REGFORM_dx(decor) R_d(decor)
359 #define _REGFORM_edx(decor) R_d(decor)
360
361 #define _REGFORM_si(decor) R_si(decor)
362 #define _REGFORM_sil(decor) R_si(decor)
363 #define _REGFORM_esi(decor) R_si(decor)
364
365 #define _REGFORM_di(decor) R_di(decor)
366 #define _REGFORM_dil(decor) R_di(decor)
367 #define _REGFORM_edi(decor) R_di(decor)
368
369 #define _REGFORM_bp(decor) R_bp(decor)
370 #define _REGFORM_bpl(decor) R_bp(decor)
371 #define _REGFORM_ebp(decor) R_bp(decor)
372
373 #define _REGFORM_sp(decor) R_sp(decor)
374 #define _REGFORM_spl(decor) R_sp(decor)
375 #define _REGFORM_esp(decor) R_sp(decor)
376
377 #if CPUFAM_AMD64
378
379 # define _REGFORM_rip(decor) R_ip(decor)
380 # define _REGFORM_rsp(decor) R_sp(decor)
381 # define _REGFORM_rbp(decor) R_bp(decor)
382 # define _REGFORM_rdi(decor) R_di(decor)
383 # define _REGFORM_rsi(decor) R_si(decor)
384 # define _REGFORM_rdx(decor) R_d(decor)
385 # define _REGFORM_rcx(decor) R_c(decor)
386 # define _REGFORM_rbx(decor) R_b(decor)
387 # define _REGFORM_rax(decor) R_a(decor)
388
389 # define _REGFORM_r8(decor) R_r8(decor)
390 # define _REGFORM_r8b(decor) R_r8(decor)
391 # define _REGFORM_r8w(decor) R_r8(decor)
392 # define _REGFORM_r8d(decor) R_r8(decor)
393
394 # define _REGFORM_r9(decor) R_r9(decor)
395 # define _REGFORM_r9b(decor) R_r9(decor)
396 # define _REGFORM_r9w(decor) R_r9(decor)
397 # define _REGFORM_r9d(decor) R_r9(decor)
398
399 # define _REGFORM_r10(decor) R_r10(decor)
400 # define _REGFORM_r10b(decor) R_r10(decor)
401 # define _REGFORM_r10w(decor) R_r10(decor)
402 # define _REGFORM_r10d(decor) R_r10(decor)
403
404 # define _REGFORM_r11(decor) R_r11(decor)
405 # define _REGFORM_r11b(decor) R_r11(decor)
406 # define _REGFORM_r11w(decor) R_r11(decor)
407 # define _REGFORM_r11d(decor) R_r11(decor)
408
409 # define _REGFORM_r12(decor) R_r12(decor)
410 # define _REGFORM_r12b(decor) R_r12(decor)
411 # define _REGFORM_r12w(decor) R_r12(decor)
412 # define _REGFORM_r12d(decor) R_r12(decor)
413
414 # define _REGFORM_r13(decor) R_r13(decor)
415 # define _REGFORM_r13b(decor) R_r13(decor)
416 # define _REGFORM_r13w(decor) R_r13(decor)
417 # define _REGFORM_r13d(decor) R_r13(decor)
418
419 # define _REGFORM_r14(decor) R_r14(decor)
420 # define _REGFORM_r14b(decor) R_r14(decor)
421 # define _REGFORM_r14w(decor) R_r14(decor)
422 # define _REGFORM_r14d(decor) R_r14(decor)
423
424 # define _REGFORM_r15(decor) R_r15(decor)
425 # define _REGFORM_r15b(decor) R_r15(decor)
426 # define _REGFORM_r15w(decor) R_r15(decor)
427 # define _REGFORM_r15d(decor) R_r15(decor)
428
429 #endif
430
431 // Macros for converting register names.
432 #define BYTE(reg) _REGFORM(reg, b)
433 #define HIBYTE(reg) _REGFORM(reg, h)
434 #define WORD(reg) _REGFORM(reg, w)
435 #define DWORD(reg) _REGFORM(reg, d)
436 #if CPUFAM_AMD64
437 # define QWORD(reg) _REGFORM(reg, q)
438 #endif
439 #define WHOLE(reg) _REGFORM(reg, r)
440
441 // Stack management and unwinding.
442 .macro setfp fp, offset = 0
443 .if \offset == 0
444 mov \fp, R_sp(r)
445 #if __ELF__
446 .cfi_def_cfa_register \fp
447 #endif
448 #if ABI_WIN && CPUFAM_AMD64
449 .seh_setframe \fp, 0
450 #endif
451 .else
452 lea \fp, [R_sp(r) + \offset]
453 #if __ELF__
454 .cfi_def_cfa_register \fp
455 .cfi_adjust_cfa_offset -\offset
456 #endif
457 #if ABI_WIN && CPUFAM_AMD64
458 .seh_setframe \fp, \offset
459 #endif
460 .endif
461 .L$_frameptr_p = -1
462 .macro dropfp; _dropfp \fp, \offset; .endm
463 .endm
464
465 .macro _dropfp fp, offset = 0
466 .if \offset == 0
467 mov R_sp(r), \fp
468 #if __ELF__
469 .cfi_def_cfa_register R_sp(r)
470 #endif
471 .else
472 lea R_sp(r), [\fp - \offset]
473 #if __ELF__
474 .cfi_def_cfa_register R_sp(r)
475 .cfi_adjust_cfa_offset +\offset
476 #endif
477 .endif
478 .L$_frameptr_p = 0
479 .purgem dropfp
480 .endm
481
482 .macro stalloc n
483 sub R_sp(r), \n
484 #if __ELF__
485 .cfi_adjust_cfa_offset +\n
486 #endif
487 #if ABI_WIN && CPUFAM_AMD64
488 .seh_stackalloc \n
489 #endif
490 .endm
491
492 .macro stfree n
493 add R_sp(r), \n
494 #if __ELF__
495 .cfi_adjust_cfa_offset -\n
496 #endif
497 .endm
498
499 .macro pushreg r
500 push \r
501 #if __ELF__
502 .cfi_adjust_cfa_offset +WORDSZ
503 .cfi_rel_offset \r, 0
504 #endif
505 #if ABI_WIN && CPUFAM_AMD64
506 .seh_pushreg \r
507 #endif
508 .endm
509
510 .macro popreg r
511 pop \r
512 #if __ELF__
513 .cfi_adjust_cfa_offset -WORDSZ
514 .cfi_restore \r
515 #endif
516 .endm
517
518 .macro savexmm r, offset
519 movdqa [R_sp(r) + \offset], \r
520 #if ABI_WIN && CPUFAM_AMD64
521 .seh_savexmm \r, \offset
522 #endif
523 .endm
524
525 .macro rstrxmm r, offset
526 movdqa \r, [R_sp(r) + \offset]
527 .endm
528
529 .macro endprologue
530 #if ABI_WIN && CPUFAM_AMD64
531 .seh_endprologue
532 #endif
533 .L$_prologue_p = -1
534 .endm
535
536 #endif
537
538 #if CPUFAM_X86
539
540 .macro _reg.0
541 // Stash GP registers and establish temporary stack frame.
542 pushfd
543 push eax
544 push ecx
545 push edx
546 push ebp
547 mov ebp, esp
548 and esp, ~15
549 sub esp, 512
550 fxsave [esp]
551 .endm
552
553 .macro _reg.1
554 .endm
555
556 .macro _reg.2
557 .endm
558
559 .macro _reg.3 fmt
560 // Print FMT and the other established arguments.
561 lea eax, .L$_reg$msg.\@
562 push eax
563 call printf
564 jmp .L$_reg$cont.\@
565 .L$_reg$msg.\@:
566 .ascii ";; \fmt\n\0"
567 .L$_reg$cont.\@:
568 mov eax, ebp
569 and eax, ~15
570 sub eax, 512
571 fxrstor [eax]
572 mov esp, ebp
573 pop ebp
574 pop edx
575 pop ecx
576 pop eax
577 popfd
578 .endm
579
580 .macro msg msg
581 _reg.0
582 _reg.1
583 _reg.2
584 _reg.3 "\msg"
585 .endm
586
587 .macro reg r, msg
588 _reg.0
589 .ifeqs "\r", "esp"
590 lea eax, [ebp + 20]
591 push eax
592 .else
593 .ifeqs "\r", "ebp"
594 push [ebp]
595 .else
596 push \r
597 .endif
598 .endif
599 _reg.1
600 _reg.2
601 _reg.3 "\msg: \r = %08x"
602 .endm
603
604 .macro xmmreg r, msg
605 _reg.0
606 _reg.1
607 _reg.2
608 movdqu xmm0, \r
609 pshufd xmm0, xmm0, 0x1b
610 sub esp, 16
611 movdqa [esp], xmm0
612 _reg.3 "\msg: \r = %08x %08x %08x %08x"
613 .endm
614
615 .macro mmreg r, msg
616 _reg.0
617 _reg.1
618 _reg.2
619 pshufw \r, \r, 0x4e
620 sub esp, 8
621 movq [esp], \r
622 _reg.3 "\msg: \r = %08x %08x"
623 .endm
624
625 .macro freg i, msg
626 _reg.0
627 _reg.1
628 _reg.2
629 finit
630 fldt [esp + 32 + 16*\i]
631 sub esp, 12
632 fstpt [esp]
633 _reg.3 "\msg: st(\i) = %.20Lg"
634 .endm
635
636 .macro fxreg i, msg
637 _reg.0
638 _reg.1
639 _reg.2
640 finit
641 fldt [esp + 32 + 16*\i]
642 sub esp, 12
643 fstpt [esp]
644 _reg.3 "\msg: st(\i) = %La"
645 .endm
646
647 #endif
648
649 ///--------------------------------------------------------------------------
650 /// ARM-specific hacking.
651
652 #if CPUFAM_ARMEL
653
654 // ARM/Thumb mode things. Use ARM by default.
655 #define ARM .arm; .L$_pcoff = 8
656 #define THUMB .thumb; .L$_pcoff = 4
657 ARM
658
659 // Set the function hooks.
660 #define FUNC_PREHOOK(_) .balign 4; .fnstart
661 #define ENDFUNC_HOOK(_) .fnend; .ltorg
662
663 // Call external subroutine at ADDR, possibly via PLT.
664 .macro callext addr, cond=
665 #if WANT_PIC
666 bl\cond \addr(PLT)
667 #else
668 bl\cond \addr
669 #endif
670 .endm
671
672 // Do I need to arrange a spare GOT register?
673 #if WANT_PIC
674 # define NEED_GOT 1
675 #endif
676 #define GOTREG r9
677
678 // Maybe load GOT address into GOT.
679 .macro ldgot cond=, got=GOTREG
680 #if WANT_PIC
681 ldr\cond \got, .L$_ldgot$\@
682 .L$_ldgot_pc$\@:
683 add\cond \got, pc, \got
684 _LIT
685 .balign 4
686 .L$_ldgot$\@:
687 .word _GLOBAL_OFFSET_TABLE_ - .L$_ldgot_pc$\@ - .L$_pcoff
688 _ENDLIT
689 #endif
690 .endm
691
692 // Load address of external symbol ADDR into REG, maybe using GOT.
693 .macro leaext reg, addr, cond=, got=GOTREG
694 #if WANT_PIC
695 ldr\cond \reg, .L$_leaext$\@
696 ldr\cond \reg, [\got, \reg]
697 _LIT
698 .balign 4
699 .L$_leaext$\@:
700 .word \addr(GOT)
701 _ENDLIT
702 #else
703 ldr\cond \reg, =\addr
704 #endif
705 .endm
706
707 // Load address of external symbol ADDR into REG directly.
708 .macro leaextq reg, addr, cond=
709 #if WANT_PIC
710 ldr\cond \reg, .L$_leaextq$\@
711 .L$_leaextq_pc$\@:
712 .if .L$_pcoff == 8
713 ldr\cond \reg, [pc, \reg]
714 .else
715 add\cond \reg, pc
716 ldr\cond \reg, [\reg]
717 .endif
718 _LIT
719 .balign 4
720 .L$_leaextq$\@:
721 .word \addr(GOT_PREL) + (. - .L$_leaextq_pc$\@ - .L$_pcoff)
722 _ENDLIT
723 #else
724 ldr\cond \reg, =\addr
725 #endif
726 .endm
727
728 // Apply decoration decor to register name reg.
729 #define _REGFORM(reg, decor) _GLUE(_REGFORM_, reg)(decor)
730
731 // Internal macros: `_REGFORM_r(decor)' applies decoration decor to register
732 // name r.
733
734 #define _REGFORM_s0(decor) _DECOR(s, decor, 0)
735 #define _REGFORM_s1(decor) _DECOR(s, decor, 1)
736 #define _REGFORM_s2(decor) _DECOR(s, decor, 2)
737 #define _REGFORM_s3(decor) _DECOR(s, decor, 3)
738 #define _REGFORM_s4(decor) _DECOR(s, decor, 4)
739 #define _REGFORM_s5(decor) _DECOR(s, decor, 5)
740 #define _REGFORM_s6(decor) _DECOR(s, decor, 6)
741 #define _REGFORM_s7(decor) _DECOR(s, decor, 7)
742 #define _REGFORM_s8(decor) _DECOR(s, decor, 8)
743 #define _REGFORM_s9(decor) _DECOR(s, decor, 9)
744 #define _REGFORM_s10(decor) _DECOR(s, decor, 10)
745 #define _REGFORM_s11(decor) _DECOR(s, decor, 11)
746 #define _REGFORM_s12(decor) _DECOR(s, decor, 12)
747 #define _REGFORM_s13(decor) _DECOR(s, decor, 13)
748 #define _REGFORM_s14(decor) _DECOR(s, decor, 14)
749 #define _REGFORM_s15(decor) _DECOR(s, decor, 15)
750 #define _REGFORM_s16(decor) _DECOR(s, decor, 16)
751 #define _REGFORM_s17(decor) _DECOR(s, decor, 17)
752 #define _REGFORM_s18(decor) _DECOR(s, decor, 18)
753 #define _REGFORM_s19(decor) _DECOR(s, decor, 19)
754 #define _REGFORM_s20(decor) _DECOR(s, decor, 20)
755 #define _REGFORM_s21(decor) _DECOR(s, decor, 21)
756 #define _REGFORM_s22(decor) _DECOR(s, decor, 22)
757 #define _REGFORM_s23(decor) _DECOR(s, decor, 23)
758 #define _REGFORM_s24(decor) _DECOR(s, decor, 24)
759 #define _REGFORM_s25(decor) _DECOR(s, decor, 25)
760 #define _REGFORM_s26(decor) _DECOR(s, decor, 26)
761 #define _REGFORM_s27(decor) _DECOR(s, decor, 27)
762 #define _REGFORM_s28(decor) _DECOR(s, decor, 28)
763 #define _REGFORM_s29(decor) _DECOR(s, decor, 29)
764 #define _REGFORM_s30(decor) _DECOR(s, decor, 30)
765 #define _REGFORM_s31(decor) _DECOR(s, decor, 31)
766
767 #define _REGFORM_d0(decor) _DECOR(d, decor, 0)
768 #define _REGFORM_d1(decor) _DECOR(d, decor, 1)
769 #define _REGFORM_d2(decor) _DECOR(d, decor, 2)
770 #define _REGFORM_d3(decor) _DECOR(d, decor, 3)
771 #define _REGFORM_d4(decor) _DECOR(d, decor, 4)
772 #define _REGFORM_d5(decor) _DECOR(d, decor, 5)
773 #define _REGFORM_d6(decor) _DECOR(d, decor, 6)
774 #define _REGFORM_d7(decor) _DECOR(d, decor, 7)
775 #define _REGFORM_d8(decor) _DECOR(d, decor, 8)
776 #define _REGFORM_d9(decor) _DECOR(d, decor, 9)
777 #define _REGFORM_d10(decor) _DECOR(d, decor, 10)
778 #define _REGFORM_d11(decor) _DECOR(d, decor, 11)
779 #define _REGFORM_d12(decor) _DECOR(d, decor, 12)
780 #define _REGFORM_d13(decor) _DECOR(d, decor, 13)
781 #define _REGFORM_d14(decor) _DECOR(d, decor, 14)
782 #define _REGFORM_d15(decor) _DECOR(d, decor, 15)
783 #define _REGFORM_d16(decor) _DECOR(d, decor, 16)
784 #define _REGFORM_d17(decor) _DECOR(d, decor, 17)
785 #define _REGFORM_d18(decor) _DECOR(d, decor, 18)
786 #define _REGFORM_d19(decor) _DECOR(d, decor, 19)
787 #define _REGFORM_d20(decor) _DECOR(d, decor, 20)
788 #define _REGFORM_d21(decor) _DECOR(d, decor, 21)
789 #define _REGFORM_d22(decor) _DECOR(d, decor, 22)
790 #define _REGFORM_d23(decor) _DECOR(d, decor, 23)
791 #define _REGFORM_d24(decor) _DECOR(d, decor, 24)
792 #define _REGFORM_d25(decor) _DECOR(d, decor, 25)
793 #define _REGFORM_d26(decor) _DECOR(d, decor, 26)
794 #define _REGFORM_d27(decor) _DECOR(d, decor, 27)
795 #define _REGFORM_d28(decor) _DECOR(d, decor, 28)
796 #define _REGFORM_d29(decor) _DECOR(d, decor, 29)
797 #define _REGFORM_d30(decor) _DECOR(d, decor, 30)
798 #define _REGFORM_d31(decor) _DECOR(d, decor, 31)
799
800 #define _REGFORM_q0(decor) _DECOR(q, decor, 0)
801 #define _REGFORM_q1(decor) _DECOR(q, decor, 1)
802 #define _REGFORM_q2(decor) _DECOR(q, decor, 2)
803 #define _REGFORM_q3(decor) _DECOR(q, decor, 3)
804 #define _REGFORM_q4(decor) _DECOR(q, decor, 4)
805 #define _REGFORM_q5(decor) _DECOR(q, decor, 5)
806 #define _REGFORM_q6(decor) _DECOR(q, decor, 6)
807 #define _REGFORM_q7(decor) _DECOR(q, decor, 7)
808 #define _REGFORM_q8(decor) _DECOR(q, decor, 8)
809 #define _REGFORM_q9(decor) _DECOR(q, decor, 9)
810 #define _REGFORM_q10(decor) _DECOR(q, decor, 10)
811 #define _REGFORM_q11(decor) _DECOR(q, decor, 11)
812 #define _REGFORM_q12(decor) _DECOR(q, decor, 12)
813 #define _REGFORM_q13(decor) _DECOR(q, decor, 13)
814 #define _REGFORM_q14(decor) _DECOR(q, decor, 14)
815 #define _REGFORM_q15(decor) _DECOR(q, decor, 15)
816
817 // `_LOPART(n)' and `_HIPART(n)' return the numbers of the register halves of
818 // register n, i.e., 2*n and 2*n + 1 respectively.
819 #define _LOPART(n) _GLUE(_LOPART_, n)
820 #define _HIPART(n) _GLUE(_HIPART_, n)
821
822 // Internal macros: `_LOPART_n' and `_HIPART_n' return the numbers of the
823 // register halves of register n, i.e., 2*n and 2*n + 1 respectively.
824
825 #define _LOPART_0 0
826 #define _HIPART_0 1
827 #define _LOPART_1 2
828 #define _HIPART_1 3
829 #define _LOPART_2 4
830 #define _HIPART_2 5
831 #define _LOPART_3 6
832 #define _HIPART_3 7
833 #define _LOPART_4 8
834 #define _HIPART_4 9
835 #define _LOPART_5 10
836 #define _HIPART_5 11
837 #define _LOPART_6 12
838 #define _HIPART_6 13
839 #define _LOPART_7 14
840 #define _HIPART_7 15
841 #define _LOPART_8 16
842 #define _HIPART_8 17
843 #define _LOPART_9 18
844 #define _HIPART_9 19
845 #define _LOPART_10 20
846 #define _HIPART_10 21
847 #define _LOPART_11 22
848 #define _HIPART_11 23
849 #define _LOPART_12 24
850 #define _HIPART_12 25
851 #define _LOPART_13 26
852 #define _HIPART_13 27
853 #define _LOPART_14 28
854 #define _HIPART_14 29
855 #define _LOPART_15 30
856 #define _HIPART_15 31
857
858 // Return the register number of the pair containing register n, i.e.,
859 // floor(n/2).
860 #define _PAIR(n) _GLUE(_PAIR_, n)
861
862 // Internal macros: `_PAIR_n' returns the register number of the pair
863 // containing register n, i.e., floor(n/2).
864 #define _PAIR_0 0
865 #define _PAIR_1 0
866 #define _PAIR_2 1
867 #define _PAIR_3 1
868 #define _PAIR_4 2
869 #define _PAIR_5 2
870 #define _PAIR_6 3
871 #define _PAIR_7 3
872 #define _PAIR_8 4
873 #define _PAIR_9 4
874 #define _PAIR_10 5
875 #define _PAIR_11 5
876 #define _PAIR_12 6
877 #define _PAIR_13 6
878 #define _PAIR_14 7
879 #define _PAIR_15 7
880 #define _PAIR_16 8
881 #define _PAIR_17 8
882 #define _PAIR_18 9
883 #define _PAIR_19 9
884 #define _PAIR_20 10
885 #define _PAIR_21 10
886 #define _PAIR_22 11
887 #define _PAIR_23 11
888 #define _PAIR_24 12
889 #define _PAIR_25 12
890 #define _PAIR_26 13
891 #define _PAIR_27 13
892 #define _PAIR_28 14
893 #define _PAIR_29 14
894 #define _PAIR_30 15
895 #define _PAIR_31 15
896
897 // Apply decoration decor to register number n of type ty. Decorations are
898 // as follows.
899 //
900 // decor types meaning
901 // Q s, d the NEON qN register containing this one
902 // D s the NEON dN register containing this one
903 // D0 q the low 64-bit half of this one
904 // D1 q the high 64-bit half of this one
905 // S0 d, q the first 32-bit piece of this one
906 // S1 d, q the second 32-bit piece of this one
907 // S2 q the third 32-bit piece of this one
908 // S3 q the fourth 32-bit piece of this one
909 // Bn q the nth byte of this register, as a scalar
910 // Hn q the nth halfword of this register, as a scalar
911 // Wn q the nth word of this register, as a scalar
912 #define _DECOR(ty, decor, n) _DECOR_##ty##_##decor(n)
913
914 // Internal macros: `_DECOR_ty_decor(n)' applies decoration decor to register
915 // number n of type ty.
916
917 #define _DECOR_s_Q(n) GLUE(q, _PAIR(_PAIR(n)))
918 #define _DECOR_s_D(n) GLUE(d, _PAIR(n))
919
920 #define _DECOR_d_Q(n) GLUE(q, _PAIR(n))
921 #define _DECOR_d_S0(n) GLUE(s, _LOPART(n))
922 #define _DECOR_d_S1(n) GLUE(s, _LOPART(n))
923
924 #define _DECOR_q_D0(n) GLUE(d, _LOPART(n))
925 #define _DECOR_q_D1(n) GLUE(d, _HIPART(n))
926 #define _DECOR_q_S0(n) GLUE(s, _LOPART(_LOPART(n)))
927 #define _DECOR_q_S1(n) GLUE(s, _HIPART(_LOPART(n)))
928 #define _DECOR_q_S2(n) GLUE(s, _LOPART(_HIPART(n)))
929 #define _DECOR_q_S3(n) GLUE(s, _HIPART(_HIPART(n)))
930 #define _DECOR_q_W0(n) GLUE(d, _LOPART(n))[0]
931 #define _DECOR_q_W1(n) GLUE(d, _LOPART(n))[1]
932 #define _DECOR_q_W2(n) GLUE(d, _HIPART(n))[0]
933 #define _DECOR_q_W3(n) GLUE(d, _HIPART(n))[1]
934 #define _DECOR_q_H0(n) GLUE(d, _LOPART(n))[0]
935 #define _DECOR_q_H1(n) GLUE(d, _LOPART(n))[1]
936 #define _DECOR_q_H2(n) GLUE(d, _LOPART(n))[2]
937 #define _DECOR_q_H3(n) GLUE(d, _LOPART(n))[3]
938 #define _DECOR_q_H4(n) GLUE(d, _HIPART(n))[0]
939 #define _DECOR_q_H5(n) GLUE(d, _HIPART(n))[1]
940 #define _DECOR_q_H6(n) GLUE(d, _HIPART(n))[2]
941 #define _DECOR_q_H7(n) GLUE(d, _HIPART(n))[3]
942 #define _DECOR_q_B0(n) GLUE(d, _LOPART(n))[0]
943 #define _DECOR_q_B1(n) GLUE(d, _LOPART(n))[1]
944 #define _DECOR_q_B2(n) GLUE(d, _LOPART(n))[2]
945 #define _DECOR_q_B3(n) GLUE(d, _LOPART(n))[3]
946 #define _DECOR_q_B4(n) GLUE(d, _LOPART(n))[4]
947 #define _DECOR_q_B5(n) GLUE(d, _LOPART(n))[5]
948 #define _DECOR_q_B6(n) GLUE(d, _LOPART(n))[6]
949 #define _DECOR_q_B7(n) GLUE(d, _LOPART(n))[7]
950 #define _DECOR_q_B8(n) GLUE(d, _HIPART(n))[0]
951 #define _DECOR_q_B9(n) GLUE(d, _HIPART(n))[1]
952 #define _DECOR_q_B10(n) GLUE(d, _HIPART(n))[2]
953 #define _DECOR_q_B11(n) GLUE(d, _HIPART(n))[3]
954 #define _DECOR_q_B12(n) GLUE(d, _HIPART(n))[4]
955 #define _DECOR_q_B13(n) GLUE(d, _HIPART(n))[5]
956 #define _DECOR_q_B14(n) GLUE(d, _HIPART(n))[6]
957 #define _DECOR_q_B15(n) GLUE(d, _HIPART(n))[7]
958
959 // Macros for navigating the NEON register hierarchy.
960 #define S0(reg) _REGFORM(reg, S0)
961 #define S1(reg) _REGFORM(reg, S1)
962 #define S2(reg) _REGFORM(reg, S2)
963 #define S3(reg) _REGFORM(reg, S3)
964 #define D(reg) _REGFORM(reg, D)
965 #define D0(reg) _REGFORM(reg, D0)
966 #define D1(reg) _REGFORM(reg, D1)
967 #define Q(reg) _REGFORM(reg, Q)
968
969 // Macros for indexing quadword registers.
970 #define QB(reg, i) _REGFORM(reg, B##i)
971 #define QH(reg, i) _REGFORM(reg, H##i)
972 #define QW(reg, i) _REGFORM(reg, W##i)
973
974 // Macros for converting vldm/vstm ranges.
975 #define QQ(qlo, qhi) D0(qlo)-D1(qhi)
976
977 // Stack management and unwinding.
978 .macro setfp fp, offset = 0
979 .if \offset == 0
980 mov \fp, sp
981 .setfp \fp, sp
982 .else
983 add \fp, sp, #\offset
984 .setfp \fp, sp, #\offset
985 .endif
986 .macro dropfp; _dropfp \fp, \offset; .endm
987 .L$_frameptr_p = -1
988 .endm
989
990 .macro _dropfp fp, offset = 0
991 .if \offset == 0
992 mov sp, \fp
993 .else
994 sub sp, \fp, #\offset
995 .endif
996 .purgem dropfp
997 .L$_frameptr_p = 0
998 .endm
999
1000 .macro stalloc n
1001 sub sp, sp, #\n
1002 .pad #\n
1003 .endm
1004
1005 .macro stfree n
1006 add sp, sp, #\n
1007 .pad #-\n
1008 .endm
1009
1010 .macro pushreg rr:vararg
1011 stmfd sp!, {\rr}
1012 .save {\rr}
1013 .endm
1014
1015 .macro popreg rr:vararg
1016 ldmfd sp!, {\rr}
1017 .endm
1018
1019 .macro pushvfp rr:vararg
1020 vstmdb sp!, {\rr}
1021 .vsave {\rr}
1022 .endm
1023
1024 .macro popvfp rr:vararg
1025 vldmia sp!, {\rr}
1026 .endm
1027
1028 .macro endprologue
1029 .endm
1030
1031 // No need for prologue markers on ARM.
1032 #define FUNC_POSTHOOK(_) .L$_prologue_p = -1
1033
1034 #endif
1035
1036 ///--------------------------------------------------------------------------
1037 /// AArch64-specific hacking.
1038
1039 #if CPUFAM_ARM64
1040
1041 // Set the function hooks.
1042 #define FUNC_PREHOOK(_) .balign 4
1043 #define FUNC_POSTHOOK(_) .cfi_startproc; .L$_prologue_p = -1
1044 #define ENDFUNC_HOOK(_) .cfi_endproc
1045
1046 // Call external subroutine at ADDR, possibly via PLT.
1047 .macro callext addr
1048 bl \addr
1049 .endm
1050
1051 // Load address of external symbol ADDR into REG.
1052 .macro leaext reg, addr
1053 #if WANT_PIC
1054 adrp \reg, :got:\addr
1055 ldr \reg, [\reg, #:got_lo12:\addr]
1056 #else
1057 adrp \reg, \addr
1058 add \reg, \reg, #:lo12:\addr
1059 #endif
1060 .endm
1061
1062 // Stack management and unwinding.
1063 .macro setfp fp, offset = 0
1064 // If you're just going through the motions with a fixed-size stack frame,
1065 // then you want to say `add x29, sp, #OFFSET' directly, which will avoid
1066 // pointlessly restoring sp later.
1067 .if \offset == 0
1068 mov \fp, sp
1069 .cfi_def_cfa_register \fp
1070 .else
1071 add \fp, sp, #\offset
1072 .cfi_def_cfa_register \fp
1073 .cfi_adjust_cfa_offset -\offset
1074 .endif
1075 .macro dropfp; _dropfp \fp, \offset; .endm
1076 .L$_frameptr_p = -1
1077 .endm
1078
1079 .macro _dropfp fp, offset = 0
1080 .if \offset == 0
1081 mov sp, \fp
1082 .cfi_def_cfa_register sp
1083 .else
1084 sub sp, \fp, #\offset
1085 .cfi_def_cfa_register sp
1086 .cfi_adjust_cfa_offset +\offset
1087 .endif
1088 .purgem dropfp
1089 .L$_frameptr_p = 0
1090 .endm
1091
1092 .macro stalloc n
1093 sub sp, sp, #\n
1094 .cfi_adjust_cfa_offset +\n
1095 .endm
1096
1097 .macro stfree n
1098 add sp, sp, #\n
1099 .cfi_adjust_cfa_offset -\n
1100 .endm
1101
1102 .macro pushreg x, y=
1103 .ifeqs "\y", ""
1104 str \x, [sp, #-16]!
1105 .cfi_adjust_cfa_offset +16
1106 .cfi_rel_offset \x, 0
1107 .else
1108 stp \x, \y, [sp, #-16]!
1109 .cfi_adjust_cfa_offset +16
1110 .cfi_rel_offset \x, 0
1111 .cfi_rel_offset \y, 8
1112 .endif
1113 .endm
1114
1115 .macro popreg x, y=
1116 .ifeqs "\y", ""
1117 ldr \x, [sp], #16
1118 .cfi_restore \x
1119 .cfi_adjust_cfa_offset -16
1120 .else
1121 ldp \x, \y, [sp], #16
1122 .cfi_restore \x
1123 .cfi_restore \y
1124 .cfi_adjust_cfa_offset -16
1125 .endif
1126 .endm
1127
1128 .macro savereg x, y, z=
1129 .ifeqs "\z", ""
1130 str \x, [sp, #\y]
1131 .cfi_rel_offset \x, \y
1132 .else
1133 stp \x, \y, [sp, #\z]
1134 .cfi_rel_offset \x, \z
1135 .cfi_rel_offset \y, \z + 8
1136 .endif
1137 .endm
1138
1139 .macro rstrreg x, y, z=
1140 .ifeqs "\z", ""
1141 ldr \x, [sp, #\y]
1142 .cfi_restore \x
1143 .else
1144 ldp \x, \y, [sp, #\z]
1145 .cfi_restore \x
1146 .cfi_restore \y
1147 .endif
1148 .endm
1149
1150 .macro endprologue
1151 .endm
1152
1153 #endif
1154
1155 ///--------------------------------------------------------------------------
1156 /// Final stuff.
1157
1158 // Default values for the various hooks.
1159 #ifndef FUNC_PREHOOK
1160 # define FUNC_PREHOOK(_)
1161 #endif
1162 #ifndef FUNC_POSTHOOK
1163 # define FUNC_POSTHOOK(_)
1164 #endif
1165 #ifndef ENDFUNC_HOOK
1166 # define ENDFUNC_HOOK(_)
1167 #endif
1168
1169 #ifndef F
1170 # define F(name) name
1171 #endif
1172
1173 #ifndef TYPE_FUNC
1174 # define TYPE_FUNC(name)
1175 #endif
1176
1177 #ifndef SIZE_OBJ
1178 # define SIZE_OBJ(name)
1179 #endif
1180
1181 #if __ELF__ && !defined(WANT_EXECUTABLE_STACK)
1182 .pushsection .note.GNU-stack, "", _SECTTY(progbits)
1183 .popsection
1184 #endif
1185
1186 ///----- That's all, folks --------------------------------------------------
1187
1188 #endif