Conversation
Summary of ChangesHello @waruqi, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a significant enhancement to Xmake by adding a Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new bin2obj rule, which is a great performance improvement over bin2c. The implementation for the COFF format is well-done. However, the feature is incomplete as it lacks support for ELF and Mach-O formats, which will cause builds to fail on Linux and macOS. This is a critical issue that needs to be addressed. I've also provided several suggestions to improve the C implementation's robustness and maintainability, such as handling files larger than 4GB and using structs for binary data serialization to improve code clarity.
Compared to bin2c, it skips codegen and code compilation, directly generates the object file (coff, elf, macho), and then performs linking.
Therefore, it will be quite fast.
#7099 (comment)
Test results on macOS. xmake.ico is a 120MB file.
bin2obj
1.8s
bin2c
354.636s
bin2obj - Binary to Object File Converter
Overview
The
bin2objrule converts binary files directly into object files that can be linked into your program, eliminating the need for compilation. This is more efficient than the traditionalbin2capproach, as it avoids the compilation step and allows direct linking of binary data.Features
Supported Formats
COFF (Common Object File Format)
.obj_binary_becomes__binary_) to match the C compiler's behaviorELF (Executable and Linkable Format)
.o.note.GNU-stacksection to mark stack as non-executableMach-O (Mach Object)
.oBasic Usage
1. Add the Rule
Add the
utils.bin2objrule to your target:2. Use the Binary Data in C/C++
The binary file is converted to an object file with two symbols:
_binary_<basename>_start: Pointer to the start of the data_binary_<basename>_end: Pointer to the end of the dataExample:
Configuration Options
Rule-Level Configuration
Configure options for all binary files in the target:
File-Level Configuration
Configure options for individual files:
Custom File Extensions
By default, the rule processes
.binfiles. You can specify additional extensions:Advanced Usage
Manual Format Specification
Force a specific object file format:
Custom Symbol Prefix
Use a custom prefix for symbol names:
Then in C code:
Null Terminator Control
Control whether a null terminator (
\0) is appended to the data:Symbol Naming
The symbol names are generated from the filename:
data.bin→ Symbols:_binary_data_bin_start,_binary_data_bin_endimage.png→ Symbols:_binary_image_png_start,_binary_image_png_endconfig.json→ Symbols:_binary_config_json_start,_binary_config_json_endDots (
.) in the filename are replaced with underscores (_) in the symbol name.Special Case: i386 on Windows
On i386 Windows (MinGW), the C compiler automatically adds an underscore prefix to external symbols. Therefore:
_binary_data_bin_start__binary_data_bin_start(two underscores)The
bin2objrule automatically handles this by using__binary_as the default prefix for i386.Command-Line Usage
You can also use
bin2objdirectly from the command line:Command-Line Options
-i, --binarypath: Input binary file path (required)-o, --outputpath: Output object file path (required)-f, --format: Object file format (coff,elf,macho) (required)-a, --arch: Target architecture (e.g.,x86_64,arm64,i386) (required)-p, --plat: Target platform (e.g.,linux,macosx,windows) (required)--symbol_prefix: Symbol prefix (default:_binary_)--target_minver: Target minimum version for Mach-O (e.g.,10.0,18.2)--xcode_sdkver: Xcode SDK version for Mach-O (e.g.,10.0,18.2)--zeroend: Append a null terminator at the end of dataExamples
Supported Architectures
x86/x86_64
i386,x86→ 32-bitx86_64,x64→ 64-bitARM
arm,armv7,armeabi-v7a,armv6,armv5→ 32-bit ARMarm64,aarch64,arm64-v8a→ 64-bit ARMMIPS
mips,mipsel→ 32-bit MIPSmips64,mips64el→ 64-bit MIPSRISC-V
riscv→ 32-bit RISC-Vriscv64→ 64-bit RISC-VPowerPC
ppc,powerpc→ 32-bit PowerPCppc64,powerpc64→ 64-bit PowerPCSPARC
sparc→ 32-bit SPARCsparc64→ 64-bit SPARCOthers
s390x→ IBM z/Architectureloongarch,loongarch64,loong64→ LoongArchwasm32→ WebAssembly 32-bitwasm64→ WebAssembly 64-bitsh,sh4,superh→ SuperHia64,itanium→ IA-64 (Itanium)Complete Example
Comparison with bin2c
Notes
File Size Limit: The current implementation supports files up to 4GB (32-bit size limit for COFF format).
Symbol Visibility: All symbols are exported as global symbols, making them accessible from other object files.
Data Alignment: Data is aligned according to the target architecture's requirements (4 bytes for 32-bit, 8 bytes for 64-bit).
Platform-Specific Behavior:
.note.GNU-stacksection to prevent executable stack warningsPerformance: Direct object file generation is faster than the
bin2capproach, especially for large files, as it skips the compilation step.Troubleshooting
Undefined Symbol Errors
If you get undefined symbol errors, check:
__binary_prefix (two underscores) instead of_binary_Format Detection Issues
If the wrong format is detected, explicitly specify the format:
Large File Issues
For files larger than 4GB, consider splitting the file or using the
bin2capproach instead.See Also