Debian Codegraph @

arch/amd64/local.c

raw symbols SCIP
1 /* $Id: local.c,v 1.104 2021/08/29 09:21:56 gmcgarry Exp $ */
2 /*
3 * Copyright (c) 2008 Michael Shalayeff
4 * Copyright (c) 2003 Anders Magnusson ([email protected]).
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28
29 #include "pass1.h"
30
31 #ifndef LANG_CXX
32 #define NODE P1ND
33 #define ccopy p1tcopy
34 #define tfree p1tfree
35 #define nfree p1nfree
36 #define fwalk p1fwalk
37 #define talloc p1alloc
38 #endif
39
40 /* this file contains code which is dependent on the target machine */
41
42 /*
43 * Check if a constant is too large for a type.
44 */
45 #ifdef notyet
46 static int
47 toolarge(TWORD t, CONSZ con)
48 {
49 U_CONSZ ucon = con;
50
51 switch (t) {
52 case ULONG:
53 case LONG:
54 case ULONGLONG:
55 case LONGLONG:
56 break; /* cannot be too large */
57 #define SCHK(i) case i: if (con > MAX_##i || con < MIN_##i) return 1; break
58 #define UCHK(i) case i: if (ucon > MAX_##i) return 1; break
59 SCHK(INT);
60 SCHK(SHORT);
61 case BOOL:
62 SCHK(CHAR);
63 UCHK(UNSIGNED);
64 UCHK(USHORT);
65 UCHK(UCHAR);
66 default:
67 cerror("toolarge");
68 }
69 return 0;
70 }
71 #endif
72
73 static char *
74 getsoname(struct symtab *sp)
75 {
76 struct attr *ap;
77 return (ap = attr_find(sp->sap, ATTR_SONAME)) ?
78 ap->sarg(0) : sp->sname;
79 }
80
81
82 #define IALLOC(sz) (isinlining ? permalloc(sz) : tmpalloc(sz))
83
84 /*
85 * Make a symtab entry for PIC use.
86 */
87 static struct symtab *
88 picsymtab(char *p, char *s, char *s2)
89 {
90 struct symtab *sp = IALLOC(sizeof(struct symtab));
91 size_t len = strlen(p) + strlen(s) + strlen(s2) + 1;
92
93 sp->sname = IALLOC(len);
94 strlcpy(sp->sname, p, len);
95 strlcat(sp->sname, s, len);
96 strlcat(sp->sname, s2, len);
97 sp->sap = attr_new(ATTR_SONAME, 1);
98 sp->sap->sarg(0) = sp->sname;
99 sp->sclass = EXTERN;
100 sp->sflags = sp->slevel = 0;
101 return sp;
102 }
103
104 int gotnr; /* tempnum for GOT register */
105 int argstacksize;
106
107 /*
108 * Create a reference for an extern variable or function.
109 */
110 static NODE *
111 picext(NODE *p)
112 {
113 #if defined(ELFABI)
114
115 NODE *q;
116 struct symtab *sp;
117 char *c;
118
119 if (attr_find(p->n_sp->sap, ATTR_AMD64_BEENHERE))
120 return p;
121 #ifdef GCC_COMPAT
122 struct attr *ga;
123 if ((ga = attr_find(p->n_sp->sap, GCC_ATYP_VISIBILITY)) &&
124 strcmp(ga->sarg(0), "hidden") == 0)
125 return p; /* no GOT reference */
126 #endif
127
128 c = getexname(p->n_sp);
129 #ifndef PASS1
130 sp = picsymtab("", c, mcmodel & MCLARGE ? "@GOTOFF" : "@GOTPCREL");
131 #else
132 sp = picsymtab("", c, "@GOTOFF");
133 #endif
134 sp->sap = attr_add(sp->sap, attr_new(ATTR_AMD64_BEENHERE, 1));
135 q = block(NAME, NIL, NIL, INCREF(p->n_type), p->n_df, p->n_ap);
136 q->n_sp = sp;
137 q = block(UMUL, q, 0, p->n_type, p->n_df, p->n_ap);
138 q->n_sp = sp;
139 nfree(p);
140 return q;
141
142 #elif defined(MACHOABI)
143
144 return p;
145
146 #endif
147 }
148
149 static NODE *
150 cmop(NODE *l, NODE *r)
151 {
152 return block(CM, l, r, INT, 0, 0);
153 }
154
155 static NODE *
156 mkx(char *s, NODE *p)
157 {
158 p = block(XARG, p, NIL, INT, 0, 0);
159 p->n_name = s;
160 return p;
161 }
162
163 static char *
164 mk3str(char *s1, char *s2, char *s3)
165 {
166 size_t len = strlen(s1) + strlen(s2) + strlen(s3) + 1;
167 char *sd;
168
169 sd = tmpalloc(len);
170 strlcpy(sd, s1, len);
171 strlcat(sd, s2, len);
172 strlcat(sd, s3, len);
173 return sd;
174 }
175
176 /*
177 * Create a reference for a TLS variable.
178 * This is the "General dynamic" version.
179 */
180 static NODE *
181 tlspic(NODE *p)
182 {
183 NODE *q, *r, *s;
184 char *s1, *s2;
185
186 /*
187 * .byte 0x66
188 * leaq x@TLSGD(%rip),%rdi
189 * .word 0x6666
190 * rex64
191 * call __tls_get_addr@PLT
192 */
193
194 /* Need the .byte stuff around. Why? */
195 /* Use inline assembler */
196 q = mkx("%rdx", bcon(0));
197 q = cmop(q, mkx("%rcx", bcon(0)));
198 q = cmop(q, mkx("%rsi", bcon(0)));
199 q = cmop(q, mkx("%rdi", bcon(0)));
200 q = cmop(q, mkx("%r8", bcon(0)));
201 q = cmop(q, mkx("%r9", bcon(0)));
202 q = cmop(q, mkx("%r10", bcon(0)));
203 q = cmop(q, mkx("%r11", bcon(0)));
204
205 s = ccopy(r = tempnode(0, INCREF(p->n_type), p->n_df, p->n_ap));
206 r = mkx("=a", r);
207 r = block(XASM, r, q, INT, 0, 0);
208
209 /* Create the magic string */
210 s1 = ".byte 0x66\n\tleaq ";
211 s2 = "@TLSGD(%%rip),%%rdi\n"
212 "\t.word 0x6666\n\trex64\n\tcall __tls_get_addr@PLT";
213 if (attr_find(p->n_sp->sap, ATTR_SONAME) == NULL) {
214 p->n_sp->sap = attr_add(p->n_sp->sap, attr_new(ATTR_SONAME, 1));
215 p->n_sp->sap->sarg(0) = p->n_sp->sname;
216 }
217 r->n_name = addstring(mk3str(s1,
218 attr_find(p->n_sp->sap, ATTR_SONAME)->sarg(0), s2));
219
220 r = block(COMOP, r, s, INCREF(p->n_type), p->n_df, p->n_ap);
221 r = buildtree(UMUL, r, NIL);
222 tfree(p);
223 return r;
224 }
225
226 #ifdef GCC_COMPAT
227 /*
228 * The "initial exec" tls model.
229 */
230 static NODE *
231 tlsinitialexec(NODE *p)
232 {
233 NODE *q, *r, *s;
234 char *s1, *s2;
235
236 /*
237 * movq %fs:0,%rax
238 * addq x@GOTTPOFF(%rip),%rax
239 */
240
241 q = bcon(0);
242 q->n_type = STRTY;
243
244 s = ccopy(r = tempnode(0, INCREF(p->n_type), p->n_df, p->n_ap));
245 r = mkx("=r", r);
246 r = block(XASM, r, q, INT, 0, 0);
247
248 s1 = "movq %%fs:0,%0\n\taddq ";
249 s2 = "@GOTTPOFF(%%rip),%0";
250 if (attr_find(p->n_sp->sap, ATTR_SONAME) == NULL) {
251 p->n_sp->sap = attr_add(p->n_sp->sap, attr_new(ATTR_SONAME, 1));
252 p->n_sp->sap->sarg(0) = p->n_sp->sname;
253 }
254 r->n_name = mk3str(s1,
255 attr_find(p->n_sp->sap, ATTR_SONAME)->sarg(0), s2);
256
257 r = block(COMOP, r, s, INCREF(p->n_type), p->n_df, p->n_ap);
258 r = buildtree(UMUL, r, NIL);
259 tfree(p);
260 return r;
261 }
262 #endif
263
264 static NODE *
265 tlsref(NODE *p)
266 {
267 #ifdef GCC_COMPAT
268 struct symtab *sp = p->n_sp;
269 struct attr *ga;
270 char *c;
271
272 if ((ga = attr_find(sp->sap, GCC_ATYP_TLSMODEL)) != NULL) {
273 c = ga->sarg(0);
274 if (strcmp(c, "initial-exec") == 0)
275 return tlsinitialexec(p);
276 else if (strcmp(c, "global-dynamic") == 0)
277 ;
278 else
279 werror("unsupported tls model '%s'", c);
280 }
281 #endif
282 return tlspic(p);
283 }
284
285 /* clocal() is called to do local transformations on
286 * an expression tree preparitory to its being
287 * written out in intermediate code.
288 *
289 * the major essential job is rewriting the
290 * automatic variables and arguments in terms of
291 * REG and OREG nodes
292 * conversion ops which are not necessary are also clobbered here
293 * in addition, any special features (such as rewriting
294 * exclusive or) are easily handled here as well
295 */
296 NODE *
297 clocal(NODE *p)
298 {
299
300 register struct symtab *q;
301 register NODE *r, *l;
302 register int o;
303 register int m;
304 TWORD t;
305
306 #ifdef PCC_DEBUG
307 if (xdebug) {
308 printf("clocal: %p\n", p);
309 fwalk(p, eprint, 0);
310 }
311 #endif
312 switch( o = p->n_op ){
313
314 case NAME:
315 if ((q = p->n_sp) == NULL)
316 return p; /* Nothing to care about */
317
318 switch (q->sclass) {
319
320 case PARAM:
321 case AUTO:
322 /* fake up a structure reference */
323 r = block(REG, NIL, NIL, PTR+STRTY, 0, 0);
324 slval(r, 0);
325 r->n_rval = FPREG;
326 p = stref(block(STREF, r, p, 0, 0, 0));
327 break;
328
329 case USTATIC:
330 if (kflag == 0)
331 break;
332 /* FALLTHROUGH */
333 case STATIC:
334 #ifdef TLS
335 if (q->sflags & STLS) {
336 p = tlsref(p);
337 break;
338 }
339 #endif
340 break;
341
342 case REGISTER:
343 p->n_op = REG;
344 slval(p, 0);
345 p->n_rval = q->soffset;
346 break;
347
348 case EXTERN:
349 case EXTDEF:
350 #ifdef TLS
351 if (q->sflags & STLS) {
352 p = tlsref(p);
353 break;
354 }
355 #endif
356 if (kflag == 0 || statinit)
357 break;
358 if (blevel > 0)
359 p = picext(p);
360 break;
361 }
362 break;
363
364 case UCALL:
365 case USTCALL:
366 /* For now, always clear eax */
367 l = block(REG, NIL, NIL, INT, 0, 0);
368 regno(l) = RAX;
369 p->n_right = clocal(buildtree(ASSIGN, l, bcon(0)));
370 p->n_op -= (UCALL-CALL);
371 break;
372
373 case SCONV:
374 /* Special-case shifts */
375 if (p->n_type == LONG && (l = p->n_left)->n_op == LS &&
376 l->n_type == INT && l->n_right->n_op == ICON) {
377 p->n_left = l->n_left;
378 p = buildtree(LS, p, l->n_right);
379 nfree(l);
380 break;
381 }
382
383 l = p->n_left;
384
385 /* Float conversions may need extra casts */
386 if (p->n_type == FLOAT || p->n_type == DOUBLE ||
387 p->n_type == LDOUBLE) {
388 if (l->n_type < INT || l->n_type == BOOL) {
389 p->n_left = block(SCONV, l, NIL,
390 ISUNSIGNED(l->n_type) ? UNSIGNED : INT,
391 l->n_df, l->n_ap);
392 break;
393 }
394 }
395
396 if (p->n_type == l->n_type) {
397 nfree(p);
398 return l;
399 }
400
401 if ((p->n_type & TMASK) == 0 && (l->n_type & TMASK) == 0 &&
402 tsize(p->n_type, p->n_df, p->n_ap) ==
403 tsize(l->n_type, l->n_df, l->n_ap)) {
404 if (p->n_type != FLOAT && p->n_type != DOUBLE &&
405 l->n_type != FLOAT && l->n_type != DOUBLE &&
406 l->n_type != LDOUBLE && p->n_type != LDOUBLE) {
407 if (l->n_op == NAME || l->n_op == UMUL ||
408 l->n_op == TEMP) {
409 l->n_type = p->n_type;
410 nfree(p);
411 return l;
412 }
413 }
414 }
415
416 if (DEUNSIGN(p->n_type) == INT && DEUNSIGN(l->n_type) == INT &&
417 coptype(l->n_op) == BITYPE && l->n_op != COMOP &&
418 l->n_op != QUEST && l->n_op != ASSIGN) {
419 l->n_type = p->n_type;
420 nfree(p);
421 return l;
422 }
423
424 o = l->n_op;
425 m = p->n_type;
426
427 if (o == ICON) {
428 /* if named constant and pointer, allow cast
429 to long/ulong */
430 if (!nncon(l) && (l->n_type & TMASK) &&
431 (m == LONG || m == ULONG)) {
432 l->n_type = m;
433 l->n_ap = 0;
434 return nfree(p);
435 }
436
437 if (ISPTR(l->n_type) && !nncon(l))
438 break; /* cannot convert named pointers */
439
440 if (l->n_sp == 0) {
441 p->n_type = ULONG;
442 concast(l, m);
443 } else if (m != LONG && m != ULONG)
444 break;
445
446 l->n_type = m;
447 l->n_ap = 0;
448 nfree(p);
449 return l;
450 } else if (l->n_op == FCON)
451 cerror("SCONV FCON");
452 if ((p->n_type == CHAR || p->n_type == UCHAR ||
453 p->n_type == SHORT || p->n_type == USHORT) &&
454 (l->n_type == FLOAT || l->n_type == DOUBLE ||
455 l->n_type == LDOUBLE)) {
456 p = block(SCONV, p, NIL, p->n_type, p->n_df, p->n_ap);
457 p->n_left->n_type = INT;
458 return p;
459 }
460 break;
461
462 case MOD:
463 case DIV:
464 if (o == DIV && p->n_type != CHAR && p->n_type != SHORT)
465 break;
466 if (o == MOD && p->n_type != CHAR && p->n_type != SHORT)
467 break;
468 /* make it an int division by inserting conversions */
469 p->n_left = makety(p->n_left, INT, 0, 0, 0);
470 p->n_right = makety(p->n_right, INT, 0, 0, 0);
471 p = makety(p, p->n_type, 0, 0, 0);
472 p->n_left->n_type = INT;
473 break;
474
475 case FORCE:
476 /* put return value in return reg */
477 p->n_op = ASSIGN;
478 p->n_right = p->n_left;
479 p->n_left = block(REG, NIL, NIL, p->n_type, 0, 0);
480 t = p->n_type;
481 if (ISITY(t))
482 t = t - (FIMAG-FLOAT);
483 p->n_left->n_rval = p->n_left->n_type == BOOL ?
484 RETREG(CHAR) : RETREG(t);
485 break;
486
487 case LS:
488 case RS:
489 /* shift count must be in a char */
490 if (p->n_right->n_type == CHAR || p->n_right->n_type == UCHAR)
491 break;
492 p->n_right = makety(p->n_right, CHAR, 0, 0, 0);
493 break;
494 }
495 #ifdef PCC_DEBUG
496 if (xdebug) {
497 printf("clocal end: %p\n", p);
498 fwalk(p, eprint, 0);
499 }
500 #endif
501 return(p);
502 }
503
504 void
505 myp2tree(NODE *p)
506 {
507 struct attr *ap;
508 struct symtab *sp, sps;
509 static int dblxor, fltxor;
510 int codeatyp(NODE *);
511
512 if (p->n_op == STCALL || p->n_op == USTCALL) {
513 /* save struct encoding */
514 p->n_ap = attr_add(p->n_ap,
515 ap = attr_new(ATTR_AMD64_CMPLRET, 1));
516 ap->iarg(0) = codeatyp(p);
517 }
518
519 if (p->n_op == UMINUS && (p->n_type == FLOAT || p->n_type == DOUBLE)) {
520 /* Store xor code for sign change */
521 if (dblxor == 0) {
522 dblxor = getlab();
523 fltxor = getlab();
524 sps.stype = LDOUBLE;
525 sps.squal = CON >> TSHIFT;
526 sps.sflags = sps.sclass = 0;
527 sps.sname = "";
528 sps.slevel = 1;
529 sps.sap = NULL;
530 sps.soffset = dblxor;
531 locctr(DATA, &sps);
532 defloc(&sps);
533 printf("\t.long 0,0x80000000,0,0\n");
534 printf(LABFMT ":\n", fltxor);
535 printf("\t.long 0x80000000,0,0,0\n");
536 }
537 p->n_ap = attr_add(p->n_ap,
538 ap = attr_new(ATTR_AMD64_XORLBL, 1));
539 ap->iarg(0) = p->n_type == FLOAT ? fltxor : dblxor;
540 return;
541 }
542 if (kflag && (cdope(p->n_op) & CALLFLG) && p->n_left->n_op == NAME) {
543 /* Convert @GOTPCREL to @PLT */
544 char *s;
545
546 sp = p->n_left->n_sp;
547 if ((s = strstr(sp->sname, "@GOTPCREL")) != NULL) {
548 #ifndef PASS1
549 if (mcmodel & MCLARGE)
550 memcpy(s, "@PLTOFF", sizeof("@PLTOFF"));
551 else
552 memcpy(s, "@PLT", sizeof("@PLT"));
553 #else
554 memcpy(s, "@PLTOFF", sizeof("@PLTOFF"));
555 #endif
556 p->n_left->n_op = ICON;
557 }
558 return;
559 }
560 if (p->n_op != FCON)
561 return;
562
563 #ifdef mach_amd64
564 {
565 /* Do not lose negative zeros */
566 long long ll[2];
567 short ss;
568 #ifdef LANG_CXX
569 memcpy(ll, &p->n_dcon, sizeof(ll));
570 #else
571 memcpy(ll, p->n_scon, sizeof(ll));
572 #endif
573 memcpy(&ss, &ll[1], sizeof(ss));
574 if (ll[0] == 0 && ss == 0)
575 return;
576 }
577 #else
578 #error fixme
579 #endif
580
581 /* XXX should let float constants follow */
582 sp = IALLOC(sizeof(struct symtab));
583 sp->sclass = STATIC;
584 sp->sap = NULL;
585 sp->slevel = 1; /* fake numeric label */
586 sp->soffset = getlab();
587 sp->sflags = 0;
588 sp->stype = p->n_type;
589 sp->squal = (CON >> TSHIFT);
590 sp->sname = NULL;
591
592 locctr(DATA, sp);
593 defloc(sp);
594 inval(0, tsize(sp->stype, sp->sdf, sp->sap), p);
595
596 p->n_op = NAME;
597 slval(p, 0);
598 p->n_sp = sp;
599 }
600
601 /*
602 * Convert ADDROF NAME to ICON?
603 */
604 int
605 andable(NODE *p)
606 {
607 #ifdef notdef
608 /* shared libraries cannot have direct referenced static syms */
609 if (p->n_sp->sclass == STATIC || p->n_sp->sclass == USTATIC)
610 return 1;
611 #endif
612
613 #ifdef MACHOABI
614 return (!kflag || p->n_sp->sclass == EXTERN);
615 #else
616 return !kflag;
617 #endif
618 }
619
620 /*
621 * Return 1 if a variable of type type is OK to put in register.
622 */
623 int
624 cisreg(TWORD t)
625 {
626 if (t == LDOUBLE)
627 return 0;
628 return 1;
629 }
630
631 /*
632 * Allocate off bits on the stack. p is a tree that when evaluated
633 * is the multiply count for off, t is a storeable node where to write
634 * the allocated address.
635 */
636 void
637 spalloc(NODE *t, NODE *p, OFFSZ off)
638 {
639 NODE *sp;
640
641 p = buildtree(MUL, p, bcon(off/SZCHAR));
642 p = buildtree(PLUS, p, bcon(30));
643 p = buildtree(AND, p, xbcon(-16, NULL, LONG));
644
645 /* sub the size from sp */
646 sp = block(REG, NIL, NIL, p->n_type, 0, 0);
647 slval(sp, 0);
648 sp->n_rval = STKREG;
649 ecomp(buildtree(MINUSEQ, sp, p));
650
651 /* save the address of sp */
652 sp = block(REG, NIL, NIL, PTR+LONG, t->n_df, t->n_ap);
653 slval(sp, 0);
654 sp->n_rval = STKREG;
655 t->n_type = sp->n_type;
656 ecomp(buildtree(ASSIGN, t, sp)); /* Emit! */
657
658 }
659
660 /*
661 * print out a constant node, may be associated with a label.
662 * Do not free the node after use.
663 * off is bit offset from the beginning of the aggregate
664 * fsz is the number of bits this is referring to
665 */
666 int
667 ninval(CONSZ off, int fsz, NODE *p)
668 {
669 return 0;
670 }
671
672 /* make a name look like an external name in the local machine */
673 char *
674 exname(char *p)
675 {
676 #ifdef MACHOABI
677
678 #define NCHNAM 256
679 static char text[NCHNAM+1];
680 int i;
681
682 if (p == NULL)
683 return "";
684
685 text[0] = '_';
686 for (i=1; *p && i<NCHNAM; ++i)
687 text[i] = *p++;
688
689 text[i] = '\0';
690 text[NCHNAM] = '\0'; /* truncate */
691
692 return (text);
693 #else
694 return (p == NULL ? "" : p);
695 #endif
696 }
697
698 /*
699 * map types which are not defined on the local machine
700 */
701 TWORD
702 ctype(TWORD type)
703 {
704 switch (BTYPE(type)) {
705 case LONGLONG:
706 MODTYPE(type,LONG);
707 break;
708
709 case ULONGLONG:
710 MODTYPE(type,ULONG);
711
712 }
713 return (type);
714 }
715
716 void
717 calldec(NODE *p, NODE *q)
718 {
719 }
720
721 void
722 extdec(struct symtab *q)
723 {
724 }
725
726 /* make a common declaration for id, if reasonable */
727 void
728 defzero(struct symtab *sp)
729 {
730 int al;
731 OFFSZ off;
732 char *name;
733
734 name = getexname(sp);
735 off = tsize(sp->stype, sp->sdf, sp->sap);
736 SETOFF(off,SZCHAR);
737 off /= SZCHAR;
738 al = talign(sp->stype, sp->sap)/SZCHAR;
739
740 #ifdef MACHOABI
741 if (sp->sclass == STATIC) {
742 al = ispow2(al);
743 printf("\t.zerofill __DATA,__bss,");
744 if (sp->slevel == 0) {
745 printf("%s", name);
746 } else
747 printf(LABFMT, sp->soffset);
748 printf(",%lld,%d\n", off, al);
749 } else {
750 printf("\t.comm %s,0%llo,%d\n", name, off, al);
751 }
752 #else
753 if (sp->sclass == STATIC) {
754 if (sp->slevel == 0) {
755 printf("\t.local %s\n", name);
756 } else
757 printf("\t.local " LABFMT "\n", sp->soffset);
758 }
759 if (sp->slevel == 0) {
760 printf("\t.comm %s,0%llo,%d\n", name, off, al);
761 } else
762 printf("\t.comm " LABFMT ",0%llo,%d\n", sp->soffset, off, al);
763 #endif
764 }
765
766 static char *
767 section2string(char *name)
768 {
769 size_t len = strlen(name);
770
771 if (strncmp(name, "link_set", 8) == 0) {
772 const char postfix[] = ",\"aw\",@progbits";
773 char *s;
774
775 s = IALLOC(len + sizeof(postfix));
776 memcpy(s, name, len);
777 memcpy(s + len, postfix, sizeof(postfix));
778 return s;
779 }
780
781 return newstring(name, len);
782 }
783
784 char *nextsect;
785 static int gottls;
786 static char *alias;
787 static int constructor;
788 static int destructor;
789
790 /*
791 * Give target the opportunity of handling pragmas.
792 */
793 int
794 mypragma(char *str)
795 {
796 char *a2 = pragtok(NULL);
797
798 if (strcmp(str, "tls") == 0 && a2 == NULL) {
799 gottls = 1;
800 return 1;
801 }
802 if (strcmp(str, "constructor") == 0 || strcmp(str, "init") == 0) {
803 constructor = 1;
804 return 1;
805 }
806 if (strcmp(str, "destructor") == 0 || strcmp(str, "fini") == 0) {
807 destructor = 1;
808 return 1;
809 }
810 if (strcmp(str, "section") == 0 && a2 != NULL) {
811 nextsect = section2string(a2);
812 return 1;
813 }
814 if (strcmp(str, "alias") == 0 && a2 != NULL) {
815 alias = tmpstrdup(a2);
816 return 1;
817 }
818
819 return 0;
820 }
821
822 /*
823 * Called when a identifier has been declared.
824 */
825 void
826 fixdef(struct symtab *sp)
827 {
828 /* may have sanity checks here */
829 if (gottls)
830 sp->sflags |= STLS;
831 gottls = 0;
832
833 #ifdef GCC_COMPAT
834 struct attr *ga;
835
836 #ifdef HAVE_WEAKREF
837 /* not many as'es have this directive */
838 if ((ga = attr_find(sp->sap, GCC_ATYP_WEAKREF)) != NULL) {
839 char *wr = ga->sarg(0);
840 char *sn = getsoname(sp);
841 if (wr == NULL) {
842 if ((ga = attr_find(sp->sap, GCC_ATYP_ALIAS))) {
843 wr = ga->sarg(0);
844 }
845 }
846 if (wr == NULL)
847 printf("\t.weak %s\n", sn);
848 else
849 printf("\t.weakref %s,%s\n", sn, wr);
850 } else
851 #endif
852 if ((ga = attr_find(sp->sap, GCC_ATYP_ALIAS)) != NULL) {
853 char *an = ga->sarg(0);
854 char *sn = getsoname(sp);
855 char *v;
856
857 v = attr_find(sp->sap, GCC_ATYP_WEAK) ? "weak" : "globl";
858 printf("\t.%s %s\n", v, sn);
859 printf("\t.set %s,%s\n", sn, an);
860 }
861 #endif
862 if (alias != NULL && (sp->sclass != PARAM)) {
863 printf("\t.globl %s\n", getexname(sp));
864 printf("%s = ", getexname(sp));
865 printf("%s\n", exname(alias));
866 alias = NULL;
867 }
868 if ((constructor || destructor) && (sp->sclass != PARAM)) {
869 NODE *p = talloc();
870
871 p->n_op = NAME;
872 p->n_sp =
873 (struct symtab *)(constructor ? "constructor" : "destructor");
874 #ifdef GCC_COMPAT
875 sp->sap = attr_add(sp->sap, gcc_attr_parse(p));
876 #endif
877 constructor = destructor = 0;
878 }
879 }
880
881 void
882 pass1_lastchance(struct interpass *ip)
883 {
884 }
885