3

I am using flat binary files as external programs for my OS. When I compile them, like so:

gcc -Wall ctest.c -o ctest.bin -nostdlib -Wl,-Ttext=0x8000,-nostdlib -masm=intel
objcopy -O binary -j .text ctest.bin ctest

But doing this, the contents of the character array aren't put in the file. This is my code:

static volatile char string[4] = "Hi!\0";
static volatile char string2[15] = "Hello, World!\n\0";

int _start()
{
    asm("mov eax, [string]");
    asm("mov ebx, 0x00");
    asm("int 0x01");
    asm("mov eax, [string2]");
    asm("mov ebx, 0x00");
    asm("int 0x01");
    return 0;
}

and when I run objdump (I ran it on the elf, but I verified it had the same code as this):

00008000 <_start>:
8000:   55                      push   ebp
8001:   89 e5                   mov    ebp,esp
8003:   a1 70 90 00 00          mov    eax,ds:0x9070
8008:   bb 00 00 00 00          mov    ebx,0x0
800d:   cd 01                   int    0x1
800f:   a1 74 90 00 00          mov    eax,ds:0x9074
8014:   bb 00 00 00 00          mov    ebx,0x0
8019:   cd 01                   int    0x1
801b:   b8 00 00 00 00          mov    eax,0x0
8020:   5d                      pop    ebp
8021:   c3                      ret    

As you can see, the text is nowhere to be found. I was hoping it would do something like this: string db "Hi!", 0 which I would do with nasm.

What should I do so it includes the characters in the output bin file without coding this in assembly?
Thanks in advance.

5
  • Added some more useful tags Commented Dec 28, 2012 at 6:55
  • @GrijeshChauhan Thanks, but would this really qualify as embedded? As this is running on a regular PC. Commented Dec 28, 2012 at 7:02
  • Yes, But I added so that can be noticed people working for embedded. I think they can also help you on this question :) Commented Dec 28, 2012 at 7:10
  • @GrijeshChauhan: doesn't that only examine files though? I cant find anywhere a way to create a binary with it. Commented Dec 28, 2012 at 18:04
  • What flag are you giving to objdump? Commented Feb 13, 2013 at 9:08

2 Answers 2

4

A binary executable file is typically divided into sections. Your strings have simply been placed into a different section than the code. This makes sense, since the code should be read-only but the strings have been declared non-const and volatile.

Sign up to request clarification or add additional context in comments.

6 Comments

Is there any way to force them into the same section (text I think) before/while doing the objcopy?
@farlepet: you could add a -j .data to the objcopy arguments to get the strings into the binary.
@MichaelBurr: When i do that, there is a 4000 byte gap between the text and the data sections, is there a way to remove that without doing it manually?
@farlepet, try just declaring the strings const and removing volatile. It's still up to the compiler where they go, but the chances they will be placed in the text section increases.
@MarkRansom: I tried that, but that doesn't include them in the .text section, and now when i use objcopy with -j .data, it doesn't add anything after the text section
|
1

I figured out how to do it. First I created a linker script like this (You can change phys to whatever address you want to load it at):

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x8000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(0);
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    . = ALIGN(0);
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    *(.bss)
    . = ALIGN(0);
  }
  end = .;
}

Then compiled and linked the executable like this:

gcc -m32 -nostdlib -nostdinc -fno-builtin -o exec.o exec.c
ld -T link.ld -melf_i386 -o exec.bin exec.o

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.