473,769 Members | 7,097 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to insert unique ID into binary file that created after compilation?

hello All,
how can i insert unique ID into binary file (that created by compiler)?
(so after compiling i can to identify the src that i use)

thx

Nov 15 '05 #1
24 3182
In article <11************ **********@g43g 2000cwa.googleg roups.com>,
pristo <Ma*******@gmai l.com> wrote:
hello All,
how can i insert unique ID into binary file (that created by compiler)?
(so after compiling i can to identify the src that i use)


Not portable. Can't discuss it here. Blah, blah, blah.

Nov 15 '05 #2


pristo wrote On 11/08/05 11:36,:
hello All,
how can i insert unique ID into binary file (that created by compiler)?
(so after compiling i can to identify the src that i use)


In portable C, the closest you can come is to create
a static variable initialized with the ID you desire. The
source file name, date, and time are often used for this:

static const char version_info[] =
"@@@ VERSION INFO @@@"
" Compiled from " __FILE__
" on " __DATE__
" at " __TIME__;

Many systems have utilities that can extract and display
the string constants from an executable or object file,
and ways to separate the "@@@ VERSION INFO @@@" strings
from the others (on Unix systems, you could use `strings'
and `grep'). Some source-management systems can be made
to provide part or all of the data, which could automate
the inclusion of a version identifier for each file. Of
course, all such utilities are not part of the C language,
and will vary from system to system.

A disadvantage of this technique is that some compilers
will issue diagnostic messages about variables that are
declared but not used; such compilers are likely to complain
about the `version_info' variable.

--
Er*********@sun .com

Nov 15 '05 #3
>how can i insert unique ID into binary file (that created by compiler)?
(so after compiling i can to identify the src that i use)


One common method of inserting an ID is:

static char rcsid[] = "$Id: foo.c,v 1.7 2005/11/08 12:01:32 root Exp $";

A compiler might give a warning about an unreferenced variable (and
might be smart enough to try to discard it): so reference it, for
example, make "foo -V" print out the version string.

The method of INSERTING the id is portable. The method of extracting
it isn't, but locating such a string is fairly easy and makes few
assumptions beyond the file being a raw binary file: scan until
you see a $, followed by a known keyword, followed by a colon, then
output the stuff from a $ to the next $. I think if non-text
characters (whatever that means) or newlines are encountered between
the two $, it's not output. I've never seen the program that
extracts the headers have a false match, although constructing a
failure case is easy.

RCS and CVS (source code control systems) will even maintain the
strings for you with a current version, time stamp, and user who
last checked it in. You actually put in:
static char rcsid[] = "$Id$";
and RCS or CVS will expand it for you.

Gordon L. Burditt
Nov 15 '05 #4
If you want to mark the binary file that is generated by more than one
source code: e.g (main.c, other.c, etc.) which will cause more than one
rcsid, and you want one unique mark for the whole binary, a good way to
do this is creating your own marker, something like the piece of code
below: (notice that I'm not testing for i/o, string or any other errors
in this example).

typedef struct tagFooter {
BYTE area1[10];
BYTE area2[10];
} _FOOTER;
#define size_footer sizeof(_FOOTER)

int main(int argc,char **argv)
{
FILE *f;
int size;
_FOOTER myInfo;

if(argc < 3) {
/* we expect to receive at least 2 parameters from command line */
return(1);
}
strcpy(myinfo.a rea1,argv[1]);
strcpy(myinfo.a rea2,argv[2]);

f=fopen("myfile .bin","wb");
fseek(f,0,SEEK_ END);
fwrite(&myinfo, size_footer ,1,f);
fclose(f);
return(0);
}

Instead of getting the values from command line, you could put a CRC
check or anything you desire to use. Also It's pretty easy to create a
program to show the footer information for a marked file.

Hope this can help you.

Nov 15 '05 #5
first thx to all for helping,
i try use your suggestion but its seem that the compiler is optmize the
variable.
i write the code for embdded system and i dont have interface to print
the version out
so how can i cause the compiler think that i use the variable and not
to optmize it?

Pristo

Nov 15 '05 #6
>If you want to mark the binary file that is generated by more than one
source code: e.g (main.c, other.c, etc.) which will cause more than one
rcsid,
Nothing wrong with more than one ID, if you make them static.
Often that's the whole point: you get the version number of
everything that was used to create the executable.
and you want one unique mark for the whole binary, a good way to
A common way to deal with this is to use the RCS ID in the version.c
file or the one in the main program as the primary ID, and that's
the one that the -V flag prints.
do this is creating your own marker, something like the piece of code
below: (notice that I'm not testing for i/o, string or any other errors
in this example).
Appending random crap to an executable may destroy it. Particularly
if the executable has a checksum, digital signature, or other
integrity check. Also, the program doing it *SHOULD* be flagged
as a virus.

There's no guarantee that SEEK_END on a binary file will work.
(Some OS such as CP/M only keep file sizes in sectors, so
there may be unwritten stuff at the end of the last sector in
a binary file, with no way to tell how much of it is unwritten.)

Gordon L. Burditt

typedef struct tagFooter {
BYTE area1[10];
BYTE area2[10];
} _FOOTER;
#define size_footer sizeof(_FOOTER)

int main(int argc,char **argv)
{
FILE *f;
int size;
_FOOTER myInfo;

if(argc < 3) {
/* we expect to receive at least 2 parameters from command line */
return(1);
}
strcpy(myinfo.a rea1,argv[1]);
strcpy(myinfo.a rea2,argv[2]);

f=fopen("myfile .bin","wb");
fseek(f,0,SEEK_ END);
fwrite(&myinfo, size_footer ,1,f);
fclose(f);
return(0);
}

Instead of getting the values from command line, you could put a CRC
check or anything you desire to use. Also It's pretty easy to create a
program to show the footer information for a marked file.

Hope this can help you.

Nov 15 '05 #7
thx for helping.
i try use your solution
and its seen that the copiler optimize the variable,
i write for embedded system and i dont have interface for print the
version out
how can i cause the compiler not to optmize this constant?

thx

Nov 15 '05 #8
In article <11************ *********@g49g2 000cwa.googlegr oups.com>,
pristo <Ma*******@gmai l.com> wrote:
i try use your suggestion but its seem that the compiler is optmize the
variable.
i write the code for embdded system and i dont have interface to print
the version out
so how can i cause the compiler think that i use the variable and not
to optmize it?


Sometimes declaring the variable as volatile is enough to get the
compiler to leave it alone even if it isn't used. Sometimes
simple tricks like taking its strlen() are enough. It depends on
how smart the compiler is about finding "dead code" and "dead variables".
--
If you lie to the compiler, it will get its revenge. -- Eric Sosman
Nov 15 '05 #9
pristo wrote:
thx for helping.
i try use your solution
and its seen that the copiler optimize the variable,
i write for embedded system and i dont have interface for print the
version out
how can i cause the compiler not to optmize this constant?

Please quote the relevant parts of posts your reply to.

Your compiler or linker may have an option that tells it to emit statics
even if they're not used. For gcc, it is notable that
-fkeep-static-consts does *not* do the trick in general; this only works
if you're not optimizing. Consult your documentation.

Otherwise, you'll have to try and outsmart your compiler (this is
generally a bad idea, but it may be the only option). That is, involve
the constant in expressions that are too complicated for the compiler to
detect as going unused or evaluating to a constant.

If you find such an expression, it may be a good idea to encapsulate it
in a function, otherwise it'll look very mysterious.

S.
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2062
by: Randell D. | last post by:
Folks, I use PHP to write my form data to MySQL. I have a database with about ten tables. I'm trying to fill one table with some dummy data (its a contact manager table holding names of people, addresses and whatever but I'm createing random values for the moment). The insert fails telling me that access is denied - however I use the exact same username/password/database name/host name on the other tables, and the insert and updates...
6
12358
by: pk | last post by:
Sorry for the piece-by-piece nature of this post, I moved it from a dormant group to this one and it was 3 separate posts in the other group. Anyway... I'm trying to bulk insert a text file of 10 columns into a table with 12. How can I specify which columns to insert to? I think format files are what I'm supposed to use, but I can't figure them out. I've also tried using a view, as was suggested on one of the many websites I've...
16
17018
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then IF FOUND UPDATE <TABLE> SET .... <Values entered here> ELSE INSERT INTO <TABLE> VALUES <Values...
2
6560
by: D. Dante Lorenso | last post by:
I'm trying to build a table that will store a history of records by enumerating the records. I want the newest record to always be number ZERO, so I created a trigger on my table to handle the assignment of version numbers: CREATE TRIGGER "trg_audio_file_insert" BEFORE INSERT ON "public"."audio_file" FOR EACH ROW EXECUTE PROCEDURE "public"."trg_audio_file_insert"(); My trigger function looks like this...
1
5375
by: cricrin | last post by:
Hello guys! This is Cristian From Argentina, and I wanted to ask you some help, I've looking on this and it makes me mad, I found that the error is in the $content , when y try to insert the record into the table via PHP code i receive an error message saying that the sin taxis its incorrect,however if i print the query echo ($query) and i copy and paste the query in the PHPmyAdmin the insert executes successfully. the $content variable...
16
2882
by: horacius.rex | last post by:
Hi, I don't know if it is better to post this question to a C or C++ group, so my apologies. My general question: I have a very big C++ program (Main.cpp) which compiles on machine X with compiler XA++.
0
10214
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10048
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3963
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.