|
From: | Amaury Bouchard |
Subject: | Re: [Tinycc-devel] Problem with libtcc DLL + MEMORY output |
Date: | Mon, 28 Nov 2016 18:10:05 +0100 |
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 compiledchar *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 functionOK+ 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 relocationtcc: error: '_etext' defined twicetcc: error: '_edata' defined twicetcc: error: '_end' defined twicetcc: error: '__preinit_array_start' defined twicetcc: error: '__preinit_array_end' defined twicetcc: error: '__init_array_start' defined twicetcc: error: '__init_array_end' defined twicetcc: error: '__fini_array_start' defined twicetcc: 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
[Prev in Thread] | Current Thread | [Next in Thread] |