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.
Embedding Binary Objects in C
91–100 of 141 posts
Re: Embedding Binary Objects in C
#92Re: Embedding Binary Objects in C
#93There'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
Re: Embedding Binary Objects in C
#94Earlier 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.
Re: Embedding Binary Objects in C
#95Earlier 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?
Re: Embedding Binary Objects in C
#96Earlier 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.
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
#97I 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…
Re: Embedding Binary Objects in C
#98According 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
Re: Embedding Binary Objects in C
#99I 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
#100Earlier 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…