IN LINUX: Not sure if it is possible. I have 100 source file, and 100 respective executable files. Now, given the executable file, is it possible to determine, respective source file.
-
Is it a given fact that every single one of these 100 executables comes from a single source code file?Mike Nakis– Mike Nakis2012-01-04 09:18:44 +00:00Commented Jan 4, 2012 at 9:18
-
No, all are from 100 source files each.Whoami– Whoami2012-01-04 09:21:13 +00:00Commented Jan 4, 2012 at 9:21
-
2Well then you need to reformulate your question big time.Mike Nakis– Mike Nakis2012-01-04 09:24:01 +00:00Commented Jan 4, 2012 at 9:24
-
@MikeNakis, Edited Question, is it ok ?Whoami– Whoami2012-01-04 09:28:32 +00:00Commented Jan 4, 2012 at 9:28
5 Answers
I guess you can give this a try.
readelf -s a.out | grep FILE
I think you can add some grep and sed magic to the above command and get the source file name.
1 Comment
strings as by @mouviciel is the next-best thing after readelf); one can easily strip or modify the executable..No, since your assumption, that a single binary comes from exactly one source file, is very false.
Most real applications consist of hundreds, if not thousands, of individual source files that are all compiled separately, with the results liked together to form the binary.
If you have non-stripped binaries, or (even better) binaries compiled with debugging information present, then there might (or will, for the case of debugging info) be information left in the file to allow you to figure out the names of the source files, but in general you won't have such binaries unless you build them yourself.
Comments
If source filenames are present in an executable, you can find them with:
strings executable | grep '\.c'
But filenames may or may not be present in the executable and they may or may not represent the source filenames.
Change .c to whatever extension you assume the program has been written in.
Comments
Your question only makes sense if we presume that it is a given fact that every single one of these 100 executables comes from a single source file, and that you have all those source files and are capable of compiling them all.
What you can do is to declare within each source file a string that looks like "HERE!HERE!>>>" + __FILE__ and then write a utility which searches for "HERE!HERE!>>>" inside the executable and parses the string which follows it. __FILE__ is a preprocessor directive which expands to the full pathname of the source file being compiled.
1 Comment
This kind of help falls in the 'close the barn door after the horse has run away' kind of thing, but it might help future posters.
This is an old problem. UNIX and Linux support the what command which was invented by Mark Rochkind (if I remember correctly), for his version of SCCS. Handles exactly this type of problem. It is only 100% reliable for one source file -> one exectuable (or object file ) kind of thing. There are other more important uses.
char unique_id[] = "@(#)identification information";
The @(#) is called a "what string" and does not occur as a by-product of compiling source into an executable image. Use what from the command line. Inside code use maybe something like this (assumes you get only one file name as an answer, therefore choose your what strings carefully):
char *foo(char *whoami, size_t len_whoami)
{
char tmp[80]={0x0};
FILE *cmd;
sprintf(tmp, "/usr/bin/grep -F -l '%s' /path/to/*.c", unique_id);
cmd=popen(tmp, "r");
fgets(whoami, len_whoami, cmd);
pclose(cmd);
return whoami;
}
will return the source code file name with the same what string from which your executable was built. In other words, exactly what you asked, except I'm sure you never heard of what strings, so they do not exist in your current code base.