Live data from Hacker News

Embedding Binary Objects in C

flak.tedunangst.com

61–70 of 141 posts

Re: Embedding Binary Objects in C

#61
post #15

Is it possible to automate this with cmake?

You may want to checkout the CMRC library (CMake Resource Compiler) [0]. [0]: https://github.com/vector-of-bool/cmrc

Neat. Looks a little more complex than it needs to be though, and I'm surprised it targets C++ (and even relies on exceptions) with no support for C.

Re: Embedding Binary Objects in C

#63
post #55

Unfortunately, this technique is a bit problematic with modern C compilers. Because the `start` and `end` symbols are unrelated objects, as far as the compiler is concerned, the subtraction `&end - &start` to get the length of the data invokes undefined behavior. Just for that reason, I feel the include file with a hex dump is the better method.

To get the size you would use the `_binary_FILENAME_size` symbol that the linker puts into the object file.

Re: Embedding Binary Objects in C

#64
post #55

Unfortunately, this technique is a bit problematic with modern C compilers. Because the `start` and `end` symbols are unrelated objects, as far as the compiler is concerned, the subtraction `&end - &start` to get the length of the data invokes undefined behavior. Just for that reason, I feel the include file with a hex dump is the better method.

Last time I checked in the Green Hills compiler I'm using you also have the option of using a symbol that contains the size.

Re: Embedding Binary Objects in C

#65
post #54

My favorite is simply using a tool to create a C file with your binary data: static uint8_t mydata[] = {0xDE, 0xAD, 0xBE, 0xEF, ... }; The advantage is that it works anywhere, with any compiler. The disadvantage is that it can increase compile time. I would limit each .c file to 10MB; it seems there's a quadratic increase in build time with file size, at least with gcc. Also, instead of "0x%02x" I use decimal notatio…

I've never noticed the "quadratic" build time increase you mention, so I did a test[0]. Files in size from 1mb to 50mb, 3 trials each. These are the results, and they look absolutely linear[1]. [0]: https://pastebin.com/Z9329xkc [1]: https://i.imgur.com/I3XSBDg.png

Nice, I love it when people actually try it out. In the end, I much prefer the method outlined in the article, less processing overall, no need to transform it into an array nor compile the array into an object afterwards. You just skip 2 steps and bring it to an object directly!

Re: Embedding Binary Objects in C

#66

Earlier quoted context omitted.

How exactly is the ordering of items across the section decided? Is it relatively random or is it based on the order that the object files are passed to the linker?

It's unspecified, and I've seen it change over time as the linker changes it's data structures.

This is true, to my knowledge, I would NOT relie on the order to be always consistent. But I think you can enforce the order in the linker file, i.e. load the arrays one after the other, and then the order should keep constant. But you still have several places that you need to keep synchronized and, really, most people don't look in the linker file at all.

Re: Embedding Binary Objects in C

#67
post #49
post #5

There's also gnu's objcopy. This post covers a lot of different approaches, and has lots of tips: https://www.devever.net/~hl/incbin

The image background on that webpage is extremely distracting

Make your browser width narrow, and it drops to the bottom. Not that you should have to :)

Re: Embedding Binary Objects in C

#68

I needed this last week! Building an embedded firmware image for a dashboard display, with lots of PNG files for icons etc. 61 of them. The original developer wrote a tool to expand the PNGs to BMPs (arrays of 32-bit pixel values) and generate a C array definition as a text file. Which is lots bigger than the original PNG (13K => 100K sort of thing). Then included that C source in the build. Used up 700K of my firmwa…

Nope. Dumb Visual Studio doesn't support embedded binary in cross-compile. That's the tool the client wanted, so I'm stuck with it. Funny, they (VisualGDB) even have a tutorial on doing it. But the option 'Embedded Resource' doesn't exist in my (modern) Visual Studio. Ha! And the feedback I gave VisualGDB about their outdated tutorial, they responded "Just pay HERE and we'll be glad to solve your problem!" They can g…

Could you not try using the objcopy method mentioned here outside Visual Studio (or somewhere before the build process) and then just using the .o at link time, just referencing it from the outside at linkage? I expect at least a way to:

- hook custom tool calls

- link external objects into the project

Also, hello fellow automotive embedded SW eng! I know your suffering! :))

Re: Embedding Binary Objects in C

#70
post #59

Totally not relevant for this post, but this is how we do this in Nim: const a = readFile("mydata") The `const` makes the expression evaluate at compile time, conveniently slurping the file into `a`, where it will be available at run time.

That is pretty cool, but also not the behaviour I'd expect coming from other languages where this would be "get the contents of this file from the current directory of my runtime environment, and assign them to immutable variable `a`."
Post reply on HN