Live data from Hacker News

Embedding Binary Objects in C

flak.tedunangst.com

91–100 of 141 posts

Re: Embedding Binary Objects in C

#91
post #89
post #73

Earlier quoted context omitted.

Can you give an example of a use case where this is needed?

This is used for constructor & destructor functions in C code. The function pointers get put in an array in a certain section which is collated then the startup and exit code calls them. Ordering is not guaranteed.

Note that the ordering that the constructors and destructors will be run once the binary has been constructed is guaranteed by your C runtime.

Re: Embedding Binary Objects in C

#92

In Rust: `let foo = include_bytes!("path/to/file")`. And people wonder why I think C is difficult.

C does not have the feature (yet), it is not about being difficult or not.

The fact that C doesn't have the feature makes it difficult if I want to perform that task!

Re: Embedding Binary Objects in C

#93
post #58
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

Thanks for the link! That’s got a lot of goodies. One of us from Memfault also wrote a post about tips with Binutils, and this approach is covered in it. https://interrupt.memfault.com/blog/gnu-binutils#objcopy

TIL objdump --visualize-jumps…

Re: Embedding Binary Objects in C

#94
post #84
post #24

Earlier quoted context omitted.

For those wondering, the easiest way to do this is with: xxd -i filename.bin

Too bad that xxd doesn't include the const before the array decl. With const it saves a lot of memory.

I don't see how const would save memory, unless you have a separate ROM region that keeps that out of the main memory.

Re: Embedding Binary Objects in C

#95

Earlier quoted context omitted.

In C, accessing data outside the bounds of an object is undefined (like, you can’t legally “go past the end” of an array and end up in some other object). The “start” and “end” pointers, as far as the compiler is concerned, are totally different objects, so it may optimize a loop from one pointer to the other out since it’s impossible to increment an address so it’ll go from pounding at one thing to another.

So is the idea that just because the two objects happen to appear sequentially in memory under a certain implementation, the compiler (or linker in this case?) has no obligation to ensure that assumption holds?

Correct. (Although, the pointers being defined that way seems to make it quite unlikely that the compiler could optimize this incorrectly…)

Re: Embedding Binary Objects in C

#96

Earlier quoted context omitted.

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.

You're right, you can manually specify the ordering of object files in the linker file. Though, like you say, this is rarely done because of how brittle it is.

I was referring more to the way shown above, where I've seen a linker change the list of object files from a sorted list of filenames, to a hash table of filenames, which obviously changed the ordering with which it iterated across all object files.

Re: Embedding Binary Objects in C

#97

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…

imagemagick and netpbm can do that. Gimp too.

Re: Embedding Binary Objects in C

#98
post #90

According to https://news.ycombinator.com/item?id=22865842 #embed proposal is in the review process http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2499.pdf . This similar (same?) proposal is also presented and in the review at WG21 to be included in C++ standard. The author puts a lot of work towards making this proposal a reality (as far as I as a casual twitter/slack observer can see) and I'm looking forward to i…

Unfortunately, the proposal has been stalled by the C++ committee and the author is uninterested in continuing it. See their post here https://thephd.github.io/full-circle-embed

Is Circle something that is actually coming to C++?

Re: Embedding Binary Objects in C

#99
I am in India and I cannot access the site. The DNS resolves to `23.227.131.12` but the server doesn't seem to respond to pings either.

I can access the site through a VPN.

Anyone else facing the same issue? Anyone know what's up with that?

Re: Embedding Binary Objects in C

#100
post #24

Earlier quoted context omitted.

For those wondering, the easiest way to do this is with: xxd -i filename.bin

Yes, so long as you're willing to take a build-dep on vim. An od+awk+sed+sh combo will get you there from a POSIX base—and you have to add some wrapper text around the output of xxd anyway…

xxd exists in standalone form. Compile it.

https://github.com/ConorOG/xxd

Post reply on HN