bugTiny C Compiler - Bugs: bug #48900, Wrong code generation on Windows...

 
 

bug #48900: Wrong code generation on Windows for external variables

Submitter:  Shixin Zeng <shixinzeng>
Submitted:  Thu 25 Aug 2016 04:18:19 PM UTC
   
 
Category:  None Severity:  3 - Normal
Item Group:  None Status:  None
Privacy:  Public Assigned to:  None
Open/Closed:  Open
* Mandatory Fields

Add a New Comment Rich Markup
   

Thu 25 Aug 2016 06:29:53 PM UTC, comment #2: 

Somebody please close it, I can't find anywhere to change the status.

Shixin Zeng <shixinzeng>
Thu 25 Aug 2016 06:28:13 PM UTC, comment #1: 

My bad, the extern variable needs _attribute_((dllimport)) to make it work.

After change the code to:

extern _attribute_((dllimport)) long long global;

it works fine.


Shixin Zeng <shixinzeng>
Thu 25 Aug 2016 04:18:19 PM UTC, original submission:  

I found this while testing this program:
#include <stdlib.h>
#include "libtcc.h"

int global = 100;
int main()
{
int m_size;
void *m;
void (*p) (void);
TCCState *s = tcc_new();
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
    tcc_set_options(s, "-nostdinc -nostdlib");
    tcc_compile_string(s, "extern long long global;\nvoid printf(const char*fmt, ...);\nvoid p() {__asm__ volatile (\"int $0x03\");\nprintf(\"global: %d\n\", global);}\n");
#ifdef WIN32
    tcc_add_library_path(s, "c:\\Users\\user\\tcc");
    tcc_add_library(s, "tcc1");
    tcc_add_library(s, "ucrtbase");
    tcc_add_library(s, "msvcrt");
#else
    tcc_add_library_path(s, "/home/zsx/work/r3-build/tcc");
    tcc_add_library(s, "tcc1");
#endif
tcc_add_symbol(s, "global", &global);
m_size = tcc_relocate(s, NULL);
m = malloc(m_size);
tcc_relocate(s, m);
p = tcc_get_symbol(s, "p");
p ();
   
    free(m);
return 0;

}

What I tried to do was passing the global variable to the tcc compiled code, and print it out.
On Linux, it worked fine (outputs 100), on Windows it's not(outputs some random number, like -866870476).

I looked at the assembly code generated on both platforms, and I found this difference:

Linux
   0x0000000000668700: push   rbp
   0x0000000000668701: mov    rbp,rsp
   0x0000000000668704: sub    rsp,0x0
   0x000000000066870b: int3  
=> 0x000000000066870c: mov    rax,QWORD PTR [rip+0x45]        # 0x668758
   0x0000000000668713: mov    rax,QWORD PTR [rax]    #reading memory pointed by rax. The content of that memory location is address of "global" passed by tcc_add_symbol.
   0x0000000000668716: mov    rsi,rax
   0x0000000000668719: lea    rax,[rip+0x10]        # 0x668730
   0x0000000000668720: mov    rdi,rax
   0x0000000000668723: mov    eax,0x0
   0x0000000000668728: call   0x668780
   0x000000000066872d: leave 
   0x000000000066872e: ret   

Windows
000002697FA51A50  push        rbp 
000002697FA51A51  mov         rbp,rsp 
000002697FA51A54  sub         rsp,20h 
000002697FA51A5B  int         3 
000002697FA51A5C  mov         rax,qword ptr [2697FA51ADCh] 
000002697FA51A63  mov         r11,rax             #Copy rax to r11, should be "move r11, QWORD PTR [rax]", I think.
000002697FA51A66  lea         rax,[2697FA51A90h] 
000002697FA51A6D  mov         r10,rax 
000002697FA51A70  mov         rcx,r10 
000002697FA51A73  mov         rdx,r11 
000002697FA51A76  call        000002697FA51A88 
000002697FA51A7B  leave 
000002697FA51A7C  ret

Shixin Zeng <shixinzeng>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by shixinzeng (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    No changes have been made to this item

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code