Exetools

Exetools (https://forum.exetools.com/index.php)
-   General Discussion (https://forum.exetools.com/forumdisplay.php?f=2)
-   -   Omitting unused global variables (https://forum.exetools.com/showthread.php?t=12907)

ArC 07-27-2010 02:09

Omitting unused global variables
 
Recently I came across somewhat strange (?) behaviour of Microsoft's link.exe (10.00.30319.01).

Code:

#include <stdio.h>

#define SECTION_NAME                ".4711"

#pragma code_seg( push, r1, SECTION_NAME )
#pragma comment( linker, "/SECTION:"SECTION_NAME",RWE" )

__declspec(allocate(SECTION_NAME)) const char pString[] = "Blaaaaahhhhhhhh";

void TestFunc2()
{
        printf( __FUNCTION__"\n" );
}

void TestFunc()
{
#if 1
        printf( __FUNCTION__"\n" );
#endif

#if 1
        printf( __FUNCTION__": %s\n", pString );
#endif

        TestFunc2();
}

#pragma code_seg( pop, r1 )

void main()
{
#if 0
        TestFunc();
#endif
}

Based off my understanding of the linking process, the linker should not put TestFunc, TestFunc2 and pString into the executable image since there's no path from the entrypoint which leads to code that references them.

The linker does indeed omit TestFunc and TestFunc2, but it puts pString into the image unless I disable the reference in TestFunc.

So my question is if that's intended, correct behaviour or if this is some kind of bug in link.exe and if someone possibly knows a workaround.

Thanks in advance.

BTW The code was compiled/linked with /O2 /Ob0 /Oy and /OPT:REF.

metr0 07-27-2010 10:09

Although the MSDN doesn't directly say so, my guess would be that manually assigning a section the string shall be placed in (explicitly allocating the space) makes the linker think of it as something external - why would one want to explicitly allocate/assign it if not for specific reasons? You're taking the trouble, so the linker might assume you know what you're doing.

Or it's just due to some internal MS design issues no one will ever know more about.

ArC 07-27-2010 22:17

The bevaviour is the same for variables without manual section assignment.

It would be interesting to know whether other versions of link behave in the same way. Unfortunately I uninstalled my 2008 Express install in favour of 2010 so I can't check myself. :(

bilbo 07-28-2010 05:50

Well, tried on VC6 (link 6.00.8168): exactly the same behaviour,
even if I remove the three lines
Code:

#pragma code_seg(SECTION_NAME)  // push not understood
#pragma comment(linker, "/SECTION:"SECTION_NAME",RWE")
__declspec(allocate(SECTION_NAME))

and put everyting in ".text" section

Regards, bilbo

piccolo 07-30-2010 04:22

I think that this is based on the optimizer. If it isnt set properly then the code stayes in the obj file and thus it gets linked. This behaviour is at least not strange to me. I was more amazed that delphi does properly optimize more things out...
Anyway you should check the object file and you see it is one block of compiled code. Now if those two functions are in a second .c file and also compiler then they will not be linked indeed...


All times are GMT +8. The time now is 06:45.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX