(Note the following applies only to MSVC)
My first answer was misleading since I thought that the literal merging was magic done by the linker (and so that the /GF
flag would only be needed by the linker).
However, that was a mistake. It turns out the linker has little special involvement in merging string literals - what happens is that when the /GF
option is given to the compiler, it puts string literals in a "COMDAT" section of the object file with an object name that's based on the contents of the string literal. So the /GF
flag is needed for the compile step, not for the link step.
When you use the /GF
option, the compiler places each string literal in the object file in a separate section as a COMDAT object. The various COMDAT objects with the same name will be folded by the linker (I'm not exactly sure about the semantics of COMDAT, or what the linker might do if objects with the same name have different data). So a C file that contains
char* another_string = "this is a string";
Will have something like the following in the object file:
SECTION HEADER #3
.rdata name
0 physical address
0 virtual address
11 size of raw data
147 file pointer to raw data (00000147 to 00000157)
0 file pointer to relocation table
0 file pointer to line numbers
0 number of relocations
0 number of line numbers
40301040 flags
Initialized Data
COMDAT; sym= "`string'" (??_C@_0BB@LFDAHJNG@this?5is?5a?5string?$AA@)
4 byte align
Read Only
RAW DATA #3
00000000: 74 68 69 73 20 69 73 20 61 20 73 74 72 69 6E 67 this is a string
00000010: 00
with the relocation table wiring up the another_string1
variable name to the literal data.
Note that the name of the string literal object is clearly based on the contents of the literal string, but with some sort of mangling. The mangling scheme has been partially documented on Wikipedia (see "String constants").
Anyway, if you want literals in an assembly file to be treated in the same manner, you'd need to arrange for the literals to be placed in the object file in the same manner. I honestly don't know what (if any) mechanism the assembler might have for that. Placing an object in a "COMDAT" section is probably pretty easy - getting the name of the object to be based on the string contents (and mangled in the appropriate manner) is another story.
Unless there's some assembly directive/keyword that specifically supports this scenario, I think you might be out of luck. There certainly might be one, but I'm sufficiently rusty with ml.exe
to have no idea, and a quick look at the skimpy MSDN docs for ml.exe
didn't have anything jump out.
However, if you're willing to put the sting literals in a C file and refer to them in your assembly code via externs, it should work. However, that's essentially what Mark Ransom advocates in his comments to the question.