This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
Re: ld: Simple Linker Script Example
On Thursday 06 November 2003 10:23 pm, Ian Lance Taylor wrote:
> What kind of problems are you seeing?
What I'm trying to do is put all of the sections coming from ext.c into one
place in virtual memory.
Given two files:
/*
* main.c
*/
#include <stdio.h>
extern int ext(int x);
int main(){
return printf("%d\n",ext(1));
}
and
/*
* ext.c
*/
int ext(int x){
return ++x;
}
gcc -Wall -c main.c ext.c
gcc -Wall -Wl,--verbose main.o ext.o > script.ld 2>&1
Edit script.ld to remove everything that before or after the script proper.
gcc -Wall -Wl,-T script.ld main.c ext.c
Executable works -- great, I've got a baseline. Now change the script as
follows:
--- script.ld.orig 2003-11-06 22:45:57.000000000 -0500
+++ script.ld 2003-11-06 22:52:08.000000000 -0500
@@ -9,7 +9,24 @@
SECTIONS
{
/* Read-only sections, merged into text segment: */
- . = 0x08048000 + SIZEOF_HEADERS;
+ . = 0x08048000 + 0x200;
+
+ . += 0x1000;
+ . = ALIGN(0x1000);
+
+ .sandbox_text : { ext.o ( .text ) }
+ . += 0x1000;
+ . = ALIGN(0x1000);
+
+ .sandbox_data : { ext.o ( .data ) }
+ . += 0x1000;
+ . = ALIGN(0x1000);
+
+ .sandbox_bss : { ext.o ( .bss ) }
+ . += 0x1000;
+ . = ALIGN(0x1000);
+
+
.interp : { *(.interp) }
.hash : { *(.hash) }
.dynsym : { *(.dynsym) }
Finally,
gcc -Wall -Wl,-T script.ld main.o ext.o
No complaints from gcc, but the binary segfaults.
This is using dynamic linking -- I hadn't thought about going to static until
you mentioned it. Replacing SIZEOF_HEADERS with 0x200 is just a guess.
readelf -a looks reasonable as far as the addresses go:
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf
Al
[ 0] NULL 00000000 000000 000000 00 0 0
0
[ 1] .sandbox_text PROGBITS 0804a000 001000 00000b 00 AX 0 0
4
[ 2] .sandbox_data PROGBITS 0804c000 002000 000000 00 WA 0 0
4
[ 3] .sandbox_bss NOBITS 0804d000 002000 000000 00 WA 0 0
4
[ 4] .interp PROGBITS 0804e000 002000 000013 00 A 0 0
1
[ 5] .note.ABI-tag NOTE 0804e014 002014 000020 00 A 0 0
4
[ 6] .hash HASH 0804e034 002034 000028 04 A 7 0
4
.
.
.
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
PHDR 0x000034 0x00001034 0x00000000 0x00120 0x00120 R E 0x4
INTERP 0x002000 0x0804e000 0x0804e000 0x00013 0x00013 R 0x1
[Requesting program interpreter: /lib/ld-linux.so.2]
LOAD 0x001000 0x0804a000 0x0804a000 0x0000b 0x0000b R E 0x1000
LOAD 0x002000 0x0804c000 0x0804c000 0x00000 0x00000 RW 0x1000
LOAD 0x002000 0x0804d000 0x0804d000 0x00000 0x00000 RW 0x1000
LOAD 0x002000 0x0804e000 0x0804e000 0x00340 0x00340 R E 0x1000
LOAD 0x002340 0x0804f340 0x0804f340 0x00100 0x00104 RW 0x1000
DYNAMIC 0x00234c 0x0804f34c 0x0804f34c 0x000c8 0x000c8 RW 0x4
NOTE 0x002014 0x0804e014 0x0804e014 0x00020 0x00020 R 0x4
This is really obvious, right? I can feel it... ;-)
>
> Ian
Barry