Trouble with extern variable in 64 bit flat binary
Posted: Tue Nov 25, 2014 9:28 am
Enviorment : Linux Fedora 20
gcc version 4.8.3 20140624 (Red Hat 4.8.3-1)
my makefile is mk
my linker srcipt is LinkerScript_02
I have declared a var x as extern in test.h
define x in test.c
use make -f mk test.bin flat binary
disasemble it with ndisasm -b 64 test.bin >> test.nds
file test.h:
extern unsigned long int x ;
------------------------------
file test.c:
#include "test.h"
__attribute__((section(".data"))) unsigned long int x = 0x123456789abcdef;
void *_start(void)
{
unsigned long y = 0x123456789ABCDEF;
x = (unsigned long)&y;
return((void*)x);
}
------------------
make file
file mk:
CC = gcc
SOURCES = test.c
OBJ = test.o
BIN = test.bin
#use this to intel syntax
CFLAGS = -masm=intel -m64 -fno-builtin -nodefaultlibs -nostdinc -nostdlib -nostartfiles -fno-exceptions -fno-stack-protector -s -O2
#liker options
LFLAGS = -fdce -fdse -free -pie -fpic -s -static -T LinkerScript_02 -nostartfiles -nostdlib -nostdinc -nodefaultlibs -Wl,--oformat,binary -Wl,-e0x0 -Wl,-N -Wl,-q
$(BIN): $(OBJ)
$(CC) $(LFLAGS) $(OBJ) -o $(BIN)
$(OBJ): $(SOURCES)
$(CC) $(CFLAGS) $(SOURCES) -o $(OBJ)
--------------
Linker script
file LinkerScript_02:
ENTRY(start)
start = 0x0;
OUTPUT_FORMAT(binary)
SECTIONS
{
. = 0x0;
.text . :
{
*(.text) ;
}
.data . :
{
*(.data) *(.rodata) *(.bss) *(COMMON)
}
/DISCARD/ : { *(.got.plt) *(.shstrtab) *(.note.gnu.build-id ) *(.xdata) *(.pdata) *(.comment) *(.note.GNU-stack) *(.eh_frame) *(.eh_frame_hdr) }
}
---------------------------------------------------
make -f mk procuces test.bin
ndisasm -b 64 test.bin >> test.nds
-------------------------------------
test.nds:
disasembled code:
00000000 488D4424F8 lea rax,[rsp-0x8]
00000005 488905740E2000 mov [rel 0x200e80],rax
0000000C C3 ret
0000000D 0000 add [rax],al
0000000F 00EF add bh,ch
00000011 CDAB int 0xab
00000013 896745 mov [rdi+0x45],esp
00000016 2301 and eax,[rcx]
note : x address is 0x200e80 +IP
x value is stored at 0x11
I expected x addess at 0x11
How I can set the address of x to 0x11 ??
gcc version 4.8.3 20140624 (Red Hat 4.8.3-1)
my makefile is mk
my linker srcipt is LinkerScript_02
I have declared a var x as extern in test.h
define x in test.c
use make -f mk test.bin flat binary
disasemble it with ndisasm -b 64 test.bin >> test.nds
file test.h:
extern unsigned long int x ;
------------------------------
file test.c:
#include "test.h"
__attribute__((section(".data"))) unsigned long int x = 0x123456789abcdef;
void *_start(void)
{
unsigned long y = 0x123456789ABCDEF;
x = (unsigned long)&y;
return((void*)x);
}
------------------
make file
file mk:
CC = gcc
SOURCES = test.c
OBJ = test.o
BIN = test.bin
#use this to intel syntax
CFLAGS = -masm=intel -m64 -fno-builtin -nodefaultlibs -nostdinc -nostdlib -nostartfiles -fno-exceptions -fno-stack-protector -s -O2
#liker options
LFLAGS = -fdce -fdse -free -pie -fpic -s -static -T LinkerScript_02 -nostartfiles -nostdlib -nostdinc -nodefaultlibs -Wl,--oformat,binary -Wl,-e0x0 -Wl,-N -Wl,-q
$(BIN): $(OBJ)
$(CC) $(LFLAGS) $(OBJ) -o $(BIN)
$(OBJ): $(SOURCES)
$(CC) $(CFLAGS) $(SOURCES) -o $(OBJ)
--------------
Linker script
file LinkerScript_02:
ENTRY(start)
start = 0x0;
OUTPUT_FORMAT(binary)
SECTIONS
{
. = 0x0;
.text . :
{
*(.text) ;
}
.data . :
{
*(.data) *(.rodata) *(.bss) *(COMMON)
}
/DISCARD/ : { *(.got.plt) *(.shstrtab) *(.note.gnu.build-id ) *(.xdata) *(.pdata) *(.comment) *(.note.GNU-stack) *(.eh_frame) *(.eh_frame_hdr) }
}
---------------------------------------------------
make -f mk procuces test.bin
ndisasm -b 64 test.bin >> test.nds
-------------------------------------
test.nds:
disasembled code:
00000000 488D4424F8 lea rax,[rsp-0x8]
00000005 488905740E2000 mov [rel 0x200e80],rax
0000000C C3 ret
0000000D 0000 add [rax],al
0000000F 00EF add bh,ch
00000011 CDAB int 0xab
00000013 896745 mov [rdi+0x45],esp
00000016 2301 and eax,[rcx]
note : x address is 0x200e80 +IP
x value is stored at 0x11
I expected x addess at 0x11
How I can set the address of x to 0x11 ??