tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] Problem with libtcc DLL + MEMORY output


From: grischka
Subject: Re: [Tinycc-devel] Problem with libtcc DLL + MEMORY output
Date: Mon, 28 Nov 2016 19:38:58 +0100
User-agent: Thunderbird 2.0.0.23 (Windows/20090812)

Amaury Bouchard wrote:
Hi all,

So, nobody here ever tried to use TinyCC library to compile some code, then
generate a .so/.dll file, then execute this code?

Why would anyone want to do that?

Just in case, there is a comment for tcc_set_output_type()" in both
libtcc.h and the libtcc_test example:

    /* set output type. MUST BE CALLED before any compilation */
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I suppose you have seen it but wanted to see what happens if you ignore
it, and the answer is the one that you already have:  does not work.

As with all software there are things within its scope and things beyond
its scope and decisions were made depending on usefulness and required
effort.

-- gr

Some help would be really appreciated.

Thank you in advance for your answer.

Amaury


2016-11-21 15:27 GMT+01:00 Amaury Bouchard <address@hidden>:

Hi all,

I have a problem using libtcc. It works perfectly when I want to compile
some code on the fly (setting the output type to MEMORY and then fetching a
pointer to the desired function); it seems to work fine too when I use it
to generate a shared object file (DLL output type).
But when I try to compile some code, then write the .so file, then execute
the code on the fly, I get errors at the first call to tcc_relocate().

Here is an example:
<<
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libtcc.h>

void main(int argc, char **argv)
{
   // the C code that will be compiled
   char *code = "int wrapper() { printf(\"OK\\n\"); }";
   TCCState *state;
   int (*wrapper)(void);
   int size;
   void *memory;

   printf("+ create TCC context\n");
   state = tcc_new();
   printf("+ code compilation\n");
   tcc_compile_string(state, code);
   if (argc == 2)
   {
      printf("+ set output type to DLL\n");
      tcc_set_output_type(state, TCC_OUTPUT_DLL);
      printf("+ write shared object file\n");
      tcc_output_file(state, "demo.so");
   }
   printf("+ set output type to MEMORY\n");
   tcc_set_output_type(state, TCC_OUTPUT_MEMORY);
   printf("+ phony memory relocation\n");
   size = tcc_relocate(state, NULL);
   if (size == -1)
   {
      printf("+ tcc_relocate() error\n");
      exit(2);
   }
   printf("+ destination memory allocation\n");
   memory = malloc(size);
   printf("+ memory relocation\n");
   tcc_relocate(state, memory);
   printf("+ get a pointer to the function\n");
   wrapper = tcc_get_symbol(state, "wrapper");
   printf("+ release the allocated memory\n");
   tcc_delete(state);
   printf("+ execution of the function\n");
   wrapper();
   printf("+ release the memory that contains the binary code\n");
   free(memory);
}
When I execute this program without any parameter (the .so file is not
generated), everything is fine. Here is the output:
<<
+ create TCC context
+ code compilation
+ set output type to MEMORY
+ phony memory relocation
+ destination memory allocation
+ memory relocation
+ get a pointer to the function
+ release the allocated memory
+ execution of the function
OK
+ release the memory that contains the binary code
But when I want to generate the .so file and execute the function, I get
some errors (and a core dump):
<<
+ create TCC context
+ code compilation
+ set output type to DLL
+ write shared object file
+ set output type to MEMORY
+ phony memory relocation
tcc: error: '_etext' defined twice
tcc: error: '_edata' defined twice
tcc: error: '_end' defined twice
tcc: error: '__preinit_array_start' defined twice
tcc: error: '__preinit_array_end' defined twice
tcc: error: '__init_array_start' defined twice
tcc: error: '__init_array_end' defined twice
tcc: error: '__fini_array_start' defined twice
tcc: error: '__fini_array_end' defined twice
*** Error in `./demo': realloc(): invalid old size: 0x0000000001055220 ***
Abandon (core dumped)
Any idea? What am I doing wrong ?

Best regards,

Amaury



------------------------------------------------------------------------

_______________________________________________
Tinycc-devel mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/tinycc-devel




reply via email to

[Prev in Thread] Current Thread [Next in Thread]