-2

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;
    }
4

1 回答 1

1

我的代表不足以让我添加评论。

正如 Chris Stratton 所说,您的问题听起来模棱两可(仍在编辑后!)。你想要_____吗

  1. 编写一个引导加载程序,它将加载“Hello, World”而不是真正的操作系统?<--Actual Problem is saying this OR

  2. 编写一个程序,该程序将在操作系统上运行(因此将有完整的操作系统),并使用该程序加载另一个可执行文件?<--Comments are saying this

取决于此,答案会有所不同。

在第一种情况下,BIOS 上存在引导加载程序,它将一些预定义的内存块获取到 RAM。所以你需要做的就是把你Hello, World放在这个地方。关于这个有很多事情,比如链加载等等,但不确定这是否是你想要实现的。如果这不是您想要的,为什么要bootstrap使用标签?

在第二种情况下,fork() + exec()将为您完成。但是要确保这样,会有两个不同的地址空间。如果您希望它们在同一个地址空间中,我对daily used OS(for normal guys). 您的大部分内容听起来像是您想要做的。

如果您想问与此不同的问题,请编辑几乎整个问题并只问那部分。(避免告诉您为什么要尝试做某事,您认为您已经了解了什么等)

于 2013-09-26T07:56:53.033 回答