1

i'm using the tcc compiler to compile the following code:

#include <stdio.h>
#include <string.h>

void main()
{
    // intializing the secret code in the secret section.
    char* secret __attribute__ ((section(".secret")));
    secret = "myKey";

    // receive the user input.
    char* guess;    
    printf( "Enter your password\n");
    scanf("%s", guess);

    // detrmine success
    int result = strcmp(secret, guess);
    if(!result)
        printf( "Success\n" );
    else
        printf( "Wrong\n" );

}

according to the tcc reference: https://bellard.org/tcc/tcc-doc.html#linker section 3.3, tcc implement the "__ attribute __" GNU extension for C, so after compilation the 'secrect' string should be in a new assembly section. But, when i'm using dumpbin utility on the .exe file it tell me i have only two sections: .text and .data...

  Summary

    1000 .data
    1000 .text

i got this problem even when i'm using existing section like ".text", still the 'secret' stored in ".data"

even with unintialized integers i have no ".bss" section, just ".text" and ".data".

note: i got the last version of tcc.

please help!

Z E Nir
  • 332
  • 1
  • 2
  • 15
  • The way I see it, you're only putting `char* secret` in the secret section, but not the actual string literal. I might be wrong, though. – Blaze Oct 08 '18 at 09:21
  • thanks that might be true, but i should have a ".secret" section in the dumpbin summary anyway... i have the same problem with integers, the new section is not appearing, even for regular unintialized variabels - there is no '.bss' section - just '.data' and '.text' – Z E Nir Oct 08 '18 at 09:28
  • With gcc I have an error that local variable can't be placed at .secret section. Maybe there is some relation? Coud you try as global variable? – Nick S Oct 08 '18 at 09:35
  • I don't know about this compiler, but there is no reason for the compiler to allocate `secret` anywhere at all, since all it contains is an address to the `.rodata` string literal. Probably the code got optimized. But overall, the beginner mistakes in your question show that you don't grasp how strings work in C on a fundamental level. So start by studying the basics before moving on to advanced topics like custom memory locations. – Lundin Oct 08 '18 at 09:37
  • Study material: [Crash or “segmentation fault” when data is copied/scanned/read to an uninitialized pointer](https://stackoverflow.com/questions/37549594/crash-or-segmentation-fault-when-data-is-copied-scanned-read-to-an-uninitializ). – Lundin Oct 08 '18 at 09:41
  • @Lundin thanks. char* is new and challenging for me, since most of the time i code in other languages where i got "String" as an object not as an array... like .NET or Java. – Z E Nir Oct 09 '18 at 11:19

0 Answers0