Differences

This shows you the differences between two versions of the page.

Link to this comparison view

wiki:embedding_resources_in_executables [27.03.2013 06:54]
81.57.94.90 [Object Files]
wiki:embedding_resources_in_executables [10.01.2018 15:57] (current)
rgareus [Alternative option using asm]
Line 68: Line 68:
 The "[..] ''g  .data '' [...] ''<name>''" part should ring a bell. This data-section can be referenced from the C code simply by using: The "[..] ''g  .data '' [...] ''<name>''" part should ring a bell. This data-section can be referenced from the C code simply by using:
  
-  extern const unsigned char *_binary_example_jpg_start;+  extern const unsigned char _binary_example_jpg_start[];
  
 and the length can be calculated from and the length can be calculated from
  
-  extern const unsigned char *_binary_example_jpg_start; +  extern const unsigned char _binary_example_jpg_start[]
-  extern const unsigned char *_binary_example_jpg_end;+  extern const unsigned char _binary_example_jpg_end[];
   size_t len = _binary_example_jpg_end - _binary_example_jpg_start;   size_t len = _binary_example_jpg_end - _binary_example_jpg_start;
  
Line 102: Line 102:
  is recognized by the gcc compiler on OSX. It produces the same result as calling ''getsectbyname()->addr''. Short of reading the actual code, information about osx linker internals is not easy to come by. ''getsectbyname()'' actually opens the executable file and searches the relevant data section while the application is running. ''_section$'' may or may not already be resolved at link-time for a given architecture ((should you know more about this or have any pointers to documentation, please contact me)).  is recognized by the gcc compiler on OSX. It produces the same result as calling ''getsectbyname()->addr''. Short of reading the actual code, information about osx linker internals is not easy to come by. ''getsectbyname()'' actually opens the executable file and searches the relevant data section while the application is running. ''_section$'' may or may not already be resolved at link-time for a given architecture ((should you know more about this or have any pointers to documentation, please contact me)).
  
 +Update (Oct 2016 - Thanks to Eugene Gershnik):  On newer versions of OSX/macOS that run executables with [[wp>Address_space_layout_randomization|ASLR]], the call to `getsectbyname` needs to be replaced with `getsectiondata` ((`getsectiondata` is a drop-in replacement, see also http://stackoverflow.com/questions/28978788/crash-reading-bytes-from-getsectbyname)). However this API is only available from OS 10.7 onwards. -- 
 ===== Architecture Abstraction -- working solution ===== ===== Architecture Abstraction -- working solution =====
  
Line 161: Line 161:
 A complete project that uses this approach to include a jpeg image file and a javascript text file is [[https://github.com/x42/harvid|harvid]]. It also outlines how to use a x-platform ''Makefile'' for creating the object files and adding the relevant flags to the OSX gcc command. A complete project that uses this approach to include a jpeg image file and a javascript text file is [[https://github.com/x42/harvid|harvid]]. It also outlines how to use a x-platform ''Makefile'' for creating the object files and adding the relevant flags to the OSX gcc command.
  
 +===== Alternative option using asm =====
 +
 +User ColaEuphoria points out that an alternative for x86 architecture is to use assembly 
 +
 +  section .rodata
 +  
 +  global _my_data;
 +  
 +  _my_data:     incbin "my_data.file"
 +  _my_data_size:    dd $-_my_data
 +  
 +and compile it with `''nasm -felf64 resource.s -o resource.o''` (Note that ''-felf64'' here is Linux 64bit. The options needs to be replaced with the corresponding target architecture and OS).
 +
 +The data can then be referenced using
 +
 +  extern const unsigned char _my_data[];
 +
 +with gcc. MSVC does not need the leading underscore in c-code and one can reference it using ''my_data''.
  
 {{tag>development article}} {{tag>development article}}
 
wiki/embedding_resources_in_executables.1364363662.txt.gz · Last modified: 27.03.2013 06:54 by 81.57.94.90