views:

47

answers:

5

I'm hoping someone has run into this sort of problem before, and can give me a hint to solve it.

With Microsoft Visual C++ 2005, I have this code in a program:

DWORD locator[FOURXFLAGCOUNT+1]={
    0x58585858, 0x58585858, 0x58585858, 0x58585858, 0x58585858,
    0x58585858, 0x58585858, 0x58585858, 0x58585858, 0x58585858,
    0x58585858, 0x58585858, 0x58585858, 0x58585858, 0x58585858,
    0x58585858, 0x58585858, 0x58585858, 0x58585858, 0x58585858,
    0x58585858, 0x58585858, 0x58585858, 0x58585858, 0x58585858,
    0x58585858, 0x58585858, 0x58585858, 0x58585858, 0x58585858,
    0x58585858, 0x58585858, 0x58585858, 0x58585858, 0x58585858,
    0x58585858, 0x58585858, 0x58585858, 0x00000000
};

The idea is to make the locator discoverable (and fillable) from outside the program -- i.e. another program is filling it in, so that this program will have it built in when it starts up. This is for an anti-theft thing, so there's no conventional way to get the data, it has to be done something like this.

This worked just fine when I was compiling the program on its own, but when I added a static library to the program, the data vanished. The locator symbol is still there; the data it's supposed to be initialized with (and which is supposed to be visible outside it) isn't.

The linker switch /OPT:NOREF solves the problem, but at an unacceptable price: the program grows by several hundred K (doesn't seem like much, but it is in this case). Using the #pragma comment(linker, "/include:?locator@@BLAHBLAH") (don't remember what the "BLAHBLAH" part was) did nothing -- the locator symbol is already visible, it's just not initialized. Moving the locator definition into the library doesn't help either.

Ditching the static library is a last resort, I'd rather not do it if I can avoid it.

Any ideas?

A: 

You could try changing DWORD locator to static DWORD locator. Not sure if it would help but just a thought. Alternatively, include a reference to locator somewhere else in your code.

Shane MacLaughlin
Tried both already, to no avail. As I said, the symbol is there, it's just missing the data that it's supposed to be initialized with.
Head Geek
At a small function that iterates through the values, say to compute a checksum, to prove the data has been included.
Shane MacLaughlin
A: 

Add an function which uses locator, e.g. which builds the xor over it. You don't need to call this function, but you have to export it by adding it to the .DEF file or by using __declspec( DllExport ).

ur
Thanks, but as I said, that's not the problem -- `locator` is already there, it just isn't initialized like it's supposed to be.
Head Geek
+1  A: 

I haven't been able to find any acceptable solution to this problem. The linker is just too aggressive about what it trims... maybe a bug, maybe deliberate, though for the life of me, I can't imagine a case where you would want to eliminate the initialization of a variable while keeping the variable itself.

For now, I've turned on the /OPT:NOREF option. I'll just have to deal with the extra size, at least until I can find a way around it.

Head Geek
Check your assembly language manual. Some assembly languages can *include* another file. You could put the data into a file and have the assembly language *include* it in the data section. This allows you to change the data in the file with a text editor which *may* be easier than changing the assembly language.
Thomas Matthews
(For those who can't see how Thomas Matthews' comment relates, check the edits for the answer. It does, though only tangentially to the original problem.)
Head Geek
A: 

Is the problem that the locator is being optimised out because nothing references it? Would putting something like this in your main program help?

DWORD d=0;
int i=0;
for (i=0; i<FOURXFLAGCOUNT; i++) d += locator[i];

Or even just this, but maybe not on all compilers:

(void)locator[0];
crazyscot
Thanks, but I tried something similar to your first suggestion before I asked the question. No luck. The linker wasn't optimizing away the `locator` symbol, just its initialization. The more I think about it, the more I think it's a bug.
Head Geek
A: 

Does it make a difference if you declare the array extern?

Jan
I had it declared `extern` in a number of modules, but it still has to be declared normally somewhere, and it's the normal declaration that's having trouble.
Head Geek