rules_cc icon indicating copy to clipboard operation
rules_cc copied to clipboard

FR: capability to embed binary data in CC object file.

Open aiuto opened this issue 5 years ago • 3 comments

It is often extremely useful to embed a generated (or static) binary object directly in a C++ library. For example, the ICU library build compiles text files describing character attributes into compressed binary structures that can be evaluated at run time. The canonical technique is to

  • generate a data structure that is appropriate for the target platform (in byte alignment and endian-ness)
  • wrap it up just enough so that it becomes a linkable object (.o, .obj) file

Typically, the object file defines at least 2 symbols unit8_t _DATA_OBJECT_NAME_start[] unit32_t _DATA_OBJECT_NAME_size Those are declared in a generated .h file and provided by the object module.

Problem scope

  • needs to support at least COFF & ELF object formats
  • needs to work for *nix and Windows
  • there may be a need to specify what section the data is in
  • it may be useful to pack multiple blobs into a single .o file
  • generating a large C or Assembler file to compile with the CC toolchain does not work. Large blobs can easily generate text files that hit compiler limits.

There is an interesting overview of issues and techniques here: https://www.devever.net/~hl/incbin

aiuto avatar Feb 25 '19 22:02 aiuto

Any suggestions on this?

bkotsopoulossc avatar Apr 08 '21 03:04 bkotsopoulossc

A partial solution (hits about half the bullets under "Scope"): https://github.com/bcsgh/bazel_rules/tree/master/cc_embed_data

I've only tested that on the limited environments I've needed it in (Linux/ELF) so YMMV. If anyone wants to add Win/COFF I'd accept pull requests.

bcsgh avatar Sep 29 '21 15:09 bcsgh

Related: https://github.com/bazelbuild/rules_cc/issues/41.

aiuto avatar Sep 29 '21 16:09 aiuto