MaX
MaX

Reputation: 1344

Is there a way to get string literals from GCC?

I have been looking around for a while on google but no fruitful results. I am actually looking for a way to export or some how get list of all string literals e.g.

int main(){
    const char *p = "Hello";
    const char x[] = "World";
}

For compiling this code is there a way to know "Hello" and "World"? I do know that they are available in disassemblers (used some in Windows). But is there some proper tool to actually dump these strings or somehow export them?

Upvotes: 0

Views: 1530

Answers (4)

sanjeev mk
sanjeev mk

Reputation: 4336

I don't get the question completely. You have a source file, and when you compile it, you need GCC to tell you how many string constants you've used. Am I correct at interpreting this?

If yes, you can tell gcc to compile with "gcc -S source_file" . This will give you the .s file, which contains the string constants you've defined, against ".string" . You can parse this file for ".string" occurances and get the count (either manually or with "grep" piped to "wc -l")

Upvotes: 3

Why do you need exactly all the string literal constants in a C code? (Using the strings utility on the binary might be enough).

You could customize the GCC compiler, e.g. with MELT, for that purpose, but that could take you days and I feel it is an overkill.

You might also emit assembly code from GCC and parse that assembly code for string constants.

And of course, you could tokenize and parse the preprocessed form.

Upvotes: 0

Jim Balter
Jim Balter

Reputation: 16406

Write a simple tokenizer that recognizes strings and comments and ignores everything else.

Upvotes: -1

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137398

Without debug information, you can only know that they will simply be plopped into a read-only data area (probably the .rdata section, IIRC).

The linux strings utility will look at a binary file and dump out any recognized strings within the given length and encoding constraints.

Upvotes: 1

Related Questions