Chinaunix首页 | 论坛 | 博客
  • 博客访问: 713563
  • 博文数量: 176
  • 博客积分: 2548
  • 博客等级: 少校
  • 技术积分: 1749
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-29 16:36
个人简介

爱咋咋地

文章分类

全部博文(176)

文章存档

2024年(1)

2023年(17)

2022年(19)

2021年(3)

2020年(1)

2018年(1)

2017年(1)

2014年(1)

2013年(4)

2012年(11)

2011年(19)

2010年(22)

2009年(71)

2008年(5)

分类: C/C++

2009-06-02 14:39:00

先贴5封gcc-help的邮件
/****************************** 1st ************************************/
Dear Gcc-help,

I want to embed an image into an executable.  I managed
to do it by running "ld -r -b binary -o blob.o blob.png"
on the blob.png file, thus creating a blob.o that I can
link into my main program.  I have access to the binary
blob by declaring an extern variable:

extern char _binary_blob_png_start [];

Normally I know the size of the blob I just linked in.
But suppose I didn't.  Is there a way I can determine
it from within the C program?  I know that by running
"objdump -x blob.o" I can see the symbol table on the
obj file, and that the symbols _binary_blob_png_end and
_binary_blob_png_size are also defined:

SYMBOL TABLE:
0000000000000000 l    d  .data  0000000000000000 .data
0000000000000400 g       .data  0000000000000000 _binary_blob_png_end
0000000000000000 g       .data  0000000000000000 _binary_blob_png_start
0000000000000400 g       *ABS*  0000000000000000 _binary_blob_png_size

I assumed that _binary_blob_png_end was a pointer to the
end of the blob, but it doesn't seem so.  Also, I always
get a segfault if I assume that _binary_blob_png_size is
an integer value.

Thank you!!
Jean
/****************************** 2nd ************************************/
_binary_blob_png_end is a pointer to the byte just past the blob;
subtracting _start from _end gets you the size.

_binary_blob_png_size is a weird symbol; symbols can only be addresses,
not data, so you can't reference the _size symbol directly. You must
take its address, then cast from a pointer to a size_t. However, I would
recommend using the _end - _start method instead, because the _size
symbol will get modified if it is located in a shared library and will
become useless.
/****************************** 3rd ************************************/
As you've observed, _binary_blob_png_size is the size.  To access it
from C, you have to write something like:
    extern char _binary_blob_png_size[];
    size_t size = (size_t) &_binary_blob_png_size[0];
It's an awkward translation from a linker symbol to a C value.

Ian
/****************************** 4th ************************************/
Hi,
Thank you all for the help!  I have tried both ways, and it
does work.  It didn't work before because I was trying to
use _end directly as pointer (my C is bit rusty).  Anyway,
I'll use the pointer subtraction method because, according
to Kevin, it is more robust than using size.

Regards,
Jean
/****************************** 5th ************************************/
Just another point to consider: objcopy can also be used to do this
process, and has the benefit of allowing you to rename the created data
section and change its flags; I do this to make the section named
'.rodata' and mark it READONLY, which will help with swapping and other
related things (since it won't have to be swapped out). This can also be
done using ld, but requires writing a small linker script to accomplish
the same result.

--
Kevin P. Fleming
Director of Software Technologies
Digium, Inc. - "The Genuine Asterisk Experience" (TM)
 
/********************************** 正文  **********************************/
今天我也准备做同样一个东西,不知道该怎样做,去网上google了半天也没有答案。搜出来的结果全都是嵌入式开发的东西,郁闷!被逼无奈去gcc主页上面搜索,一下就出来了,高兴的不得了。
现在准备试试看。结果以后发布
阅读(958) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~