2
0

x86unix.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. #!/usr/local/bin/perl
  2. package x86unix; # GAS actually...
  3. $label="L000";
  4. $const="";
  5. $constl=0;
  6. $align=($main'aout)?"4":"16";
  7. $under=($main'aout or $main'coff)?"_":"";
  8. $dot=($main'aout)?"":".";
  9. $com_start="#" if ($main'aout or $main'coff);
  10. sub main'asm_init_output { @out=(); }
  11. sub main'asm_get_output { return(@out); }
  12. sub main'get_labels { return(@labels); }
  13. sub main'external_label { push(@labels,@_); }
  14. if ($main'cpp)
  15. {
  16. $align="ALIGN";
  17. $under="";
  18. $com_start='/*';
  19. $com_end='*/';
  20. }
  21. %lb=( 'eax', '%al',
  22. 'ebx', '%bl',
  23. 'ecx', '%cl',
  24. 'edx', '%dl',
  25. 'ax', '%al',
  26. 'bx', '%bl',
  27. 'cx', '%cl',
  28. 'dx', '%dl',
  29. );
  30. %hb=( 'eax', '%ah',
  31. 'ebx', '%bh',
  32. 'ecx', '%ch',
  33. 'edx', '%dh',
  34. 'ax', '%ah',
  35. 'bx', '%bh',
  36. 'cx', '%ch',
  37. 'dx', '%dh',
  38. );
  39. %regs=( 'eax', '%eax',
  40. 'ebx', '%ebx',
  41. 'ecx', '%ecx',
  42. 'edx', '%edx',
  43. 'esi', '%esi',
  44. 'edi', '%edi',
  45. 'ebp', '%ebp',
  46. 'esp', '%esp',
  47. 'mm0', '%mm0',
  48. 'mm1', '%mm1',
  49. 'mm2', '%mm2',
  50. 'mm3', '%mm3',
  51. 'mm4', '%mm4',
  52. 'mm5', '%mm5',
  53. 'mm6', '%mm6',
  54. 'mm7', '%mm7',
  55. 'xmm0', '%xmm0',
  56. 'xmm1', '%xmm1',
  57. 'xmm2', '%xmm2',
  58. 'xmm3', '%xmm3',
  59. 'xmm4', '%xmm4',
  60. 'xmm5', '%xmm5',
  61. 'xmm6', '%xmm6',
  62. 'xmm7', '%xmm7',
  63. );
  64. %reg_val=(
  65. 'eax', 0x00,
  66. 'ebx', 0x03,
  67. 'ecx', 0x01,
  68. 'edx', 0x02,
  69. 'esi', 0x06,
  70. 'edi', 0x07,
  71. 'ebp', 0x05,
  72. 'esp', 0x04,
  73. );
  74. sub main'LB
  75. {
  76. (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
  77. return($lb{$_[0]});
  78. }
  79. sub main'HB
  80. {
  81. (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
  82. return($hb{$_[0]});
  83. }
  84. sub main'DWP
  85. {
  86. local($addr,$reg1,$reg2,$idx)=@_;
  87. $ret="";
  88. $addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/;
  89. $reg1="$regs{$reg1}" if defined($regs{$reg1});
  90. $reg2="$regs{$reg2}" if defined($regs{$reg2});
  91. $ret.=$addr if ($addr ne "") && ($addr ne 0);
  92. if ($reg2 ne "")
  93. {
  94. if($idx ne "" && $idx != 0)
  95. { $ret.="($reg1,$reg2,$idx)"; }
  96. else
  97. { $ret.="($reg1,$reg2)"; }
  98. }
  99. elsif ($reg1 ne "")
  100. { $ret.="($reg1)" }
  101. return($ret);
  102. }
  103. sub main'QWP
  104. {
  105. return(&main'DWP(@_));
  106. }
  107. sub main'BP
  108. {
  109. return(&main'DWP(@_));
  110. }
  111. sub main'BC
  112. {
  113. return @_;
  114. }
  115. sub main'DWC
  116. {
  117. return @_;
  118. }
  119. #sub main'BP
  120. # {
  121. # local($addr,$reg1,$reg2,$idx)=@_;
  122. #
  123. # $ret="";
  124. #
  125. # $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
  126. # $reg1="$regs{$reg1}" if defined($regs{$reg1});
  127. # $reg2="$regs{$reg2}" if defined($regs{$reg2});
  128. # $ret.=$addr if ($addr ne "") && ($addr ne 0);
  129. # if ($reg2 ne "")
  130. # { $ret.="($reg1,$reg2,$idx)"; }
  131. # else
  132. # { $ret.="($reg1)" }
  133. # return($ret);
  134. # }
  135. sub main'mov { &out2("movl",@_); }
  136. sub main'movb { &out2("movb",@_); }
  137. sub main'and { &out2("andl",@_); }
  138. sub main'or { &out2("orl",@_); }
  139. sub main'shl { &out2("sall",@_); }
  140. sub main'shr { &out2("shrl",@_); }
  141. sub main'xor { &out2("xorl",@_); }
  142. sub main'xorb { &out2("xorb",@_); }
  143. sub main'add { &out2($_[0]=~/%[a-d][lh]/?"addb":"addl",@_); }
  144. sub main'adc { &out2("adcl",@_); }
  145. sub main'sub { &out2("subl",@_); }
  146. sub main'sbb { &out2("sbbl",@_); }
  147. sub main'rotl { &out2("roll",@_); }
  148. sub main'rotr { &out2("rorl",@_); }
  149. sub main'exch { &out2($_[0]=~/%[a-d][lh]/?"xchgb":"xchgl",@_); }
  150. sub main'cmp { &out2("cmpl",@_); }
  151. sub main'lea { &out2("leal",@_); }
  152. sub main'mul { &out1("mull",@_); }
  153. sub main'div { &out1("divl",@_); }
  154. sub main'jmp { &out1("jmp",@_); }
  155. sub main'jmp_ptr { &out1p("jmp",@_); }
  156. sub main'je { &out1("je",@_); }
  157. sub main'jle { &out1("jle",@_); }
  158. sub main'jne { &out1("jne",@_); }
  159. sub main'jnz { &out1("jnz",@_); }
  160. sub main'jz { &out1("jz",@_); }
  161. sub main'jge { &out1("jge",@_); }
  162. sub main'jl { &out1("jl",@_); }
  163. sub main'ja { &out1("ja",@_); }
  164. sub main'jae { &out1("jae",@_); }
  165. sub main'jb { &out1("jb",@_); }
  166. sub main'jbe { &out1("jbe",@_); }
  167. sub main'jc { &out1("jc",@_); }
  168. sub main'jnc { &out1("jnc",@_); }
  169. sub main'jno { &out1("jno",@_); }
  170. sub main'dec { &out1("decl",@_); }
  171. sub main'inc { &out1($_[0]=~/%[a-d][hl]/?"incb":"incl",@_); }
  172. sub main'push { &out1("pushl",@_); $stack+=4; }
  173. sub main'pop { &out1("popl",@_); $stack-=4; }
  174. sub main'pushf { &out0("pushfl"); $stack+=4; }
  175. sub main'popf { &out0("popfl"); $stack-=4; }
  176. sub main'not { &out1("notl",@_); }
  177. sub main'call { my $pre=$under;
  178. foreach $i (%label)
  179. { if ($label{$i} eq $_[0]) { $pre=''; last; } }
  180. &out1("call",$pre.$_[0]);
  181. }
  182. sub main'call_ptr { &out1p("call",@_); }
  183. sub main'ret { &out0("ret"); }
  184. sub main'nop { &out0("nop"); }
  185. sub main'test { &out2("testl",@_); }
  186. sub main'bt { &out2("btl",@_); }
  187. sub main'leave { &out0("leave"); }
  188. sub main'cpuid { &out0(".byte\t0x0f,0xa2"); }
  189. sub main'rdtsc { &out0(".byte\t0x0f,0x31"); }
  190. sub main'halt { &out0("hlt"); }
  191. sub main'movz { &out2("movzbl",@_); }
  192. sub main'neg { &out1("negl",@_); }
  193. sub main'cld { &out0("cld"); }
  194. # SSE2
  195. sub main'emms { &out0("emms"); }
  196. sub main'movd { &out2("movd",@_); }
  197. sub main'movdqu { &out2("movdqu",@_); }
  198. sub main'movdqa { &out2("movdqa",@_); }
  199. sub main'movdq2q{ &out2("movdq2q",@_); }
  200. sub main'movq2dq{ &out2("movq2dq",@_); }
  201. sub main'paddq { &out2("paddq",@_); }
  202. sub main'pmuludq{ &out2("pmuludq",@_); }
  203. sub main'psrlq { &out2("psrlq",@_); }
  204. sub main'psllq { &out2("psllq",@_); }
  205. sub main'pxor { &out2("pxor",@_); }
  206. sub main'por { &out2("por",@_); }
  207. sub main'pand { &out2("pand",@_); }
  208. sub main'movq {
  209. local($p1,$p2,$optimize)=@_;
  210. if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
  211. # movq between mmx registers can sink Intel CPUs
  212. { push(@out,"\tpshufw\t\$0xe4,%$p2,%$p1\n"); }
  213. else { &out2("movq",@_); }
  214. }
  215. # The bswapl instruction is new for the 486. Emulate if i386.
  216. sub main'bswap
  217. {
  218. if ($main'i386)
  219. {
  220. &main'comment("bswapl @_");
  221. &main'exch(main'HB(@_),main'LB(@_));
  222. &main'rotr(@_,16);
  223. &main'exch(main'HB(@_),main'LB(@_));
  224. }
  225. else
  226. {
  227. &out1("bswapl",@_);
  228. }
  229. }
  230. sub out2
  231. {
  232. local($name,$p1,$p2)=@_;
  233. local($l,$ll,$t);
  234. local(%special)=( "roll",0xD1C0,"rorl",0xD1C8,
  235. "rcll",0xD1D0,"rcrl",0xD1D8,
  236. "shll",0xD1E0,"shrl",0xD1E8,
  237. "sarl",0xD1F8);
  238. if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
  239. {
  240. $op=$special{$name}|$reg_val{$p1};
  241. $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
  242. $tmp2=sprintf(".byte %d\t",$op &0xff);
  243. push(@out,$tmp1);
  244. push(@out,$tmp2);
  245. $p2=&conv($p2);
  246. $p1=&conv($p1);
  247. &main'comment("$name $p2 $p1");
  248. return;
  249. }
  250. push(@out,"\t$name\t");
  251. $t=&conv($p2).",";
  252. $l=length($t);
  253. push(@out,$t);
  254. $ll=4-($l+9)/8;
  255. $tmp1=sprintf("\t" x $ll);
  256. push(@out,$tmp1);
  257. push(@out,&conv($p1)."\n");
  258. }
  259. sub out1
  260. {
  261. local($name,$p1)=@_;
  262. local($l,$t);
  263. local(%special)=("bswapl",0x0FC8);
  264. if ((defined($special{$name})) && defined($regs{$p1}))
  265. {
  266. $op=$special{$name}|$reg_val{$p1};
  267. $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
  268. $tmp2=sprintf(".byte %d\t",$op &0xff);
  269. push(@out,$tmp1);
  270. push(@out,$tmp2);
  271. $p2=&conv($p2);
  272. $p1=&conv($p1);
  273. &main'comment("$name $p2 $p1");
  274. return;
  275. }
  276. push(@out,"\t$name\t".&conv($p1)."\n");
  277. }
  278. sub out1p
  279. {
  280. local($name,$p1)=@_;
  281. local($l,$t);
  282. push(@out,"\t$name\t*".&conv($p1)."\n");
  283. }
  284. sub out0
  285. {
  286. push(@out,"\t$_[0]\n");
  287. }
  288. sub conv
  289. {
  290. local($p)=@_;
  291. # $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
  292. $p=$regs{$p} if (defined($regs{$p}));
  293. $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
  294. $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
  295. return $p;
  296. }
  297. sub main'file
  298. {
  299. local($file)=@_;
  300. local($tmp)=<<"EOF";
  301. .file "$file.s"
  302. EOF
  303. push(@out,$tmp);
  304. }
  305. sub main'function_begin
  306. {
  307. local($func)=@_;
  308. &main'external_label($func);
  309. $func=$under.$func;
  310. local($tmp)=<<"EOF";
  311. .text
  312. .globl $func
  313. EOF
  314. push(@out,$tmp);
  315. if ($main'cpp)
  316. { $tmp=push(@out,"TYPE($func,\@function)\n"); }
  317. elsif ($main'coff)
  318. { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
  319. elsif ($main'aout and !$main'pic)
  320. { }
  321. else { $tmp=push(@out,".type\t$func,\@function\n"); }
  322. push(@out,".align\t$align\n");
  323. push(@out,"$func:\n");
  324. $tmp=<<"EOF";
  325. pushl %ebp
  326. pushl %ebx
  327. pushl %esi
  328. pushl %edi
  329. EOF
  330. push(@out,$tmp);
  331. $stack=20;
  332. }
  333. sub main'function_begin_B
  334. {
  335. local($func,$extra)=@_;
  336. &main'external_label($func);
  337. $func=$under.$func;
  338. local($tmp)=<<"EOF";
  339. .text
  340. .globl $func
  341. EOF
  342. push(@out,$tmp);
  343. if ($main'cpp)
  344. { push(@out,"TYPE($func,\@function)\n"); }
  345. elsif ($main'coff)
  346. { $tmp=push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
  347. elsif ($main'aout and !$main'pic)
  348. { }
  349. else { push(@out,".type $func,\@function\n"); }
  350. push(@out,".align\t$align\n");
  351. push(@out,"$func:\n");
  352. $stack=4;
  353. }
  354. sub main'function_end
  355. {
  356. local($func)=@_;
  357. $func=$under.$func;
  358. local($tmp)=<<"EOF";
  359. popl %edi
  360. popl %esi
  361. popl %ebx
  362. popl %ebp
  363. ret
  364. ${dot}L_${func}_end:
  365. EOF
  366. push(@out,$tmp);
  367. if ($main'cpp)
  368. { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
  369. elsif ($main'coff or $main'aout)
  370. { }
  371. else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
  372. push(@out,".ident \"$func\"\n");
  373. $stack=0;
  374. %label=();
  375. }
  376. sub main'function_end_A
  377. {
  378. local($func)=@_;
  379. local($tmp)=<<"EOF";
  380. popl %edi
  381. popl %esi
  382. popl %ebx
  383. popl %ebp
  384. ret
  385. EOF
  386. push(@out,$tmp);
  387. }
  388. sub main'function_end_B
  389. {
  390. local($func)=@_;
  391. $func=$under.$func;
  392. push(@out,"${dot}L_${func}_end:\n");
  393. if ($main'cpp)
  394. { push(@out,"SIZE($func,${dot}L_${func}_end-$func)\n"); }
  395. elsif ($main'coff or $main'aout)
  396. { }
  397. else { push(@out,".size\t$func,${dot}L_${func}_end-$func\n"); }
  398. push(@out,".ident \"$func\"\n");
  399. $stack=0;
  400. %label=();
  401. }
  402. sub main'wparam
  403. {
  404. local($num)=@_;
  405. return(&main'DWP($stack+$num*4,"esp","",0));
  406. }
  407. sub main'stack_push
  408. {
  409. local($num)=@_;
  410. $stack+=$num*4;
  411. &main'sub("esp",$num*4);
  412. }
  413. sub main'stack_pop
  414. {
  415. local($num)=@_;
  416. $stack-=$num*4;
  417. &main'add("esp",$num*4);
  418. }
  419. sub main'swtmp
  420. {
  421. return(&main'DWP($_[0]*4,"esp","",0));
  422. }
  423. # Should use swtmp, which is above esp. Linix can trash the stack above esp
  424. #sub main'wtmp
  425. # {
  426. # local($num)=@_;
  427. #
  428. # return(&main'DWP(-($num+1)*4,"esp","",0));
  429. # }
  430. sub main'comment
  431. {
  432. if (!defined($com_start) or $main'elf)
  433. { # Regarding $main'elf above...
  434. # GNU and SVR4 as'es use different comment delimiters,
  435. push(@out,"\n"); # so we just skip ELF comments...
  436. return;
  437. }
  438. foreach (@_)
  439. {
  440. if (/^\s*$/)
  441. { push(@out,"\n"); }
  442. else
  443. { push(@out,"\t$com_start $_ $com_end\n"); }
  444. }
  445. }
  446. sub main'public_label
  447. {
  448. $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]}));
  449. push(@out,".globl\t$label{$_[0]}\n");
  450. }
  451. sub main'label
  452. {
  453. if (!defined($label{$_[0]}))
  454. {
  455. $label{$_[0]}="${dot}${label}${_[0]}";
  456. $label++;
  457. }
  458. return($label{$_[0]});
  459. }
  460. sub main'set_label
  461. {
  462. if (!defined($label{$_[0]}))
  463. {
  464. $label{$_[0]}="${dot}${label}${_[0]}";
  465. $label++;
  466. }
  467. if ($_[1]!=0)
  468. {
  469. if ($_[1]>1) { main'align($_[1]); }
  470. else { push(@out,".align $align\n"); }
  471. }
  472. push(@out,"$label{$_[0]}:\n");
  473. }
  474. sub main'file_end
  475. {
  476. # try to detect if SSE2 or MMX extensions were used on ELF platform...
  477. if ($main'elf && grep {/%[x]*mm[0-7]/i} @out) {
  478. local($tmp);
  479. push (@out,"\n.section\t.bss\n");
  480. push (@out,".comm\t${under}OPENSSL_ia32cap_P,4,4\n");
  481. push (@out,".section\t.init\n");
  482. # One can argue that it's wasteful to craft every
  483. # SSE/MMX module with this snippet... Well, it's 72
  484. # bytes long and for the moment we have two modules.
  485. # Let's argue when we have 7 modules or so...
  486. #
  487. # $1<<10 sets a reserved bit to signal that variable
  488. # was initialized already...
  489. &main'picmeup("edx","OPENSSL_ia32cap_P");
  490. $tmp=<<___;
  491. cmpl \$0,(%edx)
  492. jne 1f
  493. movl \$1<<10,(%edx)
  494. pushf
  495. popl %eax
  496. movl %eax,%ecx
  497. xorl \$1<<21,%eax
  498. pushl %eax
  499. popf
  500. pushf
  501. popl %eax
  502. xorl %ecx,%eax
  503. btl \$21,%eax
  504. jnc 1f
  505. pushl %edi
  506. pushl %ebx
  507. movl %edx,%edi
  508. movl \$1,%eax
  509. .byte 0x0f,0xa2
  510. orl \$1<<10,%edx
  511. movl %edx,0(%edi)
  512. popl %ebx
  513. popl %edi
  514. jmp 1f
  515. .align $align
  516. 1:
  517. ___
  518. push (@out,$tmp);
  519. }
  520. if ($const ne "")
  521. {
  522. push(@out,".section .rodata\n");
  523. push(@out,$const);
  524. $const="";
  525. }
  526. }
  527. sub main'data_byte
  528. {
  529. push(@out,"\t.byte\t".join(',',@_)."\n");
  530. }
  531. sub main'data_word
  532. {
  533. push(@out,"\t.long\t".join(',',@_)."\n");
  534. }
  535. sub main'align
  536. {
  537. my $val=$_[0],$p2,$i;
  538. if ($main'aout) {
  539. for ($p2=0;$val!=0;$val>>=1) { $p2++; }
  540. $val=$p2-1;
  541. $val.=",0x90";
  542. }
  543. push(@out,".align\t$val\n");
  544. }
  545. # debug output functions: puts, putx, printf
  546. sub main'puts
  547. {
  548. &pushvars();
  549. &main'push('$Lstring' . ++$constl);
  550. &main'call('puts');
  551. $stack-=4;
  552. &main'add("esp",4);
  553. &popvars();
  554. $const .= "Lstring$constl:\n\t.string \"@_[0]\"\n";
  555. }
  556. sub main'putx
  557. {
  558. &pushvars();
  559. &main'push($_[0]);
  560. &main'push('$Lstring' . ++$constl);
  561. &main'call('printf');
  562. &main'add("esp",8);
  563. $stack-=8;
  564. &popvars();
  565. $const .= "Lstring$constl:\n\t.string \"\%X\"\n";
  566. }
  567. sub main'printf
  568. {
  569. $ostack = $stack;
  570. &pushvars();
  571. for ($i = @_ - 1; $i >= 0; $i--)
  572. {
  573. if ($i == 0) # change this to support %s format strings
  574. {
  575. &main'push('$Lstring' . ++$constl);
  576. $const .= "Lstring$constl:\n\t.string \"@_[$i]\"\n";
  577. }
  578. else
  579. {
  580. if ($_[$i] =~ /([0-9]*)\(%esp\)/)
  581. {
  582. &main'push(($1 + $stack - $ostack) . '(%esp)');
  583. }
  584. else
  585. {
  586. &main'push($_[$i]);
  587. }
  588. }
  589. }
  590. &main'call('printf');
  591. $stack-=4*@_;
  592. &main'add("esp",4*@_);
  593. &popvars();
  594. }
  595. sub pushvars
  596. {
  597. &main'pushf();
  598. &main'push("edx");
  599. &main'push("ecx");
  600. &main'push("eax");
  601. }
  602. sub popvars
  603. {
  604. &main'pop("eax");
  605. &main'pop("ecx");
  606. &main'pop("edx");
  607. &main'popf();
  608. }
  609. sub main'picmeup
  610. {
  611. local($dst,$sym)=@_;
  612. if ($main'cpp)
  613. {
  614. local($tmp)=<<___;
  615. #if (defined(ELF) || defined(SOL)) && defined(PIC)
  616. call 1f
  617. 1: popl $regs{$dst}
  618. addl \$_GLOBAL_OFFSET_TABLE_+[.-1b],$regs{$dst}
  619. movl $sym\@GOT($regs{$dst}),$regs{$dst}
  620. #else
  621. leal $sym,$regs{$dst}
  622. #endif
  623. ___
  624. push(@out,$tmp);
  625. }
  626. elsif ($main'pic && ($main'elf || $main'aout))
  627. {
  628. &main'call(&main'label("PIC_me_up"));
  629. &main'set_label("PIC_me_up");
  630. &main'blindpop($dst);
  631. &main'add($dst,"\$${under}_GLOBAL_OFFSET_TABLE_+[.-".
  632. &main'label("PIC_me_up") . "]");
  633. &main'mov($dst,&main'DWP($under.$sym."\@GOT",$dst));
  634. }
  635. else
  636. {
  637. &main'lea($dst,&main'DWP($sym));
  638. }
  639. }
  640. sub main'blindpop { &out1("popl",@_); }
  641. sub main'initseg
  642. {
  643. local($f)=@_;
  644. local($tmp);
  645. if ($main'elf)
  646. {
  647. $tmp=<<___;
  648. .section .init
  649. call $under$f
  650. jmp .Linitalign
  651. .align $align
  652. .Linitalign:
  653. ___
  654. }
  655. elsif ($main'coff)
  656. {
  657. $tmp=<<___; # applies to both Cygwin and Mingw
  658. .section .ctors
  659. .long $under$f
  660. ___
  661. }
  662. elsif ($main'aout)
  663. {
  664. local($ctor)="${under}_GLOBAL_\$I\$$f";
  665. $tmp=".text\n";
  666. $tmp.=".type $ctor,\@function\n" if ($main'pic);
  667. $tmp.=<<___; # OpenBSD way...
  668. .globl $ctor
  669. .align 2
  670. $ctor:
  671. jmp $under$f
  672. ___
  673. }
  674. push(@out,$tmp) if ($tmp);
  675. }
  676. 1;