NO

Author Topic: #pragma data_seg  (Read 7304 times)

czerny

  • Guest
#pragma data_seg
« on: January 31, 2015, 05:36:18 PM »
I have here an C++ code which I want to translate to C.

I have the following peace of code:
Code: [Select]
#pragma data_seg(".drectve")
static char szShared[] = "-section:Shared,rws";
#pragma data_seg()

If I comment this out, the program compiles.
If I leave it in, I get the following errors:
Code: [Select]
warning #2135: Static 'szShared' is not referenced.
warning #3122: [asm] Redefinition of section '.drectve'; ignored.

POLINK: error: Unresolved external symbol '___chkstk'.
POLINK: error: Unresolved external symbol '_memset'.
POLINK: error: Unresolved external symbol '_qsort'.
POLINK: error: Unresolved external symbol '__WinMainCRTStartup'.

I would like to understand what exactly is happening here.

What is this (
Code: [Select]
"-section:Shared,rws") for?
I do not use memset() and __chkstk() in my code. They must be called during startup?

Any ideas?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: #pragma data_seg
« Reply #1 on: January 31, 2015, 06:45:28 PM »
This is a 'handcrafted' version for:
Code: [Select]
#pragma comment(linker, "-section:Shared, rws")You can replace the 3 llines with this one only.
The section '.drectve' is a dummy section used by the compiler to send options to the linker. This section is removed from the final PE.
In the code you got the line:
Code: [Select]
static char szShared[] = "-section:Shared,rws";This line is used by the compiler to encode the linker command to create a new section named 'Shared' having Read, write and Shared attributes.
This is typically used in DLL's to define a common memory area that is the same for all instances of the library.

I forgot, about ___chkstk it is part of the standard function prolog, it allocates space for local variables and checks that there is enaugh space on local stack. memset() should be used to init to zero local vars (are there initialized local variables?).
« Last Edit: January 31, 2015, 08:11:13 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

czerny

  • Guest
Re: #pragma data_seg
« Reply #2 on: January 31, 2015, 09:03:01 PM »
This is a 'handcrafted' version for:
Code: [Select]
#pragma comment(linker, "-section:Shared, rws")You can replace the 3 llines with this one only.
The section '.drectve' is a dummy section used by the compiler to send options to the linker. This section is removed from the final PE.
Pelles C seems to not recognize this 'handcrafted' version? Is that right?
The replacement #pragma gives the following error:
Code: [Select]
POLINK: fatal error: Invalid /SECTION arguments; use name and E,R,W,S,D,K and/or P.
In the code you got the line:
Code: [Select]
static char szShared[] = "-section:Shared,rws";This line is used by the compiler to encode the linker command to create a new section named 'Shared' having Read, write and Shared attributes.
This is typically used in DLL's to define a common memory area that is the same for all instances of the library.

I forgot, about ___chkstk it is part of the standard function prolog, it allocates space for local variables and checks that there is enaugh space on local stack. memset() should be used to init to zero local vars (are there initialized local variables?).
Why is the linker searching for the startup functions?

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: #pragma data_seg
« Reply #3 on: January 31, 2015, 09:43:31 PM »
PellesC don't like spaces in the pragma linker comment  >:(
Remove the space between the section name and flags:
Code: [Select]
#pragma comment(linker, "-section:Shared,rws")It should recognize also handcrafted version because every text in .drectve section should be parsed by linker for commands.
Why is the linker searching for the startup functions?
I don'know, I should llook at the project. I can suppose that other similiar linker comments prevent the linker from searching libraries...
« Last Edit: January 31, 2015, 09:45:35 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

czerny

  • Guest
Re: #pragma data_seg
« Reply #4 on: January 31, 2015, 10:35:53 PM »
PellesC don't like spaces in the pragma linker comment  >:(
Remove the space between the section name and flags:
Code: [Select]
#pragma comment(linker, "-section:Shared,rws")It should recognize also handcrafted version because every text in .drectve section should be parsed by linker for commands.
Why is the linker searching for the startup functions?
I don'know, I should llook at the project. I can suppose that other similiar linker comments prevent the linker from searching libraries...
Hmm, now the code compiles ok and the application doesn't start a second time (that's why this shared section was needed).
But the screen resolution list is not seen (just a little bit).

I have attached the project.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: #pragma data_seg
« Reply #5 on: January 31, 2015, 11:49:29 PM »
On Win7 it compiles ok and shows all available resolutions for the screen.
I changed the resolution of my screen to 640x480 8bits  :P Really ugly!

P.s. the original setting for shared memory works, but overwrites the contents of .drective section removing the default libraries.
Adding pocrt.lib in linker libraries list remove the missing symbols error and the executable works as expected.  ;)
« Last Edit: February 01, 2015, 12:06:20 AM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

czerny

  • Guest
Re: #pragma data_seg
« Reply #6 on: February 01, 2015, 01:14:07 AM »
I too get a working exe this way (win2k).

But the following warnings:
Code: [Select]
warning #2135: Static 'szShared' is not referenced.
warning #3122: [asm] Redefinition of section '.drectve'; ignored.
I don't understand that.

If the redefinition of .drectve would really be ignored the 'run once' feature would not work.

And why is the
Code: [Select]
#pragma comment(linker, "-section:Shared,rws") variant not working?

Edit: No, after selecting a new resolution I wanted to change it back and the menu comes not up. And one times the
Code: [Select]
#pragma comment(linker, "-section:Shared,rws") variant was working.

If I use
Code: [Select]
#pragma comment(lib, "pocrt.lib")at the beginning of the file I get
Code: [Select]
POLINK: fatal error: File not found: 'pocrt.lib-section:Shared,rws'.looks like a concatenation of the library and the linker pragmas.

In the moment no variant is working (no list displayed). I have tried several times (with pocrt.lib in the linker tab).
« Last Edit: February 01, 2015, 01:44:17 AM by czerny »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2113
Re: #pragma data_seg
« Reply #7 on: February 01, 2015, 11:10:51 AM »
I too get a working exe this way (win2k).

But the following warnings:
Code: [Select]
warning #2135: Static 'szShared' is not referenced.
warning #3122: [asm] Redefinition of section '.drectve'; ignored.
I don't understand that.

If the redefinition of .drectve would really be ignored the 'run once' feature would not work.
The warning #2135 tells you that you declared a static variable that is not used in current file. But this declaration was intended to 'write' in .drectve section the linker command.
For error #3122 I'm not sure about what the message would be. The linker command changes the attributes of 'Shared' section, not that of the '.drectve' section. No change must be done to this section that, as I already told is only  for compiler->linker communication and will never be inserted in final PE.
Using the correct form you have to remove all three lines:
Code: [Select]
#pragma data_seg(".drectve")
static char szShared[] = "-section:Shared,rws";
#pragma data_seg()

And why is the
Code: [Select]
#pragma comment(linker, "-section:Shared,rws") variant not working?

Edit: No, after selecting a new resolution I wanted to change it back and the menu comes not up. And one times the
Code: [Select]
#pragma comment(linker, "-section:Shared,rws") variant was working.

??? You are messing something there... The one above is the correct form and works!

If I use
Code: [Select]
#pragma comment(lib, "pocrt.lib")at the beginning of the file I get
Code: [Select]
POLINK: fatal error: File not found: 'pocrt.lib-section:Shared,rws'.looks like a concatenation of the library and the linker pragmas.

In the moment no variant is working (no list displayed). I have tried several times (with pocrt.lib in the linker tab).
There should be different problems here...
The uncorrect layout of linker commands in the .drectve section is due to the 'handcrafted' form, and as I told you in my previous post it overwrites section data.
Now the OS, and related to it the compiler version, I haven't checked if the API's used in your sample are supported on W2K, have you?
What is the PellesC version you are using? Maybe is an older buggy one?
Compiling the sample with PellesC V8RC6 everything works ok on XP and 7.
On PellesC V8RC6 these errors are not show.
« Last Edit: February 01, 2015, 12:08:04 PM by frankie »
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

czerny

  • Guest
Re: #pragma data_seg
« Reply #8 on: February 04, 2015, 10:52:09 AM »
But the screen resolution list is not seen (just a little bit).
The y-position of the menu is taken from current mouse position, which is rather big.
I have set pt.y = 0. Now the menu is seen.
Don't know why it sometimes worked.