Gentoo Archives: gentoo-embedded

From: Arkadi Shishlov <arkadi.shishlov@×××××.com>
To: gentoo-embedded@l.g.o
Subject: Re: [gentoo-embedded] gcc question - entry point problem in small, static app
Date: Mon, 30 Mar 2009 10:53:11
Message-Id: 20090330135244.42d46c2a@hal9000.mebius.lv
In Reply to: Re: [gentoo-embedded] gcc question - entry point problem in small, static app by Martin Guy
1 On Mon, 30 Mar 2009 11:15:30 +0100 Martin Guy <martinwguy@×××××.it> wrote:
2 > On 3/30/09, Christopher Friedt <chrisfriedt@×××××.com> wrote:
3 > > I'm writing code in C for a very small static binary, and the code
4 > > doesn't use any headers or rely on any other libraries.
5 >
6 > > I've noticed that there is a lot of static libc junk built into my
7 > > binary build my program with options like -nostdlib -nodefaultlibs
8 > > -nostartfiles, I get an error that says
9 > >
10 > > warning: cannot find entry symbol _start; defaulting to
11 > > 000000000000008074
12 >
13 > These days it is fairly hard, and system- and architecture-dependent,
14 > to get a minimal binary from a C program.
15
16 I don't know if arm is different, but I used the following on i386 to get flat binary that does not require any runtime support:
17 1. provide void _start(void) instead of main()
18 2. use LD and OBJCOPY directly:
19
20 IMAGE = image
21 IMAGE_OFFSET = 0x20000
22 CC = gcc
23 LD = ld
24 OBJCOPY = objcopy
25 CFLAGS = -march=i486 -O2 -fomit-frame-pointer -pipe -Wall
26 LDFLAGS = -m elf_i386 -Ttext $(IMAGE_OFFSET) -T
27 OBJCOPYFLAGS = -O binary
28 LIBS = ./../libucos.a /usr/lib/libc.a
29 INCL = -I./../
30 OBJS = main.o
31 all: $(IMAGE)
32 $(IMAGE): $(OBJS) $(LIBS) ./../_startup.o
33 $(LD) $(LDFLAGS) $(LIBS) $(OBJS) -o $(IMAGE).linked
34 $(OBJCOPY) $(OBJCOPYFLAGS) $(IMAGE).linked $(IMAGE)
35 @chmod -x image
36 @rm -f $(IMAGE).linked
37
38 3. ucos-ldscript linker script is stock ldscript modified to
39
40 SEARCH_DIR(/home/arkadi/work/ucos-gcc);
41 STARTUP(_startup.o) # my _start() is in _startup.c
42 comment out all ALIGN()
43
44 use different offset (that depends on loader and memory layout)
45
46 SECTIONS
47 {
48 /* Read-only sections, merged into text segment: */
49 /*. = 0x08048000 + SIZEOF_HEADERS;*/
50 . = 0x00020000 + SIZEOF_HEADERS;
51
52 Time has passed since than, so things may changed.

Replies