Linking data into program

This is a discussion on Linking data into program within the C Programming forums, part of the General Programming Boards category; I have a binary data file (a boot sector) that I need to link into a program. The way I ...

  1. #1
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065

    Linking data into program

    I have a binary data file (a boot sector) that I need to link into a program. The way I normally do this is to create a small program that reads the entire contents of the file, puts it into a second file in the format "0xXX, ". What I'd rather be able to do is to somehow define a location for the file to be linked at compile time then access that location. . . but I don't even know what to search for on the net. . . tried "Linking data program" but I didn't come up with anything worthy.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I think you should stick to your ususal solution. Its shurely the simplest way.
    Another way I could imagine is to create a lot more complicated program that converts the data to an object-file that you could link to your program. But I'm shure to create a valid object-file that the linker recognizes is far less trivial then to create a simple c compilable source file.
    Kurt

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    I was afraid of that. It's just such a pain to have to remake that file any time I make a change. Also, it looks really crappy turning in a code file that has nothing but 0xXX, in an array. . .

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by Kennedy View Post
    I was afraid of that. It's just such a pain to have to remake that file any time I make a change. Also, it looks really crappy turning in a code file that has nothing but 0xXX, in an array. . .
    What I've done in the past is write a small utility program which reads a binary file and dumps it in C array format. You can work this into your makefile so that whenever you change the binary it automatically rebuilds the C module.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by brewbuck View Post
    What I've done in the past is write a small utility program which reads a binary file and dumps it in C array format. You can work this into your makefile so that whenever you change the binary it automatically rebuilds the C module.
    I'm lazy, so that was the first thing I did. Just don't like having the "data" file being C code.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by Kennedy View Post
    I'm lazy, so that was the first thing I did. Just don't like having the "data" file being C code.
    Why not? IMHO it's the more elegant way. Having it in an object makes it easy to link it whichever way you want.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > Also, it looks really crappy turning in a code file that has nothing but 0xXX, in an array
    True, but it is relatively portable.

    You can do it the other way, but it requires a lot more knowledge of the particular .obj file format (coff, elf, etc) of your tool chain in order to wrap a section, symbol, length around your binary data.

    If the length is constant say, then you could rip apart a previous output file to see where the binary data lives inside the .obj file, then substitute the new image.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    > I'm lazy

    SO. . .

    >If the length is constant say, then you could rip apart a previous output file to see where the
    >binary data lives inside the .obj file, then substitute the new image.

    Probably won't happen. I'll just stick with the old standby. . . I was really hoping that I was just being thick and didn't see the "obvious" way to do it. . . bummer.

    EDIT: And, I'm sick off looking at stuff through a hex editor (on this project).
    Last edited by Kennedy; 04-25-2007 at 12:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Replies: 2
    Last Post: 04-09-2008, 01:54 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  5. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21