-3

As I understand, the core of a boot loader is a loader program. By loader, I mean the program that will load another program. Or to be more specific first it will load itself then the high level image - for example kernel. Instead of making a bootloader, I thought to clear my doubts on loader by running on an OS that will load another program. I do understand that every process map is entirely independent to another. So, what I am trying to do is make a simple program hello_world.c this will print the great "hello world". Now, I want to make a loader program that will load this program hello world. As I understand the crux is in two steps

  1. Load the hello world program on the RAM - loader address.
  2. JMP to the Entry Address.

Since, this is to understand the concept, I am using the readymade utility readelf to read the address of the hello world binary. The intention here is not to make a ELF parser. As all the process are independent and use virtual memory. This will fail, If I use the virtual memory addresses. Now, I am stuck over here, how can I achieve this?

#include "stdio.h"
   #include <sys/mman.h>

    int main( int argc, char **argv)
    {
      char *mem_ptr;
      FILE *fp;

      char *val;
      char *exec;

      mem_ptr = (char*) malloc(10*1024);
      fp = fopen("./hello_world.out","rb");

      fread(mem_ptr, 10240, 1, fp);

      //val = mem_ptr + 0x8048300;

      printf("The mem_ptr is %p\r\n",mem_ptr);


    exec = mmap(NULL, 10240, PROT_READ | PROT_WRITE | PROT_EXEC,
                      MAP_PRIVATE | MAP_ANONYMOUS, 0x9c65008, 0);


      memcpy(mem_ptr,exec,10240);


     __asm__("jmp 0x9c65008");

     fclose(fp);


      return 0;
    }
Siguza
  • 21,155
  • 6
  • 52
  • 89
dexterous
  • 6,422
  • 12
  • 51
  • 99
  • 1
    How do you expect to load an elf to an executable memory image without parsing it??? – Chris Stratton Sep 24 '13 at 15:48
  • using readelf. I don't want to get into parsing elf in the code. Since I am understanding the concept here. – dexterous Sep 25 '13 at 06:14
  • But that's not what you describe in your question, so how did you expect anyone to help? Likely the tool you want is objcopy to make a flat binary - as often used before flashing an embedded micro. But realize on Linux you will have trouble not dealing with the dynamic linker. – Chris Stratton Sep 25 '13 at 12:33
  • I was trying to explain the same but I was discouraged with -2 ratings. So, I have just kept quite. Also, It was updated to me that I don't know the very obvious concepts. I really doubt on it. It is a good question, If someone knows how to load another program then anyone can write the bootloader. The crux of bootloader is the loader program only. Load itself and load other program. In case, you can edit my question that will be great to attract the answers. I am not good in English. – dexterous Sep 26 '13 at 02:12
  • You got the 2nd downvote only *after* your comment explaining that you were trying do something *other* than what your question said, so that cannot be the excuse for why you didn't state what you were doing in the first place. Correct your question (especially it's title, which incorrectly says elf) to specify what you are *actually trying to do* and I'll think about revoking it. – Chris Stratton Sep 26 '13 at 02:59
  • How should I reword it? I am clear that, I want to make a loader program that will load another program. This is to clear the concept not to write an elf parser.For getting the address, I can use readymade utility- readelf. – dexterous Sep 26 '13 at 03:19
  • You need to do a lot more than "get an address". I suggest you compile a trivially simple program and thoroughly examine it with objdump until you understand exactly what all of the parts of the elf are there to do, and what you would need to do with them to load the program into memory in usable form. Where you using a system where static linking were practical, the task would be simpler - you could just objcopy the elf to a flat binary file and load that instead. – Chris Stratton Sep 26 '13 at 03:23
  • Let's say, I have a program hello world.c this prints helloworld. The binary is helloworld.out. I can know about the sections of helloworld through readelf -s or objdump . Anything is possible. Now, after reading this I will get the text address. This is nothing but load address. Now, I am making another program that will load the helloworld.out on the load address read in elf and will jump on it. – dexterous Sep 26 '13 at 03:36
  • adding further , ran out of space. This won't work actually because these addresses are virtual, and every process has a different process map.By reading elf or objdump, we get the virtual address. I need to do something more, what I need to do is what I am missing.So, to conclude the steps are like 1) load the binary to the code segment address - starting address (obviously). 2) JMP to the load address. I want to discuss this further, but -2 vote discourages me. I have not appreciated Objcopy much, it's job is to copy the complete elf for sections of elf to another copy. How is it used here? – dexterous Sep 26 '13 at 03:37
  • @ChrisStratton : I have dared to reword this problem again. – dexterous Sep 26 '13 at 04:51

1 Answers1

0

my rep is not enough to let me add comments.

As Chris Stratton said, your problem sounds ambiguous(still after editing!). Do you want to

  1. Write a bootloader, that will load "Hello, World" instead of real OS? <--Actual Problem is saying this OR

  2. Write a program, that will be running on OS(so full fledged OS will be there), and load another executable using this program?<--Comments are saying this

Answers will vary a lot depending on this.

In first case, bootloader is present on BIOS, that will fetch some predefined memory block to RAM. So what u need to do is just place your Hello, World at this place. There are many things regarding this, such as chain loading and all, but not sure if this is what you want achieve. If this is NOT something you wanted, why is bootstrap tag used?

In second case, fork() + exec() will do it for you. But be sure that this way, there will be two different address spaces. If you want them in the same address space, I am doubtful about daily used OS(for normal guys). Most of the your part sounds like this is what you want to do.

If you want to ask something different than this, please edit almost entire question and ask ONLY that part.(Avoid telling why you are trying to do something, what you think you already understand etc)