delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2005/12/31/19:27:52

X-Spam-Check-By: sourceware.org
Message-ID: <43B72200.8140861A@dessent.net>
Date: Sat, 31 Dec 2005 16:27:44 -0800
From: Brian Dessent <brian AT dessent DOT net>
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: executable = exe + data
References: <uek3vqivg DOT fsf AT gnu DOT org> <43B7186A DOT 8CE0CB91 AT dessent DOT net>
X-IsSubscribed: yes
Reply-To: cygwin AT cygwin DOT com
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

Brian Dessent wrote:

> All the methods mentioned so far are essentially hacks working against
> the linker, doing stuff behind its back.  Why not go with the flow?  Put
> your data in its own section, and write a linker script to handle that
> section in the desired way.  You can access the address by referencing
> the linker script variables in your source code.  See section 3 of the
> ld manual, particularly 3.5.3.  This should work on any platform that
> uses ld.

Even simpler, use objcopy -B to convert raw binary data to an object
file.

$ echo -ne "this is some binary data\0" >data.bin

$ od -A x -v -t x1z -w12 data.bin
000000 74 68 69 73 20 69 73 20 73 6f 6d 65  >this is some<
00000c 20 62 69 6e 61 72 79 20 64 61 74 61  > binary data<
000018 00                                   >.<
000019

$ cat >prog.c <<EOF
#include <stdio.h>

extern long binary_data_bin_start, binary_data_bin_end, 
            binary_data_bin_size;

int main()
{
  printf("data starts at %x, ends at %x, with size %ld: \"%s\"\n", 
      &binary_data_bin_start, &binary_data_bin_end,
      &binary_data_bin_size, &binary_data_bin_start);
}
EOF

$ objcopy -B i386 -I binary -O pe-i386 data.bin data.o

$ gcc -c prog.c

$ gcc -o prog data.o prog.o

$ ./prog
data starts at 402000, ends at 402019, with size 25: "this is some
binary data"

This works on linux also, except you must use "-O elf32-i386" instead of
"-O pe-i386" and you must use a leading underscore on the symbol names. 
There's probably a way to abstract these differences away in the build
system.

Brian


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019