Giter Site home page Giter Site logo

taletn / binobj Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 1.0 8 KB

Binary to object (OBJ) file Windows dev tool.

License: Do What The F*ck You Want To Public License

Makefile 10.16% C 89.84%
binary object windows developer-tool devtool developer-tools development-tools devtools obj object-file

binobj's Introduction

Binary to object Windows dev tool

Converts a binary file to a machine-independent Windows object (OBJ) file, so you can link the binary file with your own code.

How to build & run

  1. Open the Command Prompt for VS (Visual Studio).

  2. Run the Program Maintenance Utility:

    nmake

  3. Convert your binary file to an object file:

    binobj.exe MyArray 16 my_data.bin my_data.obj

    Replace MyArray with the symbol name you want to use for your data, and replace 16 with the alignment (in bytes) that is appropriate for your data.

  4. Add this line to your own source code to declare the data as external:

    extern "C" const unsigned char MyArray[1024*1024];

    Replace unsigned char with the POD type of your binary data, and replace 1024*1024 with the number of elements that are needed to hold your data. If you don't need to know the size at compile time, then you can also omit the number of elements.

  5. Link your own code against the object file, e.g.:

    cl my_program.cpp my_data.obj

Alternate solution for other platforms

This tool is Windows only, so it won't work on other platforms, like macOS or Linux. However, if your platform comes with the GNU Binary Utilities, and if it includes objcopy, then you could do the following:

  1. Create a C source file (my_data.c) that contains a single line:

    const unsigned char MyArray[1024*1024] = { 0 };

    Replace unsigned char with the POD type of your binary data, and replace 1024*1024 with the number of elements that are needed to hold your data.

  2. Compile this C file to create an object file containing zeros, and then use objcopy to replace the zeros with the actual binary data:

    gcc -fvisibility=hidden -c -o my_data.o my_data.c
    objcopy --update-section .rodata=my_data.bin my_data.o

  3. Add this line to your own source code to declare the data as external:

    extern "C" const unsigned char MyArray[1024*1024];

    If you don't need to know the size at compile time, then you can also omit the number of elements here.

  4. Link your own code against the object file, e.g.:

    g++ my_program.cpp my_data.o

Note that Apple's Xcode apparently doesn't include objcopy, but there is yet another way:

  1. Create an assembly source file (my_data.s) that contains the following lines:

    .static_const
    .align 4
    .globl _MyArray
    _MyArray: .incbin "my_data.bin"

    Replace .align 4 with the alignment that is appropriate for your data. Note that this alignment is in 2^n bytes, so .align 4 actually means 2^4 = 16 bytes.

  2. Assemble this assembly file to create an object file:

    as -o my_data.o my_data.s

  3. Add this line to your own source code to declare the data as external:

    extern "C" const unsigned char MyArray[1024*1024];

    If you don't need to know the size at compile time, then you can also omit the number of elements here.

  4. Link your own code against the object file:

    g++ my_program.cpp my_data.o

  5. Create a symbol file (my_symbols.exp) containing the following line:

    _MyArray

    Note that this file can also hold multiple lines/symbols, in case you have multiple binary files.

  6. Strip the symbols from your binary code:

    strip -R my_symbols.exp -i a.out

Note that you can also add the assembly file to your Xcode project, and then set Build Settings > Additional Strip Flags to -R my_symbols.exp -i, so you can build using Xcode.

License

Copyright © 2021-2024 Theo Niessink <[email protected]>
This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.

binobj's People

Contributors

taletn avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

retronick2020

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.